You are here

function _classified_page_overview in Classified Ads 7.3

Same name and namespace in other branches
  1. 6.3 classified.module \_classified_page_overview()

Page callback for 'classified'.

Build a page listing ad categories and the number of ads in them. This is a bit heavy on CPU so we cache the whole overview.

Return value

array Render array for the listing.

1 string reference to '_classified_page_overview'
classified_menu in ./classified.module
Implements hook_menu().

File

./classified.module, line 465
A pure D7 classified ads module inspired by the ed_classified module.

Code

function _classified_page_overview() {
  $cid = 'classified:overview';
  $admin_access = user_access('administer nodes') || user_access('administer classified ads');
  $cached = $admin_access ? FALSE : cache_get($cid);
  if ($cached) {
    list($header, $rows, $total) = empty($cached->data) ? array(
      array(),
      array(),
      0,
    ) : $cached->data;

    // Just in case you'd want to give a hint that the data is not really fresh.
    $ret['table']['#attributes']['class']['classified-cached'] = 'classified-cached';
  }
  else {
    $vid = _classified_get('vid');
    $terms = taxonomy_get_tree($vid);
    $header = array(
      t('Ad category'),
      array(
        'class' => 'classified-number',
        'data' => t('# Ads'),
      ),
    );
    $rows = array();
    $total = 0;
    foreach ($terms as $term) {
      $q = new EntityFieldQuery();
      $q
        ->fieldCondition('classified_category', 'tid', $term->tid, '=', 0)
        ->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', 'classified')
        ->count();
      if (!$admin_access) {
        $q
          ->propertyCondition('status', 1);
      }
      $count = $q
        ->execute();
      $params = array(
        '!link' => l($term->name, classified_term_path($term)),
        '!description' => filter_xss_admin($term->description),
      );
      $category = empty($term->description) ? $params['!link'] : t('!link - !description', $params);
      $rows[] = array(
        theme('indentation', array(
          'size' => $term->depth,
        )) . $category,
        array(
          'class' => 'classified-number',
          'data' => $count,
        ),
      );
      $total += $count;
    }
    if (!$admin_access) {

      // One minute minimum lifetime.
      cache_set($cid, array(
        $header,
        $rows,
        $total,
      ), 'cache', REQUEST_TIME + 60);
    }
  }
  $link = url('node/add/classified');
  $caption = user_access('create classified content') ? format_plural($total, 'Only one Classified Ad. <a href="!link">Add one</a>', '@count Classified Ads. <a href="!link">Add one</a>', array(
    '!link' => $link,
  )) : format_plural($total, 'Only one Classified Ad.', '@count Classified Ads');
  $ret = array(
    'table' => array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => array(
        'class' => array(
          'classified-term-list',
        ),
      ),
      '#caption' => $caption,
    ),
  );
  return $ret;
}