BannerForm.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-12-03 22:05
  7. */
  8. namespace backend\models\form;
  9. use Yii;
  10. use common\libs\Constants;
  11. use yii\web\UploadedFile;
  12. class BannerForm extends \common\models\Options
  13. {
  14. public $id;
  15. public $tips;
  16. public $sign;
  17. public $img;
  18. public $target = Constants::TARGET_BLANK;
  19. public $link;
  20. public $sort = 0;
  21. public $status = Constants::Status_Enable;
  22. public $desc;
  23. /**
  24. * @inheritdoc
  25. */
  26. public function attributeLabels()
  27. {
  28. return [
  29. 'name' => Yii::t('app', 'Sign'),
  30. 'tips' => Yii::t('app', 'Description'),
  31. 'img' => Yii::t('app', 'Image'),
  32. 'target' => Yii::t('app', 'Target'),
  33. 'link' => Yii::t('app', 'Jump Link'),
  34. 'sort' => Yii::t('app', 'Sort'),
  35. 'status' => Yii::t('app', 'Status'),
  36. 'desc' => Yii::t('app', 'Description'),
  37. ];
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['sort', 'status'], 'integer'],
  46. [['sign', 'target', 'link', 'desc'], 'string'],
  47. [['img'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, webp'],
  48. ];
  49. }
  50. public function beforeValidate()
  51. {
  52. if ($this->img !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
  53. $this->img = UploadedFile::getInstance($this, "img");
  54. }
  55. return parent::beforeValidate();
  56. }
  57. public function setAttributes($values, $safeOnly = true)
  58. {
  59. if( is_string($values) ){
  60. $banner = json_decode($values, true);
  61. $this->sign = $banner['sign'];
  62. $this->img = $banner['img'];
  63. $this->target = $banner['target'];
  64. $this->desc = $banner['desc'];
  65. $this->link = $banner['link'];
  66. $this->sort = $banner['sort'];
  67. $this->status = $banner['status'];
  68. }else{
  69. parent::setAttributes($values, $safeOnly);
  70. }
  71. }
  72. public function getValue()
  73. {
  74. return [
  75. 'sign' => $this->sign ? $this->sign : uniqid(),
  76. 'img' => $this->img,
  77. 'target' => $this->target,
  78. 'desc' => $this->desc,
  79. 'link' => $this->link,
  80. 'sort' => $this->sort,
  81. 'status' => $this->status,
  82. ];
  83. }
  84. }