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