<?php

class Cms_Cms_UserController extends Pampa_Controller_Action_Cms {

  /**
   * @var Model_Cms_Users
   */
  private $modelCmsUsers;

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

    parent::init();
  }

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

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

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

  public function listAction()
  {
    if($this->view->loggedUser()->usergroup_id <= 2)
    {

      // 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->modelCmsUsers->adminList($filters);
    }
    else
    {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo("index");
    }
  }

  public function newAction() {
    if($this->view->loggedUser()->usergroup_id <= 2)
    {

      // create the blank row
      $this->view->user = $this->modelCmsUsers->createRow();

      // the usergroup list
      $modelUserGropus = new Model_Cms_UsersGroups();
      $this->view->usergroups = $modelUserGropus->fetchForSelect("usergroup");
    }
    else
    {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo("index");
    }
  }

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

      // add the row
      $add = $this->modelCmsUsers->add($_POST['user']);

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

  public function editAction()
  {
    $user_id = $this->_getParam("id");
    if($this->view->loggedUser()->usergroup_id <= 2 || $this->view->loggedUser()->id == $user_id)
    {
      // user id

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

      // the usergroup list
      $modelUserGropus = new Model_Cms_UsersGroups();
      $this->view->usergroups = $modelUserGropus->fetchForSelect("usergroup");
    }
    else
    {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo("index");
    }
  }

  public function updateAction() {
    if(!$this->_request->isPost()) {
      $this->_redirector->gotoSimple("list");
    } else {
      if($this->view->loggedUser()->usergroup_id <= 2 || $this->view->loggedUser()->id == $_POST['user']['id'])
      {
        // update the row
        $update = $this->modelCmsUsers->update($_POST['user']);

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

  public function deleteAction()
  {
    if($this->view->loggedUser()->usergroup_id <= 2)
    {
      $user_id = $this->_getParam("id");

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

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

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

      $this->_redirector->gotoSimple("list");
    }
    else
    {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo("index");
    }
  }

  public function onlineAction()
  {
    if($this->view->loggedUser()->usergroup_id <= 2)
    {
      // atualiza
      $upd = $this->modelCmsUsers->online($_POST['id'], $_POST['online']);
      // show the json result
      $this->renderJson($upd);
    }
    else
    {
      $this->_helper->messenger('error', 'Invalid resource');
      $this->redirectTo("index");
    }
  }
}