123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace manager\controllers;
- use Yii;
- use \app\models\base\Story;
- use \app\models\base\NewsTopic;
- use yii\web\UploadedFile;
- class NewsController extends BaseController
- {
- // ** Статьи (новости) **
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- ];
- }
- public function actionIndex()
- {
- return $this->render("/default/news",[]);
- }
- public function actionList()
- {
- return $this->render('/default/newsList', []);
- }
- public function actionInactive()
- {
- return $this->render('/default/newsInactive', []);
- }
- public function actionNew()
- {
- return $this->render('/default/newsNew', []);
- }
- // ** Рубрики **
- public function actionTopiclist()
- {
- return $this->render('/default/topicList', []);
- }
- public function actionTopicall()
- {
- return $this->render('/default/topicAll', []);
- }
- public function actionTopicdel($id)
- {
- $model = $this->findTopicModel($id);
- if($model) $model->del($id);
- return $this->render('/default/topicList', []);
- }
- public function actionTopicshow($id)
- {
- $model = $this->findTopicModel($id);
- if($model) $model->Show($id);
- return $this->render('/default/topicList', []);
- }
- public function actionTopicactive($id)
- {
- $model = $this->findTopicModel($id);
- if($model) $model->Active($id);
- return $this->render('/default/topicList', []);
- }
- public function actionTopicsort()
- {
- if (isset($_POST['item']) && is_array($_POST['item'])) {
- $i = 0;
- $a = array();
- // beginTransaction(); было бы неплохо обернуть в транзакцию но хз как это сделать
- foreach ($_POST['item'] as $item) {
- $model = $this->findTopicModel($item);
- $model->order = $i;
- $a[$model->id] = $i;
- $model->save(true, ['order']);
- // print_a($model->errors);
- $i++;
- }
- return json_encode(['status'=>'ok','data'=>$a]);
- }
- return json_encode(['status'=>'err']);
- }
- public function actionTopiccreate()
- {
- $model = new NewsTopic();
- if (Yii::$app->request->isPost && Yii::$app->request->post()){
- if($model->load($_POST) && $model->save()){
- if (is_uploaded_file($_FILES['NewsTopic']['tmp_name']['photo'])) {
- // $id = -1; //test
- $model->saveImg($model->id, $_FILES['NewsTopic']['tmp_name']['photo'], $_FILES['NewsTopic']['type']['photo']);
- }
- }else{
- print_a($model->errors);
- }
- return $this->redirect('topiclist');
- }
- return $this->render('/default/topicCreate', ['model'=>$model]);
- }
- public function actionTopicupdate($id)
- {
- $model = $this->findTopicModel($id);
- if (Yii::$app->request->isPost && Yii::$app->request->post()){
- if($model->load($_POST) && $model->save()){
- // обновление фото
- if (is_uploaded_file($_FILES['NewsTopic']['tmp_name']['photo'])) {
- $model->saveImg($model->id, $_FILES['NewsTopic']['tmp_name']['photo'], $_FILES['NewsTopic']['type']['photo']);
- }
- }else{
- print_a($model->errors);
- }
- return $this->redirect('topiclist');
- }
- return $this->render('/default/topicUpdate', ['model'=>$model]);
- }
- // ** Сюжеты **
- public function actionStorylist()
- {
- return $this->render('/default/storyList', []);
- }
- public function actionStorydel($id)
- {
- $model = $this->findStoryModel($id);
- if($model) $model->del($id);
- return $this->render('/default/storyList', []);
- }
- public function actionStoryshow($id)
- {
- $model = $this->findStoryModel($id);
- if($model) $model->Show($id);
- return $this->render('/default/storyList', []);
- }
- public function actionAjaxstory()
- {
- if (Yii::$app->request->isGet && $get = Yii::$app->request->get('q')){
- $model = new Story();
- $items = $model->search($get, 10);
- $res = array();
- if( $items && is_array( $items ) ){
- foreach( $items as $item ){
- $res[] = array( 'id'=>$item['id']*1, 'label'=>$item['title'] );
- }
- return json_encode( ['status'=>'ok', 'item'=>$res] );
- }
- }
- return json_encode( ['status'=>'err'] );
- }
- public function actionStoryactive($id)
- {
- $model = $this->findStoryModel($id);
- if($model) $model->Active($id);
- return $this->render('/default/storyList', []);
- }
- public function actionStorycreate()
- {
- $model = new Story();
- if (Yii::$app->request->isPost && Yii::$app->request->post()){
- if($model->load($_POST) && $model->save()){
- if (is_uploaded_file($_FILES['Story']['tmp_name']['photo'])) {
- // $id = -1; //test
- $model->saveImg($model->id, $_FILES['Story']['tmp_name']['photo'], $_FILES['Story']['type']['photo']);
- }
- }else{
- print_a($model->errors);
- }
- return $this->redirect('storylist');
- }
- return $this->render('/default/storyCreate', ['model'=>$model]);
- }
- public function actionStoryupdate($id)
- {
- $model = $this->findStoryModel($id);
- if (Yii::$app->request->isPost && Yii::$app->request->post()){
- if($model->load($_POST) && $model->save()){
- // обновление фото
- if (is_uploaded_file($_FILES['Story']['tmp_name']['photo'])) {
- $model->saveImg($model->id, $_FILES['Story']['tmp_name']['photo'], $_FILES['Story']['type']['photo']);
- }
- }else{
- print_a($model->errors);
- }
- return $this->redirect('storylist');
- }
- return $this->render('/default/storyUpdate', ['model'=>$model]);
- }
- // ** отладка **
- public function actionTest()
- {
- return $this->render('/default/test', []);
- }
- /**
- * Найти сюжет.
- *
- * @param $id
- * @return Story|null
- * @throws NotFoundHttpException
- */
- protected function findStoryModel($id)
- {
- if (($model = Story::findOne($id)) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('Ой! сюжет не найдена.');
- return false;
- }
- /**
- * Найти Ркбрику.
- *
- * @param $id
- * @return Story|null
- * @throws NotFoundHttpException
- */
- protected function findTopicModel($id)
- {
- if (($model = NewsTopic::findOne($id)) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('Ой! рубрика не найдена.');
- return false;
- }
- }
|