dolibarr 21.0.0-alpha
api_recruitments.class.php
1<?php
2/* Copyright (C) 2022 Thibault FOUCART <support@ptibogxiv.net>
3 * Copyright (C) 2024 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
56 public function __construct()
57 {
58 global $db;
59 $this->db = $db;
60 $this->jobposition = new RecruitmentJobPosition($this->db);
61 $this->candidature = new RecruitmentCandidature($this->db);
62 }
63
64
78 public function getJobPosition($id)
79 {
80 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
81 throw new RestException(403);
82 }
83
84 $result = $this->jobposition->fetch($id);
85 if (!$result) {
86 throw new RestException(404, 'JobPosition not found');
87 }
88
89 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
90 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
91 }
92
93 return $this->_cleanObjectDatas($this->jobposition);
94 }
95
109 public function getCandidature($id)
110 {
111 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
112 throw new RestException(403);
113 }
114
115 $result = $this->candidature->fetch($id);
116 if (!$result) {
117 throw new RestException(404, 'Candidature not found');
118 }
119
120 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
121 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
122 }
123
124 return $this->_cleanObjectDatas($this->candidature);
125 }
126
147 public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
148 {
149 $obj_ret = array();
150 $tmpobject = new RecruitmentJobPosition($this->db);
151
152 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
153 throw new RestException(403);
154 }
155
156 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
157
158 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
159
160 // If the internal user must only see his customers, force searching by him
161 $search_sale = 0;
162 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
163 $search_sale = DolibarrApiAccess::$user->id;
164 }
165
166 $sql = "SELECT t.rowid";
167 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
168 $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
169 $sql .= " WHERE 1 = 1";
170 if ($tmpobject->ismultientitymanaged) {
171 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
172 }
173 if ($restrictonsocid && $socid) {
174 $sql .= " AND t.fk_soc = ".((int) $socid);
175 }
176 // Search on sale representative
177 if ($search_sale && $search_sale != '-1') {
178 if ($search_sale == -2) {
179 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
180 } elseif ($search_sale > 0) {
181 $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).")";
182 }
183 }
184 if ($sqlfilters) {
185 $errormessage = '';
186 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
187 if ($errormessage) {
188 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
189 }
190 }
191
192 //this query will return total orders with the filters given
193 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
194
195 $sql .= $this->db->order($sortfield, $sortorder);
196 if ($limit) {
197 if ($page < 0) {
198 $page = 0;
199 }
200 $offset = $limit * $page;
201
202 $sql .= $this->db->plimit($limit + 1, $offset);
203 }
204
205 $result = $this->db->query($sql);
206 $i = 0;
207 if ($result) {
208 $num = $this->db->num_rows($result);
209 while ($i < $num) {
210 $obj = $this->db->fetch_object($result);
211 $tmp_object = new RecruitmentJobPosition($this->db);
212 if ($tmp_object->fetch($obj->rowid)) {
213 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
214 }
215 $i++;
216 }
217 } else {
218 throw new RestException(503, 'Error when retrieving jobposition list: '.$this->db->lasterror());
219 }
220
221 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
222 if ($pagination_data) {
223 $totalsResult = $this->db->query($sqlTotals);
224 $total = $this->db->fetch_object($totalsResult)->total;
225
226 $tmp = $obj_ret;
227 $obj_ret = [];
228
229 $obj_ret['data'] = $tmp;
230 $obj_ret['pagination'] = [
231 'total' => (int) $total,
232 'page' => $page, //count starts from 0
233 'page_count' => ceil((int) $total / $limit),
234 'limit' => $limit
235 ];
236 }
237
238 return $obj_ret;
239 }
240
261 public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
262 {
263 global $db, $conf;
264
265 $obj_ret = array();
266 $tmpobject = new RecruitmentCandidature($this->db);
267
268 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
269 throw new RestException(403);
270 }
271
272 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
273
274 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
275
276 // If the internal user must only see his customers, force searching by him
277 $search_sale = 0;
278 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
279 $search_sale = DolibarrApiAccess::$user->id;
280 }
281
282 $sql = "SELECT t.rowid";
283 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
284 $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
285 $sql .= " WHERE 1 = 1";
286 if ($tmpobject->ismultientitymanaged) {
287 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
288 }
289 if ($restrictonsocid && $socid) {
290 $sql .= " AND t.fk_soc = ".((int) $socid);
291 }
292 // Search on sale representative
293 if ($search_sale && $search_sale != '-1') {
294 if ($search_sale == -2) {
295 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
296 } elseif ($search_sale > 0) {
297 $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).")";
298 }
299 }
300 if ($sqlfilters) {
301 $errormessage = '';
302 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
303 if ($errormessage) {
304 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
305 }
306 }
307
308 //this query will return total orders with the filters given
309 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
310
311 $sql .= $this->db->order($sortfield, $sortorder);
312 if ($limit) {
313 if ($page < 0) {
314 $page = 0;
315 }
316 $offset = $limit * $page;
317
318 $sql .= $this->db->plimit($limit + 1, $offset);
319 }
320
321 $result = $this->db->query($sql);
322 $i = 0;
323 if ($result) {
324 $num = $this->db->num_rows($result);
325 while ($i < $num) {
326 $obj = $this->db->fetch_object($result);
327 $tmp_object = new RecruitmentCandidature($this->db);
328 if ($tmp_object->fetch($obj->rowid)) {
329 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
330 }
331 $i++;
332 }
333 } else {
334 throw new RestException(503, 'Error when retrieving candidature list: '.$this->db->lasterror());
335 }
336
337 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
338 if ($pagination_data) {
339 $totalsResult = $this->db->query($sqlTotals);
340 $total = $this->db->fetch_object($totalsResult)->total;
341
342 $tmp = $obj_ret;
343 $obj_ret = [];
344
345 $obj_ret['data'] = $tmp;
346 $obj_ret['pagination'] = [
347 'total' => (int) $total,
348 'page' => $page, //count starts from 0
349 'page_count' => ceil((int) $total / $limit),
350 'limit' => $limit
351 ];
352 }
353
354 return $obj_ret;
355 }
356
369 public function postJobPosition($request_data = null)
370 {
371 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
372 throw new RestException(403);
373 }
374
375 // Check mandatory fields
376 $result = $this->_validate($request_data);
377
378 foreach ($request_data as $field => $value) {
379 if ($field === 'caller') {
380 // 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
381 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
382 continue;
383 }
384
385 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
386 }
387
388 // Clean data
389 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
390
391 if ($this->jobposition->create(DolibarrApiAccess::$user) < 0) {
392 throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
393 }
394 return $this->jobposition->id;
395 }
396
409 public function postCandidature($request_data = null)
410 {
411 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
412 throw new RestException(403);
413 }
414
415 // Check mandatory fields
416 $result = $this->_validate($request_data);
417
418 foreach ($request_data as $field => $value) {
419 if ($field === 'caller') {
420 // 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
421 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
422 continue;
423 }
424
425 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
426 }
427
428 // Clean data
429 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
430
431 if ($this->candidature->create(DolibarrApiAccess::$user) < 0) {
432 throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
433 }
434 return $this->candidature->id;
435 }
436
450 public function putJobPosition($id, $request_data = null)
451 {
452 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
453 throw new RestException(403);
454 }
455
456 $result = $this->jobposition->fetch($id);
457 if (!$result) {
458 throw new RestException(404, 'jobposition not found');
459 }
460
461 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
462 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
463 }
464
465 foreach ($request_data as $field => $value) {
466 if ($field == 'id') {
467 continue;
468 }
469 if ($field === 'caller') {
470 // 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
471 $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
472 continue;
473 }
474
475 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
476 }
477
478 // Clean data
479 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
480
481 if ($this->jobposition->update(DolibarrApiAccess::$user, false) > 0) {
482 return $this->getJobPosition($id);
483 } else {
484 throw new RestException(500, $this->jobposition->error);
485 }
486 }
487
501 public function putCandidature($id, $request_data = null)
502 {
503 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
504 throw new RestException(403);
505 }
506
507 $result = $this->candidature->fetch($id);
508 if (!$result) {
509 throw new RestException(404, 'candidature not found');
510 }
511
512 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
513 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
514 }
515
516 foreach ($request_data as $field => $value) {
517 if ($field == 'id') {
518 continue;
519 }
520 if ($field === 'caller') {
521 // 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
522 $this->candidature->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
523 continue;
524 }
525
526 $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
527 }
528
529 // Clean data
530 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
531
532 if ($this->candidature->update(DolibarrApiAccess::$user, false) > 0) {
533 return $this->getCandidature($id);
534 } else {
535 throw new RestException(500, $this->candidature->error);
536 }
537 }
538
539
552 public function deleteJobPosition($id)
553 {
554 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
555 throw new RestException(403);
556 }
557 $result = $this->jobposition->fetch($id);
558 if (!$result) {
559 throw new RestException(404, 'jobposition not found');
560 }
561
562 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
563 throw new RestException(403, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
564 }
565
566 if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
567 throw new RestException(500, 'Error when deleting jobposition : '.$this->jobposition->error);
568 }
569
570 return array(
571 'success' => array(
572 'code' => 200,
573 'message' => 'jobposition deleted'
574 )
575 );
576 }
577
590 public function deleteCandidature($id)
591 {
592 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
593 throw new RestException(403);
594 }
595 $result = $this->candidature->fetch($id);
596 if (!$result) {
597 throw new RestException(404, 'candidature not found');
598 }
599
600 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
601 throw new RestException(403, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
602 }
603
604 if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
605 throw new RestException(500, 'Error when deleting candidature : '.$this->candidature->error);
606 }
607
608 return array(
609 'success' => array(
610 'code' => 200,
611 'message' => 'candidature deleted'
612 )
613 );
614 }
615
616
617 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
624 protected function _cleanObjectDatas($object)
625 {
626 // phpcs:enable
627 $object = parent::_cleanObjectDatas($object);
628
629 unset($object->rowid);
630 unset($object->canvas);
631
632 /*unset($object->name);
633 unset($object->lastname);
634 unset($object->firstname);
635 unset($object->civility_id);
636 unset($object->statut);
637 unset($object->state);
638 unset($object->state_id);
639 unset($object->state_code);
640 unset($object->region);
641 unset($object->region_code);
642 unset($object->country);
643 unset($object->country_id);
644 unset($object->country_code);
645 unset($object->barcode_type);
646 unset($object->barcode_type_code);
647 unset($object->barcode_type_label);
648 unset($object->barcode_type_coder);
649 unset($object->total_ht);
650 unset($object->total_tva);
651 unset($object->total_localtax1);
652 unset($object->total_localtax2);
653 unset($object->total_ttc);
654 unset($object->fk_account);
655 unset($object->comments);
656 unset($object->note);
657 unset($object->mode_reglement_id);
658 unset($object->cond_reglement_id);
659 unset($object->cond_reglement);
660 unset($object->shipping_method_id);
661 unset($object->fk_incoterms);
662 unset($object->label_incoterms);
663 unset($object->location_incoterms);
664 */
665
666 // If object has lines, remove $db property
667 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
668 $nboflines = count($object->lines);
669 for ($i = 0; $i < $nboflines; $i++) {
670 $this->_cleanObjectDatas($object->lines[$i]);
671
672 unset($object->lines[$i]->lines);
673 unset($object->lines[$i]->note);
674 }
675 }
676
677 return $object;
678 }
679
688 private function _validate($data)
689 {
690 $jobposition = array();
691 foreach ($this->jobposition->fields as $field => $propfield) {
692 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
693 continue; // Not a mandatory field
694 }
695 if (!isset($data[$field])) {
696 throw new RestException(400, "$field field missing");
697 }
698 $jobposition[$field] = $data[$field];
699 }
700 return $jobposition;
701 }
702}
$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.