<?php

class Cms_AgendascategoriasController extends Pampa_Controller_Action_Cms {
  /**
   * @var Model_Tags
   */
  private $_model;
  private $modelCategories;
  private $categoryId;
  /**
   * initialize controller, call super initialization
   */
  public function init() {
    // initialize all models
    $this->_model = new Model_Agendas_Categorias();
    
    
    $this->categoryId = $this->_getParam("r");
    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", 20000),
            "p" => $this->_getParam("p", 1),
            "s" => $this->_getParam("s")
    );

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

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

    
  }

  /**
   * new form creates a blank instance
   */
  public function newAction() {
    // create the blank row
    $this->view->page = $this->_model->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->_model->submit($_POST['page']);

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

  }

  /**
   * edit item checking valid id
   */
  public function editAction() {
    // page id
    $page_id = $this->_getParam("id");

    if(empty($page_id)) {
      $this->_redirector->gotoSimple("list");
    }
    // the page row
    $page = $this->_model->find($page_id)->current();
    if(empty($page)) {
      $this->_redirector->gotoSimple("list");
    }
    
    $this->view->page = $page;
  }

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

      if($upd->error) {
        // send the error
        $this->sendError($upd->message, TRUE);
      } else {
        // send the success
        $this->sendSuccess('Item updated!', $this->getUrlTo('agendascategorias', 'edit', array('id' => $upd->id)), false);
      }
    }
  }
  
  /**
   * fully delete element from db
   */
  public function deleteAction() {
    $page_id = $this->_getParam("id");

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

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

    if($del->error) {
      $this->_helper->messenger('error', $del->message);
    } else {
      // $this->_helper->messenger('ok', "Item removed!");
    }
    $this->sendSuccess('Item removed!', $this->getUrlTo('agendascategorias', 'list', array()), TRUE);
    
  }
  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("agendascategorias", "list");

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

    // set the message
    if($orderSet) {
      // $this->_helper->messenger('ok', "Item atualizado!");
    } else {
      $this->_helper->messenger('error', "Erro!");
    }

    $this->sendSuccess('Item updated!', $this->getUrlTo('agendascategorias', 'list', array()), TRUE);
  }
   public function onlineAction()
  {
    
      // atualiza
      $upd = $this->_model->online($_POST['id'], $_POST['online']);
      // show the json result
      $this->renderJson($upd);
    
    
  }
}