Merge pull request #16429 from ixiam/dev/core#1113
[civicrm-core.git] / CRM / Admin / Form / ContactType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for ContactSub Type.
20 */
21 class CRM_Admin_Form_ContactType extends CRM_Admin_Form {
22
23 /**
24 * Build the form object.
25 */
26 public function buildQuickForm() {
27 parent::buildQuickForm();
28 $this->setPageTitle(ts('Contact Type'));
29
30 if ($this->_action & CRM_Core_Action::DELETE) {
31 return;
32 }
33 $this->applyFilter('__ALL__', 'trim');
34 $this->add('text', 'label', ts('Name'),
35 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_ContactType', 'label'),
36 TRUE
37 );
38 $contactType = $this->add('select', 'parent_id', ts('Basic Contact Type'),
39 CRM_Contact_BAO_ContactType::basicTypePairs(FALSE, 'id')
40 );
41 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
42 if ($this->_action & CRM_Core_Action::UPDATE) {
43 $contactType->freeze();
44 // We'll display actual "name" for built-in types (for reference) when editing their label / image_URL
45 $contactTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $this->_id, 'name');
46 $this->assign('contactTypeName', $contactTypeName);
47
48 $this->_parentId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $this->_id, 'parent_id');
49 // Freeze Enabled field for built-in contact types (parent_id is NULL for these)
50 if (is_null($this->_parentId)) {
51 $enabled->freeze();
52 }
53 }
54 $this->addElement('text', 'image_URL', ts('Image URL'));
55 $this->add('text', 'description', ts('Description'),
56 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_ContactType', 'description')
57 );
58
59 $this->assign('cid', $this->_id);
60 $this->addFormRule(['CRM_Admin_Form_ContactType', 'formRule'], $this);
61 }
62
63 /**
64 * Global form rule.
65 *
66 * @param array $fields
67 * The input form values.
68 *
69 * @param $files
70 * @param $self
71 *
72 * @return bool|array
73 * true if no errors, else array of errors
74 */
75 public static function formRule($fields, $files, $self) {
76
77 $errors = [];
78
79 if ($self->_id) {
80 $contactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $self->_id, 'name');
81 }
82 else {
83 $contactName = ucfirst(CRM_Utils_String::munge($fields['label']));
84 }
85
86 if (!CRM_Core_DAO::objectExists($contactName, 'CRM_Contact_DAO_ContactType', $self->_id)) {
87 $errors['label'] = ts('This contact type name already exists in database. Contact type names must be unique.');
88 }
89
90 $reservedKeyWords = CRM_Core_SelectValues::customGroupExtends();
91 //restrict "name" from being a reserved keyword when a new contact subtype is created
92 if (!$self->_id && in_array($contactName, array_keys($reservedKeyWords))) {
93 $errors['label'] = ts('Contact type names should not use reserved keywords.');
94 }
95 return empty($errors) ? TRUE : $errors;
96 }
97
98 /**
99 * Process the form submission.
100 */
101 public function postProcess() {
102 CRM_Utils_System::flushCache();
103
104 if ($this->_action & CRM_Core_Action::DELETE) {
105 $isDelete = CRM_Contact_BAO_ContactType::del($this->_id);
106 if ($isDelete) {
107 CRM_Core_Session::setStatus(ts('Selected contact type has been deleted.'), ts('Record Deleted'), 'success');
108 }
109 else {
110 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');
111 }
112 return;
113 }
114 // store the submitted values in an array
115 $params = $this->exportValues();
116
117 if ($this->_action & CRM_Core_Action::UPDATE) {
118 $params['id'] = $this->_id;
119 // Force Enabled = true for built-in contact types to fix problems caused by CRM-6471 (parent_id is NULL for these types)
120 if (is_null($this->_parentId)) {
121 $params['is_active'] = 1;
122 }
123 }
124
125 $contactType = CRM_Contact_BAO_ContactType::add($params);
126 CRM_Core_Session::setStatus(ts("The Contact Type '%1' has been saved.",
127 [1 => $contactType->label]
128 ), ts('Saved'), 'success');
129 }
130
131 }