dolibarr  16.0.5
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 
18 use Luracast\Restler\RestException;
19 
20 dol_include_once('/recruitment/class/recruitmentjobposition.class.php');
21 dol_include_once('/recruitment/class/recruitmentcandidature.class.php');
22 
23 
24 
37 class Recruitment extends DolibarrApi
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->rights->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->rights->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->rights->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";
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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
198  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
199  }
200  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
201  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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->_cleanObjectDatas($tmp_object);
223  }
224  $i++;
225  }
226  } else {
227  throw new RestException(503, 'Error when retrieving jobposition list: '.$this->db->lasterror());
228  }
229  if (!count($obj_ret)) {
230  throw new RestException(404, 'No jobposition found');
231  }
232  return $obj_ret;
233  }
234 
251  public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
252  {
253  global $db, $conf;
254 
255  $obj_ret = array();
256  $tmpobject = new RecruitmentCandidature($this->db);
257 
258  if (!DolibarrApiAccess::$user->rights->recruitment->recruitmentjobposition->read) {
259  throw new RestException(401);
260  }
261 
262  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
263 
264  $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
265 
266  // If the internal user must only see his customers, force searching by him
267  $search_sale = 0;
268  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
269  $search_sale = DolibarrApiAccess::$user->id;
270  }
271 
272  $sql = "SELECT t.rowid";
273  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
274  $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)
275  }
276  $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
277 
278  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
279  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
280  }
281  $sql .= " WHERE 1 = 1";
282 
283  // Example of use $mode
284  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
285  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
286 
287  if ($tmpobject->ismultientitymanaged) {
288  $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
289  }
290  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
291  $sql .= " AND t.fk_soc = sc.fk_soc";
292  }
293  if ($restrictonsocid && $socid) {
294  $sql .= " AND t.fk_soc = ".((int) $socid);
295  }
296  if ($restrictonsocid && $search_sale > 0) {
297  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
298  }
299  // Insert sale filter
300  if ($restrictonsocid && $search_sale > 0) {
301  $sql .= " AND sc.fk_user = ".((int) $search_sale);
302  }
303  if ($sqlfilters) {
304  $errormessage = '';
305  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
306  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
307  }
308  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
309  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
310  }
311 
312  $sql .= $this->db->order($sortfield, $sortorder);
313  if ($limit) {
314  if ($page < 0) {
315  $page = 0;
316  }
317  $offset = $limit * $page;
318 
319  $sql .= $this->db->plimit($limit + 1, $offset);
320  }
321 
322  $result = $this->db->query($sql);
323  $i = 0;
324  if ($result) {
325  $num = $this->db->num_rows($result);
326  while ($i < $num) {
327  $obj = $this->db->fetch_object($result);
328  $tmp_object = new RecruitmentCandidature($this->db);
329  if ($tmp_object->fetch($obj->rowid)) {
330  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
331  }
332  $i++;
333  }
334  } else {
335  throw new RestException(503, 'Error when retrieving candidature list: '.$this->db->lasterror());
336  }
337  if (!count($obj_ret)) {
338  throw new RestException(404, 'No candidature found');
339  }
340  return $obj_ret;
341  }
342 
353  public function postJobPosition($request_data = null)
354  {
355  if (!DolibarrApiAccess::$user->rights->recruitment->recruitmentjobposition->write) {
356  throw new RestException(401);
357  }
358 
359  // Check mandatory fields
360  $result = $this->_validate($request_data);
361 
362  foreach ($request_data as $field => $value) {
363  $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
364  }
365 
366  // Clean data
367  // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
368 
369  if ($this->jobposition->create(DolibarrApiAccess::$user)<0) {
370  throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
371  }
372  return $this->jobposition->id;
373  }
374 
385  public function postCandidature($request_data = null)
386  {
387  if (!DolibarrApiAccess::$user->rights->recruitment->recruitmentjobposition->write) {
388  throw new RestException(401);
389  }
390 
391  // Check mandatory fields
392  $result = $this->_validate($request_data);
393 
394  foreach ($request_data as $field => $value) {
395  $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
396  }
397 
398  // Clean data
399  // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
400 
401  if ($this->candidature->create(DolibarrApiAccess::$user)<0) {
402  throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
403  }
404  return $this->candidature->id;
405  }
406 
418  public function putJobPosition($id, $request_data = null)
419  {
420  if (!DolibarrApiAccess::$user->rights->recruitment->recruitmentjobposition->write) {
421  throw new RestException(401);
422  }
423 
424  $result = $this->jobposition->fetch($id);
425  if (!$result) {
426  throw new RestException(404, 'jobposition not found');
427  }
428 
429  if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
430  throw new RestException(401, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
431  }
432 
433  foreach ($request_data as $field => $value) {
434  if ($field == 'id') {
435  continue;
436  }
437  $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
438  }
439 
440  // Clean data
441  // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
442 
443  if ($this->jobposition->update(DolibarrApiAccess::$user, false) > 0) {
444  return $this->get($id);
445  } else {
446  throw new RestException(500, $this->jobposition->error);
447  }
448  }
449 
461  public function putCandidature($id, $request_data = null)
462  {
463  if (!DolibarrApiAccess::$user->rights->recruitment->recruitmentjobposition->write) {
464  throw new RestException(401);
465  }
466 
467  $result = $this->candidature->fetch($id);
468  if (!$result) {
469  throw new RestException(404, 'candidature not found');
470  }
471 
472  if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
473  throw new RestException(401, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
474  }
475 
476  foreach ($request_data as $field => $value) {
477  if ($field == 'id') {
478  continue;
479  }
480  $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
481  }
482 
483  // Clean data
484  // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
485 
486  if ($this->candidature->update(DolibarrApiAccess::$user, false) > 0) {
487  return $this->get($id);
488  } else {
489  throw new RestException(500, $this->candidature->error);
490  }
491  }
492 
493 
504  public function deleteJobPosition($id)
505  {
506  if (!DolibarrApiAccess::$user->rights->recruitment->recruitmentjobposition->delete) {
507  throw new RestException(401);
508  }
509  $result = $this->jobposition->fetch($id);
510  if (!$result) {
511  throw new RestException(404, 'jobposition not found');
512  }
513 
514  if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
515  throw new RestException(401, 'Access to instance id='.$this->jobposition->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
516  }
517 
518  if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
519  throw new RestException(500, 'Error when deleting jobposition : '.$this->jobposition->error);
520  }
521 
522  return array(
523  'success' => array(
524  'code' => 200,
525  'message' => 'jobposition deleted'
526  )
527  );
528  }
529 
540  public function deleteCandidature($id)
541  {
542  if (!DolibarrApiAccess::$user->rights->recruitment->recruitmentjobposition->delete) {
543  throw new RestException(401);
544  }
545  $result = $this->candidature->fetch($id);
546  if (!$result) {
547  throw new RestException(404, 'candidature not found');
548  }
549 
550  if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
551  throw new RestException(401, 'Access to instance id='.$this->candidature->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
552  }
553 
554  if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
555  throw new RestException(500, 'Error when deleting candidature : '.$this->candidature->error);
556  }
557 
558  return array(
559  'success' => array(
560  'code' => 200,
561  'message' => 'candidature deleted'
562  )
563  );
564  }
565 
566 
567  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
574  protected function _cleanObjectDatas($object)
575  {
576  // phpcs:enable
577  $object = parent::_cleanObjectDatas($object);
578 
579  unset($object->rowid);
580  unset($object->canvas);
581 
582  /*unset($object->name);
583  unset($object->lastname);
584  unset($object->firstname);
585  unset($object->civility_id);
586  unset($object->statut);
587  unset($object->state);
588  unset($object->state_id);
589  unset($object->state_code);
590  unset($object->region);
591  unset($object->region_code);
592  unset($object->country);
593  unset($object->country_id);
594  unset($object->country_code);
595  unset($object->barcode_type);
596  unset($object->barcode_type_code);
597  unset($object->barcode_type_label);
598  unset($object->barcode_type_coder);
599  unset($object->total_ht);
600  unset($object->total_tva);
601  unset($object->total_localtax1);
602  unset($object->total_localtax2);
603  unset($object->total_ttc);
604  unset($object->fk_account);
605  unset($object->comments);
606  unset($object->note);
607  unset($object->mode_reglement_id);
608  unset($object->cond_reglement_id);
609  unset($object->cond_reglement);
610  unset($object->shipping_method_id);
611  unset($object->fk_incoterms);
612  unset($object->label_incoterms);
613  unset($object->location_incoterms);
614  */
615 
616  // If object has lines, remove $db property
617  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
618  $nboflines = count($object->lines);
619  for ($i = 0; $i < $nboflines; $i++) {
620  $this->_cleanObjectDatas($object->lines[$i]);
621 
622  unset($object->lines[$i]->lines);
623  unset($object->lines[$i]->note);
624  }
625  }
626 
627  return $object;
628  }
629 
638  private function _validate($data)
639  {
640  $jobposition = array();
641  foreach ($this->jobposition->fields as $field => $propfield) {
642  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
643  continue; // Not a mandatory field
644  }
645  if (!isset($data[$field])) {
646  throw new RestException(400, "$field field missing");
647  }
648  $jobposition[$field] = $data[$field];
649  }
650  return $jobposition;
651  }
652 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Recruitment\postCandidature
postCandidature($request_data=null)
Create candidature object.
Definition: api_recruitment.class.php:385
Recruitment\deleteJobPosition
deleteJobPosition($id)
Delete jobposition.
Definition: api_recruitment.class.php:504
Recruitment
Definition: api_recruitment.class.php:37
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
RecruitmentJobPosition
Class for RecruitmentJobPosition.
Definition: recruitmentjobposition.class.php:32
Recruitment\getJobPosition
getJobPosition($id)
Get properties of a jobposition object.
Definition: api_recruitment.class.php:77
Recruitment\indexCandidature
indexCandidature($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List candatures.
Definition: api_recruitment.class.php:251
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Recruitment\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_recruitment.class.php:638
Recruitment\putJobPosition
putJobPosition($id, $request_data=null)
Update jobposition.
Definition: api_recruitment.class.php:418
Recruitment\deleteCandidature
deleteCandidature($id)
Delete candidature.
Definition: api_recruitment.class.php:540
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Recruitment\postJobPosition
postJobPosition($request_data=null)
Create jobposition object.
Definition: api_recruitment.class.php:353
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Recruitment\putCandidature
putCandidature($id, $request_data=null)
Update candidature.
Definition: api_recruitment.class.php:461
RecruitmentCandidature
Class for RecruitmentCandidature.
Definition: recruitmentcandidature.class.php:32
Recruitment\__construct
__construct()
Constructor.
Definition: api_recruitment.class.php:55
Recruitment\indexJobPosition
indexJobPosition($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List jobpositions.
Definition: api_recruitment.class.php:143
DolibarrApi\_checkValForAPI
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition: api.class.php:86
Recruitment\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_recruitment.class.php:574
Recruitment\getCandidature
getCandidature($id)
Get properties of a candidature object.
Definition: api_recruitment.class.php:108