[ 'class' => 'yii\web\ErrorAction', ], 'galleryApi' => [ 'class' => GalleryManagerAction::className(), // mappings between type names and model classes (should be the same as in behaviour) 'types' => [ 'product' => News::className(), 'post_gallery' => Gallery::className() ] ], ]; } public function actionIndex() { return $this->render("/default/news",[]); } public function actionList() { if (Yii::$app->request->isGet && Yii::$app->request->get('id')){ Yii::$app->session->get('dt-newslistadm'); }else{ Yii::$app->session->remove('dt-newslistadm'); } return $this->render('/default/newsList', []); } public function actionInactive() { return $this->render('/default/newsInactive', []); } public function actionNewsshow($id) { $model = News::findOne($id); if($model) $model->Active($id); $s = ($model->active == 'Y')?'s':'h'; $profile = Yii::$app->user->identity->profile; $model->editors = $model->editors.(($model->editors)?',':'').$s.':'.$profile->name; $model->save(false); return $this->redirect(array('list', 'id'=>$id)); } public function actionDelete($id) { if(Yii::$app->user->can('deleteNews')){ $model = News::findOne($id); if( $model ){ $model->deleteImg($id); $model->delete(); } } return $this->redirect('list'); } public function actionNewsnh($id) { $model = News::findOne($id); if($model) $model->NH($id); return $this->redirect(array('list', 'id'=>$id)); } public function actionNewskd($id) { $model = News::findOne($id); if($model) $model->KD($id); return $this->redirect(array('list', 'id'=>$id)); } public function actionNewsping($id) { $cache = Yii::$app->cache; $user = Yii::$app->user->identity->profile->name; if( $id && $res = $cache->get("editNews_$id") ){ if( unserialize($res)['author'] != $user ){ $cache->set('editNews_'.$id, serialize( array('author'=>$user, 'type' => 'ping' ) ), 90); return json_encode( ['status'=>'warn', 'author'=>unserialize($res)['author']] ); } } $cache->set('editNews_'.$id, serialize( array('author'=>$user, 'type' => 'ping' ) ), 90); return json_encode( ['status'=>'ok', 'id'=>$id] ); } public function actionTestping() { $authors = array(); $cache = Yii::$app->cache; $post = Yii::$app->request->get('data'); foreach( unserialize($post) as $id ){ $r = $cache->get("editNews_$id"); if( $r ) $authors[$id] = unserialize($r)['author']; } return json_encode( ['status'=>'ok', 'authors'=>$authors] ); } public function actionCreate() { $model = new News(); if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){ $pnews = $post['News']; if( isset($post['cropping']['url']) && $post['cropping']['url'] != '' ){ $pnews['photo_name'] = $post['cropping']['url']; } $pnews = $this->formNormalizer($pnews); $post['News'] = []; $post['News'] = $pnews; if($model->load($post, 'News')) { if( $model->save($pnews) ){ $id = $model->id; if( isset($post['Topics']) ) { $tr = new NewsTopicRelation(); $tr->saveNewsTopics($id, $post['Topics']); } if( isset($post['Story']) ) { $tr = new StoryRelation(); $tr->saveNewsStory($id, $post['Story']); } //echo "photo"; //print_r( $_FILES ); if (isset($_FILES['News']) && is_uploaded_file($_FILES['News']['tmp_name']['photo'])) { // $id = -1; //test $model->saveImg($id, $_FILES['News']['tmp_name']['photo'], $_FILES['News']['type']['photo']); } $tr = new TagsRelation(); if( isset($post['Tags']) ) { $tr->clearNewsSTG($id); try { $tr->saveNewsTags($id, $post['Tags']); } catch (\yii\db\Exception $e) { Yii::warning("Подобный тэг существует"); } } if( isset($post['ExtTag']) ) { $exttag = []; //удалить повторы foreach( $post['ExtTag'] as $item ) { if( isset($post['Tags']) ){ if( !isset( array_flip($post['Tags'])[$item] ) ) $exttag[] = $item; }else{ $exttag[] = $item; } } try { $tr->saveNewsTags($id, $exttag); } catch (\yii\db\Exception $e) { Yii::warning("Подобный тэг существует"); } } $PFilter = Yii::$app->request->post('PFilter'); if($PFilter){ $pfilter = PhotoFilter::findOne($id); if( !$pfilter ){ $pfilter = new PhotoFilter(); $pfilter->id = $id; } $pfilter->load($_POST, 'PFilter'); $pfilter->save(); } if( isset( $post['send'] ) ){ Yii::$app->cache->delete("editNews_".$id); if( isset($post['News']['dt_pub']) ){ $dt = $post['News']['dt_pub']; return $this->redirect(array('list', 'dt'=>$dt)); } return $this->redirect('list'); } //var_dump($model); ///echo $id; exit; return $this->redirect(['update', 'id'=>$id]); }else{ var_dump($model->getErrors()); } }else{ var_dump($model->getErrors()); } } return $this->render('/default/newsCreate', ['model'=>$model]); } public function actionUpdate($id) { if (Yii::$app->request->isPost && $post = Yii::$app->request->post()){ // $model = new \app\models\News(); // $model = $model->find()->andWhere(['id'=>$id])->one(); // Обновить заг галереи if( isset($post['gallery_name']) && count($post['gallery_name']) > 0 ){ foreach($post['gallery_name'] as $key=>$value){ if( trim($value) == '' ) continue; \Yii::$app->db->createCommand() ->update( \manager\models\Gallery::tableName(), ['name' => $value], ['id' => $key] )->execute(); //->rawSql; //->execute(); } } $model = News::findOne($id); // Удалить фото новости if( isset($post['News']['del_photo']) && $post['News']['del_photo'] == 'Y'){ $model->deleteImg($id); } $pnews = $post['News']; if( isset($post['cropping']['url']) && $post['cropping']['url'] != '' ){ $pnews['photo_name'] = $post['cropping']['url']; } $pnews = $this->formNormalizer($pnews); $post['News'] = []; $post['News'] = $pnews; $model->rev++; $model->dt_upd = date("Y-m-d H:i:s", time()); if($model->load($post, 'News')) { if($model->save($pnews)) { $tr = new NewsTopicRelation(); if( isset($post['Topics']) ) { $tr->clearNewsSTG($id); $tr->saveNewsTopics($id, $post['Topics']); }else{ $tr->clearNewsSTG($id); } $tr = new StoryRelation(); if( isset($post['Story']) ) { $tr->clearNewsSTG($id); $tr->saveNewsStory($id, $post['Story']); }else{ $tr->clearNewsSTG($id); } //print_r($_FILES['News']); //print_r($_REQUEST['cropping']); if (isset($_FILES['News']) && is_uploaded_file($_FILES['News']['tmp_name']['photo'])) { // $id = -1; //test $model->saveImg($id, $_FILES['News']['tmp_name']['photo'], $_FILES['News']['type']['photo']); } $tr = new TagsRelation(); if( isset($post['Tags']) ) { $tr->clearNewsSTG($id); try { $tr->saveNewsTags($id, $post['Tags']); } catch (\yii\db\Exception $e) { Yii::warning("Подобный тэг существует"); } }else{ $tr->clearNewsSTG($id); } // добавить тэги для меню фильтров if( isset($post['ExtTag']) ) { $exttag = []; //удалить повторы foreach( $post['ExtTag'] as $item ) { if( isset($post['Tags']) ){ if( !isset( array_flip($post['Tags'])[$item] ) ) $exttag[] = $item; }else{ $exttag[] = $item; } } try { $tr->saveNewsTags($id, $exttag); } catch (\yii\db\Exception $e) { Yii::warning("Подобный тэг существует"); } } $PFilter = Yii::$app->request->post('PFilter'); if($PFilter){ //var_dump($PFilter);exit; $pfilter = PhotoFilter::findOne($id); if( !$pfilter ){ $pfilter = new PhotoFilter(); $pfilter->id = $id; } $pfilter->load($_POST, 'PFilter'); $pfilter->save(); } Yii::$app->cache->delete("editNews_".$id); if( isset( $post['send'] ) ){ if( isset($post['News']['dt_pub']) ){ $dt = $post['News']['dt_pub']; return $this->redirect(array('list', 'dt'=>$dt)); } return $this->redirect('list'); } }else{ echo "no save"; var_dump($model->getErrors()); } }else{ echo "load"; var_dump($model->getErrors()); } return $this->render('/default/newsUpdate', ['id'=>$id, 'model'=>$model]); } return $this->render('/default/newsUpdate', ['id'=>$id]); } public function formNormalizer($post) { $profile = Yii::$app->user->identity->profile; $post['title'] = trim(str_replace( "\xc2\xa0", ' ', $post['title'] )); $post['title'] = str_replace( ['«','»','«','»','«','«'], '"', $post['title'] ); $post['lid'] = trim(str_replace( "\xc2\xa0", ' ', $post['lid'] )); $post['lid'] = str_replace( ['«','»','«','»','«','«'], '"', $post['lid'] ); $post['text'] = str_replace( "\xc2\xa0", ' ', $post['text'] ); //удалить 4х-байтные символы UTF $post['text'] = preg_replace("/[\xf0-\xf7][\x80-\xbf]{3}/", " ", $post['text'] ); $post['active'] = $this->CheckYN(@$post['active']); $post['show_author'] = $this->CheckYN(@$post['show_author']); $post['verifed'] = $this->CheckYN(@$post['verifed']); $post['photo_include'] = $this->CheckYN(@$post['photo_include']); $post['photo_rcol'] = $this->CheckYN(@$post['photo_rcol']); $post['top'] = $this->CheckYN(@$post['top']); if( isset($post['NH']) ){ if( $post['NH'] == 'Y' ){ $post['NH'] = 'Y'; } if( $post['NH'] == 'F' ){ $post['NH'] = 'F'; } }else{ $post['NH'] = 'N'; } $post['photo'] = $this->CheckYN(@$post['photo']); $post['video'] = $this->CheckYN(@$post['video']); $post['audio'] = $this->CheckYN(@$post['audio']); $post['comments'] = ( isset($post['comments']) || ( isset($post['comments']) && $post['comments'] == 'Y') )?'N':'Y'; $post['noindex'] = $this->CheckYN(@$post['noindex']); $post['export_rss'] = (isset($post['fdzen']) && $post['fdzen'] == 'Y')?1:0; $post['export_rss'] += (isset($post['fmail']) && $post['fmail'] == 'Y')?2:0; $post['inscription'] = isset($post['commerc'])?$post['commerc']*1:0; $post['dt_pub'] = date("Y-m-d H:i:s", strtotime($post['dt_pub'])); $post['alias'] = (isset($post['alias']) && trim( $post['alias'] ) != '' && $post['active'] == 'Y')?$post['alias']:Transliterator::toUrl($post['title']); $post['editors'] = $post['editors'].(($post['editors'])?',':'').$profile->name; if( isset($_FILES['News']) && is_uploaded_file($_FILES['News']['tmp_name']['photo']) ){ $post['photo_name'] = ''; }else{ $post['photo_name'] = isset($post['photo_name'])?$post['photo_name']:''; } $post['flags'] = isset($post['nofirst'])?1:0; $post['flags'] = $post['flags'] | (isset($post['photoamic'])?2:0); return $post; } public function CheckYN($attr) { return (isset($attr) && $attr == 'Y')?'Y':'N'; } // ** Рубрики ** 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); Yii::$app->cache->delete("archive_rubrics-{$id}"); return $this->render('/default/topicList', []); } public function actionTopicshow($id) { $model = $this->findTopicModel($id); if($model) $model->Show($id); Yii::$app->cache->delete("archive_rubrics-{$id}"); return $this->redirect('topiclist'); } public function actionTopicactive($id) { $model = $this->findTopicModel($id); if($model) $model->Active($id); Yii::$app->cache->delete("archive_rubrics-{$id}"); return $this->redirect('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++; } Yii::$app->cache->delete("archive_rubrics-"); 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']); Yii::$app->cache->delete("archive_rubrics-{$model->id}"); } }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']); Yii::$app->cache->delete("archive_rubrics-{$id}"); } }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, 20); $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 actionAddGallery() { $gallery = new \manager\models\Gallery(); $gallery->load(Yii::$app->request->get(),''); $gallery->save(); return $this->renderPartial('@manager/views/news/form/galleryItem',["model"=>$gallery]); } public function actionRemoveGallery($id) { $gallery = \manager\models\Gallery::find()->andWhere(['id'=>$id])->one(); $gallery->delete(); return true; } public function actionAjaxgalleryslide() { if (Yii::$app->request->isGet){ $id = Yii::$app->request->get('id'); $gid = Yii::$app->request->get('gid'); $title = Yii::$app->request->get('title'); $description = Yii::$app->request->get('description'); //p = new \manager\models\GalleryImage->getUrl(1,'small'); $db = \Yii::$app->db; $gallery = \manager\models\Gallery::find()->andWhere(['id'=>$gid])->one(); // print_r( $gallery ); $m = $gallery->getBehavior('galleryBehavior'); // print_r( $db->quoteTableName($m->tableName) ); // $tableName = 'gallery_image'; // хз как получить из компонента $r = $db->createCommand() ->insert( $m->tableName, [ 'type' => 'html', 'ownerId' => $gid, 'name' => $title, 'description' => $description ] )->execute(); // $p = new \manager\models\Gallery(); // echo $p->behaviorName; /* \yii\helpers\VarDumper::dump(\Yii::$app->db->createCommand() ->update( GalleryImage::tableName(), ['name' => $title], ['id' => $id] )->rawSql); //->execute(); */ if( $r ){ return json_encode( ['status'=>'ok', 'id'=>$id, 'gid'=>$gid, 'title'=>$title, 'description'=>$description] ); } } return json_encode( ['status'=>'err'] ); } // ** отладка ** 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; } }