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