3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for name badge layout.
21 class CRM_Badge_Form_Layout
extends CRM_Admin_Form
{
23 const FIELD_ROWCOUNT
= 6;
26 * Build the form object.
28 public function buildQuickForm() {
29 if ($this->_action
& CRM_Core_Action
::DELETE
) {
30 return parent
::buildQuickForm();
33 $config = CRM_Core_Config
::singleton();
34 $resources = CRM_Core_Resources
::singleton();
35 $resources->addSetting(
37 'kcfinderPath' => CRM_Utils_File
::addTrailingSlash(Civi
::paths()->getVariable('civicrm.packages', 'url'), '/'),
40 $resources->addScriptFile('civicrm', 'templates/CRM/Badge/Form/Layout.js', 1, 'html-header');
42 $this->applyFilter('__ALL__', 'trim');
44 $this->add('text', 'title', ts('Title'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_PrintLabel', 'title'), TRUE);
46 $labelStyle = CRM_Core_BAO_LabelFormat
::getList(TRUE, 'name_badge');
47 $this->add('select', 'label_format_name', ts('Label Format'), ['' => ts('- select -')] +
$labelStyle, TRUE);
49 $this->add('text', 'description', ts('Description'),
50 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
53 $contactTokens = CRM_Core_SelectValues
::contactTokens();
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'),
60 $participantTokens = CRM_Core_SelectValues
::participantTokens();
62 $tokens = array_merge($contactTokens, $eventTokens, $participantTokens);
65 $tokens = array_merge(['spacer' => ts('- spacer -')] +
$tokens);
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']);
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);
83 $this->assign('rowCount', $rowCount);
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);
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]);
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]);
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);
106 $this->add('checkbox', 'is_default', ts('Default?'));
107 $this->add('checkbox', 'is_active', ts('Enabled?'));
108 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
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');
120 'name' => ts('Save'),
125 'name' => ts('Save and Preview'),
129 'name' => ts('Cancel'),
135 * Set default values for the form.
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
, '[]')));
143 for ($i = 1; $i <= self
::FIELD_ROWCOUNT
; $i++
) {
144 $defaults['text_alignment'][$i] = "C";
148 if ($this->_action
== CRM_Core_Action
::DELETE
&& isset($defaults['title'])) {
149 $this->assign('delName', $defaults['title']);
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;
159 * Process the form submission.
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');
168 $params = $data = $this->exportValues();
170 unset($data['qfKey']);
171 $params['data'] = json_encode($data);
174 $params['id'] = $this->_id
;
177 // store the submitted values in an array
178 $badgeInfo = CRM_Badge_BAO_Layout
::create($params);
180 if (isset($params['_qf_Layout_refresh'])) {
181 $this->set('id', $badgeInfo->id
);
182 $params['badge_id'] = $badgeInfo->id
;
183 self
::buildPreview($params);
186 CRM_Core_Session
::setStatus(ts("The badge layout '%1' has been saved.",
187 [1 => $params['title']]
188 ), ts('Saved'), 'success');
193 * @param array $params
195 public function buildPreview(&$params) {
196 // get a max participant id
197 $participantID = CRM_Core_DAO
::singleValueQuery('select max(id) from civicrm_participant');
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');
205 $this->_single
= TRUE;
206 $this->_participantIds
= [$participantID];
207 $this->_componentClause
= " civicrm_participant.id = $participantID ";
209 CRM_Badge_BAO_Badge
::buildBadges($params, $this);