<?php

class Cms_Cms_ModulesController extends Pampa_Controller_Action_Cms {
  /**
   * @var Model_Cms_Systems
   */
  private $modelCmsSystems;

  /**
   * @var Model_Cms_System_Modules
   */
  private $modelCmsModules;

  /**
   * @var Current module url
   */
  private $modulo;
  private $supModulo;

  /**
   * initialize controller, call super initialization
   */
  public function init() {
    // initialize all models
    $this->modelCmsSystems  = new Model_Cms_Systems();
    $this->modelCmsModules = new Model_Cms_System_Modules();

    $this->modulo = 'cms_modules';
    $this->supModulo = 'cms_system';

    parent::init();
  }

  /**
   * index page forward to list
   */
  public function indexAction() {
    $this->_forward("list");
  }

  /**
   * list based on filters
   */
  public function listAction() {

    // setup the list filters
    $filters = array(
            "t" => $this->_getParam("t", 20),
            "p" => $this->_getParam("p", 1),
            "s" => $this->_getParam("s"),
            "r" => $this->_getParam("r")
    );
    $this->view->filters = $filters;

    // get the resource
    $page = $this->modelCmsSystems->find($filters['r'])->current();
    if (!$page) {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo($this->supModulo, 'list');
    }
    $this->view->page = $page;

    // retrieves the items list
    $this->view->items = $this->modelCmsModules->adminList($filters);
  }

  /**
   * new form creates a blank instance
   */
  public function newAction()
  {
    // get the resource
    $pageId = $this->_getParam('r');
    $page = $this->modelCmsSystems->find($pageId)->current();
    if (!$page) {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo($this->supModulo, 'list');
    }
    $this->view->page = $page;

    $modelUserGropus = new Model_Cms_UsersGroups();
    $this->view->usergroups = $modelUserGropus->fetchForSelect("usergroup", 'id', null, false, 'usergroup DESC');

    $this->view->categories = $this->modelCmsSystems->fetchForSelect('page_cat', 'id', null, false, 'order_id');

		// create the blank row
    $this->view->module = $this->modelCmsModules->createRow();
  }

  /**
   * add the register to db, redirect with errors
   */
  public function addAction() {
    if(!$this->_request->isPost()) {
      $this->_redirector->gotoSimple("list");
    } else {
      // add the row
      $add = $this->modelCmsModules->submit($_POST['module'], $_FILES['image']);

      if($add->error) {
        // send the error
        $this->sendError($add->message, TRUE);
      } else {
        // send the success
        $this->sendSuccess('Item added!', $this->getUrlTo($this->modulo, 'edit', array('id' => $add->id, 'r' => $add->page_id)), TRUE);
      }
    }

  }

  /**
   * edit item checking valid id
   */
  public function editAction() {
    // get the resource
    $pageId = $this->_getParam('r');
    $page = $this->modelCmsSystems->find($pageId)->current();
    if (!$page) {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo($this->supModulo, 'list');
    }
    $this->view->page = $page;

		// page id
    $itemId = $this->_getParam("id");

    if(empty($itemId)) {
      $this->_redirector->gotoSimple("list");
    }
    // the page row
    $item = $this->modelCmsModules->find($itemId)->current();
    if(empty($item)) {
      $this->_redirector->gotoSimple("list");
    }
    $this->view->module = $item;

    $modelUserGropus = new Model_Cms_UsersGroups();
    $this->view->usergroups = $modelUserGropus->fetchForSelect("usergroup", 'id', null, false, 'usergroup DESC');

    $this->view->categories = $this->modelCmsSystems->fetchForSelect('page_cat', 'id', null, false, 'order_id');
  }

  /**
   * updates element on db
   */
  public function updateAction() {
    if(!$this->_request->isPost()) {
      $this->_redirector->gotoSimple("list");
    } else {
			
      // update the row
      $upd = $this->modelCmsModules->submit($_POST['module'], $_FILES['image']);

      if($upd->error) {
        // send the error
        $this->sendError($upd->message, TRUE);
      } else {
        // send the success
        $this->sendSuccess('Item updated!', $this->getUrlTo($this->modulo, 'edit', array('id' => $upd->id, 'r' => $upd->page_id)), TRUE);
      }
    }
  }

  /**
   * fully delete element from db
   */
  public function deleteAction() {
    // get the resource
    $pageId = $this->_getParam('r');
    $page_id = $this->_getParam("id");

    if(empty($page_id)) {
      $this->_redirector->gotoSimple("list");
    }

    $del = $this->modelCmsModules->deleteFromId($page_id);

    if($del->error) {
      $this->_helper->messenger('error', $del->message);
    } else {
      $this->_helper->messenger('ok', "Item removed!");
    }

    $this->redirectTo($this->modulo, 'list', array('r' => $pageId));
  }

	public function orderAction() {
    // row id
    $id = $this->_getParam("id", FALSE);
    // direction
    $dir = $this->_getParam("dir", FALSE);
    // category id
    $pageId = (int)$this->_getParam("r");

    // check for valids
    if($id === FALSE || $dir === FALSE || empty($pageId))
      $this->redirectTo($this->modulo, 'list', array('r' => $pageId));

    // change the row's order
    $orderSet = $this->modelCmsModules->setOrder($id, $dir, 'page_cat_id = '. $pageId);

    // set the message
    if($orderSet) {
      $this->_helper->messenger('ok', "Row moved!");
    } else {
      $this->_helper->messenger('error', "Error moving!");
    }

    $this->redirectTo($this->modulo, "list", array('r' => $pageId));
  }

  public function onlineAction()
  {
    // atualiza
    $upd = $this->modelCmsModules->online($_POST['id'], $_POST['online']);
    // show the json result
    $this->renderJson($upd);
  }

}