Merge pull request #16001 from agileware/CIVICRM-1383
[civicrm-core.git] / CRM / Case / Info.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * This class introduces component to the system and provides all the
14 * information about it. It needs to extend CRM_Core_Component_Info
15 * abstract class.
16 *
17 * @package CRM
ca5cec67 18 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
19 */
20class CRM_Case_Info extends CRM_Core_Component_Info {
21
22
e7c15cb6 23 /**
f157740d 24 * @var string
e7c15cb6
CW
25 * @inheritDoc
26 */
6a488035
TO
27 protected $keyword = 'case';
28
4c6ce474 29 /**
e7c15cb6 30 * @inheritDoc
4c6ce474
EM
31 * @return array
32 */
6a488035 33 public function getInfo() {
be2fb01f 34 return [
6a488035
TO
35 'name' => 'CiviCase',
36 'translatedName' => ts('CiviCase'),
37 'title' => ts('CiviCase Engine'),
38 'search' => 1,
39 'showActivitiesInCore' => 0,
be2fb01f 40 ];
6a488035
TO
41 }
42
4c58e251 43 /**
e7c15cb6 44 * @inheritDoc
4c58e251
TO
45 */
46 public function getAngularModules() {
8456e727 47 global $civicrm_root;
8c7e0ae8 48
be2fb01f 49 $result = [];
8456e727 50 $result['crmCaseType'] = include "$civicrm_root/ang/crmCaseType.ang.php";
4c58e251
TO
51 return $result;
52 }
53
4c6ce474 54 /**
e7c15cb6 55 * @inheritDoc
4c6ce474
EM
56 * @return array
57 * @throws CRM_Core_Exception
58 */
78650402 59 public function getManagedEntities() {
32e74b94
TO
60 $entities = array_merge(
61 CRM_Case_ManagedEntities::createManagedCaseTypes(),
62 CRM_Case_ManagedEntities::createManagedActivityTypes(CRM_Case_XMLRepository::singleton(), CRM_Core_ManagedEntities::singleton()),
63 CRM_Case_ManagedEntities::createManagedRelationshipTypes(CRM_Case_XMLRepository::singleton(), CRM_Core_ManagedEntities::singleton())
64 );
78650402
N
65 return $entities;
66 }
67
4c6ce474 68 /**
e7c15cb6 69 * @inheritDoc
4c6ce474 70 * @param bool $getAllUnconditionally
221b21b4
AH
71 * @param bool $descriptions
72 * Whether to return permission descriptions
4c6ce474
EM
73 *
74 * @return array
75 */
221b21b4 76 public function getPermissions($getAllUnconditionally = FALSE, $descriptions = FALSE) {
be2fb01f
CW
77 $permissions = [
78 'delete in CiviCase' => [
221b21b4 79 ts('delete in CiviCase'),
174a1918 80 ts('Delete cases'),
be2fb01f
CW
81 ],
82 'administer CiviCase' => [
221b21b4 83 ts('administer CiviCase'),
174a1918 84 ts('Define case types, access deleted cases'),
be2fb01f
CW
85 ],
86 'access my cases and activities' => [
221b21b4 87 ts('access my cases and activities'),
174a1918 88 ts('View and edit only those cases managed by this user'),
be2fb01f
CW
89 ],
90 'access all cases and activities' => [
221b21b4 91 ts('access all cases and activities'),
174a1918 92 ts('View and edit all cases (for visible contacts)'),
be2fb01f
CW
93 ],
94 'add cases' => [
221b21b4 95 ts('add cases'),
174a1918 96 ts('Open a new case'),
be2fb01f
CW
97 ],
98 ];
221b21b4
AH
99
100 if (!$descriptions) {
101 foreach ($permissions as $name => $attr) {
102 $permissions[$name] = array_shift($attr);
103 }
104 }
105
106 return $permissions;
6a488035
TO
107 }
108
168f458e 109 /**
e7c15cb6 110 * @inheritDoc
168f458e
TO
111 */
112 public function getReferenceCounts($dao) {
be2fb01f 113 $result = [];
168f458e
TO
114 if ($dao instanceof CRM_Core_DAO_OptionValue) {
115 /** @var $dao CRM_Core_DAO_OptionValue */
116 $activity_type_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'activity_type', 'id', 'name');
117 if ($activity_type_gid == $dao->option_group_id) {
118 $count = CRM_Case_XMLRepository::singleton()
119 ->getActivityReferenceCount($dao->name);
120 if ($count > 0) {
be2fb01f 121 $result[] = [
168f458e
TO
122 'name' => 'casetypexml:activities',
123 'type' => 'casetypexml',
124 'count' => $count,
be2fb01f 125 ];
168f458e
TO
126 }
127 }
128 }
129 elseif ($dao instanceof CRM_Contact_DAO_RelationshipType) {
130 /** @var $dao CRM_Contact_DAO_RelationshipType */
d0a94888
AF
131
132 // Need to look both directions, but no need to translate case role
133 // direction from XML perspective to client-based perspective
134 $xmlRepo = CRM_Case_XMLRepository::singleton();
135 $count = $xmlRepo->getRelationshipReferenceCount($dao->label_a_b);
136 if ($dao->label_a_b != $dao->label_b_a) {
137 $count += $xmlRepo->getRelationshipReferenceCount($dao->label_b_a);
138 }
168f458e 139 if ($count > 0) {
be2fb01f 140 $result[] = [
168f458e
TO
141 'name' => 'casetypexml:relationships',
142 'type' => 'casetypexml',
143 'count' => $count,
be2fb01f 144 ];
168f458e
TO
145 }
146 }
147 return $result;
148 }
149
4c6ce474 150 /**
e7c15cb6 151 * @inheritDoc
4c6ce474
EM
152 * @return array
153 */
6a488035 154 public function getUserDashboardElement() {
be2fb01f 155 return [];
6a488035
TO
156 }
157
4c6ce474 158 /**
e7c15cb6 159 * @inheritDoc
4c6ce474
EM
160 * @return array
161 */
6a488035 162 public function registerTab() {
be2fb01f 163 return [
353ffa53 164 'title' => ts('Cases'),
6a488035
TO
165 'url' => 'case',
166 'weight' => 50,
be2fb01f 167 ];
6a488035
TO
168 }
169
b04115b4
CW
170 /**
171 * @inheritDoc
172 * @return string
173 */
174 public function getIcon() {
175 return 'crm-i fa-folder-open-o';
176 }
177
4c6ce474 178 /**
e7c15cb6 179 * @inheritDoc
4c6ce474
EM
180 * @return array
181 */
6a488035 182 public function registerAdvancedSearchPane() {
be2fb01f 183 return [
353ffa53 184 'title' => ts('Cases'),
6a488035 185 'weight' => 50,
be2fb01f 186 ];
6a488035
TO
187 }
188
4c6ce474 189 /**
e7c15cb6 190 * @inheritDoc
4c6ce474
EM
191 * @return null
192 */
6a488035
TO
193 public function getActivityTypes() {
194 return NULL;
195 }
196
4c6ce474 197 /**
fe482240 198 * add shortcut to Create New.
4c6ce474
EM
199 * @param $shortCuts
200 */
6a488035
TO
201 public function creatNewShortcut(&$shortCuts) {
202 if (CRM_Core_Permission::check('access all cases and activities') ||
203 CRM_Core_Permission::check('add cases')
204 ) {
972a6042 205 $activityType = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
206 if ($activityType) {
be2fb01f
CW
207 $shortCuts = array_merge($shortCuts, [
208 [
353ffa53 209 'path' => 'civicrm/case/add',
972a6042 210 'query' => "reset=1&action=add&atype={$activityType}&context=standalone",
353ffa53
TO
211 'ref' => 'new-case',
212 'title' => ts('Case'),
be2fb01f
CW
213 ],
214 ]);
6a488035
TO
215 }
216 }
217 }
2bc3bd8f
TO
218
219 /**
220 * (Setting Callback)
221 * Respond to changes in the "enable_components" setting
222 *
223 * If CiviCase is being enabled, load the case related sample data
224 *
64bd5a0e
TO
225 * @param array $oldValue
226 * List of component names.
227 * @param array $newValue
228 * List of component names.
229 * @param array $metadata
230 * Specification of the setting (per *.settings.php).
2bc3bd8f
TO
231 */
232 public static function onToggleComponents($oldValue, $newValue, $metadata) {
e6d720bb
TO
233 if (
234 in_array('CiviCase', $newValue)
235 &&
236 (!$oldValue || !in_array('CiviCase', $oldValue))
2bc3bd8f 237 ) {
e547f744 238 $pathToCaseSampleTpl = __DIR__ . '/xml/configuration.sample/';
281db812 239 self::loadCaseSampleData($pathToCaseSampleTpl . 'case_sample.mysql.tpl');
2bc3bd8f
TO
240 if (!CRM_Case_BAO_Case::createCaseViews()) {
241 $msg = ts("Could not create the MySQL views for CiviCase. Your mysql user needs to have the 'CREATE VIEW' permission");
242 CRM_Core_Error::fatal($msg);
243 }
244 }
245 }
96025800 246
281db812 247 /**
248 * Load case sample data.
249 *
250 * @param string $fileName
251 * @param bool $lineMode
252 */
253 public static function loadCaseSampleData($fileName, $lineMode = FALSE) {
254 $dao = new CRM_Core_DAO();
255 $db = $dao->getDatabaseConnection();
256
257 $domain = new CRM_Core_DAO_Domain();
258 $domain->find(TRUE);
259 $multiLingual = (bool) $domain->locales;
260 $smarty = CRM_Core_Smarty::singleton();
261 $smarty->assign('multilingual', $multiLingual);
262 $smarty->assign('locales', explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales));
263
264 if (!$lineMode) {
265
266 $string = $smarty->fetch($fileName);
267 // change \r\n to fix windows issues
268 $string = str_replace("\r\n", "\n", $string);
269
270 //get rid of comments starting with # and --
271
272 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
273 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
274
275 $queries = preg_split('/;$/m', $string);
276 foreach ($queries as $query) {
277 $query = trim($query);
278 if (!empty($query)) {
279 $res = &$db->query($query);
280 if (PEAR::isError($res)) {
281 die("Cannot execute $query: " . $res->getMessage());
282 }
283 }
284 }
285 }
286 else {
287 $fd = fopen($fileName, "r");
288 while ($string = fgets($fd)) {
289 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
290 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
291
292 $string = trim($string);
293 if (!empty($string)) {
294 $res = &$db->query($string);
295 if (PEAR::isError($res)) {
296 die("Cannot execute $string: " . $res->getMessage());
297 }
298 }
299 }
300 }
301 }
302
b83de994
TO
303 /**
304 * @return array
305 * Array(string $value => string $label).
306 */
307 public static function getRedactOptions() {
be2fb01f 308 return [
ec61a2b2 309 'default' => ts('Default'),
b83de994
TO
310 '0' => ts('Do not redact emails'),
311 '1' => ts('Redact emails'),
be2fb01f 312 ];
b83de994
TO
313 }
314
315 /**
316 * @return array
317 * Array(string $value => string $label).
318 */
319 public static function getMultiClientOptions() {
be2fb01f 320 return [
ec61a2b2 321 'default' => ts('Default'),
b83de994
TO
322 '0' => ts('Single client per case'),
323 '1' => ts('Multiple client per case'),
be2fb01f 324 ];
b83de994
TO
325 }
326
327 /**
328 * @return array
329 * Array(string $value => string $label).
330 */
331 public static function getSortOptions() {
be2fb01f 332 return [
ec61a2b2 333 'default' => ts('Default'),
b83de994
TO
334 '0' => ts('Definition order'),
335 '1' => ts('Alphabetical order'),
be2fb01f 336 ];
b83de994
TO
337 }
338
6a488035 339}