actions_adherentcard_common.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.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 <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/adherents/canvas/actions_adherentcard_common.class.php
  20. * \ingroup adherent
  21. * \brief Fichier de la classe Adherent card controller (common)
  22. */
  23. /**
  24. * Class to maage members using default canvas
  25. */
  26. abstract class ActionsAdherentCardCommon
  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 Adherent($this->db);
  64. if (! empty($id)) $object->fetch($id);
  65. $this->object = $object;
  66. //}
  67. }
  68. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  69. /**
  70. * Set content of ->tpl array, to use into template
  71. *
  72. * @param string $action Type of action
  73. * @param int $id Id
  74. * @return string HTML output
  75. */
  76. public function assign_values(&$action, $id)
  77. {
  78. // phpcs:enable
  79. global $conf, $langs, $user, $canvas;
  80. global $form, $formcompany, $objsoc;
  81. if ($action == 'add' || $action == 'update') $this->assign_post();
  82. foreach($this->object as $key => $value)
  83. {
  84. $this->tpl[$key] = $value;
  85. }
  86. $this->tpl['error']=$this->error;
  87. $this->tpl['errors']=$this->errors;
  88. if ($action == 'create' || $action == 'edit')
  89. {
  90. if ($conf->use_javascript_ajax)
  91. {
  92. $this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
  93. jQuery(document).ready(function () {
  94. jQuery("#selectcountry_id").change(function() {
  95. document.formsoc.action.value="'.$action.'";
  96. document.formsoc.canvas.value="'.$canvas.'";
  97. document.formsoc.submit();
  98. });
  99. })
  100. </script>'."\n";
  101. }
  102. if (is_object($objsoc) && $objsoc->id > 0)
  103. {
  104. $this->tpl['company'] = $objsoc->getNomUrl(1);
  105. $this->tpl['company_id'] = $objsoc->id;
  106. }
  107. else
  108. {
  109. $this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
  110. }
  111. // Civility
  112. $this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
  113. // Predefined with third party
  114. if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE'))
  115. {
  116. if (dol_strlen(trim($this->object->address)) == 0) $this->tpl['address'] = $objsoc->address;
  117. if (dol_strlen(trim($this->object->zip)) == 0) $this->object->zip = $objsoc->zip;
  118. if (dol_strlen(trim($this->object->town)) == 0) $this->object->town = $objsoc->town;
  119. if (dol_strlen(trim($this->object->phone_perso)) == 0) $this->object->phone_perso = $objsoc->phone;
  120. if (dol_strlen(trim($this->object->phone_mobile)) == 0) $this->object->phone_mobile = $objsoc->phone_mobile;
  121. if (dol_strlen(trim($this->object->email)) == 0) $this->object->email = $objsoc->email;
  122. }
  123. // Zip
  124. $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town','selectcountry_id','state_id'), 6);
  125. // Town
  126. $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode','selectcountry_id','state_id'));
  127. if (dol_strlen(trim($this->object->country_id)) == 0) $this->object->country_id = $objsoc->country_id;
  128. // Country
  129. $this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
  130. $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
  131. if ($user->admin) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  132. // State
  133. if ($this->object->country_id) $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
  134. else $this->tpl['select_state'] = $countrynotdefined;
  135. // Physical or Moral
  136. $selectarray=array('0'=>$langs->trans("Physical"),'1'=>$langs->trans("Moral"));
  137. $this->tpl['select_morphy'] = $form->selectarray('morphy', $selectarray, $this->object->morphy, 0);
  138. }
  139. if ($action == 'view' || $action == 'edit' || $action == 'delete')
  140. {
  141. // Emailing
  142. if (! empty($conf->mailing->enabled))
  143. {
  144. $langs->load("mails");
  145. $this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
  146. }
  147. // Dolibarr user
  148. if ($this->object->user_id)
  149. {
  150. $dolibarr_user=new User($this->db);
  151. $result=$dolibarr_user->fetch($this->object->user_id);
  152. $this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
  153. }
  154. else $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
  155. }
  156. if ($action == 'view' || $action == 'delete')
  157. {
  158. $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
  159. if ($this->object->socid > 0)
  160. {
  161. $objsoc = new Societe($this->db);
  162. $objsoc->fetch($this->object->socid);
  163. $this->tpl['company'] = $objsoc->getNomUrl(1);
  164. }
  165. else
  166. {
  167. $this->tpl['company'] = $langs->trans("AdherentNotLinkedToThirdParty");
  168. }
  169. $this->tpl['civility'] = $this->object->getCivilityLabel();
  170. $this->tpl['address'] = dol_nl2br($this->object->address);
  171. $this->tpl['zip'] = ($this->object->zip?$this->object->zip.'&nbsp;':'');
  172. $img=picto_from_langcode($this->object->country_code);
  173. $this->tpl['country'] = ($img?$img.' ':'').$this->object->country;
  174. $this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
  175. $this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
  176. $this->tpl['email'] = dol_print_email($this->object->email, 0, $this->object->id, 'AC_EMAIL');
  177. $this->tpl['visibility'] = $this->object->getmorphylib($this->object->morphy);
  178. $this->tpl['note'] = nl2br($this->object->note);
  179. }
  180. if ($action == 'create_user')
  181. {
  182. // Full firstname and lastname separated with a dot : firstname.lastname
  183. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  184. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  185. $login=dol_buildlogin($this->object->lastname, $this->object->firstname);
  186. $generated_password=getRandomPassword(false);
  187. $password=$generated_password;
  188. // Create a form array
  189. $formquestion=array(
  190. array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
  191. array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
  192. $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');
  193. }
  194. }
  195. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  196. /**
  197. * Assign POST values into object
  198. *
  199. * @return string HTML output
  200. */
  201. private function assign_post()
  202. {
  203. // phpcs:enable
  204. global $langs, $mysoc;
  205. $this->object->old_name = $_POST["old_name"];
  206. $this->object->old_firstname = $_POST["old_firstname"];
  207. $this->object->fk_soc = $_POST["fk_soc"];
  208. $this->object->lastname = $_POST["lastname"];
  209. $this->object->firstname = $_POST["firstname"];
  210. $this->object->civility_id = $_POST["civility_id"];
  211. $this->object->address = $_POST["address"];
  212. $this->object->zip = $_POST["zipcode"];
  213. $this->object->town = $_POST["town"];
  214. $this->object->country_id = $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
  215. $this->object->state_id = $_POST["state_id"];
  216. $this->object->phone_perso = $_POST["phone_perso"];
  217. $this->object->phone_mobile = $_POST["phone_mobile"];
  218. $this->object->email = $_POST["email"];
  219. $this->object->note = $_POST["note"];
  220. $this->object->canvas = $_POST["canvas"];
  221. // We set country_id, and country_code label of the chosen country
  222. if ($this->object->country_id)
  223. {
  224. $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".$this->object->country_id;
  225. $resql=$this->db->query($sql);
  226. if ($resql)
  227. {
  228. $obj = $this->db->fetch_object($resql);
  229. $this->object->country_code = $obj->code;
  230. $this->object->country = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
  231. }
  232. else
  233. {
  234. dol_print_error($this->db);
  235. }
  236. }
  237. }
  238. }