Skip to content

Commit

Permalink
#197 [Session] add: clone action
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Feb 20, 2023
1 parent 075de07 commit 8e31099
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 188 deletions.
59 changes: 59 additions & 0 deletions class/saturnesignature.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,4 +738,63 @@ public function deletePreviousSignatories($role, $fk_object, $object_type)
return -1;
}
}

/**
* Clone an object into another one
*
* @param User $user User that creates
* @param int $fromid ID of object to clone
* @param int $objectID ID of object element to link with signatory
* @return int New object created, <0 if KO
* @throws Exception
*/
public function createFromClone(User $user, int $fromid, int $objectID): int
{
dol_syslog(__METHOD__, LOG_DEBUG);

$object = new self($this->db);

$this->db->begin();

// Load source object
$object->fetchCommon($fromid);

// Reset some properties
unset($object->id);
unset($object->fk_user_creat);
unset($object->import_key);
unset($object->signature_date);
unset($object->last_email_sent_date);

// Clear fields
if (property_exists($object, 'date_creation')) {
$object->date_creation = dol_now();
}
if (property_exists($object, 'fk_object')) {
$object->fk_object = $objectID;
}
if (property_exists($object, 'status')) {
$object->status = 1;
}
if (property_exists($object, 'signature')) {
$object->signature = '';
}
if (property_exists($object, 'signature_url')) {
$object->signature_url = generate_random_id();
}

// Create clone
$object->context['createfromclone'] = 'createfromclone';
$result = $object->createCommon($user);
unset($object->context['createfromclone']);

// End
if ($result > 0) {
$this->db->commit();
return $result;
} else {
$this->db->rollback();
return -1;
}
}
}
220 changes: 78 additions & 142 deletions class/session.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,148 +875,84 @@ public function setCategories($categories): int
return parent::setCategoriesCommon($categories, 'session');
}

// /**
// * Clone an object into another one
// *
// * @param User $user User that creates
// * @param int $fromid Id of object to clone
// * @param $options
// * @return mixed New object created, <0 if KO
// * @throws Exception
// */
// public function createFromClone(User $user, $fromid, $options)
// {
// global $conf, $langs;
// $error = 0;
//
// require_once __DIR__ . '/meeting.class.php';
// require_once __DIR__ . '/trainingsession.class.php';
// require_once __DIR__ . '/audit.class.php';
//
//
// switch ($this->type) {
// case 'meeting':
// $signatory = new TrainingSessionSignature($this->db);
// break;
// case 'trainingsession':
// $signatory = new MeetingSignature($this->db);
// break;
// case 'audit':
// $signatory = new AuditSignature($this->db);
// break;
// }
//
// $conf_mod = 'DOLIMEET_' . strtoupper($this->type) . '_ADDON';
// $refObjectMod = new $conf->global->$conf_mod($this->db);
//
// dol_syslog(__METHOD__, LOG_DEBUG);
//
// $object = new self($this->db);
//
// $this->db->begin();
//
// // Load source object
// $result = $object->fetchCommon($fromid);
// if ($result > 0 && ! empty($object->table_element_line)) {
// $object->fetchLines();
// }
//
// // Load signatory and ressources from source object
// $signatories = $signatory->fetchSignatory('', $fromid, $this->type);
//
// if ( ! empty($signatories) && $signatories > 0) {
// foreach ($signatories as $arrayRole) {
// foreach ($arrayRole as $signatoryRole) {
// $signatoriesID[$signatoryRole->role] = $signatoryRole->id;
// if ($signatoryRole->role == strtoupper($this->type) . '_EXTERNAL_ATTENDANT') {
// $extintervenant_ids[] = $signatoryRole->id;
// }
// }
// }
// }
//
// // Reset some properties
// unset($object->id);
// unset($object->fk_user_creat);
// unset($object->import_key);
//
// // Clear fields
// if (property_exists($object, 'ref')) {
// $object->ref = $refObjectMod->getNextValue($object);
// }
// if (property_exists($object, 'ref_ext')) {
// $object->ref_ext = 'dolimeet_' . $object->ref;
// }
// if (property_exists($object, 'label')) {
// $object->label = empty($this->fields['label']['default']) ? $langs->trans('CopyOf') . ' ' . $object->label : $this->fields['label']['default'];
// }
// if (property_exists($object, 'date_creation')) {
// $object->date_creation = dol_now();
// }
// if (property_exists($object, 'status')) {
// $object->status = 1;
// }
//
// // Create clone
// $object->context['createfromclone'] = 'createfromclone';
// $object_id = $object->create($user);
//
// if ($object_id > 0) {
// if (!empty($signatoriesID)) {
// $signatory->createFromClone($user, $signatoriesID[strtoupper($this->type) . '_EXTERNAL_ATTENDANT'], $object_id);
// $signatory->createFromClone($user, $signatoriesID[strtoupper($this->type) . '_SOCIETY_ATTENDANT'], $object_id);
// }
//
// if ( ! empty($options['schedule'])) {
// if ( ! empty($openinghours)) {
// $openinghours->element_id = $object_id;
// $openinghours->create($user);
// }
// }
//
// if ( ! empty($options['attendants'])) {
// if ( ! empty($extintervenant_ids) && $extintervenant_ids > 0) {
// foreach ($extintervenant_ids as $extintervenant_id) {
// $signatory->createFromClone($user, $extintervenant_id, $object_id);
// }
// }
// }
//
// if ( ! empty($options['preventionplan_risk'])) {
// $num = (!empty($object->lines) ? count($object->lines) : 0);
// for ($i = 0; $i < $num; $i++) {
// $line = $object->lines[$i];
// if (property_exists($line, 'ref')) {
// $line->ref = $refPreventionPlanDetMod->getNextValue($line);
// }
// $line->category = empty($line->category) ? 0 : $line->category;
// $line->fk_preventionplan = $object_id;
//
// $result = $line->insert($user, 1);
// if ($result < 0) {
// $this->error = $this->db->lasterror();
// $this->db->rollback();
// return -1;
// }
// }
// }
// } else {
// $error++;
// $this->error = $object->error;
// $this->errors = $object->errors;
// }
//
// unset($object->context['createfromclone']);
//
// // End
// if ( ! $error) {
// $this->db->commit();
// return $object_id;
// } else {
// $this->db->rollback();
// return -1;
// }
// }
/**
* Clone an object into another one
*
* @param User $user User that creates
* @param int $fromid ID of object to clone
* @param array $options Options array
* @return int New object created, <0 if KO
* @throws Exception
*/
public function createFromClone(User $user, int $fromid, array $options): int
{
dol_syslog(__METHOD__, LOG_DEBUG);

global $conf, $langs;
$error = 0;

$object = new self($this->db);

$this->db->begin();

// Load source object
$object->fetchCommon($fromid);

// Reset some properties
unset($object->id);
unset($object->fk_user_creat);
unset($object->import_key);

// Clear fields
if (property_exists($object, 'ref')) {
$object->ref = '';
}
if (!empty($options['label'])) {
if (property_exists($object, 'label')) {
$object->label = $options['label'];
}
}
if (property_exists($object, 'date_creation')) {
$object->date_creation = dol_now();
}
if (property_exists($object, 'status')) {
$object->status = 0;
}

// Create clone
$object->context['createfromclone'] = 'createfromclone';
$result = $object->create($user);

if ($result > 0) {
if (!empty($options['attendants'])) {
// Load signatory from source object
$signatory = new SaturneSignature($this->db);
$signatories = $signatory->fetchSignatory('', $fromid, $this->type);
if (is_array($signatories) && !empty($signatories)) {
foreach ($signatories as $arrayRole) {
foreach ($arrayRole as $signatoryRole) {
$signatory->createFromClone($user, $signatoryRole->id, $result);
}
}
}
}
} else {
$error++;
$this->error = $object->error;
$this->errors = $object->errors;
}

unset($object->context['createfromclone']);

// End
if (!$error) {
$this->db->commit();
return $result;
} else {
$this->db->rollback();
return -1;
}
}
}

/**
Expand Down
56 changes: 30 additions & 26 deletions core/tpl/signature/signature_action_view.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,44 @@
<div class="wpeo-dropdown">
<div class="dropdown-toggle wpeo-button button-main"><span><?php echo $langs->trans('SignatureActions'); ?></span> <i class="fas fa-caret-down"></i></div>
<ul class="dropdown-content">
<?php if (empty($element->signature) && $element->status != $element::STATUS_ABSENT) {
<?php if ($object->status < $object::STATUS_LOCKED) {
if (empty($element->signature) && $element->status != $element::STATUS_ABSENT) {
print '<li class="dropdown-item">';
print '<form method="POST" action="' . $_SERVER['REQUEST_URI'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="set_absent">';
print '<input type="hidden" name="signatoryID" value="' . $element->id . '">';
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
print '<button type="submit" class="signature-absent wpeo-button button-primary" value="' . $element->id . '">';
print '<span>' . $langs->trans('Absent') . '</span>';
print '</button>';
print '</form>';
print '</li>';
}
print '<li class="dropdown-item">';
print '<form method="POST" action="' . $_SERVER['REQUEST_URI'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="set_absent">';
print '<input type="hidden" name="action" value="send">';
print '<input type="hidden" name="signatoryID" value="' . $element->id . '">';
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
print '<button type="submit" class="signature-absent wpeo-button button-primary" value="' . $element->id . '">';
print '<span>' . $langs->trans('Absent') . '</span>';
print '<button type="submit" class="signature-email wpeo-button button-primary" value="' . $element->id . '">';
print '<span><i class="fas fa-at"></i> ' . $langs->trans('SendEmail') . '</span>';
print '</button>';
print '</form>';
print '</li>';
}
print '<li class="dropdown-item">';
print '<form method="POST" action="' . $_SERVER['REQUEST_URI'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="send">';
print '<input type="hidden" name="signatoryID" value="' . $element->id . '">';
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
print '<button type="submit" class="signature-email wpeo-button button-primary" value="' . $element->id . '">';
print '<span><i class="fas fa-at"></i>' . $langs->trans('SendEmail') . '</span>';
print '</button>';
print '</form>';
print '</li>';
print '<li class="dropdown-item">';
print '<form method="POST" action="' . $_SERVER['REQUEST_URI'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="delete_attendant">';
print '<input type="hidden" name="signatoryID" value="' . $element->id . '">';
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
print '<button type="submit" name="deleteAttendant" id="deleteAttendant" class="attendant-delete wpeo-button button-primary" value="' . $element->id . '">';
print '<span><i class="fas fa-trash"></i>' . $langs->trans('DeleteAttendant') . '</span>';
print '</button>';
print '</form>';
print '</li>'; ?>
if ($object->status < $object::STATUS_LOCKED) {
print '<li class="dropdown-item">';
print '<form method="POST" action="' . $_SERVER['REQUEST_URI'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="delete_attendant">';
print '<input type="hidden" name="signatoryID" value="' . $element->id . '">';
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
print '<button type="submit" name="deleteAttendant" id="deleteAttendant" class="attendant-delete wpeo-button button-primary" value="' . $element->id . '">';
print '<span><i class="fas fa-trash"></i>' . $langs->trans('DeleteAttendant') . '</span>';
print '</button>';
print '</form>';
print '</li>';
} ?>
</ul>
</div>
2 changes: 1 addition & 1 deletion public/signature/add_signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@

$title = $langs->trans('Signature');
$morejs = ['/dolimeet/js/signature-pad.min.js', '/saturne/js/saturne.min.js'];
$morecss = ['/saturne/css/saturne.css'];
$morecss = ['/saturne/css/saturne.min.css'];

llxHeaderSignature($title, '', 0, 0, $morejs, $morecss);

Expand Down
Loading

0 comments on commit 8e31099

Please sign in to comment.