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

class Inspira_Widget_LandingPageMusicController extends Engine_Content_Widget_Abstract {

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

    $music = Engine_Api::_()->getApi('core', 'music');
    $params = array('search' => true);

    // Get paginator
    $table = Engine_Api::_()->getItemTable('music_playlist');
    $select = $music->getItemsSelect($table->select(), $params)
                  ->order($popularCol . ' DESC');

    $select = Engine_Api::_()->network()->getNetworkSelect($table->info('name'), $select);

    $authorisedSelect = $music->getAuthorisedSelect($select);
    $this->view->paginator = $paginator = Zend_Paginator::factory($authorisedSelect);

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 3));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));

    // Hide if nothing to show
    if( $paginator->getTotalItemCount() <= 0 ) {
        return $this->setNoRender();
    }
  }
}
