Merge pull request #16005 from magnolia61/Contribution_Invoice_Privacy
[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 CRM_Core_Exception
36 */
37 public function onPreInstall(CRM_Extension_Info $info) {
38 $customReports = $this->getCustomReportsByName();
39 if (array_key_exists($info->key, $customReports)) {
40 throw new CRM_Core_Exception(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 throw new CRM_Core_Exception(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 $customReports = $this->getCustomReportsByName();
78 $cr = $this->getCustomReportsById();
79 $id = $cr[$customReports[$info->key]];
80 $optionValue = CRM_Core_BAO_OptionValue::del($id);
81
82 return $optionValue ? TRUE : FALSE;
83 }
84
85 /**
86 * @param CRM_Extension_Info $info
87 */
88 public function onPreDisable(CRM_Extension_Info $info) {
89 $customReports = $this->getCustomReportsByName();
90 $cr = $this->getCustomReportsById();
91 $id = $cr[$customReports[$info->key]];
92 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
93 }
94
95 /**
96 * @param CRM_Extension_Info $info
97 */
98 public function onPreEnable(CRM_Extension_Info $info) {
99 $customReports = $this->getCustomReportsByName();
100 $cr = $this->getCustomReportsById();
101 $id = $cr[$customReports[$info->key]];
102 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
103 }
104
105 /**
106 * @return array
107 */
108 public function getCustomReportsByName() {
109 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
110 }
111
112 /**
113 * @return array
114 */
115 public function getCustomReportsById() {
116 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
117 }
118
119 }