dolibarr  7.0.0-beta
carte.php
Go to the documentation of this file.
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-2011 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 <http://www.gnu.org/licenses/>.
18  */
19 
25 require '../../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/modules/member/modules_cards.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/modules/printsheet/modules_labels.php';
31 
32 $langs->load("members");
33 $langs->load("errors");
34 
35 // Choix de l'annee d'impression ou annee courante.
36 $now = dol_now();
37 $year=dol_print_date($now,'%Y');
38 $month=dol_print_date($now,'%m');
39 $day=dol_print_date($now,'%d');
40 $foruserid=GETPOST('foruserid');
41 $foruserlogin=GETPOST('foruserlogin');
42 $mode=GETPOST('mode');
43 $model=GETPOST("model"); // Doc template to use for business cards
44 $modellabel=GETPOST("modellabel"); // Doc template to use for address sheet
45 $mesg='';
46 
47 $adherentstatic=new Adherent($db);
48 
49 $extrafields = new ExtraFields($db);
50 // fetch optionals attributes and labels
51 $extralabels = $extrafields->fetch_name_optionals_label('adherent');
52 
53 
54 /*
55  * Actions
56  */
57 
58 if ($mode == 'cardlogin' && empty($foruserlogin))
59 {
60  $mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Login"));
61 }
62 
63 if ((! empty($foruserid) || ! empty($foruserlogin) || ! empty($mode)) && ! $mesg)
64 {
65  $arrayofmembers=array();
66 
67  // request taking into account member with up to date subscriptions
68  $sql = "SELECT d.rowid, d.firstname, d.lastname, d.login, d.societe as company, d.datefin,";
69  $sql.= " d.address, d.zip, d.town, d.country, d.birth, d.email, d.photo,";
70  $sql.= " t.libelle as type,";
71  $sql.= " c.code as country_code, c.label as country";
72  // Add fields from extrafields
73  foreach ($extrafields->attribute_label as $key => $val)
74  $sql.=($extrafields->attribute_type[$key] != 'separate' ? ",ef.".$key.' as options_'.$key : '');
75  $sql.= " FROM ".MAIN_DB_PREFIX."adherent_type as t, ".MAIN_DB_PREFIX."adherent as d";
76  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.country = c.rowid";
77  if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label)) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ef on (d.rowid = ef.fk_object)";
78  $sql.= " WHERE d.fk_adherent_type = t.rowid AND d.statut = 1";
79  $sql.= " AND d.entity IN (".getEntity('adherent').")";
80  if (is_numeric($foruserid)) $sql.=" AND d.rowid=".$foruserid;
81  if ($foruserlogin) $sql.=" AND d.login='".$db->escape($foruserlogin)."'";
82  $sql.= " ORDER BY d.rowid ASC";
83 
84  dol_syslog("Search members", LOG_DEBUG);
85  $result = $db->query($sql);
86  if ($result)
87  {
88  $num = $db->num_rows($result);
89  $i = 0;
90  while ($i < $num)
91  {
92  $objp = $db->fetch_object($result);
93 
94  if ($objp->country == '-') $objp->country='';
95 
96  $adherentstatic->id=$objp->rowid;
97  $adherentstatic->lastname=$objp->lastname;
98  $adherentstatic->firstname=$objp->firstname;
99 
100  // format extrafiled so they can be parsed in function complete_substitutions_array
101  if (is_array($extrafields->attribute_label) && count($extrafields->attribute_label))
102  {
103  $adherentstatic->array_options = array();
104  foreach($extrafields->attribute_label as $key => $val)
105  {
106  $tmpkey='options_'.$key;
107  if (!empty($objp->$tmpkey))
108  {
109  $adherentstatic->array_options[$tmpkey] = $objp->$tmpkey;
110  }
111  //if (!empty($objp->$key))
112  // $objp->array_options[$tmpkey] = $objp->$key;
113  //$objp->array_options[$tmpkey] = $extrafields->showOutputField($key, $objp->$tmpkey, '', 1); //$objp->$tmpkey;
114  }
115  }
116 
117  // List of values to scan for a replacement
118  $substitutionarray = array (
119  '__ID__'=>$objp->rowid,
120  '__LOGIN__'=>$objp->login,
121  '__FIRSTNAME__'=>$objp->firstname,
122  '__LASTNAME__'=>$objp->lastname,
123  '__FULLNAME__'=>$adherentstatic->getFullName($langs),
124  '__COMPANY__'=>$objp->company,
125  '__ADDRESS__'=>$objp->address,
126  '__ZIP__'=>$objp->zip,
127  '__TOWN__'=>$objp->town,
128  '__COUNTRY__'=>$objp->country,
129  '__COUNTRY_CODE__'=>$objp->country_code,
130  '__EMAIL__'=>$objp->email,
131  '__BIRTH__'=>dol_print_date($objp->birth,'day'),
132  '__TYPE__'=>$objp->type,
133  '__YEAR__'=>$year,
134  '__MONTH__'=>$month,
135  '__DAY__'=>$day,
136  '__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
137  '__SERVER__'=>"http://".$_SERVER["SERVER_NAME"]."/"
138  );
139  complete_substitutions_array($substitutionarray, $langs, $adherentstatic);
140 
141  // For business cards
142  if (empty($mode) || $mode=='card' || $mode=='cardlogin')
143  {
144  $textleft=make_substitutions($conf->global->ADHERENT_CARD_TEXT, $substitutionarray);
145  $textheader=make_substitutions($conf->global->ADHERENT_CARD_HEADER_TEXT, $substitutionarray);
146  $textfooter=make_substitutions($conf->global->ADHERENT_CARD_FOOTER_TEXT, $substitutionarray);
147  $textright=make_substitutions($conf->global->ADHERENT_CARD_TEXT_RIGHT, $substitutionarray);
148 
149  if (is_numeric($foruserid) || $foruserlogin)
150  {
151  $nb = $_Avery_Labels[$model]['NX'] * $_Avery_Labels[$model]['NY'];
152  if ($nb <= 0) $nb=1; // Protection to avoid empty page
153 
154  for($j=0;$j<$nb;$j++)
155  {
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  else
167  {
168  $arrayofmembers[]=array(
169  'textleft'=>$textleft,
170  'textheader'=>$textheader,
171  'textfooter'=>$textfooter,
172  'textright'=>$textright,
173  'id'=>$objp->rowid,
174  'photo'=>$objp->photo
175  );
176  }
177  }
178 
179  // For labels
180  if ($mode == 'label')
181  {
182  if (empty($conf->global->ADHERENT_ETIQUETTE_TEXT)) $conf->global->ADHERENT_ETIQUETTE_TEXT="__FULLNAME__\n__ADDRESS__\n__ZIP__ __TOWN__\n__COUNTRY__";
183  $textleft=make_substitutions($conf->global->ADHERENT_ETIQUETTE_TEXT, $substitutionarray);
184  $textheader='';
185  $textfooter='';
186  $textright='';
187 
188  $arrayofmembers[]=array('textleft'=>$textleft,
189  'textheader'=>$textheader,
190  'textfooter'=>$textfooter,
191  'textright'=>$textright,
192  'id'=>$objp->rowid,
193  'photo'=>$objp->photo);
194  }
195 
196  $i++;
197  }
198 
199  // Build and output PDF
200  if (empty($mode) || $mode=='card' || $mode=='cardlogin')
201  {
202  if (! count($arrayofmembers))
203  {
204  $mesg=$langs->trans("ErrorRecordNotFound");
205  }
206  if (empty($model) || $model == '-1')
207  {
208  $mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DescADHERENT_CARD_TYPE"));
209  }
210  if (! $mesg) $result=members_card_pdf_create($db, $arrayofmembers, $model, $outputlangs);
211 
212  }
213  elseif ($mode == 'label')
214  {
215  if (! count($arrayofmembers))
216  {
217  $mesg=$langs->trans("ErrorRecordNotFound");
218  }
219  if (empty($modellabel) || $modellabel == '-1')
220  {
221  $mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
222  }
223  if (! $mesg) $result=doc_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs);
224  }
225 
226  if ($result <= 0)
227  {
228  dol_print_error('',$result);
229  }
230  }
231  else
232  {
233  dol_print_error($db);
234  }
235 
236  if (! $mesg)
237  {
238  $db->close();
239  exit;
240  }
241 }
242 
243 
244 /*
245  * View
246  */
247 
248 $form=new Form($db);
249 
250 llxHeader('',$langs->trans("MembersCards"));
251 
252 print load_fiche_titre($langs->trans("LinkToGeneratedPages"));
253 print '<br>';
254 
255 print $langs->trans("LinkToGeneratedPagesDesc").'<br>';
256 print '<br>';
257 
258 dol_htmloutput_errors($mesg);
259 
260 print img_picto('','puce').' '.$langs->trans("DocForAllMembersCards",($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="foruserid" value="all">';
263 print '<input type="hidden" name="mode" value="card">';
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 {
270  $arrayoflabels[$codecards]=$_Avery_Labels[$codecards]['name'];
271 }
272 asort($arrayoflabels);
273 print $form->selectarray('model', $arrayoflabels, (GETPOST('model')?GETPOST('model'):$conf->global->ADHERENT_CARD_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
274 print '<br><input class="button" type="submit" value="'.$langs->trans("BuildDoc").'">';
275 print '</form>';
276 print '<br>';
277 
278 print img_picto('','puce').' '.$langs->trans("DocForOneMemberCards",($conf->global->ADHERENT_CARD_TYPE?$conf->global->ADHERENT_CARD_TYPE:$langs->transnoentitiesnoconv("None"))).' ';
279 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
280 print '<input type="hidden" name="mode" value="cardlogin">';
281 print '<input type="hidden" name="action" value="builddoc">';
282 print $langs->trans("DescADHERENT_CARD_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 {
287  $arrayoflabels[$codecards]=$_Avery_Labels[$codecards]['name'];
288 }
289 asort($arrayoflabels);
290 print $form->selectarray('model',$arrayoflabels,(GETPOST('model')?GETPOST('model'):$conf->global->ADHERENT_CARD_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
291 print '<br>'.$langs->trans("Login").': <input size="10" type="text" name="foruserlogin" value="'.GETPOST('foruserlogin').'">';
292 print '<br><input class="button" type="submit" value="'.$langs->trans("BuildDoc").'">';
293 print '</form>';
294 print '<br>';
295 
296 print img_picto('','puce').' '.$langs->trans("DocForLabels",$conf->global->ADHERENT_ETIQUETTE_TYPE).' ';
297 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
298 print '<input type="hidden" name="mode" value="label">';
299 print '<input type="hidden" name="action" value="builddoc">';
300 print $langs->trans("DescADHERENT_ETIQUETTE_TYPE").' ';
301 // List of possible labels (defined into $_Avery_Labels variable set into format_cards.lib.php)
302 $arrayoflabels=array();
303 foreach(array_keys($_Avery_Labels) as $codecards)
304 {
305  $arrayoflabels[$codecards]=$_Avery_Labels[$codecards]['name'];
306 }
307 asort($arrayoflabels);
308 print $form->selectarray('modellabel',$arrayoflabels,(GETPOST('modellabel')?GETPOST('modellabel'):$conf->global->ADHERENT_ETIQUETTE_TYPE), 1, 0, 0, '', 0, 0, 0, '', '', 1);
309 print '<br><input class="button" type="submit" value="'.$langs->trans("BuildDoc").'">';
310 print '</form>';
311 print '<br>';
312 
313 llxFooter();
314 
315 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
doc_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir='', $template='standardlabel', $filename='tmp_address_sheet.pdf')
Create a document onto disk according to template module.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
members_card_pdf_create($db, $arrayofmembers, $modele, $outputlangs, $outputdir='', $template='standard')
Cree un fichier de cartes de visites en fonction du modele de ADHERENT_CARDS_ADDON_PDF.
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_htmloutput_errors($mesgstring='', $mesgarray='', $keepembedded=0)
Print formated error messages to output (Used to show messages on html output).
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage standard extra fields.
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Class to manage members of a foundation.
llxHeader()
Empty header.
Definition: wrapper.php:46
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substition into a text string, replacing keys with vals from $substitutionarray (oldval=>newval)...
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...