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';
36 static $FIELDS = array(
54 $this->task =
new Task($this->
db);
67 public function get($id)
69 if (!DolibarrApiAccess::$user->rights->projet->lire) {
70 throw new RestException(401);
73 $result = $this->project->fetch($id);
75 throw new RestException(404,
'Project not found');
79 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
82 $this->project->fetchObjectLinked();
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'')
109 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
113 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
115 $sql =
"SELECT t.rowid";
116 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
117 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet as t";
119 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_project as c";
121 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
123 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
124 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
125 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
126 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
128 if ($search_sale > 0)
130 $sql .=
" AND sc.fk_user = ".$search_sale;
134 $sql .=
" AND c.fk_categorie = ".$this->db->escape($category).
" AND c.fk_project = t.rowid ";
141 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
143 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
144 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
147 $sql .= $this->
db->order($sortfield, $sortorder);
152 $offset = $limit * $page;
154 $sql .= $this->
db->plimit($limit + 1, $offset);
158 $result = $this->
db->query($sql);
162 $num = $this->
db->num_rows($result);
163 $min = min($num, ($limit <= 0 ? $num : $limit));
166 $obj = $this->
db->fetch_object($result);
167 $project_static =
new Project($this->
db);
168 if ($project_static->fetch($obj->rowid)) {
174 throw new RestException(503,
'Error when retrieve project list : '.$this->
db->lasterror());
176 if (!count($obj_ret)) {
177 throw new RestException(404,
'No project found');
188 public function post($request_data = null)
190 if (!DolibarrApiAccess::$user->rights->projet->creer) {
191 throw new RestException(401,
"Insuffisant rights");
194 $result = $this->
_validate($request_data);
196 foreach ($request_data as $field => $value) {
197 $this->project->$field = $value;
206 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
207 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
210 return $this->project->id;
223 public function getLines($id, $includetimespent = 0)
225 if (!DolibarrApiAccess::$user->rights->projet->lire) {
226 throw new RestException(401);
229 $result = $this->project->fetch($id);
231 throw new RestException(404,
'Project not found');
235 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
237 $this->project->getLinesArray(DolibarrApiAccess::$user);
239 foreach ($this->project->lines as $line)
241 if ($includetimespent == 1)
243 $timespent = $line->getSummaryOfTimeSpent(0);
245 if ($includetimespent == 1)
270 if (!DolibarrApiAccess::$user->rights->projet->lire) {
271 throw new RestException(401);
274 $result = $this->project->fetch($id);
276 throw new RestException(404,
'Project not found');
280 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
283 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
284 $taskstatic =
new Task($this->
db);
285 $userp = DolibarrApiAccess::$user;
288 $userp =
new User($this->
db);
289 $userp->fetch($userid);
291 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, 0, $id, 0);
293 foreach ($this->project->roles as $line) {
431 public function put($id, $request_data = null)
433 if (!DolibarrApiAccess::$user->rights->projet->creer) {
434 throw new RestException(401);
437 $result = $this->project->fetch($id);
439 throw new RestException(404,
'Project not found');
443 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
445 foreach ($request_data as $field => $value) {
446 if ($field ==
'id')
continue;
447 $this->project->$field = $value;
450 if ($this->project->update(DolibarrApiAccess::$user) >= 0)
452 return $this->
get($id);
454 throw new RestException(500, $this->project->error);
465 public function delete($id)
467 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
468 throw new RestException(401);
470 $result = $this->project->fetch($id);
472 throw new RestException(404,
'Project not found');
476 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
479 if (!$this->project->delete(DolibarrApiAccess::$user)) {
480 throw new RestException(500,
'Error when delete project : '.$this->project->error);
486 'message' =>
'Project deleted'
511 if (!DolibarrApiAccess::$user->rights->projet->creer) {
512 throw new RestException(401);
514 $result = $this->project->fetch($id);
516 throw new RestException(404,
'Project not found');
520 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
523 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
525 throw new RestException(304,
'Error nothing done. May be object is already validated');
528 throw new RestException(500,
'Error when validating Project: '.$this->project->error);
534 'message' =>
'Project validated'
550 $object = parent::_cleanObjectDatas($object);
552 unset($object->datec);
553 unset($object->datem);
554 unset($object->barcode_type);
555 unset($object->barcode_type_code);
556 unset($object->barcode_type_label);
557 unset($object->barcode_type_coder);
558 unset($object->cond_reglement_id);
559 unset($object->cond_reglement);
560 unset($object->fk_delivery_address);
561 unset($object->shipping_method_id);
562 unset($object->fk_account);
563 unset($object->note);
564 unset($object->fk_incoterms);
565 unset($object->label_incoterms);
566 unset($object->location_incoterms);
567 unset($object->name);
568 unset($object->lastname);
569 unset($object->firstname);
570 unset($object->civility_id);
571 unset($object->mode_reglement_id);
572 unset($object->country);
573 unset($object->country_id);
574 unset($object->country_code);
576 unset($object->weekWorkLoad);
577 unset($object->weekWorkLoad);
581 unset($object->total_ht);
582 unset($object->total_tva);
583 unset($object->total_localtax1);
584 unset($object->total_localtax2);
585 unset($object->total_ttc);
587 unset($object->comments);
602 foreach (self::$FIELDS as $field) {
603 if (!isset($data[$field]))
604 throw new RestException(400,
"$field field missing");
605 $object[$field] = $data[$field];
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $category=0, $sqlfilters= '')
List projects.
getRoles($id, $userid=0)
Get roles a user is assigned to a project with.
put($id, $request_data=null)
Add a task to given project.
Class to manage Dolibarr users.
getLines($id, $includetimespent=0)
Get tasks of a project.
post($request_data=null)
Create project object.
_validate($data)
Validate fields before create or update object.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
validate($id, $notrigger=0)
Validate a project.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensible object datas.
Class to manage projects.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.