Update Copywrite year to be 2019
[civicrm-core.git] / CRM / Admin / Form / LocationType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
b6c94f42 35 * This class generates form components for Location Type.
6a488035
TO
36 */
37class CRM_Admin_Form_LocationType extends CRM_Admin_Form {
38
39 /**
eceb18cc 40 * Build the form object.
6a488035
TO
41 */
42 public function buildQuickForm() {
6a488035 43 parent::buildQuickForm();
e2046b33 44 $this->setPageTitle(ts('Location Type'));
6a488035
TO
45
46 if ($this->_action & CRM_Core_Action::DELETE) {
47 return;
48 }
49
50 $this->applyFilter('__ALL__', 'trim');
51 $this->add('text',
52 'name',
53 ts('Name'),
54 CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'name'),
55 TRUE
56 );
57 $this->addRule('name',
58 ts('Name already exists in Database.'),
59 'objectExists',
60 array('CRM_Core_DAO_LocationType', $this->_id)
61 );
62 $this->addRule('name',
63 ts('Name can only consist of alpha-numeric characters'),
64 'variable'
65 );
66
e2046b33 67 $this->add('text', 'display_name', ts('Display Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'display_name'), TRUE);
6a488035
TO
68 $this->add('text', 'vcard_name', ts('vCard Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'vcard_name'));
69
70 $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_LocationType', 'description'));
71
72 $this->add('checkbox', 'is_active', ts('Enabled?'));
73 $this->add('checkbox', 'is_default', ts('Default?'));
ea9cdce1
CW
74
75 if ($this->_action & CRM_Core_Action::UPDATE) {
76 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_reserved')) {
77 $this->freeze(array('name', 'description', 'is_active'));
78 }
79 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $this->_id, 'is_default')) {
80 $this->freeze(array('is_default'));
81 }
6a488035
TO
82 }
83 }
84
85 /**
eceb18cc 86 * Process the form submission.
6a488035
TO
87 */
88 public function postProcess() {
d7d6c461 89 CRM_Utils_System::flushCache();
6a488035
TO
90
91 if ($this->_action & CRM_Core_Action::DELETE) {
92 CRM_Core_BAO_LocationType::del($this->_id);
93 CRM_Core_Session::setStatus(ts('Selected Location type has been deleted.'), ts('Record Deleted'), 'success');
94 return;
95 }
96
97 // store the submitted values in an array
98 $params = $this->exportValues();
99 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
100 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
101
102 // action is taken depending upon the mode
103 $locationType = new CRM_Core_DAO_LocationType();
104 $locationType->name = $params['name'];
105 $locationType->display_name = $params['display_name'];
106 $locationType->vcard_name = $params['vcard_name'];
107 $locationType->description = $params['description'];
108 $locationType->is_active = $params['is_active'];
109 $locationType->is_default = $params['is_default'];
110
111 if ($params['is_default']) {
112 $query = "UPDATE civicrm_location_type SET is_default = 0";
33621c4f 113 CRM_Core_DAO::executeQuery($query);
6a488035
TO
114 }
115
116 if ($this->_action & CRM_Core_Action::UPDATE) {
117 $locationType->id = $this->_id;
118 }
119
120 $locationType->save();
121
122 CRM_Core_Session::setStatus(ts("The location type '%1' has been saved.",
353ffa53
TO
123 array(1 => $locationType->name)
124 ), ts('Saved'), 'success');
6a488035 125 }
e2046b33 126
6a488035 127}