<?php
/**
 * Notify acquisition 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]
 *
 * @since
 * Class created on 18/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 FrmNotifyAcquisition extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $busLibraryUnit;
    public $busOperationMaterial;


    public function __construct()
    {
        $this->MIOLO  = MIOLO::getInstance();
        $this->module = MIOLO::getCurrentModule();
        $this->busLibraryUnit       = $this->MIOLO->getBusiness($this->module, 'BusLibraryUnit');
        $this->busOperationMaterial = $this->MIOLO->getBusiness($this->module, 'BusOperationMaterial');
        parent::__construct(_M('Notify acquisition', $this->module));
        $this->eventHandler();
    }


    public function createFields()
    {
        $lblInfo = new MLabel(_M('Communicates by email users about new material on the library, according to their areas of interest', $this->module));
        $lblInfo->setBold(true);
        $fields[] = $lblInfo;

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

        $fields[] = new MCalendarField('endDate', GnutecaDate::now(GnutecaDate::getDbMask('DATE')), _M('Data final', $this->module), FIELD_DATE_SIZE);
        $validators[] = new MDateDMYValidator('endDate', null, 'required');
        $fields[] = new MButton('btnOk', _M('Send', $this->module), ':doAction');
        $fields[] = new MDiv('divGrid');

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


    public function doAction()
    {
    	$data = $this->getData();
        //valida o formulário
    	$this->gValidator->errors = $errors;
        $errors = $this->gValidator->validate( $data );

        if ( $errors && is_array( $errors ) )
        {
            $this->error(implode('<br>', $errors), 'gnuteca.closeAction()');
            return false;
        }
        
        $ok     = $this->busOperationMaterial->notifyAcquisition($data->endDate, $data->libraryUnitId);
        $goto   = $this->MIOLO->getActionURL($this->module, $this->_action);

        if (($data = $this->busOperationMaterial->getGridData()) && (count($data) > 0))
        {
            $grid = $this->MIOLO->getUI()->getGrid($this->module, 'GrdOperationMaterial');
            $grid->setData($data);
            $this->setResponse($grid, 'divGrid');
        }
        else if (count($this->busOperationMaterial->getMessages()) > 0)
        {
            $this->injectContent($this->busOperationMaterial->getMessagesTableRaw(), true);
        }
        else
        {
            $this->information(_M('There are no acquisitions to notify', $this->module), $goto);
        }
    }
}
?>
