<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Music_AdminManageController extends Core_Controller_Action_Admin
{
  public function init()
  {
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('music_admin_main', array(), 'music_admin_main_manage');
  }
  
  public function indexAction()
  {

    if ($this->getRequest()->isPost())
    {
      $values = $this->getRequest()->getPost();
      foreach ($values as $key=>$value) {
        if ($key == 'delete_' . $value)
        {
          $playlist = Engine_Api::_()->getItem('music_playlist', $value);
          $playlist->delete();
        }
      }
    }
    $page = $this->_getParam('page',1);
    $this->view->paginator = Engine_Api::_()->music()->getPlaylistPaginator(array('adminView' => true, 'showmusic' => 1,));
    $this->view->paginator->setItemCountPerPage(25);
    $this->view->paginator->setCurrentPageNumber($page);
  }

  public function suggestAction()
  {
    $page = $this->_getParam('page');
    $query = $this->_getParam('query');

    $playlistTable = Engine_Api::_()->getItemTable('music_playlist');
    $playlistSelect = $playlistTable->select()
      ->where('title LIKE ?', $query . '%');
    $paginator = Zend_Paginator::factory($playlistSelect);
    $paginator->setCurrentPageNumber($page);

    $data = array();
    foreach( $paginator as $playlist ) {
      $data[$playlist->playlist_id] = $playlist->getTitle();
    }
    $this->view->status = true;
    $this->view->data = $data;
  }

  public function infoAction()
  {
    $playlistIdentity = $this->_getParam('playlist_id');
    if( !$playlistIdentity ) {
      $this->view->status = false;
      return;
    }

    $playlist = Engine_Api::_()->getItem('music_playlist', $playlistIdentity);
    if( !$playlist ) {
      $this->view->status = false;
      return;
    }

    $this->view->status = true;
    $this->view->identity = $playlist->getIdentity();
    $this->view->title = $playlist->getTitle();
    $this->view->description = $playlist->getDescription();
    $this->view->href = $playlist->getHref();
    $this->view->photo = $playlist->getPhotoUrl('thumb.profile');
  }

  public function deleteAction()
  {
    // In smoothbox
    $this->_helper->layout->setLayout('admin-simple');
    $id = $this->_getParam('id');
    $this->view->playlist_id=$id;
    // Check post
    if( $this->getRequest()->isPost() )
    {
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();

      try
      {
        $playlist = Engine_Api::_()->getItem('music_playlist', $id);
        // delete the playlist into the database
        $playlist->delete();
        $db->commit();
      }

      catch( Exception $e )
      {
        $db->rollBack();
        throw $e;
      }

      $this->_forward('success', 'utility', 'core', array(
          'smoothboxClose' => 10,
          'parentRefresh'=> 10,
          'messages' => array('')
      ));
    }

    // Output
    $this->renderScript('admin-manage/delete.tpl');
  }
  
  //Approved Action
  public function approvedAction() {
  
    $id = $this->_getParam('id');
    if (!empty($id)) {
    
      $item = Engine_Api::_()->getItem('music_playlist', $id);
      $item->approved = !$item->approved;
      $item->save();

      // Re-index
      Engine_Api::_()->getApi('search', 'core')->index($item);
      
      if ($item->approved) {
        Engine_Api::_()->getDbTable('notifications', 'activity')->addNotification($item->getOwner(), $item->getOwner(), $item, 'music_approvedbyadmin', array('music_title' => $item->getTitle(), 'musicowner_title' => $item->getOwner()->getTitle(), 'object_link' => $item->getHref(), 'host' => $_SERVER['HTTP_HOST']));
      } else {
        Engine_Api::_()->getDbTable('notifications', 'activity')->addNotification($item->getOwner(), $item->getOwner(), $item, 'music_disapprovedbyadmin', array('music_title' => $item->getTitle(), 'musicowner_title' => $item->getOwner()->getTitle(), 'object_link' => $item->getHref(), 'host' => $_SERVER['HTTP_HOST']));
      }
    }
    $this->_redirect('admin/music/manage');
  }
}
