Commit | Line | Data |
---|---|---|
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 | */ |
21 | class CRM_Admin_Form_LocationType extends CRM_Admin_Form { | |
22 | ||
88aae6d4 A |
23 | /** |
24 | * @var bool | |
25 | */ | |
26 | public $submitOnce = TRUE; | |
27 | ||
6a488035 | 28 | /** |
eceb18cc | 29 | * Build the form object. |
6a488035 TO |
30 | */ |
31 | public function buildQuickForm() { | |
6a488035 | 32 | parent::buildQuickForm(); |
e2046b33 | 33 | $this->setPageTitle(ts('Location Type')); |
6a488035 TO |
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', | |
be2fb01f | 49 | ['CRM_Core_DAO_LocationType', $this->_id] |
6a488035 TO |
50 | ); |
51 | $this->addRule('name', | |
52 | ts('Name can only consist of alpha-numeric characters'), | |
53 | 'variable' | |
54 | ); | |
55 | ||
e2046b33 | 56 | $this->add('text', 'display_name', ts('Display Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'display_name'), TRUE); |
6a488035 TO |
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?')); | |
ea9cdce1 CW |
63 | |
64 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
65 | if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_reserved')) { | |
be2fb01f | 66 | $this->freeze(['name', 'description', 'is_active']); |
ea9cdce1 CW |
67 | } |
68 | if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_default')) { | |
be2fb01f | 69 | $this->freeze(['is_default']); |
ea9cdce1 | 70 | } |
6a488035 TO |
71 | } |
72 | } | |
73 | ||
74 | /** | |
eceb18cc | 75 | * Process the form submission. |
6a488035 TO |
76 | */ |
77 | public function postProcess() { | |
d7d6c461 | 78 | CRM_Utils_System::flushCache(); |
6a488035 TO |
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"; | |
33621c4f | 102 | CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
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.", | |
be2fb01f | 112 | [1 => $locationType->name] |
353ffa53 | 113 | ), ts('Saved'), 'success'); |
6a488035 | 114 | } |
e2046b33 | 115 | |
6a488035 | 116 | } |