| 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 | if (!CRM_Case_BAO_Case::createCaseViews()) { |
| 243 | throw new CRM_Core_Exception(ts("Could not create the MySQL views for CiviCase. Your mysql user needs to have the 'CREATE VIEW' permission")); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Load case sample data. |
| 250 | * |
| 251 | * @param string $fileName |
| 252 | * @param bool $lineMode |
| 253 | */ |
| 254 | public static function loadCaseSampleData($fileName, $lineMode = FALSE) { |
| 255 | $dao = new CRM_Core_DAO(); |
| 256 | $db = $dao->getDatabaseConnection(); |
| 257 | |
| 258 | $domain = new CRM_Core_DAO_Domain(); |
| 259 | $domain->find(TRUE); |
| 260 | $multiLingual = (bool) $domain->locales; |
| 261 | $smarty = CRM_Core_Smarty::singleton(); |
| 262 | $smarty->assign('multilingual', $multiLingual); |
| 263 | $smarty->assign('locales', explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales)); |
| 264 | |
| 265 | if (!$lineMode) { |
| 266 | |
| 267 | $string = $smarty->fetch($fileName); |
| 268 | // change \r\n to fix windows issues |
| 269 | $string = str_replace("\r\n", "\n", $string); |
| 270 | |
| 271 | //get rid of comments starting with # and -- |
| 272 | |
| 273 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); |
| 274 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); |
| 275 | |
| 276 | $queries = preg_split('/;$/m', $string); |
| 277 | foreach ($queries as $query) { |
| 278 | $query = trim($query); |
| 279 | if (!empty($query)) { |
| 280 | $res = &$db->query($query); |
| 281 | if (PEAR::isError($res)) { |
| 282 | die("Cannot execute $query: " . $res->getMessage()); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | else { |
| 288 | $fd = fopen($fileName, "r"); |
| 289 | while ($string = fgets($fd)) { |
| 290 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); |
| 291 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); |
| 292 | |
| 293 | $string = trim($string); |
| 294 | if (!empty($string)) { |
| 295 | $res = &$db->query($string); |
| 296 | if (PEAR::isError($res)) { |
| 297 | die("Cannot execute $string: " . $res->getMessage()); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @return array |
| 306 | * Array(string $value => string $label). |
| 307 | */ |
| 308 | public static function getRedactOptions() { |
| 309 | return [ |
| 310 | 'default' => ts('Default'), |
| 311 | '0' => ts('Do not redact emails'), |
| 312 | '1' => ts('Redact emails'), |
| 313 | ]; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * @return array |
| 318 | * Array(string $value => string $label). |
| 319 | */ |
| 320 | public static function getMultiClientOptions() { |
| 321 | return [ |
| 322 | 'default' => ts('Default'), |
| 323 | '0' => ts('Single client per case'), |
| 324 | '1' => ts('Multiple client per case'), |
| 325 | ]; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * @return array |
| 330 | * Array(string $value => string $label). |
| 331 | */ |
| 332 | public static function getSortOptions() { |
| 333 | return [ |
| 334 | 'default' => ts('Default'), |
| 335 | '0' => ts('Definition order'), |
| 336 | '1' => ts('Alphabetical order'), |
| 337 | ]; |
| 338 | } |
| 339 | |
| 340 | } |