PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/contact/canvas/actions_contactcard_common.class.php

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