Merge pull request #3342 from systopia/CRM-14741
[civicrm-core.git] / CRM / Case / Info.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 * $Id$
36 *
37 */
38 class CRM_Case_Info extends CRM_Core_Component_Info {
39
40
41 // docs inherited from interface
42 protected $keyword = 'case';
43
44 // docs inherited from interface
45 /**
46 * @return array
47 */
48 public function getInfo() {
49 return array(
50 'name' => 'CiviCase',
51 'translatedName' => ts('CiviCase'),
52 'title' => ts('CiviCase Engine'),
53 'search' => 1,
54 'showActivitiesInCore' => 0,
55 );
56 }
57
58 /**
59 * {@inheritDoc}
60 */
61 public function getAngularModules() {
62 $result = array();
63 $result['crmCaseType'] = array(
64 'ext' => 'civicrm',
65 'js' => array('js/angular-crmCaseType.js'),
66 'css' => array('css/angular-crmCaseType.css'),
67 );
68 // Need full OptionValue records
69 $actStatuses = civicrm_api3('OptionValue', 'get', array('option_group_id' => 'activity_status'));
70 $actTypes = civicrm_api3('OptionValue', 'get', array('option_group_id' => 'activity_type'));
71 CRM_Core_Resources::singleton()->addSetting(array(
72 'crmCaseType' => array(
73 'actStatuses' => array_values($actStatuses['values']),
74 'actTypes' => array_values($actTypes['values']),
75 //CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'),
76 ),
77 ));
78 return $result;
79 }
80
81 // docs inherited from interface
82 /**
83 * @return array
84 * @throws CRM_Core_Exception
85 */
86 public function getManagedEntities() {
87 // Use hook_civicrm_caseTypes to build a list of OptionValues
88 // In the long run, we may want more specialized logic for this, but
89 // this design is fairly convenient and will allow us to replace it
90 // without changing the hook_civicrm_caseTypes interface.
91 $entities = array();
92
93 $caseTypes = array();
94 CRM_Utils_Hook::caseTypes($caseTypes);
95
96 $proc = new CRM_Case_XMLProcessor();
97 foreach ($caseTypes as $name => $caseType) {
98 $xml = $proc->retrieve($name);
99 if (!$xml) {
100 throw new CRM_Core_Exception("Failed to load XML for case type (" . $name . ")");
101 }
102
103 if (isset($caseType['module'], $caseType['name'], $caseType['file'])) {
104 $entities[] = array(
105 'module' => $caseType['module'],
106 'name' => $caseType['name'],
107 'entity' => 'CaseType',
108 'params' => array(
109 'version' => 3,
110 'name' => $caseType['name'],
111 'title' => (string) $xml->name,
112 'description' => (string) $xml->description,
113 'is_reserved' => 1,
114 'is_active' => 1,
115 'weight' => $xml->weight ? $xml->weight : 1,
116 ),
117 );
118 }
119 else {
120 throw new CRM_Core_Exception("Invalid case type");
121 }
122 }
123
124 return $entities;
125 }
126
127 // docs inherited from interface
128 /**
129 * @param bool $getAllUnconditionally
130 *
131 * @return array
132 */
133 public function getPermissions($getAllUnconditionally = FALSE) {
134 return array(
135 'delete in CiviCase',
136 'administer CiviCase',
137 'access my cases and activities',
138 'access all cases and activities',
139 'add cases',
140 );
141 }
142
143 /**
144 * {@inheritdoc}
145 */
146 public function getReferenceCounts($dao) {
147 $result = array();
148 if ($dao instanceof CRM_Core_DAO_OptionValue) {
149 /** @var $dao CRM_Core_DAO_OptionValue */
150 $activity_type_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'activity_type', 'id', 'name');
151 if ($activity_type_gid == $dao->option_group_id) {
152 $count = CRM_Case_XMLRepository::singleton()
153 ->getActivityReferenceCount($dao->name);
154 if ($count > 0) {
155 $result[] = array(
156 'name' => 'casetypexml:activities',
157 'type' => 'casetypexml',
158 'count' => $count,
159 );
160 }
161 }
162 }
163 elseif ($dao instanceof CRM_Contact_DAO_RelationshipType) {
164 /** @var $dao CRM_Contact_DAO_RelationshipType */
165 $count = CRM_Case_XMLRepository::singleton()
166 ->getRelationshipReferenceCount($dao->{CRM_Case_XMLProcessor::REL_TYPE_CNAME});
167 if ($count > 0) {
168 $result[] = array(
169 'name' => 'casetypexml:relationships',
170 'type' => 'casetypexml',
171 'count' => $count,
172 );
173 }
174 }
175 return $result;
176 }
177
178 // docs inherited from interface
179 /**
180 * @return array
181 */
182 public function getUserDashboardElement() {
183 return array();
184 }
185
186 // docs inherited from interface
187 /**
188 * @return array
189 */
190 public function registerTab() {
191 return array('title' => ts('Cases'),
192 'url' => 'case',
193 'weight' => 50,
194 );
195 }
196
197 // docs inherited from interface
198 /**
199 * @return array
200 */
201 public function registerAdvancedSearchPane() {
202 return array('title' => ts('Cases'),
203 'weight' => 50,
204 );
205 }
206
207 // docs inherited from interface
208 /**
209 * @return null
210 */
211 public function getActivityTypes() {
212 return NULL;
213 }
214
215 // add shortcut to Create New
216 /**
217 * @param $shortCuts
218 */
219 public function creatNewShortcut(&$shortCuts) {
220 if (CRM_Core_Permission::check('access all cases and activities') ||
221 CRM_Core_Permission::check('add cases')
222 ) {
223 $atype = CRM_Core_OptionGroup::getValue('activity_type',
224 'Open Case',
225 'name'
226 );
227 if ($atype) {
228 $shortCuts = array_merge($shortCuts, array(
229 array('path' => 'civicrm/case/add',
230 'query' => "reset=1&action=add&atype=$atype&context=standalone",
231 'ref' => 'new-case',
232 'title' => ts('Case'),
233 )));
234 }
235 }
236 }
237
238 /**
239 * (Setting Callback)
240 * Respond to changes in the "enable_components" setting
241 *
242 * If CiviCase is being enabled, load the case related sample data
243 *
244 * @param array $oldValue List of component names
245 * @param array $newValue List of component names
246 * @param array $metadata Specification of the setting (per *.settings.php)
247 */
248 public static function onToggleComponents($oldValue, $newValue, $metadata) {
249 if (
250 in_array('CiviCase', $newValue)
251 &&
252 (!$oldValue || !in_array('CiviCase', $oldValue))
253 ) {
254 $config = CRM_Core_Config::singleton();
255 CRM_Admin_Form_Setting_Component::loadCaseSampleData($config->dsn, $config->sqlDir . 'case_sample.mysql');
256 if (!CRM_Case_BAO_Case::createCaseViews()) {
257 $msg = ts("Could not create the MySQL views for CiviCase. Your mysql user needs to have the 'CREATE VIEW' permission");
258 CRM_Core_Error::fatal($msg);
259 }
260 }
261 }
262 }
263