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;
392 return $this->contact->delete(DolibarrApiAccess::$user);
410 if (!isset($request_data[
"login"])) {
411 throw new RestException(400,
"login field missing");
413 if (!isset($request_data[
"password"])) {
414 throw new RestException(400,
"password field missing");
417 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
418 throw new RestException(401,
'No permission to read contacts');
420 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
421 throw new RestException(401,
'No permission to create user');
424 $contact =
new Contact($this->db);
425 $contact->fetch($id);
426 if ($contact->id <= 0) {
427 throw new RestException(404,
'Contact not found');
431 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
435 $login = $request_data[
"login"];
436 $password = $request_data[
"password"];
437 $useraccount =
new User($this->db);
438 $result = $useraccount->create_from_contact($contact, $login, $password);
440 throw new RestException(500,
"User not created");
443 $useraccount->setPassword($useraccount, $password);
461 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
463 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
464 throw new RestException(401);
469 $result = $categories->getListForItem($id,
'contact', $sortfield, $sortorder, $limit, $page);
472 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
493 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
494 throw new RestException(401,
'Insufficient rights');
497 $result = $this->contact->fetch($id);
499 throw new RestException(404,
'Contact not found');
502 $result = $category->fetch($category_id);
504 throw new RestException(404,
'category not found');
508 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
511 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
514 $category->add_type($this->contact,
'contact');
533 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
534 throw new RestException(401,
'Insufficient rights');
537 $result = $this->contact->fetch($id);
539 throw new RestException(404,
'Contact not found');
542 $result = $category->fetch($category_id);
544 throw new RestException(404,
'category not found');
548 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
551 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
554 $category->del_type($this->contact,
'contact');
569 $object = parent::_cleanObjectDatas($object);
571 unset($object->total_ht);
572 unset($object->total_tva);
573 unset($object->total_localtax1);
574 unset($object->total_localtax2);
575 unset($object->total_ttc);
577 unset($object->note);
578 unset($object->lines);
579 unset($object->thirdparty);
594 foreach (Contacts::$FIELDS as $field) {
595 if (!isset($data[$field])) {
596 throw new RestException(400,
"$field field missing");
598 $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