dolibarr 22.0.5
api_recruitments.class.php
1<?php
2/* Copyright (C) 2022 Thibault FOUCART <support@ptibogxiv.net>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21dol_include_once('/recruitment/class/recruitmentjobposition.class.php');
22dol_include_once('/recruitment/class/recruitmentcandidature.class.php');
23
24
25
39{
43 public $jobposition;
47 public $candidature;
48
49
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
146 public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
147 {
148 $obj_ret = array();
149 $tmpobject = new RecruitmentJobPosition($this->db);
150
151 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
152 throw new RestException(403);
153 }
154
155 $socid = DolibarrApiAccess::$user->socid ?: 0;
156
157 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
158
159 // If the internal user must only see his customers, force searching by him
160 $search_sale = 0;
161 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
162 $search_sale = DolibarrApiAccess::$user->id;
163 }
164
165 $sql = "SELECT t.rowid";
166 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
167 $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
168 $sql .= " WHERE 1 = 1";
169 if ($tmpobject->ismultientitymanaged) {
170 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
171 }
172 if ($restrictonsocid && $socid) {
173 $sql .= " AND t.fk_soc = ".((int) $socid);
174 }
175 // Search on sale representative
176 if ($search_sale && $search_sale != '-1') {
177 if ($search_sale == -2) {
178 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
179 } elseif ($search_sale > 0) {
180 $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).")";
181 }
182 }
183 if ($sqlfilters) {
184 $errormessage = '';
185 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
186 if ($errormessage) {
187 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
188 }
189 }
190
191 //this query will return total orders with the filters given
192 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
193
194 $sql .= $this->db->order($sortfield, $sortorder);
195 if ($limit) {
196 if ($page < 0) {
197 $page = 0;
198 }
199 $offset = $limit * $page;
200
201 $sql .= $this->db->plimit($limit + 1, $offset);
202 }
203
204 $result = $this->db->query($sql);
205 $i = 0;
206 if ($result) {
207 $num = $this->db->num_rows($result);
208 while ($i < $num) {
209 $obj = $this->db->fetch_object($result);
210 $tmp_object = new RecruitmentJobPosition($this->db);
211 if ($tmp_object->fetch($obj->rowid)) {
212 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
213 }
214 $i++;
215 }
216 } else {
217 throw new RestException(503, 'Error when retrieving jobposition list: '.$this->db->lasterror());
218 }
219
220 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
221 if ($pagination_data) {
222 $totalsResult = $this->db->query($sqlTotals);
223 $total = $this->db->fetch_object($totalsResult)->total;
224
225 $tmp = $obj_ret;
226 $obj_ret = [];
227
228 $obj_ret['data'] = $tmp;
229 $obj_ret['pagination'] = [
230 'total' => (int) $total,
231 'page' => $page, //count starts from 0
232 'page_count' => ceil((int) $total / $limit),
233 'limit' => $limit
234 ];
235 }
236
237 return $obj_ret;
238 }
239
260 public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
261 {
262 global $db, $conf;
263
264 $obj_ret = array();
265 $tmpobject = new RecruitmentCandidature($this->db);
266
267 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
268 throw new RestException(403);
269 }
270
271 $socid = DolibarrApiAccess::$user->socid ?: 0;
272
273 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
274
275 // If the internal user must only see his customers, force searching by him
276 $search_sale = 0;
277 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
278 $search_sale = DolibarrApiAccess::$user->id;
279 }
280
281 $sql = "SELECT t.rowid";
282 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
283 $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
284 $sql .= " WHERE 1 = 1";
285 if ($tmpobject->ismultientitymanaged) {
286 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
287 }
288 if ($restrictonsocid && $socid) {
289 $sql .= " AND t.fk_soc = ".((int) $socid);
290 }
291 // Search on sale representative
292 if ($search_sale && $search_sale != '-1') {
293 if ($search_sale == -2) {
294 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
295 } elseif ($search_sale > 0) {
296 $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).")";
297 }
298 }
299 if ($sqlfilters) {
300 $errormessage = '';
301 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
302 if ($errormessage) {
303 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
304 }
305 }
306
307 //this query will return total orders with the filters given
308 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
309
310 $sql .= $this->db->order($sortfield, $sortorder);
311 if ($limit) {
312 if ($page < 0) {
313 $page = 0;
314 }
315 $offset = $limit * $page;
316
317 $sql .= $this->db->plimit($limit + 1, $offset);
318 }
319
320 $result = $this->db->query($sql);
321 $i = 0;
322 if ($result) {
323 $num = $this->db->num_rows($result);
324 while ($i < $num) {
325 $obj = $this->db->fetch_object($result);
326 $tmp_object = new RecruitmentCandidature($this->db);
327 if ($tmp_object->fetch($obj->rowid)) {
328 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
329 }
330 $i++;
331 }
332 } else {
333 throw new RestException(503, 'Error when retrieving candidature list: '.$this->db->lasterror());
334 }
335
336 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
337 if ($pagination_data) {
338 $totalsResult = $this->db->query($sqlTotals);
339 $total = $this->db->fetch_object($totalsResult)->total;
340
341 $tmp = $obj_ret;
342 $obj_ret = [];
343
344 $obj_ret['data'] = $tmp;
345 $obj_ret['pagination'] = [
346 'total' => (int) $total,
347 'page' => $page, //count starts from 0
348 'page_count' => ceil((int) $total / $limit),
349 'limit' => $limit
350 ];
351 }
352
353 return $obj_ret;
354 }
355
368 public function postJobPosition($request_data = null)
369 {
370 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
371 throw new RestException(403);
372 }
373
374 // Check mandatory fields
375 $result = $this->_validate($request_data, $this->jobposition);
376
377 foreach ($request_data as $field => $value) {
378 if ($field === 'caller') {
379 // 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
380 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
381 continue;
382 }
383
384 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
385 }
386
387 // Clean data
388 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
389
390 if ($this->jobposition->create(DolibarrApiAccess::$user) < 0) {
391 throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
392 }
393 return $this->jobposition->id;
394 }
395
408 public function postCandidature($request_data = null)
409 {
410 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
411 throw new RestException(403);
412 }
413
414 // Check mandatory fields. Validate against the candidature model so the API
415 // rejects with the actual missing candidature fields (e.g. email) instead of
416 // the unrelated job position fields (see issue #38429).
417 $result = $this->_validate($request_data, $this->candidature);
418
419 foreach ($request_data as $field => $value) {
420 if ($field === 'caller') {
421 // 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
422 $this->candidature->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
423 continue;
424 }
425
426 $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
427 }
428
429 // Clean data
430 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
431
432 if ($this->candidature->create(DolibarrApiAccess::$user) < 0) {
433 throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
434 }
435 return $this->candidature->id;
436 }
437
451 public function putJobPosition($id, $request_data = null)
452 {
453 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
454 throw new RestException(403);
455 }
456
457 $result = $this->jobposition->fetch($id);
458 if (!$result) {
459 throw new RestException(404, 'jobposition not found');
460 }
461
462 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
463 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
464 }
465
466 foreach ($request_data as $field => $value) {
467 if ($field == 'id') {
468 continue;
469 }
470 if ($field === 'caller') {
471 // 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
472 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
473 continue;
474 }
475
476 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
477 }
478
479 // Clean data
480 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
481
482 if ($this->jobposition->update(DolibarrApiAccess::$user, 0) > 0) {
483 return $this->getJobPosition($id);
484 } else {
485 throw new RestException(500, $this->jobposition->error);
486 }
487 }
488
502 public function putCandidature($id, $request_data = null)
503 {
504 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
505 throw new RestException(403);
506 }
507
508 $result = $this->candidature->fetch($id);
509 if (!$result) {
510 throw new RestException(404, 'candidature not found');
511 }
512
513 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
514 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
515 }
516
517 foreach ($request_data as $field => $value) {
518 if ($field == 'id') {
519 continue;
520 }
521 if ($field === 'caller') {
522 // 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
523 $this->candidature->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
524 continue;
525 }
526
527 $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
528 }
529
530 // Clean data
531 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
532
533 if ($this->candidature->update(DolibarrApiAccess::$user, 0) > 0) {
534 return $this->getCandidature($id);
535 } else {
536 throw new RestException(500, $this->candidature->error);
537 }
538 }
539
540
553 public function deleteJobPosition($id)
554 {
555 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
556 throw new RestException(403);
557 }
558 $result = $this->jobposition->fetch($id);
559 if (!$result) {
560 throw new RestException(404, 'jobposition not found');
561 }
562
563 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
564 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
565 }
566
567 if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
568 throw new RestException(500, 'Error when deleting jobposition : '.$this->jobposition->error);
569 }
570
571 return array(
572 'success' => array(
573 'code' => 200,
574 'message' => 'jobposition deleted'
575 )
576 );
577 }
578
591 public function deleteCandidature($id)
592 {
593 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
594 throw new RestException(403);
595 }
596 $result = $this->candidature->fetch($id);
597 if (!$result) {
598 throw new RestException(404, 'candidature not found');
599 }
600
601 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
602 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
603 }
604
605 if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
606 throw new RestException(500, 'Error when deleting candidature : '.$this->candidature->error);
607 }
608
609 return array(
610 'success' => array(
611 'code' => 200,
612 'message' => 'candidature deleted'
613 )
614 );
615 }
616
617
618 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
625 protected function _cleanObjectDatas($object)
626 {
627 // phpcs:enable
628 $object = parent::_cleanObjectDatas($object);
629
630 unset($object->rowid);
631 unset($object->canvas);
632
633 /*unset($object->name);
634 unset($object->lastname);
635 unset($object->firstname);
636 unset($object->civility_id);
637 unset($object->statut);
638 unset($object->state);
639 unset($object->state_id);
640 unset($object->state_code);
641 unset($object->region);
642 unset($object->region_code);
643 unset($object->country);
644 unset($object->country_id);
645 unset($object->country_code);
646 unset($object->barcode_type);
647 unset($object->barcode_type_code);
648 unset($object->barcode_type_label);
649 unset($object->barcode_type_coder);
650 unset($object->total_ht);
651 unset($object->total_tva);
652 unset($object->total_localtax1);
653 unset($object->total_localtax2);
654 unset($object->total_ttc);
655 unset($object->fk_account);
656 unset($object->comments);
657 unset($object->note);
658 unset($object->mode_reglement_id);
659 unset($object->cond_reglement_id);
660 unset($object->cond_reglement);
661 unset($object->shipping_method_id);
662 unset($object->fk_incoterms);
663 unset($object->label_incoterms);
664 unset($object->location_incoterms);
665 */
666
667 // If object has lines, remove $db property
668 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
669 $nboflines = count($object->lines);
670 for ($i = 0; $i < $nboflines; $i++) {
671 $this->_cleanObjectDatas($object->lines[$i]);
672
673 unset($object->lines[$i]->lines);
674 unset($object->lines[$i]->note);
675 }
676 }
677
678 return $object;
679 }
680
690 private function _validate($data, $object)
691 {
692 if ($data === null) {
693 $data = array();
694 }
695 $result = array();
696 foreach ($object->fields as $field => $propfield) {
697 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || empty($propfield['notnull']) || $propfield['notnull'] != 1) {
698 continue; // Not a mandatory field
699 }
700 if (!isset($data[$field])) {
701 throw new RestException(400, "$field field missing");
702 }
703 $result[$field] = $data[$field];
704 }
705 return $result;
706 }
707}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_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:98
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.
putJobPosition($id, $request_data=null)
Update jobposition.
_validate($data, $object)
Validate fields before create or update object.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79