<?php

class Cms_Cms_SystemController extends Pampa_Controller_Action_Cms {

  /**
   * @var Model_Cms_Systems
   */
  private $modelCmsSystems;

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

  public function init() {
    // the models
    $this->modelCmsSystems = new Model_Cms_Systems();

    $this->modulo = 'cms_system';

    parent::init();
  }

  public function indexAction() {
    $this->_forward("list");
  }

  public function profileAction() {
    // get the logged user
    $user = Pampa_Auth_Cms::getInstance()->getIdentity();

    // redirect
    $this->redirectTo($this->modulo, "edit", array("id" => $user->id));
  }

  public function listAction() {

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

    $this->view->filters = $filters;

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

  public function newAction()
  {
    // create the blank row
    $this->view->system = $this->modelCmsSystems->createRow();
  }

  public function addAction() {
    if(!$this->_request->isPost()) {
      $this->_redirector->gotoSimple("list");
    } else {

      // add the row
      $add = $this->modelCmsSystems->submit($_POST['system']);
      
      if($add->error) {
        // send the error
        $this->sendError($add->message);
      } else {
        // send the success
        $this->sendSuccess('Item added!', $this->getUrlTo($this->modulo, 'edit', array('id' => $add->id)));
      }
    }

  }

  public function editAction() {
    // user id
    $user_id = $this->_getParam("id");

    if(empty($user_id)) {
      $this->_redirector->gotoSimple("list");
    }
    // the user row
    $this->view->system = $this->modelCmsSystems->find($user_id)->current();
  }

  public function updateAction() {
    if(!$this->_request->isPost()) {
      $this->_redirector->gotoSimple("list");
    } else {

      // update the row
      $update = $this->modelCmsSystems->submit($_POST['system']);

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

  public function deleteAction()
  {
    $user_id = $this->_getParam("id");

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

    $del = $this->modelCmsSystems->deleteFromId($user_id);

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

    $this->_redirector->gotoSimple("list");
  }

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

    // check for valids
    if($id === FALSE || $dir === FALSE)
      $this->redirectTo($this->modulo, "list");

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

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

    $this->redirectTo($this->modulo, "list");
  }

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

}