<?php

class ControllerApi extends Controller{

  private $erro     = array();

  public function __construct($registry) {
    parent::__construct($registry);
    $this->load->type('tipoapi');
  }

  public function setErro($errCode, $errogerado){
    $this->erro['erro'][] = array(
      'errorcode' => $errCode,
      'mensagem'  => $errogerado
    );
  }
  
  protected function estaAutenticado(){
    return isset($this->session->data['api_key']);    
  }  

  protected function validaAutenticacao(){
    if(!$this->estaAutenticado()){
      $this->setErro(Type_ApiReturn::Tp_invalidToken, Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_invalidToken));
      return false;
    }
  }

  protected function getReturnSuccessmethod($codereturn, $messageReturn){
    $retorno['retorno'][] = array(
      'returncode' => $codereturn,
      'mensagem'   => $messageReturn
    );

    return $retorno;
  }

  protected function haserror(){
    return (isset($this->erro['erro'])) && (sizeof($this->erro['erro']) > 0);
  }

  protected function geterror(){
    return $this->erro;
  }

  protected function validFieldValue($tiporeq, $fieldparam){

    if($tiporeq == Type_requisicao::Tp_reqGet){
      if((!isset($this->request->get[$fieldparam])) || ($this->request->get[$fieldparam] == "")){
        $this->setErro(Type_ApiReturn::Tp_InvalidParameter, sprintf(Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_InvalidParameter), $fieldparam));
        return false;
      } else {
        return true;
      }
    } else{
      if((!isset($this->request->post[$fieldparam])) || ($this->request->post[$fieldparam] == "")){
        $this->setErro(Type_ApiReturn::Tp_InvalidParameter, sprintf(Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_InvalidParameter), $fieldparam));
        return false;
      } else {
        return true;
      }
    }

    /*if((!isset($fieldparam)) || ($fieldparam == "")){
      $this->setErro(Type_ApiReturn::Tp_InvalidParameter, sprintf(Type_ApiReturn::getNickFromValue(Type_ApiReturn::Tp_InvalidParameter), $fieldname));
      return false;
    }else{
      return true;
    }*/

  }


}
