<?php
/**
 * gtcDictionary business
 *
 * @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 03/12/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 BusinessGnuteca3BusDictionary extends GnutecaBusiness
{
    public $dictionaryId;
    public $dictionaryIdS;
    public $description;
    public $tags;
    public $readOnly;


    public function __construct()
    {
        $table = 'gtcDictionary';
        $pkeys = 'dictionaryId';
        $cols  = 'description,
                  tags,
                  readOnly';
        parent::__construct($table, $pkeys, $cols);
    }


    public function insertDictionary()
    {
        return $this->autoInsert();
    }


    public function updateDictionary()
    {
        return $this->autoUpdate();
    }


    public function deleteDictionary($dictionaryId)
    {   
        $busDictionaryContent = $this->MIOLO->getBusiness($this->module, 'BusDictionaryContent');

        $busDictionaryContent->deleteDictionaryContentFromDictionary($dictionaryId);
        return $this->autoDelete($dictionaryId);
    }


    public function getDictionary($dictionaryId)
    {
        $this->clear();
        return $this->autoGet($dictionaryId);
    }


    public function searchDictionary($object = false)
    {
        unset($this->dictionaryId); //estava ocorrendo bug pos-busca no formulario
        $this->clear();
        $filters = array(
            'dictionaryId'      => 'equals',
            'description'        => 'ilike',
            'tags'              => 'ilike',
            'readOnly'           => 'equals'
        );
        return $this->autoSearch($filters, $object);
    }


    public function getRelatedTerms($tag, $term)
    {
        //FIXME este sql deve ser revisto
    	$tag = explode('.',$tag);
    	$this->fieldId         = $tag[0];
    	$this->subfieldId      = $tag[1];
    	$this->relatedContent  = $term;
    	$return    = $this->searchDictionary(true);
    	$content   = $return[0]->content;
    	if (strtolower($content) != strtolower($term) )
    	{
    	   return $content;
    	}
    }


    public function listDictionary()
    {
        return $this->autoList();
    }


    public function checkExistsDictionaryForTag($tag)
    {
        $this->clear();
        $this->tags = $tag;
        $filters = array
        (
            'tags' => 'like',
        );

        $d = $this->autoSearch($filters, true);

        if(!$d)
        {
            return false;
        }

        return isset($d[0]) ? $d[0] : false;
    }

}
?>
