carte.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/adherents/cartes/carte.php
  21. * \ingroup member
  22. * \brief Page to output members business cards
  23. */
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_cards.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/modules/printsheet/modules_labels.php';
  30. $langs->loadLangs(array("members", "errors"));
  31. // Choice of printing year or current year.
  32. $now = dol_now();
  33. $year = dol_print_date($now, '%Y');
  34. $month = dol_print_date($now, '%m');
  35. $day = dol_print_date($now, '%d');
  36. $foruserid = GETPOST('foruserid', 'alphanohtml');
  37. $foruserlogin = GETPOST('foruserlogin', 'alphanohtml');
  38. $mode = GETPOST('mode', 'aZ09');
  39. $modelcard = GETPOST("modelcard", 'aZ09'); // Doc template to use for business cards
  40. $model = GETPOST("model", 'aZ09'); // Doc template to use for business cards
  41. $modellabel = GETPOST("modellabel", 'aZ09'); // Doc template to use for address sheet
  42. $mesg = '';
  43. $adherentstatic = new Adherent($db);
  44. $object = new Adherent($db);
  45. $extrafields = new ExtraFields($db);
  46. // Fetch optionals attributes and labels
  47. $extrafields->fetch_name_optionals_label($object->table_element);
  48. // Security check
  49. $result = restrictedArea($user, 'adherent');
  50. /*
  51. * Actions
  52. */
  53. if ($mode == 'cardlogin' && empty($foruserlogin)) {
  54. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Login"));
  55. }
  56. if ((!empty($foruserid) || !empty($foruserlogin) || !empty($mode)) && !$mesg) {
  57. $arrayofmembers = array();
  58. // request taking into account member with up to date subscriptions
  59. $sql = "SELECT d.rowid, d.ref, d.firstname, d.lastname, d.login, d.societe as company, d.datefin,";
  60. $sql .= " d.address, d.zip, d.town, d.country, d.birth, d.email, d.photo,";
  61. $sql .= " t.libelle as type,";
  62. $sql .= " c.code as country_code, c.label as country";
  63. // Add fields from extrafields
  64. if (!empty($extrafields->attributes[$object->table_element]['label'])) {
  65. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  66. $sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key." as options_".$key : '');
  67. }
  68. }
  69. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
  70. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.country = c.rowid";
  71. if (isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
  72. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ef on (d.rowid = ef.fk_object)";
  73. }
  74. $sql .= " WHERE d.fk_adherent_type = t.rowid AND d.statut = 1";
  75. $sql .= " AND d.entity IN (".getEntity('adherent').")";
  76. if (is_numeric($foruserid)) {
  77. $sql .= " AND d.rowid = ".(int) $foruserid;
  78. }
  79. if ($foruserlogin) {
  80. $sql .= " AND d.login = '".$db->escape($foruserlogin)."'";
  81. }
  82. $sql .= " ORDER BY d.rowid ASC";
  83. dol_syslog("Search members", LOG_DEBUG);
  84. $result = $db->query($sql);
  85. if ($result) {
  86. $num = $db->num_rows($result);
  87. $i = 0;
  88. while ($i < $num) {
  89. $objp = $db->fetch_object($result);
  90. if ($objp->country == '-') {
  91. $objp->country = '';
  92. }
  93. $adherentstatic->id = $objp->rowid;
  94. $adherentstatic->ref = $objp->ref;
  95. $adherentstatic->lastname = $objp->lastname;
  96. $adherentstatic->firstname = $objp->firstname;
  97. // Format extrafield so they can be parsed in function complete_substitutions_array
  98. if (isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
  99. $adherentstatic->array_options = array();
  100. foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
  101. $tmpkey = 'options_'.$key;
  102. if (!empty($objp->$tmpkey)) {
  103. $adherentstatic->array_options[$tmpkey] = $objp->$tmpkey;
  104. }
  105. //if (!empty($objp->$key))
  106. // $objp->array_options[$tmpkey] = $objp->$key;
  107. //$objp->array_options[$tmpkey] = $extrafields->showOutputField($key, $objp->$tmpkey, '', $object->table_element); //$objp->$tmpkey;
  108. }
  109. }
  110. // List of values to scan for a replacement
  111. $substitutionarray = array(
  112. '__ID__'=>$objp->rowid,
  113. '__REF__'=>$objp->ref,
  114. '__LOGIN__'=>$objp->login,
  115. '__FIRSTNAME__'=>$objp->firstname,
  116. '__LASTNAME__'=>$objp->lastname,
  117. '__FULLNAME__'=>$adherentstatic->getFullName($langs),
  118. '__COMPANY__'=>$objp->company,
  119. '__ADDRESS__'=>$objp->address,
  120. '__ZIP__'=>$objp->zip,
  121. '__TOWN__'=>$objp->town,
  122. '__COUNTRY__'=>$objp->country,
  123. '__COUNTRY_CODE__'=>$objp->country_code,
  124. '__EMAIL__'=>$objp->email,
  125. '__BIRTH__'=>dol_print_date($objp->birth, 'day'),
  126. '__TYPE__'=>$objp->type,
  127. '__YEAR__'=>$year,
  128. '__MONTH__'=>$month,
  129. '__DAY__'=>$day,
  130. '__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
  131. '__SERVER__'=>"https://".$_SERVER["SERVER_NAME"]."/"
  132. );
  133. complete_substitutions_array($substitutionarray, $langs, $adherentstatic);
  134. // For business cards
  135. if (empty($mode) || $mode == 'card' || $mode == 'cardlogin') {
  136. $textleft = make_substitutions($conf->global->ADHERENT_CARD_TEXT, $substitutionarray);
  137. $textheader = make_substitutions($conf->global->ADHERENT_CARD_HEADER_TEXT, $substitutionarray);
  138. $textfooter = make_substitutions($conf->global->ADHERENT_CARD_FOOTER_TEXT, $substitutionarray);
  139. $textright = make_substitutions($conf->global->ADHERENT_CARD_TEXT_RIGHT, $substitutionarray);
  140. if (is_numeric($foruserid) || $foruserlogin) {
  141. $nb = $_Avery_Labels[$model]['NX'] * $_Avery_Labels[$model]['NY']; // $_Avery_Labels is defined into an include
  142. if ($nb <= 0) {
  143. $nb = 1; // Protection to avoid empty page
  144. }
  145. for ($j = 0; $j < $nb; $j++) {
  146. $arrayofmembers[] = array(
  147. 'textleft'=>$textleft,
  148. 'textheader'=>$textheader,
  149. 'textfooter'=>$textfooter,
  150. 'textright'=>$textright,
  151. 'id'=>$objp->rowid,
  152. 'ref'=>$objp->ref,
  153. 'photo'=>$objp->photo
  154. );
  155. }
  156. } else {
  157. $arrayofmembers[] = array(
  158. 'textleft'=>$textleft,
  159. 'textheader'=>$textheader,
  160. 'textfooter'=>$textfooter,
  161. 'textright'=>$textright,
  162. 'id'=>$objp->rowid,
  163. 'ref'=>$objp->ref,
  164. 'photo'=>$objp->photo
  165. );
  166. }
  167. }
  168. // For labels
  169. if ($mode == 'label') {
  170. if (empty($conf->global->ADHERENT_ETIQUETTE_TEXT)) {
  171. $conf->global->ADHERENT_ETIQUETTE_TEXT = "__FULLNAME__\n__ADDRESS__\n__ZIP__ __TOWN__\n__COUNTRY__";
  172. }
  173. $textleft = make_substitutions($conf->global->ADHERENT_ETIQUETTE_TEXT, $substitutionarray);
  174. $textheader = '';
  175. $textfooter = '';
  176. $textright = '';
  177. $arrayofmembers[] = array(
  178. 'textleft'=>$textleft,
  179. 'textheader'=>$textheader,
  180. 'textfooter'=>$textfooter,
  181. 'textright'=>$textright,
  182. 'id'=>$objp->rowid,
  183. 'ref'=>$objp->ref,
  184. 'photo'=>$objp->photo,
  185. );
  186. }
  187. $i++;
  188. }
  189. // Build and output PDF
  190. $outputlangs = $langs;
  191. if (empty($mode) || $mode == 'card') {
  192. if (!count($arrayofmembers)) {
  193. $mesg = $langs->trans("ErrorRecordNotFound");
  194. }
  195. if (empty($modelcard) || $modelcard == '-1') {
  196. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DescADHERENT_CARD_TYPE"));
  197. }
  198. if (!$mesg) {
  199. $result = members_card_pdf_create($db, $arrayofmembers, $modelcard, $outputlangs, '', 'standard', 'tmp_cards');
  200. }
  201. } elseif ($mode == 'cardlogin') {
  202. if (!count($arrayofmembers)) {
  203. $mesg = $langs->trans("ErrorRecordNotFound");
  204. }
  205. if (empty($model) || $model == '-1') {
  206. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DescADHERENT_CARD_TYPE"));
  207. }
  208. if (!$mesg) {
  209. $result = members_card_pdf_create($db, $arrayofmembers, $model, $outputlangs, '', 'standard', 'tmp_cards_login');
  210. }
  211. } elseif ($mode == 'label') {
  212. if (!count($arrayofmembers)) {
  213. $mesg = $langs->trans("ErrorRecordNotFound");
  214. }
  215. if (empty($modellabel) || $modellabel == '-1') {
  216. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
  217. }
  218. if (!$mesg) {
  219. $result = doc_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs);
  220. }
  221. }
  222. if ($result <= 0) {
  223. dol_print_error('', $result);
  224. }
  225. } else {
  226. dol_print_error($db);
  227. }
  228. if (!$mesg) {
  229. $db->close();
  230. exit;
  231. }
  232. }
  233. /*
  234. * View
  235. */
  236. $form = new Form($db);
  237. llxHeader('', $langs->trans("MembersCards"));
  238. print load_fiche_titre($langs->trans("LinkToGeneratedPages"), '', $adherentstatic->picto);
  239. print '<span class="opacitymedium">'.$langs->trans("LinkToGeneratedPagesDesc").'</span><br>';
  240. print '<br>';
  241. dol_htmloutput_errors($mesg);
  242. print '<br>';
  243. print img_picto('', 'card').' '.$langs->trans("DocForAllMembersCards", (!empty($conf->global->ADHERENT_CARD_TYPE) ? $conf->global->ADHERENT_CARD_TYPE : $langs->transnoentitiesnoconv("None"))).' ';
  244. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  245. print '<input type="hidden" name="token" value="'.newToken().'">';
  246. print '<input type="hidden" name="foruserid" value="all">';
  247. print '<input type="hidden" name="mode" value="card">';
  248. print '<input type="hidden" name="action" value="builddoc">';
  249. print $langs->trans("DescADHERENT_CARD_TYPE").' ';
  250. // List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
  251. $arrayoflabels = array();
  252. foreach (array_keys($_Avery_Labels) as $codecards) {
  253. $arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
  254. }
  255. asort($arrayoflabels);
  256. print $form->selectarray('modelcard', $arrayoflabels, (GETPOST('modelcard') ? GETPOST('modelcard') : (empty($conf->global->ADHERENT_CARD_TYPE) ? '' : $conf->global->ADHERENT_CARD_TYPE)), 1, 0, 0, '', 0, 0, 0, '', '', 1);
  257. print '<br><input type="submit" class="button small" value="'.$langs->trans("BuildDoc").'">';
  258. print '</form>';
  259. print '<br><br>';
  260. print img_picto('', 'card').' '.$langs->trans("DocForOneMemberCards", (!empty($conf->global->ADHERENT_CARD_TYPE) ? $conf->global->ADHERENT_CARD_TYPE : $langs->transnoentitiesnoconv("None"))).' ';
  261. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  262. print '<input type="hidden" name="token" value="'.newToken().'">';
  263. print '<input type="hidden" name="mode" value="cardlogin">';
  264. print '<input type="hidden" name="action" value="builddoc">';
  265. print $langs->trans("DescADHERENT_CARD_TYPE").' ';
  266. // List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
  267. $arrayoflabels = array();
  268. foreach (array_keys($_Avery_Labels) as $codecards) {
  269. $arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
  270. }
  271. asort($arrayoflabels);
  272. print $form->selectarray('model', $arrayoflabels, (GETPOST('model') ?GETPOST('model') : (empty($conf->global->ADHERENT_CARD_TYPE) ? '' : $conf->global->ADHERENT_CARD_TYPE)), 1, 0, 0, '', 0, 0, 0, '', '', 1);
  273. print '<br>'.$langs->trans("Login").': <input size="10" type="text" name="foruserlogin" value="'.GETPOST('foruserlogin').'">';
  274. print '<br><input type="submit" class="button small" value="'.$langs->trans("BuildDoc").'">';
  275. print '</form>';
  276. print '<br><br>';
  277. print img_picto('', 'card').' '.$langs->trans("DocForLabels", (empty($conf->global->ADHERENT_ETIQUETTE_TYPE) ? '' : $conf->global->ADHERENT_ETIQUETTE_TYPE)).' ';
  278. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  279. print '<input type="hidden" name="token" value="'.newToken().'">';
  280. print '<input type="hidden" name="mode" value="label">';
  281. print '<input type="hidden" name="action" value="builddoc">';
  282. print $langs->trans("DescADHERENT_ETIQUETTE_TYPE").' ';
  283. // List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
  284. $arrayoflabels = array();
  285. foreach (array_keys($_Avery_Labels) as $codecards) {
  286. $arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
  287. }
  288. asort($arrayoflabels);
  289. print $form->selectarray('modellabel', $arrayoflabels, (GETPOST('modellabel') ? GETPOST('modellabel') : (empty($conf->global->ADHERENT_ETIQUETTE_TYPE) ? '' : $conf->global->ADHERENT_ETIQUETTE_TYPE)), 1, 0, 0, '', 0, 0, 0, '', '', 1);
  290. print '<br><input type="submit" class="button small" value="'.$langs->trans("BuildDoc").'">';
  291. print '</form>';
  292. // End of page
  293. llxFooter();
  294. $db->close();