Merge pull request #4593 from totten/master-civimail-preview
[civicrm-core.git] / CRM / Extension / Manager / Report.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * This class stores logic for managing CiviCRM extensions.
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36 class CRM_Extension_Manager_Report extends CRM_Extension_Manager_Base {
37
38 /**
39 *
40 */
41 CONST REPORT_GROUP_NAME = 'report_template';
42
43 /**
44 *
45 */
46 public function __construct() {
47 parent::__construct(TRUE);
48 $this->groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
49 self::REPORT_GROUP_NAME, 'id', 'name'
50 );
51 }
52
53 /**
54 * @param CRM_Extension_Info $info
55 *
56 * @throws Exception
57 */
58 public function onPreInstall(CRM_Extension_Info $info) {
59 $customReports = $this->getCustomReportsByName();
60 if (array_key_exists($info->key, $customReports)) {
61 CRM_Core_Error::fatal('This report is already registered.');
62 }
63
64 if ($info->typeInfo['component'] === 'Contact') {
65 $compId = 'null';
66 }
67 else {
68 $comp = CRM_Core_Component::get($info->typeInfo['component']);
69 $compId = $comp->componentID;
70 }
71 if (empty($compId)) {
72 CRM_Core_Error::fatal("Component for which you're trying to install the extension (" . $info->typeInfo['component'] . ") is currently disabled.");
73 }
74 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
75 array('option_group_id' => $this->groupId)
76 );
77 $ids = array();
78 $params = array(
79 'label' => $info->label . ' (' . $info->key . ')',
80 'value' => $info->typeInfo['reportUrl'],
81 'name' => $info->key,
82 'weight' => $weight,
83 'description' => $info->label . ' (' . $info->key . ')',
84 'component_id' => $compId,
85 'option_group_id' => $this->groupId,
86 'is_active' => 1,
87 );
88
89 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
90 }
91
92 /**
93 * @param CRM_Extension_Info $info
94 *
95 * @return bool
96 */
97 public function onPreUninstall(CRM_Extension_Info $info) {
98
99 // if( !array_key_exists( $info->key, $this->customReports ) ) {
100 // CRM_Core_Error::fatal( 'This report is not registered.' );
101 // }
102
103 $customReports = $this->getCustomReportsByName();
104 $cr = $this->getCustomReportsById();
105 $id = $cr[$customReports[$info->key]];
106 $optionValue = CRM_Core_BAO_OptionValue::del($id);
107
108 return $optionValue ? TRUE : FALSE;
109 }
110
111 /**
112 * @param CRM_Extension_Info $info
113 */
114 public function onPreDisable(CRM_Extension_Info $info) {
115 $customReports = $this->getCustomReportsByName();
116 $cr = $this->getCustomReportsById();
117 $id = $cr[$customReports[$info->key]];
118 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
119 }
120
121 /**
122 * @param CRM_Extension_Info $info
123 */
124 public function onPreEnable(CRM_Extension_Info $info) {
125 $customReports = $this->getCustomReportsByName();
126 $cr = $this->getCustomReportsById();
127 $id = $cr[$customReports[$info->key]];
128 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
129 }
130
131 /**
132 * @return array
133 */
134 public function getCustomReportsByName() {
135 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
136 }
137
138 /**
139 * @return array
140 */
141 public function getCustomReportsById() {
142 return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
143 }
144 }