3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class contains the functions for Case Type management.
21 class CRM_Case_BAO_CaseType
extends CRM_Case_DAO_CaseType
{
24 * Static field for all the case information that we can potentially export.
28 public static $_exportableFields = NULL;
31 * Takes an associative array and creates a Case Type object.
33 * the function extract all the params it needs to initialize the create a
34 * case type object. the params array could contain additional unused name/value
37 * @param array $params
38 * (reference ) an assoc array of name/value pairs.
40 * @throws CRM_Core_Exception
42 * @return CRM_Case_BAO_CaseType
44 public static function add(&$params) {
45 $caseTypeDAO = new CRM_Case_DAO_CaseType();
47 // form the name only if missing: CRM-627
48 $nameParam = $params['name'] ??
NULL;
49 if (!$nameParam && empty($params['id'])) {
50 $params['name'] = CRM_Utils_String
::titleToVar($params['title']);
53 // Old case-types (pre-4.5) may keep their ucky names, but new case-types must satisfy isValidName()
54 if (empty($params['id']) && !empty($params['name']) && !CRM_Case_BAO_CaseType
::isValidName($params['name'])) {
55 throw new CRM_Core_Exception("Cannot create new case-type with malformed name [{$params['name']}]");
58 $caseTypeName = (isset($params['name'])) ?
$params['name'] : CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $params['id'], 'name', 'id', TRUE);
60 // function to format definition column
61 if (isset($params['definition']) && is_array($params['definition'])) {
62 $params['definition'] = self
::convertDefinitionToXML($caseTypeName, $params['definition']);
63 CRM_Core_ManagedEntities
::scheduleReconciliation();
66 $caseTypeDAO->copyValues($params);
67 $result = $caseTypeDAO->save();
68 CRM_Case_XMLRepository
::singleton()->flush();
73 * Generate and assign an arbitrary value to a field of a test object.
75 * @param string $fieldName
76 * @param array $fieldDef
78 * The globally-unique ID of the test object.
80 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
81 if ($fieldName == 'definition') {
82 $this->{$fieldName} = "<CaseType><name>TestCaseType{$counter}</name></CaseType>";
85 parent
::assignTestValue($fieldName, $fieldDef, $counter);
90 * Format / convert submitted array to xml for case type definition
93 * @param array $definition
94 * The case-type definition expressed as an array-tree.
98 public static function convertDefinitionToXML($name, $definition) {
99 $xmlFile = '<?xml version="1.0" encoding="utf-8" ?>' . "\n\n<CaseType>\n";
100 $xmlFile .= "<name>" . self
::encodeXmlString($name) . "</name>\n";
102 if (array_key_exists('forkable', $definition)) {
103 $xmlFile .= "<forkable>" . ((int) $definition['forkable']) . "</forkable>\n";
106 if (isset($definition['activityTypes'])) {
107 $xmlFile .= "<ActivityTypes>\n";
108 foreach ($definition['activityTypes'] as $values) {
109 $xmlFile .= "<ActivityType>\n";
110 foreach ($values as $key => $value) {
111 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
113 $xmlFile .= "</ActivityType>\n";
115 $xmlFile .= "</ActivityTypes>\n";
118 if (!empty($definition['statuses'])) {
119 $xmlFile .= "<Statuses>\n";
120 foreach ($definition['statuses'] as $value) {
121 $xmlFile .= "<Status>$value</Status>\n";
123 $xmlFile .= "</Statuses>\n";
126 if (isset($definition['activitySets'])) {
127 $xmlFile .= "<ActivitySets>\n";
128 foreach ($definition['activitySets'] as $k => $val) {
129 $xmlFile .= "<ActivitySet>\n";
130 foreach ($val as $index => $setVal) {
132 case 'activityTypes':
133 if (!empty($setVal)) {
134 $xmlFile .= "<ActivityTypes>\n";
135 foreach ($setVal as $values) {
136 $xmlFile .= "<ActivityType>\n";
137 foreach ($values as $key => $value) {
138 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
140 $xmlFile .= "</ActivityType>\n";
142 $xmlFile .= "</ActivityTypes>\n";
150 $xmlFile .= "<{$index}>true</{$index}>\n";
155 $xmlFile .= "<{$index}>" . self
::encodeXmlString($setVal) . "</{$index}>\n";
159 $xmlFile .= "</ActivitySet>\n";
162 $xmlFile .= "</ActivitySets>\n";
165 if (isset($definition['caseRoles'])) {
166 $xmlFile .= "<CaseRoles>\n";
167 foreach ($definition['caseRoles'] as $values) {
168 $xmlFile .= "<RelationshipType>\n";
169 foreach ($values as $key => $value) {
170 $xmlFile .= "<{$key}>" . ($key == 'groups' ?
implode(',', array_map(['\CRM_Case_BAO_CaseType', 'encodeXmlString'], (array) $value)) : self
::encodeXmlString($value)) . "</{$key}>\n";
172 $xmlFile .= "</RelationshipType>\n";
174 $xmlFile .= "</CaseRoles>\n";
177 if (array_key_exists('restrictActivityAsgmtToCmsUser', $definition)) {
178 $xmlFile .= "<RestrictActivityAsgmtToCmsUser>" . $definition['restrictActivityAsgmtToCmsUser'] . "</RestrictActivityAsgmtToCmsUser>\n";
181 if (!empty($definition['activityAsgmtGrps'])) {
182 $xmlFile .= "<ActivityAsgmtGrps>\n";
183 foreach ((array) $definition['activityAsgmtGrps'] as $value) {
184 $xmlFile .= "<Group>$value</Group>\n";
186 $xmlFile .= "</ActivityAsgmtGrps>\n";
189 $xmlFile .= '</CaseType>';
195 * Ugh. This shouldn't exist. Use a real XML-encoder.
197 * Escape a string for use in XML.
200 * A string which should outputted to XML.
204 protected static function encodeXmlString($str) {
205 // PHP 5.4: return htmlspecialchars($str, ENT_XML1, 'UTF-8')
206 if (is_scalar($str)) {
207 return htmlspecialchars($str);
215 * Get the case definition either from db or read from xml file.
217 * @param SimpleXmlElement $xml
218 * A single case-type record.
221 * the definition of the case-type, expressed as PHP array-tree
223 public static function convertXmlToDefinition($xml) {
224 // build PHP array based on definition
227 if (isset($xml->forkable
)) {
228 $definition['forkable'] = (int) $xml->forkable
;
231 if (isset($xml->RestrictActivityAsgmtToCmsUser
)) {
232 $definition['restrictActivityAsgmtToCmsUser'] = (int) $xml->RestrictActivityAsgmtToCmsUser
;
235 if (isset($xml->ActivityAsgmtGrps
)) {
236 $definition['activityAsgmtGrps'] = (array) $xml->ActivityAsgmtGrps
->Group
;
237 // Backwards compat - convert group ids to group names if ids are supplied
238 if (array_filter($definition['activityAsgmtGrps'], ['\CRM_Utils_Rule', 'integer']) === $definition['activityAsgmtGrps']) {
239 foreach ($definition['activityAsgmtGrps'] as $idx => $group) {
240 $definition['activityAsgmtGrps'][$idx] = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_Group', $group);
245 // set activity types
246 if (isset($xml->ActivityTypes
)) {
247 $definition['activityTypes'] = [];
248 foreach ($xml->ActivityTypes
->ActivityType
as $activityTypeXML) {
249 $definition['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
254 if (isset($xml->Statuses
)) {
255 $definition['statuses'] = (array) $xml->Statuses
->Status
;
259 if (isset($xml->ActivitySets
)) {
260 $definition['activitySets'] = [];
261 $definition['timelineActivityTypes'] = [];
263 foreach ($xml->ActivitySets
->ActivitySet
as $activitySetXML) {
264 // parse basic properties
266 $activitySet['name'] = (string) $activitySetXML->name
;
267 $activitySet['label'] = (string) $activitySetXML->label
;
268 if ('true' == (string) $activitySetXML->timeline
) {
269 $activitySet['timeline'] = 1;
271 if ('true' == (string) $activitySetXML->sequence
) {
272 $activitySet['sequence'] = 1;
275 if (isset($activitySetXML->ActivityTypes
)) {
276 $activitySet['activityTypes'] = [];
277 foreach ($activitySetXML->ActivityTypes
->ActivityType
as $activityTypeXML) {
278 $activityType = json_decode(json_encode($activityTypeXML), TRUE);
279 $activitySet['activityTypes'][] = $activityType;
280 if ($activitySetXML->timeline
) {
281 $definition['timelineActivityTypes'][] = $activityType;
285 $definition['activitySets'][] = $activitySet;
290 if (isset($xml->CaseRoles
)) {
291 $definition['caseRoles'] = [];
292 foreach ($xml->CaseRoles
->RelationshipType
as $caseRoleXml) {
293 $caseRole = json_decode(json_encode($caseRoleXml), TRUE);
294 if (!empty($caseRole['groups'])) {
295 $caseRole['groups'] = explode(',', $caseRole['groups']);
297 $definition['caseRoles'][] = $caseRole;
305 * Given the list of params in the params array, fetch the object
306 * and store the values in the values array
308 * @param array $params
309 * Input parameters to find object.
310 * @param array $values
311 * Output values of the object.
313 * @return CRM_Case_BAO_CaseType|null the found object or null
315 public static function &getValues(&$params, &$values) {
316 $caseType = new CRM_Case_BAO_CaseType();
318 $caseType->copyValues($params);
320 if ($caseType->find(TRUE)) {
321 CRM_Core_DAO
::storeValues($caseType, $values);
328 * Takes an associative array and creates a case type object.
330 * @param array $params
331 * (reference ) an assoc array of name/value pairs.
333 * @return CRM_Case_BAO_CaseType
335 public static function &create(&$params) {
336 $transaction = new CRM_Core_Transaction();
338 if (!empty($params['id'])) {
339 CRM_Utils_Hook
::pre('edit', 'CaseType', $params['id'], $params);
342 CRM_Utils_Hook
::pre('create', 'CaseType', NULL, $params);
345 $caseType = self
::add($params);
347 if (is_a($caseType, 'CRM_Core_Error')) {
348 $transaction->rollback();
352 if (!empty($params['id'])) {
353 CRM_Utils_Hook
::post('edit', 'CaseType', $caseType->id
, $case);
356 CRM_Utils_Hook
::post('create', 'CaseType', $caseType->id
, $case);
358 $transaction->commit();
359 CRM_Case_XMLRepository
::singleton(TRUE);
360 CRM_Core_OptionGroup
::flushAll();
366 * Retrieve DB object based on input parameters.
368 * It also stores all the retrieved values in the default array.
370 * @param array $params
371 * (reference ) an assoc array of name/value pairs.
372 * @param array $defaults
373 * (reference ) an assoc array to hold the name / value pairs.
374 * in a hierarchical manner
376 * @return CRM_Case_BAO_CaseType
378 public static function retrieve(&$params, &$defaults) {
379 $caseType = CRM_Case_BAO_CaseType
::getValues($params, $defaults);
384 * @param int $caseTypeId
386 * @throws CRM_Core_Exception
389 public static function del($caseTypeId) {
390 $caseType = new CRM_Case_DAO_CaseType();
391 $caseType->id
= $caseTypeId;
392 $refCounts = $caseType->getReferenceCounts();
393 $total = array_sum(CRM_Utils_Array
::collect('count', $refCounts));
395 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.", [1 => $total]));
397 $result = $caseType->delete();
398 CRM_Case_XMLRepository
::singleton(TRUE);
403 * Determine if a case-type name is well-formed
405 * @param string $caseType
408 public static function isValidName($caseType) {
409 return preg_match('/^[a-zA-Z0-9_]+$/', $caseType);
413 * Determine if the case-type has *both* DB and file-based definitions.
415 * @param int $caseTypeId
417 * TRUE if there are *both* DB and file-based definitions
419 public static function isForked($caseTypeId) {
420 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
422 $dbDefinition = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'definition', 'id', TRUE);
423 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
424 return $fileDefinition && $dbDefinition;
430 * Determine if modifications are allowed on the case-type
432 * @param int $caseTypeId
434 * TRUE if the definition can be modified
436 public static function isForkable($caseTypeId) {
437 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
439 // if file-based definition explicitly disables "forkable" option, then don't allow changes to definition
440 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
441 if ($fileDefinition && isset($fileDefinition->forkable
)) {
442 return CRM_Utils_String
::strtobool((string) $fileDefinition->forkable
);