Merge pull request #17641 from MegaphoneJon/core-1590
[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);
a7718acb
TO
27 }
28
29 public function getGroupId() {
30 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
6a488035
TO
31 self::REPORT_GROUP_NAME, 'id', 'name'
32 );
33 }
34
e0ef6999
EM
35 /**
36 * @param CRM_Extension_Info $info
37 *
df9f2d1a 38 * @throws CRM_Core_Exception
e0ef6999 39 */
6a488035
TO
40 public function onPreInstall(CRM_Extension_Info $info) {
41 $customReports = $this->getCustomReportsByName();
42 if (array_key_exists($info->key, $customReports)) {
df9f2d1a 43 throw new CRM_Core_Exception(ts('This report is already registered.'));
6a488035
TO
44 }
45
46 if ($info->typeInfo['component'] === 'Contact') {
47 $compId = 'null';
48 }
49 else {
50 $comp = CRM_Core_Component::get($info->typeInfo['component']);
51 $compId = $comp->componentID;
52 }
53 if (empty($compId)) {
df9f2d1a 54 throw new CRM_Core_Exception(ts('Component for which you are trying to install the extension (%1) is currently disabled.', [1 => $info->typeInfo['component']]));
6a488035
TO
55 }
56 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
a7718acb 57 ['option_group_id' => $this->getGroupId()]
6a488035 58 );
be2fb01f 59 $params = [
6a488035
TO
60 'label' => $info->label . ' (' . $info->key . ')',
61 'value' => $info->typeInfo['reportUrl'],
62 'name' => $info->key,
63 'weight' => $weight,
64 'description' => $info->label . ' (' . $info->key . ')',
65 'component_id' => $compId,
a7718acb 66 'option_group_id' => $this->getGroupId(),
6a488035 67 'is_active' => 1,
be2fb01f 68 ];
6a488035 69
d8efe404 70 $optionValue = CRM_Core_BAO_OptionValue::add($params);
6a488035
TO
71 }
72
e0ef6999
EM
73 /**
74 * @param CRM_Extension_Info $info
75 *
76 * @return bool
77 */
6a488035 78 public function onPreUninstall(CRM_Extension_Info $info) {
6a488035 79 $customReports = $this->getCustomReportsByName();
353ffa53
TO
80 $cr = $this->getCustomReportsById();
81 $id = $cr[$customReports[$info->key]];
6a488035
TO
82 $optionValue = CRM_Core_BAO_OptionValue::del($id);
83
84 return $optionValue ? TRUE : FALSE;
85 }
86
e0ef6999
EM
87 /**
88 * @param CRM_Extension_Info $info
89 */
6a488035
TO
90 public function onPreDisable(CRM_Extension_Info $info) {
91 $customReports = $this->getCustomReportsByName();
353ffa53
TO
92 $cr = $this->getCustomReportsById();
93 $id = $cr[$customReports[$info->key]];
6a488035
TO
94 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
95 }
96
e0ef6999
EM
97 /**
98 * @param CRM_Extension_Info $info
99 */
6a488035
TO
100 public function onPreEnable(CRM_Extension_Info $info) {
101 $customReports = $this->getCustomReportsByName();
353ffa53
TO
102 $cr = $this->getCustomReportsById();
103 $id = $cr[$customReports[$info->key]];
6a488035
TO
104 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
105 }
106
e0ef6999
EM
107 /**
108 * @return array
109 */
6a488035
TO
110 public function getCustomReportsByName() {
111 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
112 }
113
e0ef6999
EM
114 /**
115 * @return array
116 */
6a488035
TO
117 public function getCustomReportsById() {
118 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
119 }
96025800 120
6a488035 121}