FriendlyLink.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace common\models;
  9. use Yii;
  10. use common\helpers\Util;
  11. use yii\behaviors\TimestampBehavior;
  12. use yii\db\ActiveRecord;
  13. use yii\web\UploadedFile;
  14. /**
  15. * This is the model class for table "{{%friend_link}}".
  16. *
  17. * @property string $id
  18. * @property string $name
  19. * @property string $image
  20. * @property string $url
  21. * @property string $target
  22. * @property string $sort
  23. * @property integer $status
  24. * @property string $created_at
  25. * @property string $updated_at
  26. */
  27. class FriendlyLink extends ActiveRecord
  28. {
  29. const DISPLAY_YES = 1;
  30. const DISPLAY_NO = 0;
  31. public function behaviors()
  32. {
  33. return [
  34. TimestampBehavior::className(),
  35. ];
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%friendly_link}}';
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['target'], 'string'],
  51. [['sort', 'status', 'created_at', 'updated_at'], 'integer'],
  52. [['sort'], 'compare', 'compareValue' => 0, 'operator' => '>='],
  53. [['name', 'url'], 'string', 'max' => 255],
  54. [['image'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, webp'],
  55. [['name', 'url'], 'required'],
  56. ];
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => Yii::t('app', 'ID'),
  65. 'name' => Yii::t('app', 'Name'),
  66. 'image' => Yii::t('app', 'Image'),
  67. 'url' => Yii::t('app', 'Url'),
  68. 'target' => Yii::t('app', 'Target'),
  69. 'sort' => Yii::t('app', 'Sort'),
  70. 'status' => Yii::t('app', 'Is Display'),
  71. 'created_at' => Yii::t('app', 'Created At'),
  72. 'updated_at' => Yii::t('app', 'Updated At'),
  73. ];
  74. }
  75. public function beforeValidate()
  76. {
  77. if($this->image !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
  78. $this->image = UploadedFile::getInstance($this, "image");
  79. }
  80. return parent::beforeValidate();
  81. }
  82. /**
  83. * @inheritdoc
  84. */
  85. public function beforeSave($insert)
  86. {
  87. Util::handleModelSingleFileUpload($this, 'image', $insert, '@friendlylink/');
  88. return parent::beforeSave($insert);
  89. }
  90. public function beforeDelete()
  91. {
  92. if( !empty( $this->image ) ){
  93. Util::deleteThumbnails(Yii::getAlias('@frontend/web/') . str_replace(Yii::$app->params['site']['url'], '', $this->image), [], true);
  94. }
  95. return parent::beforeDelete();
  96. }
  97. public function afterFind()
  98. {
  99. /** @var $cdn \feehi\cdn\TargetAbstract $cdn */
  100. $cdn = Yii::$app->get('cdn');
  101. $this->image = $cdn->getCdnUrl($this->image);
  102. }
  103. }