public function actionEditable() { $name = yii::$app->request->post('name'); $value = yii::$app->request->post('value'); $pk = unserialize(base64_decode(yii::$app->request->post('pk'))); Filter::saveEdit($pk, $name, $value); }
public function search($params) { $query = Filter::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => new \yii\data\Sort(['attributes' => ['name', 'type', 'is_filter']])]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'type' => $this->type, 'is_filter' => $this->is_filter]); $query->andFilterWhere(['like', 'name', $this->name]); $query->andFilterWhere(['like', 'slug', $this->slug]); return $dataProvider; }
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 function getFilters() { $model = $this->owner; $return = []; $filters = Filter::find()->all(); foreach ($filters as $filter) { $field = $filter->relation_field_name; $show = false; if (empty($filter->relation_field_value)) { $show = true; } else { foreach ($filter->relation_field_value as $rfv) { if ($model->{$field} == $rfv) { $show = true; } } } if ($show == true) { $return[] = $filter; } } return $return; }
public function getFilters() { return $this->hasOne(Filter::className(), ['id' => 'filter_id']); }
public function run() { $params = ['is_filter' => 'yes']; if ($this->filterId) { $params['id'] = $this->filterId; } $filters = Filter::find()->orderBy('sort DESC')->andWhere($params)->all(); $return = []; foreach ($filters as $filter) { if (in_array($this->itemId, $filter->selected)) { $block = ''; $title = Html::tag('p', $filter->name, ['class' => 'heading']); if ($this->findModel) { $variants = $filter->getVariantsByFindModel($this->findModel)->all(); } else { $variants = $filter->variants; } if ($filter->type == 'range') { $max = 0; $min = 0; foreach ($variants as $variant) { if ($max < $variant->numeric_value) { $max = $variant->numeric_value; } if ($min > $variant->numeric_value) { $min = $variant->numeric_value; } } $fieldName = $this->fieldName . '[' . $filter->id . ']'; $from = $min; $to = $max; $value = yii::$app->request->get($this->fieldName)[$filter->id]; if ($value) { $values = explode(';', $value); $from = $values[0]; $to = $values[1]; } if (!empty($variants)) { $step = round($max / count($variants)); } else { $step = 1; } $block = IonSlider::widget(['name' => $fieldName, 'value' => $value, 'type' => "double", 'pluginOptions' => ['drag_interval' => true, 'grid' => true, 'min' => $min, 'max' => $max, 'from' => $from, 'to' => $to, 'step' => $step]]); } elseif ($filter->type == 'select') { $fieldName = $this->fieldName . '[' . $filter->id . ']'; $value = yii::$app->request->get($this->fieldName)[$filter->id]; $variantsListWithNull = ['' => '-']; $variantsList = ArrayHelper::map($variants, 'id', 'value'); foreach ($variantsList as $id => $value) { $variantsListWithNull[$id] = $value; } $block = Html::dropDownList($fieldName, $value, $variantsListWithNull, ['class' => 'form-control']); } else { foreach ($variants as $variant) { $checked = false; if ($filterData = yii::$app->request->get('filter')) { if (isset($filterData[$filter->id]) && isset($filterData[$filter->id][$variant->id]) | $filterData[$filter->id] == $variant->id) { $checked = true; } } if (!in_array($filter->type, array('radio', 'checkbox', 'range'))) { $filter->type = 'checkbox'; } if ($filter->type == 'radio') { $fieldName = $this->fieldName . '[' . $filter->id . ']'; } else { $fieldName = $this->fieldName . '[' . $filter->id . '][' . $variant->id . ']'; } $field = Html::input($filter->type, $fieldName, $variant->id, ['checked' => $checked, 'data-item-css-class' => $this->itemCssClass, 'id' => "variant{$variant->id}"]); $field .= Html::label($variant->value, "variant{$variant->id}"); $block .= Html::tag('div', $field); } } if (!empty($variants)) { $return[] = Html::tag('div', $title . $block, ['class' => $this->blockCssClass]); } } } if ($return) { $return[] = Html::input('submit', '', $this->submitButtonValue, ['class' => 'btn btn-submit']); return Html::tag('form', implode('', $return), ['data-resulthtmlselector' => $this->resultHtmlSelector, 'name' => 'pistol88-filter', 'action' => '', 'class' => 'pistol88-filter']); } return null; }
public static function saveEdit($id, $name, $value) { $setting = Filter::findOne($id); $setting->{$name} = $value; $setting->save(); }