Diydata.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace addons\cms\model;
  3. use think\Model;
  4. class Diydata extends Model
  5. {
  6. // 开启自动写入时间戳字段
  7. protected $autoWriteTimestamp = 'int';
  8. // 定义时间戳字段名
  9. protected $createTime = 'createtime';
  10. protected $updateTime = 'updatetime';
  11. public $diyform = null;
  12. protected static $config = [];
  13. protected static function init()
  14. {
  15. $config = get_addon_config('cms');
  16. self::$config = $config;
  17. }
  18. public function __construct($data = [], $diyform = null)
  19. {
  20. static $diyformModel;
  21. if (is_null($diyformModel)) {
  22. $diyformModel = $diyform;
  23. }
  24. $this->diyform = $diyformModel;
  25. $this->name = $diyformModel['table'];
  26. return parent::__construct($data);
  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($this->diyform->diyname) && $this->diyform->diyname ? $this->diyform->diyname : 'all';
  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_show'] ?? static::$config['urlsuffix'];
  48. return addon_url('cms/diyform/show', $vars, $suffix, $domain);
  49. }
  50. public function getStatusList()
  51. {
  52. return ['normal' => __('Normal'), 'hidden' => __('Hidden'), 'rejected' => __('Rejected')];
  53. }
  54. public function getStatusTextAttr($value, $data)
  55. {
  56. $value = $value ? $value : $data['status'];
  57. $list = $this->getStatusList();
  58. return isset($list[$value]) ? $list[$value] : '';
  59. }
  60. }