Created
August 6, 2016 12:24
-
-
Save jovanialferez/c457b96c75b530d82e2792669e3a23de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\models; | |
use Yii; | |
use yii\behaviors\BlameableBehavior; | |
use yii\behaviors\TimestampBehavior; | |
use yii\imagine\Image; | |
/** | |
* This is the model class for table "files". | |
* | |
* @property integer $id | |
* @property integer $user_id | |
* @property integer $vehicle_id | |
* @property integer $booking_id | |
* @property string $name | |
* @property string $description | |
* @property string $url | |
* @property string $notes | |
* @property string $status | |
* @property string $expiration | |
* @property integer $uploaded_by | |
* @property integer $created_at | |
* @property integer $updated_at | |
* | |
* @property User $uploadedBy | |
* @property Profile[] $profiles | |
*/ | |
class File extends \common\models\BaseAR | |
{ | |
const TYPE_PHOTO = 'photo'; | |
const TYPE_LICENSE = 'license'; | |
const TYPE_AUTHORITY_CARD = 'authority_card'; | |
const TYPE_REGISTRATION = 'registration'; | |
const TYPE_PARTNERSHIP = 'partnership'; | |
const TYPE_VEHICLE_REGISTRATION = 'vehicle_registration'; | |
const TYPE_VEHICLE_INSURANCE = 'vehicle_insurance'; | |
const TYPE_TRACKS = 'tracks'; | |
const STATUS_ARCHIVED = 4; | |
const STATUS_APPROVED = 3; | |
const STATUS_REJECTED = 2; | |
const STATUS_PENDING = 1; | |
const STATUS_EMPTY = 0; | |
const SCENARIO_UPDATE = 'update'; | |
/** | |
* @var array | |
*/ | |
public static $typeLabels = [ | |
self::TYPE_VEHICLE_INSURANCE => 'Vehicle Insurance', | |
self::TYPE_VEHICLE_REGISTRATION => 'Registration Certificate', | |
]; | |
/** | |
* @var array | |
*/ | |
public static $statusLabels = [ | |
self::STATUS_PENDING => 'Pending', | |
self::STATUS_REJECTED => 'Rejected', | |
self::STATUS_APPROVED => 'Approved', | |
]; | |
/** | |
* @inheritdoc | |
*/ | |
public static function tableName() | |
{ | |
return '{{%files}}'; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function behaviors() | |
{ | |
return [ | |
'blameable' => [ | |
'class' => BlameableBehavior::className(), | |
'attributes' => [ | |
self::EVENT_BEFORE_INSERT => ['uploaded_by'], | |
self::EVENT_BEFORE_UPDATE => ['uploaded_by'], | |
], | |
'value' => function ($event) { | |
if (\Yii::$app instanceof \yii\console\Application) { | |
return User::find()->superadmin()->one()->id; | |
} else { | |
$user = Yii::$app->get('user', false); | |
return $user && $user->identity && !$user->isGuest ? $user->id : null; | |
} | |
} | |
], | |
TimestampBehavior::className(), | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['vehicle_id', 'booking_id', 'notes', 'expiration'], 'safe'], | |
[['name', 'type'], 'required', 'on' => self::SCENARIO_DEFAULT], | |
[['name', 'type', 'user_id'], 'safe', 'on' => self::SCENARIO_UPDATE], | |
[['name', 'description', 'url'], 'string', 'max' => 255], | |
]; | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getUploadedBy() | |
{ | |
return $this->hasOne(User::className(), ['id' => 'uploaded_by']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getUser() | |
{ | |
return $this->hasOne(User::className(), ['id' => 'user_id']); | |
} | |
/** | |
* Generates random file name. | |
*/ | |
public static function generateRandomFileName() | |
{ | |
return uniqid(date('Ymd') . '_'); | |
} | |
public static function getStoragePath($fileType = 'documents') | |
{ | |
if ($fileType == 'photo') { | |
return 'photos/'; | |
} | |
return 'documents/'; | |
} | |
/** | |
* Resize a photo, create a local copy, then upload to S3. | |
* | |
* @param int $userId owner of this photo | |
* @param string $sourceFile path of the source image file | |
* @param array $sizes valid values are ['original', '100', '200', '300', '600'] | |
* @return array indexed by the expected sizes | |
*/ | |
public static function photoResizeAndStore($userId, $sourceFile, $sizes) | |
{ | |
$toReturn = []; | |
$baseFilename = basename($sourceFile); | |
foreach ($sizes as $size) { | |
if (!in_array($size, ['original', '100', '200', '300', '600'])) { | |
continue; | |
} | |
$url = ''; | |
$filename = $size == 'original' ? $baseFilename : $size . '_' . $baseFilename; | |
$destinationFile = File::getStoragePath(File::TYPE_PHOTO) . $filename; | |
$localDestinationFile = \Yii::$app->params['fileUploadsPath'] . $destinationFile; | |
if ($size == 'original') { | |
copy($sourceFile, $localDestinationFile); | |
$url = \Yii::$app->storage->uploadFile($sourceFile, $destinationFile); | |
} else { | |
$image = Image::getImagine()->open($sourceFile); | |
$image->resize($image->getSize()->widen($size)); | |
$image->save($localDestinationFile, ['quality' => 50]); | |
// Image::thumbnail($sourceFile, $size, $size)->save($localDestinationFile, ['quality' => 50]); | |
// Upload resized copy | |
$url = \Yii::$app->storage->uploadFile($localDestinationFile, $destinationFile); | |
} | |
$file = new self; | |
$file->user_id = $userId; | |
$file->name = $filename; | |
$file->url = $url; | |
$file->type = File::TYPE_PHOTO; | |
$file->status = File::STATUS_APPROVED; | |
if ($file->save()) { | |
$toReturn[$size] = [ | |
'id' => $file->id, | |
'url' => $url, | |
]; | |
} | |
} | |
return $toReturn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment