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

class Stories_AdminManageController extends Core_Controller_Action_Admin
{

	public function indexAction()
	{

		$this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('stories_admin_main', array(), 'stories_admin_main_manage');

		$this->view->formFilter = $formFilter = new Stories_Form_Admin_Manage_Filter();

		// Process form
		$values = array();
		if ($formFilter->isValid($this->_getAllParams())) {
			$values = $formFilter->getValues();
		}
		foreach ($_GET as $key => $value) {
			if ('' === $value) {
				unset($_GET[$key]);
			} else
				$values[$key] = $value;
		}
		if ($this->getRequest()->isPost()) {
			$values = $this->getRequest()->getPost();
			foreach ($values as $key => $value) {
				if ($key == 'delete_' . $value) {
					$story = Engine_Api::_()->getItem('stories_story', $value);
					$story->delete($story);
				}
			}
		}

		$table = Engine_Api::_()->getDbtable('stories', 'stories');
		$tableName = $table->info('name');

		$tableUserName = Engine_Api::_()->getItemTable('user')->info('name');

		$select = $table->select()
			->from($tableName)
			->setIntegrityCheck(false)
			->joinLeft($tableUserName, "$tableUserName.user_id = $tableName.owner_id", 'username')

			->order('story_id DESC');

		// Set up select info
		if (!empty($_GET['title']))
			$select->where($tableName . '.title LIKE ?', '%' . $values['title'] . '%');
		if (isset($_GET['type']) && $_GET['type'] != ''){
			
			if($_GET["type"] == 0){
				$select->where($tableName . '.file_id != ?', 0);
				$select->where($tableName . '.type = ?', 0);
			}else if($_GET["type"] == 3){
				$select->where($tableName . '.file_id = ?', 0);
				$select->where($tableName . '.type = ?', 0);
			}else{
				$select->where($tableName . '.type = ?', $values['type']);
			}
		}
		if (isset($_GET['plateform']) && $_GET['plateform'] != '')
			$select->where($tableName . '.plateform = ?', $values['plateform']);
		if (!empty($values['creation_date']))
			$select->where($tableName . '.creation_date LIKE ?', $values['creation_date'] . '%');
 
		if (!empty($_GET['owner_name']))
			$select->where($tableUserName . '.displayname LIKE ?', '%' . $_GET['owner_name'] . '%');

		$page = $this->_getParam('page', 1);
		$this->view->paginator = $paginator = Zend_Paginator::factory($select);
		$paginator->setItemCountPerPage(10);
		$paginator->setCurrentPageNumber($page);
	}

	public function deleteAction()
	{

		// In smoothbox
		$this->_helper->layout->setLayout('admin-simple');

		$this->view->form = $form = new Core_Form_Admin_Delete();
		$form->setTitle('Delete Story?');
		$form->setDescription('Are you sure that you want to delete this story? It will not be recoverable after being deleted. ');
		$form->submit->setLabel('Delete');

		$id = $this->_getParam('id');
		$this->view->note_id = $id;
		// Check post
		if ($this->getRequest()->isPost()) {
			$db = Engine_Db_Table::getDefaultAdapter();
			$db->beginTransaction();

			try {
				$story = Engine_Api::_()->getItem('stories_story', $id);
				$story->delete();
				$db->commit();
			} catch (Exception $e) {
				$db->rollBack();
				throw $e;
			}
			$this->_forward('success', 'utility', 'core', array(
				'smoothboxClose' => 10,
				'parentRefresh' => 10,
				'messages' => array('')
			));
		}
	}
}
