/** * Load an object from its id and create a new one in database * * @param int $fromid Id of object to clone * @param bool $clone_contact clone contact of project * @param bool $clone_task clone task of project * @param bool $clone_project_file clone file of project * @param bool $clone_task_file clone file of task (if task are copied) * @param bool $clone_note clone note of project * @param bool $notrigger no trigger flag * @return int New id of clone */ function createFromClone($fromid, $clone_contact = false, $clone_task = true, $clone_project_file = false, $clone_task_file = false, $clone_note = true, $notrigger = 0) { global $user, $langs, $conf; $error = 0; dol_syslog("createFromClone clone_contact=" . $clone_contact . " clone_task=" . $clone_task . " clone_project_file=" . $clone_project_file . " clone_note=" . $clone_note); $now = dol_mktime(0, 0, 0, idate('m', dol_now()), idate('d', dol_now()), idate('Y', dol_now())); $clone_project = new Project($this->db); $clone_project->context['createfromclone'] = 'createfromclone'; $this->db->begin(); // Load source object $clone_project->fetch($fromid); $clone_project->fetch_thirdparty(); $orign_dt_start = $clone_project->date_start; $orign_project_ref = $clone_project->ref; $clone_project->id = 0; $clone_project->date_start = $now; if (!empty($clone_project->date_end)) { $clone_project->date_end = $clone_project->date_end + ($now - $orign_dt_start); } $clone_project->datec = $now; if (!$clone_note) { $clone_project->note_private = ''; $clone_project->note_public = ''; } //Generate next ref $defaultref = ''; $obj = empty($conf->global->PROJECT_ADDON) ? 'mod_project_simple' : $conf->global->PROJECT_ADDON; if (!empty($conf->global->PROJECT_ADDON) && is_readable(DOL_DOCUMENT_ROOT . "/core/modules/project/" . $conf->global->PROJECT_ADDON . ".php")) { require_once DOL_DOCUMENT_ROOT . "/core/modules/project/" . $conf->global->PROJECT_ADDON . '.php'; $modProject = new $obj(); $defaultref = $modProject->getNextValue(is_object($clone_project->thirdparty) ? $clone_project->thirdparty->id : 0, $clone_project); } if (is_numeric($defaultref) && $defaultref <= 0) { $defaultref = ''; } $clone_project->ref = $defaultref; // Create clone $result = $clone_project->create($user, $notrigger); // Other options if ($result < 0) { $this->error .= $clone_project->error; $error++; } if (!$error) { //Get the new project id $clone_project_id = $clone_project->id; //Note Update if (!$clone_note) { $clone_project->note_private = ''; $clone_project->note_public = ''; } else { $this->db->begin(); $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_public, ENT_QUOTES), '_public'); if ($res < 0) { $this->error .= $clone_project->error; $error++; $this->db->rollback(); } else { $this->db->commit(); } $this->db->begin(); $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_private, ENT_QUOTES), '_private'); if ($res < 0) { $this->error .= $clone_project->error; $error++; $this->db->rollback(); } else { $this->db->commit(); } } //Duplicate contact if ($clone_contact) { $origin_project = new Project($this->db); $origin_project->fetch($fromid); foreach (array('internal', 'external') as $source) { $tab = $origin_project->liste_contact(-1, $source); foreach ($tab as $contacttoadd) { $clone_project->add_contact($contacttoadd['id'], $contacttoadd['code'], $contacttoadd['source'], $notrigger); if ($clone_project->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') { $langs->load("errors"); $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"); $error++; } else { if ($clone_project->error != '') { $this->error .= $clone_project->error; $error++; } } } } } //Duplicate file if ($clone_project_file) { require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; $clone_project_dir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($defaultref); $ori_project_dir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($orign_project_ref); if (dol_mkdir($clone_project_dir) >= 0) { $filearray = dol_dir_list($ori_project_dir, "files", 0, '', '(\\.meta|_preview\\.png)$', '', SORT_ASC, 1); foreach ($filearray as $key => $file) { $rescopy = dol_copy($ori_project_dir . '/' . $file['name'], $clone_project_dir . '/' . $file['name'], 0, 1); if (is_numeric($rescopy) && $rescopy < 0) { $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_project_dir . '/' . $file['name'], $clone_project_dir . '/' . $file['name']); $error++; } } } else { $this->error .= $langs->trans('ErrorInternalErrorDetected') . ':dol_mkdir'; $error++; } } //Duplicate task if ($clone_task) { require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php'; $taskstatic = new Task($this->db); // Security check $socid = 0; if ($user->societe_id > 0) { $socid = $user->societe_id; } $tasksarray = $taskstatic->getTasksArray(0, 0, $fromid, $socid, 0); //manage new parent clone task id $tab_conv_child_parent = array(); foreach ($tasksarray as $tasktoclone) { $result_clone = $taskstatic->createFromClone($tasktoclone->id, $clone_project_id, $tasktoclone->fk_parent, true, true, false, $clone_task_file, true, false); if ($result_clone <= 0) { $this->error .= $result_clone->error; $error++; } else { $new_task_id = $result_clone; $taskstatic->fetch($tasktoclone->id); //manage new parent clone task id // if the current task has child we store the original task id and the equivalent clone task id if ($taskstatic->hasChildren() && !array_key_exists($tasktoclone->id, $tab_conv_child_parent)) { $tab_conv_child_parent[$tasktoclone->id] = $new_task_id; } } } //Parse all clone node to be sure to update new parent $tasksarray = $taskstatic->getTasksArray(0, 0, $clone_project_id, $socid, 0); foreach ($tasksarray as $task_cloned) { $taskstatic->fetch($task_cloned->id); if ($taskstatic->fk_task_parent != 0) { $taskstatic->fk_task_parent = $tab_conv_child_parent[$taskstatic->fk_task_parent]; } $res = $taskstatic->update($user, $notrigger); if ($result_clone <= 0) { $this->error .= $taskstatic->error; $error++; } } } } unset($clone_project->context['createfromclone']); if (!$error) { $this->db->commit(); return $clone_project_id; } else { $this->db->rollback(); dol_syslog(get_class($this) . "::createFromClone nbError: " . $error . " error : " . $this->error, LOG_ERR); return -1; } }