dolibarr  20.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 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 
36  public $dirmodule;
37  public $targetmodule;
38  public $canvas;
39  public $card;
40 
42  public $tpl = array();
44  public $object;
45 
49  public $error = '';
50 
51 
55  public $errors = array();
56 
57 
64  public function getObject($id)
65  {
66  /*$ret = $this->getInstanceDao();
67 
68  if (is_object($this->object) && method_exists($this->object,'fetch'))
69  {
70  if (!empty($id)) $this->object->fetch($id);
71  }
72  else
73  {*/
74  $object = new Contact($this->db);
75  if (!empty($id)) {
76  $object->fetch($id);
77  }
78 
79  $this->object = $object;
80 
81  return $object;
82  }
83 
84  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
92  public function assign_values(&$action, $id)
93  {
94  // phpcs:enable
95  global $conf, $langs, $user, $canvas;
96  global $form, $formcompany, $objsoc;
97 
98  if ($action == 'add' || $action == 'update') {
99  $this->assign_post();
100  }
101 
102  foreach ($this->object as $key => $value) {
103  $this->tpl[$key] = $value;
104  }
105 
106  $this->tpl['error'] = $this->error;
107  $this->tpl['errors'] = $this->errors;
108 
109  if ($action == 'create' || $action == 'edit') {
110  if ($conf->use_javascript_ajax) {
111  $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript">
112  jQuery(document).ready(function () {
113  jQuery("#selectcountry_id").change(function() {
114  document.formsoc.action.value="'.$action.'";
115  document.formsoc.canvas.value="'.$canvas.'";
116  document.formsoc.submit();
117  });
118  })
119  </script>'."\n";
120  }
121 
122  if (is_object($objsoc) && $objsoc->id > 0) {
123  $this->tpl['company'] = $objsoc->getNomUrl(1);
124  $this->tpl['company_id'] = $objsoc->id;
125  } else {
126  $this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
127  }
128 
129  // Civility
130  $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
131 
132  // Predefined with third party
133  if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || getDolGlobalString('CONTACT_USE_COMPANY_ADDRESS')) {
134  if (dol_strlen(trim($this->object->address)) == 0) {
135  $this->tpl['address'] = $objsoc->address;
136  }
137  if (dol_strlen(trim($this->object->zip)) == 0) {
138  $this->object->zip = $objsoc->zip;
139  }
140  if (dol_strlen(trim($this->object->town)) == 0) {
141  $this->object->town = $objsoc->town;
142  }
143  if (dol_strlen(trim($this->object->phone_pro)) == 0) {
144  $this->object->phone_pro = $objsoc->phone;
145  }
146  if (dol_strlen(trim($this->object->fax)) == 0) {
147  $this->object->fax = $objsoc->fax;
148  }
149  if (dol_strlen(trim($this->object->email)) == 0) {
150  $this->object->email = $objsoc->email;
151  }
152  }
153 
154  // Zip
155  $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6);
156 
157  // Town
158  $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
159 
160  if (dol_strlen(trim((string) $this->object->country_id)) == 0) {
161  $this->object->country_id = $objsoc->country_id;
162  }
163 
164  // Country
165  $this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
166  $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
167 
168  if ($user->admin) {
169  $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
170  }
171 
172  // State
173  if ($this->object->country_id) {
174  $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
175  } else {
176  $this->tpl['select_state'] = $countrynotdefined;
177  }
178 
179  // Public or private
180  $selectarray = array('0' => $langs->trans("ContactPublic"), '1' => $langs->trans("ContactPrivate"));
181  $this->tpl['select_visibility'] = $form->selectarray('priv', $selectarray, $this->object->priv, 0);
182  }
183 
184  if ($action == 'view' || $action == 'edit' || $action == 'delete') {
185  // Emailing
186  if (isModEnabled('mailing')) {
187  $langs->load("mails");
188  $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
189  }
190 
191  // Linked element
192  $this->tpl['contact_element'] = array();
193  $i = 0;
194 
195  $this->object->load_ref_elements();
196 
197  if (isModEnabled('order')) {
198  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
199  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande ? $this->object->ref_commande : $langs->trans("NoContactForAnyOrder");
200  $i++;
201  }
202  if (isModEnabled("propal")) {
203  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
204  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal ? $this->object->ref_propal : $langs->trans("NoContactForAnyProposal");
205  $i++;
206  }
207  if (isModEnabled('contract')) {
208  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
209  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat ? $this->object->ref_contrat : $langs->trans("NoContactForAnyContract");
210  $i++;
211  }
212  if (isModEnabled('invoice')) {
213  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
214  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation ? $this->object->ref_facturation : $langs->trans("NoContactForAnyInvoice");
215  $i++;
216  }
217 
218  // Dolibarr user
219  if ($this->object->user_id) {
220  $dolibarr_user = new User($this->db);
221  $result = $dolibarr_user->fetch($this->object->user_id);
222  $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
223  } else {
224  $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
225  }
226  }
227 
228  if ($action == 'view' || $action == 'delete') {
229  $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
230 
231  if ($this->object->socid > 0) {
232  $objsoc = new Societe($this->db);
233 
234  $objsoc->fetch($this->object->socid);
235  $this->tpl['company'] = $objsoc->getNomUrl(1);
236  } else {
237  $this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
238  }
239 
240  $this->tpl['civility'] = $this->object->getCivilityLabel();
241 
242  $this->tpl['address'] = dol_nl2br($this->object->address);
243 
244  $this->tpl['zip'] = ($this->object->zip ? $this->object->zip.'&nbsp;' : '');
245 
246  $img = picto_from_langcode($this->object->country_code);
247  $this->tpl['country'] = ($img ? $img.' ' : '').$this->object->country;
248 
249  $this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
250  $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
251  $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
252  $this->tpl['fax'] = dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
253  $this->tpl['email'] = dol_print_email($this->object->email, 0, $this->object->id, 1);
254 
255  $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
256 
257  $this->tpl['note'] = $this->object->note_private;
258  }
259 
260  if ($action == 'create_user') {
261  // Full firstname and lastname separated with a dot : firstname.lastname
262  include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
263  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
264  $login = dol_buildlogin($this->object->lastname, $this->object->firstname);
265 
266  $generated_password = getRandomPassword(false);
267  $password = $generated_password;
268 
269  // Create a form array
270  $formquestion = array(
271  array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
272  array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
273 
274  $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');
275  }
276  }
277 
278  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
284  private function assign_post()
285  {
286  // phpcs:enable
287  global $langs, $mysoc;
288 
289  $this->object->socid = GETPOSTINT("socid");
290  $this->object->lastname = GETPOST("name");
291  $this->object->firstname = GETPOST("firstname");
292  $this->object->civility_id = GETPOST("civility_id");
293  $this->object->poste = GETPOST("poste");
294  $this->object->address = GETPOST("address");
295  $this->object->zip = GETPOST("zipcode");
296  $this->object->town = GETPOST("town");
297  $this->object->country_id = GETPOST("country_id") ? GETPOST("country_id") : $mysoc->country_id;
298  $this->object->state_id = GETPOST("state_id");
299  $this->object->phone_pro = GETPOST("phone_pro");
300  $this->object->phone_perso = GETPOST("phone_perso");
301  $this->object->phone_mobile = GETPOST("phone_mobile");
302  $this->object->fax = GETPOST("fax");
303  $this->object->email = GETPOST("email");
304  $this->object->priv = GETPOST("priv");
305  $this->object->note = GETPOST("note", "restricthtml");
306  $this->object->canvas = GETPOST("canvas");
307 
308  // We set country_id, and country_code label of the chosen country
309  if ($this->object->country_id) {
310  $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".((int) $this->object->country_id);
311  $resql = $this->db->query($sql);
312  if ($resql) {
313  $obj = $this->db->fetch_object($resql);
314  } else {
315  dol_print_error($this->db);
316  }
317  $this->object->country_id = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
318  $this->object->country_code = $obj->code;
319  $this->object->country = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
320  }
321  }
322 }
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.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
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_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)
Show EMail link formatted for HTML output.
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0)
Format phone numbers according to country.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information in HTML for admin users or standard users.
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 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.