dolibarr 18.0.6
api_recruitment.class.php
Go to the documentation of this file.
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
143 public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
144 {
145 global $db, $conf;
146
147 $obj_ret = array();
148 $tmpobject = new RecruitmentJobPosition($this->db);
149
150 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
151 throw new RestException(401);
152 }
153
154 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
155
156 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
157
158 // If the internal user must only see his customers, force searching by him
159 $search_sale = 0;
160 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
161 $search_sale = DolibarrApiAccess::$user->id;
162 }
163
164 $sql = "SELECT t.rowid";
165 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
166 $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)
167 }
168 $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
169
170 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
171 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
172 }
173 $sql .= " WHERE 1 = 1";
174
175 // Example of use $mode
176 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
177 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
178
179 if ($tmpobject->ismultientitymanaged) {
180 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
181 }
182 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
183 $sql .= " AND t.fk_soc = sc.fk_soc";
184 }
185 if ($restrictonsocid && $socid) {
186 $sql .= " AND t.fk_soc = ".((int) $socid);
187 }
188 if ($restrictonsocid && $search_sale > 0) {
189 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
190 }
191 // Insert sale filter
192 if ($restrictonsocid && $search_sale > 0) {
193 $sql .= " AND sc.fk_user = ".((int) $search_sale);
194 }
195 if ($sqlfilters) {
196 $errormessage = '';
197 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
198 if ($errormessage) {
199 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
200 }
201 }
202
203 $sql .= $this->db->order($sortfield, $sortorder);
204 if ($limit) {
205 if ($page < 0) {
206 $page = 0;
207 }
208 $offset = $limit * $page;
209
210 $sql .= $this->db->plimit($limit + 1, $offset);
211 }
212
213 $result = $this->db->query($sql);
214 $i = 0;
215 if ($result) {
216 $num = $this->db->num_rows($result);
217 while ($i < $num) {
218 $obj = $this->db->fetch_object($result);
219 $tmp_object = new RecruitmentJobPosition($this->db);
220 if ($tmp_object->fetch($obj->rowid)) {
221 $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
222 }
223 $i++;
224 }
225 } else {
226 throw new RestException(503, 'Error when retrieving jobposition list: '.$this->db->lasterror());
227 }
228 if (!count($obj_ret)) {
229 throw new RestException(404, 'No jobposition found');
230 }
231 return $obj_ret;
232 }
233
250 public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
251 {
252 global $db, $conf;
253
254 $obj_ret = array();
255 $tmpobject = new RecruitmentCandidature($this->db);
256
257 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
258 throw new RestException(401);
259 }
260
261 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
262
263 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
264
265 // If the internal user must only see his customers, force searching by him
266 $search_sale = 0;
267 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
268 $search_sale = DolibarrApiAccess::$user->id;
269 }
270
271 $sql = "SELECT t.rowid";
272 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
273 $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)
274 }
275 $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
276
277 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
278 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
279 }
280 $sql .= " WHERE 1 = 1";
281
282 // Example of use $mode
283 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
284 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
285
286 if ($tmpobject->ismultientitymanaged) {
287 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
288 }
289 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
290 $sql .= " AND t.fk_soc = sc.fk_soc";
291 }
292 if ($restrictonsocid && $socid) {
293 $sql .= " AND t.fk_soc = ".((int) $socid);
294 }
295 if ($restrictonsocid && $search_sale > 0) {
296 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
297 }
298 // Insert sale filter
299 if ($restrictonsocid && $search_sale > 0) {
300 $sql .= " AND sc.fk_user = ".((int) $search_sale);
301 }
302 if ($sqlfilters) {
303 $errormessage = '';
304 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
305 if ($errormessage) {
306 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
307 }
308 }
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->_cleanObjectDatas($tmp_object);
329 }
330 $i++;
331 }
332 } else {
333 throw new RestException(503, 'Error when retrieving candidature list: '.$this->db->lasterror());
334 }
335 if (!count($obj_ret)) {
336 throw new RestException(404, 'No candidature found');
337 }
338 return $obj_ret;
339 }
340
351 public function postJobPosition($request_data = null)
352 {
353 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
354 throw new RestException(401);
355 }
356
357 // Check mandatory fields
358 $result = $this->_validate($request_data);
359
360 foreach ($request_data as $field => $value) {
361 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
362 }
363
364 // Clean data
365 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
366
367 if ($this->jobposition->create(DolibarrApiAccess::$user)<0) {
368 throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
369 }
370 return $this->jobposition->id;
371 }
372
383 public function postCandidature($request_data = null)
384 {
385 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
386 throw new RestException(401);
387 }
388
389 // Check mandatory fields
390 $result = $this->_validate($request_data);
391
392 foreach ($request_data as $field => $value) {
393 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
394 }
395
396 // Clean data
397 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
398
399 if ($this->candidature->create(DolibarrApiAccess::$user)<0) {
400 throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
401 }
402 return $this->candidature->id;
403 }
404
416 public function putJobPosition($id, $request_data = null)
417 {
418 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
419 throw new RestException(401);
420 }
421
422 $result = $this->jobposition->fetch($id);
423 if (!$result) {
424 throw new RestException(404, 'jobposition not found');
425 }
426
427 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
428 throw new RestException(401, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
429 }
430
431 foreach ($request_data as $field => $value) {
432 if ($field == 'id') {
433 continue;
434 }
435 $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
436 }
437
438 // Clean data
439 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
440
441 if ($this->jobposition->update(DolibarrApiAccess::$user, false) > 0) {
442 return $this->getJobPosition($id);
443 } else {
444 throw new RestException(500, $this->jobposition->error);
445 }
446 }
447
459 public function putCandidature($id, $request_data = null)
460 {
461 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
462 throw new RestException(401);
463 }
464
465 $result = $this->candidature->fetch($id);
466 if (!$result) {
467 throw new RestException(404, 'candidature not found');
468 }
469
470 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
471 throw new RestException(401, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
472 }
473
474 foreach ($request_data as $field => $value) {
475 if ($field == 'id') {
476 continue;
477 }
478 $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
479 }
480
481 // Clean data
482 // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
483
484 if ($this->candidature->update(DolibarrApiAccess::$user, false) > 0) {
485 return $this->getCandidature($id);
486 } else {
487 throw new RestException(500, $this->candidature->error);
488 }
489 }
490
491
502 public function deleteJobPosition($id)
503 {
504 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
505 throw new RestException(401);
506 }
507 $result = $this->jobposition->fetch($id);
508 if (!$result) {
509 throw new RestException(404, 'jobposition not found');
510 }
511
512 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
513 throw new RestException(401, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
514 }
515
516 if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
517 throw new RestException(500, 'Error when deleting jobposition : '.$this->jobposition->error);
518 }
519
520 return array(
521 'success' => array(
522 'code' => 200,
523 'message' => 'jobposition deleted'
524 )
525 );
526 }
527
538 public function deleteCandidature($id)
539 {
540 if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
541 throw new RestException(401);
542 }
543 $result = $this->candidature->fetch($id);
544 if (!$result) {
545 throw new RestException(404, 'candidature not found');
546 }
547
548 if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
549 throw new RestException(401, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
550 }
551
552 if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
553 throw new RestException(500, 'Error when deleting candidature : '.$this->candidature->error);
554 }
555
556 return array(
557 'success' => array(
558 'code' => 200,
559 'message' => 'candidature deleted'
560 )
561 );
562 }
563
564
565 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
572 protected function _cleanObjectDatas($object)
573 {
574 // phpcs:enable
575 $object = parent::_cleanObjectDatas($object);
576
577 unset($object->rowid);
578 unset($object->canvas);
579
580 /*unset($object->name);
581 unset($object->lastname);
582 unset($object->firstname);
583 unset($object->civility_id);
584 unset($object->statut);
585 unset($object->state);
586 unset($object->state_id);
587 unset($object->state_code);
588 unset($object->region);
589 unset($object->region_code);
590 unset($object->country);
591 unset($object->country_id);
592 unset($object->country_code);
593 unset($object->barcode_type);
594 unset($object->barcode_type_code);
595 unset($object->barcode_type_label);
596 unset($object->barcode_type_coder);
597 unset($object->total_ht);
598 unset($object->total_tva);
599 unset($object->total_localtax1);
600 unset($object->total_localtax2);
601 unset($object->total_ttc);
602 unset($object->fk_account);
603 unset($object->comments);
604 unset($object->note);
605 unset($object->mode_reglement_id);
606 unset($object->cond_reglement_id);
607 unset($object->cond_reglement);
608 unset($object->shipping_method_id);
609 unset($object->fk_incoterms);
610 unset($object->label_incoterms);
611 unset($object->location_incoterms);
612 */
613
614 // If object has lines, remove $db property
615 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
616 $nboflines = count($object->lines);
617 for ($i = 0; $i < $nboflines; $i++) {
618 $this->_cleanObjectDatas($object->lines[$i]);
619
620 unset($object->lines[$i]->lines);
621 unset($object->lines[$i]->note);
622 }
623 }
624
625 return $object;
626 }
627
636 private function _validate($data)
637 {
638 $jobposition = array();
639 foreach ($this->jobposition->fields as $field => $propfield) {
640 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
641 continue; // Not a mandatory field
642 }
643 if (!isset($data[$field])) {
644 throw new RestException(400, "$field field missing");
645 }
646 $jobposition[$field] = $data[$field];
647 }
648 return $jobposition;
649 }
650}
Class for API REST v1.
Definition api.class.php:31
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:86
Class for RecruitmentCandidature.
indexJobPosition($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List jobpositions.
_validate($data)
Validate fields before create or update object.
getJobPosition($id)
Get properties of a jobposition object.
deleteCandidature($id)
Delete candidature.
__construct()
Constructor.
indexCandidature($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List candatures.
putCandidature($id, $request_data=null)
Update candidature.
deleteJobPosition($id)
Delete jobposition.
getCandidature($id)
Get properties of a candidature object.
postJobPosition($request_data=null)
Create jobposition object.
postCandidature($request_data=null)
Create candidature object.
putJobPosition($id, $request_data=null)
Update jobposition.
_cleanObjectDatas($object)
Clean sensible object datas.
Class for RecruitmentJobPosition.
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.