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