Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 27 | */ |
6a488035 TO |
28 | |
29 | /** | |
30 | * | |
31 | * @package CRM | |
8c9251b3 | 32 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
33 | */ |
34 | ||
35 | /** | |
b6c94f42 | 36 | * This class generates form components for Label Format Settings. |
6a488035 TO |
37 | */ |
38 | class CRM_Admin_Form_LabelFormats extends CRM_Admin_Form { | |
39 | ||
40 | /** | |
eceb18cc | 41 | * Label Format ID. |
6a488035 TO |
42 | */ |
43 | protected $_id = NULL; | |
44 | ||
eaf5045f KJ |
45 | /** |
46 | * Group name, label format or name badge | |
47 | */ | |
48 | protected $_group = NULL; | |
49 | ||
00be9182 | 50 | public function preProcess() { |
eaf5045f KJ |
51 | $this->_id = $this->get('id'); |
52 | $this->_group = CRM_Utils_Request::retrieve('group', 'String', $this, FALSE, 'label_format'); | |
53 | $this->_values = array(); | |
54 | if (isset($this->_id)) { | |
55 | $params = array('id' => $this->_id); | |
56 | CRM_Core_BAO_LabelFormat::retrieve($params, $this->_values, $this->_group); | |
57 | } | |
58 | } | |
59 | ||
6a488035 | 60 | /** |
eceb18cc | 61 | * Build the form object. |
6a488035 TO |
62 | */ |
63 | public function buildQuickForm() { | |
64 | parent::buildQuickForm(); | |
65 | ||
66 | if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::COPY)) { | |
67 | $formatName = CRM_Core_BAO_LabelFormat::getFieldValue('CRM_Core_BAO_LabelFormat', $this->_id, 'label'); | |
68 | $this->assign('formatName', $formatName); | |
69 | return; | |
70 | } | |
71 | ||
eaf5045f KJ |
72 | $disabled = array(); |
73 | $required = TRUE; | |
6a488035 TO |
74 | $is_reserved = $this->_id ? CRM_Core_BAO_LabelFormat::getFieldValue('CRM_Core_BAO_LabelFormat', $this->_id, 'is_reserved') : FALSE; |
75 | if ($is_reserved) { | |
76 | $disabled['disabled'] = 'disabled'; | |
77 | $required = FALSE; | |
78 | } | |
79 | ||
80 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat'); | |
81 | $this->add('text', 'label', ts('Name'), $attributes['label'] + $disabled, $required); | |
82 | $this->add('text', 'description', ts('Description'), array('size' => CRM_Utils_Type::HUGE)); | |
83 | $this->add('checkbox', 'is_default', ts('Is this Label Format the default?')); | |
2b221de0 | 84 | |
f5cf8c8d | 85 | // currently we support only mailing label creation, hence comment below code |
86 | /* | |
2b221de0 | 87 | $options = array( |
e70a7fc0 TO |
88 | 'label_format' => ts('Mailing Label'), |
89 | 'name_badge' => ts('Name Badge'), | |
2b221de0 KJ |
90 | ); |
91 | ||
92 | $labelType = $this->addRadio('label_type', ts('Used For'), $options, null, ' '); | |
93 | ||
94 | if ($this->_action != CRM_Core_Action::ADD) { | |
e70a7fc0 | 95 | $labelType->freeze(); |
2b221de0 | 96 | } |
e70a7fc0 | 97 | */ |
2b221de0 | 98 | |
6a488035 TO |
99 | $this->add('select', 'paper_size', ts('Sheet Size'), |
100 | array( | |
21dfd5f5 | 101 | 0 => ts('- default -'), |
eaf5045f | 102 | ) + CRM_Core_BAO_PaperSize::getList(TRUE), FALSE, |
6a488035 | 103 | array( |
21dfd5f5 | 104 | 'onChange' => "selectPaper( this.value );", |
eaf5045f | 105 | ) + $disabled |
6a488035 TO |
106 | ); |
107 | $this->add('static', 'paper_dimensions', NULL, ts('Sheet Size (w x h)')); | |
108 | $this->add('select', 'orientation', ts('Orientation'), CRM_Core_BAO_LabelFormat::getPageOrientations(), FALSE, | |
109 | array( | |
21dfd5f5 | 110 | 'onChange' => "updatePaperDimensions();", |
eaf5045f | 111 | ) + $disabled |
6a488035 | 112 | ); |
eaf5045f | 113 | $this->add('select', 'font_name', ts('Font Name'), CRM_Core_BAO_LabelFormat::getFontNames($this->_group)); |
6a488035 TO |
114 | $this->add('select', 'font_size', ts('Font Size'), CRM_Core_BAO_LabelFormat::getFontSizes()); |
115 | $this->add('static', 'font_style', ts('Font Style')); | |
116 | $this->add('checkbox', 'bold', ts('Bold')); | |
117 | $this->add('checkbox', 'italic', ts('Italic')); | |
118 | $this->add('select', 'metric', ts('Unit of Measure'), CRM_Core_BAO_LabelFormat::getUnits(), FALSE, | |
119 | array('onChange' => "selectMetric( this.value );") | |
120 | ); | |
121 | $this->add('text', 'width', ts('Label Width'), array('size' => 8, 'maxlength' => 8) + $disabled, $required); | |
122 | $this->add('text', 'height', ts('Label Height'), array('size' => 8, 'maxlength' => 8) + $disabled, $required); | |
123 | $this->add('text', 'NX', ts('Labels Per Row'), array('size' => 3, 'maxlength' => 3) + $disabled, $required); | |
124 | $this->add('text', 'NY', ts('Labels Per Column'), array('size' => 3, 'maxlength' => 3) + $disabled, $required); | |
125 | $this->add('text', 'tMargin', ts('Top Margin'), array('size' => 8, 'maxlength' => 8) + $disabled, $required); | |
126 | $this->add('text', 'lMargin', ts('Left Margin'), array('size' => 8, 'maxlength' => 8) + $disabled, $required); | |
127 | $this->add('text', 'SpaceX', ts('Horizontal Spacing'), array('size' => 8, 'maxlength' => 8) + $disabled, $required); | |
128 | $this->add('text', 'SpaceY', ts('Vertical Spacing'), array('size' => 8, 'maxlength' => 8) + $disabled, $required); | |
129 | $this->add('text', 'lPadding', ts('Left Padding'), array('size' => 8, 'maxlength' => 8), $required); | |
130 | $this->add('text', 'tPadding', ts('Top Padding'), array('size' => 8, 'maxlength' => 8), $required); | |
7ecddde4 | 131 | $this->add('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat', 'weight'), TRUE); |
6a488035 | 132 | |
eaf5045f KJ |
133 | $this->addRule('label', ts('Name already exists in Database.'), 'objectExists', array( |
134 | 'CRM_Core_BAO_LabelFormat', | |
21dfd5f5 | 135 | $this->_id, |
eaf5045f | 136 | )); |
d5be719d ML |
137 | $this->addRule('NX', ts('Please enter a valid integer.'), 'integer'); |
138 | $this->addRule('NY', ts('Please enter a valid integer.'), 'integer'); | |
139 | $this->addRule('tMargin', ts('Please enter a valid number.'), 'numeric'); | |
140 | $this->addRule('lMargin', ts('Please enter a valid number.'), 'numeric'); | |
141 | $this->addRule('SpaceX', ts('Please enter a valid number.'), 'numeric'); | |
142 | $this->addRule('SpaceY', ts('Please enter a valid number.'), 'numeric'); | |
143 | $this->addRule('lPadding', ts('Please enter a valid number.'), 'numeric'); | |
144 | $this->addRule('tPadding', ts('Please enter a valid number.'), 'numeric'); | |
145 | $this->addRule('width', ts('Please enter a valid number.'), 'numeric'); | |
146 | $this->addRule('height', ts('Please enter a valid number.'), 'numeric'); | |
147 | $this->addRule('weight', ts('Please enter a valid integer.'), 'integer'); | |
6a488035 TO |
148 | } |
149 | ||
e0ef6999 EM |
150 | /** |
151 | * @return int | |
152 | */ | |
00be9182 | 153 | public function setDefaultValues() { |
6a488035 | 154 | if ($this->_action & CRM_Core_Action::ADD) { |
eaf5045f | 155 | $defaults['weight'] = CRM_Utils_Array::value('weight', CRM_Core_BAO_LabelFormat::getDefaultValues($this->_group), 0); |
6a488035 TO |
156 | } |
157 | else { | |
158 | $defaults = $this->_values; | |
159 | // Convert field names that are illegal PHP/SMARTY variable names | |
160 | $defaults['paper_size'] = $defaults['paper-size']; | |
161 | unset($defaults['paper-size']); | |
162 | $defaults['font_name'] = $defaults['font-name']; | |
163 | unset($defaults['font-name']); | |
164 | $defaults['font_size'] = $defaults['font-size']; | |
165 | unset($defaults['font-size']); | |
166 | ||
167 | $defaults['bold'] = (stripos($defaults['font-style'], 'B') !== FALSE); | |
168 | $defaults['italic'] = (stripos($defaults['font-style'], 'I') !== FALSE); | |
169 | unset($defaults['font-style']); | |
170 | } | |
2b221de0 KJ |
171 | |
172 | $defaults['label_type'] = $this->_group; | |
6a488035 TO |
173 | return $defaults; |
174 | } | |
175 | ||
176 | /** | |
eceb18cc | 177 | * Process the form submission. |
6a488035 TO |
178 | */ |
179 | public function postProcess() { | |
180 | if ($this->_action & CRM_Core_Action::DELETE) { | |
181 | // delete Label Format | |
eaf5045f | 182 | CRM_Core_BAO_LabelFormat::del($this->_id, $this->_group); |
6a488035 TO |
183 | CRM_Core_Session::setStatus(ts('Selected Label Format has been deleted.'), ts('Record Deleted'), 'success'); |
184 | return; | |
185 | } | |
186 | if ($this->_action & CRM_Core_Action::COPY) { | |
187 | // make a copy of the Label Format | |
eaf5045f | 188 | $labelFormat = CRM_Core_BAO_LabelFormat::getById($this->_id, $this->_group); |
d7bd49d2 ML |
189 | $newlabel = ts('Copy of %1', array(1 => $labelFormat['label'])); |
190 | ||
4f1ddb25 | 191 | $list = CRM_Core_BAO_LabelFormat::getList(TRUE, $this->_group); |
eaf5045f | 192 | $count = 1; |
d7bd49d2 ML |
193 | |
194 | while (in_array($newlabel, $list)) { | |
195 | $count++; | |
196 | $newlabel = ts('Copy %1 of %2', array(1 => $count, 2 => $labelFormat['label'])); | |
6a488035 | 197 | } |
d7bd49d2 ML |
198 | |
199 | $labelFormat['label'] = $newlabel; | |
6a488035 TO |
200 | $labelFormat['grouping'] = CRM_Core_BAO_LabelFormat::customGroupName(); |
201 | $labelFormat['is_default'] = 0; | |
202 | $labelFormat['is_reserved'] = 0; | |
eaf5045f | 203 | |
6a488035 | 204 | $bao = new CRM_Core_BAO_LabelFormat(); |
4f1ddb25 | 205 | $bao->saveLabelFormat($labelFormat, NULL, $this->_group); |
d7bd49d2 | 206 | CRM_Core_Session::setStatus(ts('%1 has been created.', array(1 => $labelFormat['label'])), ts('Saved'), 'success'); |
6a488035 TO |
207 | return; |
208 | } | |
209 | ||
210 | $values = $this->controller->exportValues($this->getName()); | |
f5cf8c8d | 211 | |
212 | // since we currently support only mailing label format | |
213 | $values['label_type'] = 'label_format'; | |
214 | ||
6a488035 TO |
215 | $values['is_default'] = isset($values['is_default']); |
216 | ||
217 | // Restore field names that were converted because they are illegal PHP/SMARTY variable names | |
218 | if (isset($values['paper_size'])) { | |
219 | $values['paper-size'] = $values['paper_size']; | |
220 | unset($values['paper_size']); | |
221 | } | |
222 | if (isset($values['font_name'])) { | |
223 | $values['font-name'] = $values['font_name']; | |
224 | unset($values['font_name']); | |
225 | } | |
226 | if (isset($values['font_size'])) { | |
227 | $values['font-size'] = $values['font_size']; | |
228 | unset($values['font_size']); | |
229 | } | |
230 | ||
231 | $style = ''; | |
232 | if (isset($values['bold'])) { | |
233 | $style .= 'B'; | |
234 | } | |
235 | if (isset($values['italic'])) { | |
236 | $style .= 'I'; | |
237 | } | |
238 | $values['font-style'] = $style; | |
239 | ||
240 | $bao = new CRM_Core_BAO_LabelFormat(); | |
2b221de0 | 241 | $bao->saveLabelFormat($values, $this->_id, $values['label_type']); |
6a488035 TO |
242 | |
243 | $status = ts('Your new Label Format titled <strong>%1</strong> has been saved.', array(1 => $values['label'])); | |
244 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
245 | $status = ts('Your Label Format titled <strong>%1</strong> has been updated.', array(1 => $values['label'])); | |
246 | } | |
247 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
248 | } | |
96025800 | 249 | |
6a488035 | 250 | } |