<?php
/**
 *
 * @author Luiz Gilberto Gregory Filho [luiz@solis.coop.br]
 *
 * @version $Id$
 *
 * \b Maintainers: \n
 * Eduardo Bonfandini [eduardo@solis.coop.br]
 * Jamiel Spezia [jamiel@solis.coop.br]
 * Luiz Gregory Filho [luiz@solis.coop.br]
 * Moises Heberle [moises@solis.coop.br]
 *
 * @since
 * Class created on 03/10/2008
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2007 SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b License: \n
 * Licensed under GPL (for further details read the COPYING file or http://www.gnu.org/copyleft/gpl.html )
 *
 * \b History: \n
 * See history in SVN repository: http://gnuteca.solis.coop.br
 *
 **/
class FrmMyReservesSearch extends GnutecaForm
{
    public
            $function,
            $localFields,
            $UI,
            $grid,
            $business,
            $businessReserve,
            $busOperationReserve,
            $busAuthenticate,
            $busExemplaryControl,
            $busFavorite;

    public $action;


    /**
     * Constructor Method
     **/
    function __construct()
    {
        $this->MIOLO    = MIOLO::getInstance();
        $this->module   = MIOLO::getCurrentModule();
        $this->action   = MIOLO::getCurrentAction();
        $this->function = MIOLO::_REQUEST('function');
        $this->UI       = $this->MIOLO->getUI();
        $this->business            = $this->MIOLO->getBusiness($this->module, 'BusMyReserves');
        $this->businessReserve     = $this->MIOLO->getBusiness($this->module, 'BusReserve');
        $this->busOperationReserve = $this->MIOLO->getBusiness($this->module, 'BusOperationReserve');
        $this->busAuthenticate     = $this->MIOLO->getBusiness($this->module, 'BusAuthenticate');
        $this->busExemplaryControl = $this->MIOLO->getBusiness($this->module, 'BusExemplaryControl');
        $this->busFavorite         = $this->MIOLO->getBusiness($this->module, 'BusFavorite');

        $this->MIOLO->getClass( $this->module, 'GnutecaMaterialDetail' );

        parent::__construct( _M("My reserves", $this->module) );
        parent::setIcon( $this->UI->getImage( $this->module, "myReserves-16x16.png" ) );

        $this->eventHandler();

    }

    /**
     * Create Default Fileds for Search Form
     *
     * @return void
     */
    public function createFields()
    {
        $this->setLabelWidth(FIELD_LABEL_SIZE);
        $this->defaultButton = false;

        switch( $this->function )
        {
            case 'cancel':
                $this->cancel();
                break;
            case 'favorites':
                $this->favorites();
                break;
            case 'showDetail':
                $this->showDetail();
                break;
            default :
                $this->getGrid();
        }
    }


    /**
     * Create a grid list
     *
     * @return void
     */
    public function getGrid()
    {
        //$this->business->setData( $this->getData() );
        $this->grid = $this->UI->getGrid( $this->module, 'GrdMyReserves' );

        // verifica se clicou em buscar para adicionar os dados do banco
        $data = array();
        //if ( MIOLO::_REQUEST("btnSearch") || $this->page->isPostBack())
        {
            $data = $this->business->getMyReserves();
        }

        $this->grid->setData($data);

        //Mensagem a ser mostrada no topo da tela
        $fields[] = new MLabel(_M(LABEL_MY_RESERVES, $this->module));

        $fields[] = new GnutecaUserMenu();
        $fields[] = $this->grid;
        $this->setFields($fields);
    }


    /**
     * Cancela uma determinada reserva
     */
    public function cancel()
    {
        if(!MIOLO::_REQUEST('cancelConfirm'))
        {
            $opts = array
            (
                'cancelConfirm' => 'ok',
                'function'      => 'cancel',
                'reserveId'     => MIOLO::_REQUEST('reserveId')
            );
            $gotoYes = $this->MIOLO ->getActionURL( $this->module, $this->action, null, $opts );
            $gotoNo  = $this->MIOLO ->getActionURL( $this->module, $this->action);
            $msg     = _M('Tem certeza que deseja cancelar a reserva?', $this->module) ;
            $this->question($msg, $gotoYes);
        }
        else
        {
            try
            {
                $opts = array('function'=>'search', 'btnSearch' => 1);
                $goto = $this->MIOLO->getActionURL($this->module, $this->action, null, $opts);

                if ( $this->busOperationReserve->cancelReserve(MIOLO::_REQUEST('reserveId')) )
                {
                    $this->information('Reserva cancelada!', $goto);
                }
                else
                {
                    $this->error( MSG_RECORD_ERROR, $goto );
                }
            }
            catch( EDatabaseException $e )
            {
                $this->error( $e->getMessage() );
            }
        }
    }


    /**
     * Ao clicar na estrela dos favoritos
     *
     * @param object $args stdclass miolo ajax object
     */
    public function favorites()
    {
        if (!($personId = $this->busAuthenticate->getUserCode()) || !($reserveId = MIOLO::_REQUEST('reserveId')))
        {
            return;
        }
        $reserve             = $this->businessReserve->getReserve($reserveId);
        $itemNumber          = $reserve->reserveComposition[0]->itemNumber;
        $controlNumber       = $this->busExemplaryControl->getExemplaryControl($itemNumber)->controlNumber;
        $data                = new StdClass();
        $data->controlNumber = $controlNumber;
        $data->personId      = $personId;
        $data->entraceDate   = GnutecaDate::now( GnutecaDate::getDBMask('TIMESTAMP') ) ;
        $favorite = $this->busFavorite->getFavorite($data->personId, $data->controlNumber);

        if ( $favorite->personId )
        {
            $this->error( _M('This material is already favorited', $this->module), 'javascript:gnuteca.closeAction()', null, false);
        }
        else
        {
            $this->busFavorite->setData($data);
            $this->busFavorite->insertFavorite();
            $this->information( _M('This material has favorited with success.', $this->module), 'javascript:gnuteca.closeAction()', null, false);
        }
    }


    /**
     * Mostra detalhes da obra
     *
     */
    public function showDetail()
    {
    	$reserveId     = MIOLO::_REQUEST('reserveId');
    	$reserve       = $this->businessReserve->getReserve($reserveId);
    	$itemNumber    = $reserve->reserveComposition[0]->itemNumber;
    	$controlNumber = $this->busExemplaryControl->getExemplaryControl($itemNumber)->controlNumber;

    	$fields[] = new GnutecaMaterialDetail($controlNumber, null, ADMINISTRATION_SEARCH_FORMAT_ID);
    	$this->injectContent($fields, false, _M('Detail', $this->module));
    }
}
?>
