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