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();
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) {
180 throw new RestException(401,
'No permission to read contacts');
184 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
188 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
189 $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(
'contact').
')';
205 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
208 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
209 $sql .=
" AND t.fk_soc = sc.fk_soc";
211 if ($search_sale > 0) {
212 $sql .=
" AND s.rowid = sc.fk_soc";
215 if ($search_sale > 0) {
216 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
221 $sql .=
" AND c.fk_categorie = ".((int) $category);
222 $sql .=
" AND c.fk_socpeople = t.rowid ";
230 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
234 $sql .= $this->db->order($sortfield, $sortorder);
240 $offset = $limit * $page;
242 $sql .= $this->db->plimit($limit + 1, $offset);
244 $result = $this->db->query($sql);
246 $num = $this->db->num_rows($result);
247 $min = min($num, ($limit <= 0 ? $num : $limit));
250 $obj = $this->db->fetch_object($result);
251 $contact_static =
new Contact($this->db);
252 if ($contact_static->fetch($obj->rowid)) {
253 $contact_static->fetchRoles();
255 $contact_static->load_ref_elements();
258 $contact_static->fetchRoles();
260 if (isModEnabled(
'mailing')) {
261 $contact_static->getNoEmail();
270 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
272 if (!count($obj_ret)) {
273 throw new RestException(404,
'Contacts not found');
284 public function post($request_data =
null)
286 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
287 throw new RestException(401,
'No permission to create/update contacts');
290 $result = $this->
_validate($request_data);
292 foreach ($request_data as $field => $value) {
293 $this->contact->$field = $value;
295 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
296 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
298 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
299 $this->contact->setNoEmail($this->contact->no_email);
301 return $this->contact->id;
314 public function put($id, $request_data =
null)
316 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
317 throw new RestException(401,
'No permission to create/update contacts');
320 $result = $this->contact->fetch($id);
322 throw new RestException(404,
'Contact not found');
326 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
329 foreach ($request_data as $field => $value) {
330 if ($field ==
'id') {
332 } elseif ($field ==
'array_options' && is_array($value)) {
333 foreach ($value as $index => $val) {
334 $this->contact->array_options[$index] = $val;
337 $this->contact->$field = $value;
341 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
342 $this->contact->setNoEmail($this->contact->no_email);
345 if ($this->contact->update($id, DolibarrApiAccess::$user, 0,
'update') > 0) {
346 return $this->
get($id);
348 throw new RestException(500, $this->contact->error);
358 public function delete($id)
360 if (!DolibarrApiAccess::$user->rights->societe->contact->supprimer) {
361 throw new RestException(401,
'No permission to delete contacts');
363 $result = $this->contact->fetch($id);
365 throw new RestException(404,
'Contact not found');
369 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
371 $this->contact->oldcopy = clone $this->contact;
373 if ($this->contact->delete() <= 0) {
374 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
380 'message' =>
'Contact deleted'
400 if (!isset($request_data[
"login"])) {
401 throw new RestException(400,
"login field missing");
403 if (!isset($request_data[
"password"])) {
404 throw new RestException(400,
"password field missing");
407 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
408 throw new RestException(401,
'No permission to read contacts');
410 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
411 throw new RestException(401,
'No permission to create user');
414 $contact =
new Contact($this->db);
415 $contact->fetch($id);
416 if ($contact->id <= 0) {
417 throw new RestException(404,
'Contact not found');
421 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
425 $login = $request_data[
"login"];
426 $password = $request_data[
"password"];
427 $useraccount =
new User($this->db);
428 $result = $useraccount->create_from_contact($contact, $login, $password);
430 throw new RestException(500,
"User not created");
433 $useraccount->setPassword($useraccount, $password);
451 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
453 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
454 throw new RestException(401);
459 $result = $categories->getListForItem($id,
'contact', $sortfield, $sortorder, $limit, $page);
461 if (empty($result)) {
462 throw new RestException(404,
'No category found');
466 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
487 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
488 throw new RestException(401,
'Insufficient rights');
491 $result = $this->contact->fetch($id);
493 throw new RestException(404,
'Contact not found');
496 $result = $category->fetch($category_id);
498 throw new RestException(404,
'category not found');
502 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
505 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 $category->add_type($this->contact,
'contact');
527 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
528 throw new RestException(401,
'Insufficient rights');
531 $result = $this->contact->fetch($id);
533 throw new RestException(404,
'Contact not found');
536 $result = $category->fetch($category_id);
538 throw new RestException(404,
'category not found');
542 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
545 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 $category->del_type($this->contact,
'contact');
563 $object = parent::_cleanObjectDatas($object);
565 unset($object->total_ht);
566 unset($object->total_tva);
567 unset($object->total_localtax1);
568 unset($object->total_localtax2);
569 unset($object->total_ttc);
571 unset($object->note);
572 unset($object->lines);
573 unset($object->thirdparty);
588 foreach (Contacts::$FIELDS as $field) {
589 if (!isset($data[$field])) {
590 throw new RestException(400,
"$field field missing");
592 $contact[$field] = $data[$field];
Class to manage categories.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria