label configuration form fixes, CRM-12965
[civicrm-core.git] / CRM / Admin / Form / LabelFormats.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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-2013
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 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 * Function to build the form
65 *
66 * @return None
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
78 $disabled = array();
79 $required = TRUE;
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?'));
90 $this->add('select', 'paper_size', ts('Sheet Size'),
91 array(
92 0 => ts('- default -')
93 ) + CRM_Core_BAO_PaperSize::getList(TRUE), FALSE,
94 array(
95 'onChange' => "selectPaper( this.value );"
96 ) + $disabled
97 );
98 $this->add('static', 'paper_dimensions', NULL, ts('Sheet Size (w x h)'));
99 $this->add('select', 'orientation', ts('Orientation'), CRM_Core_BAO_LabelFormat::getPageOrientations(), FALSE,
100 array(
101 'onChange' => "updatePaperDimensions();"
102 ) + $disabled
103 );
104 $this->add('select', 'font_name', ts('Font Name'), CRM_Core_BAO_LabelFormat::getFontNames($this->_group));
105 $this->add('select', 'font_size', ts('Font Size'), CRM_Core_BAO_LabelFormat::getFontSizes());
106 $this->add('static', 'font_style', ts('Font Style'));
107 $this->add('checkbox', 'bold', ts('Bold'));
108 $this->add('checkbox', 'italic', ts('Italic'));
109 $this->add('select', 'metric', ts('Unit of Measure'), CRM_Core_BAO_LabelFormat::getUnits(), FALSE,
110 array('onChange' => "selectMetric( this.value );")
111 );
112 $this->add('text', 'width', ts('Label Width'), array('size' => 8, 'maxlength' => 8) + $disabled, $required);
113 $this->add('text', 'height', ts('Label Height'), array('size' => 8, 'maxlength' => 8) + $disabled, $required);
114 $this->add('text', 'NX', ts('Labels Per Row'), array('size' => 3, 'maxlength' => 3) + $disabled, $required);
115 $this->add('text', 'NY', ts('Labels Per Column'), array('size' => 3, 'maxlength' => 3) + $disabled, $required);
116 $this->add('text', 'tMargin', ts('Top Margin'), array('size' => 8, 'maxlength' => 8) + $disabled, $required);
117 $this->add('text', 'lMargin', ts('Left Margin'), array('size' => 8, 'maxlength' => 8) + $disabled, $required);
118 $this->add('text', 'SpaceX', ts('Horizontal Spacing'), array('size' => 8, 'maxlength' => 8) + $disabled, $required);
119 $this->add('text', 'SpaceY', ts('Vertical Spacing'), array('size' => 8, 'maxlength' => 8) + $disabled, $required);
120 $this->add('text', 'lPadding', ts('Left Padding'), array('size' => 8, 'maxlength' => 8), $required);
121 $this->add('text', 'tPadding', ts('Top Padding'), array('size' => 8, 'maxlength' => 8), $required);
122 $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat', 'weight'), TRUE);
123
124 $this->addRule('label', ts('Name already exists in Database.'), 'objectExists', array(
125 'CRM_Core_BAO_LabelFormat',
126 $this->_id
127 ));
128 $this->addRule('NX', ts('Must be an integer'), 'integer');
129 $this->addRule('NY', ts('Must be an integer'), 'integer');
130 $this->addRule('tMargin', ts('Must be numeric'), 'numeric');
131 $this->addRule('lMargin', ts('Must be numeric'), 'numeric');
132 $this->addRule('SpaceX', ts('Must be numeric'), 'numeric');
133 $this->addRule('SpaceY', ts('Must be numeric'), 'numeric');
134 $this->addRule('lPadding', ts('Must be numeric'), 'numeric');
135 $this->addRule('tPadding', ts('Must be numeric'), 'numeric');
136 $this->addRule('width', ts('Must be numeric'), 'numeric');
137 $this->addRule('height', ts('Must be numeric'), 'numeric');
138 $this->addRule('weight', ts('Weight must be integer'), 'integer');
139 }
140
141 function setDefaultValues() {
142 if ($this->_action & CRM_Core_Action::ADD) {
143 $defaults['weight'] = CRM_Utils_Array::value('weight', CRM_Core_BAO_LabelFormat::getDefaultValues($this->_group), 0);
144 }
145 else {
146 $defaults = $this->_values;
147 // Convert field names that are illegal PHP/SMARTY variable names
148 $defaults['paper_size'] = $defaults['paper-size'];
149 unset($defaults['paper-size']);
150 $defaults['font_name'] = $defaults['font-name'];
151 unset($defaults['font-name']);
152 $defaults['font_size'] = $defaults['font-size'];
153 unset($defaults['font-size']);
154
155 $defaults['bold'] = (stripos($defaults['font-style'], 'B') !== FALSE);
156 $defaults['italic'] = (stripos($defaults['font-style'], 'I') !== FALSE);
157 unset($defaults['font-style']);
158 }
159 return $defaults;
160 }
161
162 /**
163 * Function to process the form
164 *
165 * @access public
166 *
167 * @return None
168 */
169 public function postProcess() {
170 if ($this->_action & CRM_Core_Action::DELETE) {
171 // delete Label Format
172 CRM_Core_BAO_LabelFormat::del($this->_id, $this->_group);
173 CRM_Core_Session::setStatus(ts('Selected Label Format has been deleted.'), ts('Record Deleted'), 'success');
174 return;
175 }
176 if ($this->_action & CRM_Core_Action::COPY) {
177 // make a copy of the Label Format
178 $labelFormat = CRM_Core_BAO_LabelFormat::getById($this->_id, $this->_group);
179 $list = CRM_Core_BAO_LabelFormat::getList(TRUE, $this->_group);
180 $count = 1;
181 $prefix = ts('Copy of ');
182 while (in_array($prefix . $labelFormat['label'], $list)) {
183 $prefix = ts('Copy') . ' (' . ++$count . ') ' . ts('of ');
184 }
185 $labelFormat['label'] = $prefix . $labelFormat['label'];
186 $labelFormat['grouping'] = CRM_Core_BAO_LabelFormat::customGroupName();
187 $labelFormat['is_default'] = 0;
188 $labelFormat['is_reserved'] = 0;
189
190 $bao = new CRM_Core_BAO_LabelFormat();
191 $bao->saveLabelFormat($labelFormat, NULL, $this->_group);
192 CRM_Core_Session::setStatus($labelFormat['label'] . ts(' has been created.'), ts('Saved'), 'success');
193 return;
194 }
195
196 $values = $this->controller->exportValues($this->getName());
197 $values['is_default'] = isset($values['is_default']);
198
199 // Restore field names that were converted because they are illegal PHP/SMARTY variable names
200 if (isset($values['paper_size'])) {
201 $values['paper-size'] = $values['paper_size'];
202 unset($values['paper_size']);
203 }
204 if (isset($values['font_name'])) {
205 $values['font-name'] = $values['font_name'];
206 unset($values['font_name']);
207 }
208 if (isset($values['font_size'])) {
209 $values['font-size'] = $values['font_size'];
210 unset($values['font_size']);
211 }
212
213 $style = '';
214 if (isset($values['bold'])) {
215 $style .= 'B';
216 }
217 if (isset($values['italic'])) {
218 $style .= 'I';
219 }
220 $values['font-style'] = $style;
221
222 $bao = new CRM_Core_BAO_LabelFormat();
223 $bao->saveLabelFormat($values, $this->_id, $this->_group);
224
225 $status = ts('Your new Label Format titled <strong>%1</strong> has been saved.', array(1 => $values['label']));
226 if ($this->_action & CRM_Core_Action::UPDATE) {
227 $status = ts('Your Label Format titled <strong>%1</strong> has been updated.', array(1 => $values['label']));
228 }
229 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
230 }
231 }