2024-02-16 15:13:29 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\models;
|
|
|
|
|
2024-04-22 14:16:45 +08:00
|
|
|
use Yii;
|
2024-03-04 15:50:51 +08:00
|
|
|
use yii\db\ActiveQuery;
|
|
|
|
use yii\db\ActiveRecord;
|
2024-02-16 15:13:29 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the model class for table "share".
|
|
|
|
*
|
|
|
|
* @property int $share_id 分享ID
|
|
|
|
* @property int $sharer_id 分享者ID
|
|
|
|
* @property string $file_relative_path 文件的相对路径
|
|
|
|
* @property string $access_code 分享密钥
|
|
|
|
* @property string $creation_date 分享创建日期
|
2024-03-04 16:26:37 +08:00
|
|
|
* @property int|null $status 分享是否启用
|
2024-04-07 14:34:43 +08:00
|
|
|
* @property int|null $dl_count 下载次数
|
2024-02-16 15:13:29 +08:00
|
|
|
*
|
2024-04-07 14:34:43 +08:00
|
|
|
* @property DownloadLogs[] $downloadLogs
|
|
|
|
* * @property User $sharer
|
2024-02-16 15:13:29 +08:00
|
|
|
*/
|
2024-03-04 15:50:51 +08:00
|
|
|
class Share extends ActiveRecord
|
2024-02-16 15:13:29 +08:00
|
|
|
{
|
2024-04-11 15:36:46 +08:00
|
|
|
const SCENARIO_UPDATE = 'update'; // PHP8.2 need to remove "string"
|
2024-04-07 14:34:43 +08:00
|
|
|
|
2024-02-16 15:13:29 +08:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public static function tableName(): string
|
2024-02-16 15:13:29 +08:00
|
|
|
{
|
|
|
|
return 'share';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public function rules(): array
|
2024-02-16 15:13:29 +08:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['file_relative_path', 'access_code'], 'required'],
|
2024-04-07 14:34:43 +08:00
|
|
|
[['sharer_id', 'status', 'dl_count'], 'integer'],
|
2024-02-16 15:13:29 +08:00
|
|
|
[['creation_date'], 'safe'],
|
|
|
|
[['file_relative_path'], 'string', 'max' => 255],
|
|
|
|
[['access_code'], 'string', 'max' => 4],
|
|
|
|
[['sharer_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['sharer_id' => 'id']],
|
|
|
|
[['access_code'], 'required', 'on' => self::SCENARIO_UPDATE], // 在 'update' 场景中,只验证 'access_code' 字段
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public function attributeLabels(): array
|
2024-02-16 15:13:29 +08:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'share_id' => '分享ID',
|
|
|
|
'sharer_id' => '分享者ID',
|
|
|
|
'file_relative_path' => '文件位置',
|
|
|
|
'access_code' => '访问密码',
|
|
|
|
'creation_date' => '分享创建日期',
|
2024-03-04 16:26:37 +08:00
|
|
|
'status' => 'Status',
|
2024-04-07 14:34:43 +08:00
|
|
|
'dl_count' => '下载次数',
|
|
|
|
|
2024-02-16 15:13:29 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets query for [[Sharer]].
|
|
|
|
*
|
2024-03-04 15:50:51 +08:00
|
|
|
* @return ActiveQuery
|
2024-02-16 15:13:29 +08:00
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public function getSharer(): ActiveQuery
|
2024-02-16 15:13:29 +08:00
|
|
|
{
|
|
|
|
return $this->hasOne(User::class, ['id' => 'sharer_id']);
|
|
|
|
}
|
2024-04-07 14:34:43 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets query for [[DownloadLogs]].
|
|
|
|
*
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getDownloadLogs(): ActiveQuery
|
|
|
|
{
|
|
|
|
return $this->hasMany(DownloadLogs::class, ['share_id' => 'share_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2024-03-17 15:37:13 +08:00
|
|
|
public function getSharerUsername(): ?string
|
2024-02-17 16:43:39 +08:00
|
|
|
{
|
|
|
|
return $this->sharer->username;
|
|
|
|
}
|
2024-04-07 14:34:43 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setDlCountPlus1(): void
|
|
|
|
{
|
|
|
|
$this->dl_count += 1;
|
|
|
|
$this->save(true, ['dl_count']);
|
2024-04-22 14:16:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Give me the file name of the shared file/folder.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getShareFileName(): string
|
|
|
|
{
|
|
|
|
return basename($this->getAbsoluteFilePath());
|
|
|
|
}
|
2024-04-07 14:34:43 +08:00
|
|
|
|
2024-04-22 14:16:45 +08:00
|
|
|
public function getAbsoluteFilePath(): string
|
|
|
|
{
|
|
|
|
return Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $this->sharer_id . '/' . $this->file_relative_path;
|
2024-04-07 14:34:43 +08:00
|
|
|
}
|
2024-04-22 14:16:45 +08:00
|
|
|
|
2024-02-16 15:13:29 +08:00
|
|
|
}
|