PageRenderTime 79ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/adherent/canvas/actions_adherentcard_common.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 435 lines | 294 code | 64 blank | 77 comment | 79 complexity | db2f647e7679894fa9803dbee221c5f0 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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. /**
  19. * \file htdocs/contact/canvas/actions_adherentcard_common.class.php
  20. * \ingroup thirdparty
  21. * \brief Fichier de la classe Adherent card controller (common)
  22. */
  23. /**
  24. * \class ActionsAdherentCardCommon
  25. * \brief Classe permettant la gestion des adherents par defaut
  26. */
  27. abstract class ActionsAdherentCardCommon
  28. {
  29. var $db;
  30. var $dirmodule;
  31. var $targetmodule;
  32. var $canvas;
  33. var $card;
  34. //! Template container
  35. var $tpl = array();
  36. //! Object container
  37. var $object;
  38. //! Error string
  39. var $error;
  40. //! Error array
  41. var $errors=array();
  42. /**
  43. * Instantiation of DAO class
  44. *
  45. * @return void
  46. */
  47. private function getInstanceDao()
  48. {
  49. if (! is_object($this->object))
  50. {
  51. $modelclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/dao_'.$this->targetmodule.'_'.$this->canvas.'.class.php');
  52. if (file_exists($modelclassfile))
  53. {
  54. // Include dataservice class (model)
  55. $ret = require_once $modelclassfile;
  56. if ($ret)
  57. {
  58. // Instantiate dataservice class (model)
  59. $modelclassname = 'Dao'.ucfirst($this->targetmodule).ucfirst($this->canvas);
  60. $this->object = new $modelclassname($this->db);
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Get object
  67. *
  68. * @param int $id Object id
  69. * @return object Object loaded
  70. */
  71. function getObject($id)
  72. {
  73. $ret = $this->getInstanceDao();
  74. if (is_object($this->object) && method_exists($this->object,'fetch'))
  75. {
  76. if (! empty($id)) $this->object->fetch($id);
  77. }
  78. else
  79. {
  80. $object = new Adherent($this->db);
  81. if (! empty($id)) $object->fetch($id);
  82. $this->object = $object;
  83. }
  84. }
  85. /**
  86. * Load data control
  87. *
  88. * @param string &$action Type of action
  89. * @param int $id Id of object
  90. * @return void
  91. */
  92. function doActions(&$action, $id)
  93. {
  94. global $conf, $user, $langs;
  95. // Creation utilisateur depuis Adherent
  96. if ($action == 'confirm_create_user' && GETPOST("confirm") == 'yes')
  97. {
  98. // Recuperation adherent actuel
  99. $result = $this->object->fetch($id);
  100. if ($result > 0)
  101. {
  102. $this->db->begin();
  103. // Creation user
  104. $nuser = new User($this->db);
  105. $result=$nuser->create_from_member($this->object,$_POST["login"]);
  106. if ($result > 0)
  107. {
  108. $result2=$nuser->setPassword($user,$_POST["password"],0,1,1);
  109. if ($result2)
  110. {
  111. $this->db->commit();
  112. }
  113. else
  114. {
  115. $this->db->rollback();
  116. }
  117. }
  118. else
  119. {
  120. $this->errors=$nuser->error;
  121. $this->db->rollback();
  122. }
  123. }
  124. else
  125. {
  126. $this->errors=$this->object->errors;
  127. }
  128. }
  129. // Creation adherent
  130. if ($action == 'add')
  131. {
  132. $this->assign_post();
  133. if (! $_POST["name"])
  134. {
  135. array_push($this->errors,$langs->trans("ErrorFieldRequired",$langs->transnoentities("Lastname").' / '.$langs->transnoentities("Label")));
  136. $action = 'create';
  137. }
  138. if ($_POST["name"])
  139. {
  140. $id = $this->object->create($user);
  141. if ($id > 0)
  142. {
  143. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  144. exit;
  145. }
  146. else
  147. {
  148. $this->errors=$this->object->errors;
  149. $action = 'create';
  150. }
  151. }
  152. }
  153. if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes')
  154. {
  155. $result=$this->object->fetch($id);
  156. $this->object->old_name = $_POST["old_name"];
  157. $this->object->old_firstname = $_POST["old_firstname"];
  158. $result = $this->object->delete();
  159. if ($result > 0)
  160. {
  161. header("Location: list.php");
  162. exit;
  163. }
  164. else
  165. {
  166. $this->errors=$this->object->errors;
  167. }
  168. }
  169. if ($action == 'update')
  170. {
  171. if ($_POST["cancel"])
  172. {
  173. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
  174. exit;
  175. }
  176. if (empty($_POST["name"]))
  177. {
  178. $this->error=array($langs->trans("ErrorFieldRequired",$langs->transnoentities("Name").' / '.$langs->transnoentities("Label")));
  179. $action = 'edit';
  180. }
  181. if (empty($this->error))
  182. {
  183. $this->object->fetch($_POST["adherentid"]);
  184. $this->object->oldcopy=dol_clone($this->object);
  185. $this->assign_post();
  186. $result = $this->object->update($_POST["adherentid"], $user);
  187. if ($result > 0)
  188. {
  189. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
  190. exit;
  191. }
  192. else
  193. {
  194. $this->errors=$this->object->errors;
  195. $action = 'edit';
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * Set content of ->tpl array, to use into template
  202. *
  203. * @param string &$action Type of action
  204. * @param int $id Id
  205. * @return string HTML output
  206. */
  207. function assign_values(&$action, $id)
  208. {
  209. global $conf, $langs, $user, $canvas;
  210. global $form, $formcompany, $objsoc;
  211. if ($action == 'add' || $action == 'update') $this->assign_post();
  212. foreach($this->object as $key => $value)
  213. {
  214. $this->tpl[$key] = $value;
  215. }
  216. $this->tpl['error']=$this->error;
  217. $this->tpl['errors']=$this->errors;
  218. if ($action == 'create' || $action == 'edit')
  219. {
  220. if ($conf->use_javascript_ajax)
  221. {
  222. $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
  223. jQuery(document).ready(function () {
  224. jQuery("#selectcountry_id").change(function() {
  225. document.formsoc.action.value="'.$action.'";
  226. document.formsoc.canvas.value="'.$canvas.'";
  227. document.formsoc.submit();
  228. });
  229. })
  230. </script>'."\n";
  231. }
  232. if (is_object($objsoc) && $objsoc->id > 0)
  233. {
  234. $this->tpl['company'] = $objsoc->getNomUrl(1);
  235. $this->tpl['company_id'] = $objsoc->id;
  236. }
  237. else
  238. {
  239. $this->tpl['company'] = $form->select_company($this->object->socid,'socid','',1);
  240. }
  241. // Civility
  242. $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civilite_id);
  243. // Predefined with third party
  244. if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE'))
  245. {
  246. if (dol_strlen(trim($this->object->address)) == 0) $this->tpl['address'] = $objsoc->address;
  247. if (dol_strlen(trim($this->object->zip)) == 0) $this->object->zip = $objsoc->zip;
  248. if (dol_strlen(trim($this->object->town)) == 0) $this->object->town = $objsoc->town;
  249. if (dol_strlen(trim($this->object->phone_perso)) == 0) $this->object->phone_perso = $objsoc->phone;
  250. if (dol_strlen(trim($this->object->phone_mobile)) == 0) $this->object->phone_mobile = $objsoc->phone_mobile;
  251. if (dol_strlen(trim($this->object->email)) == 0) $this->object->email = $objsoc->email;
  252. }
  253. // Zip
  254. $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip,'zipcode',array('town','selectcountry_id','departement_id'),6);
  255. // Town
  256. $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town,'town',array('zipcode','selectcountry_id','departement_id'));
  257. if (dol_strlen(trim($this->object->country_id)) == 0) $this->object->country_id = $objsoc->country_id;
  258. // Country
  259. $this->tpl['select_country'] = $form->select_country($this->object->country_id,'country_id');
  260. $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
  261. if ($user->admin) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  262. // State
  263. if ($this->object->country_id) $this->tpl['select_state'] = $formcompany->select_state($this->object->fk_departement,$this->object->country_code);
  264. else $this->tpl['select_state'] = $countrynotdefined;
  265. // Physical or Moral
  266. $selectarray=array('0'=>$langs->trans("Physical"),'1'=>$langs->trans("Moral"));
  267. $this->tpl['select_morphy'] = $form->selectarray('morphy',$selectarray,$this->object->morphy,0);
  268. }
  269. if ($action == 'view' || $action == 'edit' || $action == 'delete')
  270. {
  271. // Emailing
  272. if (! empty($conf->mailing->enabled))
  273. {
  274. $langs->load("mails");
  275. $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
  276. }
  277. // Dolibarr user
  278. if ($this->object->user_id)
  279. {
  280. $dolibarr_user=new User($this->db);
  281. $result=$dolibarr_user->fetch($this->object->user_id);
  282. $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
  283. }
  284. else $this->tpl['dolibarr_user'] = $langs->trans("NoSpeedealingAccess");
  285. }
  286. if ($action == 'view' || $action == 'delete')
  287. {
  288. $this->tpl['showrefnav'] = $form->showrefnav($this->object,'id');
  289. if ($this->object->socid > 0)
  290. {
  291. $objsoc = new Societe($this->db);
  292. $objsoc->fetch($this->object->socid);
  293. $this->tpl['company'] = $objsoc->getNomUrl(1);
  294. }
  295. else
  296. {
  297. $this->tpl['company'] = $langs->trans("AdherentNotLinkedToThirdParty");
  298. }
  299. $this->tpl['civility'] = $this->object->getCivilityLabel();
  300. $this->tpl['address'] = dol_nl2br($this->object->address);
  301. $this->tpl['zip'] = ($this->object->zip?$this->object->zip.'&nbsp;':'');
  302. $img=picto_from_langcode($this->object->country_code);
  303. $this->tpl['country'] = ($img?$img.' ':'').$this->object->country;
  304. $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso,$this->object->country_code,0,$this->object->id,'AC_TEL');
  305. $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile,$this->object->country_code,0,$this->object->id,'AC_TEL');
  306. $this->tpl['email'] = dol_print_email($this->object->email,0,$this->object->id,'AC_EMAIL');
  307. $this->tpl['visibility'] = $this->object->getmorphylib($this->object->morphy);
  308. $this->tpl['note'] = nl2br($this->object->note);
  309. }
  310. if ($action == 'create_user')
  311. {
  312. // Full firstname and name separated with a dot : firstname.name
  313. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  314. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  315. $login=dol_buildlogin($this->object->nom, $this->object->prenom);
  316. $generated_password=getRandomPassword('');
  317. $password=$generated_password;
  318. // Create a form array
  319. $formquestion=array(
  320. array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
  321. array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
  322. $this->tpl['action_create_user'] = $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$this->object->id,$langs->trans("CreateSpeedealingLogin"),$langs->trans("ConfirmCreateAdherent"),"confirm_create_user",$formquestion,'no');
  323. }
  324. }
  325. /**
  326. * Assign POST values into object
  327. *
  328. * @return string HTML output
  329. */
  330. private function assign_post()
  331. {
  332. global $langs, $mysoc;
  333. $this->object->old_name = $_POST["old_name"];
  334. $this->object->old_firstname = $_POST["old_firstname"];
  335. $this->object->fk_soc = $_POST["fk_soc"];
  336. $this->object->name = $_POST["name"];
  337. $this->object->firstname = $_POST["firstname"];
  338. $this->object->civilite_id = $_POST["civilite_id"];
  339. $this->object->address = $_POST["address"];
  340. $this->object->zip = $_POST["zipcode"];
  341. $this->object->town = $_POST["town"];
  342. $this->object->fk_pays = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
  343. $this->object->fk_departement = $_POST["departement_id"];
  344. $this->object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
  345. $this->object->state_id = $_POST["departement_id"];
  346. $this->object->phone_perso = $_POST["phone_perso"];
  347. $this->object->phone_mobile = $_POST["phone_mobile"];
  348. $this->object->email = $_POST["email"];
  349. $this->object->note = $_POST["note"];
  350. $this->object->canvas = $_POST["canvas"];
  351. // We set country_id, and country_code label of the chosen country
  352. if ($this->object->country_id)
  353. {
  354. $sql = "SELECT code, libelle FROM ".MAIN_DB_PREFIX."c_pays WHERE rowid = ".$this->object->country_id;
  355. $resql=$this->db->query($sql);
  356. if ($resql)
  357. {
  358. $obj = $this->db->fetch_object($resql);
  359. }
  360. else
  361. {
  362. dol_print_error($this->db);
  363. }
  364. $this->object->pays_code = $obj->code;
  365. $this->object->pays = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
  366. $this->object->country_code = $obj->code;
  367. $this->object->country = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
  368. }
  369. }
  370. }
  371. ?>