19 use 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';
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();
116 public function getByEmail($email, $includecount = 0, $includeroles = 0)
118 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
119 throw new RestException(401,
'No permission to read contacts');
123 $result = $this->contact->initAsSpecimen();
125 $result = $this->contact->fetch(
'',
'',
'', $email);
129 throw new RestException(404,
'Contact not found');
133 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
137 $this->contact->load_ref_elements();
141 $this->contact->fetchRoles();
165 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $includecount = 0, $includeroles = 0)
171 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
172 throw new RestException(401,
'No permission to read contacts');
176 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
180 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
181 $search_sale = DolibarrApiAccess::$user->id;
184 $sql =
"SELECT t.rowid";
185 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
187 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_contact as c";
189 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
190 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
192 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
194 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
195 $sql .=
' WHERE t.entity IN ('.getEntity(
'contact').
')';
197 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
200 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
201 $sql .=
" AND t.fk_soc = sc.fk_soc";
203 if ($search_sale > 0) {
204 $sql .=
" AND s.rowid = sc.fk_soc";
207 if ($search_sale > 0) {
208 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
213 $sql .=
" AND c.fk_categorie = ".((int) $category);
214 $sql .=
" AND c.fk_socpeople = t.rowid ";
221 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
223 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
224 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
227 $sql .= $this->
db->order($sortfield, $sortorder);
233 $offset = $limit * $page;
235 $sql .= $this->
db->plimit($limit + 1, $offset);
237 $result = $this->
db->query($sql);
239 $num = $this->
db->num_rows($result);
240 $min = min($num, ($limit <= 0 ? $num : $limit));
243 $obj = $this->
db->fetch_object($result);
244 $contact_static =
new Contact($this->
db);
245 if ($contact_static->fetch($obj->rowid)) {
246 $contact_static->fetchRoles();
248 $contact_static->load_ref_elements();
251 $contact_static->fetchRoles();
260 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
262 if (!count($obj_ret)) {
263 throw new RestException(404,
'Contacts not found');
274 public function post($request_data =
null)
276 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
277 throw new RestException(401,
'No permission to create/update contacts');
280 $result = $this->
_validate($request_data);
282 foreach ($request_data as $field => $value) {
283 $this->contact->$field = $value;
285 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
286 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
288 return $this->contact->id;
298 public function put($id, $request_data =
null)
300 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
301 throw new RestException(401,
'No permission to create/update contacts');
304 $result = $this->contact->fetch($id);
306 throw new RestException(404,
'Contact not found');
310 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
313 foreach ($request_data as $field => $value) {
314 if ($field ==
'id') {
317 $this->contact->$field = $value;
320 if ($this->contact->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update')) {
321 return $this->
get($id);
333 public function delete($id)
335 if (!DolibarrApiAccess::$user->rights->societe->contact->supprimer) {
336 throw new RestException(401,
'No permission to delete contacts');
338 $result = $this->contact->fetch($id);
340 throw new RestException(404,
'Contact not found');
344 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
346 $this->contact->oldcopy = clone $this->contact;
347 return $this->contact->delete();
365 if (!isset($request_data[
"login"])) {
366 throw new RestException(400,
"login field missing");
368 if (!isset($request_data[
"password"])) {
369 throw new RestException(400,
"password field missing");
372 if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
373 throw new RestException(401,
'No permission to read contacts');
375 if (!DolibarrApiAccess::$user->rights->user->user->creer) {
376 throw new RestException(401,
'No permission to create user');
380 $contact->fetch($id);
381 if ($contact->id <= 0) {
382 throw new RestException(404,
'Contact not found');
386 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
390 $login = $request_data[
"login"];
391 $password = $request_data[
"password"];
392 $useraccount =
new User($this->
db);
393 $result = $useraccount->create_from_contact($contact, $login, $password);
395 throw new RestException(500,
"User not created");
398 $useraccount->setPassword($useraccount, $password);
416 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
418 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
419 throw new RestException(401);
424 $result = $categories->getListForItem($id,
'contact', $sortfield, $sortorder, $limit, $page);
426 if (empty($result)) {
427 throw new RestException(404,
'No category found');
431 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
452 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
453 throw new RestException(401,
'Insufficient rights');
456 $result = $this->contact->fetch($id);
458 throw new RestException(404,
'Contact not found');
461 $result = $category->fetch($category_id);
463 throw new RestException(404,
'category not found');
467 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
470 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
473 $category->add_type($this->contact,
'contact');
492 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
493 throw new RestException(401,
'Insufficient rights');
496 $result = $this->contact->fetch($id);
498 throw new RestException(404,
'Contact not found');
501 $result = $category->fetch($category_id);
503 throw new RestException(404,
'category not found');
507 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
510 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
513 $category->del_type($this->contact,
'contact');
528 $object = parent::_cleanObjectDatas($object);
530 unset($object->total_ht);
531 unset($object->total_tva);
532 unset($object->total_localtax1);
533 unset($object->total_localtax2);
534 unset($object->total_ttc);
536 unset($object->note);
537 unset($object->lines);
538 unset($object->thirdparty);
553 foreach (Contacts::$FIELDS as $field) {
554 if (!isset($data[$field])) {
555 throw new RestException(400,
"$field field missing");
557 $contact[$field] = $data[$field];