Commit | Line | Data |
---|---|---|
50998125 KJ |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
50998125 | 5 | | | |
bc77d7c0 TO |
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 | | |
50998125 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
50998125 KJ |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
50998125 KJ |
16 | */ |
17 | ||
18 | /** | |
ce064e4f | 19 | * This class generates form components for name badge layout. |
50998125 KJ |
20 | */ |
21 | class CRM_Badge_Form_Layout extends CRM_Admin_Form { | |
22 | ||
ea343109 | 23 | const FIELD_ROWCOUNT = 6; |
353ffa53 | 24 | |
50998125 | 25 | /** |
eceb18cc | 26 | * Build the form object. |
50998125 KJ |
27 | */ |
28 | public function buildQuickForm() { | |
50998125 | 29 | if ($this->_action & CRM_Core_Action::DELETE) { |
a1f552a9 | 30 | return parent::buildQuickForm(); |
50998125 KJ |
31 | } |
32 | ||
d87c3006 KJ |
33 | $config = CRM_Core_Config::singleton(); |
34 | $resources = CRM_Core_Resources::singleton(); | |
35 | $resources->addSetting( | |
be2fb01f | 36 | [ |
6b51b85c | 37 | 'kcfinderPath' => CRM_Utils_File::addTrailingSlash(Civi::paths()->getVariable('civicrm.packages', 'url'), '/'), |
be2fb01f | 38 | ] |
d87c3006 | 39 | ); |
96ed17aa | 40 | $resources->addScriptFile('civicrm', 'templates/CRM/Badge/Form/Layout.js', 1, 'html-header'); |
d87c3006 | 41 | |
50998125 | 42 | $this->applyFilter('__ALL__', 'trim'); |
636f1cbe | 43 | |
3a872e59 | 44 | $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'), TRUE); |
636f1cbe | 45 | |
d34dbf05 | 46 | $labelStyle = CRM_Core_BAO_LabelFormat::getList(TRUE, 'name_badge'); |
be2fb01f | 47 | $this->add('select', 'label_format_name', ts('Label Format'), ['' => ts('- select -')] + $labelStyle, TRUE); |
636f1cbe KJ |
48 | |
49 | $this->add('text', 'description', ts('Description'), | |
2df183e7 | 50 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title')); |
636f1cbe | 51 | |
873bfeb5 EM |
52 | // get the tokens - at the point of rendering the token processor is used so |
53 | // the only reason for this cut-down set of tokens is UI on this | |
54 | // screen and / or historical. | |
012ee9a8 | 55 | $contactTokens = CRM_Core_SelectValues::contactTokens(); |
be2fb01f | 56 | $eventTokens = [ |
f5ea9580 | 57 | '{event.event_id}' => ts('Event ID'), |
58 | '{event.title}' => ts('Event Title'), | |
873bfeb5 EM |
59 | // This layout selection is day + month eg October 27th |
60 | // obviously someone felt year was not logical for dates. | |
61 | '{event.start_date|crmDate:"%B %E%f"}' => ts('Event Start Date'), | |
62 | '{event.end_date|crmDate:"%B %E%f"}' => ts('Event End Date'), | |
be2fb01f | 63 | ]; |
ea921622 KJ |
64 | $participantTokens = CRM_Core_SelectValues::participantTokens(); |
65 | ||
66 | $tokens = array_merge($contactTokens, $eventTokens, $participantTokens); | |
636f1cbe KJ |
67 | asort($tokens); |
68 | ||
be2fb01f | 69 | $tokens = array_merge(['spacer' => ts('- spacer -')] + $tokens); |
50f52554 | 70 | |
636f1cbe | 71 | $fontSizes = CRM_Core_BAO_LabelFormat::getFontSizes(); |
0bc1ba89 | 72 | $fontStyles = CRM_Core_BAO_LabelFormat::getFontStyles(); |
636f1cbe KJ |
73 | $fontNames = CRM_Core_BAO_LabelFormat::getFontNames('name_badge'); |
74 | $textAlignment = CRM_Core_BAO_LabelFormat::getTextAlignments(); | |
609df1ed | 75 | $imageAlignment = $textAlignment; |
e0ef6999 | 76 | unset($imageAlignment['C']); |
636f1cbe | 77 | |
ea343109 | 78 | $rowCount = self::FIELD_ROWCOUNT; |
481a74f4 | 79 | for ($i = 1; $i <= $rowCount; $i++) { |
be2fb01f | 80 | $this->add('select', "token[$i]", ts('Token'), ['' => ts('- skip -')] + $tokens); |
636f1cbe KJ |
81 | $this->add('select', "font_name[$i]", ts('Font Name'), $fontNames); |
82 | $this->add('select', "font_size[$i]", ts('Font Size'), $fontSizes); | |
0bc1ba89 | 83 | $this->add('select', "font_style[$i]", ts('Font Style'), $fontStyles); |
636f1cbe KJ |
84 | $this->add('select', "text_alignment[$i]", ts('Alignment'), $textAlignment); |
85 | } | |
86 | $rowCount++; | |
87 | $this->assign('rowCount', $rowCount); | |
88 | ||
f38395f7 | 89 | $barcodeTypes = CRM_Core_SelectValues::getBarcodeTypes(); |
636f1cbe | 90 | $this->add('checkbox', 'add_barcode', ts('Barcode?')); |
f38395f7 | 91 | $this->add('select', "barcode_type", ts('Type'), $barcodeTypes); |
636f1cbe KJ |
92 | $this->add('select', "barcode_alignment", ts('Alignment'), $textAlignment); |
93 | ||
be2fb01f | 94 | $attributes = ['readonly' => TRUE]; |
b82c9857 | 95 | $this->add('text', 'image_1', ts('Image (top left)'), |
8367d0c5 | 96 | $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title')); |
be2fb01f CW |
97 | $this->add('text', 'width_image_1', ts('Width (mm)'), ['size' => 6]); |
98 | $this->add('text', 'height_image_1', ts('Height (mm)'), ['size' => 6]); | |
07d113b7 | 99 | |
b82c9857 | 100 | $this->add('text', 'image_2', ts('Image (top right)'), |
8367d0c5 | 101 | $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title')); |
be2fb01f CW |
102 | $this->add('text', 'width_image_2', ts('Width (mm)'), ['size' => 6]); |
103 | $this->add('text', 'height_image_2', ts('Height (mm)'), ['size' => 6]); | |
d87c3006 | 104 | |
609df1ed | 105 | $this->add('checkbox', 'show_participant_image', ts('Use Participant Image?')); |
be2fb01f CW |
106 | $this->add('text', 'width_participant_image', ts('Width (mm)'), ['size' => 6]); |
107 | $this->add('text', 'height_participant_image', ts('Height (mm)'), ['size' => 6]); | |
609df1ed | 108 | $this->add('select', "alignment_participant_image", ts('Image Alignment'), $imageAlignment); |
cf723d2e | 109 | |
50998125 | 110 | $this->add('checkbox', 'is_default', ts('Default?')); |
636f1cbe KJ |
111 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
112 | $this->add('checkbox', 'is_reserved', ts('Reserved?')); | |
890a1fbb | 113 | |
50f52554 | 114 | $this->addRule('width_image_1', ts('Enter valid width'), 'positiveInteger'); |
115 | $this->addRule('width_image_2', ts('Enter valid width'), 'positiveInteger'); | |
116 | $this->addRule('height_image_1', ts('Enter valid height'), 'positiveInteger'); | |
117 | $this->addRule('height_image_2', ts('Enter valid height'), 'positiveInteger'); | |
cf723d2e M |
118 | $this->addRule('height_participant_image', ts('Enter valid height'), 'positiveInteger'); |
119 | $this->addRule('width_participant_image', ts('Enter valid height'), 'positiveInteger'); | |
6d4a64c8 | 120 | |
be2fb01f | 121 | $this->addButtons([ |
5d4fcf54 TO |
122 | [ |
123 | 'type' => 'next', | |
124 | 'name' => ts('Save'), | |
125 | 'isDefault' => TRUE, | |
126 | ], | |
127 | [ | |
128 | 'type' => 'refresh', | |
129 | 'name' => ts('Save and Preview'), | |
130 | ], | |
131 | [ | |
132 | 'type' => 'cancel', | |
133 | 'name' => ts('Cancel'), | |
134 | ], | |
135 | ]); | |
890a1fbb KJ |
136 | } |
137 | ||
636f1cbe | 138 | /** |
ce064e4f | 139 | * Set default values for the form. |
636f1cbe | 140 | */ |
00be9182 | 141 | public function setDefaultValues() { |
ea921622 | 142 | if (isset($this->_id)) { |
2df183e7 | 143 | $defaults = array_merge($this->_values, |
18cb89ba | 144 | CRM_Badge_BAO_Layout::getDecodedData(CRM_Utils_Array::value('data', $this->_values, '[]'))); |
2df183e7 | 145 | } |
3c7ab739 | 146 | else { |
ea343109 | 147 | for ($i = 1; $i <= self::FIELD_ROWCOUNT; $i++) { |
3c7ab739 KJ |
148 | $defaults['text_alignment'][$i] = "C"; |
149 | } | |
150 | } | |
d87c3006 | 151 | |
636f1cbe KJ |
152 | if ($this->_action == CRM_Core_Action::DELETE && isset($defaults['title'])) { |
153 | $this->assign('delName', $defaults['title']); | |
154 | } | |
155 | ||
156 | // its ok if there is no element called is_active | |
157 | $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1; | |
158 | ||
159 | return $defaults; | |
50998125 KJ |
160 | } |
161 | ||
162 | /** | |
eceb18cc | 163 | * Process the form submission. |
50998125 KJ |
164 | */ |
165 | public function postProcess() { | |
636f1cbe KJ |
166 | if ($this->_action & CRM_Core_Action::DELETE) { |
167 | CRM_Badge_BAO_Layout::del($this->_id); | |
168 | CRM_Core_Session::setStatus(ts('Selected badge layout has been deleted.'), ts('Record Deleted'), 'success'); | |
169 | return; | |
170 | } | |
171 | ||
6d4a64c8 | 172 | $params = $data = $this->exportValues(); |
636f1cbe KJ |
173 | |
174 | unset($data['qfKey']); | |
175 | $params['data'] = json_encode($data); | |
176 | ||
177 | if ($this->_id) { | |
178 | $params['id'] = $this->_id; | |
179 | } | |
180 | ||
181 | // store the submitted values in an array | |
6d4a64c8 KJ |
182 | $badgeInfo = CRM_Badge_BAO_Layout::create($params); |
183 | ||
184 | if (isset($params['_qf_Layout_refresh'])) { | |
1a502055 | 185 | $this->set('id', $badgeInfo->id); |
6d4a64c8 KJ |
186 | $params['badge_id'] = $badgeInfo->id; |
187 | self::buildPreview($params); | |
188 | } | |
189 | else { | |
190 | CRM_Core_Session::setStatus(ts("The badge layout '%1' has been saved.", | |
be2fb01f | 191 | [1 => $params['title']] |
6d4a64c8 KJ |
192 | ), ts('Saved'), 'success'); |
193 | } | |
194 | } | |
195 | ||
e0ef6999 | 196 | /** |
c490a46a | 197 | * @param array $params |
e0ef6999 | 198 | */ |
6d4a64c8 KJ |
199 | public function buildPreview(&$params) { |
200 | // get a max participant id | |
201 | $participantID = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_participant'); | |
202 | ||
1a502055 KJ |
203 | if (!$participantID) { |
204 | CRM_Core_Session::setStatus(ts('Preview requires at least one event and one participant record. | |
205 | If you are just getting started, you can add a test participant record.'), ts('Preview Requirements'), 'alert'); | |
206 | return; | |
207 | } | |
208 | ||
6d4a64c8 | 209 | $this->_single = TRUE; |
be2fb01f | 210 | $this->_participantIds = [$participantID]; |
6d4a64c8 | 211 | $this->_componentClause = " civicrm_participant.id = $participantID "; |
636f1cbe | 212 | |
6d4a64c8 | 213 | CRM_Badge_BAO_Badge::buildBadges($params, $this); |
50998125 | 214 | } |
96025800 | 215 | |
50998125 | 216 | } |