Merge pull request #18506 from totten/master-bundle-array
[civicrm-core.git] / CRM / Admin / Form / LabelFormats.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright (C) 2011 Marty Wright |
7 | Licensed to CiviCRM under the Academic Free License version 3.0. |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC https://civicrm.org/licensing
33 */
34
35 /**
36 * This class generates form components for Label Format Settings.
37 */
38 class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form {
39
40 /**
41 * Label Format ID.
42 * @var int
43 */
44 public $_id = NULL;
45
46 /**
47 * Group name, label format or name badge
48 * @var string
49 */
50 protected $_group = NULL;
51
52 public function preProcess() {
53 $this->_id = $this->get('id');
54 $this->_group = CRM_Utils_Request::retrieve('group', 'String', $this, FALSE, 'label_format');
55 $this->_values = [];
56 if (isset($this->_id)) {
57 $params = ['id' => $this->_id];
58 CRM_Core_BAO_LabelFormat::retrieve($params, $this->_values, $this->_group);
59 }
60 }
61
62 /**
63 * Build the form object.
64 */
65 public function buildQuickForm() {
66 parent::buildQuickForm();
67
68 if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::COPY)) {
69 $formatName = CRM_Core_BAO_LabelFormat::getFieldValue('CRM_Core_BAO_LabelFormat', $this->_id, 'label');
70 $this->assign('formatName', $formatName);
71 return;
72 }
73
74 $disabled = [];
75 $required = TRUE;
76 $is_reserved = $this->_id ? CRM_Core_BAO_LabelFormat::getFieldValue('CRM_Core_BAO_LabelFormat', $this->_id, 'is_reserved') : FALSE;
77 if ($is_reserved) {
78 $disabled['disabled'] = 'disabled';
79 $required = FALSE;
80 }
81
82 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat');
83 $this->add('text', 'label', ts('Name'), $attributes['label'] + $disabled, $required);
84 $this->add('text', 'description', ts('Description'), ['size' => CRM_Utils_Type::HUGE]);
85 $this->add('checkbox', 'is_default', ts('Is this Label Format the default?'));
86
87 // currently we support only mailing label creation, hence comment below code
88 /*
89 $options = array(
90 'label_format' => ts('Mailing Label'),
91 'name_badge' => ts('Name Badge'),
92 );
93
94 $labelType = $this->addRadio('label_type', ts('Used For'), $options, null, '&nbsp;&nbsp;');
95
96 if ($this->_action != CRM_Core_Action::ADD) {
97 $labelType->freeze();
98 }
99 */
100
101 $this->add('select', 'paper_size', ts('Sheet Size'),
102 [
103 0 => ts('- default -'),
104 ] + CRM_Core_BAO_PaperSize::getList(TRUE), FALSE,
105 [
106 'onChange' => "selectPaper( this.value );",
107 ] + $disabled
108 );
109 $this->add('static', 'paper_dimensions', NULL, ts('Sheet Size (w x h)'));
110 $this->add('select', 'orientation', ts('Orientation'), CRM_Core_BAO_LabelFormat::getPageOrientations(), FALSE,
111 [
112 'onChange' => "updatePaperDimensions();",
113 ] + $disabled
114 );
115 $this->add('select', 'font_name', ts('Font Name'), CRM_Core_BAO_LabelFormat::getFontNames($this->_group));
116 $this->add('select', 'font_size', ts('Font Size'), CRM_Core_BAO_LabelFormat::getFontSizes());
117 $this->add('static', 'font_style', ts('Font Style'));
118 $this->add('checkbox', 'bold', ts('Bold'));
119 $this->add('checkbox', 'italic', ts('Italic'));
120 $this->add('select', 'metric', ts('Unit of Measure'), CRM_Core_BAO_LabelFormat::getUnits(), FALSE,
121 ['onChange' => "selectMetric( this.value );"]
122 );
123 $this->add('text', 'width', ts('Label Width'), ['size' => 8, 'maxlength' => 8] + $disabled, $required);
124 $this->add('text', 'height', ts('Label Height'), ['size' => 8, 'maxlength' => 8] + $disabled, $required);
125 $this->add('text', 'NX', ts('Labels Per Row'), ['size' => 3, 'maxlength' => 3] + $disabled, $required);
126 $this->add('text', 'NY', ts('Labels Per Column'), ['size' => 3, 'maxlength' => 3] + $disabled, $required);
127 $this->add('text', 'tMargin', ts('Top Margin'), ['size' => 8, 'maxlength' => 8] + $disabled, $required);
128 $this->add('text', 'lMargin', ts('Left Margin'), ['size' => 8, 'maxlength' => 8] + $disabled, $required);
129 $this->add('text', 'SpaceX', ts('Horizontal Spacing'), ['size' => 8, 'maxlength' => 8] + $disabled, $required);
130 $this->add('text', 'SpaceY', ts('Vertical Spacing'), ['size' => 8, 'maxlength' => 8] + $disabled, $required);
131 $this->add('text', 'lPadding', ts('Left Padding'), ['size' => 8, 'maxlength' => 8], $required);
132 $this->add('text', 'tPadding', ts('Top Padding'), ['size' => 8, 'maxlength' => 8], $required);
133 $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat', 'weight'), TRUE);
134
135 $this->addRule('label', ts('Name already exists in Database.'), 'objectExists', [
136 'CRM_Core_BAO_LabelFormat',
137 $this->_id,
138 ]);
139 $this->addRule('NX', ts('Please enter a valid integer.'), 'integer');
140 $this->addRule('NY', ts('Please enter a valid integer.'), 'integer');
141 $this->addRule('tMargin', ts('Please enter a valid number.'), 'numeric');
142 $this->addRule('lMargin', ts('Please enter a valid number.'), 'numeric');
143 $this->addRule('SpaceX', ts('Please enter a valid number.'), 'numeric');
144 $this->addRule('SpaceY', ts('Please enter a valid number.'), 'numeric');
145 $this->addRule('lPadding', ts('Please enter a valid number.'), 'numeric');
146 $this->addRule('tPadding', ts('Please enter a valid number.'), 'numeric');
147 $this->addRule('width', ts('Please enter a valid number.'), 'numeric');
148 $this->addRule('height', ts('Please enter a valid number.'), 'numeric');
149 $this->addRule('weight', ts('Please enter a valid integer.'), 'integer');
150 }
151
152 /**
153 * @return int
154 */
155 public function setDefaultValues() {
156 if ($this->_action & CRM_Core_Action::ADD) {
157 $defaults['weight'] = CRM_Utils_Array::value('weight', CRM_Core_BAO_LabelFormat::getDefaultValues($this->_group), 0);
158 $defaults['font_name'] = CRM_Utils_Array::value('font-name', CRM_Core_BAO_LabelFormat::getDefaultValues($this->_group), '');
159 }
160 else {
161 $defaults = $this->_values;
162 // Convert field names that are illegal PHP/SMARTY variable names
163 $defaults['paper_size'] = $defaults['paper-size'];
164 unset($defaults['paper-size']);
165 $defaults['font_name'] = $defaults['font-name'];
166 unset($defaults['font-name']);
167 $defaults['font_size'] = $defaults['font-size'];
168 unset($defaults['font-size']);
169
170 $defaults['bold'] = (stripos($defaults['font-style'], 'B') !== FALSE);
171 $defaults['italic'] = (stripos($defaults['font-style'], 'I') !== FALSE);
172 unset($defaults['font-style']);
173 }
174
175 $defaults['label_type'] = $this->_group;
176 return $defaults;
177 }
178
179 /**
180 * Process the form submission.
181 */
182 public function postProcess() {
183 if ($this->_action & CRM_Core_Action::DELETE) {
184 // delete Label Format
185 CRM_Core_BAO_LabelFormat::del($this->_id, $this->_group);
186 CRM_Core_Session::setStatus(ts('Selected Label Format has been deleted.'), ts('Record Deleted'), 'success');
187 return;
188 }
189 if ($this->_action & CRM_Core_Action::COPY) {
190 // make a copy of the Label Format
191 $labelFormat = CRM_Core_BAO_LabelFormat::getById($this->_id, $this->_group);
192 $newlabel = ts('Copy of %1', [1 => $labelFormat['label']]);
193
194 $list = CRM_Core_BAO_LabelFormat::getList(TRUE, $this->_group);
195 $count = 1;
196
197 while (in_array($newlabel, $list)) {
198 $count++;
199 $newlabel = ts('Copy %1 of %2', [1 => $count, 2 => $labelFormat['label']]);
200 }
201
202 $labelFormat['label'] = $newlabel;
203 $labelFormat['grouping'] = CRM_Core_BAO_LabelFormat::customGroupName();
204 $labelFormat['is_default'] = 0;
205 $labelFormat['is_reserved'] = 0;
206
207 $bao = new CRM_Core_BAO_LabelFormat();
208 $bao->saveLabelFormat($labelFormat, NULL, $this->_group);
209 CRM_Core_Session::setStatus(ts('%1 has been created.', [1 => $labelFormat['label']]), ts('Saved'), 'success');
210 return;
211 }
212
213 $values = $this->controller->exportValues($this->getName());
214
215 // since we currently support only mailing label format
216 $values['label_type'] = 'label_format';
217
218 $values['is_default'] = isset($values['is_default']);
219
220 // Restore field names that were converted because they are illegal PHP/SMARTY variable names
221 if (isset($values['paper_size'])) {
222 $values['paper-size'] = $values['paper_size'];
223 unset($values['paper_size']);
224 }
225 if (isset($values['font_name'])) {
226 $values['font-name'] = $values['font_name'];
227 unset($values['font_name']);
228 }
229 if (isset($values['font_size'])) {
230 $values['font-size'] = $values['font_size'];
231 unset($values['font_size']);
232 }
233
234 $style = '';
235 if (isset($values['bold'])) {
236 $style .= 'B';
237 }
238 if (isset($values['italic'])) {
239 $style .= 'I';
240 }
241 $values['font-style'] = $style;
242
243 $bao = new CRM_Core_BAO_LabelFormat();
244 $bao->saveLabelFormat($values, $this->_id, $values['label_type']);
245
246 $status = ts('Your new Label Format titled <strong>%1</strong> has been saved.', [1 => $values['label']]);
247 if ($this->_action & CRM_Core_Action::UPDATE) {
248 $status = ts('Your Label Format titled <strong>%1</strong> has been updated.', [1 => $values['label']]);
249 }
250 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
251 }
252
253 }