Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / CRM / Extension / Manager / Report.php
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 stores logic for managing CiviCRM extensions.
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18 class CRM_Extension_Manager_Report extends CRM_Extension_Manager_Base {
19
20 const REPORT_GROUP_NAME = 'report_template';
21
22 /**
23 * CRM_Extension_Manager_Report constructor.
24 */
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
32 /**
33 * @param CRM_Extension_Info $info
34 *
35 * @throws Exception
36 */
37 public function onPreInstall(CRM_Extension_Info $info) {
38 $customReports = $this->getCustomReportsByName();
39 if (array_key_exists($info->key, $customReports)) {
40 CRM_Core_Error::fatal(ts('This report is already registered.'));
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)) {
51 CRM_Core_Error::fatal(ts('Component for which you are trying to install the extension (%1) is currently disabled.', [1 => $info->typeInfo['component']]));
52 }
53 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
54 ['option_group_id' => $this->groupId]
55 );
56 $ids = [];
57 $params = [
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,
66 ];
67
68 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
69 }
70
71 /**
72 * @param CRM_Extension_Info $info
73 *
74 * @return bool
75 */
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();
83 $cr = $this->getCustomReportsById();
84 $id = $cr[$customReports[$info->key]];
85 $optionValue = CRM_Core_BAO_OptionValue::del($id);
86
87 return $optionValue ? TRUE : FALSE;
88 }
89
90 /**
91 * @param CRM_Extension_Info $info
92 */
93 public function onPreDisable(CRM_Extension_Info $info) {
94 $customReports = $this->getCustomReportsByName();
95 $cr = $this->getCustomReportsById();
96 $id = $cr[$customReports[$info->key]];
97 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
98 }
99
100 /**
101 * @param CRM_Extension_Info $info
102 */
103 public function onPreEnable(CRM_Extension_Info $info) {
104 $customReports = $this->getCustomReportsByName();
105 $cr = $this->getCustomReportsById();
106 $id = $cr[$customReports[$info->key]];
107 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
108 }
109
110 /**
111 * @return array
112 */
113 public function getCustomReportsByName() {
114 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
115 }
116
117 /**
118 * @return array
119 */
120 public function getCustomReportsById() {
121 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
122 }
123
124 }