Merge pull request #13967 from eileenmcnaughton/activity_token
[civicrm-core.git] / CRM / Case / BAO / CaseType.php
CommitLineData
8ffdec17
ARW
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
8ffdec17 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
8ffdec17
ARW
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 */
8ffdec17
ARW
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
8ffdec17
ARW
32 */
33
34/**
67d19299 35 * This class contains the functions for Case Type management.
8ffdec17
ARW
36 */
37class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
38
39 /**
d2e5d2ce 40 * Static field for all the case information that we can potentially export.
8ffdec17
ARW
41 *
42 * @var array
8ffdec17
ARW
43 */
44 static $_exportableFields = NULL;
45
46 /**
d2e5d2ce 47 * Takes an associative array and creates a Case Type object.
8ffdec17
ARW
48 *
49 * the function extract all the params it needs to initialize the create a
50 * case type object. the params array could contain additional unused name/value
51 * pairs
52 *
64bd5a0e
TO
53 * @param array $params
54 * (reference ) an assoc array of name/value pairs.
77b97be7 55 *
c490a46a 56 * @throws CRM_Core_Exception
8ffdec17 57 *
16b10e64 58 * @return CRM_Case_BAO_CaseType
8ffdec17 59 */
00be9182 60 public static function add(&$params) {
8ffdec17 61 $caseTypeDAO = new CRM_Case_DAO_CaseType();
20173e0e 62
c7bccb5f 63 // form the name only if missing: CRM-627
64 $nameParam = CRM_Utils_Array::value('name', $params, NULL);
65 if (!$nameParam && empty($params['id'])) {
66 $params['name'] = CRM_Utils_String::titleToVar($params['title']);
67 }
46ec593d
TO
68
69 // Old case-types (pre-4.5) may keep their ucky names, but new case-types must satisfy isValidName()
70 if (empty($params['id']) && !empty($params['name']) && !CRM_Case_BAO_CaseType::isValidName($params['name'])) {
71 throw new CRM_Core_Exception("Cannot create new case-type with malformed name [{$params['name']}]");
076f81b6 72 }
c7bccb5f 73
32f1c917
TO
74 $caseTypeName = (isset($params['name'])) ? $params['name'] : CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $params['id'], 'name', 'id', TRUE);
75
20173e0e 76 // function to format definition column
c2f2bbe6 77 if (isset($params['definition']) && is_array($params['definition'])) {
32f1c917 78 $params['definition'] = self::convertDefinitionToXML($caseTypeName, $params['definition']);
ba3228d1 79 CRM_Core_ManagedEntities::scheduleReconciliation();
c2f2bbe6 80 }
20173e0e 81
88396939 82 $caseTypeDAO->copyValues($params);
e492e278
TO
83 $result = $caseTypeDAO->save();
84 CRM_Case_XMLRepository::singleton()->flush();
85 return $result;
8ffdec17
ARW
86 }
87
cd5823ae
EM
88 /**
89 * Generate and assign an arbitrary value to a field of a test object.
90 *
91 * @param string $fieldName
92 * @param array $fieldDef
93 * @param int $counter
94 * The globally-unique ID of the test object.
95 */
1753bd71 96 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
e547f744 97 if ($fieldName == 'definition') {
1753bd71 98 $this->{$fieldName} = "<CaseType><name>TestCaseType{$counter}</name></CaseType>";
0db6c3e1
TO
99 }
100 else {
1753bd71
TO
101 parent::assignTestValue($fieldName, $fieldDef, $counter);
102 }
103 }
104
105
20173e0e 106 /**
100fef9d 107 * Format / convert submitted array to xml for case type definition
20173e0e 108 *
10b0bbee 109 * @param string $name
64bd5a0e 110 * @param array $definition
b44e3f84 111 * The case-type definition expressed as an array-tree.
a6c01b45
CW
112 * @return string
113 * XML
20173e0e 114 */
00be9182 115 public static function convertDefinitionToXML($name, $definition) {
c9c175f9 116 $xmlFile = '<?xml version="1.0" encoding="utf-8" ?>' . "\n\n<CaseType>\n";
e0d34d59 117 $xmlFile .= "<name>" . self::encodeXmlString($name) . "</name>\n";
20173e0e 118
466fba66
TO
119 if (array_key_exists('forkable', $definition)) {
120 $xmlFile .= "<forkable>" . ((int) $definition['forkable']) . "</forkable>\n";
121 }
122
728b5029 123 if (isset($definition['activityTypes'])) {
20173e0e 124 $xmlFile .= "<ActivityTypes>\n";
10b0bbee 125 foreach ($definition['activityTypes'] as $values) {
20173e0e 126 $xmlFile .= "<ActivityType>\n";
127 foreach ($values as $key => $value) {
e0d34d59 128 $xmlFile .= "<{$key}>" . self::encodeXmlString($value) . "</{$key}>\n";
20173e0e 129 }
130 $xmlFile .= "</ActivityType>\n";
131 }
132 $xmlFile .= "</ActivityTypes>\n";
133 }
134
7c2b40d1
CW
135 if (!empty($definition['statuses'])) {
136 $xmlFile .= "<Statuses>\n";
137 foreach ($definition['statuses'] as $value) {
138 $xmlFile .= "<Status>$value</Status>\n";
139 }
140 $xmlFile .= "</Statuses>\n";
141 }
142
728b5029 143 if (isset($definition['activitySets'])) {
20173e0e 144 $xmlFile .= "<ActivitySets>\n";
10b0bbee 145 foreach ($definition['activitySets'] as $k => $val) {
834bc8e2 146 $xmlFile .= "<ActivitySet>\n";
147 foreach ($val as $index => $setVal) {
2f58fb09
TO
148 switch ($index) {
149 case 'activityTypes':
150 if (!empty($setVal)) {
151 $xmlFile .= "<ActivityTypes>\n";
152 foreach ($setVal as $values) {
153 $xmlFile .= "<ActivityType>\n";
154 foreach ($values as $key => $value) {
e0d34d59 155 $xmlFile .= "<{$key}>" . self::encodeXmlString($value) . "</{$key}>\n";
2f58fb09
TO
156 }
157 $xmlFile .= "</ActivityType>\n";
834bc8e2 158 }
2f58fb09 159 $xmlFile .= "</ActivityTypes>\n";
20173e0e 160 }
2f58fb09 161 break;
e547f744 162
2f58fb09
TO
163 case 'sequence': // passthrough
164 case 'timeline':
165 if ($setVal) {
166 $xmlFile .= "<{$index}>true</{$index}>\n";
167 }
168 break;
e547f744 169
2f58fb09 170 default:
e0d34d59 171 $xmlFile .= "<{$index}>" . self::encodeXmlString($setVal) . "</{$index}>\n";
20173e0e 172 }
173 }
834bc8e2 174
175 $xmlFile .= "</ActivitySet>\n";
20173e0e 176 }
177
20173e0e 178 $xmlFile .= "</ActivitySets>\n";
179 }
180
728b5029 181 if (isset($definition['caseRoles'])) {
20173e0e 182 $xmlFile .= "<CaseRoles>\n";
10b0bbee 183 foreach ($definition['caseRoles'] as $values) {
20173e0e 184 $xmlFile .= "<RelationshipType>\n";
185 foreach ($values as $key => $value) {
e0d34d59 186 $xmlFile .= "<{$key}>" . self::encodeXmlString($value) . "</{$key}>\n";
20173e0e 187 }
188 $xmlFile .= "</RelationshipType>\n";
189 }
190 $xmlFile .= "</CaseRoles>\n";
191 }
192
06f21064
TO
193 if (array_key_exists('restrictActivityAsgmtToCmsUser', $definition)) {
194 $xmlFile .= "<RestrictActivityAsgmtToCmsUser>" . $definition['restrictActivityAsgmtToCmsUser'] . "</RestrictActivityAsgmtToCmsUser>\n";
195 }
196
197 if (!empty($definition['activityAsgmtGrps'])) {
198 $xmlFile .= "<ActivityAsgmtGrps>\n";
199 foreach ($definition['activityAsgmtGrps'] as $value) {
200 $xmlFile .= "<Group>$value</Group>\n";
201 }
202 $xmlFile .= "</ActivityAsgmtGrps>\n";
203 }
204
20173e0e 205 $xmlFile .= '</CaseType>';
06f21064 206
10b0bbee 207 return $xmlFile;
20173e0e 208 }
209
e0d34d59
TO
210 /**
211 * Ugh. This shouldn't exist. Use a real XML-encoder.
212 *
213 * Escape a string for use in XML.
214 *
215 * @param string $str
216 * A string which should outputted to XML.
217 * @return string
218 * @deprecated
219 */
220 protected static function encodeXmlString($str) {
221 // PHP 5.4: return htmlspecialchars($str, ENT_XML1, 'UTF-8')
222 return htmlspecialchars($str);
223 }
224
20173e0e 225 /**
d2e5d2ce 226 * Get the case definition either from db or read from xml file.
20173e0e 227 *
64bd5a0e
TO
228 * @param SimpleXmlElement $xml
229 * A single case-type record.
20173e0e 230 *
a6c01b45
CW
231 * @return array
232 * the definition of the case-type, expressed as PHP array-tree
20173e0e 233 */
00be9182 234 public static function convertXmlToDefinition($xml) {
93d47462
TO
235 // build PHP array based on definition
236 $definition = array();
20173e0e 237
466fba66
TO
238 if (isset($xml->forkable)) {
239 $definition['forkable'] = (int) $xml->forkable;
240 }
241
06f21064
TO
242 if (isset($xml->RestrictActivityAsgmtToCmsUser)) {
243 $definition['restrictActivityAsgmtToCmsUser'] = (int) $xml->RestrictActivityAsgmtToCmsUser;
244 }
245
246 if (isset($xml->ActivityAsgmtGrps)) {
247 $definition['activityAsgmtGrps'] = (array) $xml->ActivityAsgmtGrps->Group;
248 }
249
20173e0e 250 // set activity types
728b5029
TO
251 if (isset($xml->ActivityTypes)) {
252 $definition['activityTypes'] = array();
253 foreach ($xml->ActivityTypes->ActivityType as $activityTypeXML) {
254 $definition['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
255 }
ae195e71 256 }
257
7c2b40d1
CW
258 // set statuses
259 if (isset($xml->Statuses)) {
260 $definition['statuses'] = (array) $xml->Statuses->Status;
261 }
262
728b5029
TO
263 // set activity sets
264 if (isset($xml->ActivitySets)) {
265 $definition['activitySets'] = array();
093f1cfd
AP
266 $definition['timelineActivityTypes'] = array();
267
728b5029
TO
268 foreach ($xml->ActivitySets->ActivitySet as $activitySetXML) {
269 // parse basic properties
2f58fb09
TO
270 $activitySet = array();
271 $activitySet['name'] = (string) $activitySetXML->name;
272 $activitySet['label'] = (string) $activitySetXML->label;
273 if ('true' == (string) $activitySetXML->timeline) {
274 $activitySet['timeline'] = 1;
275 }
276 if ('true' == (string) $activitySetXML->sequence) {
277 $activitySet['sequence'] = 1;
278 }
728b5029 279
728b5029
TO
280 if (isset($activitySetXML->ActivityTypes)) {
281 $activitySet['activityTypes'] = array();
282 foreach ($activitySetXML->ActivityTypes->ActivityType as $activityTypeXML) {
093f1cfd
AP
283 $activityType = json_decode(json_encode($activityTypeXML), TRUE);
284 $activitySet['activityTypes'][] = $activityType;
285 if ($activitySetXML->timeline) {
286 $definition['timelineActivityTypes'][] = $activityType;
287 }
728b5029 288 }
ae195e71 289 }
728b5029 290 $definition['activitySets'][] = $activitySet;
ae195e71 291 }
b6e48667 292 }
20173e0e 293
294 // set case roles
728b5029
TO
295 if (isset($xml->CaseRoles)) {
296 $definition['caseRoles'] = array();
297 foreach ($xml->CaseRoles->RelationshipType as $caseRoleXml) {
298 $definition['caseRoles'][] = json_decode(json_encode($caseRoleXml), TRUE);
299 }
300 }
20173e0e 301
93d47462 302 return $definition;
20173e0e 303 }
304
8ffdec17
ARW
305 /**
306 * Given the list of params in the params array, fetch the object
307 * and store the values in the values array
308 *
64bd5a0e
TO
309 * @param array $params
310 * Input parameters to find object.
311 * @param array $values
312 * Output values of the object.
77b97be7 313 *
8ffdec17 314 * @return CRM_Case_BAO_CaseType|null the found object or null
8ffdec17 315 */
00be9182 316 public static function &getValues(&$params, &$values) {
8ffdec17
ARW
317 $caseType = new CRM_Case_BAO_CaseType();
318
319 $caseType->copyValues($params);
320
321 if ($caseType->find(TRUE)) {
322 CRM_Core_DAO::storeValues($caseType, $values);
323 return $caseType;
324 }
325 return NULL;
326 }
327
328 /**
d2e5d2ce 329 * Takes an associative array and creates a case type object.
8ffdec17 330 *
64bd5a0e
TO
331 * @param array $params
332 * (reference ) an assoc array of name/value pairs.
77b97be7 333 *
16b10e64 334 * @return CRM_Case_BAO_CaseType
8ffdec17 335 */
00be9182 336 public static function &create(&$params) {
8ffdec17
ARW
337 $transaction = new CRM_Core_Transaction();
338
339 if (!empty($params['id'])) {
340 CRM_Utils_Hook::pre('edit', 'CaseType', $params['id'], $params);
341 }
342 else {
343 CRM_Utils_Hook::pre('create', 'CaseType', NULL, $params);
344 }
345
346 $caseType = self::add($params);
347
348 if (is_a($caseType, 'CRM_Core_Error')) {
349 $transaction->rollback();
350 return $caseType;
351 }
352
353 if (!empty($params['id'])) {
354 CRM_Utils_Hook::post('edit', 'CaseType', $caseType->id, $case);
355 }
356 else {
357 CRM_Utils_Hook::post('create', 'CaseType', $caseType->id, $case);
358 }
359 $transaction->commit();
10ffff26 360 CRM_Case_XMLRepository::singleton(TRUE);
31c28ed5 361 CRM_Core_OptionGroup::flushAll();
8ffdec17
ARW
362
363 return $caseType;
364 }
365
366 /**
fe482240
EM
367 * Retrieve DB object based on input parameters.
368 *
369 * It also stores all the retrieved values in the default array.
8ffdec17 370 *
64bd5a0e
TO
371 * @param array $params
372 * (reference ) an assoc array of name/value pairs.
373 * @param array $defaults
374 * (reference ) an assoc array to hold the name / value pairs.
8ffdec17 375 * in a hierarchical manner
da6b46f4 376 *
16b10e64 377 * @return CRM_Case_BAO_CaseType
8ffdec17 378 */
00be9182 379 public static function retrieve(&$params, &$defaults) {
8ffdec17
ARW
380 $caseType = CRM_Case_BAO_CaseType::getValues($params, $defaults);
381 return $caseType;
382 }
383
4c6ce474 384 /**
100fef9d 385 * @param int $caseTypeId
4c6ce474 386 *
c490a46a 387 * @throws CRM_Core_Exception
4c6ce474
EM
388 * @return mixed
389 */
00be9182 390 public static function del($caseTypeId) {
8ffdec17
ARW
391 $caseType = new CRM_Case_DAO_CaseType();
392 $caseType->id = $caseTypeId;
10ffff26
TO
393 $refCounts = $caseType->getReferenceCounts();
394 $total = array_sum(CRM_Utils_Array::collect('count', $refCounts));
395 if ($total) {
84dfe0f2 396 throw new CRM_Core_Exception(ts("You can not delete this case type -- it is assigned to %1 existing case record(s). If you do not want this case type to be used going forward, consider disabling it instead.", array(1 => $total)));
10ffff26
TO
397 }
398 $result = $caseType->delete();
399 CRM_Case_XMLRepository::singleton(TRUE);
400 return $result;
8ffdec17 401 }
076f81b6 402
403 /**
404 * Determine if a case-type name is well-formed
405 *
406 * @param string $caseType
407 * @return bool
408 */
00be9182 409 public static function isValidName($caseType) {
e547f744 410 return preg_match('/^[a-zA-Z0-9_]+$/', $caseType);
076f81b6 411 }
32f1c917
TO
412
413 /**
414 * Determine if the case-type has *both* DB and file-based definitions.
415 *
416 * @param int $caseTypeId
72b3a70c
CW
417 * @return bool|null
418 * TRUE if there are *both* DB and file-based definitions
32f1c917 419 */
00be9182 420 public static function isForked($caseTypeId) {
32f1c917
TO
421 $caseTypeName = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
422 if ($caseTypeName) {
423 $dbDefinition = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'definition', 'id', TRUE);
424 $fileDefinition = CRM_Case_XMLRepository::singleton()->retrieveFile($caseTypeName);
425 return $fileDefinition && $dbDefinition;
426 }
427 return NULL;
428 }
429
430 /**
431 * Determine if modifications are allowed on the case-type
432 *
433 * @param int $caseTypeId
a6c01b45
CW
434 * @return bool
435 * TRUE if the definition can be modified
32f1c917 436 */
00be9182 437 public static function isForkable($caseTypeId) {
32f1c917
TO
438 $caseTypeName = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
439 if ($caseTypeName) {
440 // if file-based definition explicitly disables "forkable" option, then don't allow changes to definition
441 $fileDefinition = CRM_Case_XMLRepository::singleton()->retrieveFile($caseTypeName);
2210eab6 442 if ($fileDefinition && isset($fileDefinition->forkable)) {
e547f744 443 return CRM_Utils_String::strtobool((string) $fileDefinition->forkable);
32f1c917
TO
444 }
445 }
446 return TRUE;
447 }
96025800 448
8ffdec17 449}