Merge pull request #15135 from mattwire/case_links_refactor_report
[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 * Build the form object.
25 */
26 public function buildQuickForm() {
27 parent::buildQuickForm();
28 $this->setPageTitle(ts('Location Type'));
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',
44 ['CRM_Core_DAO_LocationType', $this->_id]
45 );
46 $this->addRule('name',
47 ts('Name can only consist of alpha-numeric characters'),
48 'variable'
49 );
50
51 $this->add('text', 'display_name', ts('Display Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'display_name'), TRUE);
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?'));
58
59 if ($this->_action & CRM_Core_Action::UPDATE) {
60 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_reserved')) {
61 $this->freeze(['name', 'description', 'is_active']);
62 }
63 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_default')) {
64 $this->freeze(['is_default']);
65 }
66 }
67 }
68
69 /**
70 * Process the form submission.
71 */
72 public function postProcess() {
73 CRM_Utils_System::flushCache();
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";
97 CRM_Core_DAO::executeQuery($query);
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.",
107 [1 => $locationType->name]
108 ), ts('Saved'), 'success');
109 }
110
111 }