Backend\Modules\Search\Engine\Model::saveIndex PHP Method

saveIndex() public static method

Edit an index
public static saveIndex ( string $module, integer $otherId, array $fields, string $language = null )
$module string The module wherein will be searched.
$otherId integer The id of the record.
$fields array A key/value pair of fields to index.
$language string The frontend language for this entry.
    public static function saveIndex($module, $otherId, array $fields, $language = null)
    {
        // module exists?
        if (!in_array('Search', BackendModel::getModules())) {
            return;
        }
        // no fields?
        if (empty($fields)) {
            return;
        }
        // set language
        if (!$language) {
            $language = BL::getWorkingLanguage();
        }
        // get db
        $db = BackendModel::getContainer()->get('database');
        // insert search index
        foreach ($fields as $field => $value) {
            // reformat value
            $value = strip_tags((string) $value);
            // update search index
            $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
        self::invalidateCache();
    }

Usage Example

Example #1
0
 /**
  * Validate the form
  */
 protected function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->frm->cleanupFields();
         // validation
         $fields = $this->frm->getFields();
         // required fields
         $fields['category_id']->isFilled(BL::err('FieldIsRequired'));
         if ($fields['category_id']->getValue() == 'no_category') {
             $fields['category_id']->addError(BL::err('FieldIsRequired'));
         }
         // validate meta
         $this->meta->validate();
         if ($this->frm->isCorrect()) {
             // build the item
             $item['language'] = BL::getWorkingLanguage();
             $item['price'] = $fields['price']->getValue();
             $item['summary'] = $fields['summary_nl']->getValue();
             $item['text'] = $fields['text_nl']->getValue();
             $item['allow_comments'] = $fields['allow_comments']->getChecked() ? 'Y' : 'N';
             $item['num_comments'] = 0;
             $item['sequence'] = BackendCatalogModel::getMaximumSequence() + 1;
             $item['category_id'] = $fields['category_id']->getValue();
             $item['brand_id'] = $fields['brand_id']->getValue();
             $item['meta_id'] = $this->meta->save();
             $item['ballcolor'] = $fields['ballcolor']->getValue();
             $item['frontpage'] = $fields['frontpage']->getChecked();
             $item['contact'] = $fields['contact']->getChecked();
             // insert it
             $item['id'] = BackendCatalogModel::insert($item);
             //--Add the languages
             foreach ((array) BackendModel::get('fork.settings')->get('Core', 'languages') as $key => $language) {
                 $itemLanguage = array();
                 $itemLanguage['id'] = $item['id'];
                 $itemLanguage['language'] = $language;
                 $itemLanguage['title'] = $this->frm->getField('title_' . $language)->getValue();
                 $itemLanguage['text'] = $this->frm->getField('text_' . $language)->getValue();
                 $itemLanguage['summary'] = $this->frm->getField('summary_' . $language)->getValue();
                 $itemLanguage['url'] = BackendCatalogModel::getURLLanguage($this->frm->getField('title_' . $language)->getValue(), null, $language);
                 $itemLanguage['balltext'] = $this->frm->getField('balltext_' . $language)->getValue();
                 BackendCatalogModel::insertLanguage($itemLanguage);
             }
             $specificationArray = array();
             // loop trough specifications and insert values
             foreach ($this->specifications as $specification) {
                 // build the specification
                 $specificationArray['product_id'] = $item['id'];
                 $specificationArray['specification_id'] = $specification['id'];
                 foreach ((array) BackendModel::get('fork.settings')->get('Core', 'languages') as $key => $language) {
                     $field = 'specification' . $specification['id'] . '_' . $language;
                     // check if there is an value
                     if ($fields[$field]->getValue() != null) {
                         $specificationArray['value'] = $fields[$field]->getValue();
                         $specificationArray['language'] = $language;
                         // insert specification with product id and value
                         BackendCatalogModel::insertSpecificationValue($specificationArray);
                     }
                 }
             }
             // save the tags
             BackendTagsModel::saveTags($item['id'], $fields['tags']->getValue(), $this->URL->getModule());
             // save the related products
             BackendCatalogModel::saveRelatedProducts($item['id'], $this->frm->getField('related_products')->getValue());
             // add search index
             BackendSearchModel::saveIndex($this->getModule(), $item['id'], array('title' => $this->frm->getField('title_nl')->getValue(), 'summary' => $this->frm->getField('summary_nl')->getValue(), 'text' => $this->frm->getField('text_nl')->getValue()));
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add', $item);
             // redirect page
             $this->redirect(BackendModel::createURLForAction('index') . '&report=added&highlight=row-' . $item['id']);
         }
     }
 }
All Usage Examples Of Backend\Modules\Search\Engine\Model::saveIndex