Options.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\libs\Constants;
  11. use common\helpers\FileDependencyHelper;
  12. use yii\db\ActiveRecord;
  13. use yii\helpers\FileHelper;
  14. use yii\web\UploadedFile;
  15. /**
  16. * This is the model class for table "{{%options}}".
  17. *
  18. * @property integer $id
  19. * @property integer $type
  20. * @property string $name
  21. * @property string $value
  22. * @property integer $input_type
  23. * @property string $tips
  24. * @property integer $autoload
  25. * @property integer $sort
  26. */
  27. class Options extends ActiveRecord
  28. {
  29. const TYPE_SYSTEM = 0;
  30. const TYPE_CUSTOM = 1;
  31. const TYPE_BANNER = 2;
  32. const TYPE_AD = 3;
  33. const CUSTOM_AUTOLOAD_NO = 0;
  34. const CUSTOM_AUTOLOAD_YES = 1;
  35. const CACHE_DEPENDENCY_TYPE_SYSTEM_FILE_NAME = "options_type_system.txt";
  36. /**
  37. * @inheritdoc
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%options}}';
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['type', 'input_type', 'autoload', 'sort'], 'integer'],
  50. [['name', 'input_type', 'autoload'], 'required'],
  51. [['name'], 'unique'],
  52. [
  53. ['name'],
  54. 'match',
  55. 'pattern' => '/^[a-zA-Z][0-9_]*/',
  56. 'message' => Yii::t('app', 'Must begin with alphabet and can only includes alphabet,_,and number')
  57. ],
  58. [['value'], 'string'],
  59. [['value'], 'default', 'value' => ''],
  60. [['name', 'tips'], 'string', 'max' => 255],
  61. [['sort'], 'default', 'value' => 0],
  62. ];
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public function attributeLabels()
  68. {
  69. return [
  70. 'id' => Yii::t('app', 'ID'),
  71. 'type' => Yii::t('app', 'Type'),
  72. 'name' => Yii::t('app', 'Name'),
  73. 'value' => Yii::t('app', 'Value'),
  74. 'input_type' => Yii::t('app', 'Input Type'),
  75. 'tips' => Yii::t('app', 'Tips'),
  76. 'autoload' => Yii::t('app', 'Autoload'),
  77. 'sort' => Yii::t('app', 'Sort'),
  78. ];
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function getNames()
  84. {
  85. return array_keys($this->attributeLabels());
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function afterSave($insert, $changedAttributes)
  91. {
  92. $object = Yii::createObject([
  93. 'class' => FileDependencyHelper::className(),
  94. 'fileName' => self::CACHE_DEPENDENCY_TYPE_SYSTEM_FILE_NAME,
  95. ]);
  96. $object->updateFile();
  97. parent::afterSave($insert, $changedAttributes);
  98. }
  99. public function beforeSave($insert)
  100. {
  101. if( !$insert ){
  102. if( $this->input_type == Constants::INPUT_IMG ) {
  103. $temp = explode('\\', self::className());
  104. $modelName = end( $temp );
  105. $key = "{$modelName}[{$this->id}][value]";
  106. $upload = UploadedFile::getInstanceByName($key);
  107. $old = Options::findOne($this->id);
  108. /* @var $cdn \feehi\cdn\TargetInterface */
  109. $cdn = Yii::$app->get('cdn');
  110. if($upload !== null){
  111. $uploadPath = Yii::getAlias('@uploads/setting/custom-setting/');
  112. if (! FileHelper::createDirectory($uploadPath)) {
  113. $this->addError($key, "Create directory failed " . $uploadPath);
  114. return false;
  115. }
  116. $fullName = $uploadPath . date('YmdHis') . '_' . uniqid() . '.' . $upload->getExtension();
  117. if (! $upload->saveAs($fullName)) {
  118. $this->addError($key, Yii::t('app', 'Upload {attribute} error: ' . $upload->error, ['attribute' => Yii::t('app', 'Picture')]) . ': ' . $fullName);
  119. return false;
  120. }
  121. $this->value = str_replace(Yii::getAlias('@frontend/web'), '', $fullName);
  122. $cdn->upload($fullName, $this->value);
  123. if( $old !== null ){
  124. $file = Yii::getAlias('@frontend/web') . $old->value;
  125. if( file_exists($file) && is_file($file) ) unlink($file);
  126. if( $cdn->exists($old->value) ) $cdn->delete($old->value);
  127. }
  128. }else{
  129. if( $this->value !== '' ){
  130. $file = Yii::getAlias('@frontend/web') . $old->value;
  131. if( file_exists($file) && is_file($file) ) unlink($file);
  132. if( $cdn->exists($old->value) ) $cdn->delete($old->value);
  133. $this->value = '';
  134. }else {
  135. $this->value = $old->value;
  136. }
  137. }
  138. }
  139. }
  140. return parent::beforeSave($insert);
  141. }
  142. }