Diyform.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace addons\cms\model;
  3. use addons\cms\library\Service;
  4. use think\Db;
  5. use think\Model;
  6. /**
  7. * 自定义表单模型
  8. */
  9. class Diyform extends Model
  10. {
  11. protected $name = "cms_diyform";
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = '';
  16. protected $updateTime = '';
  17. // 追加属性
  18. protected $append = [
  19. 'url',
  20. ];
  21. protected static $config = [];
  22. protected static $tagCount = 0;
  23. protected static function init()
  24. {
  25. $config = get_addon_config('cms');
  26. self::$config = $config;
  27. }
  28. public function getUrlAttr($value, $data)
  29. {
  30. return $this->buildUrl($value, $data);
  31. }
  32. public function getFullurlAttr($value, $data)
  33. {
  34. return $this->buildUrl($value, $data, true);
  35. }
  36. private function buildUrl($value, $data, $domain = false)
  37. {
  38. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  39. $time = $data['createtime'] ?? time();
  40. $vars = [
  41. ':id' => $data['id'],
  42. ':diyname' => $diyname,
  43. ':year' => date("Y", $time),
  44. ':month' => date("m", $time),
  45. ':day' => date("d", $time)
  46. ];
  47. $suffix = static::$config['moduleurlsuffix']['diyform'] ?? static::$config['urlsuffix'];
  48. return addon_url('cms/diyform/index', $vars, $suffix, $domain);
  49. }
  50. public function getPosturlAttr($value, $data)
  51. {
  52. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  53. $time = $data['createtime'] ?? time();
  54. $vars = [
  55. ':id' => $data['id'],
  56. ':diyname' => $diyname,
  57. ':year' => date("Y", $time),
  58. ':month' => date("m", $time),
  59. ':day' => date("d", $time)
  60. ];
  61. $suffix = static::$config['moduleurlsuffix']['diyform_post'] ?? static::$config['urlsuffix'];
  62. return addon_url('cms/diyform/post', $vars, $suffix);
  63. }
  64. public function getSettingAttr($value, $data)
  65. {
  66. return is_array($value) ? $value : (array)json_decode($data['setting'], true);
  67. }
  68. public function setSettingAttr($value)
  69. {
  70. return is_array($value) ? json_encode($value) : $value;
  71. }
  72. public static function getDiyformFields($diyform_id, $values = [])
  73. {
  74. $fields = Service::getCustomFields('diyform', $diyform_id, $values, ['iscontribute' => 1]);
  75. return $fields;
  76. }
  77. public function getOrderFields()
  78. {
  79. $setting = $this->setting;
  80. $orderfields = isset($setting['orderfields']) ? $setting['orderfields'] : [];
  81. $orders = [
  82. ['name' => 'id', 'field' => 'id', 'title' => "ID"],
  83. ['name' => 'createtime', 'field' => 'createtime', 'title' => "添加时间"],
  84. ['name' => 'updatetime', 'field' => 'updatetime', 'title' => "更新时间"],
  85. ];
  86. return array_filter($orders, function ($item) use ($orderfields) {
  87. return in_array($item['name'], $orderfields);
  88. });
  89. }
  90. /**
  91. * 获取自定义表单数据列表
  92. * @param array $params
  93. * @return false|\PDOStatement|string|\think\Collection|array
  94. */
  95. public static function getDiydataList($params)
  96. {
  97. $config = get_addon_config('cms');
  98. $form = empty($params['diyform']) ? '' : $params['diyform'];
  99. $condition = empty($params['condition']) ? '' : $params['condition'];
  100. $field = empty($params['field']) ? '*' : $params['field'];
  101. $row = empty($params['row']) ? 10 : (int)$params['row'];
  102. $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
  103. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  104. $limit = empty($params['limit']) ? $row : $params['limit'];
  105. $cache = !isset($params['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$params['cache'];
  106. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  107. $paginate = !isset($params['paginate']) ? false : $params['paginate'];
  108. $cache = !$cache ? false : $cache;
  109. self::$tagCount++;
  110. $where = [];
  111. $diyform = null;
  112. $diyform = Diyform::where("id", $form)->cache($cache)->find();
  113. if (!$diyform) {
  114. return [];
  115. }
  116. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  117. $diyformModel = (new Diydata([], $diyform))->where($where)
  118. ->where($condition)
  119. ->field($field)
  120. ->orderRaw($order);
  121. if ($paginate) {
  122. $paginateArr = explode(',', $paginate);
  123. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  124. $config = [];
  125. $config['var_page'] = isset($paginateArr[2]) ? $paginateArr[2] : 'dpage' . self::$tagCount;
  126. $config['path'] = isset($paginateArr[3]) ? $paginateArr[3] : '';
  127. $config['fragment'] = isset($paginateArr[4]) ? $paginateArr[4] : '';
  128. $config['query'] = request()->get();
  129. $list = $diyformModel->paginate($listRows, (isset($paginateArr[1]) ? $paginateArr[1] : false), $config);
  130. } else {
  131. $list = $diyformModel->limit($limit)->cache($cache)->select();
  132. }
  133. return $list;
  134. }
  135. }