19 use Luracast\Restler\RestException;
37 static $FIELDS = array(
54 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
55 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
72 public function get($id, $includecount = 0, $includeroles = 0)
74 if (!DolibarrApiAccess::$user->rights->societe->contact->lire)
76 throw new RestException(401,
'No permission to read contacts');
79 $result = $this->contact->initAsSpecimen();
81 $result = $this->contact->fetch($id);
86 throw new RestException(404,
'Contact not found');
91 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
96 $this->contact->load_ref_elements();
101 $this->contact->fetchRoles();
120 public function getByEmail($email, $includecount = 0, $includeroles = 0)
122 if (!DolibarrApiAccess::$user->rights->societe->contact->lire)
124 throw new RestException(401,
'No permission to read contacts');
127 $result = $this->contact->initAsSpecimen();
129 $result = $this->contact->fetch(
'',
'',
'', $email);
134 throw new RestException(404,
'Contact not found');
139 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
144 $this->contact->load_ref_elements();
149 $this->contact->fetchRoles();
173 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $includecount = 0, $includeroles = 0)
179 if (!DolibarrApiAccess::$user->rights->societe->contact->lire)
181 throw new RestException(401,
'No permission to read contacts');
185 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
189 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)
190 $search_sale = DolibarrApiAccess::$user->id;
192 $sql =
"SELECT t.rowid";
193 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
195 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_contact as c";
197 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
198 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
200 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
202 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
203 $sql .=
' WHERE t.entity IN ('.getEntity(
'socpeople').
')';
204 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
206 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0)
207 $sql .=
" AND t.fk_soc = sc.fk_soc";
208 if ($search_sale > 0)
209 $sql .=
" AND s.rowid = sc.fk_soc";
211 if ($search_sale > 0)
213 $sql .=
" AND sc.fk_user = ".$search_sale;
218 $sql .=
" AND c.fk_categorie = ".$this->db->escape($category);
219 $sql .=
" AND c.fk_socpeople = t.rowid ";
227 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
229 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
230 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
233 $sql .= $this->
db->order($sortfield, $sortorder);
241 $offset = $limit * $page;
243 $sql .= $this->
db->plimit($limit + 1, $offset);
245 $result = $this->
db->query($sql);
248 $num = $this->
db->num_rows($result);
249 $min = min($num, ($limit <= 0 ? $num : $limit));
253 $obj = $this->
db->fetch_object($result);
254 $contact_static =
new Contact($this->
db);
255 if ($contact_static->fetch($obj->rowid))
257 $contact_static->fetchRoles();
260 $contact_static->load_ref_elements();
264 $contact_static->fetchRoles();
273 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
275 if (!count($obj_ret))
277 throw new RestException(404,
'Contacts not found');
288 public function post($request_data = null)
290 if (!DolibarrApiAccess::$user->rights->societe->contact->creer)
292 throw new RestException(401,
'No permission to create/update contacts');
295 $result = $this->
_validate($request_data);
297 foreach ($request_data as $field => $value)
299 $this->contact->$field = $value;
301 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
302 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
304 return $this->contact->id;
314 public function put($id, $request_data = null)
316 if (!DolibarrApiAccess::$user->rights->societe->contact->creer)
318 throw new RestException(401,
'No permission to create/update contacts');
321 $result = $this->contact->fetch($id);
324 throw new RestException(404,
'Contact not found');
329 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
332 foreach ($request_data as $field => $value)
334 if ($field ==
'id')
continue;
335 $this->contact->$field = $value;
338 if ($this->contact->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update'))
339 return $this->
get($id);
350 public function delete($id)
352 if (!DolibarrApiAccess::$user->rights->societe->contact->supprimer)
354 throw new RestException(401,
'No permission to delete contacts');
356 $result = $this->contact->fetch($id);
359 throw new RestException(404,
'Contact not found');
364 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
366 $this->contact->oldcopy = clone $this->contact;
367 return $this->contact->delete();
385 if (!isset($request_data[
"login"]))
386 throw new RestException(400,
"login field missing");
387 if (!isset($request_data[
"password"]))
388 throw new RestException(400,
"password field missing");
390 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
391 throw new RestException(401,
'No permission to read contacts');
393 if (!DolibarrApiAccess::$user->rights->user->user->creer) {
394 throw new RestException(401,
'No permission to create user');
398 $contact->fetch($id);
399 if ($contact->id <= 0) {
400 throw new RestException(404,
'Contact not found');
404 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
408 $login = $request_data[
"login"];
409 $password = $request_data[
"password"];
410 $useraccount =
new User($this->
db);
411 $result = $useraccount->create_from_contact($contact, $login, $password);
413 throw new RestException(500,
"User not created");
416 $useraccount->setPassword($useraccount, $password);
434 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
436 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
437 throw new RestException(401);
442 $result = $categories->getListForItem($id,
'contact', $sortfield, $sortorder, $limit, $page);
444 if (empty($result)) {
445 throw new RestException(404,
'No category found');
449 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
470 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
471 throw new RestException(401,
'Insufficient rights');
474 $result = $this->contact->fetch($id);
476 throw new RestException(404,
'Contact not found');
479 $result = $category->fetch($category_id);
481 throw new RestException(404,
'category not found');
485 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
488 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
491 $category->add_type($this->contact,
'contact');
510 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
511 throw new RestException(401,
'Insufficient rights');
514 $result = $this->contact->fetch($id);
516 throw new RestException(404,
'Contact not found');
519 $result = $category->fetch($category_id);
521 throw new RestException(404,
'category not found');
525 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
528 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
531 $category->del_type($this->contact,
'contact');
546 $object = parent::_cleanObjectDatas($object);
548 unset($object->total_ht);
549 unset($object->total_tva);
550 unset($object->total_localtax1);
551 unset($object->total_localtax2);
552 unset($object->total_ttc);
554 unset($object->note);
555 unset($object->lines);
556 unset($object->thirdparty);
571 foreach (Contacts::$FIELDS as $field) {
572 if (!isset($data[$field])) {
573 throw new RestException(400,
"$field field missing");
575 $contact[$field] = $data[$field];
Class to manage Dolibarr users.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Class to manage categories.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.