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 = CRM_Utils_Array
::value('name', $params, 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}>" . 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 ($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
;
239 // set activity types
240 if (isset($xml->ActivityTypes
)) {
241 $definition['activityTypes'] = [];
242 foreach ($xml->ActivityTypes
->ActivityType
as $activityTypeXML) {
243 $definition['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
248 if (isset($xml->Statuses
)) {
249 $definition['statuses'] = (array) $xml->Statuses
->Status
;
253 if (isset($xml->ActivitySets
)) {
254 $definition['activitySets'] = [];
255 $definition['timelineActivityTypes'] = [];
257 foreach ($xml->ActivitySets
->ActivitySet
as $activitySetXML) {
258 // parse basic properties
260 $activitySet['name'] = (string) $activitySetXML->name
;
261 $activitySet['label'] = (string) $activitySetXML->label
;
262 if ('true' == (string) $activitySetXML->timeline
) {
263 $activitySet['timeline'] = 1;
265 if ('true' == (string) $activitySetXML->sequence
) {
266 $activitySet['sequence'] = 1;
269 if (isset($activitySetXML->ActivityTypes
)) {
270 $activitySet['activityTypes'] = [];
271 foreach ($activitySetXML->ActivityTypes
->ActivityType
as $activityTypeXML) {
272 $activityType = json_decode(json_encode($activityTypeXML), TRUE);
273 $activitySet['activityTypes'][] = $activityType;
274 if ($activitySetXML->timeline
) {
275 $definition['timelineActivityTypes'][] = $activityType;
279 $definition['activitySets'][] = $activitySet;
284 if (isset($xml->CaseRoles
)) {
285 $definition['caseRoles'] = [];
286 foreach ($xml->CaseRoles
->RelationshipType
as $caseRoleXml) {
287 $definition['caseRoles'][] = json_decode(json_encode($caseRoleXml), TRUE);
295 * Given the list of params in the params array, fetch the object
296 * and store the values in the values array
298 * @param array $params
299 * Input parameters to find object.
300 * @param array $values
301 * Output values of the object.
303 * @return CRM_Case_BAO_CaseType|null the found object or null
305 public static function &getValues(&$params, &$values) {
306 $caseType = new CRM_Case_BAO_CaseType();
308 $caseType->copyValues($params);
310 if ($caseType->find(TRUE)) {
311 CRM_Core_DAO
::storeValues($caseType, $values);
318 * Takes an associative array and creates a case type object.
320 * @param array $params
321 * (reference ) an assoc array of name/value pairs.
323 * @return CRM_Case_BAO_CaseType
325 public static function &create(&$params) {
326 $transaction = new CRM_Core_Transaction();
328 if (!empty($params['id'])) {
329 CRM_Utils_Hook
::pre('edit', 'CaseType', $params['id'], $params);
332 CRM_Utils_Hook
::pre('create', 'CaseType', NULL, $params);
335 $caseType = self
::add($params);
337 if (is_a($caseType, 'CRM_Core_Error')) {
338 $transaction->rollback();
342 if (!empty($params['id'])) {
343 CRM_Utils_Hook
::post('edit', 'CaseType', $caseType->id
, $case);
346 CRM_Utils_Hook
::post('create', 'CaseType', $caseType->id
, $case);
348 $transaction->commit();
349 CRM_Case_XMLRepository
::singleton(TRUE);
350 CRM_Core_OptionGroup
::flushAll();
356 * Retrieve DB object based on input parameters.
358 * It also stores all the retrieved values in the default array.
360 * @param array $params
361 * (reference ) an assoc array of name/value pairs.
362 * @param array $defaults
363 * (reference ) an assoc array to hold the name / value pairs.
364 * in a hierarchical manner
366 * @return CRM_Case_BAO_CaseType
368 public static function retrieve(&$params, &$defaults) {
369 $caseType = CRM_Case_BAO_CaseType
::getValues($params, $defaults);
374 * @param int $caseTypeId
376 * @throws CRM_Core_Exception
379 public static function del($caseTypeId) {
380 $caseType = new CRM_Case_DAO_CaseType();
381 $caseType->id
= $caseTypeId;
382 $refCounts = $caseType->getReferenceCounts();
383 $total = array_sum(CRM_Utils_Array
::collect('count', $refCounts));
385 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]));
387 $result = $caseType->delete();
388 CRM_Case_XMLRepository
::singleton(TRUE);
393 * Determine if a case-type name is well-formed
395 * @param string $caseType
398 public static function isValidName($caseType) {
399 return preg_match('/^[a-zA-Z0-9_]+$/', $caseType);
403 * Determine if the case-type has *both* DB and file-based definitions.
405 * @param int $caseTypeId
407 * TRUE if there are *both* DB and file-based definitions
409 public static function isForked($caseTypeId) {
410 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
412 $dbDefinition = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'definition', 'id', TRUE);
413 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
414 return $fileDefinition && $dbDefinition;
420 * Determine if modifications are allowed on the case-type
422 * @param int $caseTypeId
424 * TRUE if the definition can be modified
426 public static function isForkable($caseTypeId) {
427 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
429 // if file-based definition explicitly disables "forkable" option, then don't allow changes to definition
430 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
431 if ($fileDefinition && isset($fileDefinition->forkable
)) {
432 return CRM_Utils_String
::strtobool((string) $fileDefinition->forkable
);