Passed
Push — EXTRACT_CLASSES ( ae6b5c...83d77a )
by Rafael
60:14 queued 23:58
created

Recruitments::indexCandidature()   F

Complexity

Conditions 20
Paths 1601

Size

Total Lines 74
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 47
nc 1601
nop 5
dl 0
loc 74
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2022       Thibault FOUCART            <[email protected]>
4
 * Copyright (C) 2024       Rafael San José             <[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace Dolibarr\Code\Recruitement\Api;
21
22
use Dolibarr\Core\Base\DolibarrApi;
23
use Luracast\Restler\RestException;
24
25
dol_include_once('/recruitment/class/recruitmentjobposition.class.php');
26
dol_include_once('/recruitment/class/recruitmentcandidature.class.php');
27
28
/**
29
 * \file    recruitment/class/api_recruitment.class.php
30
 * \ingroup recruitment
31
 * \brief   File for API management of recruitment.
32
 */
33
34
/**
35
 * API class for recruitment
36
 *
37
 * @access protected
38
 * @class  DolibarrApiAccess {@requires user,external}
39
 */
40
class Recruitments extends DolibarrApi
41
{
42
    /**
43
     * @var RecruitmentJobPosition $jobposition {@type RecruitmentJobPosition}
44
     */
45
    public $jobposition;
46
    /**
47
     * @var RecruitmentCandidature $candidature {@type RecruitmentCandidature}
48
     */
49
    public $candidature;
50
51
52
    /**
53
     * Constructor
54
     *
55
     * @url     GET /
56
     *
57
     */
58
    public function __construct()
59
    {
60
        global $db;
61
        $this->db = $db;
62
        $this->jobposition = new RecruitmentJobPosition($this->db);
63
        $this->candidature = new RecruitmentCandidature($this->db);
64
    }
65
66
67
    /**
68
     * Get properties of a jobposition object
69
     *
70
     * Return an array with jobposition information
71
     *
72
     * @param   int         $id     ID of jobposition
73
     * @return  Object              Object with cleaned properties
74
     *
75
     * @url GET jobposition/{id}
76
     *
77
     * @throws RestException 401 Not allowed
78
     * @throws RestException 404 Not found
79
     */
80
    public function getJobPosition($id)
81
    {
82
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
83
            throw new RestException(403);
84
        }
85
86
        $result = $this->jobposition->fetch($id);
87
        if (!$result) {
88
            throw new RestException(404, 'JobPosition not found');
89
        }
90
91
        if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
92
            throw new RestException(403, 'Access to instance id=' . $this->jobposition->id . ' of object not allowed for login ' . DolibarrApiAccess::$user->login);
93
        }
94
95
        return $this->_cleanObjectDatas($this->jobposition);
96
    }
97
98
    /**
99
     * Get properties of a candidature object
100
     *
101
     * Return an array with candidature information
102
     *
103
     * @param   int     $id     ID of candidature
104
     * @return  Object          Object with cleaned properties
105
     *
106
     * @url GET candidature/{id}
107
     *
108
     * @throws RestException 401 Not allowed
109
     * @throws RestException 404 Not found
110
     */
111
    public function getCandidature($id)
112
    {
113
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
114
            throw new RestException(403);
115
        }
116
117
        $result = $this->candidature->fetch($id);
118
        if (!$result) {
119
            throw new RestException(404, 'Candidature not found');
120
        }
121
122
        if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
123
            throw new RestException(403, 'Access to instance id=' . $this->candidature->id . ' of object not allowed for login ' . DolibarrApiAccess::$user->login);
124
        }
125
126
        return $this->_cleanObjectDatas($this->candidature);
127
    }
128
129
130
    /**
131
     * List jobpositions
132
     *
133
     * Get a list of jobpositions
134
     *
135
     * @param string           $sortfield           Sort field
136
     * @param string           $sortorder           Sort order
137
     * @param int              $limit               Limit for list
138
     * @param int              $page                Page number
139
     * @param string           $sqlfilters          Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
140
     * @param string    $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names
141
     * @return  array                               Array of order objects
142
     *
143
     * @throws RestException
144
     *
145
     * @url GET /jobposition/
146
     */
147
    public function indexJobPosition($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
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
        $sql .= $this->db->order($sortfield, $sortorder);
193
        if ($limit) {
194
            if ($page < 0) {
195
                $page = 0;
196
            }
197
            $offset = $limit * $page;
198
199
            $sql .= $this->db->plimit($limit + 1, $offset);
200
        }
201
202
        $result = $this->db->query($sql);
203
        $i = 0;
204
        if ($result) {
205
            $num = $this->db->num_rows($result);
206
            while ($i < $num) {
207
                $obj = $this->db->fetch_object($result);
208
                $tmp_object = new RecruitmentJobPosition($this->db);
209
                if ($tmp_object->fetch($obj->rowid)) {
210
                    $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
211
                }
212
                $i++;
213
            }
214
        } else {
215
            throw new RestException(503, 'Error when retrieving jobposition list: ' . $this->db->lasterror());
216
        }
217
218
        return $obj_ret;
219
    }
220
221
    /**
222
     * List candatures
223
     *
224
     * Get a list of candidatures
225
     *
226
     * @param string           $sortfield           Sort field
227
     * @param string           $sortorder           Sort order
228
     * @param int              $limit               Limit for list
229
     * @param int              $page                Page number
230
     * @param string           $sqlfilters          Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
231
     * @return  array                               Array of order objects
232
     *
233
     * @throws RestException
234
     *
235
     * @url GET /candidature/
236
     */
237
    public function indexCandidature($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
238
    {
239
        global $db, $conf;
240
241
        $obj_ret = array();
242
        $tmpobject = new RecruitmentCandidature($this->db);
243
244
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'read')) {
245
            throw new RestException(403);
246
        }
247
248
        $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
249
250
        $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
251
252
        // If the internal user must only see his customers, force searching by him
253
        $search_sale = 0;
254
        if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
255
            $search_sale = DolibarrApiAccess::$user->id;
256
        }
257
258
        $sql = "SELECT t.rowid";
259
        $sql .= " FROM " . MAIN_DB_PREFIX . $tmpobject->table_element . " AS t";
260
        $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
261
        $sql .= " WHERE 1 = 1";
262
        if ($tmpobject->ismultientitymanaged) {
263
            $sql .= ' AND t.entity IN (' . getEntity($tmpobject->element) . ')';
264
        }
265
        if ($restrictonsocid && $socid) {
266
            $sql .= " AND t.fk_soc = " . ((int) $socid);
267
        }
268
        // Search on sale representative
269
        if ($search_sale && $search_sale != '-1') {
270
            if ($search_sale == -2) {
271
                $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
272
            } elseif ($search_sale > 0) {
273
                $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) . ")";
274
            }
275
        }
276
        if ($sqlfilters) {
277
            $errormessage = '';
278
            $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
279
            if ($errormessage) {
280
                throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage);
281
            }
282
        }
283
284
        $sql .= $this->db->order($sortfield, $sortorder);
285
        if ($limit) {
286
            if ($page < 0) {
287
                $page = 0;
288
            }
289
            $offset = $limit * $page;
290
291
            $sql .= $this->db->plimit($limit + 1, $offset);
292
        }
293
294
        $result = $this->db->query($sql);
295
        $i = 0;
296
        if ($result) {
297
            $num = $this->db->num_rows($result);
298
            while ($i < $num) {
299
                $obj = $this->db->fetch_object($result);
300
                $tmp_object = new RecruitmentCandidature($this->db);
301
                if ($tmp_object->fetch($obj->rowid)) {
302
                    $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
303
                }
304
                $i++;
305
            }
306
        } else {
307
            throw new RestException(503, 'Error when retrieving candidature list: ' . $this->db->lasterror());
308
        }
309
310
        return $obj_ret;
311
    }
312
313
    /**
314
     * Create jobposition object
315
     *
316
     * @param array $request_data   Request datas
317
     * @return int  ID of jobposition
318
     *
319
     * @throws RestException
320
     *
321
     * @url POST jobposition/
322
     */
323
    public function postJobPosition($request_data = null)
324
    {
325
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
326
            throw new RestException(403);
327
        }
328
329
        // Check mandatory fields
330
        $result = $this->_validate($request_data);
331
332
        foreach ($request_data as $field => $value) {
333
            if ($field === 'caller') {
334
                // 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
335
                $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
336
                continue;
337
            }
338
339
            $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
340
        }
341
342
        // Clean data
343
        // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
344
345
        if ($this->jobposition->create(DolibarrApiAccess::$user) < 0) {
346
            throw new RestException(500, "Error creating jobposition", array_merge(array($this->jobposition->error), $this->jobposition->errors));
347
        }
348
        return $this->jobposition->id;
349
    }
350
351
    /**
352
    * Create candidature object
353
    *
354
    * @param array $request_data   Request datas
355
    * @return int  ID of candidature
356
    *
357
    * @throws RestException
358
    *
359
    * @url  POST candidature/
360
    */
361
    public function postCandidature($request_data = null)
362
    {
363
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
364
            throw new RestException(403);
365
        }
366
367
        // Check mandatory fields
368
        $result = $this->_validate($request_data);
369
370
        foreach ($request_data as $field => $value) {
371
            if ($field === 'caller') {
372
                // 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
373
                $this->jobposition->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
374
                continue;
375
            }
376
377
            $this->jobposition->$field = $this->_checkValForAPI($field, $value, $this->jobposition);
378
        }
379
380
        // Clean data
381
        // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
382
383
        if ($this->candidature->create(DolibarrApiAccess::$user) < 0) {
384
            throw new RestException(500, "Error creating candidature", array_merge(array($this->candidature->error), $this->candidature->errors));
385
        }
386
        return $this->candidature->id;
387
    }
388
389
    /**
390
     * Update jobposition
391
     *
392
     * @param int   $id                     Id of jobposition to update
393
     * @param array $request_data           Datas
394
     * @return      Object                  Object with cleaned properties
395
     *
396
     * @throws RestException
397
     *
398
     * @url PUT jobposition/{id}
399
     */
400
    public function putJobPosition($id, $request_data = null)
401
    {
402
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
403
            throw new RestException(403);
404
        }
405
406
        $result = $this->jobposition->fetch($id);
407
        if (!$result) {
408
            throw new RestException(404, 'jobposition not found');
409
        }
410
411
        if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
412
            throw new RestException(403, 'Access to instance id=' . $this->jobposition->id . ' of object not allowed for login ' . DolibarrApiAccess::$user->login);
413
        }
414
415
        foreach ($request_data as $field => $value) {
416
            if ($field == 'id') {
417
                continue;
418
            }
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->jobposition->update(DolibarrApiAccess::$user, false) > 0) {
432
            return $this->getJobPosition($id);
433
        } else {
434
            throw new RestException(500, $this->jobposition->error);
435
        }
436
    }
437
438
    /**
439
     * Update candidature
440
     *
441
     * @param   int     $id             Id of candidature to update
442
     * @param   array   $request_data   Datas
443
     * @return  Object                  Object with cleaned properties
444
     *
445
     * @throws RestException
446
     *
447
     * @url PUT candidature/{id}
448
     */
449
    public function putCandidature($id, $request_data = null)
450
    {
451
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'write')) {
452
            throw new RestException(403);
453
        }
454
455
        $result = $this->candidature->fetch($id);
456
        if (!$result) {
457
            throw new RestException(404, 'candidature not found');
458
        }
459
460
        if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
461
            throw new RestException(403, 'Access to instance id=' . $this->candidature->id . ' of object not allowed for login ' . DolibarrApiAccess::$user->login);
462
        }
463
464
        foreach ($request_data as $field => $value) {
465
            if ($field == 'id') {
466
                continue;
467
            }
468
            if ($field === 'caller') {
469
                // 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
470
                $this->candidature->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
471
                continue;
472
            }
473
474
            $this->candidature->$field = $this->_checkValForAPI($field, $value, $this->candidature);
475
        }
476
477
        // Clean data
478
        // $this->jobposition->abc = sanitizeVal($this->jobposition->abc, 'alphanohtml');
479
480
        if ($this->candidature->update(DolibarrApiAccess::$user, false) > 0) {
481
            return $this->getCandidature($id);
482
        } else {
483
            throw new RestException(500, $this->candidature->error);
484
        }
485
    }
486
487
488
    /**
489
     * Delete jobposition
490
     *
491
     * @param   int     $id   jobposition ID
492
     * @return  array
493
     *
494
     * @throws RestException
495
     *
496
     * @url DELETE jobposition/{id}
497
     */
498
    public function deleteJobPosition($id)
499
    {
500
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
501
            throw new RestException(403);
502
        }
503
        $result = $this->jobposition->fetch($id);
504
        if (!$result) {
505
            throw new RestException(404, 'jobposition not found');
506
        }
507
508
        if (!DolibarrApi::_checkAccessToResource('recruitment', $this->jobposition->id, 'recruitment_recruitmentjobposition')) {
509
            throw new RestException(403, 'Access to instance id=' . $this->jobposition->id . ' of object not allowed for login ' . DolibarrApiAccess::$user->login);
510
        }
511
512
        if (!$this->jobposition->delete(DolibarrApiAccess::$user)) {
513
            throw new RestException(500, 'Error when deleting jobposition : ' . $this->jobposition->error);
514
        }
515
516
        return array(
517
            'success' => array(
518
                'code' => 200,
519
                'message' => 'jobposition deleted'
520
            )
521
        );
522
    }
523
524
    /**
525
     * Delete candidature
526
     *
527
     * @param   int     $id   candidature ID
528
     * @return  array
529
     *
530
     * @throws RestException
531
     *
532
     * @url DELETE candidature/{id}
533
     */
534
    public function deleteCandidature($id)
535
    {
536
        if (!DolibarrApiAccess::$user->hasRight('recruitment', 'recruitmentjobposition', 'delete')) {
537
            throw new RestException(403);
538
        }
539
        $result = $this->candidature->fetch($id);
540
        if (!$result) {
541
            throw new RestException(404, 'candidature not found');
542
        }
543
544
        if (!DolibarrApi::_checkAccessToResource('recruitment', $this->candidature->id, 'recruitment_recruitmentcandidature')) {
545
            throw new RestException(403, 'Access to instance id=' . $this->candidature->id . ' of object not allowed for login ' . DolibarrApiAccess::$user->login);
546
        }
547
548
        if (!$this->candidature->delete(DolibarrApiAccess::$user)) {
549
            throw new RestException(500, 'Error when deleting candidature : ' . $this->candidature->error);
550
        }
551
552
        return array(
553
            'success' => array(
554
                'code' => 200,
555
                'message' => 'candidature deleted'
556
            )
557
        );
558
    }
559
560
561
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
562
    /**
563
     * Clean sensible object datas
564
     *
565
     * @param   Object  $object     Object to clean
566
     * @return  Object              Object with cleaned properties
567
     */
568
    protected function _cleanObjectDatas($object)
569
    {
570
		// phpcs:enable
571
        $object = parent::_cleanObjectDatas($object);
572
573
        unset($object->rowid);
574
        unset($object->canvas);
575
576
        /*unset($object->name);
577
        unset($object->lastname);
578
        unset($object->firstname);
579
        unset($object->civility_id);
580
        unset($object->statut);
581
        unset($object->state);
582
        unset($object->state_id);
583
        unset($object->state_code);
584
        unset($object->region);
585
        unset($object->region_code);
586
        unset($object->country);
587
        unset($object->country_id);
588
        unset($object->country_code);
589
        unset($object->barcode_type);
590
        unset($object->barcode_type_code);
591
        unset($object->barcode_type_label);
592
        unset($object->barcode_type_coder);
593
        unset($object->total_ht);
594
        unset($object->total_tva);
595
        unset($object->total_localtax1);
596
        unset($object->total_localtax2);
597
        unset($object->total_ttc);
598
        unset($object->fk_account);
599
        unset($object->comments);
600
        unset($object->note);
601
        unset($object->mode_reglement_id);
602
        unset($object->cond_reglement_id);
603
        unset($object->cond_reglement);
604
        unset($object->shipping_method_id);
605
        unset($object->fk_incoterms);
606
        unset($object->label_incoterms);
607
        unset($object->location_incoterms);
608
        */
609
610
        // If object has lines, remove $db property
611
        if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
612
            $nboflines = count($object->lines);
613
            for ($i = 0; $i < $nboflines; $i++) {
614
                $this->_cleanObjectDatas($object->lines[$i]);
615
616
                unset($object->lines[$i]->lines);
617
                unset($object->lines[$i]->note);
618
            }
619
        }
620
621
        return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object also could return the type object which is incompatible with the documented return type object.
Loading history...
622
    }
623
624
    /**
625
     * Validate fields before create or update object
626
     *
627
     * @param   array       $data   Array of data to validate
628
     * @return  array
629
     *
630
     * @throws  RestException
631
     */
632
    private function _validate($data)
633
    {
634
        $jobposition = array();
635
        foreach ($this->jobposition->fields as $field => $propfield) {
636
            if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
637
                continue; // Not a mandatory field
638
            }
639
            if (!isset($data[$field])) {
640
                throw new RestException(400, "$field field missing");
641
            }
642
            $jobposition[$field] = $data[$field];
643
        }
644
        return $jobposition;
645
    }
646
}
647