protected function findModel($id) { if (($model = Filter::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function getOption($code, $full = false) { if (isset($this->filterOptions[$code]) && is_array($this->filterOptions[$code])) { return $this->filterOptions[$code]; } if ($filter = Filter::findOne(['slug' => $code])) { $values = FilterValue::findAll(['filter_id' => $filter->id, 'item_id' => $this->owner->id]); foreach ($values as $value) { if ($full) { $this->filterOptions[$filter->slug][] = $value->variant; } else { $this->filterOptions[$filter->slug][] = $value->variant->value; } } } if (!isset($this->filterOptions[$code])) { return []; } return $this->filterOptions[$code]; }
public function actionUpdate() { $post = yii::$app->request->post('FilterValue'); $model = FilterValue::findOne(['item_id' => $post['item_id'], 'filter_id' => $post['filter_id']]); if (!$model) { $model = new FilterValue(); } else { $filter = Filter::findOne($model->filter_id); if ($filter->type == 'radio') { FilterValue::deleteAll(['item_id' => $post['item_id'], 'filter_id' => $post['filter_id']]); $model = new FilterValue(); } } $json = []; if ($model->load(yii::$app->request->post()) && $model->save()) { $json['result'] = 'success'; } else { $json['result'] = 'fail'; } return json_encode($json); }
public function filtered($filterIds = false, $mode = 0) { if (!$filterIds) { $filterIds = Yii::$app->request->get($this->fieldName); } if (empty($filterIds)) { return $this->owner; } $condition = ['OR']; $variantCount = 0; $filterCount = count($filterIds); foreach ($filterIds as $filterId => $value) { $filter = Filter::findOne($filterId); if ($filter->type == 'range' && is_string($value)) { $value = explode(';', $value); if ($value[0] != $value[1]) { $variants = FilterVariant::find()->where('filter_id = :filterId AND (numeric_value >= :min AND numeric_value <= :max)', [':filterId' => $filterId, ':min' => $value[0], ':max' => $value[1]])->select('id')->all(); } else { $variants = FilterVariant::find()->where('filter_id = :filterId AND numeric_value = :value', [':filterId' => $filterId, ':value' => $value[0]])->select('id')->all(); } $variantIds = ArrayHelper::map($variants, 'id', 'id'); } else { $variantIds = $value; } $condition[] = ['filter_id' => $filterId, 'variant_id' => $variantIds]; if ($mode == 1) { $variantCount += count($variantIds); } else { $variantCount++; } } $filtered = FilterValue::find()->select('item_id')->groupBy('item_id')->andHaving("COUNT(DISTINCT `filter_id`) = {$variantCount}")->andFilterWhere($condition); if ($filtered->count() > 0) { $this->owner->andWhere(['id' => $filtered]); } else { $this->owner->andWhere(['id' => 0]); } return $this->owner; }
public static function saveEdit($id, $name, $value) { $setting = Filter::findOne($id); $setting->{$name} = $value; $setting->save(); }