dolibarr 19.0.3
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(401);
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(401, '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(401);
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(401, '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 global $db, $conf;
147
148 $obj_ret = array();
149 $tmpobject = new RecruitmentJobPosition($this->db);
150
151 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
152 throw new RestException(401);
153 }
154
155 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
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->rights->societe->client->voir && !$socid) {
162 $search_sale = DolibarrApiAccess::$user->id;
163 }
164
165 $sql = "SELECT t.rowid";
166 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
167 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
168 }
169 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t 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
170
171 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
172 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
173 }
174 $sql .= " WHERE 1 = 1";
175
176 // Example of use $mode
177 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
178 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
179
180 if ($tmpobject->ismultientitymanaged) {
181 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
182 }
183 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
184 $sql .= " AND t.fk_soc = sc.fk_soc";
185 }
186 if ($restrictonsocid && $socid) {
187 $sql .= " AND t.fk_soc = ".((int) $socid);
188 }
189 if ($restrictonsocid && $search_sale > 0) {
190 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
191 }
192 // Insert sale filter
193 if ($restrictonsocid && $search_sale > 0) {
194 $sql .= " AND sc.fk_user = ".((int) $search_sale);
195 }
196 if ($sqlfilters) {
197 $errormessage = '';
198 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
199 if ($errormessage) {
200 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
201 }
202 }
203
204 $sql .= $this->db->order($sortfield, $sortorder);
205 if ($limit) {
206 if ($page < 0) {
207 $page = 0;
208 }
209 $offset = $limit * $page;
210
211 $sql .= $this->db->plimit($limit + 1, $offset);
212 }
213
214 $result = $this->db->query($sql);
215 $i = 0;
216 if ($result) {
217 $num = $this->db->num_rows($result);
218 while ($i < $num) {
219 $obj = $this->db->fetch_object($result);
220 $tmp_object = new RecruitmentJobPosition($this->db);
221 if ($tmp_object->fetch($obj->rowid)) {
222 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
223 }
224 $i++;
225 }
226 } else {
227 throw new RestException(503, 'Error when retrieving jobposition list: '.$this->db->lasterror());
228 }
229
230 return $obj_ret;
231 }
232
249 public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
250 {
251 global $db, $conf;
252
253 $obj_ret = array();
254 $tmpobject = new RecruitmentCandidature($this->db);
255
256 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
257 throw new RestException(401);
258 }
259
260 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
261
262 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
263
264 // If the internal user must only see his customers, force searching by him
265 $search_sale = 0;
266 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
267 $search_sale = DolibarrApiAccess::$user->id;
268 }
269
270 $sql = "SELECT t.rowid";
271 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
272 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
273 }
274 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t 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
275
276 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
277 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
278 }
279 $sql .= " WHERE 1 = 1";
280
281 // Example of use $mode
282 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
283 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
284
285 if ($tmpobject->ismultientitymanaged) {
286 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
287 }
288 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
289 $sql .= " AND t.fk_soc = sc.fk_soc";
290 }
291 if ($restrictonsocid && $socid) {
292 $sql .= " AND t.fk_soc = ".((int) $socid);
293 }
294 if ($restrictonsocid && $search_sale > 0) {
295 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
296 }
297 // Insert sale filter
298 if ($restrictonsocid && $search_sale > 0) {
299 $sql .= " AND sc.fk_user = ".((int) $search_sale);
300 }
301 if ($sqlfilters) {
302 $errormessage = '';
303 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
304 if ($errormessage) {
305 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
306 }
307 }
308
309 $sql .= $this->db->order($sortfield, $sortorder);
310 if ($limit) {
311 if ($page < 0) {
312 $page = 0;
313 }
314 $offset = $limit * $page;
315
316 $sql .= $this->db->plimit($limit + 1, $offset);
317 }
318
319 $result = $this->db->query($sql);
320 $i = 0;
321 if ($result) {
322 $num = $this->db->num_rows($result);
323 while ($i < $num) {
324 $obj = $this->db->fetch_object($result);
325 $tmp_object = new RecruitmentCandidature($this->db);
326 if ($tmp_object->fetch($obj->rowid)) {
327 $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
328 }
329 $i++;
330 }
331 } else {
332 throw new RestException(503, 'Error when retrieving candidature list: '.$this->db->lasterror());
333 }
334
335 return $obj_ret;
336 }
337
348 public function postJobPosition($request_data = null)
349 {
350 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
351 throw new RestException(401);
352 }
353
354 // Check mandatory fields
355 $result = $this->_validate($request_data);
356
357 foreach ($request_data as $field => $value) {
358 if ($field === 'caller') {
359 // 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 whith the caller
360 $this->jobposition->context['caller'] = $request_data['caller'];
361 continue;
362 }
363
364 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
365 }
366
367 // Clean data
368 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
369
370 if ($this->jobposition->create(DolibarrApiAccess::$user)<0) {
371 throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
372 }
373 return $this->jobposition->id;
374 }
375
386 public function postCandidature($request_data = null)
387 {
388 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
389 throw new RestException(401);
390 }
391
392 // Check mandatory fields
393 $result = $this->_validate($request_data);
394
395 foreach ($request_data as $field => $value) {
396 if ($field === 'caller') {
397 // 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 whith the caller
398 $this->jobposition->context['caller'] = $request_data['caller'];
399 continue;
400 }
401
402 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
403 }
404
405 // Clean data
406 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
407
408 if ($this->candidature->create(DolibarrApiAccess::$user)<0) {
409 throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
410 }
411 return $this->candidature->id;
412 }
413
425 public function putJobPosition($id, $request_data = null)
426 {
427 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
428 throw new RestException(401);
429 }
430
431 $result = $this->jobposition->fetch($id);
432 if (!$result) {
433 throw new RestException(404, 'jobposition not found');
434 }
435
436 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
437 throw new RestException(401, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
438 }
439
440 foreach ($request_data as $field => $value) {
441 if ($field == 'id') {
442 continue;
443 }
444 if ($field === 'caller') {
445 // 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 whith the caller
446 $this->jobposition->context['caller'] = $request_data['caller'];
447 continue;
448 }
449
450 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
451 }
452
453 // Clean data
454 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
455
456 if ($this->jobposition->update(DolibarrApiAccess::$user, false) > 0) {
457 return $this->getJobPosition($id);
458 } else {
459 throw new RestException(500, $this->jobposition->error);
460 }
461 }
462
474 public function putCandidature($id, $request_data = null)
475 {
476 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
477 throw new RestException(401);
478 }
479
480 $result = $this->candidature->fetch($id);
481 if (!$result) {
482 throw new RestException(404, 'candidature not found');
483 }
484
485 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
486 throw new RestException(401, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
487 }
488
489 foreach ($request_data as $field => $value) {
490 if ($field == 'id') {
491 continue;
492 }
493 if ($field === 'caller') {
494 // 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 whith the caller
495 $this->candidature->context['caller'] = $request_data['caller'];
496 continue;
497 }
498
499 $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
500 }
501
502 // Clean data
503 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
504
505 if ($this->candidature->update(DolibarrApiAccess::$user, false) > 0) {
506 return $this->getCandidature($id);
507 } else {
508 throw new RestException(500, $this->candidature->error);
509 }
510 }
511
512
523 public function deleteJobPosition($id)
524 {
525 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
526 throw new RestException(401);
527 }
528 $result = $this->jobposition->fetch($id);
529 if (!$result) {
530 throw new RestException(404, 'jobposition not found');
531 }
532
533 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
534 throw new RestException(401, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
535 }
536
537 if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
538 throw new RestException(500, 'Error when deleting jobposition : '.$this->jobposition->error);
539 }
540
541 return array(
542 'success' => array(
543 'code' => 200,
544 'message' => 'jobposition deleted'
545 )
546 );
547 }
548
559 public function deleteCandidature($id)
560 {
561 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
562 throw new RestException(401);
563 }
564 $result = $this->candidature->fetch($id);
565 if (!$result) {
566 throw new RestException(404, 'candidature not found');
567 }
568
569 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
570 throw new RestException(401, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
571 }
572
573 if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
574 throw new RestException(500, 'Error when deleting candidature : '.$this->candidature->error);
575 }
576
577 return array(
578 'success' => array(
579 'code' => 200,
580 'message' => 'candidature deleted'
581 )
582 );
583 }
584
585
586 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
593 protected function _cleanObjectDatas($object)
594 {
595 // phpcs:enable
596 $object = parent::_cleanObjectDatas($object);
597
598 unset($object->rowid);
599 unset($object->canvas);
600
601 /*unset($object->name);
602 unset($object->lastname);
603 unset($object->firstname);
604 unset($object->civility_id);
605 unset($object->statut);
606 unset($object->state);
607 unset($object->state_id);
608 unset($object->state_code);
609 unset($object->region);
610 unset($object->region_code);
611 unset($object->country);
612 unset($object->country_id);
613 unset($object->country_code);
614 unset($object->barcode_type);
615 unset($object->barcode_type_code);
616 unset($object->barcode_type_label);
617 unset($object->barcode_type_coder);
618 unset($object->total_ht);
619 unset($object->total_tva);
620 unset($object->total_localtax1);
621 unset($object->total_localtax2);
622 unset($object->total_ttc);
623 unset($object->fk_account);
624 unset($object->comments);
625 unset($object->note);
626 unset($object->mode_reglement_id);
627 unset($object->cond_reglement_id);
628 unset($object->cond_reglement);
629 unset($object->shipping_method_id);
630 unset($object->fk_incoterms);
631 unset($object->label_incoterms);
632 unset($object->location_incoterms);
633 */
634
635 // If object has lines, remove $db property
636 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
637 $nboflines = count($object->lines);
638 for ($i = 0; $i < $nboflines; $i++) {
639 $this->_cleanObjectDatas($object->lines[$i]);
640
641 unset($object->lines[$i]->lines);
642 unset($object->lines[$i]->note);
643 }
644 }
645
646 return $object;
647 }
648
657 private function _validate($data)
658 {
659 $jobposition = array();
660 foreach ($this->jobposition->fields as $field => $propfield) {
661 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
662 continue; // Not a mandatory field
663 }
664 if (!isset($data[$field])) {
665 throw new RestException(400, "$field field missing");
666 }
667 $jobposition[$field] = $data[$field];
668 }
669 return $jobposition;
670 }
671}
Class for API REST v1.
Definition api.class.php:31
_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:85
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.