20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
36 public static $FIELDS = array(
59 $this->project =
new Project($this->db);
60 $this->task =
new Task($this->db);
73 public function get(
$id)
75 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
76 throw new RestException(403);
79 $result = $this->project->fetch(
$id);
81 throw new RestException(404,
'Project with supplied id not found');
85 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
88 $this->project->fetchObjectLinked();
106 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
107 throw new RestException(403);
110 $result = $this->project->fetch(0, $ref);
112 throw new RestException(404,
'Project with supplied ref not found');
116 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
119 $this->project->fetchObjectLinked();
137 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
138 throw new RestException(403);
141 $result = $this->project->fetch(0,
'', $ref_ext);
143 throw new RestException(404,
'Project with supplied ref_ext not found');
147 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
150 $this->project->fetchObjectLinked();
168 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
169 throw new RestException(403);
172 $result = $this->project->fetch(0,
'',
'', $email_msgid);
174 throw new RestException(404,
'Project with supplied email_msgid not found');
178 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
181 $this->project->fetchObjectLinked();
201 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
203 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
204 throw new RestException(403);
210 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
214 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
215 $search_sale = DolibarrApiAccess::$user->id;
218 $sql =
"SELECT t.rowid";
219 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
220 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_extrafields AS ef ON ef.fk_object = t.rowid";
222 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_project as c";
224 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
226 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
229 if ($search_sale && $search_sale !=
'-1') {
230 if ($search_sale == -2) {
231 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
232 } elseif ($search_sale > 0) {
233 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
238 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
245 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
250 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
252 $sql .= $this->db->order($sortfield, $sortorder);
257 $offset = $limit * $page;
259 $sql .= $this->db->plimit($limit + 1, $offset);
263 $result = $this->db->query($sql);
266 $num = $this->db->num_rows($result);
267 $min = min($num, ($limit <= 0 ? $num : $limit));
270 $obj = $this->db->fetch_object($result);
271 $project_static =
new Project($this->db);
272 if ($project_static->fetch($obj->rowid)) {
278 throw new RestException(503,
'Error when retrieve project list : '.$this->db->lasterror());
282 if ($pagination_data) {
283 $totalsResult = $this->db->query($sqlTotals);
284 $total = $this->db->fetch_object($totalsResult)->total;
289 $obj_ret[
'data'] = $tmp;
290 $obj_ret[
'pagination'] = [
291 'total' => (int) $total,
293 'page_count' => ceil((
int) $total / $limit),
307 public function post($request_data =
null)
310 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
311 throw new RestException(403,
"Insuffisant rights");
314 $result = $this->
_validate($request_data);
316 foreach ($request_data as $field => $value) {
317 if ($field ===
'caller') {
319 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
323 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
334 if ($this->project->ref == -1 || $this->project->ref ===
'auto') {
342 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
343 foreach ($dirmodels as $reldir) {
344 $file =
dol_buildpath($reldir.
"core/modules/project/".$modele.
'.php', 0);
345 if (file_exists($file)) {
347 $classname = $modele;
351 if ($filefound && !empty($classname)) {
352 $result =
dol_include_once($reldir .
"core/modules/project/" . $modele .
'.php');
353 if ($result !==
false && class_exists($classname)) {
354 $modProject =
new $classname();
355 $defaultref = $modProject->getNextValue(
null, $this->project);
357 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/" . $modele .
'.php', LOG_ERR);
360 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
363 if (is_numeric($defaultref) && $defaultref <= 0) {
367 if (empty($defaultref)) {
371 $this->project->ref = $defaultref;
374 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
375 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
378 return $this->project->id;
393 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
394 throw new RestException(403);
397 $result = $this->project->fetch(
$id);
399 throw new RestException(404,
'Project not found');
403 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
405 $this->project->getLinesArray(DolibarrApiAccess::$user);
407 foreach ($this->project->lines as $line) {
408 if ($includetimespent == 1) {
409 $timespent = $line->getSummaryOfTimeSpent(0);
411 if ($includetimespent == 2) {
412 $timespent = $line->fetchTimeSpentOnTask();
433 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
434 throw new RestException(403);
437 $result = $this->project->fetch(
$id);
439 throw new RestException(404,
'Project not found');
443 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
446 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
447 $taskstatic =
new Task($this->db);
448 $userp = DolibarrApiAccess::$user;
450 $userp =
new User($this->db);
451 $userp->fetch($userid);
453 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null,
$id, 0);
455 foreach ($this->project->roles as $line) {
601 public function put(
$id, $request_data =
null)
603 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
604 throw new RestException(403);
607 $result = $this->project->fetch(
$id);
609 throw new RestException(404,
'Project not found');
613 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
615 foreach ($request_data as $field => $value) {
616 if ($field ==
'id') {
619 if ($field ===
'caller') {
621 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
624 if ($field ==
'array_options' && is_array($value)) {
625 foreach ($value as $index => $val) {
626 $this->project->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->project);
631 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
634 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
635 return $this->
get(
$id);
637 throw new RestException(500, $this->project->error);
648 public function delete(
$id)
650 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
651 throw new RestException(403);
653 $result = $this->project->fetch(
$id);
655 throw new RestException(404,
'Project not found');
659 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
662 if (!$this->project->delete(DolibarrApiAccess::$user)) {
663 throw new RestException(500,
'Error when delete project : '.$this->project->error);
669 'message' =>
'Project deleted'
694 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
695 throw new RestException(403);
697 $result = $this->project->fetch(
$id);
699 throw new RestException(404,
'Project not found');
703 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
706 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
708 throw new RestException(304,
'Error nothing done. May be object is already validated');
711 throw new RestException(500,
'Error when validating Project: '.$this->project->error);
717 'message' =>
'Project validated'
738 unset(
$object->barcode_type_code);
739 unset(
$object->barcode_type_label);
740 unset(
$object->barcode_type_coder);
741 unset(
$object->cond_reglement_id);
742 unset(
$object->cond_reglement);
743 unset(
$object->fk_delivery_address);
744 unset(
$object->shipping_method_id);
748 unset(
$object->label_incoterms);
749 unset(
$object->location_incoterms);
754 unset(
$object->mode_reglement_id);
766 unset(
$object->total_localtax1);
767 unset(
$object->total_localtax2);
785 foreach (self::$FIELDS as $field) {
786 if (!isset($data[$field])) {
787 throw new RestException(400,
"$field field missing");
789 $object[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage projects.
post($request_data=null)
Create project object.
getByMsgId($email_msgid)
Get properties of a project object.
getLines($id, $includetimespent=0)
Get tasks of a project.
validate($id, $notrigger=0)
Validate a project.
_validate($data)
Validate fields before create or update object.
getByRef($ref)
Get properties of a project object.
_cleanObjectDatas($object)
Clean sensible object datas.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List projects.
__construct()
Constructor.
put($id, $request_data=null)
Add a task to given project.
getByRefExt($ref_ext)
Get properties of a project object.
getRoles($id, $userid=0)
Get roles a user is assigned to a project with.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.