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