| Total Complexity | 81 |
| Total Lines | 701 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Projects often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Projects, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Projects extends DolibarrApi |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
| 39 | */ |
||
| 40 | public static $FIELDS = array( |
||
| 41 | 'ref', |
||
| 42 | 'title' |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Project $project {@type Project} |
||
| 47 | */ |
||
| 48 | public $project; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Task $task {@type Task} |
||
| 52 | */ |
||
| 53 | public $task; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor |
||
| 58 | */ |
||
| 59 | public function __construct() |
||
| 60 | { |
||
| 61 | global $db, $conf; |
||
| 62 | $this->db = $db; |
||
| 63 | $this->project = new Project($this->db); |
||
| 64 | $this->task = new Task($this->db); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get properties of a project object |
||
| 69 | * |
||
| 70 | * Return an array with project information |
||
| 71 | * |
||
| 72 | * @param string $ref Ref of project |
||
| 73 | * @return array Object with cleaned properties |
||
| 74 | * |
||
| 75 | * @url GET ref/{ref} |
||
| 76 | * |
||
| 77 | * @throws RestException |
||
| 78 | */ |
||
| 79 | public function getByRef($ref) |
||
| 80 | { |
||
| 81 | if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) { |
||
| 82 | throw new RestException(403); |
||
| 83 | } |
||
| 84 | |||
| 85 | $result = $this->project->fetch('', $ref); |
||
| 86 | if (!$result) { |
||
| 87 | throw new RestException(404, 'Project with supplied ref not found'); |
||
| 88 | } |
||
| 89 | |||
| 90 | if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) { |
||
| 91 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->project->fetchObjectLinked(); |
||
| 95 | return $this->_cleanObjectDatas($this->project); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Clean sensible object datas |
||
| 100 | * |
||
| 101 | * @param Object $object Object to clean |
||
| 102 | * @return Object Object with cleaned properties |
||
| 103 | */ |
||
| 104 | protected function _cleanObjectDatas($object) |
||
| 105 | { |
||
| 106 | // phpcs:enable |
||
| 107 | $object = parent::_cleanObjectDatas($object); |
||
| 108 | |||
| 109 | unset($object->datec); |
||
| 110 | unset($object->datem); |
||
| 111 | unset($object->barcode_type); |
||
| 112 | unset($object->barcode_type_code); |
||
| 113 | unset($object->barcode_type_label); |
||
| 114 | unset($object->barcode_type_coder); |
||
| 115 | unset($object->cond_reglement_id); |
||
| 116 | unset($object->cond_reglement); |
||
| 117 | unset($object->fk_delivery_address); |
||
| 118 | unset($object->shipping_method_id); |
||
| 119 | unset($object->fk_account); |
||
| 120 | unset($object->note); |
||
| 121 | unset($object->fk_incoterms); |
||
| 122 | unset($object->label_incoterms); |
||
| 123 | unset($object->location_incoterms); |
||
| 124 | unset($object->name); |
||
| 125 | unset($object->lastname); |
||
| 126 | unset($object->firstname); |
||
| 127 | unset($object->civility_id); |
||
| 128 | unset($object->mode_reglement_id); |
||
| 129 | unset($object->country); |
||
| 130 | unset($object->country_id); |
||
| 131 | unset($object->country_code); |
||
| 132 | |||
| 133 | unset($object->weekWorkLoad); |
||
| 134 | unset($object->weekWorkLoad); |
||
| 135 | |||
| 136 | //unset($object->lines); // for task we use timespent_lines, but for project we use lines |
||
| 137 | |||
| 138 | unset($object->total_ht); |
||
| 139 | unset($object->total_tva); |
||
| 140 | unset($object->total_localtax1); |
||
| 141 | unset($object->total_localtax2); |
||
| 142 | unset($object->total_ttc); |
||
| 143 | |||
| 144 | unset($object->comments); |
||
| 145 | |||
| 146 | return $object; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get properties of a project object |
||
| 151 | * |
||
| 152 | * Return an array with project information |
||
| 153 | * |
||
| 154 | * @param string $ref_ext Ref_Ext of project |
||
| 155 | * @return array Object with cleaned properties |
||
| 156 | * |
||
| 157 | * @url GET ref_ext/{ref_ext} |
||
| 158 | * |
||
| 159 | * @throws RestException |
||
| 160 | */ |
||
| 161 | public function getByRefExt($ref_ext) |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get properties of a project object |
||
| 182 | * |
||
| 183 | * Return an array with project information |
||
| 184 | * |
||
| 185 | * @param string $email_msgid Email msgid of project |
||
| 186 | * @return array Object with cleaned properties |
||
| 187 | * |
||
| 188 | * @url GET email_msgid/{email_msgid} |
||
| 189 | * |
||
| 190 | * @throws RestException |
||
| 191 | */ |
||
| 192 | public function getByMsgId($email_msgid) |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * List projects |
||
| 213 | * |
||
| 214 | * Get a list of projects |
||
| 215 | * |
||
| 216 | * @param string $sortfield Sort field |
||
| 217 | * @param string $sortorder Sort order |
||
| 218 | * @param int $limit Limit for list |
||
| 219 | * @param int $page Page number |
||
| 220 | * @param string $thirdparty_ids Thirdparty ids to filter projects of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} |
||
| 221 | * @param int $category Use this param to filter list by category |
||
| 222 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 223 | * @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names |
||
| 224 | * @return array Array of project objects |
||
| 225 | * |
||
| 226 | * @throws RestException |
||
| 227 | */ |
||
| 228 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '') |
||
| 229 | { |
||
| 230 | if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) { |
||
| 231 | throw new RestException(403); |
||
| 232 | } |
||
| 233 | |||
| 234 | $obj_ret = array(); |
||
| 235 | |||
| 236 | // case of external user, $thirdparty_ids param is ignored and replaced by user's socid |
||
| 237 | $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids; |
||
| 238 | |||
| 239 | // If the internal user must only see his customers, force searching by him |
||
| 240 | $search_sale = 0; |
||
| 241 | if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) { |
||
| 242 | $search_sale = DolibarrApiAccess::$user->id; |
||
| 243 | } |
||
| 244 | |||
| 245 | $sql = "SELECT t.rowid"; |
||
| 246 | $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t"; |
||
| 247 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "projet_extrafields AS ef ON ef.fk_object = t.rowid"; // So we will be able to filter on extrafields |
||
| 248 | if ($category > 0) { |
||
| 249 | $sql .= ", " . MAIN_DB_PREFIX . "categorie_project as c"; |
||
| 250 | } |
||
| 251 | $sql .= ' WHERE t.entity IN (' . getEntity('project') . ')'; |
||
| 252 | if ($socids) { |
||
| 253 | $sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socids) . ")"; |
||
| 254 | } |
||
| 255 | // Search on sale representative |
||
| 256 | if ($search_sale && $search_sale != '-1') { |
||
| 257 | if ($search_sale == -2) { |
||
| 258 | $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)"; |
||
| 259 | } elseif ($search_sale > 0) { |
||
| 260 | $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) . ")"; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | // Select projects of given category |
||
| 264 | if ($category > 0) { |
||
| 265 | $sql .= " AND c.fk_categorie = " . ((int)$category) . " AND c.fk_project = t.rowid "; |
||
| 266 | } |
||
| 267 | // Add sql filters |
||
| 268 | if ($sqlfilters) { |
||
| 269 | $errormessage = ''; |
||
| 270 | $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
||
| 271 | if ($errormessage) { |
||
| 272 | throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | $sql .= $this->db->order($sortfield, $sortorder); |
||
| 277 | if ($limit) { |
||
| 278 | if ($page < 0) { |
||
| 279 | $page = 0; |
||
| 280 | } |
||
| 281 | $offset = $limit * $page; |
||
| 282 | |||
| 283 | $sql .= $this->db->plimit($limit + 1, $offset); |
||
| 284 | } |
||
| 285 | |||
| 286 | dol_syslog("API Rest request"); |
||
| 287 | $result = $this->db->query($sql); |
||
| 288 | |||
| 289 | if ($result) { |
||
| 290 | $num = $this->db->num_rows($result); |
||
| 291 | $min = min($num, ($limit <= 0 ? $num : $limit)); |
||
| 292 | $i = 0; |
||
| 293 | while ($i < $min) { |
||
| 294 | $obj = $this->db->fetch_object($result); |
||
| 295 | $project_static = new Project($this->db); |
||
| 296 | if ($project_static->fetch($obj->rowid)) { |
||
| 297 | $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($project_static), $properties); |
||
| 298 | } |
||
| 299 | $i++; |
||
| 300 | } |
||
| 301 | } else { |
||
| 302 | throw new RestException(503, 'Error when retrieve project list : ' . $this->db->lasterror()); |
||
| 303 | } |
||
| 304 | |||
| 305 | return $obj_ret; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Create project object |
||
| 310 | * |
||
| 311 | * @param array $request_data Request data |
||
| 312 | * @return int ID of project |
||
| 313 | */ |
||
| 314 | public function post($request_data = null) |
||
| 315 | { |
||
| 316 | if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) { |
||
| 317 | throw new RestException(403, "Insuffisant rights"); |
||
| 318 | } |
||
| 319 | // Check mandatory fields |
||
| 320 | $result = $this->_validate($request_data); |
||
| 321 | |||
| 322 | foreach ($request_data as $field => $value) { |
||
| 323 | if ($field === 'caller') { |
||
| 324 | // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller |
||
| 325 | $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
||
| 326 | continue; |
||
| 327 | } |
||
| 328 | |||
| 329 | $this->project->$field = $this->_checkValForAPI($field, $value, $this->project); |
||
| 330 | } |
||
| 331 | /*if (isset($request_data["lines"])) { |
||
| 332 | $lines = array(); |
||
| 333 | foreach ($request_data["lines"] as $line) { |
||
| 334 | array_push($lines, (object) $line); |
||
| 335 | } |
||
| 336 | $this->project->lines = $lines; |
||
| 337 | }*/ |
||
| 338 | if ($this->project->create(DolibarrApiAccess::$user) < 0) { |
||
| 339 | throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors)); |
||
| 340 | } |
||
| 341 | |||
| 342 | return $this->project->id; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Validate fields before create or update object |
||
| 347 | * |
||
| 348 | * @param array $data Array with data to verify |
||
| 349 | * @return array |
||
| 350 | * @throws RestException |
||
| 351 | */ |
||
| 352 | private function _validate($data) |
||
| 353 | { |
||
| 354 | $object = array(); |
||
| 355 | foreach (self::$FIELDS as $field) { |
||
| 356 | if (!isset($data[$field])) { |
||
| 357 | throw new RestException(400, "$field field missing"); |
||
| 358 | } |
||
| 359 | $object[$field] = $data[$field]; |
||
| 360 | } |
||
| 361 | return $object; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get tasks of a project. |
||
| 366 | * See also API /tasks |
||
| 367 | * |
||
| 368 | * @param int $id Id of project |
||
| 369 | * @param int $includetimespent 0=Return only list of tasks. 1=Include a summary of time spent, 2=Include details of time spent lines |
||
| 370 | * @return array |
||
| 371 | * |
||
| 372 | * @url GET {id}/tasks |
||
| 373 | */ |
||
| 374 | public function getLines($id, $includetimespent = 0) |
||
| 375 | { |
||
| 376 | if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) { |
||
| 377 | throw new RestException(403); |
||
| 378 | } |
||
| 379 | |||
| 380 | $result = $this->project->fetch($id); |
||
| 381 | if (!$result) { |
||
| 382 | throw new RestException(404, 'Project not found'); |
||
| 383 | } |
||
| 384 | |||
| 385 | if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) { |
||
| 386 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 387 | } |
||
| 388 | $this->project->getLinesArray(DolibarrApiAccess::$user); |
||
| 389 | $result = array(); |
||
| 390 | foreach ($this->project->lines as $line) { // $line is a task |
||
| 391 | if ($includetimespent == 1) { |
||
| 392 | $timespent = $line->getSummaryOfTimeSpent(0); |
||
| 393 | } |
||
| 394 | if ($includetimespent == 2) { |
||
| 395 | $timespent = $line->fetchTimeSpentOnTask(); |
||
| 396 | } |
||
| 397 | array_push($result, $this->_cleanObjectDatas($line)); |
||
| 398 | } |
||
| 399 | return $result; |
||
| 400 | } |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Add a task to given project |
||
| 405 | * |
||
| 406 | * @param int $id Id of project to update |
||
| 407 | * @param array $request_data Projectline data |
||
| 408 | * |
||
| 409 | * @url POST {id}/tasks |
||
| 410 | * |
||
| 411 | * @return int |
||
| 412 | */ |
||
| 413 | /* |
||
| 414 | public function postLine($id, $request_data = null) |
||
| 415 | { |
||
| 416 | if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) { |
||
| 417 | throw new RestException(403); |
||
| 418 | } |
||
| 419 | |||
| 420 | $result = $this->project->fetch($id); |
||
| 421 | if( ! $result ) { |
||
| 422 | throw new RestException(404, 'Project not found'); |
||
| 423 | } |
||
| 424 | |||
| 425 | if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) { |
||
| 426 | throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 427 | } |
||
| 428 | |||
| 429 | $request_data = (object) $request_data; |
||
| 430 | |||
| 431 | $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml'); |
||
| 432 | |||
| 433 | $updateRes = $this->project->addline( |
||
| 434 | $request_data->desc, |
||
| 435 | $request_data->subprice, |
||
| 436 | $request_data->qty, |
||
| 437 | $request_data->tva_tx, |
||
| 438 | $request_data->localtax1_tx, |
||
| 439 | $request_data->localtax2_tx, |
||
| 440 | $request_data->fk_product, |
||
| 441 | $request_data->remise_percent, |
||
| 442 | $request_data->info_bits, |
||
| 443 | $request_data->fk_remise_except, |
||
| 444 | 'HT', |
||
| 445 | 0, |
||
| 446 | $request_data->date_start, |
||
| 447 | $request_data->date_end, |
||
| 448 | $request_data->product_type, |
||
| 449 | $request_data->rang, |
||
| 450 | $request_data->special_code, |
||
| 451 | $fk_parent_line, |
||
| 452 | $request_data->fk_fournprice, |
||
| 453 | $request_data->pa_ht, |
||
| 454 | $request_data->label, |
||
| 455 | $request_data->array_options, |
||
| 456 | $request_data->fk_unit, |
||
| 457 | $this->element, |
||
| 458 | $request_data->id |
||
| 459 | ); |
||
| 460 | |||
| 461 | if ($updateRes > 0) { |
||
| 462 | return $updateRes; |
||
| 463 | |||
| 464 | } |
||
| 465 | return false; |
||
| 466 | } |
||
| 467 | */ |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Update a task to given project |
||
| 471 | * |
||
| 472 | * @param int $id Id of project to update |
||
| 473 | * @param int $taskid Id of task to update |
||
| 474 | * @param array $request_data Projectline data |
||
| 475 | * |
||
| 476 | * @url PUT {id}/tasks/{taskid} |
||
| 477 | * |
||
| 478 | * @return object |
||
| 479 | */ |
||
| 480 | /* |
||
| 481 | public function putLine($id, $lineid, $request_data = null) |
||
| 482 | { |
||
| 483 | if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) { |
||
| 484 | throw new RestException(403); |
||
| 485 | } |
||
| 486 | |||
| 487 | $result = $this->project->fetch($id); |
||
| 488 | if( ! $result ) { |
||
| 489 | throw new RestException(404, 'Project not found'); |
||
| 490 | } |
||
| 491 | |||
| 492 | if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) { |
||
| 493 | throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 494 | } |
||
| 495 | |||
| 496 | $request_data = (object) $request_data; |
||
| 497 | |||
| 498 | $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml'); |
||
| 499 | |||
| 500 | $updateRes = $this->project->updateline( |
||
| 501 | $lineid, |
||
| 502 | $request_data->desc, |
||
| 503 | $request_data->subprice, |
||
| 504 | $request_data->qty, |
||
| 505 | $request_data->remise_percent, |
||
| 506 | $request_data->tva_tx, |
||
| 507 | $request_data->localtax1_tx, |
||
| 508 | $request_data->localtax2_tx, |
||
| 509 | 'HT', |
||
| 510 | $request_data->info_bits, |
||
| 511 | $request_data->date_start, |
||
| 512 | $request_data->date_end, |
||
| 513 | $request_data->product_type, |
||
| 514 | $request_data->fk_parent_line, |
||
| 515 | 0, |
||
| 516 | $request_data->fk_fournprice, |
||
| 517 | $request_data->pa_ht, |
||
| 518 | $request_data->label, |
||
| 519 | $request_data->special_code, |
||
| 520 | $request_data->array_options, |
||
| 521 | $request_data->fk_unit |
||
| 522 | ); |
||
| 523 | |||
| 524 | if ($updateRes > 0) { |
||
| 525 | $result = $this->get($id); |
||
| 526 | unset($result->line); |
||
| 527 | return $this->_cleanObjectDatas($result); |
||
| 528 | } |
||
| 529 | return false; |
||
| 530 | }*/ |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get roles a user is assigned to a project with |
||
| 534 | * |
||
| 535 | * @param int $id Id of project |
||
| 536 | * @param int $userid Id of user (0 = connected user) |
||
| 537 | * @return array |
||
| 538 | * |
||
| 539 | * @url GET {id}/roles |
||
| 540 | */ |
||
| 541 | public function getRoles($id, $userid = 0) |
||
| 542 | { |
||
| 543 | global $db; |
||
| 544 | |||
| 545 | if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) { |
||
| 546 | throw new RestException(403); |
||
| 547 | } |
||
| 548 | |||
| 549 | $result = $this->project->fetch($id); |
||
| 550 | if (!$result) { |
||
| 551 | throw new RestException(404, 'Project not found'); |
||
| 552 | } |
||
| 553 | |||
| 554 | if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) { |
||
| 555 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 556 | } |
||
| 557 | |||
| 558 | $taskstatic = new Task($this->db); |
||
| 559 | $userp = DolibarrApiAccess::$user; |
||
| 560 | if ($userid > 0) { |
||
| 561 | $userp = new User($this->db); |
||
| 562 | $userp->fetch($userid); |
||
| 563 | } |
||
| 564 | $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, $id, 0); |
||
|
|
|||
| 565 | $result = array(); |
||
| 566 | foreach ($this->project->roles as $line) { |
||
| 567 | array_push($result, $this->_cleanObjectDatas($line)); |
||
| 568 | } |
||
| 569 | |||
| 570 | return $result; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Update project general fields (won't touch lines of project) |
||
| 575 | * |
||
| 576 | * @param int $id Id of project to update |
||
| 577 | * @param array $request_data Datas |
||
| 578 | * @return Array Updated object |
||
| 579 | * |
||
| 580 | * @throws RestException |
||
| 581 | */ |
||
| 582 | public function put($id, $request_data = null) |
||
| 583 | { |
||
| 584 | if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) { |
||
| 585 | throw new RestException(403); |
||
| 586 | } |
||
| 587 | |||
| 588 | $result = $this->project->fetch($id); |
||
| 589 | if ($result <= 0) { |
||
| 590 | throw new RestException(404, 'Project not found'); |
||
| 591 | } |
||
| 592 | |||
| 593 | if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) { |
||
| 594 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 595 | } |
||
| 596 | foreach ($request_data as $field => $value) { |
||
| 597 | if ($field == 'id') { |
||
| 598 | continue; |
||
| 599 | } |
||
| 600 | if ($field === 'caller') { |
||
| 601 | // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller |
||
| 602 | $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
||
| 603 | continue; |
||
| 604 | } |
||
| 605 | if ($field == 'array_options' && is_array($value)) { |
||
| 606 | foreach ($value as $index => $val) { |
||
| 607 | $this->project->array_options[$index] = $this->_checkValForAPI($field, $val, $this->project); |
||
| 608 | } |
||
| 609 | continue; |
||
| 610 | } |
||
| 611 | |||
| 612 | $this->project->$field = $this->_checkValForAPI($field, $value, $this->project); |
||
| 613 | } |
||
| 614 | |||
| 615 | if ($this->project->update(DolibarrApiAccess::$user) >= 0) { |
||
| 616 | return $this->get($id); |
||
| 617 | } else { |
||
| 618 | throw new RestException(500, $this->project->error); |
||
| 619 | } |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Get properties of a project object |
||
| 624 | * |
||
| 625 | * Return an array with project information |
||
| 626 | * |
||
| 627 | * @param int $id ID of project |
||
| 628 | * @return array Object with cleaned properties |
||
| 629 | * |
||
| 630 | * @throws RestException |
||
| 631 | */ |
||
| 632 | public function get($id) |
||
| 633 | { |
||
| 634 | if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) { |
||
| 635 | throw new RestException(403); |
||
| 636 | } |
||
| 637 | |||
| 638 | $result = $this->project->fetch($id); |
||
| 639 | if (!$result) { |
||
| 640 | throw new RestException(404, 'Project with supplied id not found'); |
||
| 641 | } |
||
| 642 | |||
| 643 | if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) { |
||
| 644 | throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 645 | } |
||
| 646 | |||
| 647 | $this->project->fetchObjectLinked(); |
||
| 648 | return $this->_cleanObjectDatas($this->project); |
||
| 649 | } |
||
| 650 | |||
| 651 | |||
| 652 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Delete project |
||
| 656 | * |
||
| 657 | * @param int $id Project ID |
||
| 658 | * |
||
| 659 | * @return array |
||
| 660 | * |
||
| 661 | * @throws RestException |
||
| 662 | */ |
||
| 663 | public function delete($id) |
||
| 685 | ) |
||
| 686 | ); |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Validate a project. |
||
| 691 | * You can test this API with the following input message |
||
| 692 | * { "notrigger": 0 } |
||
| 693 | * |
||
| 694 | * @param int $id Project ID |
||
| 695 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 696 | * |
||
| 697 | * @url POST {id}/validate |
||
| 698 | * |
||
| 699 | * @return array |
||
| 700 | * |
||
| 701 | * FIXME An error 403 is returned if the request has an empty body. |
||
| 702 | * Error message: "Forbidden: Content type `text/plain` is not supported." |
||
| 703 | * Workaround: send this in the body |
||
| 704 | * { |
||
| 705 | * "notrigger": 0 |
||
| 706 | * } |
||
| 707 | * |
||
| 708 | * @throws RestException |
||
| 709 | */ |
||
| 710 | public function validate($id, $notrigger = 0) |
||
| 736 | ) |
||
| 737 | ); |
||
| 738 | } |
||
| 739 | |||
| 744 |