if ($id == '' && $ref == '' && ($action != "create" && $action != "add" && $action != "update" && !$_POST["cancel"])) { accessforbidden(); } $mine = GETPOST('mode') == 'mine' ? 1 : 0; //if (! $user->rights->projet->all->lire) $mine=1; // Special for projects // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array $hookmanager->initHooks(array('projectcard', 'globalcard')); $object = new Project($db); $extrafields = new ExtraFields($db); // Load object //include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Can't use generic include because when creating a project, ref is defined and we dont want error if fetch fails from ref. if ($id > 0 || !empty($ref)) { $ret = $object->fetch($id, $ref); // If we create project, ref may be defined into POST but record does not yet exists into database if ($ret > 0) { $object->fetch_thirdparty(); $id = $object->id; } } // Security check $socid = GETPOST('socid'); if ($user->societe_id > 0) { $socid = $user->societe_id; } $result = restrictedArea($user, 'projet', $object->id); // fetch optionals attributes and labels $extralabels = $extrafields->fetch_name_optionals_label($object->table_element); $date_start = dol_mktime(0, 0, 0, GETPOST('projectstartmonth', 'int'), GETPOST('projectstartday', 'int'), GETPOST('projectstartyear', 'int')); $date_end = dol_mktime(0, 0, 0, GETPOST('projectendmonth', 'int'), GETPOST('projectendday', 'int'), GETPOST('projectendyear', 'int')); /* * Actions
$new = dol_now(); $tmp = dol_getdate($new); //$datee=$now //$dates=dol_time_plus_duree($datee, -1, 'y'); $dates = dol_get_first_day($tmp['year'], 1); } if ($projectid == '' && $ref == '') { dol_print_error('', 'Bad parameter'); exit; } $mine = $_REQUEST['mode'] == 'mine' ? 1 : 0; //if (! $user->rights->projet->all->lire) $mine=1; // Special for projects $project = new Project($db); if ($id > 0 || !empty($ref)) { $project->fetch($id, $ref); $project->fetch_thirdparty(); $projectid = $project->id; } else { $project->fetch($projectid); $project->fetch_thirdparty(); $projectid = $project->id; } // Security check $socid = 0; if ($user->societe_id > 0) { $socid = $user->societe_id; } $result = restrictedArea($user, 'projet', $projectid); /* * View */
$error++; } if (!$error) { $result = $object->update($user); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } } } else { $action = 'edit'; } } if ($action == 'confirm_delete' && $confirm == "yes" && $user->rights->projet->supprimer) { if ($object->fetch($id, $ref) >= 0) { $result = $projectstatic->fetch($object->fk_project); $projectstatic->fetch_thirdparty(); if ($object->delete($user) > 0) { header('Location: ' . DOL_URL_ROOT . '/projet/tasks.php?id=' . $projectstatic->id . ($withproject ? '&withproject=1' : '')); exit; } else { setEventMessages($object->error, $object->errors, 'errors'); $action = ''; } } } // Retreive First Task ID of Project if withprojet is on to allow project prev next to work if (!empty($project_ref) && !empty($withproject)) { if ($projectstatic->fetch('', $project_ref) > 0) { $tasksarray = $object->getTasksArray(0, 0, $projectstatic->id, $socid, 0); if (count($tasksarray) > 0) { $id = $tasksarray[0]->id;
/** * 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; } }