Column.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\model\wwh;
  3. use think\Exception;
  4. use think\Model;
  5. class Column extends Model
  6. {
  7. // 表名
  8. protected $name = 'wwh_column';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'type_text',
  18. 'status_text',
  19. 'classify_text',
  20. 'url'
  21. ];
  22. public function getUrlAttr($value, $data)
  23. {
  24. $diyname = $data['diyname'] ? $data['diyname'] : $data['id'];
  25. return isset($data['type']) && isset($data['outlink']) && $data['type'] == 'link' ? $data['outlink'] : addon_url('wwh/column/index', [':id' => $data['id'], ':diyname' => $diyname],'html');
  26. }
  27. public static function getTypeList()
  28. {
  29. return ['list' => __('List'), 'link' => __('Link')];
  30. }
  31. public static function getStatusList()
  32. {
  33. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  34. }
  35. public function getClassifyList()
  36. {
  37. $config = get_addon_config('wwh');
  38. return $config['classify'];
  39. }
  40. public function getTypeTextAttr($value, $data)
  41. {
  42. $value = $value ? $value : $data['type'];
  43. $list = $this->getTypeList();
  44. return isset($list[$value]) ? $list[$value] : '';
  45. }
  46. public function getStatusTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : $data['status'];
  49. $list = $this->getStatusList();
  50. return isset($list[$value]) ? $list[$value] : '';
  51. }
  52. public function getClassifyTextAttr($value, $data)
  53. {
  54. $value = $value ? $value : $data['classify'];
  55. $valueArr = $value ? explode(',', $value) : [];
  56. $list = $this->getClassifyList();
  57. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  58. }
  59. public static function getChildrenIds($id, $withself = false)
  60. {
  61. static $tree;
  62. if (!$tree) {
  63. $tree = \fast\Tree::instance();
  64. $tree->init(collection(column::order('weigh desc,id desc')->field('id,parent_id,name,type,classify,diyname,status')->select())->toArray(), 'parent_id');
  65. }
  66. $childIds = $tree->getChildrenIds($id, $withself);
  67. return $childIds;
  68. }
  69. protected static function init()
  70. {
  71. self::afterInsert(function ($row) {
  72. $pk = $row->getPk();
  73. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  74. });
  75. self::beforeInsert(function ($row) {
  76. $parent_ids = 0;
  77. if ($row->parent_id != 0) {
  78. $parent_ids = self::getParentIds($row->parent_id);
  79. $parent_ids .= "," . $row->parent_id;
  80. }
  81. $row->parent_ids = $parent_ids;
  82. });
  83. self::beforeUpdate(function ($row) {
  84. if ($row['parent_id']) {
  85. $childrenIds = self::getChildrenIds($row['id'], true);
  86. if (in_array($row['parent_id'], $childrenIds)) {
  87. throw new Exception("上级栏目不能是其自身或子栏目");
  88. }
  89. }
  90. $changeData = $row->getChangedData();
  91. if (isset($changeData['parent_id'])) {
  92. $row->parent_ids = self::getParentIds($row->parent_id) . ',' . $row->parent_id;
  93. }
  94. });
  95. self::beforeWrite(function ($row) {
  96. //在更新之前对数组进行处理
  97. foreach ($row->getData() as $k => $value) {
  98. if (is_array($value) && is_array(reset($value))) {
  99. $value = json_encode(self::getArrayData($value), JSON_UNESCAPED_UNICODE);
  100. } else {
  101. $value = is_array($value) ? implode(',', $value) : $value;
  102. }
  103. $row->setAttr($k, $value);
  104. }
  105. });
  106. }
  107. public static function getParentIds($parent_id) {
  108. $row = self::get($parent_id);
  109. if ( !$row ) {
  110. return 0;
  111. }
  112. return $row->parent_ids;
  113. }
  114. public function template()
  115. {
  116. return $this->belongsTo('app\admin\model\wwh\Template', 'template_id', 'id', [], 'LEFT')->setEagerlyType(0);
  117. }
  118. }