Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
b6c94f42 | 19 | * This class generates form components for ContactSub Type. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Form_ContactType extends CRM_Admin_Form { | |
22 | ||
23 | /** | |
eceb18cc | 24 | * Build the form object. |
6a488035 TO |
25 | */ |
26 | public function buildQuickForm() { | |
27 | parent::buildQuickForm(); | |
e2046b33 CW |
28 | $this->setPageTitle(ts('Contact Type')); |
29 | ||
6a488035 TO |
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 | } | |
a452aa99 CW |
54 | // TODO: Remove when dropping image_URL column |
55 | if ($this->_id) { | |
56 | $imageUrl = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $this->_id, 'image_URL'); | |
57 | if ($imageUrl) { | |
58 | $this->addElement('text', 'image_URL', ts('Image URL')); | |
59 | } | |
60 | } | |
61 | $this->assign('hasImageUrl', !empty($imageUrl)); | |
62 | $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]); | |
6a488035 TO |
63 | $this->add('text', 'description', ts('Description'), |
64 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_ContactType', 'description') | |
65 | ); | |
66 | ||
67 | $this->assign('cid', $this->_id); | |
be2fb01f | 68 | $this->addFormRule(['CRM_Admin_Form_ContactType', 'formRule'], $this); |
6a488035 TO |
69 | } |
70 | ||
71 | /** | |
eceb18cc | 72 | * Global form rule. |
6a488035 | 73 | * |
5173bd95 TO |
74 | * @param array $fields |
75 | * The input form values. | |
77b97be7 EM |
76 | * |
77 | * @param $files | |
e8cf95b4 | 78 | * @param self $self |
6a488035 | 79 | * |
72b3a70c CW |
80 | * @return bool|array |
81 | * true if no errors, else array of errors | |
6a488035 | 82 | */ |
00be9182 | 83 | public static function formRule($fields, $files, $self) { |
6a488035 | 84 | |
be2fb01f | 85 | $errors = []; |
6a488035 TO |
86 | |
87 | if ($self->_id) { | |
88 | $contactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $self->_id, 'name'); | |
89 | } | |
90 | else { | |
91 | $contactName = ucfirst(CRM_Utils_String::munge($fields['label'])); | |
92 | } | |
93 | ||
94 | if (!CRM_Core_DAO::objectExists($contactName, 'CRM_Contact_DAO_ContactType', $self->_id)) { | |
95 | $errors['label'] = ts('This contact type name already exists in database. Contact type names must be unique.'); | |
96 | } | |
97 | ||
98 | $reservedKeyWords = CRM_Core_SelectValues::customGroupExtends(); | |
99 | //restrict "name" from being a reserved keyword when a new contact subtype is created | |
c3f7ab62 | 100 | if (!$self->_id && array_key_exists($contactName, $reservedKeyWords)) { |
6a488035 TO |
101 | $errors['label'] = ts('Contact type names should not use reserved keywords.'); |
102 | } | |
103 | return empty($errors) ? TRUE : $errors; | |
104 | } | |
105 | ||
106 | /** | |
eceb18cc | 107 | * Process the form submission. |
6a488035 TO |
108 | */ |
109 | public function postProcess() { | |
110 | CRM_Utils_System::flushCache(); | |
111 | ||
112 | if ($this->_action & CRM_Core_Action::DELETE) { | |
113 | $isDelete = CRM_Contact_BAO_ContactType::del($this->_id); | |
114 | if ($isDelete) { | |
115 | CRM_Core_Session::setStatus(ts('Selected contact type has been deleted.'), ts('Record Deleted'), 'success'); | |
116 | } | |
117 | else { | |
118 | 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'); | |
119 | } | |
120 | return; | |
121 | } | |
122 | // store the submitted values in an array | |
123 | $params = $this->exportValues(); | |
124 | ||
125 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
126 | $params['id'] = $this->_id; | |
127 | // Force Enabled = true for built-in contact types to fix problems caused by CRM-6471 (parent_id is NULL for these types) | |
128 | if (is_null($this->_parentId)) { | |
129 | $params['is_active'] = 1; | |
130 | } | |
131 | } | |
351e8d47 | 132 | |
a452aa99 CW |
133 | // If icon is set, it overrides image_URL |
134 | if (!empty($params['icon'])) { | |
135 | $params['image_URL'] = ''; | |
136 | } | |
137 | ||
6a488035 TO |
138 | $contactType = CRM_Contact_BAO_ContactType::add($params); |
139 | CRM_Core_Session::setStatus(ts("The Contact Type '%1' has been saved.", | |
be2fb01f | 140 | [1 => $contactType->label] |
353ffa53 | 141 | ), ts('Saved'), 'success'); |
6a488035 | 142 | } |
e2046b33 | 143 | |
6a488035 | 144 | } |