Esempio n. 1
0
    /**
     * Add a search index
     *
     * @param string $module The module wherin will be searched.
     * @param int $otherId The id of the record.
     * @param  array $fields A key/value pair of fields to index.
     * @param string[optional] $language The frontend language for this entry.
     */
    protected function addSearchIndex($module, $otherId, array $fields, $language)
    {
        // get db
        $db = $this->getDB();
        // validate cache
        if (empty(self::$modules)) {
            // get all modules
            self::$modules = (array) $db->getColumn('SELECT m.name FROM modules AS m');
        }
        // module exists?
        if (!in_array('search', self::$modules)) {
            return;
        }
        // no fields?
        if (empty($fields)) {
            return;
        }
        // insert search index
        foreach ($fields as $field => $value) {
            // reformat value
            $value = strip_tags((string) $value);
            // insert in db
            $db->execute('INSERT INTO search_index (module, other_id, language, field, value, active)
				 VALUES (?, ?, ?, ?, ?, ?)
				 ON DUPLICATE KEY UPDATE value = ?, active = ?', array((string) $module, (int) $otherId, (string) $language, (string) $field, $value, 'Y', $value, 'Y'));
        }
        // invalidate the cache for search
        foreach (SpoonFile::getList(FRONTEND_CACHE_PATH . '/search/') as $file) {
            SpoonFile::delete(FRONTEND_CACHE_PATH . '/search/' . $file);
        }
    }