<?php
/**
 * DictionaryContent 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]
 *
 * @since
 * Class created on 19/01/2009
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2009 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 BusinessGnuteca3BusDictionaryContent extends GnutecaBusiness
{
    public $dictionaryContentId;
    public $dictionaryContentIdS;
    public $dictionaryId;
    public $dictionaryContent;
	public $searchFormatAccess;

    public $busDictionaryRelatedContent;
    public $dictionaryRelatedContent;


    public function __construct()
    {
        $table = 'gtcDictionaryContent';
        $pkeys = 'dictionaryContentId';
        $cols  = 'dictionaryId,
                  dictionaryContent';
        parent::__construct($table, $pkeys, $cols);
        $this->busDictionaryRelatedContent = $this->MIOLO->getBusiness($this->module, 'BusDictionaryRelatedContent');
    }


    public function insertDictionaryContent()
    {
    	$this->dictionaryContentId = $this->db->getNewId('seq_dictionaryContentId');
        $insert = $this->autoInsert();
        if ($this->dictionaryRelatedContent)
        {
            foreach ($this->dictionaryRelatedContent as $v)
            {
                $this->busDictionaryRelatedContent->setData($v);
                $this->busDictionaryRelatedContent->dictionaryContentId = $this->dictionaryContentId;
                $this->busDictionaryRelatedContent->insertDictionaryRelatedContent();
            }
        }

        $this->createJavascriptFileFunction($this->dictionaryId);

        return $insert;
    }


    public function updateDictionaryContent()
    {
        if ($this->dictionaryRelatedContent)
        {
            foreach ($this->dictionaryRelatedContent as $v)
            {
                $this->busDictionaryRelatedContent->setData($v);
                $this->busDictionaryRelatedContent->dictionaryContentId        = $this->dictionaryContentId;
                $this->busDictionaryRelatedContent->dictionaryRelatedContentId = $v->dictionaryRelatedContentId;
                $this->busDictionaryRelatedContent->updateDictionaryRelatedContent();
            }
        }

        $this->createJavascriptFileFunction($this->dictionaryId);

        return $this->autoUpdate();
    }


    public function deleteDictionaryContent($dictionaryContentId)
    {
    	$this->busDictionaryRelatedContent->dictionaryContentId = $dictionaryContentId;
    	$search = $this->busDictionaryRelatedContent->searchDictionaryRelatedContent(TRUE);
    	if ($search)
    	{
    		foreach ($search as $v)
    		{
    			$this->busDictionaryRelatedContent->deleteDictionaryRelatedContent($v->dictionaryRelatedContentId);
    		}
    	}

    	$c = $this->getDictionaryContent($dictionaryContentId);

    	$this->createJavascriptFileFunction($this->dictionaryId);

        return $this->autoDelete($dictionaryContentId);
    }

    public function deleteDictionaryContentFromDictionary($dictionaryId)
    {
        $this->clear();

        $this->setColumns   ('dictionaryContentId');
        $this->setTables    ('gtcDictionaryContent');
        $this->setWhere     ('dictionaryId = ?');

        $sql = $this->select(array($dictionaryId));
        $result = $this->query($sql, true);

        if (!$result)
        {
            return false;
        }

        foreach ($result as $r)
        {
            $this->deleteDictionaryContent($r->dictionaryContentId);
        }

        return true;
    }

    public function getDictionaryContent($dictionaryContentId)
    {
        $this->clear();
        $data = $this->autoGet($dictionaryContentId);
        $this->setData($data);
        $this->busDictionaryRelatedContent->dictionaryContentId = $dictionaryContentId;
        $this->dictionaryRelatedContent = $this->busDictionaryRelatedContent->searchDictionaryRelatedContent(TRUE);
        return $data;
    }


    public function searchDictionaryContent($toObject = false)
    {
        $this->clear();
        if ($this->dictionaryContentIdS)
        {
            $this->setWhere('A.dictionaryContentId = ?');
        	$args[] = $this->dictionaryContentIdS;
        }
        if ($this->dictionaryId)
        {
        	$this->setWhere('A.dictionaryId = ?');
        	$args[] = $this->dictionaryId;
        }
        if ($this->dictionaryContent)
        {
        	$this->setWhere('lower(A.dictionaryContent) LIKE lower(?)');
        	$args[] = '%' . $this->dictionaryContent . '%';
        }
        $this->setTables('gtcDictionaryContent  A
                LEFT JOIN gtcDictionary         B
                       ON (A.dictionaryId = B.dictionaryId)');
        $this->setOrderBy('dictionaryContentId');
        $this->setColumns('A.dictionaryContentId,
                           B.description,
                           A.dictionaryContent');
        $sql = $this->select($args);

        return $this->query($sql, $toObject);
    }



    public function getAllDictionaryContent($dictionaryId)
    {
        $this->clear();

        $this->setColumns   ('dictionaryContent');
        $this->setTables    ('gtcDictionaryContent');
        $this->setWhere     ('dictionaryId = ?');
        $this->setOrderBy   ('dictionaryContent');

        $sql = $this->select(array($dictionaryId));
        return $this->query($sql, true);
    }


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


    function clean()
    {
        $this->dictionaryContentId  =
        $this->dictionaryId         =
        $this->dictionaryContent              = null;
    }


    /**
     * Enter description here...
     *
     * @param unknown_type $dictionaryId
     */
    function createJavascriptFileFunction($dictionaryId = null)
    {
        if(!strlen($dictionaryId))
        {
            return false;
        }

        $dictionaryContent = $this->getAllDictionaryContent($dictionaryId);

        if(!$content)

        {
            return false;
        }


        $dictionaryScriptPath   = str_replace("db", "html/scripts/dictionary", str_replace("\\", "/", dirname(__FILE__)));
        $fileName               = "{$dictionaryScriptPath}/gnutecaDictionary{$dictionaryId}.js";

        $arrayExists[$dictionaryId] = array();

        //$createFile = file_put_contents($fileName, $this->getFunctionBegin($dictionaryId));
        $fr = fopen($fileName, "w");

        if(!$fr)
        {
            return false;
        }

        fwrite($fr, $this->getFunctionBegin($dictionaryId));

        foreach ($dictionaryContent as $values)
        {
            $values->dictionaryContent = addslashes($values->dictionaryContent);

            if(!eregi("^[a-z0-9]{1}$", $values->dictionaryContent[0]))
            {
                continue;
            }

            $primeiraLetraUpper = strtoupper($values->dictionaryContent[0]);
            $str = "Content";
            $str.="['{$primeiraLetraUpper}']";

            $newArray = "";
            if(!in_array($str, $arrayExists[$dictionaryId]))
            {
                $arrayExists[$dictionaryId][] = $str;
                $newArray.= "\t$str = new Array();\n\tvar __{$primeiraLetraUpper}__ = 0;\n";
            }

            $values->dictionaryContent = htmlentities($values->dictionaryContent);
            $str.= "[__{$primeiraLetraUpper}__++] = '$values->dictionaryContent'";

            //file_put_contents($fileName, "{$newArray}\t{$str};\n", FILE_APPEND);
            fwrite($fr, "{$newArray}\t{$str};\n");
        }

        //file_put_contents($fileName, $this->getFunctionFinal(), FILE_APPEND);
        fwrite($fr, $this->getFunctionFinal());
        fclose($fr);
    }


    /**
     * Enter description here...
     *
     * @param unknown_type $dictionaryId
     * @return unknown
     */
    function getFunctionBegin($dictionaryId)
    {
        return "
        dictionary{$dictionaryId} = function(text, fieldId, divDictionary)
         {
            var Content = new Array();
        ";
    }


    /**
     * Enter description here...
     *
     */
    function getFunctionFinal()
    {
        return "

            var l = text[0].toUpperCase();

        	if(!is_array(Content[l]))
        	{
                return '';
        	}

        	var contentReturn = '';

        	var divControl = 0;

        	for(x = 0; x < Content[l].length; x++ )
        	{
                textAux = Content[l][x].toUpperCase();

        	    if(textAux.indexOf(text.toUpperCase()) > -1)
        	    {
        	        ++divControl;
        	        //onmouseover=\"selectOption(\''+ (divControl) +'\')\"  onmouseout=\"deselectOption(\''+ (divControl) +'\', true)\"

        	        displayText = Content[l][x];

                    contentReturn+= '<div id=\"dictionaryOption_'+ (divControl) +'\" title=\"'+ Content[l][x] +'\" style=\"width:100%;\" >'+ displayText +'</div>';
        	    }

        	    if(divControl > 100)
        	    {
        	       break;
        	    }
        	}

            return contentReturn;
        }
        ";
    }

} // final da classe
?>
