| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace UCore\DcatAdmin;
- use UCore\DcatAdmin\Show\Divider;
- use UCore\DcatAdmin\Show\Expand;
- use UCore\DcatAdmin\Traits\Options;
- use App\Module\File\Img;
- use Dcat\Admin\Show;
- use Illuminate\Support\Arr;
- class ShowHelper
- {
- use Options;
- /**
- * @var Show
- */
- public $show;
- /**
- * @var AdminController
- */
- public $controller;
- public function __construct($show, $controller)
- {
- $this->show = $show;
- $this->controller = $controller;
- $this->disableButton();
- }
- public function disableButton()
- {
- $this->show->disableEditButton();
- $this->show->disableQuickEdit();
- $this->show->disableDeleteButton();
- }
- public function field($name,$label='')
- {
- return $this->show->field($name, $label);
- }
- public function divider($name)
- {
- return $this->show->fields()->push(new Divider($this->controller->_translation('divider.' . $name)));
- }
- /**
- * 图片字段
- *
- * @param $name
- * @return Show\Field
- */
- public function fieldImg($name,$label='')
- {
- return $this->show->field($name, $label)->as(function ($pp){
- // dump($pp);
- return Img::img2imgurl($pp);
- })->image();
- }
- public function fieldTimes($name,$label='')
- {
- return $this->show->field($name, $label)->as(function ($ts){
- // dump($pp);
- return date('Y-m-d H:i:s',$ts);
- });
- }
- /**
- * 用户Id 字段
- *
- * @param $name
- * @return Expand
- */
- public function fieldUserId($name,$label='')
- {
- return $this->show->field($name, $label)->expand(\App\Module\System\AdminLazyRenderable\UserInfo::make([
- 'user_id' => $this->show->model()->$name
- ]));
- }
- /**
- * 扩展使用
- * @param $name
- * @param $make
- * @return Expand
- */
- public function fieldExpland($name, $make,$label='')
- {
- return $this->show->field($name, $label)->expand($make);
- }
- public function fieldUseing($name, array $keys,$label='')
- {
- $this->show->field($name, $label)->using($this->useing($name, $keys));
- }
- public function fieldModelCats($name,$label='',$default=0)
- {
- // dd();
- $cates = $this->show->model()->getCasts();
- $enmu = $cates[$name]??"";
- if($enmu === ''){
- throw new \Exception("$name is not a model casts");
- }
- // dump($cates,$enmu::getValueDescription());
- $values = $enmu::getValueDescription();
- $this->show->field($name, $label)->as(function ($value) use ($values, $default) {
- if (is_null($value)) {
- return $default;
- }
- if($value instanceof \BackedEnum){
- $value = $value->value();
- }
- return Arr::get($values, $value, $default);
- });
- }
- }
|