dolibarr 21.0.0-alpha
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 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
109 public function assign_values(&$action, $id)
110 {
111 // phpcs:enable
112 global $conf, $langs, $user, $canvas;
113 global $form, $formcompany, $objsoc;
114
115 if ($action == 'add' || $action == 'update') {
116 $this->assign_post();
117 }
118
119 foreach ($this->object as $key => $value) {
120 $this->tpl[$key] = $value;
121 }
122
123 $this->tpl['error'] = $this->error;
124 $this->tpl['errors'] = $this->errors;
125
126 if ($action == 'create' || $action == 'edit') {
127 if ($conf->use_javascript_ajax) {
128 $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript">
129 jQuery(document).ready(function () {
130 jQuery("#selectcountry_id").change(function() {
131 document.formsoc.action.value="'.$action.'";
132 document.formsoc.canvas.value="'.$canvas.'";
133 document.formsoc.submit();
134 });
135 })
136 </script>'."\n";
137 }
138
139 if (is_object($objsoc) && $objsoc->id > 0) {
140 $this->tpl['company'] = $objsoc->getNomUrl(1);
141 $this->tpl['company_id'] = $objsoc->id;
142 } else {
143 $this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
144 }
145
146 // Civility
147 $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
148
149 // Predefined with third party
150 if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || getDolGlobalString('CONTACT_USE_COMPANY_ADDRESS')) {
151 if (dol_strlen(trim($this->object->address)) == 0) {
152 $this->tpl['address'] = $objsoc->address;
153 }
154 if (dol_strlen(trim($this->object->zip)) == 0) {
155 $this->object->zip = $objsoc->zip;
156 }
157 if (dol_strlen(trim($this->object->town)) == 0) {
158 $this->object->town = $objsoc->town;
159 }
160 if (dol_strlen(trim($this->object->phone_pro)) == 0) {
161 $this->object->phone_pro = $objsoc->phone;
162 }
163 if (dol_strlen(trim($this->object->fax)) == 0) {
164 $this->object->fax = $objsoc->fax;
165 }
166 if (dol_strlen(trim($this->object->email)) == 0) {
167 $this->object->email = $objsoc->email;
168 }
169 }
170
171 // Zip
172 $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6);
173
174 // Town
175 $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
176
177 if (dol_strlen(trim((string) $this->object->country_id)) == 0) {
178 $this->object->country_id = $objsoc->country_id;
179 }
180
181 // Country
182 $this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
183 $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
184
185 if ($user->admin) {
186 $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
187 }
188
189 // State
190 if ($this->object->country_id) {
191 $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
192 } else {
193 $this->tpl['select_state'] = $countrynotdefined;
194 }
195
196 // Public or private
197 $selectarray = array('0' => $langs->trans("ContactPublic"), '1' => $langs->trans("ContactPrivate"));
198 $this->tpl['select_visibility'] = $form->selectarray('priv', $selectarray, $this->object->priv, 0);
199 }
200
201 if ($action == 'view' || $action == 'edit' || $action == 'delete') {
202 // Emailing
203 if (isModEnabled('mailing')) {
204 $langs->load("mails");
205 $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
206 }
207
208 // Linked element
209 $this->tpl['contact_element'] = array();
210 $i = 0;
211
212 $this->object->load_ref_elements();
213
214 if (isModEnabled('order')) {
215 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
216 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande ? $this->object->ref_commande : $langs->trans("NoContactForAnyOrder");
217 $i++;
218 }
219 if (isModEnabled("propal")) {
220 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
221 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal ? $this->object->ref_propal : $langs->trans("NoContactForAnyProposal");
222 $i++;
223 }
224 if (isModEnabled('contract')) {
225 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
226 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat ? $this->object->ref_contrat : $langs->trans("NoContactForAnyContract");
227 $i++;
228 }
229 if (isModEnabled('invoice')) {
230 $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
231 $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation ? $this->object->ref_facturation : $langs->trans("NoContactForAnyInvoice");
232 $i++;
233 }
234
235 // Dolibarr user
236 if ($this->object->user_id) {
237 $dolibarr_user = new User($this->db);
238 $result = $dolibarr_user->fetch($this->object->user_id);
239 $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
240 } else {
241 $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
242 }
243 }
244
245 if ($action == 'view' || $action == 'delete') {
246 $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
247
248 if ($this->object->socid > 0) {
249 $objsoc = new Societe($this->db);
250
251 $objsoc->fetch($this->object->socid);
252 $this->tpl['company'] = $objsoc->getNomUrl(1);
253 } else {
254 $this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
255 }
256
257 $this->tpl['civility'] = $this->object->getCivilityLabel();
258
259 $this->tpl['address'] = dol_nl2br($this->object->address);
260
261 $this->tpl['zip'] = ($this->object->zip ? $this->object->zip.'&nbsp;' : '');
262
263 $img = picto_from_langcode($this->object->country_code);
264 $this->tpl['country'] = ($img ? $img.' ' : '').$this->object->country;
265
266 $this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
267 $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
268 $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
269 $this->tpl['fax'] = dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
270 $this->tpl['email'] = dol_print_email($this->object->email, 0, $this->object->id, 1);
271
272 $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
273
274 $this->tpl['note'] = $this->object->note_private;
275 }
276
277 if ($action == 'create_user') {
278 // Full firstname and lastname separated with a dot : firstname.lastname
279 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
280 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
281 $login = dol_buildlogin($this->object->lastname, $this->object->firstname);
282
283 $generated_password = getRandomPassword(false);
284 $password = $generated_password;
285
286 // Create a form array
287 $formquestion = array(
288 array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
289 array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
290
291 $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');
292 }
293 }
294
295 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
301 private function assign_post()
302 {
303 // phpcs:enable
304 global $langs, $mysoc;
305
306 $this->object->socid = GETPOSTINT("socid");
307 $this->object->lastname = GETPOST("name");
308 $this->object->firstname = GETPOST("firstname");
309 $this->object->civility_id = GETPOST("civility_id");
310 $this->object->poste = GETPOST("poste");
311 $this->object->address = GETPOST("address");
312 $this->object->zip = GETPOST("zipcode");
313 $this->object->town = GETPOST("town");
314 $this->object->country_id = GETPOST("country_id") ? GETPOST("country_id") : $mysoc->country_id;
315 $this->object->state_id = GETPOSTINT("state_id");
316 $this->object->phone_pro = GETPOST("phone_pro");
317 $this->object->phone_perso = GETPOST("phone_perso");
318 $this->object->phone_mobile = GETPOST("phone_mobile");
319 $this->object->fax = GETPOST("fax");
320 $this->object->email = GETPOST("email");
321 $this->object->priv = GETPOSTINT("priv");
322 $this->object->note = GETPOST("note", "restricthtml");
323 $this->object->canvas = GETPOST("canvas");
324
325 // We set country_id, and country_code label of the chosen country
326 if ($this->object->country_id) {
327 $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".((int) $this->object->country_id);
328 $resql = $this->db->query($sql);
329 $obj = null;
330 if ($resql) {
331 $obj = $this->db->fetch_object($resql);
332 } else {
333 dol_print_error($this->db);
334 }
335 if ($obj !== null) {
336 $this->object->country_id = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
337 $this->object->country_code = $obj->code;
338 $this->object->country = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
339 }
340 }
341 }
342}
$id
Definition account.php:39
Common Abstract Class for contact managmeent.
assign_post()
Assign POST values into object.
assign_values(&$action, $id)
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.
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.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
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, $cid=0, $socid=0, $addlink=0, $max=64, $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.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
Show information in HTML for admin users or standard users.
getRandomPassword($generic=false, $replaceambiguouschars=null, $length=32)
Return a generated password using default module.