Update release-notes/5.52.1.md
[civicrm-core.git] / CRM / Case / Info.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
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
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 */
20 class CRM_Case_Info extends CRM_Core_Component_Info {
21
22
23 /**
24 * @var string
25 * @inheritDoc
26 */
27 protected $keyword = 'case';
28
29 /**
30 * @inheritDoc
31 * @return array
32 */
33 public function getInfo() {
34 return [
35 'name' => 'CiviCase',
36 'translatedName' => ts('CiviCase'),
37 'title' => ts('CiviCase Engine'),
38 'search' => 1,
39 'showActivitiesInCore' => Civi::settings()->get('civicaseShowCaseActivities') ?? 0,
40 ];
41 }
42
43 /**
44 * @inheritDoc
45 */
46 public function getAngularModules() {
47 global $civicrm_root;
48
49 $result = [];
50 $result['crmCaseType'] = include "$civicrm_root/ang/crmCaseType.ang.php";
51 return $result;
52 }
53
54 /**
55 * @inheritDoc
56 * @return array
57 * @throws CRM_Core_Exception
58 */
59 public function getManagedEntities() {
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 );
65 return $entities;
66 }
67
68 /**
69 * @inheritDoc
70 * @param bool $getAllUnconditionally
71 * @param bool $descriptions
72 * Whether to return permission descriptions
73 *
74 * @return array
75 */
76 public function getPermissions($getAllUnconditionally = FALSE, $descriptions = FALSE) {
77 $permissions = [
78 'delete in CiviCase' => [
79 ts('delete in CiviCase'),
80 ts('Delete cases'),
81 ],
82 'administer CiviCase' => [
83 ts('administer CiviCase'),
84 ts('Define case types, access deleted cases'),
85 ],
86 'access my cases and activities' => [
87 ts('access my cases and activities'),
88 ts('View and edit only those cases managed by this user'),
89 ],
90 'access all cases and activities' => [
91 ts('access all cases and activities'),
92 ts('View and edit all cases (for visible contacts)'),
93 ],
94 'add cases' => [
95 ts('add cases'),
96 ts('Open a new case'),
97 ],
98 ];
99
100 if (!$descriptions) {
101 foreach ($permissions as $name => $attr) {
102 $permissions[$name] = array_shift($attr);
103 }
104 }
105
106 return $permissions;
107 }
108
109 /**
110 * @inheritDoc
111 */
112 public function getReferenceCounts($dao) {
113 $result = [];
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) {
121 $result[] = [
122 'name' => 'casetypexml:activities',
123 'type' => 'casetypexml',
124 'count' => $count,
125 ];
126 }
127 }
128 }
129 elseif ($dao instanceof CRM_Contact_DAO_RelationshipType) {
130 /** @var $dao CRM_Contact_DAO_RelationshipType */
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 }
139 if ($count > 0) {
140 $result[] = [
141 'name' => 'casetypexml:relationships',
142 'type' => 'casetypexml',
143 'count' => $count,
144 ];
145 }
146 }
147 return $result;
148 }
149
150 /**
151 * @inheritDoc
152 * @return array
153 */
154 public function getUserDashboardElement() {
155 return [];
156 }
157
158 /**
159 * @inheritDoc
160 * @return array
161 */
162 public function registerTab() {
163 return [
164 'title' => ts('Cases'),
165 'url' => 'case',
166 'weight' => 50,
167 ];
168 }
169
170 /**
171 * @inheritDoc
172 * @return string
173 */
174 public function getIcon() {
175 return 'crm-i fa-folder-open-o';
176 }
177
178 /**
179 * @inheritDoc
180 * @return array
181 */
182 public function registerAdvancedSearchPane() {
183 return [
184 'title' => ts('Cases'),
185 'weight' => 50,
186 ];
187 }
188
189 /**
190 * @inheritDoc
191 * @return null
192 */
193 public function getActivityTypes() {
194 return NULL;
195 }
196
197 /**
198 * add shortcut to Create New.
199 * @param $shortCuts
200 */
201 public function creatNewShortcut(&$shortCuts) {
202 if (CRM_Core_Permission::check('access all cases and activities') ||
203 CRM_Core_Permission::check('add cases')
204 ) {
205 $activityType = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
206 if ($activityType) {
207 $shortCuts = array_merge($shortCuts, [
208 [
209 'path' => 'civicrm/case/add',
210 'query' => "reset=1&action=add&atype={$activityType}&context=standalone",
211 'ref' => 'new-case',
212 'title' => ts('Case'),
213 ],
214 ]);
215 }
216 }
217 }
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 *
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).
231 *
232 * @throws \CRM_Core_Exception.
233 */
234 public static function onToggleComponents($oldValue, $newValue, $metadata) {
235 if (
236 in_array('CiviCase', $newValue)
237 &&
238 (!$oldValue || !in_array('CiviCase', $oldValue))
239 ) {
240 $pathToCaseSampleTpl = __DIR__ . '/xml/configuration.sample/';
241 self::loadCaseSampleData($pathToCaseSampleTpl . 'case_sample.mysql.tpl');
242 }
243 }
244
245 /**
246 * Load case sample data.
247 *
248 * @param string $fileName
249 * @param bool $lineMode
250 */
251 public static function loadCaseSampleData($fileName, $lineMode = FALSE) {
252 $dao = new CRM_Core_DAO();
253 $db = $dao->getDatabaseConnection();
254
255 $locales = CRM_Core_I18n::getMultilingual();
256 $smarty = CRM_Core_Smarty::singleton();
257 $smarty->assign('multilingual', (bool) $locales);
258 $smarty->assign('locales', $locales);
259
260 if (!$lineMode) {
261
262 $string = $smarty->fetch($fileName);
263 // change \r\n to fix windows issues
264 $string = str_replace("\r\n", "\n", $string);
265
266 //get rid of comments starting with # and --
267
268 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
269 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
270
271 $queries = preg_split('/;$/m', $string);
272 foreach ($queries as $query) {
273 $query = trim($query);
274 if (!empty($query)) {
275 try {
276 $res = &$db->query($query);
277 }
278 catch (Exception $e) {
279 die("Cannot execute $query: " . $e->getMessage());
280 }
281 }
282 }
283 }
284 else {
285 $fd = fopen($fileName, "r");
286 while ($string = fgets($fd)) {
287 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
288 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
289
290 $string = trim($string);
291 if (!empty($string)) {
292 try {
293 $res = &$db->query($string);
294 }
295 catch (Exception $e) {
296 die("Cannot execute $string: " . $e->getMessage());
297 }
298 }
299 }
300 }
301 }
302
303 /**
304 * @return array
305 * Array(string $value => string $label).
306 */
307 public static function getRedactOptions() {
308 return [
309 'default' => ts('Default'),
310 '0' => ts('Do not redact emails'),
311 '1' => ts('Redact emails'),
312 ];
313 }
314
315 /**
316 * @return array
317 * Array(string $value => string $label).
318 */
319 public static function getMultiClientOptions() {
320 return [
321 'default' => ts('Default'),
322 '0' => ts('Single client per case'),
323 '1' => ts('Multiple client per case'),
324 ];
325 }
326
327 /**
328 * @return array
329 * Array(string $value => string $label).
330 */
331 public static function getSortOptions() {
332 return [
333 'default' => ts('Default'),
334 '0' => ts('Definition order'),
335 '1' => ts('Alphabetical order'),
336 ];
337 }
338
339 }