CRM-13927 - Counties: don't need to find path to county file
[civicrm-core.git] / CRM / Admin / Form / ContactType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for ContactSub Type
38 *
39 */
40 class CRM_Admin_Form_ContactType extends CRM_Admin_Form {
41
42 /**
43 * Function to build the form
44 *
45 * @return void
46 * @access public
47 */
48 public function buildQuickForm() {
49 parent::buildQuickForm();
50 if ($this->_action & CRM_Core_Action::DELETE) {
51 return;
52 }
53 $this->applyFilter('__ALL__', 'trim');
54 $this->add('text', 'label', ts('Name'),
55 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_ContactType', 'label'),
56 TRUE
57 );
58 $contactType = $this->add('select', 'parent_id', ts('Basic Contact Type'),
59 CRM_Contact_BAO_ContactType::basicTypePairs(FALSE, 'id')
60 );
61 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
62 if ($this->_action & CRM_Core_Action::UPDATE) {
63 $contactType->freeze();
64 // We'll display actual "name" for built-in types (for reference) when editing their label / image_URL
65 $contactTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $this->_id, 'name');
66 $this->assign('contactTypeName', $contactTypeName);
67
68 $this->_parentId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $this->_id, 'parent_id');
69 // Freeze Enabled field for built-in contact types (parent_id is NULL for these)
70 if (is_null($this->_parentId)) {
71 $enabled->freeze();
72 }
73 }
74 $this->addElement('text', 'image_URL', ts('Image URL'));
75 $this->add('text', 'description', ts('Description'),
76 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_ContactType', 'description')
77 );
78
79 $this->assign('cid', $this->_id);
80 $this->addFormRule(array('CRM_Admin_Form_ContactType', 'formRule'), $this);
81 }
82
83 /**
84 * global form rule
85 *
86 * @param array $fields the input form values
87 *
88 * @return true if no errors, else array of errors
89 * @access public
90 * @static
91 */
92 static function formRule($fields, $files, $self) {
93
94 $errors = array();
95
96 if ($self->_id) {
97 $contactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $self->_id, 'name');
98 }
99 else {
100 $contactName = ucfirst(CRM_Utils_String::munge($fields['label']));
101 }
102
103 if (!CRM_Core_DAO::objectExists($contactName, 'CRM_Contact_DAO_ContactType', $self->_id)) {
104 $errors['label'] = ts('This contact type name already exists in database. Contact type names must be unique.');
105 }
106
107 $reservedKeyWords = CRM_Core_SelectValues::customGroupExtends();
108 //restrict "name" from being a reserved keyword when a new contact subtype is created
109 if (!$self->_id && in_array($contactName, array_keys($reservedKeyWords))) {
110 $errors['label'] = ts('Contact type names should not use reserved keywords.');
111 }
112 return empty($errors) ? TRUE : $errors;
113 }
114
115 /**
116 * Function to process the form
117 *
118 * @access public
119 *
120 * @return void
121 */
122 public function postProcess() {
123 CRM_Utils_System::flushCache();
124
125 if ($this->_action & CRM_Core_Action::DELETE) {
126 $isDelete = CRM_Contact_BAO_ContactType::del($this->_id);
127 if ($isDelete) {
128 CRM_Core_Session::setStatus(ts('Selected contact type has been deleted.'), ts('Record Deleted'), 'success');
129 }
130 else {
131 CRM_Core_Session::setStatus(ts("Selected contact type can not be deleted. Make sure contact type doesn't have any associated custom data or group."), ts('Sorry'), 'error');
132 }
133 return;
134 }
135 // store the submitted values in an array
136 $params = $this->exportValues();
137
138 if ($this->_action & CRM_Core_Action::UPDATE) {
139 $params['id'] = $this->_id;
140 // Force Enabled = true for built-in contact types to fix problems caused by CRM-6471 (parent_id is NULL for these types)
141 if (is_null($this->_parentId)) {
142 $params['is_active'] = 1;
143 }
144 }
145 if ($this->_action & CRM_Core_Action::ADD) {
146 $params['name'] = ucfirst(CRM_Utils_String::munge($params['label']));
147 }
148 $contactType = CRM_Contact_BAO_ContactType::add($params);
149 CRM_Core_Session::setStatus(ts("The Contact Type '%1' has been saved.",
150 array(1 => $contactType->label)
151 ), ts('Saved'), 'success');
152 }
153 }
154