ColumnDefinitionTrait.php 13.4 KB
Newer Older
Mathieu Lutfy's avatar
Mathieu Lutfy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
/**
 * Created by IntelliJ IDEA.
 * User: emcnaughton
 * Date: 9/1/18
 * Time: 1:05 AM
 */

/**
 * Trait CRM_Extendedreport_Form_Report_ColumnDefinitionTrait
 *
 * This trait serves to organise the long getColumns functions into one function.
 *
 * It is for code organisation & may or may not make the most sense long term.
 */
trait CRM_Extendedreport_Form_Report_ColumnDefinitionTrait {
  /*
 * Function to get Activity Columns
 * @param array $options column options
 */
  /**
   * @param array $options
   *
   * @return array
   */
26 27
  function getActivityColumns($options = []) {
    $defaultOptions = [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
28 29 30 31 32 33
      'prefix' => '',
      'prefix_label' => '',
      'fields' => TRUE,
      'group_by' => FALSE,
      'order_by' => TRUE,
      'filters' => TRUE,
34 35 36 37 38
      'fields_defaults' => [],
      'filters_defaults' => [],
      'group_bys_defaults' => [],
      'order_by_defaults' => [],
    ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
39 40 41 42

    $options = array_merge($defaultOptions, $options);
    $defaults = $this->getDefaultsFromOptions($options);

43 44 45 46
    $spec = [
      'id' => [
        'name' => 'id',
        'title' => ts('Activity ID'),
Mathieu Lutfy's avatar
Mathieu Lutfy committed
47
        'is_group_bys' => $options['group_by'],
48 49 50
        'is_fields' => TRUE,
      ],
      'source_record_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
51 52
        'no_display' => TRUE,
        'required' => FALSE,
53 54
      ],
      'activity_type_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
55 56 57 58 59 60 61 62 63
        'title' => ts('Activity Type'),
        'alter_display' => 'alterActivityType',
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
        'operatorType' => CRM_Report_Form::OP_MULTISELECT,
        'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE),
        'name' => 'activity_type_id',
        'type' => CRM_Utils_Type::T_INT,
64 65
      ],
      'subject' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
66 67 68 69
        'title' => ts('Subject'),
        'name' => 'subject',
        'is_fields' => TRUE,
        'is_filters' => TRUE,
70
        'crm_editable' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
71 72 73
          'id_table' => 'civicrm_activity',
          'id_field' => 'id',
          'entity' => 'activity',
74
        ],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
75

76 77
      ],
      'activity_date_time' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
78 79 80 81 82 83 84 85
        'title' => ts('Activity Date'),
        'default' => TRUE,
        'name' => 'activity_date_time',
        'operatorType' => CRM_Report_Form::OP_DATE,
        'type' => CRM_Utils_Type::T_DATE,
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
86 87
      ],
      'status_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
88 89 90 91 92 93 94 95 96
        'title' => ts('Activity Status'),
        'name' => 'status_id',
        'type' => CRM_Utils_Type::T_STRING,
        'alter_display' => 'alterPseudoConstant',
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
        'operatorType' => CRM_Report_Form::OP_MULTISELECT,
        'options' => CRM_Core_PseudoConstant::activityStatus(),
97
        'crm_editable' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
98 99 100 101
          'id_table' => 'civicrm_activity',
          'id_field' => 'id',
          'entity' => 'activity',
          'options' => $this->_getOptions('activity', 'activity_status_id'),
102 103 104
        ],
      ],
      'duration' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
105 106
        'title' => ts('Duration (sum for all contacts)'),
        'type' => CRM_Utils_Type::T_INT,
107 108 109
        'statistics' => [
          'sum' => ts('Total Duration'),
        ],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
110
        'is_fields' => TRUE,
111 112
      ],
      'duration_each' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
113 114 115 116
        'title' => ts('Duration (for each contact)'),
        'name' => 'duration',
        'type' => CRM_Utils_Type::T_INT,
        'is_fields' => TRUE,
117 118
      ],
      'details' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
119 120 121 122
        'title' => ts('Activity Details'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'type' => CRM_Utils_Type::T_TEXT,
123
        'crm_editable' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
124 125 126
          'id_table' => 'civicrm_activity',
          'id_field' => 'id',
          'entity' => 'activity',
127
        ],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
128

129 130
      ],
      'result' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
131 132 133 134
        'title' => ts('Activity Result'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'type' => CRM_Utils_Type::T_TEXT,
135
        'crm_editable' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
136 137 138
          'id_table' => 'civicrm_activity',
          'id_field' => 'id',
          'entity' => 'activity',
139 140 141
        ],
      ],
      'is_current_revision' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
142 143 144 145
        'type' => CRM_Report_Form::OP_INT,
        'operatorType' => CRM_Report_Form::OP_SELECT,
        'title' => ts("Current Revision"),
        'name' => 'is_current_revision',
146
        'options' => ['1' => 'Yes', '0' => 'No',],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
147
        'is_filters' => TRUE,
148 149
      ],
      'is_deleted' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
150 151 152 153
        'type' => CRM_Report_Form::OP_INT,
        'operatorType' => CRM_Report_Form::OP_SELECT,
        'title' => ts("Is activity deleted"),
        'name' => 'is_deleted',
154
        'options' => ['0' => 'No', '1' => 'Yes',],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
155
        'is_filters' => TRUE,
156
      ],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
157

158
    ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
159 160 161 162 163 164 165 166 167 168 169 170 171
    return $this->buildColumns($spec, $options['prefix'] . 'civicrm_activity', 'CRM_Activity_DAO_Activity', NULL, $defaults, $options);
  }

  /**
   * Get columns for Case.
   *
   * @param $options
   *
   * @return array
   */
  function getCaseColumns($options) {
    $config = CRM_Core_Config::singleton();
    if (!in_array('CiviCase', $config->enableComponents)) {
172
      return ['civicrm_case' => ['fields' => [], 'metadata' => []]];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
173 174
    }

175 176 177 178
    $spec = [
      'civicrm_case' => [
        'fields' => [
          'id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
179 180 181
            'title' => ts('Case ID'),
            'name' => 'id',
            'is_fields' => TRUE,
182 183
          ],
          'subject' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
184 185 186 187
            'title' => ts('Case Subject'),
            'default' => TRUE,
            'is_fields' => TRUE,
            'is_filters' => TRUE,
188 189
          ],
          'status_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
190 191 192 193 194 195 196 197
            'title' => ts('Case Status'),
            'default' => TRUE,
            'name' => 'status_id',
            'is_fields' => TRUE,
            'is_filters' => TRUE,
            'operatorType' => CRM_Report_Form::OP_MULTISELECT,
            'options' => CRM_Case_BAO_Case::buildOptions('case_status_id'),
            'type' => CRM_Utils_Type::T_INT,
198 199
          ],
          'case_type_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
200 201 202 203 204 205 206
            'title' => ts('Case Type'),
            'is_fields' => TRUE,
            'is_filters' => TRUE,
            'operatorType' => CRM_Report_Form::OP_MULTISELECT,
            'options' => CRM_Case_BAO_Case::buildOptions('case_type_id'),
            'name' => 'case_type_id',
            'type' => CRM_Utils_Type::T_INT,
207 208
          ],
          'start_date' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
209 210 211 212 213 214
            'title' => ts('Case Start Date'),
            'name' => 'start_date',
            'operatorType' => CRM_Report_Form::OP_DATE,
            'type' => CRM_Utils_Type::T_DATE,
            'is_fields' => TRUE,
            'is_filters' => TRUE,
215 216
          ],
          'end_date' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
217 218 219 220 221 222
            'title' => ts('Case End Date'),
            'name' => 'end_date',
            'is_fields' => TRUE,
            'is_filters' => TRUE,
            'operatorType' => CRM_Report_Form::OP_DATE,
            'type' => CRM_Utils_Type::T_DATE,
223 224
          ],
          'is_deleted' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
225 226 227 228 229 230
            'name' => 'is_deleted',
            'title' => ts('Case Deleted?'),
            'type' => CRM_Utils_Type::T_BOOLEAN,
            'is_fields' => TRUE,
            'is_filters' => TRUE,
            'operatorType' => CRM_Report_Form::OP_SELECT,
231 232 233 234
            'options' => ['' => '--select--'] + CRM_Case_BAO_Case::buildOptions('is_deleted'),
          ],
        ],
      ],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
235

236
    ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
237 238 239 240 241 242 243 244 245
    // Case is a special word in mysql so pass an alias to prevent it from using case.
    return $this->buildColumns($spec['civicrm_case']['fields'], $options['prefix'] . 'civicrm_case', 'CRM_Case_DAO_Case', 'case_civireport');
  }

  /**
   * @param array $options
   *
   * @return array
   */
246 247
  function getContactColumns($options = []) {
    $defaultOptions = [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
248 249 250 251 252 253
      'prefix' => '',
      'prefix_label' => '',
      'group_by' => TRUE,
      'order_by' => TRUE,
      'filters' => TRUE,
      'fields' => TRUE,
254 255 256 257 258
      'custom_fields' => ['Individual', 'Contact', 'Organization'],
      'fields_defaults' => ['display_name', 'id'],
      'filters_defaults' => [],
      'group_bys_defaults' => [],
      'order_by_defaults' => ['sort_name ASC'],
Mathieu Lutfy's avatar
Mathieu Lutfy committed
259
      'contact_type' => NULL,
260
    ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
261 262 263 264 265 266 267 268

    $options = array_merge($defaultOptions, $options);
    $orgOnly = FALSE;
    if (CRM_Utils_Array::value('contact_type', $options) == 'Organization') {
      $orgOnly = TRUE;
    }
    $tableAlias = $options['prefix'] . 'civicrm_contact';

269 270
    $spec = [
      $options['prefix'] . 'display_name' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
271 272 273
        'name' => 'display_name',
        'title' => ts($options['prefix_label'] . 'Contact Name'),
        'is_fields' => TRUE,
274 275 276 277
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
      ],
      $options['prefix'] . 'contact_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
278 279 280 281 282 283 284 285 286
        'name' => 'id',
        'title' => ts($options['prefix_label'] . 'Contact ID'),
        'alter_display' => 'alterContactID',
        'type' => CRM_Utils_Type::T_INT,
        'is_order_bys' => TRUE,
        'is_group_bys' => TRUE,
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_contact_filter' => TRUE,
287 288
      ],
      $options['prefix'] . 'external_identifier' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
289 290 291 292
        'name' => 'external_identifier',
        'title' => ts($options['prefix_label'] . 'External ID'),
        'type' => CRM_Utils_Type::T_INT,
        'is_fields' => TRUE,
293 294
      ],
      $options['prefix'] . 'sort_name' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
295 296 297 298 299
        'name' => 'sort_name',
        'title' => ts($options['prefix_label'] . 'Contact Name (in sort format)'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
300 301
      ],
      $options['prefix'] . 'contact_type' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
302 303 304 305 306 307 308
        'title' => ts($options['prefix_label'] . 'Contact Type'),
        'name' => 'contact_type',
        'operatorType' => CRM_Report_Form::OP_MULTISELECT,
        'options' => CRM_Contact_BAO_Contact::buildOptions('contact_type'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_group_bys' => TRUE,
309 310
      ],
      $options['prefix'] . 'contact_sub_type' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
311 312 313 314 315 316 317
        'title' => ts($options['prefix_label'] . 'Contact Sub Type'),
        'name' => 'contact_sub_type',
        'operatorType' => CRM_Report_Form::OP_MULTISELECT,
        'options' => CRM_Contact_BAO_Contact::buildOptions('contact_sub_type'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_group_bys' => TRUE,
318 319 320 321
      ],
    ];
    $individualFields = [
      $options['prefix'] . 'first_name' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
322 323 324 325 326
        'name' => 'first_name',
        'title' => ts($options['prefix_label'] . 'First Name'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
327 328
      ],
      $options['prefix'] . 'middle_name' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
329 330 331
        'name' => 'middle_name',
        'title' => ts($options['prefix_label'] . 'Middle Name'),
        'is_fields' => TRUE,
332 333
      ],
      $options['prefix'] . 'last_name' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
334 335 336 337 338
        'name' => 'last_name',
        'title' => ts($options['prefix_label'] . 'Last Name'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
339 340
      ],
      $options['prefix'] . 'nick_name' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
341 342 343 344 345
        'name' => 'nick_name',
        'title' => ts($options['prefix_label'] . 'Nick Name'),
        'is_fields' => TRUE,
        'is_filters' => TRUE,
        'is_order_bys' => TRUE,
346 347
      ],
      $options['prefix'] . 'gender_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
348 349 350 351 352 353 354
        'name' => 'gender_id',
        'title' => ts($options['prefix_label'] . 'Gender'),
        'options' => CRM_Contact_BAO_Contact::buildOptions('gender_id'),
        'operatorType' => CRM_Report_Form::OP_MULTISELECT,
        'alter_display' => 'alterGenderID',
        'is_fields' => TRUE,
        'is_filters' => TRUE,
355 356
      ],
      'birth_date' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
357 358 359 360 361
        'title' => ts($options['prefix_label'] . 'Birth Date'),
        'operatorType' => CRM_Report_Form::OP_DATE,
        'type' => CRM_Utils_Type::T_DATE,
        'is_fields' => TRUE,
        'is_filters' => TRUE,
362 363
      ],
      'age' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
364 365 366 367
        'title' => ts($options['prefix_label'] . 'Age'),
        'dbAlias' => 'TIMESTAMPDIFF(YEAR, ' . $tableAlias . '.birth_date, CURDATE())',
        'type' => CRM_Utils_Type::T_INT,
        'is_fields' => TRUE,
368 369
      ],
    ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
370 371 372 373 374
    if (!$orgOnly) {
      $spec = array_merge($spec, $individualFields);
    }

    if (!empty($options['custom_fields'])) {
375
      $this->_customGroupExtended[$options['prefix'] . 'civicrm_contact'] = [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
376 377 378 379 380
        'extends' => $options['custom_fields'],
        'title' => $options['prefix_label'],
        'filters' => $options['filters'],
        'prefix' => $options['prefix'],
        'prefix_label' => $options['prefix_label'],
381
      ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
382 383 384 385 386 387 388 389 390 391 392 393 394
    }

    return $this->buildColumns($spec, $options['prefix'] . 'civicrm_contact', 'CRM_Contact_DAO_Contact', $tableAlias, $this->getDefaultsFromOptions($options), $options);
  }

  /**
   * Function to get Activity Columns.
   *
   * @param array $options column options
   *
   * @return array
   */
  function getLatestActivityColumns($options) {
395
    $defaultOptions = [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
396 397 398 399 400 401
      'prefix' => '',
      'prefix_label' => '',
      'fields' => TRUE,
      'group_by' => FALSE,
      'order_by' => TRUE,
      'filters' => TRUE,
402 403 404 405
      'defaults' => [
        'country_id' => TRUE,
      ],
    ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
406
    $options = array_merge($defaultOptions, $options);
407 408
    $activityFields['civicrm_activity']['fields'] = [
      'activity_type_id' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
409 410 411 412 413
        'title' => ts('Latest Activity Type'),
        'default' => FALSE,
        'type' => CRM_Utils_Type::T_STRING,
        'alter_display' => 'alterActivityType',
        'is_fields' => TRUE,
414 415
      ],
      'activity_date_time' => [
Mathieu Lutfy's avatar
Mathieu Lutfy committed
416 417 418
        'title' => ts('Latest Activity Date'),
        'default' => FALSE,
        'is_fields' => TRUE,
419 420
      ],
    ];
Mathieu Lutfy's avatar
Mathieu Lutfy committed
421 422 423 424
    return $this->buildColumns($activityFields['civicrm_activity']['fields'], $options['prefix'] . 'civicrm_activity', 'CRM_Activity_DAO_Activity');
  }

}