You are here

function _classified_block_view_popular in Classified Ads 7.3

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

Implements hook_block() for ('view', 'popular').

File

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

Code

function _classified_block_view_popular() {
  if (!module_exists('statistics')) {
    $ret = NULL;
  }
  else {
    $limit = _classified_get('popular-count');
    $vid = _classified_get('vid');

    /** @var SelectQuery $q */
    $q = db_select('node', 'n')
      ->comment(__FUNCTION__);
    $nc = $q
      ->leftJoin('node_counter', 'nc', 'n.nid = nc.nid');
    $q
      ->innerJoin('taxonomy_index', 'ti', 'n.nid = ti.nid');
    $td = $q
      ->innerJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid');
    $results = $q
      ->fields('n', array(
      'nid',
      'title',
    ))
      ->fields($td, array(
      'name',
    ))
      ->condition('n.status', 1)
      ->condition('n.type', 'classified')
      ->condition("{$td}.vid", $vid)
      ->addTag('node_access')
      ->orderBy("{$nc}.totalcount", 'DESC')
      ->orderBy("{$nc}.daycount", 'DESC')
      ->orderBy('n.title', 'ASC')
      ->orderBy('n.nid', 'DESC')
      ->range(0, $limit)
      ->execute();
    $ads = array();
    foreach ($results as $result) {
      $title = t('!title (!category)', array(
        '!title' => $result->title,
        '!category' => $result->name,
      ));
      $ads[] = l($title, 'node/' . $result->nid);
    }
    $ret = array(
      'subject' => t('Popular ads'),
      'content' => count($ads) ? array(
        '#theme' => 'item_list__classified_popular',
        '#items' => $ads,
      ) : t('No ad viewed yet.'),
    );
  }
  return $ret;
}