<?php
namespace wokster\article\models;
use wokster\behaviors\ImageUploadBehavior;
use wokster\behaviors\StatusBehavior;
use wokster\tags\TagsBehavior;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\StringHelper;
use yii\helpers\Url;
use yiidreamteam\upload\FileUploadBehavior;
use common\models\ImageManager;
/* @var $this yii\web\View */
/* @var $searchModel \wokster\article\models\ArticleSearch*/
/* @var $dataProvider yii\data\ActiveDataProvider */
/**
* This is the model class for table "article".
*
* @property integer $id
* @property string $title
* @property string $url
* @property string $text
* @property integer $status_id
* @property string $image
* @property integer $date_create
* @property integer $type_id
* @property integer $date_start
* @property integer $date_finish
* @property integer $smallImage
* @property integer $new_tags
* @property integer $seo_title
*/
class Article extends \yii\db\ActiveRecord
{
public $file;
public $fileUpload;
public $new_tags;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'article';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
/* 'status' => [
'class' => StatusBehavior::className(),
'status_value' => $this->status_id,
'statusList' => Yii::$app->modules['article']->status_list,
],*/
'upload1' => [
'class' => '\yiidreamteam\upload\FileUploadBehavior',
'attribute' => 'fileUpload',
'filePath' => Yii::$app->modules['article']->imagePath,
'fileUrl' => Yii::$app->modules['article']->imageUrl,
],
'seo' => [
'class' => \wokster\seomodule\SeoBehavior::className(),
],
];
}
/* 'smallImage' => [
'class' => ImageUploadBehavior::className(),
'attribute' => 'image',
'random_name' => 'true',
'image_path' => Yii::$app->modules['article']->imagePath,
'image_url' => Yii::$app->modules['article']->imageUrl,
/* 'size_for_resize' => [
[640,480,true],
[640,null,false],
[50,50,true]
]
]
/*
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'date_create',
'updatedAtAttribute' => false,
],
'seo' => [
'class' => \wokster\seomodule\SeoBehavior::className(),
],
'tags' => [
'class' => TagsBehavior::className(),
],
];
}
/**
* @inheritdoc
*/
public function getImage()
{
return $this->hasMany(ImageManager::className(), ['item_id' => 'id'])->andWhere(['class'=>self::tableName()])->orderBy('sort');
}
public function rules()
{
return [
[['title', 'url'], 'required'],
[['date_start', 'date_finish'], 'required', 'when' => function($model) {
return $model->type_id == \wokster\article\Article::TYPE_SALE;
}],
[['text'], 'string'],
[['status_id', 'date_create', 'type_id', 'date_start', 'date_finish'], 'integer'],
[['title', 'image'], 'string', 'max' => 255],
[['url'], 'string', 'max' => 100],
[['url'], 'unique'],
[['url'], 'match', 'pattern' => '/^[a-z0-9_-]+$/', 'message' => 'Недопустимые символы в url'],
[['status_id'], 'default','value'=>0],
['file','image'],
['fileUpload', 'file'],
/* ['smallImage'],*/
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'заглавие',
'url' => 'ЧПУ',
'text' => 'контент',
'status_id' => 'статус',
'image' => 'картинка',
'date_create' => 'дата публикации',
'type_id' => 'тип',
'Status' => 'статус',
/* 'smallImage'=> 'превью',*/
'date_start' => 'дата начала',
'date_finish' => 'дата окончания'
];
}
/**
* @return mixed
*/
public static function getTypeList(){
return Yii::$app->modules['article']->type_list;
}
/**
* @return string
*/
public function getTypeName(){
$list = self::getTypeList();
return (isset($list[$this->type_id]))?$list[$this->type_id]:'';
}
/**
* @return string
*/
public function getShortText(){
return StringHelper::truncateWords(strip_tags($this->text),50);
}
}
<?php
namespace wokster\article\controllers;
use yii;
use wokster\article\models\Article;
use wokster\article\models\ArticleSearch;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yiidreamteam\upload\FileUploadBehavior;
/**
* ArticleController implements the CRUD actions for Article model.
*/
class ArticleController extends yii\web\Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actions()
{
return [
/* 'images-get' => [
'class' => 'vova07\imperavi\actions\GetAction',
'url' => \Yii::$app->controller->module->allRedactorImageUrl, // Directory URL address, where files are stored.
'path' => \Yii::$app->controller->module->redactor_upload_path_alias, // Or absolute path to directory where files are stored.
'type' => \vova07\imperavi\actions\GetAction::TYPE_IMAGES,
],*/
'image-upload' => [
'class' => 'vova07\imperavi\actions\UploadAction',
'url' => \Yii::$app->controller->module->redactorImageUrl, // Directory URL address, where files are stored.
'path' => \Yii::$app->controller->module->redactorPath, // Or absolute path to directory where files are stored.
],
];
}
/**
* Lists all Article models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ArticleSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Article model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Article model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate($type=null)
{
$model = new Article();
if(is_numeric($type))
$model->type_id = $type;
/* if ($model->load(Yii::$app->request->post()) && $model->save()) {
if(Yii::$app->request->post('toview',false)){
return $this->redirect(['view', 'id' => $model->id]);
}else{
return $this->redirect(['update', 'id' => $model->id]);
}*/
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// $file - атрибут в модели для хранения пути до файла (поле в таблице БД)
$model->file = $model->getUploadedFileUrl('fileUpload');
if($model->save())
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Article 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(['update', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Article 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']);
}
/**
* Finds the Article model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Article the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Article::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}