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