Merge pull request #15978 from civicrm/5.20
[civicrm-core.git] / CRM / Admin / Form / LocationType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
b6c94f42 19 * This class generates form components for Location Type.
6a488035
TO
20 */
21class CRM_Admin_Form_LocationType extends CRM_Admin_Form {
22
23 /**
eceb18cc 24 * Build the form object.
6a488035
TO
25 */
26 public function buildQuickForm() {
6a488035 27 parent::buildQuickForm();
e2046b33 28 $this->setPageTitle(ts('Location Type'));
6a488035
TO
29
30 if ($this->_action & CRM_Core_Action::DELETE) {
31 return;
32 }
33
34 $this->applyFilter('__ALL__', 'trim');
35 $this->add('text',
36 'name',
37 ts('Name'),
38 CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'name'),
39 TRUE
40 );
41 $this->addRule('name',
42 ts('Name already exists in Database.'),
43 'objectExists',
be2fb01f 44 ['CRM_Core_DAO_LocationType', $this->_id]
6a488035
TO
45 );
46 $this->addRule('name',
47 ts('Name can only consist of alpha-numeric characters'),
48 'variable'
49 );
50
e2046b33 51 $this->add('text', 'display_name', ts('Display Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'display_name'), TRUE);
6a488035
TO
52 $this->add('text', 'vcard_name', ts('vCard Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'vcard_name'));
53
54 $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'description'));
55
56 $this->add('checkbox', 'is_active', ts('Enabled?'));
57 $this->add('checkbox', 'is_default', ts('Default?'));
ea9cdce1
CW
58
59 if ($this->_action & CRM_Core_Action::UPDATE) {
60 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_reserved')) {
be2fb01f 61 $this->freeze(['name', 'description', 'is_active']);
ea9cdce1
CW
62 }
63 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_default')) {
be2fb01f 64 $this->freeze(['is_default']);
ea9cdce1 65 }
6a488035
TO
66 }
67 }
68
69 /**
eceb18cc 70 * Process the form submission.
6a488035
TO
71 */
72 public function postProcess() {
d7d6c461 73 CRM_Utils_System::flushCache();
6a488035
TO
74
75 if ($this->_action & CRM_Core_Action::DELETE) {
76 CRM_Core_BAO_LocationType::del($this->_id);
77 CRM_Core_Session::setStatus(ts('Selected Location type has been deleted.'), ts('Record Deleted'), 'success');
78 return;
79 }
80
81 // store the submitted values in an array
82 $params = $this->exportValues();
83 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
84 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
85
86 // action is taken depending upon the mode
87 $locationType = new CRM_Core_DAO_LocationType();
88 $locationType->name = $params['name'];
89 $locationType->display_name = $params['display_name'];
90 $locationType->vcard_name = $params['vcard_name'];
91 $locationType->description = $params['description'];
92 $locationType->is_active = $params['is_active'];
93 $locationType->is_default = $params['is_default'];
94
95 if ($params['is_default']) {
96 $query = "UPDATE civicrm_location_type SET is_default = 0";
33621c4f 97 CRM_Core_DAO::executeQuery($query);
6a488035
TO
98 }
99
100 if ($this->_action & CRM_Core_Action::UPDATE) {
101 $locationType->id = $this->_id;
102 }
103
104 $locationType->save();
105
106 CRM_Core_Session::setStatus(ts("The location type '%1' has been saved.",
be2fb01f 107 [1 => $locationType->name]
353ffa53 108 ), ts('Saved'), 'success');
6a488035 109 }
e2046b33 110
6a488035 111}