20use Luracast\Restler\RestException;
38 public static $FIELDS = array(
55 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
56 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
58 $this->contact =
new Contact($this->db);
73 public function get($id, $includecount = 0, $includeroles = 0)
75 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
76 throw new RestException(403,
'No permission to read contacts');
80 $result = $this->contact->initAsSpecimen();
82 $result = $this->contact->fetch($id);
86 throw new RestException(404,
'Contact not found');
90 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
94 $this->contact->load_ref_elements();
98 $this->contact->fetchRoles();
101 if (isModEnabled(
'mailing')) {
102 $this->contact->getNoEmail();
121 public function getByEmail($email, $includecount = 0, $includeroles = 0)
123 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
124 throw new RestException(403,
'No permission to read contacts');
128 $result = $this->contact->initAsSpecimen();
130 $result = $this->contact->fetch(
'',
'',
'', $email);
134 throw new RestException(404,
'Contact not found');
138 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
142 $this->contact->load_ref_elements();
146 $this->contact->fetchRoles();
149 if (isModEnabled(
'mailing')) {
150 $this->contact->getNoEmail();
175 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $includecount = 0, $includeroles = 0, $properties =
'')
181 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
182 throw new RestException(403,
'No permission to read contacts');
186 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
190 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
191 $search_sale = DolibarrApiAccess::$user->id;
194 $sql =
"SELECT t.rowid";
195 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
197 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_contact as c";
199 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
200 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
201 $sql .=
' WHERE t.entity IN ('.getEntity(
'contact').
')';
203 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
206 if ($search_sale && $search_sale !=
'-1') {
207 if ($search_sale == -2) {
208 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
209 } elseif ($search_sale > 0) {
210 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
215 $sql .=
" AND c.fk_categorie = ".((int) $category);
216 $sql .=
" AND c.fk_socpeople = t.rowid ";
224 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
228 $sql .= $this->db->order($sortfield, $sortorder);
234 $offset = $limit * $page;
236 $sql .= $this->db->plimit($limit + 1, $offset);
238 $result = $this->db->query($sql);
240 $num = $this->db->num_rows($result);
241 $min = min($num, ($limit <= 0 ? $num : $limit));
244 $obj = $this->db->fetch_object($result);
245 $contact_static =
new Contact($this->db);
246 if ($contact_static->fetch($obj->rowid)) {
247 $contact_static->fetchRoles();
249 $contact_static->load_ref_elements();
252 $contact_static->fetchRoles();
254 if (isModEnabled(
'mailing')) {
255 $contact_static->getNoEmail();
264 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
266 if (!count($obj_ret)) {
267 throw new RestException(404,
'Contacts not found');
280 public function post($request_data =
null)
282 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
283 throw new RestException(403,
'No permission to create/update contacts');
286 $result = $this->
_validate($request_data);
288 foreach ($request_data as $field => $value) {
289 if ($field ===
'caller') {
291 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
294 if ($field ==
'array_options' && is_array($value)) {
295 foreach ($value as $index => $val) {
296 $this->contact->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->contact);
301 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
303 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
304 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
306 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
307 $this->contact->setNoEmail($this->contact->no_email);
309 return $this->contact->id;
323 public function put($id, $request_data =
null)
325 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
326 throw new RestException(403,
'No permission to create/update contacts');
329 $result = $this->contact->fetch($id);
331 throw new RestException(404,
'Contact not found');
335 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
338 foreach ($request_data as $field => $value) {
339 if ($field ==
'id') {
342 if ($field ===
'caller') {
344 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
347 if ($field ==
'array_options' && is_array($value)) {
348 foreach ($value as $index => $val) {
349 $this->contact->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->contact);
354 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
357 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
358 $this->contact->setNoEmail($this->contact->no_email);
361 if ($this->contact->update($id, DolibarrApiAccess::$user, 0,
'update') > 0) {
362 return $this->
get($id);
364 throw new RestException(500, $this->contact->error);
374 public function delete($id)
376 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
377 throw new RestException(403,
'No permission to delete contacts');
379 $result = $this->contact->fetch($id);
381 throw new RestException(404,
'Contact not found');
385 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 $this->contact->oldcopy = clone $this->contact;
388 return $this->contact->delete(DolibarrApiAccess::$user);
407 if (!isset($request_data[
"login"])) {
408 throw new RestException(400,
"login field missing");
410 if (!isset($request_data[
"password"])) {
411 throw new RestException(400,
"password field missing");
414 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
415 throw new RestException(403,
'No permission to read contacts');
417 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
418 throw new RestException(403,
'No permission to create user');
421 $contact =
new Contact($this->db);
422 $contact->fetch($id);
423 if ($contact->id <= 0) {
424 throw new RestException(404,
'Contact not found');
428 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
432 $login = $request_data[
"login"];
433 $password = $request_data[
"password"];
434 $useraccount =
new User($this->db);
435 $result = $useraccount->create_from_contact($contact, $login, $password);
437 throw new RestException(500,
"User not created");
440 $useraccount->setPassword($useraccount, $password);
458 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
460 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
461 throw new RestException(403);
466 $result = $categories->getListForItem($id,
'contact', $sortfield, $sortorder, $limit, $page);
469 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
490 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
491 throw new RestException(403,
'Insufficient rights');
494 $result = $this->contact->fetch($id);
496 throw new RestException(404,
'Contact not found');
499 $result = $category->fetch($category_id);
501 throw new RestException(404,
'category not found');
505 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
511 $category->add_type($this->contact,
'contact');
530 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
531 throw new RestException(403,
'Insufficient rights');
534 $result = $this->contact->fetch($id);
536 throw new RestException(404,
'Contact not found');
539 $result = $category->fetch($category_id);
541 throw new RestException(404,
'category not found');
545 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
551 $category->del_type($this->contact,
'contact');
570 unset(
$object->total_localtax1);
571 unset(
$object->total_localtax2);
591 foreach (Contacts::$FIELDS as $field) {
592 if (!isset($data[$field])) {
593 throw new RestException(400,
"$field field missing");
595 $contact[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.