| 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' => CRM_Utils_File::addTrailingSlash(Civi::paths()->getVariable('civicrm.packages', 'url'), '/'), |
| 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 - 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. |
| 55 | $contactTokens = CRM_Core_SelectValues::contactTokens(); |
| 56 | $eventTokens = [ |
| 57 | '{event.event_id}' => ts('Event ID'), |
| 58 | '{event.title}' => ts('Event Title'), |
| 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'), |
| 63 | ]; |
| 64 | $participantTokens = CRM_Core_SelectValues::participantTokens(); |
| 65 | |
| 66 | $tokens = array_merge($contactTokens, $eventTokens, $participantTokens); |
| 67 | asort($tokens); |
| 68 | |
| 69 | $tokens = array_merge(['spacer' => ts('- spacer -')] + $tokens); |
| 70 | |
| 71 | $fontSizes = CRM_Core_BAO_LabelFormat::getFontSizes(); |
| 72 | $fontStyles = CRM_Core_BAO_LabelFormat::getFontStyles(); |
| 73 | $fontNames = CRM_Core_BAO_LabelFormat::getFontNames('name_badge'); |
| 74 | $textAlignment = CRM_Core_BAO_LabelFormat::getTextAlignments(); |
| 75 | $imageAlignment = $textAlignment; |
| 76 | unset($imageAlignment['C']); |
| 77 | |
| 78 | $rowCount = self::FIELD_ROWCOUNT; |
| 79 | for ($i = 1; $i <= $rowCount; $i++) { |
| 80 | $this->add('select', "token[$i]", ts('Token'), ['' => ts('- skip -')] + $tokens); |
| 81 | $this->add('select', "font_name[$i]", ts('Font Name'), $fontNames); |
| 82 | $this->add('select', "font_size[$i]", ts('Font Size'), $fontSizes); |
| 83 | $this->add('select', "font_style[$i]", ts('Font Style'), $fontStyles); |
| 84 | $this->add('select', "text_alignment[$i]", ts('Alignment'), $textAlignment); |
| 85 | } |
| 86 | $rowCount++; |
| 87 | $this->assign('rowCount', $rowCount); |
| 88 | |
| 89 | $barcodeTypes = CRM_Core_SelectValues::getBarcodeTypes(); |
| 90 | $this->add('checkbox', 'add_barcode', ts('Barcode?')); |
| 91 | $this->add('select', "barcode_type", ts('Type'), $barcodeTypes); |
| 92 | $this->add('select', "barcode_alignment", ts('Alignment'), $textAlignment); |
| 93 | |
| 94 | $attributes = ['readonly' => TRUE]; |
| 95 | $this->add('text', 'image_1', ts('Image (top left)'), |
| 96 | $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title')); |
| 97 | $this->add('text', 'width_image_1', ts('Width (mm)'), ['size' => 6]); |
| 98 | $this->add('text', 'height_image_1', ts('Height (mm)'), ['size' => 6]); |
| 99 | |
| 100 | $this->add('text', 'image_2', ts('Image (top right)'), |
| 101 | $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title')); |
| 102 | $this->add('text', 'width_image_2', ts('Width (mm)'), ['size' => 6]); |
| 103 | $this->add('text', 'height_image_2', ts('Height (mm)'), ['size' => 6]); |
| 104 | |
| 105 | $this->add('checkbox', 'show_participant_image', ts('Use Participant Image?')); |
| 106 | $this->add('text', 'width_participant_image', ts('Width (mm)'), ['size' => 6]); |
| 107 | $this->add('text', 'height_participant_image', ts('Height (mm)'), ['size' => 6]); |
| 108 | $this->add('select', "alignment_participant_image", ts('Image Alignment'), $imageAlignment); |
| 109 | |
| 110 | $this->add('checkbox', 'is_default', ts('Default?')); |
| 111 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
| 112 | $this->add('checkbox', 'is_reserved', ts('Reserved?')); |
| 113 | |
| 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'); |
| 118 | $this->addRule('height_participant_image', ts('Enter valid height'), 'positiveInteger'); |
| 119 | $this->addRule('width_participant_image', ts('Enter valid height'), 'positiveInteger'); |
| 120 | |
| 121 | $this->addButtons([ |
| 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 | ]); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Set default values for the form. |
| 140 | */ |
| 141 | public function setDefaultValues() { |
| 142 | if (isset($this->_id)) { |
| 143 | $defaults = array_merge($this->_values, |
| 144 | CRM_Badge_BAO_Layout::getDecodedData(CRM_Utils_Array::value('data', $this->_values, '[]'))); |
| 145 | } |
| 146 | else { |
| 147 | for ($i = 1; $i <= self::FIELD_ROWCOUNT; $i++) { |
| 148 | $defaults['text_alignment'][$i] = "C"; |
| 149 | } |
| 150 | } |
| 151 | |
| 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; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Process the form submission. |
| 164 | */ |
| 165 | public function postProcess() { |
| 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 | |
| 172 | $params = $data = $this->exportValues(); |
| 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 |
| 182 | $badgeInfo = CRM_Badge_BAO_Layout::create($params); |
| 183 | |
| 184 | if (isset($params['_qf_Layout_refresh'])) { |
| 185 | $this->set('id', $badgeInfo->id); |
| 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.", |
| 191 | [1 => $params['title']] |
| 192 | ), ts('Saved'), 'success'); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @param array $params |
| 198 | */ |
| 199 | public function buildPreview(&$params) { |
| 200 | // get a max participant id |
| 201 | $participantID = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_participant'); |
| 202 | |
| 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 | |
| 209 | $this->_single = TRUE; |
| 210 | $this->_participantIds = [$participantID]; |
| 211 | $this->_componentClause = " civicrm_participant.id = $participantID "; |
| 212 | |
| 213 | CRM_Badge_BAO_Badge::buildBadges($params, $this); |
| 214 | } |
| 215 | |
| 216 | } |