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