private function order($id = null, $ordering = null, $direction = null) { if ($id != null || $ordering != null || $direction != null) { $sess = Yii::$app->session->get('sessNewsletter'); if ($direction == "up") { $newSortOrder = $ordering - 1; } else { if ($direction == "down") { $newSortOrder = $ordering + 1; } } $parent = Newsletter::findOne($id); $where = array(); $where[] = "ordering = '{$newSortOrder}'"; $connection = Yii::$app->db; $sql = "SELECT id FROM tbl_newsletter " . (count($where) ? "\n WHERE " . implode(' AND ', $where) : ''); $command = $connection->createCommand($sql); $reader = $command->query(); foreach ($reader as $row) { $otherId = $row["id"]; } $where = array(); $where[] = "id = '{$id}'"; $sql = 'UPDATE tbl_newsletter SET ordering = "' . $newSortOrder . '" ' . (count($where) ? "\n WHERE " . implode(' AND ', $where) : ''); $command = $connection->createCommand($sql); $command->execute(); if ($reader->getRowCount() > 0) { $where = array(); $where[] = "id = '{$otherId}'"; $sql = 'UPDATE tbl_newsletter SET ordering = "' . $ordering . '"' . (count($where) ? "\n WHERE " . implode(' AND ', $where) : ''); $command = $connection->createCommand($sql); $command->execute(); } return $this->redirect(['index', 'page' => $_REQUEST['page']]); } }
/** * Finds the Newsletter model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Newsletter the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Newsletter::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }