<?php
/**
 * Reserve queue form
 *
 * @author Moises Heberle [moises@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]
 * Sandro Roberto Weisheimer [sandrow@solis.coop.br]
 *
 * @since
 * Class created on 17/11/2008
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2008 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 FrmReserveQueue extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $busLibraryUnit;
    public $busOperationReserve;


    public function __construct()
    {
        $this->MIOLO  = MIOLO::getInstance();
        $this->module = MIOLO::getCurrentModule();
        $this->busLibraryUnit       = $this->MIOLO->getBusiness($this->module, 'BusLibraryUnit');
        $this->busOperationReserve  = $this->MIOLO->getBusiness($this->module, 'BusOperationReserve');
        parent::__construct(_M('Reserve queue', $this->module));
        $this->eventHandler();
    }


    public function createFields()
    {
        //$fields[] = new GnutecaSelection('libraryUnitId', null, _M('Library unit', $this->module), $this->busLibraryUnit->listLibraryUnit());

        //Get date (NOW - RESERVE_QUEUE_DAYS) date
        $mask = GnutecaDate::getDbMask('DATE');
        $date = GnutecaDate::sumDays(GnutecaDate::now($mask), -RESERVE_QUEUE_DAYS, $mask);

        $this->busLibraryUnit->filterOperator = TRUE;
        $fields[] = new GnutecaSelection('libraryUnitId', null, _M('Library unit', $this->module), $this->busLibraryUnit->listLibraryUnit(), null, null, null, TRUE);

        $fields[] = new MCalendarField('date', $date, _M('Date', $this->module), FIELD_DATE_SIZE);
        $validators[] = new MDateDMYValidator('date', null, 'required');
        $fields[] = new MButton('btnOk', _M('Send', $this->module), ':doAction');
        $fields[] = new MDiv('divGrid');

        $this->setFields($fields);
        $this->setValidators($validators);
    }


    public function doAction()
    {
        $data = $this->getData();

        $ok     = $this->busOperationReserve->reorganizeQueueReserve($data->date, $data->libraryUnitId);
        $goto   = $this->MIOLO->getActionURL($this->module, $this->_action);

        if (($data = $this->busOperationReserve->getGridData()) && (count($data) > 0))
        {
            $grid = $this->MIOLO->getUI()->getGrid($this->module, 'GrdReserveQueue');
            $grid->setData($data);
            $this->setResponse($grid, 'divGrid');
        }
        else if (count($this->busOperationReserve->getMessages()) > 0)
        {
            $this->injectContent($this->busOperationReserve->getMessagesTableRaw(), true);
        }
        else
        {
            $this->information(_M('No reserves to organize.', $this->module), $goto);
        }
    }
}
?>
