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();
176 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $includecount = 0, $includeroles = 0, $properties =
'', $pagination_data =
false)
182 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
183 throw new RestException(403,
'No permission to read contacts');
187 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
191 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
192 $search_sale = DolibarrApiAccess::$user->id;
195 $sql =
"SELECT t.rowid";
196 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
198 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_contact as c";
200 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
201 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
202 $sql .=
' WHERE t.entity IN ('.getEntity(
'contact').
')';
204 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
207 if ($search_sale && $search_sale !=
'-1') {
208 if ($search_sale == -2) {
209 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
210 } elseif ($search_sale > 0) {
211 $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).
")";
216 $sql .=
" AND c.fk_categorie = ".((int) $category);
217 $sql .=
" AND c.fk_socpeople = t.rowid ";
225 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
230 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
232 $sql .= $this->db->order($sortfield, $sortorder);
238 $offset = $limit * $page;
240 $sql .= $this->db->plimit($limit + 1, $offset);
242 $result = $this->db->query($sql);
244 $num = $this->db->num_rows($result);
245 $min = min($num, ($limit <= 0 ? $num : $limit));
248 $obj = $this->db->fetch_object($result);
249 $contact_static =
new Contact($this->db);
250 if ($contact_static->fetch($obj->rowid)) {
251 $contact_static->fetchRoles();
253 $contact_static->load_ref_elements();
256 $contact_static->fetchRoles();
258 if (isModEnabled(
'mailing')) {
259 $contact_static->getNoEmail();
268 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
272 if ($pagination_data) {
273 $totalsResult = $this->db->query($sqlTotals);
274 $total = $this->db->fetch_object($totalsResult)->total;
279 $obj_ret[
'data'] = $tmp;
280 $obj_ret[
'pagination'] = [
281 'total' => (int) $total,
283 'page_count' => ceil((
int) $total / $limit),
299 public function post($request_data =
null)
301 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
302 throw new RestException(403,
'No permission to create/update contacts');
305 $result = $this->
_validate($request_data);
307 foreach ($request_data as $field => $value) {
308 if ($field ===
'caller') {
310 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
313 if ($field ==
'array_options' && is_array($value)) {
314 foreach ($value as $index => $val) {
315 $this->contact->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->contact);
320 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
322 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
323 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
325 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
326 $this->contact->setNoEmail($this->contact->no_email);
328 return $this->contact->id;
342 public function put(
$id, $request_data =
null)
344 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
345 throw new RestException(403,
'No permission to create/update contacts');
348 $result = $this->contact->fetch(
$id);
350 throw new RestException(404,
'Contact not found');
354 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
357 foreach ($request_data as $field => $value) {
358 if ($field ==
'id') {
361 if ($field ===
'caller') {
363 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
366 if ($field ==
'array_options' && is_array($value)) {
367 foreach ($value as $index => $val) {
368 $this->contact->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->contact);
373 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
376 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
377 $this->contact->setNoEmail($this->contact->no_email);
380 if ($this->contact->update(
$id, DolibarrApiAccess::$user, 0,
'update') > 0) {
381 return $this->
get(
$id);
383 throw new RestException(500, $this->contact->error);
393 public function delete(
$id)
395 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
396 throw new RestException(403,
'No permission to delete contacts');
398 $result = $this->contact->fetch(
$id);
400 throw new RestException(404,
'Contact not found');
404 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
406 $this->contact->oldcopy = clone $this->contact;
408 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
409 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
415 'message' =>
'Contact deleted'
436 if (!isset($request_data[
"login"])) {
437 throw new RestException(400,
"login field missing");
439 if (!isset($request_data[
"password"])) {
440 throw new RestException(400,
"password field missing");
443 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
444 throw new RestException(403,
'No permission to read contacts');
446 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
447 throw new RestException(403,
'No permission to create user');
450 $contact =
new Contact($this->db);
451 $contact->fetch(
$id);
452 if ($contact->id <= 0) {
453 throw new RestException(404,
'Contact not found');
457 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
461 $login = $request_data[
"login"];
462 $password = $request_data[
"password"];
463 $useraccount =
new User($this->db);
464 $result = $useraccount->create_from_contact($contact, $login, $password);
466 throw new RestException(500,
"User not created");
469 $useraccount->setPassword($useraccount, $password);
487 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
489 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
490 throw new RestException(403);
495 $result = $categories->getListForItem(
$id,
'contact', $sortfield, $sortorder, $limit, $page);
498 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
519 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
520 throw new RestException(403,
'Insufficient rights');
523 $result = $this->contact->fetch(
$id);
525 throw new RestException(404,
'Contact not found');
528 $result = $category->fetch($category_id);
530 throw new RestException(404,
'category not found');
534 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
537 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 $category->add_type($this->contact,
'contact');
559 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
560 throw new RestException(403,
'Insufficient rights');
563 $result = $this->contact->fetch(
$id);
565 throw new RestException(404,
'Contact not found');
568 $result = $category->fetch($category_id);
570 throw new RestException(404,
'category not found');
574 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
577 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
580 $category->del_type($this->contact,
'contact');
599 unset(
$object->total_localtax1);
600 unset(
$object->total_localtax2);
620 foreach (Contacts::$FIELDS as $field) {
621 if (!isset($data[$field])) {
622 throw new RestException(400,
"$field field missing");
624 $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.