Fields.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace addons\cms\model;
  3. class Fields extends \think\Model
  4. {
  5. // 表名
  6. protected $name = 'cms_fields';
  7. // 自动写入时间戳字段
  8. protected $autoWriteTimestamp = 'int';
  9. // 定义时间戳字段名
  10. protected $createTime = 'createtime';
  11. protected $updateTime = 'updatetime';
  12. // 追加属性
  13. protected $append = [
  14. 'content_list',
  15. 'isrequire',
  16. ];
  17. protected $type = [
  18. 'setting' => 'json',
  19. ];
  20. protected static $listFields = ['select', 'selects', 'checkbox', 'radio', 'array', 'selectpage', 'selectpages'];
  21. protected static function init()
  22. {
  23. }
  24. public function getIsrequireAttr($value, $data)
  25. {
  26. return $data['rule'] && in_array('required', explode(',', $data['rule']));
  27. }
  28. public function getDownloadListAttr($value, $data)
  29. {
  30. $config = get_addon_config('cms');
  31. $downloadtype = $config['downloadtype'];
  32. $result = [];
  33. foreach ($downloadtype as $index => $item) {
  34. $result[] = ['name' => $index, 'url' => '', 'password' => ''];
  35. }
  36. return json_encode($result);
  37. }
  38. public function getExtendHtmlAttr($value, $data)
  39. {
  40. $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
  41. if (isset($data[$matches[1]])) {
  42. return $data[$matches[1]];
  43. }
  44. }, $data['extend']);
  45. return $result;
  46. }
  47. /**
  48. * 获取字典列表字段
  49. * @return array
  50. */
  51. public static function getListFields()
  52. {
  53. return self::$listFields;
  54. }
  55. public function getContentListAttr($value, $data)
  56. {
  57. $result = \app\common\model\Config::decode($data['content'] ?? '');
  58. return $result ?: [];
  59. }
  60. public function getFilterListAttr($value, $data)
  61. {
  62. $result = \app\common\model\Config::decode($data['filterlist'] ?? '');
  63. return $result ?: [];
  64. }
  65. public static function getFieldsContentList($source, $source_id = 0)
  66. {
  67. $list = Fields::where('source', $source)
  68. ->where('source_id', $source_id)
  69. ->field('id,name,type,content')
  70. ->where('status', 'normal')
  71. ->cache(true)
  72. ->select();
  73. $fieldsList = [];
  74. $listFields = Fields::getListFields();
  75. foreach ($list as $index => $item) {
  76. if (in_array($item['type'], $listFields)) {
  77. $fieldsList[$item['name']] = $item['content_list'];
  78. }
  79. }
  80. return $fieldsList;
  81. }
  82. }