19use Luracast\Restler\RestException;
37 public 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';
57 $this->contact =
new Contact($this->db);
72 public function get($id, $includecount = 0, $includeroles = 0)
74 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
75 throw new RestException(401,
'No permission to read contacts');
79 $result = $this->contact->initAsSpecimen();
81 $result = $this->contact->fetch($id);
85 throw new RestException(404,
'Contact not found');
89 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
93 $this->contact->load_ref_elements();
97 $this->contact->fetchRoles();
100 if (isModEnabled(
'mailing')) {
101 $this->contact->getNoEmail();
120 public function getByEmail($email, $includecount = 0, $includeroles = 0)
122 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
123 throw new RestException(401,
'No permission to read contacts');
127 $result = $this->contact->initAsSpecimen();
129 $result = $this->contact->fetch(
'',
'',
'', $email);
133 throw new RestException(404,
'Contact not found');
137 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
141 $this->contact->load_ref_elements();
145 $this->contact->fetchRoles();
148 if (isModEnabled(
'mailing')) {
149 $this->contact->getNoEmail();
174 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $includecount = 0, $includeroles = 0, $properties =
'')
180 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;
193 $sql =
"SELECT t.rowid";
194 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
196 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_contact as c";
198 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
199 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
201 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
203 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
204 $sql .=
' WHERE t.entity IN ('.getEntity(
'contact').
')';
206 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
209 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
210 $sql .=
" AND t.fk_soc = sc.fk_soc";
212 if ($search_sale > 0) {
213 $sql .=
" AND s.rowid = sc.fk_soc";
216 if ($search_sale > 0) {
217 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
222 $sql .=
" AND c.fk_categorie = ".((int) $category);
223 $sql .=
" AND c.fk_socpeople = t.rowid ";
231 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
235 $sql .= $this->db->order($sortfield, $sortorder);
241 $offset = $limit * $page;
243 $sql .= $this->db->plimit($limit + 1, $offset);
245 $result = $this->db->query($sql);
247 $num = $this->db->num_rows($result);
248 $min = min($num, ($limit <= 0 ? $num : $limit));
251 $obj = $this->db->fetch_object($result);
252 $contact_static =
new Contact($this->db);
253 if ($contact_static->fetch($obj->rowid)) {
254 $contact_static->fetchRoles();
256 $contact_static->load_ref_elements();
259 $contact_static->fetchRoles();
261 if (isModEnabled(
'mailing')) {
262 $contact_static->getNoEmail();
271 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
273 if (!count($obj_ret)) {
274 throw new RestException(404,
'Contacts not found');
285 public function post($request_data =
null)
287 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
288 throw new RestException(401,
'No permission to create/update contacts');
291 $result = $this->
_validate($request_data);
293 foreach ($request_data as $field => $value) {
294 if ($field ===
'caller') {
296 $this->contact->context[
'caller'] = $request_data[
'caller'];
299 if ($field ==
'array_options' && is_array($value)) {
300 foreach ($value as $index => $val) {
301 $this->contact->array_options[$index] = $val;
306 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
308 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
309 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
311 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
312 $this->contact->setNoEmail($this->contact->no_email);
314 return $this->contact->id;
327 public function put($id, $request_data =
null)
329 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
330 throw new RestException(401,
'No permission to create/update contacts');
333 $result = $this->contact->fetch($id);
335 throw new RestException(404,
'Contact not found');
339 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
342 foreach ($request_data as $field => $value) {
343 if ($field ==
'id') {
346 if ($field ===
'caller') {
348 $this->contact->context[
'caller'] = $request_data[
'caller'];
351 if ($field ==
'array_options' && is_array($value)) {
352 foreach ($value as $index => $val) {
353 $this->contact->array_options[$index] = $val;
358 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
361 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
362 $this->contact->setNoEmail($this->contact->no_email);
365 if ($this->contact->update($id, DolibarrApiAccess::$user, 0,
'update') > 0) {
366 return $this->
get($id);
368 throw new RestException(500, $this->contact->error);
378 public function delete($id)
380 if (!DolibarrApiAccess::$user->rights->societe->contact->supprimer) {
381 throw new RestException(401,
'No permission to delete contacts');
383 $result = $this->contact->fetch($id);
385 throw new RestException(404,
'Contact not found');
389 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
391 $this->contact->oldcopy = clone $this->contact;
393 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
394 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
400 'message' =>
'Contact deleted'
420 if (!isset($request_data[
"login"])) {
421 throw new RestException(400,
"login field missing");
423 if (!isset($request_data[
"password"])) {
424 throw new RestException(400,
"password field missing");
427 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
428 throw new RestException(401,
'No permission to read contacts');
430 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
431 throw new RestException(401,
'No permission to create user');
434 $contact =
new Contact($this->db);
435 $contact->fetch($id);
436 if ($contact->id <= 0) {
437 throw new RestException(404,
'Contact not found');
441 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
445 $login = $request_data[
"login"];
446 $password = $request_data[
"password"];
447 $useraccount =
new User($this->db);
448 $result = $useraccount->create_from_contact($contact, $login, $password);
450 throw new RestException(500,
"User not created");
453 $useraccount->setPassword($useraccount, $password);
471 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
473 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
474 throw new RestException(401);
479 $result = $categories->getListForItem($id,
'contact', $sortfield, $sortorder, $limit, $page);
482 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
503 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
504 throw new RestException(401,
'Insufficient rights');
507 $result = $this->contact->fetch($id);
509 throw new RestException(404,
'Contact not found');
512 $result = $category->fetch($category_id);
514 throw new RestException(404,
'category not found');
518 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
521 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
524 $category->add_type($this->contact,
'contact');
543 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
544 throw new RestException(401,
'Insufficient rights');
547 $result = $this->contact->fetch($id);
549 throw new RestException(404,
'Contact not found');
552 $result = $category->fetch($category_id);
554 throw new RestException(404,
'category not found');
558 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
561 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
564 $category->del_type($this->contact,
'contact');
579 $object = parent::_cleanObjectDatas($object);
581 unset($object->total_ht);
582 unset($object->total_tva);
583 unset($object->total_localtax1);
584 unset($object->total_localtax2);
585 unset($object->total_ttc);
587 unset($object->note);
588 unset($object->lines);
589 unset($object->thirdparty);
604 foreach (Contacts::$FIELDS as $field) {
605 if (!isset($data[$field])) {
606 throw new RestException(400,
"$field field missing");
608 $contact[$field] = $data[$field];
Class to manage categories.
_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.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria