| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 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 api exposes CiviCRM report templates. |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Retrieve a report template. |
| 36 | * |
| 37 | * FIXME This is a bare-minimum placeholder |
| 38 | * |
| 39 | * @param array $params |
| 40 | * |
| 41 | * @return array |
| 42 | * details of found Option Values |
| 43 | */ |
| 44 | function civicrm_api3_report_template_get($params) { |
| 45 | require_once 'api/v3/OptionValue.php'; |
| 46 | $params['option_group_id'] = CRM_Core_DAO::getFieldValue( |
| 47 | 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name' |
| 48 | ); |
| 49 | return civicrm_api3_option_value_get($params); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Add an OptionValue. |
| 54 | * |
| 55 | * OptionValues are used to classify CRM entities (including Contacts, Groups and Actions). |
| 56 | * |
| 57 | * @param array $params |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | function civicrm_api3_report_template_create($params) { |
| 62 | require_once 'api/v3/OptionValue.php'; |
| 63 | $params['option_group_id'] = CRM_Core_DAO::getFieldValue( |
| 64 | 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name' |
| 65 | ); |
| 66 | if (!empty($params['component_id']) && !is_numeric($params['component_id'])) { |
| 67 | $components = CRM_Core_PseudoConstant::get('CRM_Core_DAO_OptionValue', 'component_id', array('onlyActive' => FALSE, 'labelColumn' => 'name')); |
| 68 | $params['component_id'] = array_search($params['component_id'], $components); |
| 69 | } |
| 70 | return civicrm_api3_option_value_create($params); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Adjust Metadata for Create action. |
| 75 | * |
| 76 | * The metadata is used for setting defaults, documentation & validation. |
| 77 | * |
| 78 | * @param array $params |
| 79 | * Array of parameters determined by getfields. |
| 80 | */ |
| 81 | function _civicrm_api3_report_template_create_spec(&$params) { |
| 82 | require_once 'api/v3/OptionValue.php'; |
| 83 | _civicrm_api3_option_value_create_spec($params); |
| 84 | $params['value']['api.aliases'] = array('report_url'); |
| 85 | $params['name']['api.aliases'] = array('class_name'); |
| 86 | $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue( |
| 87 | 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name' |
| 88 | ); |
| 89 | // $params['component']['api.required'] = TRUE; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Deletes an existing ReportTemplate. |
| 94 | * |
| 95 | * @param array $params |
| 96 | * |
| 97 | * @return array |
| 98 | * Api result |
| 99 | */ |
| 100 | function civicrm_api3_report_template_delete($params) { |
| 101 | require_once 'api/v3/OptionValue.php'; |
| 102 | return civicrm_api3_option_value_delete($params); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Retrieve rows from a report template. |
| 107 | * |
| 108 | * @param array $params |
| 109 | * Input parameters. |
| 110 | * |
| 111 | * @return array |
| 112 | * details of found instances |
| 113 | */ |
| 114 | function civicrm_api3_report_template_getrows($params) { |
| 115 | civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id')); |
| 116 | list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params); |
| 117 | return civicrm_api3_create_success($rows, $params, 'ReportTemplate', 'getrows', CRM_Core_DAO::$_nullObject, $metadata); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get report template rows. |
| 122 | * |
| 123 | * @param array $params |
| 124 | * |
| 125 | * @return array |
| 126 | * @throws API_Exception |
| 127 | * @throws CiviCRM_API3_Exception |
| 128 | */ |
| 129 | function _civicrm_api3_report_template_getrows($params) { |
| 130 | if (empty($params['report_id'])) { |
| 131 | $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id')); |
| 132 | } |
| 133 | |
| 134 | $class = civicrm_api3('option_value', 'getvalue', array( |
| 135 | 'option_group_name' => 'report_template', |
| 136 | 'return' => 'name', |
| 137 | 'value' => $params['report_id'], |
| 138 | ) |
| 139 | ); |
| 140 | |
| 141 | $reportInstance = new $class(); |
| 142 | if (!empty($params['instance_id'])) { |
| 143 | $reportInstance->setID($params['instance_id']); |
| 144 | } |
| 145 | $reportInstance->setParams($params); |
| 146 | $reportInstance->noController = TRUE; |
| 147 | $reportInstance->preProcess(); |
| 148 | $reportInstance->setDefaultValues(FALSE); |
| 149 | $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params)); |
| 150 | $options = _civicrm_api3_get_options_from_params($params, TRUE, 'ReportTemplate', 'get'); |
| 151 | $reportInstance->setLimitValue($options['limit']); |
| 152 | $reportInstance->setOffsetValue($options['offset']); |
| 153 | $reportInstance->beginPostProcessCommon(); |
| 154 | $sql = $reportInstance->buildQuery(); |
| 155 | $rows = $metadata = $requiredMetadata = array(); |
| 156 | $reportInstance->buildRows($sql, $rows); |
| 157 | $requiredMetadata = array(); |
| 158 | if (isset($params['options']) && !empty($params['options']['metadata'])) { |
| 159 | $requiredMetadata = $params['options']['metadata']; |
| 160 | if (in_array('title', $requiredMetadata)) { |
| 161 | $metadata['metadata']['title'] = $reportInstance->getTitle(); |
| 162 | } |
| 163 | if (in_array('labels', $requiredMetadata)) { |
| 164 | foreach ($reportInstance->_columnHeaders as $key => $header) { |
| 165 | //would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty |
| 166 | //NB I think these are already translated |
| 167 | $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : ''; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return array($rows, $reportInstance, $metadata); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get statistics from a given report. |
| 176 | * |
| 177 | * @param array $params |
| 178 | * |
| 179 | * @return array |
| 180 | */ |
| 181 | function civicrm_api3_report_template_getstatistics($params) { |
| 182 | list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params); |
| 183 | $stats = $reportInstance->statistics($rows); |
| 184 | return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata); |
| 185 | } |
| 186 | /** |
| 187 | * Adjust metadata for template getrows action. |
| 188 | * |
| 189 | * @param array $params |
| 190 | * Input parameters. |
| 191 | */ |
| 192 | function _civicrm_api3_report_template_getrows_spec(&$params) { |
| 193 | $params['report_id'] = array( |
| 194 | 'title' => 'Report ID - eg. member/lapse', |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | /* @codingStandardsIgnoreStart |
| 199 | function civicrm_api3_report_template_getfields($params) { |
| 200 | return civicrm_api3_create_success(array( |
| 201 | 'id' => array( |
| 202 | 'name' => 'id', |
| 203 | 'type' => 1, |
| 204 | 'required' => 1, |
| 205 | ), |
| 206 | 'option_group_id' => array( |
| 207 | 'name' => 'option_group_id', |
| 208 | 'type' => 1, |
| 209 | 'required' => 1, |
| 210 | 'FKClassName' => 'CRM_Core_DAO_OptionGroup', |
| 211 | ), |
| 212 | 'label' => array( |
| 213 | 'name' => 'label', |
| 214 | 'type' => 2, |
| 215 | 'title' => 'Option Label', |
| 216 | 'required' => 1, |
| 217 | 'maxlength' => 255, |
| 218 | 'size' => 45, |
| 219 | ), |
| 220 | 'value' => array( |
| 221 | 'name' => 'value', |
| 222 | 'type' => 2, |
| 223 | 'title' => 'Option Value', |
| 224 | 'required' => 1, |
| 225 | 'maxlength' => 512, |
| 226 | 'size' => 45, |
| 227 | ), |
| 228 | 'name' => array( |
| 229 | 'name' => 'name', |
| 230 | 'type' => 2, |
| 231 | 'title' => 'Option Name', |
| 232 | 'maxlength' => 255, |
| 233 | 'size' => 45, |
| 234 | 'import' => 1, |
| 235 | 'where' => 'civicrm_option_value.name', |
| 236 | 'export' => 1, |
| 237 | ), |
| 238 | 'grouping' => array( |
| 239 | 'name' => 'grouping', |
| 240 | 'type' => 2, |
| 241 | 'title' => 'Option Grouping Name', |
| 242 | 'maxlength' => 255, |
| 243 | 'size' => 45, |
| 244 | ), |
| 245 | 'filter' => array( |
| 246 | 'name' => 'filter', |
| 247 | 'type' => 1, |
| 248 | 'title' => 'Filter', |
| 249 | ), |
| 250 | 'is_default' => array( |
| 251 | 'name' => 'is_default', |
| 252 | 'type' => 16, |
| 253 | ), |
| 254 | 'weight' => array( |
| 255 | 'name' => 'weight', |
| 256 | 'type' => 1, |
| 257 | 'title' => 'Weight', |
| 258 | 'required' => 1, |
| 259 | ), |
| 260 | 'description' => array( |
| 261 | 'name' => 'description', |
| 262 | 'type' => 32, |
| 263 | 'title' => 'Description', |
| 264 | 'rows' => 8, |
| 265 | 'cols' => 60, |
| 266 | ), |
| 267 | 'is_optgroup' => array( |
| 268 | 'name' => 'is_optgroup', |
| 269 | 'type' => 16, |
| 270 | ), |
| 271 | 'is_reserved' => array( |
| 272 | 'name' => 'is_reserved', |
| 273 | 'type' => 16, |
| 274 | ), |
| 275 | 'is_active' => array( |
| 276 | 'name' => 'is_active', |
| 277 | 'type' => 16, |
| 278 | ), |
| 279 | 'component_id' => array( |
| 280 | 'name' => 'component_id', |
| 281 | 'type' => 1, |
| 282 | 'FKClassName' => 'CRM_Core_DAO_Component', |
| 283 | ), |
| 284 | 'domain_id' => array( |
| 285 | 'name' => 'domain_id', |
| 286 | 'type' => 1, |
| 287 | 'FKClassName' => 'CRM_Core_DAO_Domain', |
| 288 | ), |
| 289 | 'visibility_id' => array( |
| 290 | 'name' => 'visibility_id', |
| 291 | 'type' => 1, |
| 292 | 'default' => 'UL', |
| 293 | ), |
| 294 | )); |
| 295 | } |
| 296 | @codingStandardsIgnoreEnd */ |