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