use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;
use yii\imagine\Image;
use yii\helpers\Json;
use Imagine\Image\Box;
use Imagine\Image\Point;
use yii\web\UploadedFile;
/**
This is the model class for table "stadium".
@property int $id
@property string|null $name
@property string $id_region
@property string|null $district
@property string|null $address
@property string|null $description
@property string|null $image
*/
class Stadium extends \yii\db\ActiveRecord
{
/**
{@inheritdoc}
*/
public static function tableName()
{
return 'stadium';
}
public $image;
public $crop_info;
/**
{@inheritdoc}
*/
public function rules()
{
return [
[['name'], 'string', 'max' => 50],
[['name'], 'unique', 'message' => 'Такое имя уже используется'],
[['id_region'], 'required'],
[['district'], 'string', 'max' => 20],
[['address', 'description', 'imglogo' ], 'string', 'max' => 255],
{@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Стадион',
'id_region' => 'Регион',
'district' => 'Район',
'address' => 'Адрес',
'description' => 'Описание',
'imglogo' => 'Картинка',
];
}
public function getRegion(){
return $this->hasOne(Region::className(), ['id' => 'id_region']);
}
public function getCourt(){
return $this->hasMany(Court::className(), ['id_stadium' => 'id']);
}
public static function getList(){
$list = self::find()->asArray()->all();
return ArrayHelper::map($list, 'id', 'name');
}
public function afterSave($insert, $changedAttributes){
// open image
$image = Image::getImagine()->open($this->image->tempName);
// rendering information about crop of ONE option
$cropInfo = Json::decode($this->crop_info)[0];
$cropInfo['dWidth'] = (int)$cropInfo['dWidth']; //new width image
$cropInfo['dHeight'] = (int)$cropInfo['dHeight']; //new height image
$cropInfo['x'] = $cropInfo['x']; //begin position of frame crop by X
$cropInfo['y'] = $cropInfo['y']; //begin position of frame crop by Y
// Properties bolow we don't use in this example
//$cropInfo['ratio'] = $cropInfo['ratio'] == 0 ? 1.0 : (float)$cropInfo['ratio']; //ratio image.
//$cropInfo['width'] = (int)$cropInfo['width']; //width of cropped image
//$cropInfo['height'] = (int)$cropInfo['height']; //height of cropped image
//$cropInfo['sWidth'] = (int)$cropInfo['sWidth']; //width of source image
//$cropInfo['sHeight'] = (int)$cropInfo['sHeight']; //height of source image
use Yii;
use app\modules\dusik\models\Stadium;
use app\modules\dusik\models\StadiumSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\modules\dusik\models\Region;
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;
/**
StadiumController implements the CRUD actions for Stadium model.
*/
class StadiumController extends Controller
{
/**
@throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
Creates a new Stadium model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
*/
public function actionCreate()
{
$model = new Stadium();
$regions = Region::getList();
if ($model->load(Yii::$app->request->post())) {
// process uploaded image file instance
$model->image = UploadedFile::getInstance($model, 'image');
if ($model->save()) {
// upload only if valid uploaded file instance found
return $this->redirect(['view', 'id'=>$model->id]);
}
If update is successful, the browser will be redirected to the 'view' page.
@param integer $id
@return mixed
@throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id) {
$model = $this->findModel($id);
$regions = Region::getList();
if ($model->load(Yii::$app->request->post())) {
// process uploaded image file instance
$model->image = UploadedFile::getInstance($model, 'image');
if ($model->save()) {
// upload only if valid uploaded file instance found
return $this->redirect(['view', 'id'=>$model->id]);
}
If deletion is successful, the browser will be redirected to the 'index' page.
@param integer $id
@return mixed
@throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
Finds the Stadium model based on its primary key value.
If the model is not found, a 404 HTTP exception will be thrown.
@param integer $id
@return Stadium the loaded model
@throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Stadium::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
namespace app\modules\dusik\models;
use Yii; use yii\helpers\ArrayHelper; use yii\helpers\FileHelper; use yii\imagine\Image; use yii\helpers\Json; use Imagine\Image\Box; use Imagine\Image\Point; use yii\web\UploadedFile; /**
@property string|null $image */ class Stadium extends \yii\db\ActiveRecord { /**
public $image; public $crop_info;
/**
{@inheritdoc} */ public function rules() { return [ [['name'], 'string', 'max' => 50], [['name'], 'unique', 'message' => 'Такое имя уже используется'], [['id_region'], 'required'], [['district'], 'string', 'max' => 20], [['address', 'description', 'imglogo' ], 'string', 'max' => 255],
]; }
/**
public function afterSave($insert, $changedAttributes){
// open image $image = Image::getImagine()->open($this->image->tempName);
// rendering information about crop of ONE option $cropInfo = Json::decode($this->crop_info)[0]; $cropInfo['dWidth'] = (int)$cropInfo['dWidth']; //new width image $cropInfo['dHeight'] = (int)$cropInfo['dHeight']; //new height image $cropInfo['x'] = $cropInfo['x']; //begin position of frame crop by X $cropInfo['y'] = $cropInfo['y']; //begin position of frame crop by Y // Properties bolow we don't use in this example //$cropInfo['ratio'] = $cropInfo['ratio'] == 0 ? 1.0 : (float)$cropInfo['ratio']; //ratio image. //$cropInfo['width'] = (int)$cropInfo['width']; //width of cropped image //$cropInfo['height'] = (int)$cropInfo['height']; //height of cropped image //$cropInfo['sWidth'] = (int)$cropInfo['sWidth']; //width of source image //$cropInfo['sHeight'] = (int)$cropInfo['sHeight']; //height of source image
//delete old images $oldImages = FileHelper::findFiles(Yii::getAlias('@dusik') . '/web/images/stadium', [ 'only' => [ $this->id . '.', '/thumb_' . $this->id . '.', ], ]); for ($i = 0; $i != count($oldImages); $i++) { @unlink($oldImages[$i]); }
//saving thumbnail $newSizeThumb = new Box($cropInfo['dWidth'], $cropInfo['dHeight']); $cropSizeThumb = new Box(200, 200); //frame size of crop $cropPointThumb = new Point($cropInfo['x'], $cropInfo['y']); $pathThumbImage = Yii::getAlias('@dusik') . '/web/images/stadium' . '/thumb_' . $this->id . '.' . $this->image->getExtension();
$image->resize($newSizeThumb) ->crop($cropPointThumb, $cropSizeThumb) ->save($pathThumbImage, ['quality' => 100]);
//saving original $this->image->saveAs( Yii::getAlias('@dusik') . '/web/images/stadium' . '/' . $this->id . '.' . $this->image->getExtension() ); }
}
namespace app\modules\dusik\controllers;
use Yii; use app\modules\dusik\models\Stadium; use app\modules\dusik\models\StadiumSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use app\modules\dusik\models\Region; use yii\helpers\ArrayHelper; use yii\web\UploadedFile;
/**
StadiumController implements the CRUD actions for Stadium model. */ class StadiumController extends Controller { /**
/**
@return mixed */ public function actionIndex() { $searchModel = new StadiumSearch(); $regions = Region::getList(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'regions' => $regions, ]); }
/**
/**
@return mixed */ public function actionCreate() { $model = new Stadium(); $regions = Region::getList();
if ($model->load(Yii::$app->request->post())) { // process uploaded image file instance $model->image = UploadedFile::getInstance($model, 'image');
}
return $this->render('create', [ 'model' => $model, 'regions' => $regions, ]); }
/**
@throws NotFoundHttpException if the model cannot be found */ public function actionUpdate($id) { $model = $this->findModel($id); $regions = Region::getList();
if ($model->load(Yii::$app->request->post())) { // process uploaded image file instance $model->image = UploadedFile::getInstance($model, 'image');
}
return $this->render('update', [ 'model' => $model, 'regions' => $regions, ]); }
/**
@throws NotFoundHttpException if the model cannot be found */ public function actionDelete($id) { $this->findModel($id)->delete();
return $this->redirect(['index']); }
/**
@throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Stadium::findOne($id)) !== null) { return $model; }
throw new NotFoundHttpException('The requested page does not exist.'); } }