<?php
/**
 *
 * PrefixSuffix 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 06/01/2009
 *
 * \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 BusinessGnuteca3BusPrefixSuffix extends GnutecaBusiness
{
   // public $colsNoId;

    public $prefixSuffixId;
	public $fieldId;
    public $subFieldId;
	public $content;
	public $type;

    public $prefixSuffixIdS;
	public $fieldIdS;
    public $subFieldIdS;
	public $contentS;
	public $typeS;


    public function __construct()
    {
        parent::__construct();
        $this->tables   = 'gtcPrefixSuffix';
        $this->colsNoId = 'fieldId,
                           subFieldId,
                           content,
                           type';
        $this->columns  = 'prefixSuffixId, ' . $this->colsNoId;

    }


    /**
     * List all records from the table handled by the class
     *
     * @param: None
     *
     * @returns (array): Return an array with the entire table
     *
     **/
    public function listPrefixSuffix($object=FALSE)
    {
        $this->clear();
        $this->setColumns($this->columns);
        $this->setTables($this->tables);
        $this->setOrderBy('prefixSuffixId');
        $sql = $this->select();
        $rs  = $this->query($sql, $object);
        return $rs;
    }


    /**
     * Return a specific record from the database
     *
     * @param $moduleConfig (integer): Primary key of the record to be retrieved
     * @param $parameter (integer): Primary key of the record to be retrieved
     *
     * @return (object): Return an object of the type handled by the class
     *
     **/
    public function getPrefixSuffix($prefixSuffixId)
    {
        $data = array($prefixSuffixId);

        $this->clear();
        $this->setColumns($this->columns);
        $this->setTables($this->tables);
        $this->setWhere('prefixSuffixId = ?');
        $sql = $this->select($data);
        $rs  = $this->query($sql, $toObject=true);
        $this->setData($rs[0]);

        return $this;
    }


    /**
     * Do a search on the database table handled by the class
     *
     * @param $filters (object): Search filters
     *
     * @return (array): An array containing the search results
     **/
    public function searchPrefixSuffix()
    {
        $this->clear();

        if ($v = $this->prefixSuffixIdS)
        {
            $this->setWhere('prefixSuffixId = ?');
            $data[] = $v;
        }
        if ($v = $this->fieldIdS)
        {
            $this->setWhere('fieldId = ?');
            $data[] = $v;
        }
        if ($v = $this->subFieldIdS)
        {
            $this->setWhere('subFieldId = ?');
            $data[] = $v;
        }
        if ($v = $this->contentS)
        {
            $this->setWhere('lower(content) LIKE lower(?)');
            $data[] = '%' . $v . '%';
        }
        if ($v = $this->typeS)
        {
            $this->setWhere('type = ?');
            $data[] = $v;
        }

        $this->setColumns($this->columns);
        $this->setTables($this->tables);
        $this->setOrderBy('prefixSuffixId');
        $sql = $this->select($data);

        $rs  = $this->query($sql);

        return $rs;
    }


    /**
     * Insert a new record
     *
     * @param $data (object): An object of the type handled by the class
     *
     * @return True if succed, otherwise False
     *
     **/
    public function insertPrefixSuffix()
    {
        $this->clear();
        $this->setColumns($this->colsNoId);
        $this->setTables($this->tables);
        $data= array($this->fieldId,
                     $this->subFieldId,
                     $this->content,
                     $this->type);
        $sql = $this->insert($data);
        $rs  = $this->execute($sql);

        return $rs;
    }


    /**
     * Update data from a specific record
     *
     * @param $data (object): Data which will replace the old record data
     *
     * @return (boolean): True if succeed, otherwise False
     *
     **/
    public function updatePrefixSuffix()
    {
        $data= array($this->fieldId,
                     $this->subFieldId,
                     $this->content,
                     $this->type,
                     $this->prefixSuffixId);
        $this->clear();
        $this->setColumns($this->colsNoId);
        $this->setTables($this->tables);
        $this->setWhere('prefixSuffixId = ?');
        $sql = $this->update($data);
        $rs  = $this->execute($sql);

        return $rs;
    }


    /**
     * Delete a record
     *
     * @param $moduleConfig (string): Primary key for deletion
     * @param $parameter (string): Primary key for deletion
     *
     * @return (boolean): True if succeed, otherwise False
     *
     **/
    public function deletePrefixSuffix($prefixSuffixId)
    {
        $data = array($prefixSuffixId);

        $this->clear();
        $this->setTables($this->tables);
        $this->setWhere('prefixSuffixId = ?');
        $sql = $this->delete($data);
        $rs  = $this->execute($sql);

        return $rs;
    }


    /**
     * Lista os tipo
     *
     * @return array
     */
    public function listTypes()
    {
        $listType = array
        (
            1 => _M('Prefix', $this->module),
            2 => _M('Suffix', $this->module)
        );
        return $listType;
    }



    /**
     * Verifica se exite prefixo ou sufixo para uma determinada tag
     *
     * @param char(5) $tag
     * @return bollean
     */
    public function getPrefixSuffixForTag($tag)
    {
        $this->clear();
        list($this->fieldId, $this->subFieldId) = explode(".",$tag);
        $filters = array
        (
            'fieldId'       => 'like',
            'subFieldId'    => 'like',
        );

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

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



    /**
     * retorna os prefixos de uma determinada tag
     *
     * @param char(5) $tag
     * @return array
     */
    public function getPrefixForTag($tag, $forSelection = false)
    {
        $this->clear();
        list($this->fieldId, $this->subFieldId) = explode(".",$tag);
        $this->type = 1;

        $filters = array
        (
            'fieldId'       => 'equals',
            'subFieldId'    => 'equals',
            'type'          => 'equals'
        );

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

        if($forSelection && $prefix)
        {
            $prefixFor = $prefix;
            $prefix = null;
            foreach ($prefixFor as $values)
            {
                $prefix[$values->prefixSuffixId] = $values->content;
            }
        }

        return $prefix;
    }

    /**
     * retorna os suffixos de uma determinada tag
     *
     * @param char(5) $tag
     * @return array
     */
    public function getSuffixForTag($tag, $forSelection = true)
    {
        $this->clear();
        list($this->fieldId, $this->subFieldId) = explode(".",$tag);
        $this->type = 2;

        $filters = array
        (
            'fieldId'       => 'equals',
            'subFieldId'    => 'equals',
            'type'          => 'equals'
        );

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

        if($forSelection && $suffix)
        {
            $suffixFor = $suffix;
            $suffix = null;
            foreach ($suffixFor as $values)
            {
                $suffix[$values->prefixSuffixId] = $values->content;
            }
        }

        return $suffix;
    }

}
?>
