You are here

function _zoomapi_build_data_list in Zoom API 7.2

Build data / report table.

1 call to _zoomapi_build_data_list()
zoomapi_report_custom_list_form in ./zoomapi.admin.inc
Report: Custom List.

File

./zoomapi.admin.inc, line 173
Page callbacks for Zoom API administrative pages.

Code

function _zoomapi_build_data_list(&$form, &$form_state, $api) {
  $zoom_client = zoomapi_client();
  $query = drupal_get_query_parameters();
  $pager_info = [
    'page_size' => !empty($query['page_size']) ? $query['page_size'] : 20,
    'page_number' => !empty($query['page_number']) ? $query['page_number'] + 1 : 1,
    'sort_field' => !empty($query['order']) ? str_replace(' ', '_', strtolower($query['order'])) : '',
    'sort_dir' => !empty($query['sort']) ? $query['sort'] : 'asc',
    'total_records' => 0,
    'page_count' => 0,
  ];
  $cid = "zoomapi_data_list:{$api}";
  $items = [];
  $using_cached = FALSE;
  if ($cache = cache_get($cid)) {
    $items = $cache->expire > REQUEST_TIME ? $cache->data : [];
    $using_cached = !empty($items);
  }
  if (!$items) {
    $items = $zoom_client
      ->api($api)
      ->fetchAll();
    if ($items) {
      cache_set($cid, $items, 'cache', strtotime('+1 hour'));
    }
  }
  $form['list_wrapper'] = [
    'header' => [],
    'list' => [],
    'pager' => [],
    'empty' => [],
  ];
  if ($items) {
    $pager_info['total_records'] = count($items);
    $pager_info['page_count'] = ceil($pager_info['total_records'] / $pager_info['page_size']);
    $properties = _zoomapi_report_custom_list_get_properties($items);
    $header = _zoomapi_report_custom_list_get_header($properties);
    $defaults = array_fill_keys(array_keys($properties), '');
    array_walk($items, function (&$item) use ($defaults) {
      $item = array_replace($defaults, $item);
    });
    if ($pager_info['sort_field']) {
      usort($items, function ($a, $b) use ($pager_info) {
        $direction = $pager_info['sort_dir'] == 'asc' ? 1 : -1;
        $cmp = strnatcasecmp($a[$pager_info['sort_field']], $b[$pager_info['sort_field']]);
        return $cmp * $direction;
      });
    }
    foreach ($items as &$item) {
      foreach ($item as $property => $value) {
        if (is_array($value)) {
          $item[$property] = implode('<br/>', $value);
        }
      }
    }
    $pager_info['current_page'] = pager_default_initialize($pager_info['total_records'], $pager_info['page_size']);
    $chunks = array_chunk($items, $pager_info['page_size'], TRUE);
    $form['list_wrapper']['list'] = [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => !empty($chunks[$pager_info['current_page']]) ? $chunks[$pager_info['current_page']] : [],
    ];
    $form['list_wrapper']['pager'] = [
      '#theme' => 'pager',
      '#variables' => [
        'quantity' => $pager_info['page_count'],
      ],
    ];
  }
  else {
    $form['list_wrapper']['empty'] = [
      '#markup' => t('No "@api" records found.', [
        '@api' => $api,
      ]),
    ];
  }
  if ($using_cached) {
    $form['list_wrapper']['header']['clear_cache'] = [
      '#type' => 'submit',
      '#value' => t('Reload'),
      '#submit' => [
        '_zoomapi_build_data_list_clear_cache_submit',
      ],
    ];
  }
}