INFRA-132 - CRM/Activity - phpcbf
[civicrm-core.git] / CRM / Admin / Form / Options.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for Options
38 *
39 */
40class CRM_Admin_Form_Options extends CRM_Admin_Form {
41
42 /**
43 * The option group name
44 *
45 * @var array
46 * @static
47 */
48 protected $_gName;
49
50 /**
51 * The option group name in display format (capitalized, without underscores...etc)
52 *
53 * @var array
54 * @static
55 */
6c2473d5 56 protected $_gLabel;
6a488035
TO
57
58 /**
100fef9d 59 * Pre-process
6a488035 60 *
355ba699 61 * @return void
6a488035
TO
62 */
63 public function preProcess() {
64 parent::preProcess();
65 $session = CRM_Core_Session::singleton();
118e964e
CW
66 if (!$this->_gName && !empty($this->urlPath[3])) {
67 $this->_gName = $this->urlPath[3];
68 }
69 if (!$this->_gName && !empty($_GET['gid'])) {
70 $this->_gName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', (int) $_GET['gid'], 'name');
6a488035
TO
71 }
72 if ($this->_gName) {
73 $this->set('gName', $this->_gName);
74 }
75 else {
76 $this->_gName = $this->get('gName');
77 }
118e964e
CW
78 $this->_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
79 $this->_gName,
80 'id',
81 'name'
82 );
faa2cea0 83 $this->_gLabel = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gid, 'title');
6a488035 84 $url = "civicrm/admin/options/{$this->_gName}";
faa2cea0 85 $params = "reset=1";
6a488035
TO
86
87 if (($this->_action & CRM_Core_Action::DELETE) &&
88 in_array($this->_gName, array('email_greeting', 'postal_greeting', 'addressee'))
89 ) {
90 // Don't allow delete if the option value belongs to addressee, postal or email greetings and is in use.
91 $findValue = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'value');
92 $queryParam = array(1 => array($findValue, 'Integer'));
93 $columnName = $this->_gName . "_id";
94 $sql = "SELECT count(id) FROM civicrm_contact WHERE " . $columnName . " = %1";
95 $isInUse = CRM_Core_DAO::singleValueQuery($sql, $queryParam);
96 if ($isInUse) {
97 $scriptURL = "<a href='" . CRM_Utils_System::docURL2('Update Greetings and Address Data for Contacts', TRUE, NULL, NULL, NULL, "wiki") . "'>" . ts('Learn more about a script that can automatically update contact addressee and greeting options.') . "</a>";
6c2473d5 98 CRM_Core_Session::setStatus(ts('The selected %1 option has <strong>not been deleted</strong> because it is currently in use. Please update these contacts to use a different format before deleting this option. %2', array(1 => $this->_gLabel, 2 => $scriptURL)), ts('Sorry'), 'error');
6a488035
TO
99 $redirect = CRM_Utils_System::url($url, $params);
100 CRM_Utils_System::redirect($redirect);
101 }
102 }
103
104
105 $session->pushUserContext(CRM_Utils_System::url($url, $params));
106 $this->assign('id', $this->_id);
107
108 if ($this->_id && in_array($this->_gName, CRM_Core_OptionGroup::$_domainIDGroups)) {
109 $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'domain_id', 'id');
110 if (CRM_Core_Config::domainID() != $domainID) {
0499b0ad 111 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
112 }
113 }
114 }
115
116 /**
c490a46a 117 * Set default values for the form.
6a488035
TO
118 * the default values are retrieved from the database
119 *
6a488035 120 *
355ba699 121 * @return void
6a488035 122 */
00be9182 123 public function setDefaultValues() {
6a488035
TO
124 $defaults = parent::setDefaultValues();
125
126 if (!isset($defaults['weight']) || !$defaults['weight']) {
127 $fieldValues = array('option_group_id' => $this->_gid);
128 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
129 }
130
131 //setDefault of contact types for email greeting, postal greeting, addressee, CRM-4575
132 if (in_array($this->_gName, array(
133 'email_greeting', 'postal_greeting', 'addressee'))) {
134 $defaults['contactOptions'] = (CRM_Utils_Array::value('filter', $defaults)) ? $defaults['filter'] : NULL;
135 }
136 // CRM-11516
137 if ($this->_gName == 'payment_instrument' && $this->_id) {
138 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($this->_id, 'civicrm_option_value', 'financial_account_id');
139 }
140 return $defaults;
141 }
142
143 /**
c490a46a 144 * Build the form object
6a488035 145 *
355ba699 146 * @return void
6a488035
TO
147 */
148 public function buildQuickForm() {
149 parent::buildQuickForm();
e2046b33
CW
150 $this->setPageTitle(ts('%1 Option', array(1 => $this->_gLabel)));
151
6a488035
TO
152 if ($this->_action & CRM_Core_Action::DELETE) {
153 return;
154 }
155
156 $this->applyFilter('__ALL__', 'trim');
157
158 $isReserved = FALSE;
159 if ($this->_id) {
160 $isReserved = (bool) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'is_reserved');
161 }
162
163 $this->add('text',
164 'label',
165 ts('Label'),
166 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'),
167 TRUE
168 );
169
170 if (!in_array($this->_gName, array(
171 'email_greeting', 'postal_greeting', 'addressee')) && !$isReserved) {
172 $this->addRule('label',
173 ts('This Label already exists in the database for this option group. Please select a different Value.'),
174 'optionExists',
175 array('CRM_Core_DAO_OptionValue', $this->_id, $this->_gid, 'label')
176 );
177 }
178
179 if ($this->_gName == 'case_status') {
180 $classes = array('Opened' => ts('Opened'),
181 'Closed' => ts('Closed'),
182 );
183
184 $grouping = $this->add('select',
185 'grouping',
186 ts('Status Class'),
187 $classes
188 );
189 if ($isReserved) {
190 $grouping->freeze();
191 }
192 }
193 // CRM-11516
194 if ($this->_gName == 'payment_instrument') {
f743a6eb 195 $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
6a488035 196 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType));
8ef12e64 197
198 $this->add('select', 'financial_account_id', ts('Financial Account'),
fd1ae183
PN
199 array('' => ts('- select -')) + $financialAccount,
200 TRUE
6a488035
TO
201 );
202 }
203
204 $required = FALSE;
205 if ($this->_gName == 'custom_search') {
206 $required = TRUE;
207 }
208 elseif ($this->_gName == 'redaction_rule' || $this->_gName == 'engagement_index') {
209 $this->add('text',
210 'value',
211 ts('Value'),
212 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'),
213 TRUE
214 );
215 if ($this->_gName == 'redaction_rule') {
216 $this->add('checkbox',
217 'filter',
218 ts('Regular Expression?')
219 );
220 }
221 }
222 if ($this->_gName == 'participant_listing') {
223 $this->add('text',
224 'description',
225 ts('Description'),
226 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'description')
227 );
228 }
229 else {
230 // Hard-coding attributes here since description is still stored as varchar and not text in the schema. dgg
231 $this->addWysiwyg('description',
232 ts('Description'),
233 array('rows' => 4, 'cols' => 80),
234 $required
235 );
236 }
237
238 if ($this->_gName == 'event_badge') {
239 $this->add('text',
240 'name',
241 ts('Class Name'),
242 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'name')
243 );
244 }
245
246 $this->add('text',
247 'weight',
7ecddde4 248 ts('Order'),
6a488035
TO
249 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'weight'),
250 TRUE
251 );
252 $this->addRule('weight', ts('is a numeric field'), 'numeric');
253
254 // If CiviCase enabled AND "Add" mode OR "edit" mode for non-reserved activities, only allow user to pick Core or CiviCase component.
255 // FIXME: Each component should define whether adding new activity types is allowed.
256 $config = CRM_Core_Config::singleton();
257 if ($this->_gName == 'activity_type' && in_array("CiviCase", $config->enableComponents) &&
258 (($this->_action & CRM_Core_Action::ADD) || !$isReserved)
259 ) {
260 $caseID = CRM_Core_Component::getComponentID('CiviCase');
5ce691e5 261 $components = array('' => ts('Contacts AND Cases'), $caseID => ts('Cases Only'));
6a488035
TO
262 $this->add('select',
263 'component_id',
264 ts('Component'),
265 $components, FALSE
266 );
267 }
268
269 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
270
271 if ($isReserved) {
272 $enabled->freeze();
273 }
274
275 //fix for CRM-3552, CRM-4575
9eab4fdd 276 $showIsDefaultGroups = array(
277 'email_greeting',
278 'postal_greeting',
279 'addressee',
280 'from_email_address',
281 'case_status',
282 'encounter_medium',
283 'case_type',
284 'payment_instrument',
285 'communication_style',
694ca46e 286 'soft_credit_type',
3fff8d1e 287 'website_type',
9eab4fdd 288 );
289
290 if (in_array($this->_gName, $showIsDefaultGroups)) {
6a488035
TO
291 $this->assign('showDefault', TRUE);
292 $this->add('checkbox', 'is_default', ts('Default Option?'));
293 }
294
295 //get contact type for which user want to create a new greeting/addressee type, CRM-4575
296 if (in_array($this->_gName, array(
297 'email_greeting', 'postal_greeting', 'addressee')) && !$isReserved) {
298 $values = array(1 => ts('Individual'),
299 2 => ts('Household'),
300 3 => ts('Organization'),
301 4 => ts('Multiple Contact Merge'),
302 );
303 $this->add('select', 'contactOptions', ts('Contact Type'), array('' => '-select-') + $values, TRUE);
304 $this->assign('showContactFilter', TRUE);
305 }
306
307 if ($this->_gName == 'participant_status') {
308 // For Participant Status options, expose the 'filter' field to track which statuses are "Counted", and the Visibility field
309 $element = $this->add('checkbox', 'filter', ts('Counted?'));
310 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility());
311 }
312 if ($this->_gName == 'participant_role') {
313 // For Participant Role options, expose the 'filter' field to track which statuses are "Counted"
314 $this->add('checkbox', 'filter', ts('Counted?'));
315 }
316
317 $this->addFormRule(array('CRM_Admin_Form_Options', 'formRule'), $this);
318 }
319
320 /**
100fef9d 321 * Global form rule
6a488035 322 *
5173bd95
TO
323 * @param array $fields
324 * The input form values.
325 * @param array $files
326 * The uploaded files if any.
327 * @param array $self
328 * Current form object.
6a488035
TO
329 *
330 * @return array array of errors / empty array.
6a488035
TO
331 * @static
332 */
00be9182 333 public static function formRule($fields, $files, $self) {
6a488035 334 $errors = array();
8cc574cf 335 if ($self->_gName == 'case_status' && empty($fields['grouping'])) {
6a488035
TO
336 $errors['grouping'] = ts('Status class is a required field');
337 }
338
8cc574cf 339 if (in_array($self->_gName, array('email_greeting', 'postal_greeting', 'addressee')) && empty($self->_defaultValues['is_reserved'])) {
6a488035
TO
340 $label = $fields['label'];
341 $condition = " AND v.label = '{$label}' ";
342 $values = CRM_Core_OptionGroup::values($self->_gName, FALSE, FALSE, FALSE, $condition, 'filter');
343 $checkContactOptions = TRUE;
344
345 if ($self->_id && ($self->_defaultValues['contactOptions'] == $fields['contactOptions'])) {
346 $checkContactOptions = FALSE;
347 }
348
349 if ($checkContactOptions && in_array($fields['contactOptions'], $values)) {
350 $errors['label'] = ts('This Label already exists in the database for the selected contact type.');
351 }
352 }
353
354 if ($self->_gName == 'from_email_address') {
355 $formEmail = CRM_Utils_Mail::pluckEmailFromHeader($fields['label']);
356 if (!CRM_Utils_Rule::email($formEmail)) {
f289559e 357 $errors['label'] = ts('Please enter a valid email address.');
6a488035
TO
358 }
359
360 $formName = explode('"', $fields['label']);
a7488080 361 if (empty($formName[1]) || count($formName) != 3) {
6a488035
TO
362 $errors['label'] = ts('Please follow the proper format for From Email Address');
363 }
364 }
365
366 return $errors;
367 }
368
369 /**
c490a46a 370 * Process the form submission
6a488035 371 *
6a488035 372 *
355ba699 373 * @return void
6a488035
TO
374 */
375 public function postProcess() {
376 if ($this->_action & CRM_Core_Action::DELETE) {
377 $fieldValues = array('option_group_id' => $this->_gid);
378 $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues);
379
380 if (CRM_Core_BAO_OptionValue::del($this->_id)) {
381 if ($this->_gName == 'phone_type') {
382 CRM_Core_BAO_Phone::setOptionToNull(CRM_Utils_Array::value('value', $this->_defaultValues));
383 }
384
6c2473d5 385 CRM_Core_Session::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_gLabel)), ts('Record Deleted'), 'success');
6a488035
TO
386 }
387 else {
6c2473d5 388 CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_gLabel)), ts('Sorry'), 'error');
6a488035
TO
389 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues);
390 }
391 }
392 else {
393 $params = $ids = array();
394 $params = $this->exportValues();
395
396 // allow multiple defaults within group.
397 $allowMultiDefaults = array('email_greeting', 'postal_greeting', 'addressee', 'from_email_address');
398 if (in_array($this->_gName, $allowMultiDefaults)) {
399 if ($this->_gName == 'from_email_address') {
400 $params['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID());
401 }
402 elseif ($filter = CRM_Utils_Array::value('contactOptions', $params)) {
403 $params['filter'] = $filter;
404 $params['reset_default_for'] = array('filter' => "0, " . $params['filter']);
405 }
406
407 //make sure we should has to have space, CRM-6977
408 if ($this->_gName == 'from_email_address') {
409 $params['label'] = str_replace('"<', '" <', $params['label']);
410 }
411 }
412
74b187a2
KJ
413 // set value of filter if not present in params
414 if ($this->_id && !array_key_exists('filter', $params)) {
415 if ($this->_gName == 'participant_role') {
416 $params['filter'] = 0;
417 } else {
418 $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id');
419 }
6a488035
TO
420 }
421
422 $groupParams = array('name' => ($this->_gName));
423 $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id);
8ef12e64 424
6a488035 425 // CRM-11516
a7488080 426 if (!empty($params['financial_account_id'])) {
f743a6eb 427 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
428 $params = array(
429 'entity_table' => 'civicrm_option_value',
430 'entity_id' => $optionValue->id,
431 'account_relationship' => $relationTypeId,
432 'financial_account_id' => $params['financial_account_id']
433 );
434 CRM_Financial_BAO_FinancialTypeAccount::add($params);
435 }
436
6c2473d5 437 CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => $this->_gLabel, 2 => $optionValue->label)), ts('Saved'), 'success');
6a488035
TO
438 }
439 }
e2046b33 440
6a488035 441}