dolibarr 24.0.0-beta
actions_contactcard_common.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
30{
34 public $db;
35
39 public $dirmodule;
43 public $targetmodule;
47 public $canvas;
51 public $card;
52
56 public $tpl = array();
58
61 public $object;
62
66 public $error = '';
67
68
72 public $errors = array();
73
74
81 public function getObject($id)
82 {
83 /*$ret = $this->getInstanceDao();
84
85 if (is_object($this->object) && method_exists($this->object,'fetch'))
86 {
87 if (!empty($id)) $this->object->fetch($id);
88 }
89 else
90 {*/
91 $object = new Contact($this->db);
92 if (!empty($id)) {
93 $object->fetch($id);
94 }
95
96 $this->object = $object;
97
98 return $object;
99 }
100
101 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
110 public function assign_values(&$action, $id, $ref = '')
111 {
112 // phpcs:enable
113 global $conf, $langs, $user, $canvas;
114 global $form, $formcompany, $objsoc;
115
116 if ($action == 'add' || $action == 'update') {
117 $this->assign_post();
118 }
119
120 foreach ($this->object as $key => $value) {
121 $this->tpl[$key] = $value;
122 }
123
124 $this->tpl['error'] = $this->error;
125 $this->tpl['errors'] = $this->errors;
126
127 if ($action == 'create' || $action == 'edit') {
128 if ($conf->use_javascript_ajax) {
129 $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript">
130 jQuery(document).ready(function () {
131 jQuery("#selectcountry_id").change(function() {
132 document.formsoc.action.value="'.$action.'";
133 document.formsoc.canvas.value="'.$canvas.'";
134 document.formsoc.submit();
135 });
136 })
137 </script>'."\n";
138 }
139
140 if (is_object($objsoc) && $objsoc->id > 0) {
141 $this->tpl['company'] = $objsoc->getNomUrl(1);
142 $this->tpl['company_id'] = $objsoc->id;
143 } else {
144 $this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
145 }
146
147 // Civility
148 $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
149
150 // Predefined with third party
151 if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || getDolGlobalString('CONTACT_USE_COMPANY_ADDRESS')) {
152 if (dol_strlen(trim($this->object->address)) == 0) {
153 $this->tpl['address'] = $objsoc->address;
154 }
155 if (dol_strlen(trim($this->object->zip)) == 0) {
156 $this->object->zip = $objsoc->zip;
157 }
158 if (dol_strlen(trim($this->object->town)) == 0) {
159 $this->object->town = $objsoc->town;
160 }
161 if (dol_strlen(trim($this->object->phone_pro)) == 0) {
162 $this->object->phone_pro = $objsoc->phone;
163 }
164 if (dol_strlen(trim($this->object->fax)) == 0) {
165 $this->object->fax = $objsoc->fax;
166 }
167 if (dol_strlen(trim($this->object->email)) == 0) {
168 $this->object->email = $objsoc->email;
169 }
170 }
171
172 // Zip
173 $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6);
174
175 // Town
176 $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
177
178 if (dol_strlen(trim((string) $this->object->country_id)) == 0) {
179 $this->object->country_id = $objsoc->country_id;
180 }
181
182 // Country
183 $this->tpl['select_country'] = $form->select_country((string) $this->object->country_id, 'country_id');
184 $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
185
186 if ($user->admin) {
187 $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
188 }
189
190 // State
191 if ($this->object->country_id) {
192 $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
193 } else {
194 $this->tpl['select_state'] = $countrynotdefined;
195 }
196
197 // Public or private
198 $selectarray = array('0' => $langs->trans("ContactPublic"), '1' => $langs->trans("ContactPrivate"));
199 $this->tpl['select_visibility'] = $form->selectarray('priv', $selectarray, $this->object->priv, 0);
200 }
201
202 if ($action == 'view' || $action == 'edit' || $action == 'delete') {
203 // Emailing
204 if (isModEnabled('mailing')) {
205 $langs->load("mails");
206 $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
207 }
208
209 // Linked element
210 $this->tpl['contact_element'] = array();
211 $i = 0;
212
213 $this->object->load_ref_elements();
214
215 if (isModEnabled('order')) {
216 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
217 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande ? $this->object->ref_commande : $langs->trans("NoContactForAnyOrder");
218 $i++;
219 }
220 if (isModEnabled("propal")) {
221 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
222 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal ? $this->object->ref_propal : $langs->trans("NoContactForAnyProposal");
223 $i++;
224 }
225 if (isModEnabled('contract')) {
226 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
227 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat ? $this->object->ref_contrat : $langs->trans("NoContactForAnyContract");
228 $i++;
229 }
230 if (isModEnabled('invoice')) {
231 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
232 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation ? $this->object->ref_facturation : $langs->trans("NoContactForAnyInvoice");
233 $i++;
234 }
235
236 // Dolibarr user
237 if ($this->object->user_id) {
238 $dolibarr_user = new User($this->db);
239 $result = $dolibarr_user->fetch($this->object->user_id);
240 $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
241 } else {
242 $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
243 }
244 }
245
246 if ($action == 'view' || $action == 'delete') {
247 $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
248
249 if ($this->object->socid > 0) {
250 $objsoc = new Societe($this->db);
251
252 $objsoc->fetch($this->object->socid);
253 $this->tpl['company'] = $objsoc->getNomUrl(1);
254 } else {
255 $this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
256 }
257
258 $this->tpl['civility'] = $this->object->getCivilityLabel();
259
260 $this->tpl['address'] = dol_nl2br($this->object->address);
261
262 $this->tpl['zip'] = ($this->object->zip ? $this->object->zip.'&nbsp;' : '');
263
264 $img = picto_from_langcode($this->object->country_code);
265 $this->tpl['country'] = ($img ? $img.' ' : '').$this->object->country;
266
267 $this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
268 $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
269 $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
270 $this->tpl['fax'] = dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
271 $this->tpl['email'] = dol_print_email($this->object->email, 0, $this->object->id, 1);
272
273 $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
274
275 $this->tpl['note'] = $this->object->note_private;
276 }
277
278 if ($action == 'create_user') {
279 // Full firstname and lastname separated with a dot : firstname.lastname
280 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
281 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
282 $login = dol_buildlogin($this->object->lastname, $this->object->firstname);
283
284 $generated_password = getRandomPassword(false);
285 $password = $generated_password;
286
287 // Create a form array
288 $formquestion = array(
289 array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
290 array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
291
292 $this->tpl['action_create_user'] = $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$this->object->id, $langs->trans("CreateDolibarrLogin"), $langs->trans("ConfirmCreateContact"), "confirm_create_user", $formquestion, 'no');
293 }
294 }
295
296 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
302 private function assign_post()
303 {
304 // phpcs:enable
305 global $langs, $mysoc;
306
307 $this->object->socid = GETPOSTINT("socid");
308 $this->object->lastname = GETPOST("name");
309 $this->object->firstname = GETPOST("firstname");
310 $this->object->civility_id = GETPOST("civility_id");
311 $this->object->poste = GETPOST("poste");
312 $this->object->address = GETPOST("address");
313 $this->object->zip = GETPOST("zipcode");
314 $this->object->town = GETPOST("town");
315 $this->object->country_id = GETPOST("country_id") ? GETPOST("country_id") : $mysoc->country_id;
316 $this->object->state_id = GETPOSTINT("state_id");
317 $this->object->phone_pro = GETPOST("phone_pro");
318 $this->object->phone_perso = GETPOST("phone_perso");
319 $this->object->phone_mobile = GETPOST("phone_mobile");
320 $this->object->fax = GETPOST("fax");
321 $this->object->email = GETPOST("email");
322 $this->object->priv = GETPOSTINT("priv");
323 $this->object->note = GETPOST("note", "restricthtml");
324 $this->object->canvas = GETPOST("canvas");
325
326 // We set country_id, and country_code label of the chosen country
327 if ($this->object->country_id) {
328 $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".((int) $this->object->country_id);
329 $resql = $this->db->query($sql);
330 $obj = null;
331 if ($resql) {
332 $obj = $this->db->fetch_object($resql);
333 } else {
334 dol_print_error($this->db);
335 }
336 if ($obj !== null) {
337 $this->object->country_id = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
338 $this->object->country_code = $obj->code;
339 $this->object->country = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
340 }
341 }
342 }
343}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Common Abstract Class for contact managmeent.
assign_post()
Assign POST values into object.
assign_values(&$action, $id, $ref='')
Set content of ->tpl array, to use into template.
Class to manage contact/addresses.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_buildlogin($lastname, $firstname)
Build a login from lastname, firstname.
picto_from_langcode($codelang, $moreatt='', $notitlealt=0)
Return img flag of country for a language code or country code.
dol_print_phone($phone, $countrycode='', $contactid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='')
Show information in HTML for admin users or standard users.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_email($email, $contactid=0, $socid=0, $addlink=0, $max=0, $showinvalid=1, $withpicto=0, $morecss='paddingrightonly')
Show EMail link formatted for HTML output.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
getRandomPassword($generic=false, $replaceambiguouschars=null, $length=32)
Return a generated password using default module.