19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
35 public static $FIELDS = array(
58 $this->project =
new Project($this->db);
59 $this->task =
new Task($this->db);
72 public function get(
$id)
74 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
75 throw new RestException(403);
78 $result = $this->project->fetch(
$id);
80 throw new RestException(404,
'Project with supplied id not found');
84 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
87 $this->project->fetchObjectLinked();
105 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
106 throw new RestException(403);
109 $result = $this->project->fetch(
'', $ref);
111 throw new RestException(404,
'Project with supplied ref not found');
115 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
118 $this->project->fetchObjectLinked();
136 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
137 throw new RestException(403);
140 $result = $this->project->fetch(
'',
'', $ref_ext);
142 throw new RestException(404,
'Project with supplied ref_ext not found');
146 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
149 $this->project->fetchObjectLinked();
167 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
168 throw new RestException(403);
171 $result = $this->project->fetch(
'',
'',
'', $email_msgid);
173 throw new RestException(404,
'Project with supplied email_msgid not found');
177 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
180 $this->project->fetchObjectLinked();
199 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $properties =
'')
201 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
202 throw new RestException(403);
208 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
212 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
213 $search_sale = DolibarrApiAccess::$user->id;
216 $sql =
"SELECT t.rowid";
217 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
218 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_extrafields AS ef ON ef.fk_object = t.rowid";
220 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_project as c";
222 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
224 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
227 if ($search_sale && $search_sale !=
'-1') {
228 if ($search_sale == -2) {
229 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
230 } elseif ($search_sale > 0) {
231 $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).
")";
236 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_project = t.rowid ";
243 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
247 $sql .= $this->db->order($sortfield, $sortorder);
252 $offset = $limit * $page;
254 $sql .= $this->db->plimit($limit + 1, $offset);
258 $result = $this->db->query($sql);
261 $num = $this->db->num_rows($result);
262 $min = min($num, ($limit <= 0 ? $num : $limit));
265 $obj = $this->db->fetch_object($result);
266 $project_static =
new Project($this->db);
267 if ($project_static->fetch($obj->rowid)) {
273 throw new RestException(503,
'Error when retrieve project list : '.$this->db->lasterror());
285 public function post($request_data =
null)
288 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
289 throw new RestException(403,
"Insuffisant rights");
292 $result = $this->
_validate($request_data);
294 foreach ($request_data as $field => $value) {
295 if ($field ===
'caller') {
297 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
301 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
312 if ($this->project->ref == -1 || $this->project->ref ===
'auto') {
320 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
321 foreach ($dirmodels as $reldir) {
322 $file =
dol_buildpath($reldir.
"core/modules/project/".$modele.
'.php', 0);
323 if (file_exists($file)) {
325 $classname = $modele;
329 if ($filefound && !empty($classname)) {
330 $result =
dol_include_once($reldir .
"core/modules/project/" . $modele .
'.php');
331 if ($result !==
false && class_exists($classname)) {
332 $modProject =
new $classname();
333 $defaultref = $modProject->getNextValue(
null, $this->project);
335 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/" . $modele .
'.php', LOG_ERR);
338 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
341 if (is_numeric($defaultref) && $defaultref <= 0) {
345 if (empty($defaultref)) {
349 $this->project->ref = $defaultref;
352 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
353 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
356 return $this->project->id;
371 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
372 throw new RestException(403);
375 $result = $this->project->fetch(
$id);
377 throw new RestException(404,
'Project not found');
381 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
383 $this->project->getLinesArray(DolibarrApiAccess::$user);
385 foreach ($this->project->lines as $line) {
386 if ($includetimespent == 1) {
387 $timespent = $line->getSummaryOfTimeSpent(0);
389 if ($includetimespent == 2) {
390 $timespent = $line->fetchTimeSpentOnTask();
411 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
412 throw new RestException(403);
415 $result = $this->project->fetch(
$id);
417 throw new RestException(404,
'Project not found');
421 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
424 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
425 $taskstatic =
new Task($this->db);
426 $userp = DolibarrApiAccess::$user;
428 $userp =
new User($this->db);
429 $userp->fetch($userid);
431 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null,
$id, 0);
433 foreach ($this->project->roles as $line) {
579 public function put(
$id, $request_data =
null)
581 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
582 throw new RestException(403);
585 $result = $this->project->fetch(
$id);
587 throw new RestException(404,
'Project not found');
591 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
593 foreach ($request_data as $field => $value) {
594 if ($field ==
'id') {
597 if ($field ===
'caller') {
599 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
602 if ($field ==
'array_options' && is_array($value)) {
603 foreach ($value as $index => $val) {
604 $this->project->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->project);
609 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
612 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
613 return $this->
get(
$id);
615 throw new RestException(500, $this->project->error);
626 public function delete(
$id)
628 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
629 throw new RestException(403);
631 $result = $this->project->fetch(
$id);
633 throw new RestException(404,
'Project not found');
637 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
640 if (!$this->project->delete(DolibarrApiAccess::$user)) {
641 throw new RestException(500,
'Error when delete project : '.$this->project->error);
647 'message' =>
'Project deleted'
672 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
673 throw new RestException(403);
675 $result = $this->project->fetch(
$id);
677 throw new RestException(404,
'Project not found');
681 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
684 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
686 throw new RestException(304,
'Error nothing done. May be object is already validated');
689 throw new RestException(500,
'Error when validating Project: '.$this->project->error);
695 'message' =>
'Project validated'
716 unset(
$object->barcode_type_code);
717 unset(
$object->barcode_type_label);
718 unset(
$object->barcode_type_coder);
719 unset(
$object->cond_reglement_id);
720 unset(
$object->cond_reglement);
721 unset(
$object->fk_delivery_address);
722 unset(
$object->shipping_method_id);
726 unset(
$object->label_incoterms);
727 unset(
$object->location_incoterms);
732 unset(
$object->mode_reglement_id);
744 unset(
$object->total_localtax1);
745 unset(
$object->total_localtax2);
763 foreach (self::$FIELDS as $field) {
764 if (!isset($data[$field])) {
765 throw new RestException(400,
"$field field missing");
767 $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.
__construct()
Constructor.
put($id, $request_data=null)
Add a task to given project.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='')
List projects.
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.