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