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