INFRA-132 - Change "else if" to "elseif"
[civicrm-core.git] / CRM / Badge / Form / Layout.php
CommitLineData
50998125
KJ
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
50998125 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
50998125
KJ
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
50998125
KJ
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for name badge layout
38 *
39 */
40class CRM_Badge_Form_Layout extends CRM_Admin_Form {
41
ea343109 42 const FIELD_ROWCOUNT = 6;
50998125 43 /**
c490a46a 44 * Build the form object
50998125 45 *
355ba699 46 * @return void
50998125
KJ
47 */
48 public function buildQuickForm() {
50998125 49 if ($this->_action & CRM_Core_Action::DELETE) {
a1f552a9 50 return parent::buildQuickForm();
50998125
KJ
51 }
52
d87c3006
KJ
53 $config = CRM_Core_Config::singleton();
54 $resources = CRM_Core_Resources::singleton();
55 $resources->addSetting(
56 array(
57 'kcfinderPath' => $config->userFrameworkResourceURL .'packages' .DIRECTORY_SEPARATOR
58 )
59 );
96ed17aa 60 $resources->addScriptFile('civicrm', 'templates/CRM/Badge/Form/Layout.js', 1, 'html-header');
d87c3006 61
50998125 62 $this->applyFilter('__ALL__', 'trim');
636f1cbe 63
3a872e59 64 $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'), TRUE);
636f1cbe 65
d34dbf05 66 $labelStyle = CRM_Core_BAO_LabelFormat::getList(TRUE, 'name_badge');
15cb4bd7 67 $this->add('select', 'label_format_name', ts('Label Format'), array('' => ts('- select -')) + $labelStyle, TRUE);
636f1cbe
KJ
68
69 $this->add('text', 'description', ts('Description'),
2df183e7 70 CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
636f1cbe
KJ
71
72 // get the tokens
012ee9a8 73 $contactTokens = CRM_Core_SelectValues::contactTokens();
f5ea9580 74 $eventTokens = array(
75 '{event.event_id}' => ts('Event ID'),
76 '{event.title}' => ts('Event Title'),
77 '{event.start_date}' => ts('Event Start Date'),
78 '{event.end_date}' => ts('Event End Date')
79 );
ea921622
KJ
80 $participantTokens = CRM_Core_SelectValues::participantTokens();
81
82 $tokens = array_merge($contactTokens, $eventTokens, $participantTokens);
636f1cbe
KJ
83 asort($tokens);
84
50f52554 85 $tokens = array_merge(array('spacer' => ts('- spacer -')) + $tokens);
86
636f1cbe 87 $fontSizes = CRM_Core_BAO_LabelFormat::getFontSizes();
0bc1ba89 88 $fontStyles = CRM_Core_BAO_LabelFormat::getFontStyles();
636f1cbe
KJ
89 $fontNames = CRM_Core_BAO_LabelFormat::getFontNames('name_badge');
90 $textAlignment = CRM_Core_BAO_LabelFormat::getTextAlignments();
609df1ed 91 $imageAlignment = $textAlignment;
e0ef6999 92 unset($imageAlignment['C']);
636f1cbe 93
ea343109 94 $rowCount = self::FIELD_ROWCOUNT;
3a872e59 95 for ( $i = 1; $i <= $rowCount; $i++ ) {
50f52554 96 $this->add('select', "token[$i]", ts('Token'), array('' => ts('- skip -')) + $tokens);
636f1cbe
KJ
97 $this->add('select', "font_name[$i]", ts('Font Name'), $fontNames);
98 $this->add('select', "font_size[$i]", ts('Font Size'), $fontSizes);
0bc1ba89 99 $this->add('select', "font_style[$i]", ts('Font Style'), $fontStyles);
636f1cbe
KJ
100 $this->add('select', "text_alignment[$i]", ts('Alignment'), $textAlignment);
101 }
102 $rowCount++;
103 $this->assign('rowCount', $rowCount);
104
f38395f7 105 $barcodeTypes = CRM_Core_SelectValues::getBarcodeTypes();
636f1cbe 106 $this->add('checkbox', 'add_barcode', ts('Barcode?'));
f38395f7 107 $this->add('select', "barcode_type", ts('Type'), $barcodeTypes);
636f1cbe
KJ
108 $this->add('select', "barcode_alignment", ts('Alignment'), $textAlignment);
109
3a872e59 110 $attributes = array('readonly' => TRUE);
b82c9857 111 $this->add('text', 'image_1', ts('Image (top left)'),
8367d0c5 112 $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
efdb8203 113 $this->add('text', 'width_image_1', ts('Width (mm)'), array('size' => 6));
114 $this->add('text', 'height_image_1', ts('Height (mm)'), array('size' => 6));
07d113b7 115
b82c9857 116 $this->add('text', 'image_2', ts('Image (top right)'),
8367d0c5 117 $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
efdb8203 118 $this->add('text', 'width_image_2', ts('Width (mm)'), array('size' => 6));
119 $this->add('text', 'height_image_2', ts('Height (mm)'), array('size' => 6));
d87c3006 120
609df1ed 121 $this->add('checkbox', 'show_participant_image', ts('Use Participant Image?'));
cf723d2e
M
122 $this->add('text', 'width_participant_image', ts('Width (mm)'), array('size' => 6));
123 $this->add('text', 'height_participant_image', ts('Height (mm)'), array('size' => 6));
609df1ed 124 $this->add('select', "alignment_participant_image", ts('Image Alignment'), $imageAlignment);
cf723d2e 125
50998125 126 $this->add('checkbox', 'is_default', ts('Default?'));
636f1cbe
KJ
127 $this->add('checkbox', 'is_active', ts('Enabled?'));
128 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
890a1fbb 129
50f52554 130 $this->addRule('width_image_1', ts('Enter valid width'), 'positiveInteger');
131 $this->addRule('width_image_2', ts('Enter valid width'), 'positiveInteger');
132 $this->addRule('height_image_1', ts('Enter valid height'), 'positiveInteger');
133 $this->addRule('height_image_2', ts('Enter valid height'), 'positiveInteger');
cf723d2e
M
134 $this->addRule('height_participant_image', ts('Enter valid height'), 'positiveInteger');
135 $this->addRule('width_participant_image', ts('Enter valid height'), 'positiveInteger');
6d4a64c8
KJ
136
137 $this->addButtons(array(
138 array(
139 'type' => 'next',
140 'name' => ts('Save'),
141 'isDefault' => TRUE,
142 ),
143 array(
144 'type' => 'refresh',
145 'name' => ts('Save and Preview'),
146 ),
147 array(
148 'type' => 'cancel',
149 'name' => ts('Cancel'),
150 ),
151 )
152 );
890a1fbb
KJ
153 }
154
636f1cbe 155 /**
c490a46a 156 * Set default values for the form. MobileProvider that in edit/view mode
636f1cbe
KJ
157 * the default values are retrieved from the database
158 *
636f1cbe 159 *
355ba699 160 * @return void
636f1cbe 161 */
00be9182 162 public function setDefaultValues() {
ea921622 163 if (isset($this->_id)) {
2df183e7
KJ
164 $defaults = array_merge($this->_values,
165 CRM_Badge_BAO_Layout::getDecodedData($this->_values['data']));
166 }
3c7ab739 167 else {
ea343109 168 for ($i = 1; $i <= self::FIELD_ROWCOUNT; $i++) {
3c7ab739
KJ
169 $defaults['text_alignment'][$i] = "C";
170 }
171 }
d87c3006 172
636f1cbe
KJ
173 if ($this->_action == CRM_Core_Action::DELETE && isset($defaults['title'])) {
174 $this->assign('delName', $defaults['title']);
175 }
176
177 // its ok if there is no element called is_active
178 $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1;
179
180 return $defaults;
50998125
KJ
181 }
182
183 /**
c490a46a 184 * Process the form submission
50998125 185 *
50998125 186 *
355ba699 187 * @return void
50998125
KJ
188 */
189 public function postProcess() {
636f1cbe
KJ
190 if ($this->_action & CRM_Core_Action::DELETE) {
191 CRM_Badge_BAO_Layout::del($this->_id);
192 CRM_Core_Session::setStatus(ts('Selected badge layout has been deleted.'), ts('Record Deleted'), 'success');
193 return;
194 }
195
6d4a64c8 196 $params = $data = $this->exportValues();
636f1cbe
KJ
197
198 unset($data['qfKey']);
199 $params['data'] = json_encode($data);
200
201 if ($this->_id) {
202 $params['id'] = $this->_id;
203 }
204
205 // store the submitted values in an array
6d4a64c8
KJ
206 $badgeInfo = CRM_Badge_BAO_Layout::create($params);
207
208 if (isset($params['_qf_Layout_refresh'])) {
1a502055 209 $this->set('id', $badgeInfo->id);
6d4a64c8
KJ
210 $params['badge_id'] = $badgeInfo->id;
211 self::buildPreview($params);
212 }
213 else {
214 CRM_Core_Session::setStatus(ts("The badge layout '%1' has been saved.",
215 array(1 => $params['title'])
216 ), ts('Saved'), 'success');
217 }
218 }
219
e0ef6999 220 /**
c490a46a 221 * @param array $params
e0ef6999 222 */
6d4a64c8
KJ
223 public function buildPreview(&$params) {
224 // get a max participant id
225 $participantID = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_participant');
226
1a502055
KJ
227 if (!$participantID) {
228 CRM_Core_Session::setStatus(ts('Preview requires at least one event and one participant record.
229 If you are just getting started, you can add a test participant record.'), ts('Preview Requirements'), 'alert');
230 return;
231 }
232
6d4a64c8
KJ
233 $this->_single = TRUE;
234 $this->_participantIds = array($participantID);
235 $this->_componentClause = " civicrm_participant.id = $participantID ";
636f1cbe 236
6d4a64c8 237 CRM_Badge_BAO_Badge::buildBadges($params, $this);
50998125
KJ
238 }
239}