Merge pull request #3114 from Edzelopez/CRM-14621
[civicrm-core.git] / CRM / Admin / Form / CaseType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Case Type
38 *
39 */
40 class CRM_Admin_Form_CaseType extends CRM_Admin_Form {
41
42 /**
43 * Function to build the form
44 *
45 * @return void
46 * @access public
47 */
48 public function buildQuickForm() {
49 parent::buildQuickForm();
50 if ($this->_action & CRM_Core_Action::DELETE) {
51 return;
52 }
53 $this->applyFilter('__ALL__', 'trim');
54 $this->add('text', 'title', ts('Name'),
55 CRM_Core_DAO::getAttribute('CRM_Case_DAO_CaseType', 'title'),
56 TRUE
57 );
58 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
59 $this->add('text', 'description', ts('Description'),
60 CRM_Core_DAO::getAttribute('CRM_Case_DAO_CaseType', 'description')
61 );
62
63 $this->assign('cid', $this->_id);
64 $this->addFormRule(array('CRM_Admin_Form_CaseType', 'formRule'), $this);
65 }
66
67 /**
68 * global form rule
69 *
70 * @param array $fields the input form values
71 *
72 * @return true if no errors, else array of errors
73 * @access public
74 * @static
75 */
76 static function formRule($fields, $files, $self) {
77
78 $errors = array();
79
80 if ($self->_id) {
81 $caseName = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $self->_id, 'name');
82 }
83 else {
84 $caseName = ucfirst(CRM_Utils_String::munge($fields['title']));
85 }
86
87 if (!CRM_Core_DAO::objectExists($caseName, 'CRM_Case_DAO_CaseType', $self->_id)) {
88 $errors['title'] = ts('This case type name already exists in database. Case type names must be unique.');
89 }
90
91 $reservedKeyWords = CRM_Core_SelectValues::customGroupExtends();
92 //restrict "name" from being a reserved keyword when a new contact subtype is created
93 if (!$self->_id && in_array($caseName, array_keys($reservedKeyWords))) {
94 $errors['title'] = ts('Case Type names should not use reserved keywords.');
95 }
96 return empty($errors) ? TRUE : $errors;
97 }
98
99 /**
100 * Function to process the form
101 *
102 * @access public
103 *
104 * @return void
105 */
106 public function postProcess() {
107 CRM_Utils_System::flushCache();
108
109 if ($this->_action & CRM_Core_Action::DELETE) {
110 $isDelete = CRM_Case_BAO_CaseType::del($this->_id);
111 if ($isDelete) {
112 CRM_Core_Session::setStatus(ts('Selected case type has been deleted.'), ts('Record Deleted'), 'success');
113 }
114 else {
115 CRM_Core_Session::setStatus(ts("Selected case type can not be deleted."), ts('Sorry'), 'error');
116 }
117 return;
118 }
119 // store the submitted values in an array
120 $params = $this->exportValues();
121
122 if ($this->_action & CRM_Core_Action::ADD) {
123 $params['name'] = ucfirst(CRM_Utils_String::munge($params['title']));
124 } else {
125 $params['id'] = $this->_id;
126 $params['name'] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $this->_id, 'name');
127 }
128 $caseType = CRM_Case_BAO_CaseType::add($params);
129 CRM_Core_Session::setStatus(ts("The Case Type '%1' has been saved.",
130 array(1 => $caseType->title)
131 ), ts('Saved'), 'success');
132 }
133 }
134