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