<?php
namespace medeyacom\blog\controllers;
use Yii;
use common\models\ImageManager;
use medeyacom\blog\models\BlogSearch;
use yii\web\Controller;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* BlogController implements the CRUD actions for Blog model.
*/
class BlogController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
'delete-image' => ['POST'],
'sort-image' => ['POST'],
],
],
];
}
/**
* Lists all Blog models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new BlogSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Blog model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Blog model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new \medeyacom\blog\models\Blog();
$model->sort = 50;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Blog model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Blog model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionDeleteImage()
{
if(($model = ImageManager::findOne(Yii::$app->request->post('key'))) and $model->delete()){
return true;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionSortImage($id)
{
if(Yii::$app->request->isAjax){
$post = Yii::$app->request->post('sort');
if($post['oldIndex'] > $post['newIndex']){
$param = ['and',['>=','sort',$post['newIndex']],['<','sort',$post['oldIndex']]];
$counter = 1;
}else{
$param = ['and',['<=','sort',$post['newIndex']],['>','sort',$post['oldIndex']]];
$counter = -1;
}
ImageManager::updateAllCounters(['sort' => $counter], [
'and',['class'=>'blog','item_id'=>$id],$param
]);
ImageManager::updateAll(['sort' => $post['newIndex']], [
'id' => $post['stack'][$post['newIndex']]['key']
]);
return true;
}
throw new MethodNotAllowedHttpException();
}
/**
* Finds the Blog model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Blog the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = \medeyacom\blog\models\Blog::find()->with('tags')->andWhere(['id'=>$id])->one()) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\helpers\Url;
use yii\web\UploadedFile;
use vova07\imperavi\Widget;
use kartik\widgets\FileInput;
/* @var $this yii\web\View */
/* @var $searchModel medeyacom\blog\models\BlogSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Blogs';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="blog-index">
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Blog', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete} {check}',
'buttons' => [
'check' => function($url,$model,$key) {
return Html::a('<i class="fa fa-check" aria-hidden="true"></i>',$url);
}
],
'visibleButtons' => [
'check' => function ($model, $key, $index){
return $model->status_id === 1;
}
]
],
'id',
'title',
['attribute'=>'url','format'=>'raw'],
/*'headerOptions'=>['class'=>'dgfdhh']], */
/* ['attribute'=>'status_id','filter'=>['off','on'],'value'=>function($model){
$status = 'off';
if($model->status_id ==1) {
$status = 'on';
}
return $status;
}], */
['attribute'=>'status_id','filter'=>\medeyacom\blog\models\Blog::STATUS_LIST,'value'=>'statusName'],
/* return $model->statusName;
}],*/
'sort',
'smallImage:image',
'date_create:datetime',
'date_update:datetime',
['attribute'=>'tags','value'=>'tagsAsString'],
],
]); ?>
<?php Pjax::end(); ?></div>
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log','debug'],
'controllerNamespace' => 'frontend\controllers',
'modules'=>[
'debug'=>[
'class'=>'yii2\debug\Module'
],
],
'components' => [
'request' => [
'csrfParam' => '_csrf-frontend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<url>'=>'blog/one',
'blog'=>'blog/index'
],
],
],
'params' => $params,
];
'blog/delete-image'=>'blog/delete-image',
<?php
namespace frontend\controllers;
use medeyacom\blog\models\Blog;
use Yii;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* Blog controller
*/
class BlogController extends Controller
{
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex()
{
#$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_DESC])->all();
$blogs = Blog::find()->with('author')->andWhere(['status_id'=>1])->orderBy('sort');
$dataProvider = new ActiveDataProvider([
'query' => $blogs,
'pagination' => [
'pageSize' => 10,
],
]);
#$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_ASC])->all();
return $this->render('all',['dataProvider'=>$dataProvider]);
}
public function actionOne($url)
{
if($blog = Blog::find()->andWhere(['url'=>$url])->one()) {
return $this->render('one',['blog'=>$blog]);
}
throw new NotFoundHttpException('ой,нет такого блога');
}
}
<?php
namespace medeyacom\blog\controllers;
use Yii;
use common\models\ImageManager;
use medeyacom\blog\models\BlogSearch;
use yii\web\Controller;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* BlogController implements the CRUD actions for Blog model.
*/
class BlogController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
'delete-image' => ['POST'],
'sort-image' => ['POST'],
],
],
];
}
/**
* Lists all Blog models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new BlogSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Blog model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Blog model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new \medeyacom\blog\models\Blog();
$model->sort = 50;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Blog model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Blog model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionDeleteImage()
{
if(($model = ImageManager::findOne(Yii::$app->request->post('key'))) and $model->delete()){
return true;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionSortImage($id)
{
if(Yii::$app->request->isAjax){
$post = Yii::$app->request->post('sort');
if($post['oldIndex'] > $post['newIndex']){
$param = ['and',['>=','sort',$post['newIndex']],['<','sort',$post['oldIndex']]];
$counter = 1;
}else{
$param = ['and',['<=','sort',$post['newIndex']],['>','sort',$post['oldIndex']]];
$counter = -1;
}
ImageManager::updateAllCounters(['sort' => $counter], [
'and',['class'=>'blog','item_id'=>$id],$param
]);
ImageManager::updateAll(['sort' => $post['newIndex']], [
'id' => $post['stack'][$post['newIndex']]['key']
]);
return true;
}
throw new MethodNotAllowedHttpException();
}
/**
* Finds the Blog model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Blog the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = \medeyacom\blog\models\Blog::find()->with('tags')->andWhere(['id'=>$id])->one()) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
<?php
namespace frontend\controllers;
use medeyacom\blog\models\Blog;
use common\models\ImageManager;
use Yii;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use medeyacom\blog\models\BlogSearch;
/**
* Blog controller
*/
class BlogController extends Controller
{
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex()
{
#$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_DESC])->all();
$blogs = Blog::find()->with('author')->andWhere(['status_id'=>1])->orderBy('sort');
$dataProvider = new ActiveDataProvider([
'query' => $blogs,
'pagination' => [
'pageSize' => 10,
],
]);
#$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_ASC])->all();
return $this->render('all',['dataProvider'=>$dataProvider]);
}
public function actionOne($url)
{
if($blog = Blog::find()->andWhere(['url'=>$url])->one()) {
return $this->render('one',['blog'=>$blog]);
}
throw new NotFoundHttpException('ой,нет такого блога');
}
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionDeleteImage()
{
if(($model = ImageManager::findOne(Yii::$app->request->post('key'))) and $model->delete()){
return true;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
но все ровно что-то не так..