PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/speedealing/speedealing
PHP | 471 lines | 328 code | 66 blank | 77 comment | 84 complexity | 466504802101fc6d3f8f8829746da0ed 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. *
  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. /**
  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 $id 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 string &$action Type of action
  88. * @param int $id Id of object
  89. * @return void
  90. */
  91. function doActions(&$action, $id)
  92. {
  93. global $conf, $user, $langs;
  94. // Creation utilisateur depuis contact
  95. if ($action == 'confirm_create_user' && GETPOST("confirm") == 'yes')
  96. {
  97. // Recuperation contact actuel
  98. $result = $this->object->fetch($id);
  99. if ($result > 0)
  100. {
  101. $this->db->begin();
  102. // Creation user
  103. $nuser = new User($this->db);
  104. $result=$nuser->create_from_contact($this->object,$_POST["login"]);
  105. if ($result > 0)
  106. {
  107. $result2=$nuser->setPassword($user,$_POST["password"],0,1,1);
  108. if ($result2)
  109. {
  110. $this->db->commit();
  111. }
  112. else
  113. {
  114. $this->db->rollback();
  115. }
  116. }
  117. else
  118. {
  119. $this->errors=$nuser->error;
  120. $this->db->rollback();
  121. }
  122. }
  123. else
  124. {
  125. $this->errors=$this->object->errors;
  126. }
  127. }
  128. // Creation contact
  129. if ($action == 'add')
  130. {
  131. $this->assign_post();
  132. if (! $_POST["name"])
  133. {
  134. array_push($this->errors,$langs->trans("ErrorFieldRequired",$langs->transnoentities("Lastname").' / '.$langs->transnoentities("Label")));
  135. $action = 'create';
  136. }
  137. if ($_POST["name"])
  138. {
  139. $id = $this->object->create($user);
  140. if ($id > 0)
  141. {
  142. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  143. exit;
  144. }
  145. else
  146. {
  147. $this->errors=$this->object->errors;
  148. $action = 'create';
  149. }
  150. }
  151. }
  152. if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes')
  153. {
  154. $result=$this->object->fetch($id);
  155. $this->object->old_name = $_POST["old_name"];
  156. $this->object->old_firstname = $_POST["old_firstname"];
  157. $result = $this->object->delete();
  158. if ($result > 0)
  159. {
  160. header("Location: list.php");
  161. exit;
  162. }
  163. else
  164. {
  165. $this->errors=$this->object->errors;
  166. }
  167. }
  168. if ($action == 'update')
  169. {
  170. if ($_POST["cancel"])
  171. {
  172. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
  173. exit;
  174. }
  175. if (empty($_POST["name"]))
  176. {
  177. $this->error=array($langs->trans("ErrorFieldRequired",$langs->transnoentities("Name").' / '.$langs->transnoentities("Label")));
  178. $action = 'edit';
  179. }
  180. if (empty($this->error))
  181. {
  182. $this->object->fetch($_POST["contactid"]);
  183. $this->object->oldcopy=dol_clone($this->object);
  184. $this->assign_post();
  185. $result = $this->object->update($_POST["contactid"], $user);
  186. if ($result > 0)
  187. {
  188. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
  189. exit;
  190. }
  191. else
  192. {
  193. $this->errors=$this->object->errors;
  194. $action = 'edit';
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * Set content of ->tpl array, to use into template
  201. *
  202. * @param string &$action Type of action
  203. * @param int $id Id
  204. * @return string HTML output
  205. */
  206. function assign_values(&$action, $id)
  207. {
  208. global $conf, $langs, $user, $canvas;
  209. global $form, $formcompany, $objsoc;
  210. if ($action == 'add' || $action == 'update') $this->assign_post();
  211. foreach($this->object as $key => $value)
  212. {
  213. $this->tpl[$key] = $value;
  214. }
  215. $this->tpl['error']=$this->error;
  216. $this->tpl['errors']=$this->errors;
  217. if ($action == 'create' || $action == 'edit')
  218. {
  219. if ($conf->use_javascript_ajax)
  220. {
  221. $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
  222. jQuery(document).ready(function () {
  223. jQuery("#selectcountry_id").change(function() {
  224. document.formsoc.action.value="'.$action.'";
  225. document.formsoc.canvas.value="'.$canvas.'";
  226. document.formsoc.submit();
  227. });
  228. })
  229. </script>'."\n";
  230. }
  231. if (is_object($objsoc) && $objsoc->id > 0)
  232. {
  233. $this->tpl['company'] = $objsoc->getNomUrl(1);
  234. $this->tpl['company_id'] = $objsoc->id;
  235. }
  236. else
  237. {
  238. $this->tpl['company'] = $form->select_company($this->object->socid,'socid','',1);
  239. }
  240. // Civility
  241. $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civilite_id);
  242. // Predefined with third party
  243. if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || ! empty($conf->global->CONTACT_USE_COMPANY_ADDRESS))
  244. {
  245. if (dol_strlen(trim($this->object->address)) == 0) $this->tpl['address'] = $objsoc->address;
  246. if (dol_strlen(trim($this->object->zip)) == 0) $this->object->zip = $objsoc->zip;
  247. if (dol_strlen(trim($this->object->town)) == 0) $this->object->town = $objsoc->town;
  248. if (dol_strlen(trim($this->object->phone_pro)) == 0) $this->object->phone_pro = $objsoc->phone;
  249. if (dol_strlen(trim($this->object->fax)) == 0) $this->object->fax = $objsoc->fax;
  250. if (dol_strlen(trim($this->object->email)) == 0) $this->object->email = $objsoc->email;
  251. }
  252. // Zip
  253. $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip,'zipcode',array('town','selectcountry_id','departement_id'),6);
  254. // Town
  255. $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town,'town',array('zipcode','selectcountry_id','departement_id'));
  256. if (dol_strlen(trim($this->object->country_id)) == 0) $this->object->country_id = $objsoc->country_id;
  257. // Country
  258. $this->tpl['select_country'] = $form->select_country($this->object->country_id,'country_id');
  259. $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
  260. if ($user->admin) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  261. // State
  262. if ($this->object->country_id) $this->tpl['select_state'] = $formcompany->select_state($this->object->fk_departement,$this->object->country_code);
  263. else $this->tpl['select_state'] = $countrynotdefined;
  264. // Public or private
  265. $selectarray=array('0'=>$langs->trans("ContactPublic"),'1'=>$langs->trans("ContactPrivate"));
  266. $this->tpl['select_visibility'] = $form->selectarray('priv',$selectarray,$this->object->priv,0);
  267. }
  268. if ($action == 'view' || $action == 'edit' || $action == 'delete')
  269. {
  270. // Emailing
  271. if (! empty($conf->mailing->enabled))
  272. {
  273. $langs->load("mails");
  274. $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
  275. }
  276. // Linked element
  277. $this->tpl['contact_element'] = array();
  278. $i=0;
  279. $this->object->load_ref_elements();
  280. if (! empty($conf->commande->enabled))
  281. {
  282. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
  283. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande?$this->object->ref_commande:$langs->trans("NoContactForAnyOrder");
  284. $i++;
  285. }
  286. if (! empty($conf->propal->enabled))
  287. {
  288. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
  289. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal?$this->object->ref_propal:$langs->trans("NoContactForAnyProposal");
  290. $i++;
  291. }
  292. if (! empty($conf->contrat->enabled))
  293. {
  294. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
  295. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat?$this->object->ref_contrat:$langs->trans("NoContactForAnyContract");
  296. $i++;
  297. }
  298. if (! empty($conf->facture->enabled))
  299. {
  300. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
  301. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation?$this->object->ref_facturation:$langs->trans("NoContactForAnyInvoice");
  302. $i++;
  303. }
  304. // Dolibarr user
  305. if ($this->object->user_id)
  306. {
  307. $dolibarr_user=new User($this->db);
  308. $result=$dolibarr_user->fetch($this->object->user_id);
  309. $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
  310. }
  311. else $this->tpl['dolibarr_user'] = $langs->trans("NoSpeedealingAccess");
  312. }
  313. if ($action == 'view' || $action == 'delete')
  314. {
  315. $this->tpl['showrefnav'] = $form->showrefnav($this->object,'id');
  316. if ($this->object->socid > 0)
  317. {
  318. $objsoc = new Societe($this->db);
  319. $objsoc->fetch($this->object->socid);
  320. $this->tpl['company'] = $objsoc->getNomUrl(1);
  321. }
  322. else
  323. {
  324. $this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
  325. }
  326. $this->tpl['civility'] = $this->object->getCivilityLabel();
  327. $this->tpl['address'] = dol_nl2br($this->object->address);
  328. $this->tpl['zip'] = ($this->object->zip?$this->object->zip.'&nbsp;':'');
  329. $img=picto_from_langcode($this->object->country_code);
  330. $this->tpl['country'] = ($img?$img.' ':'').$this->object->country;
  331. $this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro,$this->object->country_code,0,$this->object->id,'AC_TEL');
  332. $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso,$this->object->country_code,0,$this->object->id,'AC_TEL');
  333. $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile,$this->object->country_code,0,$this->object->id,'AC_TEL');
  334. $this->tpl['fax'] = dol_print_phone($this->object->fax,$this->object->country_code,0,$this->object->id,'AC_FAX');
  335. $this->tpl['email'] = dol_print_email($this->object->email,0,$this->object->id,'AC_EMAIL');
  336. $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
  337. $this->tpl['note'] = nl2br($this->object->note);
  338. }
  339. if ($action == 'create_user')
  340. {
  341. // Full firstname and name separated with a dot : firstname.name
  342. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  343. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  344. $login=dol_buildlogin($this->object->nom, $this->object->prenom);
  345. $generated_password=getRandomPassword('');
  346. $password=$generated_password;
  347. // Create a form array
  348. $formquestion=array(
  349. array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
  350. array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
  351. $this->tpl['action_create_user'] = $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$this->object->id,$langs->trans("CreateSpeedealingLogin"),$langs->trans("ConfirmCreateContact"),"confirm_create_user",$formquestion,'no');
  352. }
  353. }
  354. /**
  355. * Assign POST values into object
  356. *
  357. * @return string HTML output
  358. */
  359. private function assign_post()
  360. {
  361. global $langs, $mysoc;
  362. $this->object->old_name = $_POST["old_name"];
  363. $this->object->old_firstname = $_POST["old_firstname"];
  364. $this->object->socid = $_POST["socid"];
  365. $this->object->name = $_POST["name"];
  366. $this->object->firstname = $_POST["firstname"];
  367. $this->object->civilite_id = $_POST["civilite_id"];
  368. $this->object->poste = $_POST["poste"];
  369. $this->object->address = $_POST["address"];
  370. $this->object->zip = $_POST["zipcode"];
  371. $this->object->town = $_POST["town"];
  372. $this->object->fk_pays = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
  373. $this->object->fk_departement = $_POST["departement_id"];
  374. $this->object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
  375. $this->object->state_id = $_POST["departement_id"];
  376. $this->object->phone_pro = $_POST["phone_pro"];
  377. $this->object->phone_perso = $_POST["phone_perso"];
  378. $this->object->phone_mobile = $_POST["phone_mobile"];
  379. $this->object->fax = $_POST["fax"];
  380. $this->object->email = $_POST["email"];
  381. $this->object->jabberid = $_POST["jabberid"];
  382. $this->object->priv = $_POST["priv"];
  383. $this->object->note = $_POST["note"];
  384. $this->object->canvas = $_POST["canvas"];
  385. // We set country_id, and country_code label of the chosen country
  386. if ($this->object->country_id)
  387. {
  388. $sql = "SELECT code, libelle FROM ".MAIN_DB_PREFIX."c_pays WHERE rowid = ".$this->object->country_id;
  389. $resql=$this->db->query($sql);
  390. if ($resql)
  391. {
  392. $obj = $this->db->fetch_object($resql);
  393. }
  394. else
  395. {
  396. dol_print_error($this->db);
  397. }
  398. $this->object->pays_code = $obj->code;
  399. $this->object->pays = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
  400. $this->object->country_code = $obj->code;
  401. $this->object->country = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
  402. }
  403. }
  404. }
  405. ?>