3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for ContactSub Type
40 class CRM_Admin_Form_ContactType
extends CRM_Admin_Form
{
43 * Build the form object
47 public function buildQuickForm() {
48 parent
::buildQuickForm();
49 $this->setPageTitle(ts('Contact Type'));
51 if ($this->_action
& CRM_Core_Action
::DELETE
) {
54 $this->applyFilter('__ALL__', 'trim');
55 $this->add('text', 'label', ts('Name'),
56 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_ContactType', 'label'),
59 $contactType = $this->add('select', 'parent_id', ts('Basic Contact Type'),
60 CRM_Contact_BAO_ContactType
::basicTypePairs(FALSE, 'id')
62 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
63 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
64 $contactType->freeze();
65 // We'll display actual "name" for built-in types (for reference) when editing their label / image_URL
66 $contactTypeName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_ContactType', $this->_id
, 'name');
67 $this->assign('contactTypeName', $contactTypeName);
69 $this->_parentId
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_ContactType', $this->_id
, 'parent_id');
70 // Freeze Enabled field for built-in contact types (parent_id is NULL for these)
71 if (is_null($this->_parentId
)) {
75 $this->addElement('text', 'image_URL', ts('Image URL'));
76 $this->add('text', 'description', ts('Description'),
77 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_ContactType', 'description')
80 $this->assign('cid', $this->_id
);
81 $this->addFormRule(array('CRM_Admin_Form_ContactType', 'formRule'), $this);
87 * @param array $fields
88 * The input form values.
93 * @return true if no errors, else array of errors
96 public static function formRule($fields, $files, $self) {
101 $contactName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_ContactType', $self->_id
, 'name');
104 $contactName = ucfirst(CRM_Utils_String
::munge($fields['label']));
107 if (!CRM_Core_DAO
::objectExists($contactName, 'CRM_Contact_DAO_ContactType', $self->_id
)) {
108 $errors['label'] = ts('This contact type name already exists in database. Contact type names must be unique.');
111 $reservedKeyWords = CRM_Core_SelectValues
::customGroupExtends();
112 //restrict "name" from being a reserved keyword when a new contact subtype is created
113 if (!$self->_id
&& in_array($contactName, array_keys($reservedKeyWords))) {
114 $errors['label'] = ts('Contact type names should not use reserved keywords.');
116 return empty($errors) ?
TRUE : $errors;
120 * Process the form submission
125 public function postProcess() {
126 CRM_Utils_System
::flushCache();
128 if ($this->_action
& CRM_Core_Action
::DELETE
) {
129 $isDelete = CRM_Contact_BAO_ContactType
::del($this->_id
);
131 CRM_Core_Session
::setStatus(ts('Selected contact type has been deleted.'), ts('Record Deleted'), 'success');
134 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');
138 // store the submitted values in an array
139 $params = $this->exportValues();
141 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
142 $params['id'] = $this->_id
;
143 // Force Enabled = true for built-in contact types to fix problems caused by CRM-6471 (parent_id is NULL for these types)
144 if (is_null($this->_parentId
)) {
145 $params['is_active'] = 1;
148 if ($this->_action
& CRM_Core_Action
::ADD
) {
149 $params['name'] = ucfirst(CRM_Utils_String
::munge($params['label']));
151 $contactType = CRM_Contact_BAO_ContactType
::add($params);
152 CRM_Core_Session
::setStatus(ts("The Contact Type '%1' has been saved.",
153 array(1 => $contactType->label
)
154 ), ts('Saved'), 'success');