SmartGroupRefresh.php 2.98 KB
Newer Older
bgm's avatar
bgm 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php

use CRM_ReportError_ExtensionUtil as E;

class CRM_ReportError_Handler_SmartGroupRefresh {

  /**
   * Try to handle a failing smartgroup refresh.
   * This will automatically disable a broken smartgroup.
   */
  static public function handler($vars, $options_overrides) {
    if (!isset($vars['exception']) && !isset($vars['pearError'])) {
      return FALSE;
    }

    if (!Civi::settings()->get('reporterror_smartgroups_autodisable')) {
      return FALSE;
    }

    $sql = NULL;
    $error_message = NULL;

    if (empty($sql) && isset($vars['exception']) && method_exists($vars['exception'], 'getExtraParams')) {
      $extra_params = $vars['exception']->getExtraParams();

      if (isset($extra_params['sql'])) {
        $sql = $extra_params['sql'];
      }

      $error_message = $vars['exception']->getMessage();
    }

    if (empty($sql) && isset($vars['pearError'])) {
      if (!empty($vars['pearError']->userinfo)) {
        $sql = $vars['pearError']->userinfo;
      }

      $error_message = $vars['pearError']->message;
    }

    if (preg_match('/^CREATE TEMPORARY TABLE civicrm_temp_group_contact_cache\d+ \(SELECT (\d+) as group_id/', $sql, $matches)) {
      $broken_group_id = $matches[1];

      $output = [
        'data' => [],
      ];

      $result = civicrm_api3('Group', 'getsingle', [
        'group_id' => $broken_group_id,
      ]);

      $description = ($result['description'] ? $result['description'] . ' -- ' : '') . 'Disabled automatically by reporterror: ' . $error_message;

      civicrm_api3('Group', 'create', [
        'group_id' => $broken_group_id,
        'description' => $description,
        'is_active' => 0,
      ]);

      if (CRM_Utils_Array::value('update_smart_groups', $_REQUEST) == 1) {
        CRM_Core_Session::setStatus(E::ts('ERROR: Group ID %1 could not be loaded and has been disabled. This may be the result of a deleted custom field or a bug in a custom search.', [1 => $broken_group_id]), '', 'error');
        CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/group', 'reset=1'));
        return TRUE;
      }
      else {
        // Assumes this is while the main database was being loaded on /civicrm/group?reset=1
        $output['data'][] = [
          'id' => 99999,
          'count' => 1,
          'title' => E::ts('ERROR: Group ID %1 could not be loaded and has been disabled. This may be the result of a deleted custom field or a bug in a custom search.', [1 => $broken_group_id]),
          'description' => '',
          'group_type' => '',
          'visibility' => '',
          'links' => '',
          'created_by' => '',
          'DT_RowId' => 'row_99999',
          'DT_RowClass' => 'crm-group-parent',
          'DT_RowAttr' => [
            'data-id' => 99999,
            'data-entity' => 'group',
          ],
        ];

        echo json_encode($output);
      }

      $vars['reporterror_subject'] = "SmartGroupRefresh";
      CRM_ReportError_Utils::sendReport($vars, $options_overrides);

      return TRUE;
    }

    return FALSE;
  }

}