dolibarr 25.0.0-alpha
contact.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
3 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2007-2019 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', 1); // Disables token renewal
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1');
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1');
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1');
39}
40if (!defined('NOREQUIRESOC')) {
41 define('NOREQUIRESOC', '1');
42}
43
44// Load Dolibarr environment
45require '../../main.inc.php';
46require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
47
56$htmlname = GETPOST('htmlname', 'aZ09');
57$outjson = (GETPOSTINT('outjson') ? GETPOSTINT('outjson') : 0);
58$action = GETPOST('action', 'aZ09');
59
60$id = GETPOSTINT('id');
61$socid = GETPOSTINT('socid');
62$exclude = GETPOST('exclude', 'intcomma');
63$showsoc = GETPOSTINT('showsoc');
64
65$object = new Contact($db);
66if ($id > 0) {
67 $object->fetch($id);
68}
69
70// Security check
71if ($user->socid > 0) {
72 $action = null;
73 $socid = $user->socid;
74 $object->id = $socid;
75}
76restrictedArea($user, 'societe', $object->id, '&societe');
77
78$permissiontoread = $user->hasRight('societe', 'lire');
79
80
81/*
82 * View
83 */
84
85top_httphead('application/json');
86
87//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
88
89if ($action == 'fetch' && !empty($id) && $permissiontoread) {
90 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
91
92 $outjson = array();
93
94 if ($object->id > 0) {
95 $outref = $object->ref;
96 $outfirstname = $object->firstname;
97 $outlastname = $object->lastname;
98 $outdesc = '';
99
100 $outjson = array('ref' => $outref, 'firstname' => $outfirstname, 'lastname' => $outlastname, 'desc' => $outdesc);
101 }
102
103 echo json_encode($outjson);
104} elseif ($permissiontoread) { // $action can be 'getContacts'
105 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
106
107 if (empty($htmlname)) {
108 return 'Error value for parameter htmlname';
109 }
110
111 // The filter on the company to search for can be:
112 // Into an array with key $htmlname123 (we take first one found). Which page use this ?
113 // Into a var with name $htmlname can be 'elemid', ...
114 $match = preg_grep('/('.preg_quote($htmlname, '/').'[0-9]+)/', array_keys($_GET));
115 sort($match);
116
117 $id = (!empty($match[0]) ? $match[0] : ''); // Take first key found into GET array with matching $htmlname123
118
119 // When used from jQuery, the search term is added as GET param "term".
120 $searchkey = (($id && GETPOST($id, 'alpha')) ? GETPOST($id, 'alpha') : (($htmlname && GETPOST($htmlname, 'alpha')) ? GETPOST($htmlname, 'alpha') : ''));
121 // A non-numeric CONTACT_USE_SEARCH_TO_SELECT ('infinite', so getDolGlobalInt() returns 0) means "infinite
122 // list": an empty term is allowed and returns the (paginated) full list instead of nothing.
123 if (!$searchkey && getDolGlobalInt('CONTACT_USE_SEARCH_TO_SELECT') >= 1) {
124 return;
125 }
126
127 if (empty($form) || !is_object($form)) {
128 $form = new Form($db);
129 }
130
131 $limitto = '';
132 $showfunction = 0;
133 $morecss = 'minwidth100';
134 $options_only = 2;
135 $forcecombo = 0;
136 $events = array();
137 $moreparam = '';
138 $htmlid = '';
139 $multiple = 0;
140 $disableifempty = 0;
141
142 $prefix = getDolGlobalString('CONTACT_DONOTSEARCH_ANYWHERE') ? '' : '%'; // Can use index if CONTACT_DONOTSEARCH_ANYWHERE is on
143
144 $nbchar = 0;
145 $filter = '';
146 $listofsearchkey = preg_split('/\s+/', $searchkey);
147 foreach ($listofsearchkey as $searchkey) {
148 $nbchar += strlen($searchkey);
149
150 $filter .= ($filter ? ' AND ' : '');
151 $filter .= '(';
152 $filter .= "(lastname:like:'".$prefix.$searchkey."%') OR (firstname:like:'".$prefix.$searchkey."%')";
153 if ($showsoc) {
154 $filter .= " OR (s.nom:like:'".$prefix.$searchkey."%')";
155 }
156 $filter .= ')';
157 }
158
159 // If CONTACT_USE_SEARCH_TO_SELECT is set, check that nb of chars in $filter is >= to avoid DOS attack
160 if (getDolGlobalInt('CONTACT_USE_SEARCH_TO_SELECT') && $nbchar < getDolGlobalInt('CONTACT_USE_SEARCH_TO_SELECT')) {
161 print json_encode(array());
162 } else {
163 // Page size of this endpoint (CONTACT_LIMIT_SIZE): select2 keeps asking for the next page while a
164 // full page is returned, so the whole (filtered or "infinite list") result stays browsable by scrolling.
165 $limit = getDolGlobalInt('CONTACT_LIMIT_SIZE', 20);
166 $page = GETPOSTINT('page');
167 $limitoffset = ($page > 1) ? ($page - 1) * $limit : 0;
168
169 $arrayresult = $form->selectcontacts($socid, array(), (string) $htmlname, 1, $exclude, $limitto, $showfunction, $morecss, $options_only, $showsoc, $forcecombo, $events, $moreparam, $htmlid, (bool) $multiple, $disableifempty, $filter, $limit, $limitoffset);
170
171 print json_encode($arrayresult);
172 }
173}
174
175$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage contact/addresses.
Class to manage generation of HTML components Only common components must be here.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.