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/**
21 *	\file 		htdocs/adherents/cartes/carte.php
22 *	\ingroup    member
23 *	\brief      Page to output members business cards
24 */
25require '../../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_cards.php';
30require_once DOL_DOCUMENT_ROOT.'/core/modules/printsheet/modules_labels.php';
31
32$langs->loadLangs(array("members", "errors"));
33
34// Choice of printing year or current year.
35$now = dol_now();
36$year = dol_print_date($now, '%Y');
37$month = dol_print_date($now, '%m');
38$day = dol_print_date($now, '%d');
39$foruserid = GETPOST('foruserid', 'alphanohtml');
40$foruserlogin = GETPOST('foruserlogin', 'alphanohtml');
41$mode = GETPOST('mode', 'aZ09');
42$model = GETPOST("model", 'aZ09'); // Doc template to use for business cards
43$modellabel = GETPOST("modellabel", 'aZ09'); // Doc template to use for address sheet
44$mesg = '';
45
46$adherentstatic = new Adherent($db);
47$object = new Adherent($db);
48
49$extrafields = new ExtraFields($db);
50// Fetch optionals attributes and labels
51$extrafields->fetch_name_optionals_label($object->table_element);
52
53
54/*
55 * Actions
56 */
57
58if ($mode == 'cardlogin' && empty($foruserlogin)) {
59	$mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Login"));
60}
61
62if ((!empty($foruserid) || !empty($foruserlogin) || !empty($mode)) && !$mesg) {
63	$arrayofmembers = array();
64
65	// request taking into account member with up to date subscriptions
66	$sql = "SELECT d.rowid, d.firstname, d.lastname, d.login, d.societe as company, d.datefin,";
67	$sql .= " d.address, d.zip, d.town, d.country, d.birth, d.email, d.photo,";
68	$sql .= " t.libelle as type,";
69	$sql .= " c.code as country_code, c.label as country";
70	// Add fields from extrafields
71	if (!empty($extrafields->attributes[$object->table_element]['label']))
72		foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) $sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key.' as options_'.$key : '');
73	$sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
74	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.country = c.rowid";
75	if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ef on (d.rowid = ef.fk_object)";
76	$sql .= " WHERE d.fk_adherent_type = t.rowid AND d.statut = 1";
77	$sql .= " AND d.entity IN (".getEntity('adherent').")";
78	if (is_numeric($foruserid)) $sql .= " AND d.rowid=".$foruserid;
79	if ($foruserlogin) $sql .= " AND d.login='".$db->escape($foruserlogin)."'";
80	$sql .= " ORDER BY d.rowid ASC";
81
82	dol_syslog("Search members", LOG_DEBUG);
83	$result = $db->query($sql);
84	if ($result) {
85		$num = $db->num_rows($result);
86		$i = 0;
87		while ($i < $num) {
88			$objp = $db->fetch_object($result);
89
90			if ($objp->country == '-') $objp->country = '';
91
92			$adherentstatic->id = $objp->rowid;
93			$adherentstatic->lastname = $objp->lastname;
94			$adherentstatic->firstname = $objp->firstname;
95
96			// Format extrafield so they can be parsed in function complete_substitutions_array
97			if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
98				$adherentstatic->array_options = array();
99				foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
100					$tmpkey = 'options_'.$key;
101					if (!empty($objp->$tmpkey)) {
102						$adherentstatic->array_options[$tmpkey] = $objp->$tmpkey;
103					}
104					//if (!empty($objp->$key))
105					//    $objp->array_options[$tmpkey] = $objp->$key;
106					//$objp->array_options[$tmpkey] = $extrafields->showOutputField($key, $objp->$tmpkey, '', 1); //$objp->$tmpkey;
107				}
108			}
109
110			// List of values to scan for a replacement
111			$substitutionarray = array(
112				'__ID__'=>$objp->rowid,
113				'__LOGIN__'=>$objp->login,
114				'__FIRSTNAME__'=>$objp->firstname,
115				'__LASTNAME__'=>$objp->lastname,
116				'__FULLNAME__'=>$adherentstatic->getFullName($langs),
117				'__COMPANY__'=>$objp->company,
118				'__ADDRESS__'=>$objp->address,
119				'__ZIP__'=>$objp->zip,
120				'__TOWN__'=>$objp->town,
121				'__COUNTRY__'=>$objp->country,
122				'__COUNTRY_CODE__'=>$objp->country_code,
123				'__EMAIL__'=>$objp->email,
124				'__BIRTH__'=>dol_print_date($objp->birth, 'day'),
125				'__TYPE__'=>$objp->type,
126				'__YEAR__'=>$year,
127				'__MONTH__'=>$month,
128				'__DAY__'=>$day,
129				'__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
130				'__SERVER__'=>"http://".$_SERVER["SERVER_NAME"]."/"
131			);
132			complete_substitutions_array($substitutionarray, $langs, $adherentstatic);
133
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
141				if (is_numeric($foruserid) || $foruserlogin) {
142					$nb = $_Avery_Labels[$model]['NX'] * $_Avery_Labels[$model]['NY'];
143					if ($nb <= 0) $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							'photo'=>$objp->photo
153						);
154					}
155				} else {
156					$arrayofmembers[] = array(
157						'textleft'=>$textleft,
158						'textheader'=>$textheader,
159						'textfooter'=>$textfooter,
160						'textright'=>$textright,
161						'id'=>$objp->rowid,
162						'photo'=>$objp->photo
163					);
164				}
165			}
166
167			// For labels
168			if ($mode == 'label') {
169				if (empty($conf->global->ADHERENT_ETIQUETTE_TEXT)) $conf->global->ADHERENT_ETIQUETTE_TEXT = "__FULLNAME__\n__ADDRESS__\n__ZIP__ __TOWN__\n__COUNTRY__";
170				$textleft = make_substitutions($conf->global->ADHERENT_ETIQUETTE_TEXT, $substitutionarray);
171				$textheader = '';
172				$textfooter = '';
173				$textright = '';
174
175				$arrayofmembers[] = array('textleft'=>$textleft,
176										'textheader'=>$textheader,
177										'textfooter'=>$textfooter,
178										'textright'=>$textright,
179										'id'=>$objp->rowid,
180										'photo'=>$objp->photo);
181			}
182
183			$i++;
184		}
185
186		// Build and output PDF
187		if (empty($mode) || $mode == 'card' || $mode == 'cardlogin') {
188			if (!count($arrayofmembers)) {
189				$mesg = $langs->trans("ErrorRecordNotFound");
190			}
191			if (empty($model) || $model == '-1') {
192				$mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DescADHERENT_CARD_TYPE"));
193			}
194			if (!$mesg) $result = members_card_pdf_create($db, $arrayofmembers, $model, $outputlangs);
195		} elseif ($mode == 'label') {
196			if (!count($arrayofmembers)) {
197				$mesg = $langs->trans("ErrorRecordNotFound");
198			}
199			if (empty($modellabel) || $modellabel == '-1') {
200				$mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
201			}
202			if (!$mesg) $result = doc_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs);
203		}
204
205		if ($result <= 0) {
206			dol_print_error('', $result);
207		}
208	} else {
209		dol_print_error($db);
210	}
211
212	if (!$mesg) {
213		$db->close();
214		exit;
215	}
216}
217
218
219/*
220 * View
221 */
222
223$form = new Form($db);
224
225llxHeader('', $langs->trans("MembersCards"));
226
227print load_fiche_titre($langs->trans("LinkToGeneratedPages"), '', 'members');
228
229print '<span class="opacitymedium">'.$langs->trans("LinkToGeneratedPagesDesc").'</span><br>';
230print '<br>';
231
232dol_htmloutput_errors($mesg);
233
234print '<br>';
235
236print img_picto('', 'puce').' '.$langs->trans("DocForAllMembersCards", ($conf->global->ADHERENT_CARD_TYPE ? $conf->global->ADHERENT_CARD_TYPE : $langs->transnoentitiesnoconv("None"))).' ';
237print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
238print '<input type="hidden" name="token" value="'.newToken().'">';
239print '<input type="hidden" name="foruserid" value="all">';
240print '<input type="hidden" name="mode" value="card">';
241print '<input type="hidden" name="action" value="builddoc">';
242print $langs->trans("DescADHERENT_CARD_TYPE").' ';
243// List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
244$arrayoflabels = array();
245foreach (array_keys($_Avery_Labels) as $codecards) {
246	$arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
247}
248asort($arrayoflabels);
249print $form->selectarray('model', $arrayoflabels, (GETPOST('model') ?GETPOST('model') : $conf->global->ADHERENT_CARD_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
250print '<br><input class="button" type="submit" value="'.$langs->trans("BuildDoc").'">';
251print '</form>';
252
253print '<br><br>';
254
255print img_picto('', 'puce').' '.$langs->trans("DocForOneMemberCards", ($conf->global->ADHERENT_CARD_TYPE ? $conf->global->ADHERENT_CARD_TYPE : $langs->transnoentitiesnoconv("None"))).' ';
256print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
257print '<input type="hidden" name="token" value="'.newToken().'">';
258print '<input type="hidden" name="mode" value="cardlogin">';
259print '<input type="hidden" name="action" value="builddoc">';
260print $langs->trans("DescADHERENT_CARD_TYPE").' ';
261// List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
262$arrayoflabels = array();
263foreach (array_keys($_Avery_Labels) as $codecards) {
264	$arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
265}
266asort($arrayoflabels);
267print $form->selectarray('model', $arrayoflabels, (GETPOST('model') ?GETPOST('model') : $conf->global->ADHERENT_CARD_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
268print '<br>'.$langs->trans("Login").': <input size="10" type="text" name="foruserlogin" value="'.GETPOST('foruserlogin').'">';
269print '<br><input class="button" type="submit" value="'.$langs->trans("BuildDoc").'">';
270print '</form>';
271
272print '<br><br>';
273
274print img_picto('', 'puce').' '.$langs->trans("DocForLabels", $conf->global->ADHERENT_ETIQUETTE_TYPE).' ';
275print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
276print '<input type="hidden" name="token" value="'.newToken().'">';
277print '<input type="hidden" name="mode" value="label">';
278print '<input type="hidden" name="action" value="builddoc">';
279print $langs->trans("DescADHERENT_ETIQUETTE_TYPE").' ';
280// List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
281$arrayoflabels = array();
282foreach (array_keys($_Avery_Labels) as $codecards) {
283	$arrayoflabels[$codecards] = $_Avery_Labels[$codecards]['name'];
284}
285asort($arrayoflabels);
286print $form->selectarray('modellabel', $arrayoflabels, (GETPOST('modellabel') ?GETPOST('modellabel') : $conf->global->ADHERENT_ETIQUETTE_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
287print '<br><input class="button" type="submit" value="'.$langs->trans("BuildDoc").'">';
288print '</form>';
289
290// End of page
291llxFooter();
292$db->close();
293