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)
287 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
288 throw new RestException(403,
"Insuffisant rights");
291 $result = $this->
_validate($request_data);
293 foreach ($request_data as $field => $value) {
294 if ($field ===
'caller') {
296 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
300 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
309 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
310 throw new RestException(500,
"Error creating project", array_merge(array($this->project->error), $this->project->errors));
313 return $this->project->id;
326 public function getLines($id, $includetimespent = 0)
328 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
329 throw new RestException(403);
332 $result = $this->project->fetch($id);
334 throw new RestException(404,
'Project not found');
338 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
340 $this->project->getLinesArray(DolibarrApiAccess::$user);
342 foreach ($this->project->lines as $line) {
343 if ($includetimespent == 1) {
344 $timespent = $line->getSummaryOfTimeSpent(0);
346 if ($includetimespent == 2) {
347 $timespent = $line->fetchTimeSpentOnTask();
368 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
369 throw new RestException(403);
372 $result = $this->project->fetch($id);
374 throw new RestException(404,
'Project not found');
378 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
381 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
382 $taskstatic =
new Task($this->db);
383 $userp = DolibarrApiAccess::$user;
385 $userp =
new User($this->db);
386 $userp->fetch($userid);
388 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp,
null, $id, 0);
390 foreach ($this->project->roles as $line) {
536 public function put($id, $request_data =
null)
538 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
539 throw new RestException(403);
542 $result = $this->project->fetch($id);
544 throw new RestException(404,
'Project not found');
548 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
550 foreach ($request_data as $field => $value) {
551 if ($field ==
'id') {
554 if ($field ===
'caller') {
556 $this->project->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
559 if ($field ==
'array_options' && is_array($value)) {
560 foreach ($value as $index => $val) {
561 $this->project->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->project);
566 $this->project->$field = $this->
_checkValForAPI($field, $value, $this->project);
569 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
570 return $this->
get($id);
572 throw new RestException(500, $this->project->error);
583 public function delete($id)
585 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
586 throw new RestException(403);
588 $result = $this->project->fetch($id);
590 throw new RestException(404,
'Project not found');
594 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
597 if (!$this->project->delete(DolibarrApiAccess::$user)) {
598 throw new RestException(500,
'Error when delete project : '.$this->project->error);
604 'message' =>
'Project deleted'
629 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
630 throw new RestException(403);
632 $result = $this->project->fetch($id);
634 throw new RestException(404,
'Project not found');
638 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
641 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
643 throw new RestException(304,
'Error nothing done. May be object is already validated');
646 throw new RestException(500,
'Error when validating Project: '.$this->project->error);
652 'message' =>
'Project validated'
673 unset(
$object->barcode_type_code);
674 unset(
$object->barcode_type_label);
675 unset(
$object->barcode_type_coder);
676 unset(
$object->cond_reglement_id);
677 unset(
$object->cond_reglement);
678 unset(
$object->fk_delivery_address);
679 unset(
$object->shipping_method_id);
683 unset(
$object->label_incoterms);
684 unset(
$object->location_incoterms);
689 unset(
$object->mode_reglement_id);
701 unset(
$object->total_localtax1);
702 unset(
$object->total_localtax2);
720 foreach (self::$FIELDS as $field) {
721 if (!isset($data[$field])) {
722 throw new RestException(400,
"$field field missing");
724 $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
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.