dolibarr  7.0.0-beta
actions_adherentcard_common.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@capnetworks.com>
3  * Copyright (C) 2012 Philippe Grand <philippe.grand@atoo-net.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 <http://www.gnu.org/licenses/>.
17  */
18 
29 {
34  var $db;
35  var $dirmodule;
36  var $targetmodule;
37  var $canvas;
38  var $card;
39 
41  var $tpl = array();
43  var $object;
45  var $error;
47  var $errors=array();
48 
49 
56  private function getInstanceDao()
57  {
58  dol_syslog(__METHOD__ . " is deprecated", LOG_WARNING);
59 
60  if (! is_object($this->object))
61  {
62  $modelclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/dao_'.$this->targetmodule.'_'.$this->canvas.'.class.php');
63  if (file_exists($modelclassfile))
64  {
65  // Include dataservice class (model)
66  $ret = require_once $modelclassfile;
67  if ($ret)
68  {
69  // Instantiate dataservice class (model)
70  $modelclassname = 'Dao'.ucfirst($this->targetmodule).ucfirst($this->canvas);
71  $this->object = new $modelclassname($this->db);
72  }
73  }
74  }
75  return 0;
76  }
77 
84  function getObject($id)
85  {
86  //$ret = $this->getInstanceDao();
87 
88  /*if (is_object($this->object) && method_exists($this->object,'fetch'))
89  {
90  if (! empty($id)) $this->object->fetch($id);
91  }
92  else
93  {*/
94  $object = new Adherent($this->db);
95  if (! empty($id)) $object->fetch($id);
96  $this->object = $object;
97  //}
98  }
99 
108  function doActions(&$action, $id)
109  {
110  global $conf, $user, $langs;
111 
112  // Creation utilisateur depuis Adherent
113  if ($action == 'confirm_create_user' && GETPOST("confirm") == 'yes')
114  {
115  // Recuperation adherent actuel
116  $result = $this->object->fetch($id);
117 
118  if ($result > 0)
119  {
120  $this->db->begin();
121 
122  // Creation user
123  $nuser = new User($this->db);
124  $result=$nuser->create_from_member($this->object,GETPOST("login"));
125 
126  if ($result > 0)
127  {
128  $result2=$nuser->setPassword($user,GETPOST("password"),0,1,1);
129  if ($result2)
130  {
131  $this->db->commit();
132  }
133  else
134  {
135  $this->db->rollback();
136  }
137  }
138  else
139  {
140  $this->errors[]=$nuser->error;
141 
142  $this->db->rollback();
143  }
144  }
145  else
146  {
147  $this->errors=$this->object->errors;
148  }
149  }
150 
151  // Creation adherent
152  if ($action == 'add')
153  {
154  $this->assign_post();
155 
156  if (! $_POST["name"])
157  {
158  array_push($this->errors,$langs->trans("ErrorFieldRequired",$langs->transnoentities("Lastname").' / '.$langs->transnoentities("Label")));
159  $action = 'create';
160  }
161 
162  if ($_POST["name"])
163  {
164  $id = $this->object->create($user);
165  if ($id > 0)
166  {
167  header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
168  exit;
169  }
170  else
171  {
172  $this->errors=$this->object->errors;
173  $action = 'create';
174  }
175  }
176  }
177 
178  if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes')
179  {
180  $result=$this->object->fetch($id);
181 
182  $this->object->old_name = $_POST["old_name"];
183  $this->object->old_firstname = $_POST["old_firstname"];
184 
185  $result = $this->object->delete(0, $user, 0);
186  if ($result > 0)
187  {
188  header("Location: list.php");
189  exit;
190  }
191  else
192  {
193  $this->errors=$this->object->errors;
194  }
195  }
196 
197  if ($action == 'update')
198  {
199  if ($_POST["cancel"])
200  {
201  header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
202  exit;
203  }
204 
205  if (empty($_POST["name"]))
206  {
207  $this->error=array($langs->trans("ErrorFieldRequired",$langs->transnoentities("Name").' / '.$langs->transnoentities("Label")));
208  $action = 'edit';
209  }
210 
211  if (empty($this->error))
212  {
213  $this->object->fetch($_POST["adherentid"]);
214 
215  $this->object->oldcopy = clone $this->object;
216 
217  $this->assign_post();
218 
219  $result = $this->object->update($_POST["adherentid"], $user);
220 
221  if ($result > 0)
222  {
223  header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
224  exit;
225  }
226  else
227  {
228  $this->errors=$this->object->errors;
229  $action = 'edit';
230  }
231  }
232  }
233  }
234 
242  function assign_values(&$action, $id)
243  {
244  global $conf, $langs, $user, $canvas;
245  global $form, $formcompany, $objsoc;
246 
247  if ($action == 'add' || $action == 'update') $this->assign_post();
248 
249  foreach($this->object as $key => $value)
250  {
251  $this->tpl[$key] = $value;
252  }
253 
254  $this->tpl['error']=$this->error;
255  $this->tpl['errors']=$this->errors;
256 
257  if ($action == 'create' || $action == 'edit')
258  {
259  if ($conf->use_javascript_ajax)
260  {
261  $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
262  jQuery(document).ready(function () {
263  jQuery("#selectcountry_id").change(function() {
264  document.formsoc.action.value="'.$action.'";
265  document.formsoc.canvas.value="'.$canvas.'";
266  document.formsoc.submit();
267  });
268  })
269  </script>'."\n";
270  }
271 
272  if (is_object($objsoc) && $objsoc->id > 0)
273  {
274  $this->tpl['company'] = $objsoc->getNomUrl(1);
275  $this->tpl['company_id'] = $objsoc->id;
276  }
277  else
278  {
279  $this->tpl['company'] = $form->select_company($this->object->socid,'socid','',1);
280  }
281 
282  // Civility
283  $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
284 
285  // Predefined with third party
286  if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE'))
287  {
288  if (dol_strlen(trim($this->object->address)) == 0) $this->tpl['address'] = $objsoc->address;
289  if (dol_strlen(trim($this->object->zip)) == 0) $this->object->zip = $objsoc->zip;
290  if (dol_strlen(trim($this->object->town)) == 0) $this->object->town = $objsoc->town;
291  if (dol_strlen(trim($this->object->phone_perso)) == 0) $this->object->phone_perso = $objsoc->phone;
292  if (dol_strlen(trim($this->object->phone_mobile)) == 0) $this->object->phone_mobile = $objsoc->phone_mobile;
293  if (dol_strlen(trim($this->object->email)) == 0) $this->object->email = $objsoc->email;
294  }
295 
296  // Zip
297  $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip,'zipcode',array('town','selectcountry_id','state_id'),6);
298 
299  // Town
300  $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town,'town',array('zipcode','selectcountry_id','state_id'));
301 
302  if (dol_strlen(trim($this->object->country_id)) == 0) $this->object->country_id = $objsoc->country_id;
303 
304  // Country
305  $this->tpl['select_country'] = $form->select_country($this->object->country_id,'country_id');
306  $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
307 
308  if ($user->admin) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
309 
310  // State
311  if ($this->object->country_id) $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id,$this->object->country_code);
312  else $this->tpl['select_state'] = $countrynotdefined;
313 
314  // Physical or Moral
315  $selectarray=array('0'=>$langs->trans("Physical"),'1'=>$langs->trans("Moral"));
316  $this->tpl['select_morphy'] = $form->selectarray('morphy',$selectarray,$this->object->morphy,0);
317  }
318 
319  if ($action == 'view' || $action == 'edit' || $action == 'delete')
320  {
321  // Emailing
322  if (! empty($conf->mailing->enabled))
323  {
324  $langs->load("mails");
325  $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
326  }
327 
328 
329  // Dolibarr user
330  if ($this->object->user_id)
331  {
332  $dolibarr_user=new User($this->db);
333  $result=$dolibarr_user->fetch($this->object->user_id);
334  $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
335  }
336  else $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
337  }
338 
339  if ($action == 'view' || $action == 'delete')
340  {
341  $this->tpl['showrefnav'] = $form->showrefnav($this->object,'id');
342 
343  if ($this->object->socid > 0)
344  {
345  $objsoc = new Societe($this->db);
346 
347  $objsoc->fetch($this->object->socid);
348  $this->tpl['company'] = $objsoc->getNomUrl(1);
349  }
350  else
351  {
352  $this->tpl['company'] = $langs->trans("AdherentNotLinkedToThirdParty");
353  }
354 
355  $this->tpl['civility'] = $this->object->getCivilityLabel();
356 
357  $this->tpl['address'] = dol_nl2br($this->object->address);
358 
359  $this->tpl['zip'] = ($this->object->zip?$this->object->zip.'&nbsp;':'');
360 
361  $img=picto_from_langcode($this->object->country_code);
362  $this->tpl['country'] = ($img?$img.' ':'').$this->object->country;
363 
364  $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso,$this->object->country_code,0,$this->object->id,'AC_TEL');
365  $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile,$this->object->country_code,0,$this->object->id,'AC_TEL');
366  $this->tpl['email'] = dol_print_email($this->object->email,0,$this->object->id,'AC_EMAIL');
367 
368  $this->tpl['visibility'] = $this->object->getmorphylib($this->object->morphy);
369 
370  $this->tpl['note'] = nl2br($this->object->note);
371  }
372 
373  if ($action == 'create_user')
374  {
375  // Full firstname and lastname separated with a dot : firstname.lastname
376  include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
377  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
378  $login=dol_buildlogin($this->object->lastname, $this->object->firstname);
379 
380  $generated_password=getRandomPassword(false);
381  $password=$generated_password;
382 
383  // Create a form array
384  $formquestion=array(
385  array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
386  array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
387 
388  $this->tpl['action_create_user'] = $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$this->object->id,$langs->trans("CreateDolibarrLogin"),$langs->trans("ConfirmCreateAdherent"),"confirm_create_user",$formquestion,'no');
389  }
390  }
391 
397  private function assign_post()
398  {
399  global $langs, $mysoc;
400 
401  $this->object->old_name = $_POST["old_name"];
402  $this->object->old_firstname = $_POST["old_firstname"];
403 
404  $this->object->fk_soc = $_POST["fk_soc"];
405  $this->object->lastname = $_POST["lastname"];
406  $this->object->firstname = $_POST["firstname"];
407  $this->object->civility_id = $_POST["civility_id"];
408  $this->object->address = $_POST["address"];
409  $this->object->zip = $_POST["zipcode"];
410  $this->object->town = $_POST["town"];
411  $this->object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
412  $this->object->state_id = $_POST["state_id"];
413  $this->object->phone_perso = $_POST["phone_perso"];
414  $this->object->phone_mobile = $_POST["phone_mobile"];
415  $this->object->email = $_POST["email"];
416  $this->object->note = $_POST["note"];
417  $this->object->canvas = $_POST["canvas"];
418 
419  // We set country_id, and country_code label of the chosen country
420  if ($this->object->country_id)
421  {
422  $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".$this->object->country_id;
423  $resql=$this->db->query($sql);
424  if ($resql)
425  {
426  $obj = $this->db->fetch_object($resql);
427 
428  $this->object->country_code = $obj->code;
429  $this->object->country = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
430  }
431  else
432  {
433  dol_print_error($this->db);
434  }
435  }
436  }
437 
438 }
Class to manage Dolibarr users.
Definition: user.class.php:39
getInstanceDao()
Instantiation of DAO class.
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...
assign_post()
Assign POST values into object.
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.
assign_values(&$action, $id)
Set content of ->tpl array, to use into template.
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...)
doActions(&$action, $id)
doActions of a canvas is not the doActions of the hook
Class to manage members of a foundation.
Class to maage members using default canvas.
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
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0)
Format phone numbers according to country.
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.