dolibarr  7.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@capnetworks.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
29 {
30  var $db;
31  var $dirmodule;
32  var $targetmodule;
33  var $canvas;
34  var $card;
35 
37  var $tpl = array();
39  var $object;
41  var $error;
43  var $errors=array();
44 
45 
52  private function getInstanceDao()
53  {
54  dol_syslog(__METHOD__ . " is deprecated", LOG_WARNING);
55 
56  if (! is_object($this->object))
57  {
58  $modelclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/dao_'.$this->targetmodule.'_'.$this->canvas.'.class.php');
59  if (file_exists($modelclassfile))
60  {
61  // Include dataservice class (model)
62  $ret = require_once $modelclassfile;
63  if ($ret)
64  {
65  // Instantiate dataservice class (model)
66  $modelclassname = 'Dao'.ucfirst($this->targetmodule).ucfirst($this->canvas);
67  $this->object = new $modelclassname($this->db);
68  }
69  }
70  }
71  return 0;
72  }
73 
80  function getObject($id)
81  {
82  /*$ret = $this->getInstanceDao();
83 
84  if (is_object($this->object) && method_exists($this->object,'fetch'))
85  {
86  if (! empty($id)) $this->object->fetch($id);
87  }
88  else
89  {*/
90  $object = new Contact($this->db);
91  if (! empty($id)) $object->fetch($id);
92  $this->object = $object;
93  //}
94  }
95 
104  function doActions(&$action, $id)
105  {
106  global $conf, $user, $langs;
107 
108  // Creation utilisateur depuis contact
109  if ($action == 'confirm_create_user' && GETPOST("confirm") == 'yes')
110  {
111  // Recuperation contact actuel
112  $result = $this->object->fetch($id);
113 
114  if ($result > 0)
115  {
116  $this->db->begin();
117 
118  // Creation user
119  $nuser = new User($this->db);
120  $result=$nuser->create_from_contact($this->object,$_POST["login"]);
121 
122  if ($result > 0)
123  {
124  $result2=$nuser->setPassword($user,$_POST["password"],0,1,1);
125  if ($result2)
126  {
127  $this->db->commit();
128  }
129  else
130  {
131  $this->db->rollback();
132  }
133  }
134  else
135  {
136  $this->errors=$nuser->error;
137 
138  $this->db->rollback();
139  }
140  }
141  else
142  {
143  $this->errors=$this->object->errors;
144  }
145  }
146 
147  // Creation contact
148  if ($action == 'add')
149  {
150  $this->assign_post();
151 
152  if (! $_POST["name"])
153  {
154  array_push($this->errors,$langs->trans("ErrorFieldRequired",$langs->transnoentities("Lastname").' / '.$langs->transnoentities("Label")));
155  $action = 'create';
156  }
157 
158  if ($_POST["name"])
159  {
160  $id = $this->object->create($user);
161  if ($id > 0)
162  {
163  header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
164  exit;
165  }
166  else
167  {
168  $this->errors=$this->object->errors;
169  $action = 'create';
170  }
171  }
172  }
173 
174  if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes')
175  {
176  $result=$this->object->fetch($id);
177 
178  $this->object->old_name = $_POST["old_name"];
179  $this->object->old_firstname = $_POST["old_firstname"];
180 
181  $result = $this->object->delete();
182  if ($result > 0)
183  {
184  header("Location: list.php");
185  exit;
186  }
187  else
188  {
189  $this->errors=$this->object->errors;
190  }
191  }
192 
193  if ($action == 'update')
194  {
195  if ($_POST["cancel"])
196  {
197  header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
198  exit;
199  }
200 
201  if (empty($_POST["name"]))
202  {
203  $this->error=array($langs->trans("ErrorFieldRequired",$langs->transnoentities("Name").' / '.$langs->transnoentities("Label")));
204  $action = 'edit';
205  }
206 
207  if (empty($this->error))
208  {
209  $this->object->fetch($_POST["contactid"]);
210 
211  $this->object->oldcopy = clone $this->object;
212 
213  $this->assign_post();
214 
215  $result = $this->object->update($_POST["contactid"], $user);
216 
217  if ($result > 0)
218  {
219  header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
220  exit;
221  }
222  else
223  {
224  $this->errors=$this->object->errors;
225  $action = 'edit';
226  }
227  }
228  }
229  }
230 
238  function assign_values(&$action, $id)
239  {
240  global $conf, $langs, $user, $canvas;
241  global $form, $formcompany, $objsoc;
242 
243  if ($action == 'add' || $action == 'update') $this->assign_post();
244 
245  foreach($this->object as $key => $value)
246  {
247  $this->tpl[$key] = $value;
248  }
249 
250  $this->tpl['error']=$this->error;
251  $this->tpl['errors']=$this->errors;
252 
253  if ($action == 'create' || $action == 'edit')
254  {
255  if ($conf->use_javascript_ajax)
256  {
257  $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
258  jQuery(document).ready(function () {
259  jQuery("#selectcountry_id").change(function() {
260  document.formsoc.action.value="'.$action.'";
261  document.formsoc.canvas.value="'.$canvas.'";
262  document.formsoc.submit();
263  });
264  })
265  </script>'."\n";
266  }
267 
268  if (is_object($objsoc) && $objsoc->id > 0)
269  {
270  $this->tpl['company'] = $objsoc->getNomUrl(1);
271  $this->tpl['company_id'] = $objsoc->id;
272  }
273  else
274  {
275  $this->tpl['company'] = $form->select_company($this->object->socid,'socid','',1);
276  }
277 
278  // Civility
279  $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
280 
281  // Predefined with third party
282  if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || ! empty($conf->global->CONTACT_USE_COMPANY_ADDRESS))
283  {
284  if (dol_strlen(trim($this->object->address)) == 0) $this->tpl['address'] = $objsoc->address;
285  if (dol_strlen(trim($this->object->zip)) == 0) $this->object->zip = $objsoc->zip;
286  if (dol_strlen(trim($this->object->town)) == 0) $this->object->town = $objsoc->town;
287  if (dol_strlen(trim($this->object->phone_pro)) == 0) $this->object->phone_pro = $objsoc->phone;
288  if (dol_strlen(trim($this->object->fax)) == 0) $this->object->fax = $objsoc->fax;
289  if (dol_strlen(trim($this->object->email)) == 0) $this->object->email = $objsoc->email;
290  }
291 
292  // Zip
293  $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip,'zipcode',array('town','selectcountry_id','state_id'),6);
294 
295  // Town
296  $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town,'town',array('zipcode','selectcountry_id','state_id'));
297 
298  if (dol_strlen(trim($this->object->country_id)) == 0) $this->object->country_id = $objsoc->country_id;
299 
300  // Country
301  $this->tpl['select_country'] = $form->select_country($this->object->country_id,'country_id');
302  $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
303 
304  if ($user->admin) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
305 
306  // State
307  if ($this->object->country_id) $this->tpl['select_state'] = $formcompany->select_state($this->object->fk_departement,$this->object->country_code);
308  else $this->tpl['select_state'] = $countrynotdefined;
309 
310  // Public or private
311  $selectarray=array('0'=>$langs->trans("ContactPublic"),'1'=>$langs->trans("ContactPrivate"));
312  $this->tpl['select_visibility'] = $form->selectarray('priv',$selectarray,$this->object->priv,0);
313  }
314 
315  if ($action == 'view' || $action == 'edit' || $action == 'delete')
316  {
317  // Emailing
318  if (! empty($conf->mailing->enabled))
319  {
320  $langs->load("mails");
321  $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
322  }
323 
324  // Linked element
325  $this->tpl['contact_element'] = array();
326  $i=0;
327 
328  $this->object->load_ref_elements();
329 
330  if (! empty($conf->commande->enabled))
331  {
332  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
333  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande?$this->object->ref_commande:$langs->trans("NoContactForAnyOrder");
334  $i++;
335  }
336  if (! empty($conf->propal->enabled))
337  {
338  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
339  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal?$this->object->ref_propal:$langs->trans("NoContactForAnyProposal");
340  $i++;
341  }
342  if (! empty($conf->contrat->enabled))
343  {
344  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
345  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat?$this->object->ref_contrat:$langs->trans("NoContactForAnyContract");
346  $i++;
347  }
348  if (! empty($conf->facture->enabled))
349  {
350  $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
351  $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation?$this->object->ref_facturation:$langs->trans("NoContactForAnyInvoice");
352  $i++;
353  }
354 
355  // Dolibarr user
356  if ($this->object->user_id)
357  {
358  $dolibarr_user=new User($this->db);
359  $result=$dolibarr_user->fetch($this->object->user_id);
360  $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
361  }
362  else $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
363  }
364 
365  if ($action == 'view' || $action == 'delete')
366  {
367  $this->tpl['showrefnav'] = $form->showrefnav($this->object,'id');
368 
369  if ($this->object->socid > 0)
370  {
371  $objsoc = new Societe($this->db);
372 
373  $objsoc->fetch($this->object->socid);
374  $this->tpl['company'] = $objsoc->getNomUrl(1);
375  }
376  else
377  {
378  $this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
379  }
380 
381  $this->tpl['civility'] = $this->object->getCivilityLabel();
382 
383  $this->tpl['address'] = dol_nl2br($this->object->address);
384 
385  $this->tpl['zip'] = ($this->object->zip?$this->object->zip.'&nbsp;':'');
386 
387  $img=picto_from_langcode($this->object->country_code);
388  $this->tpl['country'] = ($img?$img.' ':'').$this->object->country;
389 
390  $this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro,$this->object->country_code,0,$this->object->id,'AC_TEL');
391  $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso,$this->object->country_code,0,$this->object->id,'AC_TEL');
392  $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile,$this->object->country_code,0,$this->object->id,'AC_TEL');
393  $this->tpl['fax'] = dol_print_phone($this->object->fax,$this->object->country_code,0,$this->object->id,'AC_FAX');
394  $this->tpl['email'] = dol_print_email($this->object->email,0,$this->object->id,'AC_EMAIL');
395 
396  $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
397 
398  $this->tpl['note'] = nl2br($this->object->note);
399  }
400 
401  if ($action == 'create_user')
402  {
403  // Full firstname and lastname separated with a dot : firstname.lastname
404  include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
405  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
406  $login=dol_buildlogin($this->object->lastname, $this->object->firstname);
407 
408  $generated_password=getRandomPassword(false);
409  $password=$generated_password;
410 
411  // Create a form array
412  $formquestion=array(
413  array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
414  array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
415 
416  $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');
417  }
418  }
419 
425  private function assign_post()
426  {
427  global $langs, $mysoc;
428 
429  $this->object->old_name = $_POST["old_name"];
430  $this->object->old_firstname = $_POST["old_firstname"];
431 
432  $this->object->socid = $_POST["socid"];
433  $this->object->lastname = $_POST["name"];
434  $this->object->firstname = $_POST["firstname"];
435  $this->object->civility_id = $_POST["civility_id"];
436  $this->object->poste = $_POST["poste"];
437  $this->object->address = $_POST["address"];
438  $this->object->zip = $_POST["zipcode"];
439  $this->object->town = $_POST["town"];
440  $this->object->fk_departement = $_POST["state_id"];
441  $this->object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
442  $this->object->state_id = $_POST["state_id"];
443  $this->object->phone_pro = $_POST["phone_pro"];
444  $this->object->phone_perso = $_POST["phone_perso"];
445  $this->object->phone_mobile = $_POST["phone_mobile"];
446  $this->object->fax = $_POST["fax"];
447  $this->object->email = $_POST["email"];
448  $this->object->jabberid = $_POST["jabberid"];
449  $this->object->priv = $_POST["priv"];
450  $this->object->note = $_POST["note"];
451  $this->object->canvas = $_POST["canvas"];
452 
453  // We set country_id, and country_code label of the chosen country
454  if ($this->object->country_id)
455  {
456  $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".$this->object->country_id;
457  $resql=$this->db->query($sql);
458  if ($resql)
459  {
460  $obj = $this->db->fetch_object($resql);
461  }
462  else
463  {
464  dol_print_error($this->db);
465  }
466  $this->object->country_id = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->label;
467  $this->object->country_code = $obj->code;
468  $this->object->country = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->label;
469  }
470  }
471 
472 }
473 
doActions(&$action, $id)
doActions of a canvas is not the doActions of the hook
Class to manage contact/addresses.
Class to manage Dolibarr users.
Definition: user.class.php:39
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_buildlogin($lastname, $firstname)
Build a login from lastname, firstname.
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
GETPOST($paramname, $check='none', $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')
Show information for admin users or standard users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
assign_values(&$action, $id)
Set content of ->tpl array, to use into template.
Classe permettant la gestion des contacts par defaut.
getRandomPassword($generic=false)
Return a generated password using default module.
dol_print_email($email, $cid=0, $socid=0, $addlink=0, $max=64, $showinvalid=1, $withpicto=0)
Show EMail link.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
assign_post()
Assign POST values into object.
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0)
Format phone numbers according to country.
getInstanceDao()
Instantiation of DAO class.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
picto_from_langcode($codelang, $moreatt= '')
Return img flag of country for a language code or country code.