Current Path : /home/vmanager/www/common/models/ |
Linux 9dbcd5f6333d 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64 |
Current File : /home/vmanager/www/common/models/BannersReservationsVersions.php |
<?php namespace common\models; use Yii; use yii\db\Expression; use yii\behaviors\TimestampBehavior; /** * This is the model class for table "banners_reservations_versions". * * @property integer $id_version * @property integer $id_reservation * @property string $filename * @property integer $version_number * @property string $annotations * @property integer $or_accepter * @property string $created_at * @property string $updated_at * * @property BannersReservations $reservation */ class BannersReservationsVersions extends \yii\db\ActiveRecord { public $file; /** * @inheritdoc */ public static function tableName() { return 'banners_reservations_versions'; } /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'value' => new Expression('LOCALTIMESTAMP'), ] ]; } /** * @inheritdoc */ public function scenarios() { $scenarios = parent::scenarios(); $scenarios['create'] = ['!id_reservation', 'filename', '!version_number', 'annotations', 'or_accepted', 'file']; return $scenarios; } /** * @inheritdoc */ public function rules() { return [ ['id_reservation', 'required'], ['id_reservation', 'number', 'integerOnly' => true, 'min' => 1], ['id_reservation', 'exist', 'targetClass' => BannersReservations::className(), 'targetAttribute' => 'id_reservation'], ['filename', 'required'], ['filename', 'string', 'min' => 5, 'max' => 255], ['file', 'file', 'extensions' => array('jpg', 'gif', 'png', 'webp', 'swf'), 'maxFiles' => 1, 'maxSize' => 358400, 'checkExtensionByMimeType' => true, 'skipOnEmpty' => true], ['annotations', 'default'], ['annotations', 'string'], ['version_number', 'required'], ['version_number', 'number', 'integerOnly' => true, 'min' => 1], ['or_accepted', 'default', 'value' => 0], ['or_accepted', 'required'], ['or_accepted', 'number', 'integerOnly' => true, 'min' => 0, 'max' => 1], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id_version' => Yii::t('common-models', 'ID wersji'), 'id_reservation' => Yii::t('common-models', 'ID rezerwacji'), 'filename' => Yii::t('common-models', 'Plik'), 'version_number' => Yii::t('common-models', 'Wersja'), 'annotations' => Yii::t('common-models', 'Uwagi'), 'or_accepted' => Yii::t('common-models', 'Zaakceptowana'), 'created_at' => Yii::t('common-models', 'Data utworzenia'), 'updated_at' => Yii::t('common-models', 'Data aktualizacji'), 'file' => Yii::t('common-models', 'Plik'), ]; } /** * @inheritdoc */ public function afterDelete() { parent::afterDelete(); if(!empty($this->filename)) { $file = Yii::getAlias('@bannersReservationsImagesRealPath').'/'.$this->filename; if(file_exists($file)) { @unlink($file); } } } public function getUrl($absolute = false) { if(empty($this->filename)) { return null; } return $absolute === true ? Yii::getAlias('@bannersReservationsImagesUrl').'/'.$this->filename : Yii::getAlias('@bannersReservationsImagesRealPath').'/'.$this->filename; } public static function createFileName($baseName, $extension) { $baseName = strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $baseName)); $baseName = preg_replace('/\s+/i', '-', $baseName); $baseName = preg_replace('/[^a-zA-Z0-9_\-]/i', '', $baseName); return $baseName.'-'.str_replace(' ', '', substr(microtime(), 2)).'.'. strtolower($extension); } public function loadFile() { $this->file = \yii\web\UploadedFile::getInstance($this, 'file'); if($this->file) { $this->filename = self::createFileName($this->file->baseName, $this->file->extension); } } public function saveFile() { $newImagePath = Yii::getAlias('@bannersReservationsImagesRealPath').'/'.$this->filename; if(!$this->file->saveAs($newImagePath)) { return false; } return $newImagePath; } public function getViewUrl($modelPortal = null) { $encryptionEngine = new \common\components\EncriptionEngine(Yii::$app->params['bannersEncryptionKey'], Yii::$app->params['bannersInitializationVector']); $key = substr((string)microtime(true), -4).'|V|'.$this->id_version; $cryptedKey = $encryptionEngine->encrypt($key); if(empty($modelPortal)) { $modelPortal = $this->reservation->portal; } $webProtocol = $modelPortal->getSettingsKey('or_ssl') === 1 ? 'https' : 'http'; return $webProtocol.'://www.'.$modelPortal->domain.'/index/zobacz-baner?key='.urlencode($cryptedKey); } public static function getModelByCryptedKey($key) { $encryptionEngine = new \common\components\EncriptionEngine(Yii::$app->params['bannersEncryptionKey'], Yii::$app->params['bannersInitializationVector']); $decryptedKey = $encryptionEngine->decrypt($key); if(empty($decryptedKey)) { return false; } list($random, $type, $id) = explode('|', $decryptedKey); if($type !== 'V') { return false; } return self::findOne($id); } /** * @return \yii\db\ActiveQuery */ public function getReservation() { return $this->hasOne(BannersReservations::className(), ['id_reservation' => 'id_reservation']); } }