Commit | Line | Data |
---|---|---|
8ffdec17 ARW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
8ffdec17 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
8ffdec17 ARW |
32 | */ |
33 | ||
34 | /** | |
67d19299 | 35 | * This class contains the functions for Case Type management. |
8ffdec17 ARW |
36 | */ |
37 | class 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); |
8ffdec17 ARW |
83 | return $caseTypeDAO->save(); |
84 | } | |
85 | ||
cd5823ae EM |
86 | /** |
87 | * Generate and assign an arbitrary value to a field of a test object. | |
88 | * | |
89 | * @param string $fieldName | |
90 | * @param array $fieldDef | |
91 | * @param int $counter | |
92 | * The globally-unique ID of the test object. | |
93 | */ | |
1753bd71 | 94 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { |
e547f744 | 95 | if ($fieldName == 'definition') { |
1753bd71 | 96 | $this->{$fieldName} = "<CaseType><name>TestCaseType{$counter}</name></CaseType>"; |
0db6c3e1 TO |
97 | } |
98 | else { | |
1753bd71 TO |
99 | parent::assignTestValue($fieldName, $fieldDef, $counter); |
100 | } | |
101 | } | |
102 | ||
103 | ||
20173e0e | 104 | /** |
100fef9d | 105 | * Format / convert submitted array to xml for case type definition |
20173e0e | 106 | * |
10b0bbee | 107 | * @param string $name |
64bd5a0e | 108 | * @param array $definition |
b44e3f84 | 109 | * The case-type definition expressed as an array-tree. |
a6c01b45 CW |
110 | * @return string |
111 | * XML | |
20173e0e | 112 | */ |
00be9182 | 113 | public static function convertDefinitionToXML($name, $definition) { |
c9c175f9 | 114 | $xmlFile = '<?xml version="1.0" encoding="utf-8" ?>' . "\n\n<CaseType>\n"; |
e0d34d59 | 115 | $xmlFile .= "<name>" . self::encodeXmlString($name) . "</name>\n"; |
20173e0e | 116 | |
466fba66 TO |
117 | if (array_key_exists('forkable', $definition)) { |
118 | $xmlFile .= "<forkable>" . ((int) $definition['forkable']) . "</forkable>\n"; | |
119 | } | |
120 | ||
728b5029 | 121 | if (isset($definition['activityTypes'])) { |
20173e0e | 122 | $xmlFile .= "<ActivityTypes>\n"; |
10b0bbee | 123 | foreach ($definition['activityTypes'] as $values) { |
20173e0e | 124 | $xmlFile .= "<ActivityType>\n"; |
125 | foreach ($values as $key => $value) { | |
e0d34d59 | 126 | $xmlFile .= "<{$key}>" . self::encodeXmlString($value) . "</{$key}>\n"; |
20173e0e | 127 | } |
128 | $xmlFile .= "</ActivityType>\n"; | |
129 | } | |
130 | $xmlFile .= "</ActivityTypes>\n"; | |
131 | } | |
132 | ||
728b5029 | 133 | if (isset($definition['activitySets'])) { |
20173e0e | 134 | $xmlFile .= "<ActivitySets>\n"; |
10b0bbee | 135 | foreach ($definition['activitySets'] as $k => $val) { |
834bc8e2 | 136 | $xmlFile .= "<ActivitySet>\n"; |
137 | foreach ($val as $index => $setVal) { | |
2f58fb09 TO |
138 | switch ($index) { |
139 | case 'activityTypes': | |
140 | if (!empty($setVal)) { | |
141 | $xmlFile .= "<ActivityTypes>\n"; | |
142 | foreach ($setVal as $values) { | |
143 | $xmlFile .= "<ActivityType>\n"; | |
144 | foreach ($values as $key => $value) { | |
e0d34d59 | 145 | $xmlFile .= "<{$key}>" . self::encodeXmlString($value) . "</{$key}>\n"; |
2f58fb09 TO |
146 | } |
147 | $xmlFile .= "</ActivityType>\n"; | |
834bc8e2 | 148 | } |
2f58fb09 | 149 | $xmlFile .= "</ActivityTypes>\n"; |
20173e0e | 150 | } |
2f58fb09 | 151 | break; |
e547f744 | 152 | |
2f58fb09 TO |
153 | case 'sequence': // passthrough |
154 | case 'timeline': | |
155 | if ($setVal) { | |
156 | $xmlFile .= "<{$index}>true</{$index}>\n"; | |
157 | } | |
158 | break; | |
e547f744 | 159 | |
2f58fb09 | 160 | default: |
e0d34d59 | 161 | $xmlFile .= "<{$index}>" . self::encodeXmlString($setVal) . "</{$index}>\n"; |
20173e0e | 162 | } |
163 | } | |
834bc8e2 | 164 | |
165 | $xmlFile .= "</ActivitySet>\n"; | |
20173e0e | 166 | } |
167 | ||
20173e0e | 168 | $xmlFile .= "</ActivitySets>\n"; |
169 | } | |
170 | ||
728b5029 | 171 | if (isset($definition['caseRoles'])) { |
20173e0e | 172 | $xmlFile .= "<CaseRoles>\n"; |
10b0bbee | 173 | foreach ($definition['caseRoles'] as $values) { |
20173e0e | 174 | $xmlFile .= "<RelationshipType>\n"; |
175 | foreach ($values as $key => $value) { | |
e0d34d59 | 176 | $xmlFile .= "<{$key}>" . self::encodeXmlString($value) . "</{$key}>\n"; |
20173e0e | 177 | } |
178 | $xmlFile .= "</RelationshipType>\n"; | |
179 | } | |
180 | $xmlFile .= "</CaseRoles>\n"; | |
181 | } | |
182 | ||
183 | $xmlFile .= '</CaseType>'; | |
10b0bbee | 184 | return $xmlFile; |
20173e0e | 185 | } |
186 | ||
e0d34d59 TO |
187 | /** |
188 | * Ugh. This shouldn't exist. Use a real XML-encoder. | |
189 | * | |
190 | * Escape a string for use in XML. | |
191 | * | |
192 | * @param string $str | |
193 | * A string which should outputted to XML. | |
194 | * @return string | |
195 | * @deprecated | |
196 | */ | |
197 | protected static function encodeXmlString($str) { | |
198 | // PHP 5.4: return htmlspecialchars($str, ENT_XML1, 'UTF-8') | |
199 | return htmlspecialchars($str); | |
200 | } | |
201 | ||
20173e0e | 202 | /** |
d2e5d2ce | 203 | * Get the case definition either from db or read from xml file. |
20173e0e | 204 | * |
64bd5a0e TO |
205 | * @param SimpleXmlElement $xml |
206 | * A single case-type record. | |
20173e0e | 207 | * |
a6c01b45 CW |
208 | * @return array |
209 | * the definition of the case-type, expressed as PHP array-tree | |
20173e0e | 210 | */ |
00be9182 | 211 | public static function convertXmlToDefinition($xml) { |
93d47462 TO |
212 | // build PHP array based on definition |
213 | $definition = array(); | |
20173e0e | 214 | |
466fba66 TO |
215 | if (isset($xml->forkable)) { |
216 | $definition['forkable'] = (int) $xml->forkable; | |
217 | } | |
218 | ||
20173e0e | 219 | // set activity types |
728b5029 TO |
220 | if (isset($xml->ActivityTypes)) { |
221 | $definition['activityTypes'] = array(); | |
222 | foreach ($xml->ActivityTypes->ActivityType as $activityTypeXML) { | |
223 | $definition['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE); | |
224 | } | |
ae195e71 | 225 | } |
226 | ||
728b5029 TO |
227 | // set activity sets |
228 | if (isset($xml->ActivitySets)) { | |
229 | $definition['activitySets'] = array(); | |
230 | foreach ($xml->ActivitySets->ActivitySet as $activitySetXML) { | |
231 | // parse basic properties | |
2f58fb09 TO |
232 | $activitySet = array(); |
233 | $activitySet['name'] = (string) $activitySetXML->name; | |
234 | $activitySet['label'] = (string) $activitySetXML->label; | |
235 | if ('true' == (string) $activitySetXML->timeline) { | |
236 | $activitySet['timeline'] = 1; | |
237 | } | |
238 | if ('true' == (string) $activitySetXML->sequence) { | |
239 | $activitySet['sequence'] = 1; | |
240 | } | |
728b5029 | 241 | |
728b5029 TO |
242 | if (isset($activitySetXML->ActivityTypes)) { |
243 | $activitySet['activityTypes'] = array(); | |
244 | foreach ($activitySetXML->ActivityTypes->ActivityType as $activityTypeXML) { | |
245 | $activitySet['activityTypes'][] = json_decode(json_encode($activityTypeXML), TRUE); | |
246 | } | |
ae195e71 | 247 | } |
728b5029 | 248 | $definition['activitySets'][] = $activitySet; |
ae195e71 | 249 | } |
b6e48667 | 250 | } |
20173e0e | 251 | |
252 | // set case roles | |
728b5029 TO |
253 | if (isset($xml->CaseRoles)) { |
254 | $definition['caseRoles'] = array(); | |
255 | foreach ($xml->CaseRoles->RelationshipType as $caseRoleXml) { | |
256 | $definition['caseRoles'][] = json_decode(json_encode($caseRoleXml), TRUE); | |
257 | } | |
258 | } | |
20173e0e | 259 | |
93d47462 | 260 | return $definition; |
20173e0e | 261 | } |
262 | ||
8ffdec17 ARW |
263 | /** |
264 | * Given the list of params in the params array, fetch the object | |
265 | * and store the values in the values array | |
266 | * | |
64bd5a0e TO |
267 | * @param array $params |
268 | * Input parameters to find object. | |
269 | * @param array $values | |
270 | * Output values of the object. | |
77b97be7 | 271 | * |
8ffdec17 | 272 | * @return CRM_Case_BAO_CaseType|null the found object or null |
8ffdec17 | 273 | */ |
00be9182 | 274 | public static function &getValues(&$params, &$values) { |
8ffdec17 ARW |
275 | $caseType = new CRM_Case_BAO_CaseType(); |
276 | ||
277 | $caseType->copyValues($params); | |
278 | ||
279 | if ($caseType->find(TRUE)) { | |
280 | CRM_Core_DAO::storeValues($caseType, $values); | |
281 | return $caseType; | |
282 | } | |
283 | return NULL; | |
284 | } | |
285 | ||
286 | /** | |
d2e5d2ce | 287 | * Takes an associative array and creates a case type object. |
8ffdec17 | 288 | * |
64bd5a0e TO |
289 | * @param array $params |
290 | * (reference ) an assoc array of name/value pairs. | |
77b97be7 | 291 | * |
16b10e64 | 292 | * @return CRM_Case_BAO_CaseType |
8ffdec17 | 293 | */ |
00be9182 | 294 | public static function &create(&$params) { |
8ffdec17 ARW |
295 | $transaction = new CRM_Core_Transaction(); |
296 | ||
297 | if (!empty($params['id'])) { | |
298 | CRM_Utils_Hook::pre('edit', 'CaseType', $params['id'], $params); | |
299 | } | |
300 | else { | |
301 | CRM_Utils_Hook::pre('create', 'CaseType', NULL, $params); | |
302 | } | |
303 | ||
304 | $caseType = self::add($params); | |
305 | ||
306 | if (is_a($caseType, 'CRM_Core_Error')) { | |
307 | $transaction->rollback(); | |
308 | return $caseType; | |
309 | } | |
310 | ||
311 | if (!empty($params['id'])) { | |
312 | CRM_Utils_Hook::post('edit', 'CaseType', $caseType->id, $case); | |
313 | } | |
314 | else { | |
315 | CRM_Utils_Hook::post('create', 'CaseType', $caseType->id, $case); | |
316 | } | |
317 | $transaction->commit(); | |
10ffff26 | 318 | CRM_Case_XMLRepository::singleton(TRUE); |
8ffdec17 ARW |
319 | |
320 | return $caseType; | |
321 | } | |
322 | ||
323 | /** | |
fe482240 EM |
324 | * Retrieve DB object based on input parameters. |
325 | * | |
326 | * It also stores all the retrieved values in the default array. | |
8ffdec17 | 327 | * |
64bd5a0e TO |
328 | * @param array $params |
329 | * (reference ) an assoc array of name/value pairs. | |
330 | * @param array $defaults | |
331 | * (reference ) an assoc array to hold the name / value pairs. | |
8ffdec17 | 332 | * in a hierarchical manner |
da6b46f4 | 333 | * |
16b10e64 | 334 | * @return CRM_Case_BAO_CaseType |
8ffdec17 | 335 | */ |
00be9182 | 336 | public static function retrieve(&$params, &$defaults) { |
8ffdec17 ARW |
337 | $caseType = CRM_Case_BAO_CaseType::getValues($params, $defaults); |
338 | return $caseType; | |
339 | } | |
340 | ||
4c6ce474 | 341 | /** |
100fef9d | 342 | * @param int $caseTypeId |
4c6ce474 | 343 | * |
c490a46a | 344 | * @throws CRM_Core_Exception |
4c6ce474 EM |
345 | * @return mixed |
346 | */ | |
00be9182 | 347 | public static function del($caseTypeId) { |
8ffdec17 ARW |
348 | $caseType = new CRM_Case_DAO_CaseType(); |
349 | $caseType->id = $caseTypeId; | |
10ffff26 TO |
350 | $refCounts = $caseType->getReferenceCounts(); |
351 | $total = array_sum(CRM_Utils_Array::collect('count', $refCounts)); | |
352 | if ($total) { | |
84dfe0f2 | 353 | 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 |
354 | } |
355 | $result = $caseType->delete(); | |
356 | CRM_Case_XMLRepository::singleton(TRUE); | |
357 | return $result; | |
8ffdec17 | 358 | } |
076f81b6 | 359 | |
360 | /** | |
361 | * Determine if a case-type name is well-formed | |
362 | * | |
363 | * @param string $caseType | |
364 | * @return bool | |
365 | */ | |
00be9182 | 366 | public static function isValidName($caseType) { |
e547f744 | 367 | return preg_match('/^[a-zA-Z0-9_]+$/', $caseType); |
076f81b6 | 368 | } |
32f1c917 TO |
369 | |
370 | /** | |
371 | * Determine if the case-type has *both* DB and file-based definitions. | |
372 | * | |
373 | * @param int $caseTypeId | |
72b3a70c CW |
374 | * @return bool|null |
375 | * TRUE if there are *both* DB and file-based definitions | |
32f1c917 | 376 | */ |
00be9182 | 377 | public static function isForked($caseTypeId) { |
32f1c917 TO |
378 | $caseTypeName = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE); |
379 | if ($caseTypeName) { | |
380 | $dbDefinition = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'definition', 'id', TRUE); | |
381 | $fileDefinition = CRM_Case_XMLRepository::singleton()->retrieveFile($caseTypeName); | |
382 | return $fileDefinition && $dbDefinition; | |
383 | } | |
384 | return NULL; | |
385 | } | |
386 | ||
387 | /** | |
388 | * Determine if modifications are allowed on the case-type | |
389 | * | |
390 | * @param int $caseTypeId | |
a6c01b45 CW |
391 | * @return bool |
392 | * TRUE if the definition can be modified | |
32f1c917 | 393 | */ |
00be9182 | 394 | public static function isForkable($caseTypeId) { |
32f1c917 TO |
395 | $caseTypeName = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name', 'id', TRUE); |
396 | if ($caseTypeName) { | |
397 | // if file-based definition explicitly disables "forkable" option, then don't allow changes to definition | |
398 | $fileDefinition = CRM_Case_XMLRepository::singleton()->retrieveFile($caseTypeName); | |
2210eab6 | 399 | if ($fileDefinition && isset($fileDefinition->forkable)) { |
e547f744 | 400 | return CRM_Utils_String::strtobool((string) $fileDefinition->forkable); |
32f1c917 TO |
401 | } |
402 | } | |
403 | return TRUE; | |
404 | } | |
96025800 | 405 | |
8ffdec17 | 406 | } |