PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Report/BAO/ReportInstance.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 406 lines | 237 code | 40 blank | 129 comment | 50 complexity | aae9caf4c7ad3a893446823e13f29af5 MD5 | raw file
  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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
  33. /**
  34. * Takes an associative array and creates an instance object.
  35. *
  36. * the function extract all the params it needs to initialize the create a
  37. * instance object. the params array could contain additional unused name/value
  38. * pairs
  39. *
  40. * @param array $params
  41. * (reference ) an assoc array of name/value pairs.
  42. *
  43. * @return CRM_Report_DAO_ReportInstance
  44. */
  45. public static function add(&$params) {
  46. if (empty($params)) {
  47. return NULL;
  48. }
  49. $instanceID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('instance_id', $params));
  50. // convert roles array to string
  51. if (isset($params['grouprole']) && is_array($params['grouprole'])) {
  52. $grouprole_array = [];
  53. foreach ($params['grouprole'] as $key => $value) {
  54. $grouprole_array[$value] = $value;
  55. }
  56. $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
  57. array_keys($grouprole_array)
  58. );
  59. }
  60. if (!$instanceID || !isset($params['id'])) {
  61. $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
  62. $params['domain_id'] = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
  63. // CRM-17256 set created_id on report creation.
  64. $params['created_id'] = isset($params['created_id']) ? $params['created_id'] : CRM_Core_Session::getLoggedInContactID();
  65. }
  66. if ($instanceID) {
  67. CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params);
  68. }
  69. else {
  70. CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params);
  71. }
  72. $instance = new CRM_Report_DAO_ReportInstance();
  73. $instance->copyValues($params, TRUE);
  74. if (CRM_Core_Config::singleton()->userFramework == 'Joomla') {
  75. $instance->permission = 'null';
  76. }
  77. // explicitly set to null if params value is empty
  78. if (!$instanceID && empty($params['grouprole'])) {
  79. $instance->grouprole = 'null';
  80. }
  81. if ($instanceID) {
  82. $instance->id = $instanceID;
  83. }
  84. if (!$instanceID) {
  85. if ($reportID = CRM_Utils_Array::value('report_id', $params)) {
  86. $instance->report_id = $reportID;
  87. }
  88. elseif ($instanceID) {
  89. $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID);
  90. }
  91. else {
  92. // just take it from current url
  93. $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl();
  94. }
  95. }
  96. $instance->save();
  97. if ($instanceID) {
  98. CRM_Utils_Hook::post('edit', 'ReportInstance', $instance->id, $instance);
  99. }
  100. else {
  101. CRM_Utils_Hook::post('create', 'ReportInstance', $instance->id, $instance);
  102. }
  103. return $instance;
  104. }
  105. /**
  106. * Create instance.
  107. *
  108. * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
  109. *
  110. * This function is invoked from within the web form layer and also from the api layer
  111. *
  112. * @param array $params
  113. * (reference ) an assoc array of name/value pairs.
  114. *
  115. * @return CRM_Report_BAO_ReportInstance
  116. */
  117. public static function &create(&$params) {
  118. if (isset($params['report_header'])) {
  119. $params['header'] = CRM_Utils_Array::value('report_header', $params);
  120. }
  121. if (isset($params['report_footer'])) {
  122. $params['footer'] = CRM_Utils_Array::value('report_footer', $params);
  123. }
  124. // build navigation parameters
  125. if (!empty($params['is_navigation'])) {
  126. if (!array_key_exists('navigation', $params)) {
  127. $params['navigation'] = [];
  128. }
  129. $navigationParams =& $params['navigation'];
  130. $navigationParams['permission'] = [];
  131. $navigationParams['label'] = $params['title'];
  132. $navigationParams['name'] = $params['title'];
  133. $navigationParams['current_parent_id'] = CRM_Utils_Array::value('parent_id', $navigationParams);
  134. $navigationParams['parent_id'] = CRM_Utils_Array::value('parent_id', $params);
  135. $navigationParams['is_active'] = 1;
  136. if ($permission = CRM_Utils_Array::value('permission', $params)) {
  137. $navigationParams['permission'][] = $permission;
  138. }
  139. // unset the navigation related elements, not used in report form values
  140. unset($params['parent_id']);
  141. unset($params['is_navigation']);
  142. }
  143. $viewMode = !empty($params['view_mode']) ? $params['view_mode'] : FALSE;
  144. if ($viewMode) {
  145. // Do not save to the DB - it's saved in the url.
  146. unset($params['view_mode']);
  147. }
  148. // add to dashboard
  149. $dashletParams = [];
  150. if (!empty($params['addToDashboard'])) {
  151. $dashletParams = [
  152. 'label' => $params['title'],
  153. 'is_active' => 1,
  154. ];
  155. if ($permission = CRM_Utils_Array::value('permission', $params)) {
  156. $dashletParams['permission'][] = $permission;
  157. }
  158. }
  159. $transaction = new CRM_Core_Transaction();
  160. $instance = self::add($params);
  161. if (is_a($instance, 'CRM_Core_Error')) {
  162. $transaction->rollback();
  163. return $instance;
  164. }
  165. // add / update navigation as required
  166. if (!empty($navigationParams)) {
  167. if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
  168. unset($navigationParams['id']);
  169. }
  170. $navigationParams['url'] = "civicrm/report/instance/{$instance->id}" . ($viewMode == 'view' ? '?reset=1&force=1' : '?reset=1&output=criteria');
  171. $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
  172. if (!empty($navigationParams['is_active'])) {
  173. //set the navigation id in report instance table
  174. CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', $navigation->id);
  175. }
  176. else {
  177. // has been removed from the navigation bar
  178. CRM_Core_DAO::setFieldValue('CRM_Report_DAO_ReportInstance', $instance->id, 'navigation_id', 'NULL');
  179. }
  180. //reset navigation
  181. CRM_Core_BAO_Navigation::resetNavigation();
  182. }
  183. // add to dashlet
  184. if (!empty($dashletParams)) {
  185. $section = 2;
  186. $chart = $limitResult = '';
  187. if (!empty($params['charts'])) {
  188. $section = 1;
  189. $chart = "&charts=" . $params['charts'];
  190. }
  191. if (!empty($params['row_count']) && CRM_Utils_Rule::positiveInteger($params['row_count'])) {
  192. $limitResult = '&rowCount=' . $params['row_count'];
  193. }
  194. if (!empty($params['cache_minutes']) && CRM_Utils_Rule::positiveInteger($params['cache_minutes'])) {
  195. $dashletParams['cache_minutes'] = $params['cache_minutes'];
  196. }
  197. $dashletParams['name'] = "report/{$instance->id}";
  198. $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}{$chart}&context=dashlet" . $limitResult;
  199. $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1&section={$section}{$chart}&context=dashletFullscreen" . $limitResult;
  200. $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}";
  201. CRM_Core_BAO_Dashboard::addDashlet($dashletParams);
  202. }
  203. $transaction->commit();
  204. return $instance;
  205. }
  206. /**
  207. * Delete the instance of the Report.
  208. *
  209. * @param int $id
  210. *
  211. * @return mixed
  212. * $results no of deleted Instance on success, false otherwise
  213. */
  214. public static function del($id = NULL) {
  215. $navId = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $id, 'navigation_id', 'id');
  216. $dao = new CRM_Report_DAO_ReportInstance();
  217. $dao->id = $id;
  218. $result = $dao->delete();
  219. // Delete navigation if exists.
  220. if ($navId) {
  221. CRM_Core_BAO_Navigation::processDelete($navId);
  222. CRM_Core_BAO_Navigation::resetNavigation();
  223. }
  224. return $result;
  225. }
  226. /**
  227. * Retrieve instance.
  228. *
  229. * @param array $params
  230. * @param array $defaults
  231. *
  232. * @return CRM_Report_DAO_ReportInstance|null
  233. */
  234. public static function retrieve($params, &$defaults) {
  235. $instance = new CRM_Report_DAO_ReportInstance();
  236. $instance->copyValues($params);
  237. if ($instance->find(TRUE)) {
  238. CRM_Core_DAO::storeValues($instance, $defaults);
  239. return $instance;
  240. }
  241. return NULL;
  242. }
  243. /**
  244. * Check if report is private.
  245. *
  246. * @param int $instance_id
  247. *
  248. * @return bool
  249. */
  250. public static function reportIsPrivate($instance_id) {
  251. $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
  252. if ($owner_id) {
  253. return TRUE;
  254. }
  255. return FALSE;
  256. }
  257. /**
  258. * Check if the logged in user is the owner.
  259. *
  260. * @param int $instance_id
  261. *
  262. * @return TRUE if contact owns the report, FALSE if not
  263. */
  264. public static function contactIsOwner($instance_id) {
  265. $session = CRM_Core_Session::singleton();
  266. $contact_id = $session->get('userID');
  267. $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
  268. if ($contact_id === $owner_id) {
  269. return TRUE;
  270. }
  271. return FALSE;
  272. }
  273. /**
  274. * Check if the logged in contact can administer the report.
  275. *
  276. * @param int $instance_id
  277. *
  278. * @return bool
  279. * True if contact can edit the private report, FALSE if not.
  280. */
  281. public static function contactCanAdministerReport($instance_id) {
  282. if (self::reportIsPrivate($instance_id)) {
  283. if (self::contactIsOwner($instance_id) || CRM_Core_Permission::check('access all private reports')) {
  284. return TRUE;
  285. }
  286. }
  287. elseif (CRM_Core_Permission::check('administer Reports')) {
  288. return TRUE;
  289. }
  290. return FALSE;
  291. }
  292. /**
  293. * Delete a report instance wrapped in handling for the form layer.
  294. *
  295. * @param int $instanceId
  296. * @param string $bounceTo
  297. * Url to redirect the browser to on fail.
  298. * @param string $successRedirect
  299. */
  300. public static function doFormDelete($instanceId, $bounceTo = 'civicrm/report/list?reset=1', $successRedirect = NULL) {
  301. if (!CRM_Core_Permission::check('administer Reports')) {
  302. $statusMessage = ts('You do not have permission to Delete Report.');
  303. CRM_Core_Error::statusBounce($statusMessage, $bounceTo);
  304. }
  305. CRM_Report_BAO_ReportInstance::del($instanceId);
  306. CRM_Core_Session::setStatus(ts('Selected report has been deleted.'), ts('Deleted'), 'success');
  307. if ($successRedirect) {
  308. CRM_Utils_System::redirect(CRM_Utils_System::url($successRedirect));
  309. }
  310. }
  311. /**
  312. * Get the metadata of actions available for this entity.
  313. *
  314. * The thinking here is to describe the various actions on the BAO and then functions
  315. * can add a mix of actions from different BAO as appropriate. The crm.SearchForm.js code
  316. * transforms the 'confirm_mesage' into a message that needs to be confirmed.
  317. * confirm_refresh_fields need to be reviewed & potentially updated at the confirm stage.
  318. *
  319. * Ideas not yet implemented:
  320. * - supports_modal task can be loaded in a popup, theoretically worked, not attempted.
  321. * - class and or icon - per option icons or classes (I added these in addTaskMenu::addTaskMenu
  322. * but I didn't have the right classes). ie adding 'class' => 'crm-i fa-print' to print / class looked
  323. * wrong, but at the php level it worked https://github.com/civicrm/civicrm-core/pull/8529#issuecomment-227639091
  324. * - general script-add.
  325. */
  326. public static function getActionMetadata() {
  327. $actions = [];
  328. if (CRM_Core_Permission::check('save Report Criteria')) {
  329. $actions['report_instance.save'] = ['title' => ts('Save')];
  330. $actions['report_instance.copy'] = [
  331. 'title' => ts('Save a Copy'),
  332. 'data' => [
  333. 'is_confirm' => TRUE,
  334. 'confirm_title' => ts('Save a copy...'),
  335. 'confirm_refresh_fields' => json_encode([
  336. 'title' => [
  337. 'selector' => '.crm-report-instanceForm-form-block-title',
  338. 'prepend' => ts('(Copy) '),
  339. ],
  340. 'description' => [
  341. 'selector' => '.crm-report-instanceForm-form-block-description',
  342. 'prepend' => '',
  343. ],
  344. 'parent_id' => [
  345. 'selector' => '.crm-report-instanceForm-form-block-parent_id',
  346. 'prepend' => '',
  347. ],
  348. ]),
  349. ],
  350. ];
  351. }
  352. $actions['report_instance.print'] = ['title' => ts('Print Report')];
  353. $actions['report_instance.pdf'] = ['title' => ts('Print to PDF')];
  354. $actions['report_instance.csv'] = ['title' => ts('Export as CSV')];
  355. if (CRM_Core_Permission::check('administer Reports')) {
  356. $actions['report_instance.delete'] = [
  357. 'title' => ts('Delete report'),
  358. 'data' => [
  359. 'is_confirm' => TRUE,
  360. 'confirm_message' => ts('Are you sure you want delete this report? This action cannot be undone.'),
  361. ],
  362. ];
  363. }
  364. return $actions;
  365. }
  366. }