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