<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Inspira_Widget_LandingPageAlbumsController extends Engine_Content_Widget_Abstract {

  public function indexAction() {
		$this->view->allParams = $this->_getAllParams();
    // Should we consider views or comments popular?
    $popularType = $this->_getParam('popularType', 'comment');
    if( !engine_in_array($popularType, array('comment', 'view')) ) {
        $popularType = 'comment';
    }
    $this->view->popularType = $popularType;
    $this->view->popularCol = $popularCol = $popularType . '_count';
    $params = array('search' => true);

    // Get paginator
    $table = Engine_Api::_()->getItemTable('album');
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('album.allow.unauthorized', 0))
        $select = $table->getItemsSelect($params);
    else
        $select = $table->select()->where('search = ?',1);
    $select->order($popularCol . ' DESC');

    $select = Engine_Api::_()->network()->getNetworkSelect($table->info('name'), $select);
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('album.allow.unauthorized', 0))
    $select = $table->getAuthorisedSelect($select);
    $select->limit(6);
    $this->view->paginator = $results = $table->fetchAll($select);

    // Do not render if nothing to show
    if( engine_count($results) <= 0 ) {
        return $this->setNoRender();
    }
  }
}
