Merge pull request #15821 from seamuslee001/dev_core_183_custom_group
[civicrm-core.git] / CRM / Extension / Manager / Report.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * This class stores logic for managing CiviCRM extensions.
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
17 */
18class CRM_Extension_Manager_Report extends CRM_Extension_Manager_Base {
19
7da04cde 20 const REPORT_GROUP_NAME = 'report_template';
6a488035 21
8246bca4 22 /**
23 * CRM_Extension_Manager_Report constructor.
24 */
6a488035
TO
25 public function __construct() {
26 parent::__construct(TRUE);
27 $this->groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
28 self::REPORT_GROUP_NAME, 'id', 'name'
29 );
30 }
31
e0ef6999
EM
32 /**
33 * @param CRM_Extension_Info $info
34 *
35 * @throws Exception
36 */
6a488035
TO
37 public function onPreInstall(CRM_Extension_Info $info) {
38 $customReports = $this->getCustomReportsByName();
39 if (array_key_exists($info->key, $customReports)) {
2108fe32 40 CRM_Core_Error::fatal(ts('This report is already registered.'));
6a488035
TO
41 }
42
43 if ($info->typeInfo['component'] === 'Contact') {
44 $compId = 'null';
45 }
46 else {
47 $comp = CRM_Core_Component::get($info->typeInfo['component']);
48 $compId = $comp->componentID;
49 }
50 if (empty($compId)) {
2108fe32 51 CRM_Core_Error::fatal(ts('Component for which you are trying to install the extension (%1) is currently disabled.', [1 => $info->typeInfo['component']]));
6a488035
TO
52 }
53 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
be2fb01f 54 ['option_group_id' => $this->groupId]
6a488035 55 );
be2fb01f
CW
56 $ids = [];
57 $params = [
6a488035
TO
58 'label' => $info->label . ' (' . $info->key . ')',
59 'value' => $info->typeInfo['reportUrl'],
60 'name' => $info->key,
61 'weight' => $weight,
62 'description' => $info->label . ' (' . $info->key . ')',
63 'component_id' => $compId,
64 'option_group_id' => $this->groupId,
65 'is_active' => 1,
be2fb01f 66 ];
6a488035
TO
67
68 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
69 }
70
e0ef6999
EM
71 /**
72 * @param CRM_Extension_Info $info
73 *
74 * @return bool
75 */
6a488035
TO
76 public function onPreUninstall(CRM_Extension_Info $info) {
77
78 // if( !array_key_exists( $info->key, $this->customReports ) ) {
79 // CRM_Core_Error::fatal( 'This report is not registered.' );
80 // }
81
82 $customReports = $this->getCustomReportsByName();
353ffa53
TO
83 $cr = $this->getCustomReportsById();
84 $id = $cr[$customReports[$info->key]];
6a488035
TO
85 $optionValue = CRM_Core_BAO_OptionValue::del($id);
86
87 return $optionValue ? TRUE : FALSE;
88 }
89
e0ef6999
EM
90 /**
91 * @param CRM_Extension_Info $info
92 */
6a488035
TO
93 public function onPreDisable(CRM_Extension_Info $info) {
94 $customReports = $this->getCustomReportsByName();
353ffa53
TO
95 $cr = $this->getCustomReportsById();
96 $id = $cr[$customReports[$info->key]];
6a488035
TO
97 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
98 }
99
e0ef6999
EM
100 /**
101 * @param CRM_Extension_Info $info
102 */
6a488035
TO
103 public function onPreEnable(CRM_Extension_Info $info) {
104 $customReports = $this->getCustomReportsByName();
353ffa53
TO
105 $cr = $this->getCustomReportsById();
106 $id = $cr[$customReports[$info->key]];
6a488035
TO
107 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
108 }
109
e0ef6999
EM
110 /**
111 * @return array
112 */
6a488035
TO
113 public function getCustomReportsByName() {
114 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
115 }
116
e0ef6999
EM
117 /**
118 * @return array
119 */
6a488035
TO
120 public function getCustomReportsById() {
121 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
122 }
96025800 123
6a488035 124}