/** * @inheritdoc */ public function getIndexMapping() { $model = $this->getModel(); /** @var \im\base\types\EntityTypesRegister $typesRegister */ $typesRegister = Yii::$app->get('typesRegister'); $entityType = $typesRegister->getEntityType($model); $indexableAttributes = IndexAttribute::findByIndexType($entityType); $searchableAttributes = $this->getSearchableAttributes(); $attributes = []; foreach ($indexableAttributes as $indexableAttribute) { foreach ($searchableAttributes as $searchableAttribute) { if ($indexableAttribute->name === $searchableAttribute->name) { $name = $searchableAttribute->name; if ($indexableAttribute->type) { $searchableAttribute->type = $indexableAttribute->type; } if ($indexableAttribute->index_name) { $searchableAttribute->name = $indexableAttribute->index_name; } $attributes[$name] = $searchableAttribute; } } } return $attributes; }
/** * Returns index attributes by type. * * @param string $type * @return IndexAttribute[] */ public function getIndexAttributes($type) { $searchableAttributes = $this->getSearchableAttributes($type); $indexAttributes = IndexAttribute::findByIndexType($type); $attributes = []; foreach ($searchableAttributes as $searchableAttribute) { /** @var IndexAttribute $indexAttribute */ $indexAttribute = null; foreach ($indexAttributes as $attribute) { if ($attribute->name === $searchableAttribute->name) { $indexAttribute = $attribute; break; } } if ($indexAttribute) { $indexAttribute->indexable = true; $indexAttribute->label = $searchableAttribute->label; } else { $indexAttribute = new IndexAttribute(['index_type' => $type, 'name' => $searchableAttribute->name, 'label' => $searchableAttribute->label]); } $attributes[] = $indexAttribute; } return $attributes; }
/** * Saves indexable attributes from data array. * * @param array $data * @return bool whether the attributes were saved */ public static function saveFromData($data) { list($indexableCondition, $deleteCondition) = self::getConditions($data); $indexableAttributes = IndexAttribute::find()->where($indexableCondition)->all(); foreach ($data as $item) { if ($item['indexable']) { $indexable = false; foreach ($indexableAttributes as $indexableItem) { if ($item['index_type'] === $indexableItem['index_type'] && $item['name'] === $indexableItem['name']) { $indexableItem->load($item); $indexable = true; break; } } if (!$indexable) { $indexableAttributes[] = new IndexAttribute($item); } } } $saved = true; foreach ($indexableAttributes as $item) { if (!$item->save()) { $saved = false; } } if ($saved) { IndexAttribute::deleteAll($deleteCondition); } return $saved; }
/** * List index attributes. * * @param integer $id * @return mixed */ public function actionAttributes($id) { if ($data = Yii::$app->request->post('IndexAttribute', [])) { IndexAttribute::saveFromData($data); } /** @var \im\search\components\SearchManager $searchManager */ $searchManager = Yii::$app->get('searchManager'); $model = $this->findModel($id); $attributes = $searchManager->getIndexAttributes($model->type); $dataProvider = new ArrayDataProvider(['allModels' => $attributes, 'sort' => ['attributes' => ['name', 'label', 'index_name', 'indexable']]]); return $this->render('attributes', ['model' => $model, 'dataProvider' => $dataProvider]); }