3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * This class contains the functions for Case Type management.
37 class CRM_Case_BAO_CaseType
extends CRM_Case_DAO_CaseType
{
40 * Static field for all the case information that we can potentially export.
44 static $_exportableFields = NULL;
47 * Takes an associative array and creates a Case Type object.
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
53 * @param array $params
54 * (reference ) an assoc array of name/value pairs.
56 * @throws CRM_Core_Exception
58 * @return CRM_Case_BAO_CaseType
60 public static function add(&$params) {
61 $caseTypeDAO = new CRM_Case_DAO_CaseType();
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']);
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']}]");
74 $caseTypeName = (isset($params['name'])) ?
$params['name'] : CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $params['id'], 'name', 'id', TRUE);
76 // function to format definition column
77 if (isset($params['definition']) && is_array($params['definition'])) {
78 $params['definition'] = self
::convertDefinitionToXML($caseTypeName, $params['definition']);
79 CRM_Core_ManagedEntities
::scheduleReconciliation();
82 $caseTypeDAO->copyValues($params);
83 $result = $caseTypeDAO->save();
84 CRM_Case_XMLRepository
::singleton()->flush();
89 * Generate and assign an arbitrary value to a field of a test object.
91 * @param string $fieldName
92 * @param array $fieldDef
94 * The globally-unique ID of the test object.
96 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
97 if ($fieldName == 'definition') {
98 $this->{$fieldName} = "<CaseType><name>TestCaseType{$counter}</name></CaseType>";
101 parent
::assignTestValue($fieldName, $fieldDef, $counter);
107 * Format / convert submitted array to xml for case type definition
109 * @param string $name
110 * @param array $definition
111 * The case-type definition expressed as an array-tree.
115 public static function convertDefinitionToXML($name, $definition) {
116 $xmlFile = '<?xml version="1.0" encoding="utf-8" ?>' . "\n\n<CaseType>\n";
117 $xmlFile .= "<name>" . self
::encodeXmlString($name) . "</name>\n";
119 if (array_key_exists('forkable', $definition)) {
120 $xmlFile .= "<forkable>" . ((int) $definition['forkable']) . "</forkable>\n";
123 if (isset($definition['activityTypes'])) {
124 $xmlFile .= "<ActivityTypes>\n";
125 foreach ($definition['activityTypes'] as $values) {
126 $xmlFile .= "<ActivityType>\n";
127 foreach ($values as $key => $value) {
128 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
130 $xmlFile .= "</ActivityType>\n";
132 $xmlFile .= "</ActivityTypes>\n";
135 if (!empty($definition['statuses'])) {
136 $xmlFile .= "<Statuses>\n";
137 foreach ($definition['statuses'] as $value) {
138 $xmlFile .= "<Status>$value</Status>\n";
140 $xmlFile .= "</Statuses>\n";
143 if (isset($definition['activitySets'])) {
144 $xmlFile .= "<ActivitySets>\n";
145 foreach ($definition['activitySets'] as $k => $val) {
146 $xmlFile .= "<ActivitySet>\n";
147 foreach ($val as $index => $setVal) {
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) {
155 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
157 $xmlFile .= "</ActivityType>\n";
159 $xmlFile .= "</ActivityTypes>\n";
163 case 'sequence': // passthrough
166 $xmlFile .= "<{$index}>true</{$index}>\n";
171 $xmlFile .= "<{$index}>" . self
::encodeXmlString($setVal) . "</{$index}>\n";
175 $xmlFile .= "</ActivitySet>\n";
178 $xmlFile .= "</ActivitySets>\n";
181 if (isset($definition['caseRoles'])) {
182 $xmlFile .= "<CaseRoles>\n";
183 foreach ($definition['caseRoles'] as $values) {
184 $xmlFile .= "<RelationshipType>\n";
185 foreach ($values as $key => $value) {
186 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
188 $xmlFile .= "</RelationshipType>\n";
190 $xmlFile .= "</CaseRoles>\n";
193 $xmlFile .= '</CaseType>';
198 * Ugh. This shouldn't exist. Use a real XML-encoder.
200 * Escape a string for use in XML.
203 * A string which should outputted to XML.
207 protected static function encodeXmlString($str) {
208 // PHP 5.4: return htmlspecialchars($str, ENT_XML1, 'UTF-8')
209 return htmlspecialchars($str);
213 * Get the case definition either from db or read from xml file.
215 * @param SimpleXmlElement $xml
216 * A single case-type record.
219 * the definition of the case-type, expressed as PHP array-tree
221 public static function convertXmlToDefinition($xml) {
222 // build PHP array based on definition
223 $definition = array();
225 if (isset($xml->forkable
)) {
226 $definition['forkable'] = (int) $xml->forkable
;
229 // set activity types
230 if (isset($xml->ActivityTypes
)) {
231 $definition['activityTypes'] = array();
232 foreach ($xml->ActivityTypes
->ActivityType
as $activityTypeXML) {
233 $definition['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
238 if (isset($xml->Statuses
)) {
239 $definition['statuses'] = (array) $xml->Statuses
->Status
;
243 if (isset($xml->ActivitySets
)) {
244 $definition['activitySets'] = array();
245 foreach ($xml->ActivitySets
->ActivitySet
as $activitySetXML) {
246 // parse basic properties
247 $activitySet = array();
248 $activitySet['name'] = (string) $activitySetXML->name
;
249 $activitySet['label'] = (string) $activitySetXML->label
;
250 if ('true' == (string) $activitySetXML->timeline
) {
251 $activitySet['timeline'] = 1;
253 if ('true' == (string) $activitySetXML->sequence
) {
254 $activitySet['sequence'] = 1;
257 if (isset($activitySetXML->ActivityTypes
)) {
258 $activitySet['activityTypes'] = array();
259 foreach ($activitySetXML->ActivityTypes
->ActivityType
as $activityTypeXML) {
260 $activitySet['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
263 $definition['activitySets'][] = $activitySet;
268 if (isset($xml->CaseRoles
)) {
269 $definition['caseRoles'] = array();
270 foreach ($xml->CaseRoles
->RelationshipType
as $caseRoleXml) {
271 $definition['caseRoles'][] = json_decode(json_encode($caseRoleXml), TRUE);
279 * Given the list of params in the params array, fetch the object
280 * and store the values in the values array
282 * @param array $params
283 * Input parameters to find object.
284 * @param array $values
285 * Output values of the object.
287 * @return CRM_Case_BAO_CaseType|null the found object or null
289 public static function &getValues(&$params, &$values) {
290 $caseType = new CRM_Case_BAO_CaseType();
292 $caseType->copyValues($params);
294 if ($caseType->find(TRUE)) {
295 CRM_Core_DAO
::storeValues($caseType, $values);
302 * Takes an associative array and creates a case type object.
304 * @param array $params
305 * (reference ) an assoc array of name/value pairs.
307 * @return CRM_Case_BAO_CaseType
309 public static function &create(&$params) {
310 $transaction = new CRM_Core_Transaction();
312 if (!empty($params['id'])) {
313 CRM_Utils_Hook
::pre('edit', 'CaseType', $params['id'], $params);
316 CRM_Utils_Hook
::pre('create', 'CaseType', NULL, $params);
319 $caseType = self
::add($params);
321 if (is_a($caseType, 'CRM_Core_Error')) {
322 $transaction->rollback();
326 if (!empty($params['id'])) {
327 CRM_Utils_Hook
::post('edit', 'CaseType', $caseType->id
, $case);
330 CRM_Utils_Hook
::post('create', 'CaseType', $caseType->id
, $case);
332 $transaction->commit();
333 CRM_Case_XMLRepository
::singleton(TRUE);
334 CRM_Core_OptionGroup
::flushAll();
340 * Retrieve DB object based on input parameters.
342 * It also stores all the retrieved values in the default array.
344 * @param array $params
345 * (reference ) an assoc array of name/value pairs.
346 * @param array $defaults
347 * (reference ) an assoc array to hold the name / value pairs.
348 * in a hierarchical manner
350 * @return CRM_Case_BAO_CaseType
352 public static function retrieve(&$params, &$defaults) {
353 $caseType = CRM_Case_BAO_CaseType
::getValues($params, $defaults);
358 * @param int $caseTypeId
360 * @throws CRM_Core_Exception
363 public static function del($caseTypeId) {
364 $caseType = new CRM_Case_DAO_CaseType();
365 $caseType->id
= $caseTypeId;
366 $refCounts = $caseType->getReferenceCounts();
367 $total = array_sum(CRM_Utils_Array
::collect('count', $refCounts));
369 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)));
371 $result = $caseType->delete();
372 CRM_Case_XMLRepository
::singleton(TRUE);
377 * Determine if a case-type name is well-formed
379 * @param string $caseType
382 public static function isValidName($caseType) {
383 return preg_match('/^[a-zA-Z0-9_]+$/', $caseType);
387 * Determine if the case-type has *both* DB and file-based definitions.
389 * @param int $caseTypeId
391 * TRUE if there are *both* DB and file-based definitions
393 public static function isForked($caseTypeId) {
394 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
396 $dbDefinition = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'definition', 'id', TRUE);
397 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
398 return $fileDefinition && $dbDefinition;
404 * Determine if modifications are allowed on the case-type
406 * @param int $caseTypeId
408 * TRUE if the definition can be modified
410 public static function isForkable($caseTypeId) {
411 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
413 // if file-based definition explicitly disables "forkable" option, then don't allow changes to definition
414 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
415 if ($fileDefinition && isset($fileDefinition->forkable
)) {
416 return CRM_Utils_String
::strtobool((string) $fileDefinition->forkable
);