Merge pull request #16403 from totten/master-relurl
[civicrm-core.git] / CRM / Badge / Form / Layout.php
CommitLineData
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 */
21class 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
KJ
51
52 // get the tokens
012ee9a8 53 $contactTokens = CRM_Core_SelectValues::contactTokens();
be2fb01f 54 $eventTokens = [
f5ea9580 55 '{event.event_id}' => ts('Event ID'),
56 '{event.title}' => ts('Event Title'),
57 '{event.start_date}' => ts('Event Start Date'),
21dfd5f5 58 '{event.end_date}' => ts('Event End Date'),
be2fb01f 59 ];
ea921622
KJ
60 $participantTokens = CRM_Core_SelectValues::participantTokens();
61
62 $tokens = array_merge($contactTokens, $eventTokens, $participantTokens);
636f1cbe
KJ
63 asort($tokens);
64
be2fb01f 65 $tokens = array_merge(['spacer' => ts('- spacer -')] + $tokens);
50f52554 66
636f1cbe 67 $fontSizes = CRM_Core_BAO_LabelFormat::getFontSizes();
0bc1ba89 68 $fontStyles = CRM_Core_BAO_LabelFormat::getFontStyles();
636f1cbe
KJ
69 $fontNames = CRM_Core_BAO_LabelFormat::getFontNames('name_badge');
70 $textAlignment = CRM_Core_BAO_LabelFormat::getTextAlignments();
609df1ed 71 $imageAlignment = $textAlignment;
e0ef6999 72 unset($imageAlignment['C']);
636f1cbe 73
ea343109 74 $rowCount = self::FIELD_ROWCOUNT;
481a74f4 75 for ($i = 1; $i <= $rowCount; $i++) {
be2fb01f 76 $this->add('select', "token[$i]", ts('Token'), ['' => ts('- skip -')] + $tokens);
636f1cbe
KJ
77 $this->add('select', "font_name[$i]", ts('Font Name'), $fontNames);
78 $this->add('select', "font_size[$i]", ts('Font Size'), $fontSizes);
0bc1ba89 79 $this->add('select', "font_style[$i]", ts('Font Style'), $fontStyles);
636f1cbe
KJ
80 $this->add('select', "text_alignment[$i]", ts('Alignment'), $textAlignment);
81 }
82 $rowCount++;
83 $this->assign('rowCount', $rowCount);
84
f38395f7 85 $barcodeTypes = CRM_Core_SelectValues::getBarcodeTypes();
636f1cbe 86 $this->add('checkbox', 'add_barcode', ts('Barcode?'));
f38395f7 87 $this->add('select', "barcode_type", ts('Type'), $barcodeTypes);
636f1cbe
KJ
88 $this->add('select', "barcode_alignment", ts('Alignment'), $textAlignment);
89
be2fb01f 90 $attributes = ['readonly' => TRUE];
b82c9857 91 $this->add('text', 'image_1', ts('Image (top left)'),
8367d0c5 92 $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
be2fb01f
CW
93 $this->add('text', 'width_image_1', ts('Width (mm)'), ['size' => 6]);
94 $this->add('text', 'height_image_1', ts('Height (mm)'), ['size' => 6]);
07d113b7 95
b82c9857 96 $this->add('text', 'image_2', ts('Image (top right)'),
8367d0c5 97 $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
be2fb01f
CW
98 $this->add('text', 'width_image_2', ts('Width (mm)'), ['size' => 6]);
99 $this->add('text', 'height_image_2', ts('Height (mm)'), ['size' => 6]);
d87c3006 100
609df1ed 101 $this->add('checkbox', 'show_participant_image', ts('Use Participant Image?'));
be2fb01f
CW
102 $this->add('text', 'width_participant_image', ts('Width (mm)'), ['size' => 6]);
103 $this->add('text', 'height_participant_image', ts('Height (mm)'), ['size' => 6]);
609df1ed 104 $this->add('select', "alignment_participant_image", ts('Image Alignment'), $imageAlignment);
cf723d2e 105
50998125 106 $this->add('checkbox', 'is_default', ts('Default?'));
636f1cbe
KJ
107 $this->add('checkbox', 'is_active', ts('Enabled?'));
108 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
890a1fbb 109
50f52554 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');
cf723d2e
M
114 $this->addRule('height_participant_image', ts('Enter valid height'), 'positiveInteger');
115 $this->addRule('width_participant_image', ts('Enter valid height'), 'positiveInteger');
6d4a64c8 116
be2fb01f 117 $this->addButtons([
5d4fcf54
TO
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 ]);
890a1fbb
KJ
132 }
133
636f1cbe 134 /**
ce064e4f 135 * Set default values for the form.
636f1cbe 136 */
00be9182 137 public function setDefaultValues() {
ea921622 138 if (isset($this->_id)) {
2df183e7 139 $defaults = array_merge($this->_values,
18cb89ba 140 CRM_Badge_BAO_Layout::getDecodedData(CRM_Utils_Array::value('data', $this->_values, '[]')));
2df183e7 141 }
3c7ab739 142 else {
ea343109 143 for ($i = 1; $i <= self::FIELD_ROWCOUNT; $i++) {
3c7ab739
KJ
144 $defaults['text_alignment'][$i] = "C";
145 }
146 }
d87c3006 147
636f1cbe
KJ
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;
50998125
KJ
156 }
157
158 /**
eceb18cc 159 * Process the form submission.
50998125
KJ
160 */
161 public function postProcess() {
636f1cbe
KJ
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
6d4a64c8 168 $params = $data = $this->exportValues();
636f1cbe
KJ
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
6d4a64c8
KJ
178 $badgeInfo = CRM_Badge_BAO_Layout::create($params);
179
180 if (isset($params['_qf_Layout_refresh'])) {
1a502055 181 $this->set('id', $badgeInfo->id);
6d4a64c8
KJ
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.",
be2fb01f 187 [1 => $params['title']]
6d4a64c8
KJ
188 ), ts('Saved'), 'success');
189 }
190 }
191
e0ef6999 192 /**
c490a46a 193 * @param array $params
e0ef6999 194 */
6d4a64c8
KJ
195 public function buildPreview(&$params) {
196 // get a max participant id
197 $participantID = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_participant');
198
1a502055
KJ
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
6d4a64c8 205 $this->_single = TRUE;
be2fb01f 206 $this->_participantIds = [$participantID];
6d4a64c8 207 $this->_componentClause = " civicrm_participant.id = $participantID ";
636f1cbe 208
6d4a64c8 209 CRM_Badge_BAO_Badge::buildBadges($params, $this);
50998125 210 }
96025800 211
50998125 212}