| 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 | } |
| 28 | |
| 29 | public function getGroupId() { |
| 30 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
| 31 | self::REPORT_GROUP_NAME, 'id', 'name' |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param CRM_Extension_Info $info |
| 37 | * |
| 38 | * @throws CRM_Core_Exception |
| 39 | */ |
| 40 | public function onPreInstall(CRM_Extension_Info $info) { |
| 41 | $customReports = $this->getCustomReportsByName(); |
| 42 | if (array_key_exists($info->key, $customReports)) { |
| 43 | throw new CRM_Core_Exception(ts('This report is already registered.')); |
| 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)) { |
| 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']])); |
| 55 | } |
| 56 | $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', |
| 57 | ['option_group_id' => $this->getGroupId()] |
| 58 | ); |
| 59 | $params = [ |
| 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, |
| 66 | 'option_group_id' => $this->getGroupId(), |
| 67 | 'is_active' => 1, |
| 68 | ]; |
| 69 | |
| 70 | $optionValue = CRM_Core_BAO_OptionValue::add($params); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param CRM_Extension_Info $info |
| 75 | * |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function onPreUninstall(CRM_Extension_Info $info) { |
| 79 | $customReports = $this->getCustomReportsByName(); |
| 80 | $cr = $this->getCustomReportsById(); |
| 81 | $id = $cr[$customReports[$info->key]]; |
| 82 | $optionValue = CRM_Core_BAO_OptionValue::del($id); |
| 83 | |
| 84 | return $optionValue ? TRUE : FALSE; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param CRM_Extension_Info $info |
| 89 | */ |
| 90 | public function onPreDisable(CRM_Extension_Info $info) { |
| 91 | $customReports = $this->getCustomReportsByName(); |
| 92 | $cr = $this->getCustomReportsById(); |
| 93 | $id = $cr[$customReports[$info->key]]; |
| 94 | $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param CRM_Extension_Info $info |
| 99 | */ |
| 100 | public function onPreEnable(CRM_Extension_Info $info) { |
| 101 | $customReports = $this->getCustomReportsByName(); |
| 102 | $cr = $this->getCustomReportsById(); |
| 103 | $id = $cr[$customReports[$info->key]]; |
| 104 | $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return array |
| 109 | */ |
| 110 | public function getCustomReportsByName() { |
| 111 | return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return array |
| 116 | */ |
| 117 | public function getCustomReportsById() { |
| 118 | return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE); |
| 119 | } |
| 120 | |
| 121 | } |