Classes

File content/keyword/KeywordsManager.class.php

File content/keyword/KeywordsManager.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 
<?php
/**
 * @package     Content
 * @subpackage  Keyword
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 11 30
 * @since       PHPBoost 4.0 - 2013 08 28
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class KeywordsManager
{
    /**
     * @var string module identifier.
     */
    private $module_id;

    /**
     * @var KeywordsCache The cached data class.
     */
    private $keywords_cache;

    private $db_querier;

    /**
     * @param KeywordsCache $keywords_cache A child object of the class KeywordsCache
     */
    public function __construct(KeywordsCache $keywords_cache)
    {
        $this->module_id = $keywords_cache->get_module_identifier();
        $this->keywords_cache = $keywords_cache;

        $this->db_querier = PersistenceContext::get_querier();
    }

    public function get_form_field($id_in_module, $id, $label, array $field_options = array(), array $constraints = array())
    {
        $field_options['file'] = TPL_PATH_TO_ROOT . '/kernel/framework/ajax/dispatcher.php?url=/tags/';
        return new FormFieldMultipleAutocompleter($id, $label, array_keys($this->get_keywords($id_in_module)), $field_options, $constraints);
    }

    public function put_relations($id_in_module, $keywords)
    {
        $this->delete_relations($id_in_module);

        foreach ($keywords as $keyword_list)
        {
            if (!empty($keyword_list))
            {
                $array_keywords = explode(',', str_replace(array(', ', '; ', ';'), ',', $keyword_list));

                foreach ($array_keywords as $keyword)
                {
                    if (!$this->exists($keyword))
                    {
                        $result = $this->db_querier->insert(DB_TABLE_KEYWORDS, array('name' => TextHelper::htmlspecialchars($keyword), 'rewrited_name' => TextHelper::htmlspecialchars(Url::encode_rewrite($keyword))));
                        $id_keyword = $result->get_last_inserted_id();
                    }
                    else
                    {
                        $id_keyword = $this->db_querier->get_column_value(DB_TABLE_KEYWORDS, 'id', 'WHERE name=:name OR rewrited_name=:rewrited_name', array('name' => TextHelper::htmlspecialchars($keyword), 'rewrited_name' => TextHelper::htmlspecialchars(Url::encode_rewrite($keyword))));
                    }
                    $this->db_querier->insert(DB_TABLE_KEYWORDS_RELATIONS, array('module_id' => $this->module_id, 'id_in_module' => $id_in_module, 'id_keyword' => $id_keyword));
                }
            }
        }
    }

    public function get_keyword($condition, array $parameters = array())
    {
        $row = $this->db_querier->select_single_row(DB_TABLE_KEYWORDS, array('*'), $condition, $parameters);
        $keyword = new Keyword();
        $keyword->set_properties($row);
        return $keyword;
    }

    public function get_keywords($id_in_module)
    {
        return $this->get_keywords_cache()->get_keywords($id_in_module);
    }

    public function delete_relations($id_in_module)
    {
        $this->db_querier->delete(DB_TABLE_KEYWORDS_RELATIONS, 'WHERE module_id = :module_id AND id_in_module=:id_in_module', array('module_id' => $this->module_id, 'id_in_module' => $id_in_module));
    }

    public function delete_module_relations()
    {
        $this->db_querier->delete(DB_TABLE_KEYWORDS_RELATIONS, 'WHERE module_id=:module_id', array('module_id' => $this->module_id));
    }

    private function exists($name)
    {
        return $this->db_querier->row_exists(DB_TABLE_KEYWORDS, 'WHERE name=:name OR rewrited_name=:rewrited_name', array('name' => TextHelper::htmlspecialchars($name), 'rewrited_name' => TextHelper::htmlspecialchars(Url::encode_rewrite($name))));
    }

    public function regenerate_cache()
    {
        $class = get_class($this->get_keywords_cache());
        call_user_func(array($class, 'invalidate'));
    }

    /**
     * @return KeywordsCache
     */
    public function get_keywords_cache() { return $this->keywords_cache; }

    /**
     * @return string module identifier.
     */
    public function get_module_id() { return $this->module_id; }
}
?>