Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Badge / Form / Layout.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for name badge layout
38 *
39 */
40 class CRM_Badge_Form_Layout extends CRM_Admin_Form {
41
42 const FIELD_ROWCOUNT = 6;
43
44 /**
45 * Build the form object
46 *
47 * @return void
48 */
49 public function buildQuickForm() {
50 if ($this->_action & CRM_Core_Action::DELETE) {
51 return parent::buildQuickForm();
52 }
53
54 $config = CRM_Core_Config::singleton();
55 $resources = CRM_Core_Resources::singleton();
56 $resources->addSetting(
57 array(
58 'kcfinderPath' => $config->userFrameworkResourceURL . 'packages' . DIRECTORY_SEPARATOR,
59 )
60 );
61 $resources->addScriptFile('civicrm', 'templates/CRM/Badge/Form/Layout.js', 1, 'html-header');
62
63 $this->applyFilter('__ALL__', 'trim');
64
65 $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'), TRUE);
66
67 $labelStyle = CRM_Core_BAO_LabelFormat::getList(TRUE, 'name_badge');
68 $this->add('select', 'label_format_name', ts('Label Format'), array('' => ts('- select -')) + $labelStyle, TRUE);
69
70 $this->add('text', 'description', ts('Description'),
71 CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
72
73 // get the tokens
74 $contactTokens = CRM_Core_SelectValues::contactTokens();
75 $eventTokens = array(
76 '{event.event_id}' => ts('Event ID'),
77 '{event.title}' => ts('Event Title'),
78 '{event.start_date}' => ts('Event Start Date'),
79 '{event.end_date}' => ts('Event End Date'),
80 );
81 $participantTokens = CRM_Core_SelectValues::participantTokens();
82
83 $tokens = array_merge($contactTokens, $eventTokens, $participantTokens);
84 asort($tokens);
85
86 $tokens = array_merge(array('spacer' => ts('- spacer -')) + $tokens);
87
88 $fontSizes = CRM_Core_BAO_LabelFormat::getFontSizes();
89 $fontStyles = CRM_Core_BAO_LabelFormat::getFontStyles();
90 $fontNames = CRM_Core_BAO_LabelFormat::getFontNames('name_badge');
91 $textAlignment = CRM_Core_BAO_LabelFormat::getTextAlignments();
92 $imageAlignment = $textAlignment;
93 unset($imageAlignment['C']);
94
95 $rowCount = self::FIELD_ROWCOUNT;
96 for ($i = 1; $i <= $rowCount; $i++) {
97 $this->add('select', "token[$i]", ts('Token'), array('' => ts('- skip -')) + $tokens);
98 $this->add('select', "font_name[$i]", ts('Font Name'), $fontNames);
99 $this->add('select', "font_size[$i]", ts('Font Size'), $fontSizes);
100 $this->add('select', "font_style[$i]", ts('Font Style'), $fontStyles);
101 $this->add('select', "text_alignment[$i]", ts('Alignment'), $textAlignment);
102 }
103 $rowCount++;
104 $this->assign('rowCount', $rowCount);
105
106 $barcodeTypes = CRM_Core_SelectValues::getBarcodeTypes();
107 $this->add('checkbox', 'add_barcode', ts('Barcode?'));
108 $this->add('select', "barcode_type", ts('Type'), $barcodeTypes);
109 $this->add('select', "barcode_alignment", ts('Alignment'), $textAlignment);
110
111 $attributes = array('readonly' => TRUE);
112 $this->add('text', 'image_1', ts('Image (top left)'),
113 $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
114 $this->add('text', 'width_image_1', ts('Width (mm)'), array('size' => 6));
115 $this->add('text', 'height_image_1', ts('Height (mm)'), array('size' => 6));
116
117 $this->add('text', 'image_2', ts('Image (top right)'),
118 $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
119 $this->add('text', 'width_image_2', ts('Width (mm)'), array('size' => 6));
120 $this->add('text', 'height_image_2', ts('Height (mm)'), array('size' => 6));
121
122 $this->add('checkbox', 'show_participant_image', ts('Use Participant Image?'));
123 $this->add('text', 'width_participant_image', ts('Width (mm)'), array('size' => 6));
124 $this->add('text', 'height_participant_image', ts('Height (mm)'), array('size' => 6));
125 $this->add('select', "alignment_participant_image", ts('Image Alignment'), $imageAlignment);
126
127 $this->add('checkbox', 'is_default', ts('Default?'));
128 $this->add('checkbox', 'is_active', ts('Enabled?'));
129 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
130
131 $this->addRule('width_image_1', ts('Enter valid width'), 'positiveInteger');
132 $this->addRule('width_image_2', ts('Enter valid width'), 'positiveInteger');
133 $this->addRule('height_image_1', ts('Enter valid height'), 'positiveInteger');
134 $this->addRule('height_image_2', ts('Enter valid height'), 'positiveInteger');
135 $this->addRule('height_participant_image', ts('Enter valid height'), 'positiveInteger');
136 $this->addRule('width_participant_image', ts('Enter valid height'), 'positiveInteger');
137
138 $this->addButtons(array(
139 array(
140 'type' => 'next',
141 'name' => ts('Save'),
142 'isDefault' => TRUE,
143 ),
144 array(
145 'type' => 'refresh',
146 'name' => ts('Save and Preview'),
147 ),
148 array(
149 'type' => 'cancel',
150 'name' => ts('Cancel'),
151 ),
152 )
153 );
154 }
155
156 /**
157 * Set default values for the form. MobileProvider that in edit/view mode
158 * the default values are retrieved from the database
159 *
160 *
161 * @return void
162 */
163 public function setDefaultValues() {
164 if (isset($this->_id)) {
165 $defaults = array_merge($this->_values,
166 CRM_Badge_BAO_Layout::getDecodedData($this->_values['data']));
167 }
168 else {
169 for ($i = 1; $i <= self::FIELD_ROWCOUNT; $i++) {
170 $defaults['text_alignment'][$i] = "C";
171 }
172 }
173
174 if ($this->_action == CRM_Core_Action::DELETE && isset($defaults['title'])) {
175 $this->assign('delName', $defaults['title']);
176 }
177
178 // its ok if there is no element called is_active
179 $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1;
180
181 return $defaults;
182 }
183
184 /**
185 * Process the form submission
186 *
187 *
188 * @return void
189 */
190 public function postProcess() {
191 if ($this->_action & CRM_Core_Action::DELETE) {
192 CRM_Badge_BAO_Layout::del($this->_id);
193 CRM_Core_Session::setStatus(ts('Selected badge layout has been deleted.'), ts('Record Deleted'), 'success');
194 return;
195 }
196
197 $params = $data = $this->exportValues();
198
199 unset($data['qfKey']);
200 $params['data'] = json_encode($data);
201
202 if ($this->_id) {
203 $params['id'] = $this->_id;
204 }
205
206 // store the submitted values in an array
207 $badgeInfo = CRM_Badge_BAO_Layout::create($params);
208
209 if (isset($params['_qf_Layout_refresh'])) {
210 $this->set('id', $badgeInfo->id);
211 $params['badge_id'] = $badgeInfo->id;
212 self::buildPreview($params);
213 }
214 else {
215 CRM_Core_Session::setStatus(ts("The badge layout '%1' has been saved.",
216 array(1 => $params['title'])
217 ), ts('Saved'), 'success');
218 }
219 }
220
221 /**
222 * @param array $params
223 */
224 public function buildPreview(&$params) {
225 // get a max participant id
226 $participantID = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_participant');
227
228 if (!$participantID) {
229 CRM_Core_Session::setStatus(ts('Preview requires at least one event and one participant record.
230 If you are just getting started, you can add a test participant record.'), ts('Preview Requirements'), 'alert');
231 return;
232 }
233
234 $this->_single = TRUE;
235 $this->_participantIds = array($participantID);
236 $this->_componentClause = " civicrm_participant.id = $participantID ";
237
238 CRM_Badge_BAO_Badge::buildBadges($params, $this);
239 }
240 }