ModelField.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 字段模型
  13. // +----------------------------------------------------------------------
  14. namespace addons\cms\model;
  15. use think\Model;
  16. /**
  17. * 字段模型
  18. */
  19. class ModelField extends Model
  20. {
  21. protected $name = 'cms_field';
  22. protected $autoWriteTimestamp = true;
  23. public function getSettingAttr($value, $data)
  24. {
  25. return is_array($value) ? $value : (array) json_decode($data['setting'], true);
  26. }
  27. public function setSettingAttr($value)
  28. {
  29. return is_array($value) ? json_encode($value) : $value;
  30. }
  31. public function getExtendHtmlAttr($value, $data)
  32. {
  33. $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
  34. if (isset($data[$matches[1]])) {
  35. return $data[$matches[1]];
  36. }
  37. }, $data['extend']);
  38. return $result;
  39. }
  40. //生成模型字段缓存
  41. public function model_field_cache()
  42. {
  43. $cache = [];
  44. $modelList = Models::where('module', 'cms')->select();
  45. foreach ($modelList as $v) {
  46. $data = ModelField::where(["modelid" => $v['id'], "status" => 1])->order('listorder DESC, id DESC')->select()->toArray();
  47. $fieldList = [];
  48. foreach ($data as $rs) {
  49. if (!empty($rs['setting'])) {
  50. $rs = array_merge($rs, $rs['setting']);
  51. }
  52. $fieldList[$rs['name']] = $rs;
  53. }
  54. $cache[$v['id']] = $fieldList;
  55. }
  56. cms_cache('ModelField', $cache);
  57. return $cache;
  58. }
  59. }