]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/PdfAdhesionForm.php
e344b98cf7e0cb865cee2b52597241ce69de42ca
[galette.git] / galette / lib / Galette / IO / PdfAdhesionForm.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Adhesion form PDF
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category IO
28 * @package Galette
29 *
30 * @author Guillaume Rousse <guillomovitch@gmail.com>
31 * @copyright 2013-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.5dev - 2013-07-07
36 */
37
38 namespace Galette\IO;
39
40 use Galette\Core\Db;
41 use Galette\Core\Preferences;
42 use Galette\DynamicFields\DynamicField;
43 use Galette\Entity\Adherent;
44 use Galette\Entity\PdfModel;
45 use Galette\Entity\PdfAdhesionFormModel;
46 use Galette\Entity\DynamicFieldsHandle;
47 use Galette\IO\Pdf;
48 use Analog\Analog;
49
50 /**
51 * Adhesion Form PDF
52 *
53 * @category IO
54 * @name PDF
55 * @package Galette
56 * @abstract Class for expanding TCPDF.
57 * @author Guillaume Rousse <guillomovitch@gmail.com>
58 * @copyright 2013-2014 The Galette Team
59 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
60 * @link http://galette.tuxfamily.org
61 * @since Available since 0.7.5dev - 2013-07-07
62 */
63
64 class PdfAdhesionForm extends Pdf
65 {
66 protected $zdb;
67 protected $adh;
68 protected $prefs;
69 protected $filename;
70 private $path;
71
72 /**
73 * Main constructor
74 *
75 * @param Adherent $adh Adherent
76 * @param Db $zdb Database instance
77 * @param Preferences $prefs Preferences instance
78 */
79 public function __construct(Adherent $adh = null, Db $zdb, Preferences $prefs)
80 {
81 $this->zdb = $zdb;
82 $this->adh = $adh;
83 $this->prefs = $prefs;
84
85 $model = $this->getModel();
86 parent::__construct($prefs, $model);
87
88 $this->filename = $adh ?
89 __("adherent_form") . '.' . $adh->id . '.pdf' : __("adherent_form") . '.pdf';
90
91 $this->Open();
92
93 $this->AddPage();
94 if ($model !== null) {
95 $this->PageHeader();
96 $this->PageBody();
97 }
98 }
99
100 /**
101 * Get model
102 *
103 * @return PdfModel
104 */
105 protected function getModel()
106 {
107 global $login;
108
109 $model = new PdfAdhesionFormModel($this->zdb, $this->prefs, PdfModel::ADHESION_FORM_MODEL);
110 $dynamic_patterns = $model->extractDynamicPatterns();
111
112 $model->setPatterns(
113 array(
114 'adh_title' => '/{TITLE_ADH}/',
115 'adh_id' => '/{ID_ADH}/',
116 'adh_name' => '/{NAME_ADH}/',
117 'adh_last_name' => '/{LAST_NAME_ADH}/',
118 'adh_first_name' => '/{FIRST_NAME_ADH}/',
119 'adh_nick_name' => '/{NICKNAME_ADH}/',
120 'adh_gender' => '/{GENDER_ADH}/',
121 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
122 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
123 'adh_profession' => '/{PROFESSION_ADH}/',
124 'adh_company' => '/{COMPANY_ADH}/',
125 'adh_address' => '/{ADDRESS_ADH}/',
126 'adh_zip' => '/{ZIP_ADH}/',
127 'adh_town' => '/{TOWN_ADH}/',
128 'adh_country' => '/{COUNTRY_ADH}/',
129 'adh_phone' => '/{PHONE_ADH}/',
130 'adh_mobile' => '/{MOBILE_ADH}/',
131 'adh_email' => '/{EMAIL_ADH}/',
132 'adh_login' => '/{LOGIN_ADH}/',
133 'adh_main_group' => '/{GROUP_ADH}/',
134 'adh_groups' => '/{GROUPS_ADH}/'
135 )
136 );
137
138 foreach ($dynamic_patterns as $pattern) {
139 $key = strtolower($pattern);
140 $model->setPatterns(array($key => "/\{$pattern\}/"));
141 Analog::log("adding dynamic pattern $key => {" . $pattern . "}", Analog::DEBUG);
142 }
143
144 if ($this->adh !== null) {
145 $address = $this->adh->address;
146 if ($this->adh->address_continuation != '') {
147 $address .= '<br/>' . $this->adh->address_continuation;
148 }
149
150 if ($this->adh->isMan()) {
151 $gender = _T("Man");
152 } elseif ($this->adh->isWoman()) {
153 $gender = _T("Woman");
154 } else {
155 $gender = _T("Unspecified");
156 }
157
158 $member_groups = $this->adh->groups;
159 $main_group = _T("None");
160 $group_list = _T("None");
161 if (is_array($member_groups) && count($member_groups) > 0) {
162 $main_group = $member_groups[0]->getName();
163 $group_list = '<ul>';
164 foreach ($member_groups as $group) {
165 $group_list .= '<li>' . $group->getName() . '</li>';
166 }
167 $group_list .= '</ul>';
168 }
169
170 $model->setReplacements(
171 array(
172 'adh_title' => $this->adh->stitle,
173 'adh_id' => $this->adh->id,
174 'adh_name' => $this->adh->sfullname,
175 'adh_last_name' => $this->adh->surname,
176 'adh_first_name' => $this->adh->name,
177 'adh_nickname' => $this->adh->nickname,
178 'adh_gender' => $gender,
179 'adh_birth_date' => $this->adh->birthdate,
180 'adh_birth_place' => $this->adh->birth_place,
181 'adh_profession' => $this->adh->job,
182 'adh_company' => $this->adh->company_name,
183 'adh_address' => $address,
184 'adh_zip' => $this->adh->zipcode,
185 'adh_town' => $this->adh->town,
186 'adh_country' => $this->adh->country,
187 'adh_phone' => $this->adh->phone,
188 'adh_mobile' => $this->adh->gsm,
189 'adh_email' => $this->adh->email,
190 'adh_login' => $this->adh->login,
191 'adh_main_group' => $main_group,
192 'adh_groups' => $group_list
193 )
194 );
195 }
196
197 /** the list of all dynamic fields */
198 $fields =
199 new \Galette\Repository\DynamicFieldsSet($this->zdb, $login);
200 $dynamic_fields = $fields->getList('adh');
201
202 foreach ($dynamic_patterns as $pattern) {
203 $key = strtolower($pattern);
204 $value = '';
205 if (preg_match('/^DYNFIELD_([0-9]+)_ADH$/', $pattern, $match)) {
206 /** dynamic field first value */
207 $field_id = $match[1];
208 if ($this->adh !== null) {
209 $values = $this->adh->getDynamicFields()->getValues($field_id);
210 $value = $values[1];
211 }
212 }
213 if (preg_match('/^LABEL_DYNFIELD_([0-9]+)_ADH$/', $pattern, $match)) {
214 /** dynamic field label */
215 $field_id = $match[1];
216 $value = $dynamic_fields[$field_id]->getName();
217 }
218 if (preg_match('/^INPUT_DYNFIELD_([0-9]+)_ADH$/', $pattern, $match)) {
219 /** dynamic field input form element */
220 $field_id = $match[1];
221 $field_name = $dynamic_fields[$field_id]->getName();
222 $field_type = $dynamic_fields[$field_id]->getType();
223 $field_value = ['field_val' => ''];
224 if ($this->adh !== null) {
225 $field_values = $this->adh->getDynamicFields()->getValues($field_id);
226 $field_value = $field_values[0];
227 }
228 switch ($field_type) {
229 case DynamicField::TEXT:
230 $value .= '<textarea' .
231 ' id="' . $field_name . '"' .
232 ' name="' . $field_name . '"' .
233 ' value="' . $field_value['field_val'] . '"' .
234 '/>';
235 break;
236 case DynamicField::LINE:
237 $value .= '<input type="text"' .
238 ' id="' . $field_name . '"' .
239 ' name="' . $field_name . '"' .
240 ' value="' . $field_value['field_val'] . '"' .
241 ' size="20" maxlength="30"/>';
242 break;
243 case DynamicField::CHOICE:
244 $choice_values = $dynamic_fields[$field_id]->getValues();
245 foreach ($choice_values as $choice_idx => $choice_value) {
246 $value .= '<input type="radio"' .
247 ' id="' . $field_name . '"' .
248 ' name="' . $field_name . '"' .
249 ' value="' . $choice_value . '"';
250 if ($choice_idx == $field_values[0]['field_val']) {
251 $value .= ' checked="checked"';
252 }
253 $value .= '/>';
254 $value .= $choice_value;
255 $value .= '&nbsp;';
256 }
257 break;
258 case DynamicField::DATE:
259 $value .= '<input type="text" name="' .
260 $field_name . '" value="' .
261 $field_value . '" />';
262 break;
263 case DynamicField::BOOLEAN:
264 $value .= '<input type="checkbox"' .
265 ' name="' . $field_name . '"' .
266 ' value="1"';
267 if ($field_value['field_val'] == 1) {
268 $value .= ' checked="checked"';
269 }
270 $value .= '/>';
271 break;
272 case DynamicField::FILE:
273 $value .= '<input type="text" name="' .
274 $field_name . '" value="' .
275 $field_value['field_val'] . '" />';
276 break;
277 }
278 }
279
280 $model->setReplacements(array($key => $value));
281 Analog::log("adding dynamic replacement $key => $value", Analog::DEBUG);
282 }
283
284 return $model;
285 }
286
287 /**
288 * Store PDF
289 *
290 * @param string $path Path
291 *
292 * @return boolean
293 */
294 public function store($path)
295 {
296 if (file_exists($path) && is_dir($path) && is_writeable($path)) {
297 $this->path = $path . '/' . $this->filename;
298 $this->Output($this->path, 'F');
299 return true;
300 } else {
301 Analog::log(
302 __METHOD__ . ' ' . $path .
303 ' does not exists or is not a directory or is not writeable.',
304 Analog::ERROR
305 );
306 }
307 return false;
308 }
309
310 /**
311 * Get store path
312 *
313 * @return string
314 */
315 public function getPath()
316 {
317 return realpath($this->path);
318 }
319 }