dolibarr 21.0.0-alpha
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
144 public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
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 //this query will return total orders with the filters given
190 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
191
192 $sql .= $this->db->order($sortfield, $sortorder);
193 if ($limit) {
194 if ($page < 0) {
195 $page = 0;
196 }
197 $offset = $limit * $page;
198
199 $sql .= $this->db->plimit($limit + 1, $offset);
200 }
201
202 $result = $this->db->query($sql);
203 $i = 0;
204 if ($result) {
205 $num = $this->db->num_rows($result);
206 while ($i < $num) {
207 $obj = $this->db->fetch_object($result);
208 $tmp_object = new RecruitmentJobPosition($this->db);
209 if ($tmp_object->fetch($obj->rowid)) {
210 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
211 }
212 $i++;
213 }
214 } else {
215 throw new RestException(503, 'Error when retrieving jobposition list: '.$this->db->lasterror());
216 }
217
218 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
219 if ($pagination_data) {
220 $totalsResult = $this->db->query($sqlTotals);
221 $total = $this->db->fetch_object($totalsResult)->total;
222
223 $tmp = $obj_ret;
224 $obj_ret = [];
225
226 $obj_ret['data'] = $tmp;
227 $obj_ret['pagination'] = [
228 'total' => (int) $total,
229 'page' => $page, //count starts from 0
230 'page_count' => ceil((int) $total / $limit),
231 'limit' => $limit
232 ];
233 }
234
235 return $obj_ret;
236 }
237
256 public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
257 {
258 global $db, $conf;
259
260 $obj_ret = array();
261 $tmpobject = new RecruitmentCandidature($this->db);
262
263 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
264 throw new RestException(403);
265 }
266
267 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
268
269 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
270
271 // If the internal user must only see his customers, force searching by him
272 $search_sale = 0;
273 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
274 $search_sale = DolibarrApiAccess::$user->id;
275 }
276
277 $sql = "SELECT t.rowid";
278 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
279 $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
280 $sql .= " WHERE 1 = 1";
281 if ($tmpobject->ismultientitymanaged) {
282 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
283 }
284 if ($restrictonsocid && $socid) {
285 $sql .= " AND t.fk_soc = ".((int) $socid);
286 }
287 // Search on sale representative
288 if ($search_sale && $search_sale != '-1') {
289 if ($search_sale == -2) {
290 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
291 } elseif ($search_sale > 0) {
292 $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).")";
293 }
294 }
295 if ($sqlfilters) {
296 $errormessage = '';
297 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
298 if ($errormessage) {
299 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
300 }
301 }
302
303 //this query will return total orders with the filters given
304 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
305
306 $sql .= $this->db->order($sortfield, $sortorder);
307 if ($limit) {
308 if ($page < 0) {
309 $page = 0;
310 }
311 $offset = $limit * $page;
312
313 $sql .= $this->db->plimit($limit + 1, $offset);
314 }
315
316 $result = $this->db->query($sql);
317 $i = 0;
318 if ($result) {
319 $num = $this->db->num_rows($result);
320 while ($i < $num) {
321 $obj = $this->db->fetch_object($result);
322 $tmp_object = new RecruitmentCandidature($this->db);
323 if ($tmp_object->fetch($obj->rowid)) {
324 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
325 }
326 $i++;
327 }
328 } else {
329 throw new RestException(503, 'Error when retrieving candidature list: '.$this->db->lasterror());
330 }
331
332 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
333 if ($pagination_data) {
334 $totalsResult = $this->db->query($sqlTotals);
335 $total = $this->db->fetch_object($totalsResult)->total;
336
337 $tmp = $obj_ret;
338 $obj_ret = [];
339
340 $obj_ret['data'] = $tmp;
341 $obj_ret['pagination'] = [
342 'total' => (int) $total,
343 'page' => $page, //count starts from 0
344 'page_count' => ceil((int) $total / $limit),
345 'limit' => $limit
346 ];
347 }
348
349 return $obj_ret;
350 }
351
362 public function postJobPosition($request_data = null)
363 {
364 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
365 throw new RestException(403);
366 }
367
368 // Check mandatory fields
369 $result = $this->_validate($request_data);
370
371 foreach ($request_data as $field => $value) {
372 if ($field === 'caller') {
373 // 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
374 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
375 continue;
376 }
377
378 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
379 }
380
381 // Clean data
382 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
383
384 if ($this->jobposition->create(DolibarrApiAccess::$user)<0) {
385 throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
386 }
387 return $this->jobposition->id;
388 }
389
400 public function postCandidature($request_data = null)
401 {
402 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
403 throw new RestException(403);
404 }
405
406 // Check mandatory fields
407 $result = $this->_validate($request_data);
408
409 foreach ($request_data as $field => $value) {
410 if ($field === 'caller') {
411 // 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
412 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
413 continue;
414 }
415
416 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
417 }
418
419 // Clean data
420 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
421
422 if ($this->candidature->create(DolibarrApiAccess::$user)<0) {
423 throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
424 }
425 return $this->candidature->id;
426 }
427
439 public function putJobPosition($id, $request_data = null)
440 {
441 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
442 throw new RestException(403);
443 }
444
445 $result = $this->jobposition->fetch($id);
446 if (!$result) {
447 throw new RestException(404, 'jobposition not found');
448 }
449
450 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
451 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
452 }
453
454 foreach ($request_data as $field => $value) {
455 if ($field == 'id') {
456 continue;
457 }
458 if ($field === 'caller') {
459 // 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
460 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
461 continue;
462 }
463
464 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
465 }
466
467 // Clean data
468 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
469
470 if ($this->jobposition->update(DolibarrApiAccess::$user, false) > 0) {
471 return $this->getJobPosition($id);
472 } else {
473 throw new RestException(500, $this->jobposition->error);
474 }
475 }
476
488 public function putCandidature($id, $request_data = null)
489 {
490 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
491 throw new RestException(403);
492 }
493
494 $result = $this->candidature->fetch($id);
495 if (!$result) {
496 throw new RestException(404, 'candidature not found');
497 }
498
499 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
500 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
501 }
502
503 foreach ($request_data as $field => $value) {
504 if ($field == 'id') {
505 continue;
506 }
507 if ($field === 'caller') {
508 // 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
509 $this->candidature->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
510 continue;
511 }
512
513 $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
514 }
515
516 // Clean data
517 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
518
519 if ($this->candidature->update(DolibarrApiAccess::$user, false) > 0) {
520 return $this->getCandidature($id);
521 } else {
522 throw new RestException(500, $this->candidature->error);
523 }
524 }
525
526
537 public function deleteJobPosition($id)
538 {
539 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
540 throw new RestException(403);
541 }
542 $result = $this->jobposition->fetch($id);
543 if (!$result) {
544 throw new RestException(404, 'jobposition not found');
545 }
546
547 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
548 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
549 }
550
551 if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
552 throw new RestException(500, 'Error when deleting jobposition : '.$this->jobposition->error);
553 }
554
555 return array(
556 'success' => array(
557 'code' => 200,
558 'message' => 'jobposition deleted'
559 )
560 );
561 }
562
573 public function deleteCandidature($id)
574 {
575 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
576 throw new RestException(403);
577 }
578 $result = $this->candidature->fetch($id);
579 if (!$result) {
580 throw new RestException(404, 'candidature not found');
581 }
582
583 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
584 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
585 }
586
587 if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
588 throw new RestException(500, 'Error when deleting candidature : '.$this->candidature->error);
589 }
590
591 return array(
592 'success' => array(
593 'code' => 200,
594 'message' => 'candidature deleted'
595 )
596 );
597 }
598
599
600 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
607 protected function _cleanObjectDatas($object)
608 {
609 // phpcs:enable
610 $object = parent::_cleanObjectDatas($object);
611
612 unset($object->rowid);
613 unset($object->canvas);
614
615 /*unset($object->name);
616 unset($object->lastname);
617 unset($object->firstname);
618 unset($object->civility_id);
619 unset($object->statut);
620 unset($object->state);
621 unset($object->state_id);
622 unset($object->state_code);
623 unset($object->region);
624 unset($object->region_code);
625 unset($object->country);
626 unset($object->country_id);
627 unset($object->country_code);
628 unset($object->barcode_type);
629 unset($object->barcode_type_code);
630 unset($object->barcode_type_label);
631 unset($object->barcode_type_coder);
632 unset($object->total_ht);
633 unset($object->total_tva);
634 unset($object->total_localtax1);
635 unset($object->total_localtax2);
636 unset($object->total_ttc);
637 unset($object->fk_account);
638 unset($object->comments);
639 unset($object->note);
640 unset($object->mode_reglement_id);
641 unset($object->cond_reglement_id);
642 unset($object->cond_reglement);
643 unset($object->shipping_method_id);
644 unset($object->fk_incoterms);
645 unset($object->label_incoterms);
646 unset($object->location_incoterms);
647 */
648
649 // If object has lines, remove $db property
650 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
651 $nboflines = count($object->lines);
652 for ($i = 0; $i < $nboflines; $i++) {
653 $this->_cleanObjectDatas($object->lines[$i]);
654
655 unset($object->lines[$i]->lines);
656 unset($object->lines[$i]->note);
657 }
658 }
659
660 return $object;
661 }
662
671 private function _validate($data)
672 {
673 $jobposition = array();
674 foreach ($this->jobposition->fields as $field => $propfield) {
675 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
676 continue; // Not a mandatory field
677 }
678 if (!isset($data[$field])) {
679 throw new RestException(400, "$field field missing");
680 }
681 $jobposition[$field] = $data[$field];
682 }
683 return $jobposition;
684 }
685}
$id
Definition account.php:39
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.
indexJobPosition($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List jobpositions.
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.
_validate($data)
Validate fields before create or update object.
putJobPosition($id, $request_data=null)
Update jobposition.
getCandidature($id)
Get properties of a candidature object.
indexCandidature($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List candatures.
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.