AdForm.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-12-05 12:47
  7. */
  8. namespace backend\models\form;
  9. use Yii;
  10. use common\helpers\Util;
  11. use common\libs\Constants;
  12. use yii\web\UploadedFile;
  13. class AdForm extends \common\models\Options
  14. {
  15. /** @var string advertisement content. if type is image, this will be the image url. */
  16. public $ad;
  17. /** @var string click advertisement redirect url */
  18. public $link;
  19. /** @var string advertisement description, oftens be remark info */
  20. public $desc;
  21. /** @var string click advertisement open method */
  22. public $target = Constants::TARGET_BLANK;
  23. public $created_at;
  24. public $updated_at;
  25. /** @var string $advertisement store file path */
  26. private $advertisementFilePath = '@uploads/setting/ad/';
  27. /**
  28. * @inheritdoc
  29. */
  30. public function attributeLabels()
  31. {
  32. return [
  33. 'name' => Yii::t('app', 'Sign'),
  34. 'input_type' => Yii::t('app', 'Ad Type'),
  35. 'tips' => Yii::t('app', 'Description'),
  36. 'ad' => Yii::t('app', 'Ad'),
  37. 'link' => Yii::t('app', 'Jump Link'),
  38. 'desc' => Yii::t('app', 'Ad Explain'),
  39. 'autoload' => Yii::t('app', 'Status'),
  40. 'sort' => Yii::t('app', 'Sort'),
  41. 'target' => Yii::t('app', 'Target'),
  42. 'created_at' => Yii::t('app', 'Created At'),
  43. 'updated_at' => Yii::t('app', 'Updated At'),
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['name'], 'unique'],
  53. [
  54. ['name'],
  55. 'match',
  56. 'pattern' => '/^[a-zA-Z][0-9_]*/',
  57. 'message' => Yii::t('app', 'Must begin with alphabet and can only includes alphabet,_,and number')
  58. ],
  59. [['name', 'tips', 'input_type'], 'required'],
  60. [['sort', 'autoload'], 'integer'],
  61. [[ 'link', 'target', 'desc'], 'string'],
  62. [['ad'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, webp'],
  63. ];
  64. }
  65. public function beforeValidate()
  66. {
  67. if( $this->type === Constants::AD_IMG ) {;
  68. if ($this->ad !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
  69. $this->ad = UploadedFile::getInstance($this, "ad");
  70. }
  71. }
  72. return parent::beforeValidate();
  73. }
  74. public function beforeSave($insert)
  75. {
  76. $this->type = self::TYPE_AD;
  77. if( $this->input_type == Constants::AD_TEXT ){
  78. $oldInput = $this->getOldAttribute('input_type');
  79. if( $oldInput != Constants::AD_TEXT ){//delete origin advertisement file
  80. $text = $this->ad;
  81. Util::handleModelSingleFileUploadAbnormal($this, 'ad', $this->advertisementFilePath, $this->getOldAttribute('ad'), ['deleteOldFile'=>true]);
  82. $this->ad = $text;
  83. }
  84. }else {
  85. Util::handleModelSingleFileUploadAbnormal($this, 'ad', $this->advertisementFilePath, $this->getOldAttribute('ad'));
  86. }
  87. $value = [
  88. 'ad' => $this->ad,
  89. 'link' => $this->link,
  90. 'target' => $this->target,
  91. 'desc' => $this->desc,
  92. 'created_at' => $this->getIsNewRecord() ? time() : $this->created_at,
  93. 'updated_at' => time(),
  94. ];
  95. $this->value = json_encode($value);
  96. return parent::beforeSave($insert);
  97. }
  98. public function afterFind()
  99. {
  100. $value = json_decode($this->value);
  101. if( $this->input_type !== Constants::AD_TEXT){
  102. /** @var $cdn \feehi\cdn\TargetAbstract */
  103. $cdn = Yii::$app->get('cdn');
  104. $this->ad = $cdn->getCdnUrl($value->ad);
  105. }else{
  106. $this->ad = $value->ad;
  107. }
  108. $this->link = $value->link;
  109. $this->desc = $value->desc;
  110. $this->target = $value->target;
  111. $this->updated_at = $value->updated_at;
  112. $this->created_at = $value->created_at;
  113. $this->setOldAttributes([
  114. 'id' => $this->id,
  115. 'name' => $this->name,
  116. 'value' => $this->value,
  117. 'input_type' => $this->input_type,
  118. 'autoload' => $this->autoload,
  119. 'tips' => $this->tips,
  120. 'sort' => $this->sort,
  121. 'ad' => $value->ad,
  122. 'link' => $value->link,
  123. 'desc' => $value->desc,
  124. 'target' => $value->target,
  125. 'created_at' => $value->created_at,
  126. 'updated_at' => $value->updated_at,
  127. ]);
  128. parent::afterFind();
  129. }
  130. public function afterDelete()
  131. {
  132. if ($this->input_type != Constants::AD_TEXT) {
  133. $file = Yii::getAlias('@frontend/web') . $this->ad;
  134. if (file_exists($file) && is_file($file)) unlink($file);
  135. }
  136. return parent::beforeDelete();
  137. }
  138. }