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