actions_contactcard_common.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.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 <https://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. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. public $dirmodule;
  33. public $targetmodule;
  34. public $canvas;
  35. public $card;
  36. //! Template container
  37. public $tpl = array();
  38. //! Object container
  39. public $object;
  40. /**
  41. * @var string Error code (or message)
  42. */
  43. public $error = '';
  44. /**
  45. * @var string[] Error codes (or messages)
  46. */
  47. public $errors = array();
  48. /**
  49. * Get object
  50. *
  51. * @param int $id Object id
  52. * @return object Object loaded
  53. */
  54. public function getObject($id)
  55. {
  56. /*$ret = $this->getInstanceDao();
  57. if (is_object($this->object) && method_exists($this->object,'fetch'))
  58. {
  59. if (!empty($id)) $this->object->fetch($id);
  60. }
  61. else
  62. {*/
  63. $object = new Contact($this->db);
  64. if (!empty($id)) {
  65. $object->fetch($id);
  66. }
  67. $this->object = $object;
  68. return $object;
  69. }
  70. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  71. /**
  72. * Set content of ->tpl array, to use into template
  73. *
  74. * @param string $action Type of action
  75. * @param int $id Id
  76. * @return void
  77. */
  78. public function assign_values(&$action, $id)
  79. {
  80. // phpcs:enable
  81. global $conf, $langs, $user, $canvas;
  82. global $form, $formcompany, $objsoc;
  83. if ($action == 'add' || $action == 'update') {
  84. $this->assign_post();
  85. }
  86. foreach ($this->object as $key => $value) {
  87. $this->tpl[$key] = $value;
  88. }
  89. $this->tpl['error'] = $this->error;
  90. $this->tpl['errors'] = $this->errors;
  91. if ($action == 'create' || $action == 'edit') {
  92. if ($conf->use_javascript_ajax) {
  93. $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript">
  94. jQuery(document).ready(function () {
  95. jQuery("#selectcountry_id").change(function() {
  96. document.formsoc.action.value="'.$action.'";
  97. document.formsoc.canvas.value="'.$canvas.'";
  98. document.formsoc.submit();
  99. });
  100. })
  101. </script>'."\n";
  102. }
  103. if (is_object($objsoc) && $objsoc->id > 0) {
  104. $this->tpl['company'] = $objsoc->getNomUrl(1);
  105. $this->tpl['company_id'] = $objsoc->id;
  106. } else {
  107. $this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
  108. }
  109. // Civility
  110. $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
  111. // Predefined with third party
  112. if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || !empty($conf->global->CONTACT_USE_COMPANY_ADDRESS)) {
  113. if (dol_strlen(trim($this->object->address)) == 0) {
  114. $this->tpl['address'] = $objsoc->address;
  115. }
  116. if (dol_strlen(trim($this->object->zip)) == 0) {
  117. $this->object->zip = $objsoc->zip;
  118. }
  119. if (dol_strlen(trim($this->object->town)) == 0) {
  120. $this->object->town = $objsoc->town;
  121. }
  122. if (dol_strlen(trim($this->object->phone_pro)) == 0) {
  123. $this->object->phone_pro = $objsoc->phone;
  124. }
  125. if (dol_strlen(trim($this->object->fax)) == 0) {
  126. $this->object->fax = $objsoc->fax;
  127. }
  128. if (dol_strlen(trim($this->object->email)) == 0) {
  129. $this->object->email = $objsoc->email;
  130. }
  131. }
  132. // Zip
  133. $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6);
  134. // Town
  135. $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
  136. if (dol_strlen(trim($this->object->country_id)) == 0) {
  137. $this->object->country_id = $objsoc->country_id;
  138. }
  139. // Country
  140. $this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
  141. $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
  142. if ($user->admin) {
  143. $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  144. }
  145. // State
  146. if ($this->object->country_id) {
  147. $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
  148. } else {
  149. $this->tpl['select_state'] = $countrynotdefined;
  150. }
  151. // Public or private
  152. $selectarray = array('0'=>$langs->trans("ContactPublic"), '1'=>$langs->trans("ContactPrivate"));
  153. $this->tpl['select_visibility'] = $form->selectarray('priv', $selectarray, $this->object->priv, 0);
  154. }
  155. if ($action == 'view' || $action == 'edit' || $action == 'delete') {
  156. // Emailing
  157. if (isModEnabled('mailing')) {
  158. $langs->load("mails");
  159. $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
  160. }
  161. // Linked element
  162. $this->tpl['contact_element'] = array();
  163. $i = 0;
  164. $this->object->load_ref_elements();
  165. if (isModEnabled('commande')) {
  166. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
  167. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande ? $this->object->ref_commande : $langs->trans("NoContactForAnyOrder");
  168. $i++;
  169. }
  170. if (isModEnabled("propal")) {
  171. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
  172. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal ? $this->object->ref_propal : $langs->trans("NoContactForAnyProposal");
  173. $i++;
  174. }
  175. if (isModEnabled('contrat')) {
  176. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
  177. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat ? $this->object->ref_contrat : $langs->trans("NoContactForAnyContract");
  178. $i++;
  179. }
  180. if (isModEnabled('facture')) {
  181. $this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
  182. $this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation ? $this->object->ref_facturation : $langs->trans("NoContactForAnyInvoice");
  183. $i++;
  184. }
  185. // Dolibarr user
  186. if ($this->object->user_id) {
  187. $dolibarr_user = new User($this->db);
  188. $result = $dolibarr_user->fetch($this->object->user_id);
  189. $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
  190. } else {
  191. $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
  192. }
  193. }
  194. if ($action == 'view' || $action == 'delete') {
  195. $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
  196. if ($this->object->socid > 0) {
  197. $objsoc = new Societe($this->db);
  198. $objsoc->fetch($this->object->socid);
  199. $this->tpl['company'] = $objsoc->getNomUrl(1);
  200. } else {
  201. $this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
  202. }
  203. $this->tpl['civility'] = $this->object->getCivilityLabel();
  204. $this->tpl['address'] = dol_nl2br($this->object->address);
  205. $this->tpl['zip'] = ($this->object->zip ? $this->object->zip.'&nbsp;' : '');
  206. $img = picto_from_langcode($this->object->country_code);
  207. $this->tpl['country'] = ($img ? $img.' ' : '').$this->object->country;
  208. $this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
  209. $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
  210. $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
  211. $this->tpl['fax'] = dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
  212. $this->tpl['email'] = dol_print_email($this->object->email, 0, $this->object->id, 'AC_EMAIL');
  213. $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
  214. $this->tpl['note'] = $this->object->note_private;
  215. }
  216. if ($action == 'create_user') {
  217. // Full firstname and lastname separated with a dot : firstname.lastname
  218. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  219. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  220. $login = dol_buildlogin($this->object->lastname, $this->object->firstname);
  221. $generated_password = getRandomPassword(false);
  222. $password = $generated_password;
  223. // Create a form array
  224. $formquestion = array(
  225. array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
  226. array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
  227. $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');
  228. }
  229. }
  230. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  231. /**
  232. * Assign POST values into object
  233. *
  234. * @return void
  235. */
  236. private function assign_post()
  237. {
  238. // phpcs:enable
  239. global $langs, $mysoc;
  240. $this->object->socid = GETPOST("socid", 'int');
  241. $this->object->lastname = GETPOST("name");
  242. $this->object->firstname = GETPOST("firstname");
  243. $this->object->civility_id = GETPOST("civility_id");
  244. $this->object->poste = GETPOST("poste");
  245. $this->object->address = GETPOST("address");
  246. $this->object->zip = GETPOST("zipcode");
  247. $this->object->town = GETPOST("town");
  248. $this->object->country_id = GETPOST("country_id") ? GETPOST("country_id") : $mysoc->country_id;
  249. $this->object->state_id = GETPOST("state_id");
  250. $this->object->phone_pro = GETPOST("phone_pro");
  251. $this->object->phone_perso = GETPOST("phone_perso");
  252. $this->object->phone_mobile = GETPOST("phone_mobile");
  253. $this->object->fax = GETPOST("fax");
  254. $this->object->email = GETPOST("email");
  255. $this->object->priv = GETPOST("priv");
  256. $this->object->note = GETPOST("note", "restricthtml");
  257. $this->object->canvas = GETPOST("canvas");
  258. // We set country_id, and country_code label of the chosen country
  259. if ($this->object->country_id) {
  260. $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".((int) $this->object->country_id);
  261. $resql = $this->db->query($sql);
  262. if ($resql) {
  263. $obj = $this->db->fetch_object($resql);
  264. } else {
  265. dol_print_error($this->db);
  266. }
  267. $this->object->country_id = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
  268. $this->object->country_code = $obj->code;
  269. $this->object->country = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
  270. }
  271. }
  272. }