dolibarr 20.0.2
api_recruitments.class.php
1<?php
2/* Copyright (C) 2022 Thibault FOUCART <support@ptibogxiv.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18use Luracast\Restler\RestException;
19
20dol_include_once('/recruitment/class/recruitmentjobposition.class.php');
21dol_include_once('/recruitment/class/recruitmentcandidature.class.php');
22
23
24
38{
42 public $jobposition;
46 public $candidature;
47
48
55 public function __construct()
56 {
57 global $db;
58 $this->db = $db;
59 $this->jobposition = new RecruitmentJobPosition($this->db);
60 $this->candidature = new RecruitmentCandidature($this->db);
61 }
62
63
77 public function getJobPosition($id)
78 {
79 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
80 throw new RestException(403);
81 }
82
83 $result = $this->jobposition->fetch($id);
84 if (!$result) {
85 throw new RestException(404, 'JobPosition not found');
86 }
87
88 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
89 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
90 }
91
92 return $this->_cleanObjectDatas($this->jobposition);
93 }
94
108 public function getCandidature($id)
109 {
110 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
111 throw new RestException(403);
112 }
113
114 $result = $this->candidature->fetch($id);
115 if (!$result) {
116 throw new RestException(404, 'Candidature not found');
117 }
118
119 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
120 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
121 }
122
123 return $this->_cleanObjectDatas($this->candidature);
124 }
125
126
144 public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
145 {
146 $obj_ret = array();
147 $tmpobject = new RecruitmentJobPosition($this->db);
148
149 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
150 throw new RestException(403);
151 }
152
153 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
154
155 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
156
157 // If the internal user must only see his customers, force searching by him
158 $search_sale = 0;
159 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
160 $search_sale = DolibarrApiAccess::$user->id;
161 }
162
163 $sql = "SELECT t.rowid";
164 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
165 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
166 $sql .= " WHERE 1 = 1";
167 if ($tmpobject->ismultientitymanaged) {
168 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
169 }
170 if ($restrictonsocid && $socid) {
171 $sql .= " AND t.fk_soc = ".((int) $socid);
172 }
173 // Search on sale representative
174 if ($search_sale && $search_sale != '-1') {
175 if ($search_sale == -2) {
176 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
177 } elseif ($search_sale > 0) {
178 $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).")";
179 }
180 }
181 if ($sqlfilters) {
182 $errormessage = '';
183 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
184 if ($errormessage) {
185 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
186 }
187 }
188
189 $sql .= $this->db->order($sortfield, $sortorder);
190 if ($limit) {
191 if ($page < 0) {
192 $page = 0;
193 }
194 $offset = $limit * $page;
195
196 $sql .= $this->db->plimit($limit + 1, $offset);
197 }
198
199 $result = $this->db->query($sql);
200 $i = 0;
201 if ($result) {
202 $num = $this->db->num_rows($result);
203 while ($i < $num) {
204 $obj = $this->db->fetch_object($result);
205 $tmp_object = new RecruitmentJobPosition($this->db);
206 if ($tmp_object->fetch($obj->rowid)) {
207 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
208 }
209 $i++;
210 }
211 } else {
212 throw new RestException(503, 'Error when retrieving jobposition list: '.$this->db->lasterror());
213 }
214
215 return $obj_ret;
216 }
217
234 public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
235 {
236 global $db, $conf;
237
238 $obj_ret = array();
239 $tmpobject = new RecruitmentCandidature($this->db);
240
241 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
242 throw new RestException(403);
243 }
244
245 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
246
247 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
248
249 // If the internal user must only see his customers, force searching by him
250 $search_sale = 0;
251 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
252 $search_sale = DolibarrApiAccess::$user->id;
253 }
254
255 $sql = "SELECT t.rowid";
256 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
257 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
258 $sql .= " WHERE 1 = 1";
259 if ($tmpobject->ismultientitymanaged) {
260 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
261 }
262 if ($restrictonsocid && $socid) {
263 $sql .= " AND t.fk_soc = ".((int) $socid);
264 }
265 // Search on sale representative
266 if ($search_sale && $search_sale != '-1') {
267 if ($search_sale == -2) {
268 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
269 } elseif ($search_sale > 0) {
270 $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).")";
271 }
272 }
273 if ($sqlfilters) {
274 $errormessage = '';
275 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
276 if ($errormessage) {
277 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
278 }
279 }
280
281 $sql .= $this->db->order($sortfield, $sortorder);
282 if ($limit) {
283 if ($page < 0) {
284 $page = 0;
285 }
286 $offset = $limit * $page;
287
288 $sql .= $this->db->plimit($limit + 1, $offset);
289 }
290
291 $result = $this->db->query($sql);
292 $i = 0;
293 if ($result) {
294 $num = $this->db->num_rows($result);
295 while ($i < $num) {
296 $obj = $this->db->fetch_object($result);
297 $tmp_object = new RecruitmentCandidature($this->db);
298 if ($tmp_object->fetch($obj->rowid)) {
299 $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
300 }
301 $i++;
302 }
303 } else {
304 throw new RestException(503, 'Error when retrieving candidature list: '.$this->db->lasterror());
305 }
306
307 return $obj_ret;
308 }
309
320 public function postJobPosition($request_data = null)
321 {
322 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
323 throw new RestException(403);
324 }
325
326 // Check mandatory fields
327 $result = $this->_validate($request_data);
328
329 foreach ($request_data as $field => $value) {
330 if ($field === 'caller') {
331 // 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
332 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
333 continue;
334 }
335
336 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
337 }
338
339 // Clean data
340 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
341
342 if ($this->jobposition->create(DolibarrApiAccess::$user)<0) {
343 throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
344 }
345 return $this->jobposition->id;
346 }
347
358 public function postCandidature($request_data = null)
359 {
360 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
361 throw new RestException(403);
362 }
363
364 // Check mandatory fields
365 $result = $this->_validate($request_data);
366
367 foreach ($request_data as $field => $value) {
368 if ($field === 'caller') {
369 // 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
370 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
371 continue;
372 }
373
374 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
375 }
376
377 // Clean data
378 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
379
380 if ($this->candidature->create(DolibarrApiAccess::$user)<0) {
381 throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
382 }
383 return $this->candidature->id;
384 }
385
397 public function putJobPosition($id, $request_data = null)
398 {
399 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
400 throw new RestException(403);
401 }
402
403 $result = $this->jobposition->fetch($id);
404 if (!$result) {
405 throw new RestException(404, 'jobposition not found');
406 }
407
408 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
409 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
410 }
411
412 foreach ($request_data as $field => $value) {
413 if ($field == 'id') {
414 continue;
415 }
416 if ($field === 'caller') {
417 // 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
418 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
419 continue;
420 }
421
422 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
423 }
424
425 // Clean data
426 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
427
428 if ($this->jobposition->update(DolibarrApiAccess::$user, false) > 0) {
429 return $this->getJobPosition($id);
430 } else {
431 throw new RestException(500, $this->jobposition->error);
432 }
433 }
434
446 public function putCandidature($id, $request_data = null)
447 {
448 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
449 throw new RestException(403);
450 }
451
452 $result = $this->candidature->fetch($id);
453 if (!$result) {
454 throw new RestException(404, 'candidature not found');
455 }
456
457 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
458 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
459 }
460
461 foreach ($request_data as $field => $value) {
462 if ($field == 'id') {
463 continue;
464 }
465 if ($field === 'caller') {
466 // 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
467 $this->candidature->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
468 continue;
469 }
470
471 $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
472 }
473
474 // Clean data
475 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
476
477 if ($this->candidature->update(DolibarrApiAccess::$user, false) > 0) {
478 return $this->getCandidature($id);
479 } else {
480 throw new RestException(500, $this->candidature->error);
481 }
482 }
483
484
495 public function deleteJobPosition($id)
496 {
497 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
498 throw new RestException(403);
499 }
500 $result = $this->jobposition->fetch($id);
501 if (!$result) {
502 throw new RestException(404, 'jobposition not found');
503 }
504
505 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
506 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
507 }
508
509 if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
510 throw new RestException(500, 'Error when deleting jobposition : '.$this->jobposition->error);
511 }
512
513 return array(
514 'success' => array(
515 'code' => 200,
516 'message' => 'jobposition deleted'
517 )
518 );
519 }
520
531 public function deleteCandidature($id)
532 {
533 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
534 throw new RestException(403);
535 }
536 $result = $this->candidature->fetch($id);
537 if (!$result) {
538 throw new RestException(404, 'candidature not found');
539 }
540
541 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
542 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
543 }
544
545 if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
546 throw new RestException(500, 'Error when deleting candidature : '.$this->candidature->error);
547 }
548
549 return array(
550 'success' => array(
551 'code' => 200,
552 'message' => 'candidature deleted'
553 )
554 );
555 }
556
557
558 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
565 protected function _cleanObjectDatas($object)
566 {
567 // phpcs:enable
568 $object = parent::_cleanObjectDatas($object);
569
570 unset($object->rowid);
571 unset($object->canvas);
572
573 /*unset($object->name);
574 unset($object->lastname);
575 unset($object->firstname);
576 unset($object->civility_id);
577 unset($object->statut);
578 unset($object->state);
579 unset($object->state_id);
580 unset($object->state_code);
581 unset($object->region);
582 unset($object->region_code);
583 unset($object->country);
584 unset($object->country_id);
585 unset($object->country_code);
586 unset($object->barcode_type);
587 unset($object->barcode_type_code);
588 unset($object->barcode_type_label);
589 unset($object->barcode_type_coder);
590 unset($object->total_ht);
591 unset($object->total_tva);
592 unset($object->total_localtax1);
593 unset($object->total_localtax2);
594 unset($object->total_ttc);
595 unset($object->fk_account);
596 unset($object->comments);
597 unset($object->note);
598 unset($object->mode_reglement_id);
599 unset($object->cond_reglement_id);
600 unset($object->cond_reglement);
601 unset($object->shipping_method_id);
602 unset($object->fk_incoterms);
603 unset($object->label_incoterms);
604 unset($object->location_incoterms);
605 */
606
607 // If object has lines, remove $db property
608 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
609 $nboflines = count($object->lines);
610 for ($i = 0; $i < $nboflines; $i++) {
611 $this->_cleanObjectDatas($object->lines[$i]);
612
613 unset($object->lines[$i]->lines);
614 unset($object->lines[$i]->note);
615 }
616 }
617
618 return $object;
619 }
620
629 private function _validate($data)
630 {
631 $jobposition = array();
632 foreach ($this->jobposition->fields as $field => $propfield) {
633 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
634 continue; // Not a mandatory field
635 }
636 if (!isset($data[$field])) {
637 throw new RestException(400, "$field field missing");
638 }
639 $jobposition[$field] = $data[$field];
640 }
641 return $jobposition;
642 }
643}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class for API REST v1.
Definition api.class.php:30
_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.
Definition api.class.php:82
Class for RecruitmentCandidature.
Class for RecruitmentJobPosition.
getJobPosition($id)
Get properties of a jobposition object.
deleteJobPosition($id)
Delete jobposition.
__construct()
Constructor.
putCandidature($id, $request_data=null)
Update candidature.
deleteCandidature($id)
Delete candidature.
postJobPosition($request_data=null)
Create jobposition object.
_cleanObjectDatas($object)
Clean sensible object datas.
indexCandidature($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List candatures.
_validate($data)
Validate fields before create or update object.
putJobPosition($id, $request_data=null)
Update jobposition.
indexJobPosition($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List jobpositions.
getCandidature($id)
Get properties of a candidature object.
postCandidature($request_data=null)
Create candidature object.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.