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 return $caseTypeDAO->save();
87 * Generate and assign an arbitrary value to a field of a test object.
89 * @param string $fieldName
90 * @param array $fieldDef
92 * The globally-unique ID of the test object.
94 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
95 if ($fieldName == 'definition') {
96 $this->{$fieldName} = "<CaseType><name>TestCaseType{$counter}</name></CaseType>";
99 parent
::assignTestValue($fieldName, $fieldDef, $counter);
105 * Format / convert submitted array to xml for case type definition
107 * @param string $name
108 * @param array $definition
109 * The case-type definition expressed as an array-tree.
113 public static function convertDefinitionToXML($name, $definition) {
114 $xmlFile = '<?xml version="1.0" encoding="utf-8" ?>' . "\n\n<CaseType>\n";
115 $xmlFile .= "<name>" . self
::encodeXmlString($name) . "</name>\n";
117 if (array_key_exists('forkable', $definition)) {
118 $xmlFile .= "<forkable>" . ((int) $definition['forkable']) . "</forkable>\n";
121 if (isset($definition['activityTypes'])) {
122 $xmlFile .= "<ActivityTypes>\n";
123 foreach ($definition['activityTypes'] as $values) {
124 $xmlFile .= "<ActivityType>\n";
125 foreach ($values as $key => $value) {
126 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
128 $xmlFile .= "</ActivityType>\n";
130 $xmlFile .= "</ActivityTypes>\n";
133 if (!empty($definition['statuses'])) {
134 $xmlFile .= "<Statuses>\n";
135 foreach ($definition['statuses'] as $value) {
136 $xmlFile .= "<Status>$value</Status>\n";
138 $xmlFile .= "</Statuses>\n";
141 if (isset($definition['activitySets'])) {
142 $xmlFile .= "<ActivitySets>\n";
143 foreach ($definition['activitySets'] as $k => $val) {
144 $xmlFile .= "<ActivitySet>\n";
145 foreach ($val as $index => $setVal) {
147 case 'activityTypes':
148 if (!empty($setVal)) {
149 $xmlFile .= "<ActivityTypes>\n";
150 foreach ($setVal as $values) {
151 $xmlFile .= "<ActivityType>\n";
152 foreach ($values as $key => $value) {
153 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
155 $xmlFile .= "</ActivityType>\n";
157 $xmlFile .= "</ActivityTypes>\n";
161 case 'sequence': // passthrough
164 $xmlFile .= "<{$index}>true</{$index}>\n";
169 $xmlFile .= "<{$index}>" . self
::encodeXmlString($setVal) . "</{$index}>\n";
173 $xmlFile .= "</ActivitySet>\n";
176 $xmlFile .= "</ActivitySets>\n";
179 if (isset($definition['caseRoles'])) {
180 $xmlFile .= "<CaseRoles>\n";
181 foreach ($definition['caseRoles'] as $values) {
182 $xmlFile .= "<RelationshipType>\n";
183 foreach ($values as $key => $value) {
184 $xmlFile .= "<{$key}>" . self
::encodeXmlString($value) . "</{$key}>\n";
186 $xmlFile .= "</RelationshipType>\n";
188 $xmlFile .= "</CaseRoles>\n";
191 $xmlFile .= '</CaseType>';
196 * Ugh. This shouldn't exist. Use a real XML-encoder.
198 * Escape a string for use in XML.
201 * A string which should outputted to XML.
205 protected static function encodeXmlString($str) {
206 // PHP 5.4: return htmlspecialchars($str, ENT_XML1, 'UTF-8')
207 return htmlspecialchars($str);
211 * Get the case definition either from db or read from xml file.
213 * @param SimpleXmlElement $xml
214 * A single case-type record.
217 * the definition of the case-type, expressed as PHP array-tree
219 public static function convertXmlToDefinition($xml) {
220 // build PHP array based on definition
221 $definition = array();
223 if (isset($xml->forkable
)) {
224 $definition['forkable'] = (int) $xml->forkable
;
227 // set activity types
228 if (isset($xml->ActivityTypes
)) {
229 $definition['activityTypes'] = array();
230 foreach ($xml->ActivityTypes
->ActivityType
as $activityTypeXML) {
231 $definition['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
236 if (isset($xml->Statuses
)) {
237 $definition['statuses'] = (array) $xml->Statuses
->Status
;
241 if (isset($xml->ActivitySets
)) {
242 $definition['activitySets'] = array();
243 foreach ($xml->ActivitySets
->ActivitySet
as $activitySetXML) {
244 // parse basic properties
245 $activitySet = array();
246 $activitySet['name'] = (string) $activitySetXML->name
;
247 $activitySet['label'] = (string) $activitySetXML->label
;
248 if ('true' == (string) $activitySetXML->timeline
) {
249 $activitySet['timeline'] = 1;
251 if ('true' == (string) $activitySetXML->sequence
) {
252 $activitySet['sequence'] = 1;
255 if (isset($activitySetXML->ActivityTypes
)) {
256 $activitySet['activityTypes'] = array();
257 foreach ($activitySetXML->ActivityTypes
->ActivityType
as $activityTypeXML) {
258 $activitySet['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE);
261 $definition['activitySets'][] = $activitySet;
266 if (isset($xml->CaseRoles
)) {
267 $definition['caseRoles'] = array();
268 foreach ($xml->CaseRoles
->RelationshipType
as $caseRoleXml) {
269 $definition['caseRoles'][] = json_decode(json_encode($caseRoleXml), TRUE);
277 * Given the list of params in the params array, fetch the object
278 * and store the values in the values array
280 * @param array $params
281 * Input parameters to find object.
282 * @param array $values
283 * Output values of the object.
285 * @return CRM_Case_BAO_CaseType|null the found object or null
287 public static function &getValues(&$params, &$values) {
288 $caseType = new CRM_Case_BAO_CaseType();
290 $caseType->copyValues($params);
292 if ($caseType->find(TRUE)) {
293 CRM_Core_DAO
::storeValues($caseType, $values);
300 * Takes an associative array and creates a case type object.
302 * @param array $params
303 * (reference ) an assoc array of name/value pairs.
305 * @return CRM_Case_BAO_CaseType
307 public static function &create(&$params) {
308 $transaction = new CRM_Core_Transaction();
310 if (!empty($params['id'])) {
311 CRM_Utils_Hook
::pre('edit', 'CaseType', $params['id'], $params);
314 CRM_Utils_Hook
::pre('create', 'CaseType', NULL, $params);
317 $caseType = self
::add($params);
319 if (is_a($caseType, 'CRM_Core_Error')) {
320 $transaction->rollback();
324 if (!empty($params['id'])) {
325 CRM_Utils_Hook
::post('edit', 'CaseType', $caseType->id
, $case);
328 CRM_Utils_Hook
::post('create', 'CaseType', $caseType->id
, $case);
330 $transaction->commit();
331 CRM_Case_XMLRepository
::singleton(TRUE);
337 * Retrieve DB object based on input parameters.
339 * It also stores all the retrieved values in the default array.
341 * @param array $params
342 * (reference ) an assoc array of name/value pairs.
343 * @param array $defaults
344 * (reference ) an assoc array to hold the name / value pairs.
345 * in a hierarchical manner
347 * @return CRM_Case_BAO_CaseType
349 public static function retrieve(&$params, &$defaults) {
350 $caseType = CRM_Case_BAO_CaseType
::getValues($params, $defaults);
355 * @param int $caseTypeId
357 * @throws CRM_Core_Exception
360 public static function del($caseTypeId) {
361 $caseType = new CRM_Case_DAO_CaseType();
362 $caseType->id
= $caseTypeId;
363 $refCounts = $caseType->getReferenceCounts();
364 $total = array_sum(CRM_Utils_Array
::collect('count', $refCounts));
366 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)));
368 $result = $caseType->delete();
369 CRM_Case_XMLRepository
::singleton(TRUE);
374 * Determine if a case-type name is well-formed
376 * @param string $caseType
379 public static function isValidName($caseType) {
380 return preg_match('/^[a-zA-Z0-9_]+$/', $caseType);
384 * Determine if the case-type has *both* DB and file-based definitions.
386 * @param int $caseTypeId
388 * TRUE if there are *both* DB and file-based definitions
390 public static function isForked($caseTypeId) {
391 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
393 $dbDefinition = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'definition', 'id', TRUE);
394 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
395 return $fileDefinition && $dbDefinition;
401 * Determine if modifications are allowed on the case-type
403 * @param int $caseTypeId
405 * TRUE if the definition can be modified
407 public static function isForkable($caseTypeId) {
408 $caseTypeName = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE);
410 // if file-based definition explicitly disables "forkable" option, then don't allow changes to definition
411 $fileDefinition = CRM_Case_XMLRepository
::singleton()->retrieveFile($caseTypeName);
412 if ($fileDefinition && isset($fileDefinition->forkable
)) {
413 return CRM_Utils_String
::strtobool((string) $fileDefinition->forkable
);