Merge pull request #3574 from totten/master-casetype-redir
[civicrm-core.git] / CRM / Case / BAO / CaseType.php
CommitLineData
8ffdec17
ARW
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 contains the functions for Case Type management
38 *
39 */
40class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
41
42 /**
43 * static field for all the case information that we can potentially export
44 *
45 * @var array
46 * @static
47 */
48 static $_exportableFields = NULL;
49
50 /**
51 * takes an associative array and creates a Case Type object
52 *
53 * the function extract all the params it needs to initialize the create a
54 * case type object. the params array could contain additional unused name/value
55 * pairs
56 *
57 * @param array $params (reference ) an assoc array of name/value pairs
77b97be7
EM
58 *
59 * @internal param array $ids the array that holds all the db ids
8ffdec17
ARW
60 *
61 * @return object CRM_Case_BAO_CaseType object
62 * @access public
63 * @static
64 */
65 static function add(&$params) {
66 $caseTypeDAO = new CRM_Case_DAO_CaseType();
20173e0e 67
c7bccb5f 68 // form the name only if missing: CRM-627
69 $nameParam = CRM_Utils_Array::value('name', $params, NULL);
70 if (!$nameParam && empty($params['id'])) {
71 $params['name'] = CRM_Utils_String::titleToVar($params['title']);
72 }
73
20173e0e 74 // function to format definition column
c2f2bbe6
TO
75 if (isset($params['definition']) && is_array($params['definition'])) {
76 $params['definition'] = self::convertDefinitionToXML($params['name'], $params['definition']);
77 }
20173e0e 78
88396939 79 $caseTypeDAO->copyValues($params);
8ffdec17
ARW
80 return $caseTypeDAO->save();
81 }
82
20173e0e 83 /**
84 * Function to format / convert submitted array to xml for case type definition
85 *
10b0bbee
TO
86 * @param string $name
87 * @param array $definition the case-type defintion expressed as an array-tree
88 * @return string XML
20173e0e 89 * @static
90 * @access public
91 */
10b0bbee 92 static function convertDefinitionToXML($name, $definition) {
20173e0e 93 $xmlFile = '<?xml version="1.0" encoding="iso-8859-1" ?>' . "\n\n<CaseType>\n";
10b0bbee 94 $xmlFile .= "<name>{$name}</name>\n";
20173e0e 95
728b5029 96 if (isset($definition['activityTypes'])) {
20173e0e 97 $xmlFile .= "<ActivityTypes>\n";
10b0bbee 98 foreach ($definition['activityTypes'] as $values) {
20173e0e 99 $xmlFile .= "<ActivityType>\n";
100 foreach ($values as $key => $value) {
101 $xmlFile .= "<{$key}>{$value}</{$key}>\n";
102 }
103 $xmlFile .= "</ActivityType>\n";
104 }
105 $xmlFile .= "</ActivityTypes>\n";
106 }
107
728b5029 108 if (isset($definition['activitySets'])) {
20173e0e 109 $xmlFile .= "<ActivitySets>\n";
10b0bbee 110 foreach ($definition['activitySets'] as $k => $val) {
834bc8e2 111 $xmlFile .= "<ActivitySet>\n";
112 foreach ($val as $index => $setVal) {
2f58fb09
TO
113 switch ($index) {
114 case 'activityTypes':
115 if (!empty($setVal)) {
116 $xmlFile .= "<ActivityTypes>\n";
117 foreach ($setVal as $values) {
118 $xmlFile .= "<ActivityType>\n";
119 foreach ($values as $key => $value) {
120 $xmlFile .= "<{$key}>{$value}</{$key}>\n";
121 }
122 $xmlFile .= "</ActivityType>\n";
834bc8e2 123 }
2f58fb09 124 $xmlFile .= "</ActivityTypes>\n";
20173e0e 125 }
2f58fb09
TO
126 break;
127 case 'sequence': // passthrough
128 case 'timeline':
129 if ($setVal) {
130 $xmlFile .= "<{$index}>true</{$index}>\n";
131 }
132 break;
133 default:
134 $xmlFile .= "<{$index}>{$setVal}</{$index}>\n";
20173e0e 135 }
136 }
834bc8e2 137
138 $xmlFile .= "</ActivitySet>\n";
20173e0e 139 }
140
20173e0e 141 $xmlFile .= "</ActivitySets>\n";
142 }
143
728b5029 144 if (isset($definition['caseRoles'])) {
20173e0e 145 $xmlFile .= "<CaseRoles>\n";
10b0bbee 146 foreach ($definition['caseRoles'] as $values) {
20173e0e 147 $xmlFile .= "<RelationshipType>\n";
148 foreach ($values as $key => $value) {
149 $xmlFile .= "<{$key}>{$value}</{$key}>\n";
150 }
151 $xmlFile .= "</RelationshipType>\n";
152 }
153 $xmlFile .= "</CaseRoles>\n";
154 }
155
156 $xmlFile .= '</CaseType>';
10b0bbee 157 return $xmlFile;
20173e0e 158 }
159
160 /**
161 * Function to get the case definition either from db or read from xml file
162 *
10b0bbee 163 * @param SimpleXmlElement $xml a single case-type record
20173e0e 164 *
93d47462 165 * @return array the definition of the case-type, expressed as PHP array-tree
20173e0e 166 * @static
167 */
10b0bbee 168 static function convertXmlToDefinition($xml) {
93d47462
TO
169 // build PHP array based on definition
170 $definition = array();
20173e0e 171
172 // set activity types
728b5029
TO
173 if (isset($xml->ActivityTypes)) {
174 $definition['activityTypes'] = array();
175 foreach ($xml->ActivityTypes->ActivityType as $activityTypeXML) {
176 $definition['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
177 }
ae195e71 178 }
179
728b5029
TO
180 // set activity sets
181 if (isset($xml->ActivitySets)) {
182 $definition['activitySets'] = array();
183 foreach ($xml->ActivitySets->ActivitySet as $activitySetXML) {
184 // parse basic properties
2f58fb09
TO
185 $activitySet = array();
186 $activitySet['name'] = (string) $activitySetXML->name;
187 $activitySet['label'] = (string) $activitySetXML->label;
188 if ('true' == (string) $activitySetXML->timeline) {
189 $activitySet['timeline'] = 1;
190 }
191 if ('true' == (string) $activitySetXML->sequence) {
192 $activitySet['sequence'] = 1;
193 }
728b5029 194
728b5029
TO
195 if (isset($activitySetXML->ActivityTypes)) {
196 $activitySet['activityTypes'] = array();
197 foreach ($activitySetXML->ActivityTypes->ActivityType as $activityTypeXML) {
198 $activitySet['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
199 }
ae195e71 200 }
728b5029 201 $definition['activitySets'][] = $activitySet;
ae195e71 202 }
b6e48667 203 }
20173e0e 204
205 // set case roles
728b5029
TO
206 if (isset($xml->CaseRoles)) {
207 $definition['caseRoles'] = array();
208 foreach ($xml->CaseRoles->RelationshipType as $caseRoleXml) {
209 $definition['caseRoles'][] = json_decode(json_encode($caseRoleXml), TRUE);
210 }
211 }
20173e0e 212
93d47462 213 return $definition;
20173e0e 214 }
215
8ffdec17
ARW
216 /**
217 * Given the list of params in the params array, fetch the object
218 * and store the values in the values array
219 *
220 * @param array $params input parameters to find object
221 * @param array $values output values of the object
77b97be7
EM
222 *
223 * @internal param array $ids the array that holds all the db ids
8ffdec17
ARW
224 *
225 * @return CRM_Case_BAO_CaseType|null the found object or null
226 * @access public
227 * @static
228 */
229 static function &getValues(&$params, &$values) {
230 $caseType = new CRM_Case_BAO_CaseType();
231
232 $caseType->copyValues($params);
233
234 if ($caseType->find(TRUE)) {
235 CRM_Core_DAO::storeValues($caseType, $values);
236 return $caseType;
237 }
238 return NULL;
239 }
240
241 /**
242 * takes an associative array and creates a case type object
243 *
244 * @param array $params (reference ) an assoc array of name/value pairs
77b97be7
EM
245 *
246 * @internal param array $ids the array that holds all the db ids
8ffdec17
ARW
247 *
248 * @return object CRM_Case_BAO_CaseType object
249 * @access public
250 * @static
251 */
252 static function &create(&$params) {
253 $transaction = new CRM_Core_Transaction();
254
255 if (!empty($params['id'])) {
256 CRM_Utils_Hook::pre('edit', 'CaseType', $params['id'], $params);
257 }
258 else {
259 CRM_Utils_Hook::pre('create', 'CaseType', NULL, $params);
260 }
261
262 $caseType = self::add($params);
263
264 if (is_a($caseType, 'CRM_Core_Error')) {
265 $transaction->rollback();
266 return $caseType;
267 }
268
269 if (!empty($params['id'])) {
270 CRM_Utils_Hook::post('edit', 'CaseType', $caseType->id, $case);
271 }
272 else {
273 CRM_Utils_Hook::post('create', 'CaseType', $caseType->id, $case);
274 }
275 $transaction->commit();
276
277 return $caseType;
278 }
279
280 /**
281 * Takes a bunch of params that are needed to match certain criteria and
282 * retrieves the relevant objects. We'll tweak this function to be more
283 * full featured over a period of time. This is the inverse function of
284 * create. It also stores all the retrieved values in the default array
285 *
da6b46f4 286 * @param array $params (reference ) an assoc array of name/value pairs
8ffdec17
ARW
287 * @param array $defaults (reference ) an assoc array to hold the name / value pairs
288 * in a hierarchical manner
da6b46f4
EM
289 *
290 * @internal param array $ids (reference) the array that holds all the db ids
8ffdec17
ARW
291 *
292 * @return object CRM_Case_BAO_CaseType object
293 * @access public
294 * @static
295 */
296 static function retrieve(&$params, &$defaults) {
297 $caseType = CRM_Case_BAO_CaseType::getValues($params, $defaults);
298 return $caseType;
299 }
300
4c6ce474
EM
301 /**
302 * @param $caseTypeId
303 *
304 * @return mixed
305 */
8ffdec17
ARW
306 static function del($caseTypeId) {
307 $caseType = new CRM_Case_DAO_CaseType();
308 $caseType->id = $caseTypeId;
309 return $caseType->delete();
310 }
311}