Block.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace common\modules\area\models;
  3. use common\enums\BooleanEnum;
  4. use Yii;
  5. use yii\behaviors\SluggableBehavior;
  6. /**
  7. * This is the model class for table "{{%area_block}}".
  8. *
  9. * @property integer $block_id
  10. * @property string $title
  11. * @property string $slug
  12. * @property integer $type
  13. * @property integer $widget
  14. * @property integer $config
  15. * @property integer $template
  16. * @property integer $cache
  17. * @property integer $used
  18. */
  19. class Block extends \yii\db\ActiveRecord
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%area_block}}';
  27. }
  28. public function loadDefaultValues($skipIfSet = true)
  29. {
  30. parent::loadDefaultValues($skipIfSet);
  31. $this->cache = 0;
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['title'], 'required'],
  40. [['cache', 'used'], 'integer'],
  41. [['title', 'config', 'slug',"type","widget"], 'string'],
  42. ['template', 'safe']
  43. ];
  44. }
  45. public function behaviors()
  46. {
  47. $behaviors = [
  48. 'sluggable' => [
  49. 'class' => SluggableBehavior::className(),
  50. 'attribute' => 'title',
  51. "immutable"=>true,
  52. 'ensureUnique' => true
  53. ]
  54. ];
  55. return $behaviors;
  56. }
  57. public function beforeSave($insert)
  58. {
  59. if (parent::beforeSave($insert)) {
  60. $this->template = serialize($this->template);
  61. if ($insert == true) {
  62. $this->used = BooleanEnum::FLASE;
  63. return true;
  64. }
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. public function afterFind()
  71. {
  72. parent::afterFind();
  73. $this->template = unserialize($this->template);
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. public function attributeLabels()
  79. {
  80. return [
  81. 'block_id' => 'ID',
  82. 'title' => '区块名',
  83. 'slug' => '标识',
  84. 'config' => Yii::t('backend', 'Config'),
  85. 'template' => Yii::t('backend', 'Template'),
  86. 'cache' => '是否缓存',
  87. "type" => '类型',
  88. 'used' => Yii::t('backend', 'used'),
  89. 'widget' => Yii::t('backend', 'Widget'),
  90. ];
  91. }
  92. public function getWidget()
  93. {
  94. $namespace = 'common\modules\area\widgets\\';
  95. $widget = $namespace . ucfirst($this->type) . 'Widget';
  96. return $widget;
  97. }
  98. public static function widgetTypeEnum()
  99. {
  100. return [
  101. 'text' => '文本块',
  102. 'article' => '文章块'
  103. ];
  104. }
  105. }