/** * Lists all Banner models. * @params string $format, array $arraymap, string $term * @return mixed */ public function actionIndex($format = false, $arraymap = false, $term = false) { $searchModel = new BannerSearch(); $req = Yii::$app->request->queryParams; if ($term) { $req[basename(str_replace("\\", "/", get_class($searchModel)))]["term"] = $term; } $dataProvider = $searchModel->search($req); if ($format == 'json') { $model = []; foreach ($dataProvider->getModels() as $d) { $obj = $d->attributes; if ($arraymap) { $map = explode(",", $arraymap); if (count($map) == 1) { $obj = isset($d[$arraymap]) ? $d[$arraymap] : null; } else { $obj = []; foreach ($map as $a) { $k = explode(":", $a); $v = count($k) > 1 ? $k[1] : $k[0]; $obj[$k[0]] = $v == "Obj" ? json_encode($d->attributes) : (isset($d->{$v}) ? $d->{$v} : null); } } } if ($term) { if (!in_array($obj, $model)) { array_push($model, $obj); } } else { array_push($model, $obj); } } return \yii\helpers\Json::encode($model); } else { $bannerProvider = new ActiveDataProvider(['models' => Banner::find()->where('status = true AND isdel = 0 order by position asc')->all(), 'pagination' => false]); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'bannerProvider' => $bannerProvider]); } }
public static function find() { return parent::find()->where([Banner::tableName() . '.isdel' => 0]); }
public function updatePosition($position) { $models = Banner::find()->where("id != :id", ["id" => $this->id])->orderBy("position")->all(); $pos = 0; $mis = false; $low = false; $up = false; $mod = "-"; $m = false; foreach ($models as $model) { $mis = $mis === false && $model->position != $pos ? $pos : $mis; $pos = $pos + 1; $m = $model; } $alter = $mis === false && $position > $pos ? false : true; if ($m) { $mis = $mis === false ? $m->position + 1 : $mis; } $mod = $position > $mis ? "-" : "+"; $low = $position > $mis ? $mis : $position; $up = $position < $mis ? $mis : $position + 1; $res = true; if ($low !== false && $up !== false && $alter === true) { $res = $this->db->createCommand("UPDATE " . Banner::tableName() . " \n\t\t\t\t\t\tSET position = (position" . $mod . "1) \n\t\t\t\t\t\tWHERE position >= :low AND position < :up AND id != :id")->bindValues(["low" => $low, "up" => $up, "id" => $this->id])->execute(); } return $res; }