2022-09-21 11:59:53 +08:00

283 lines
6.8 KiB
PHP

<?php
namespace Encore\Admin\Grid\Displayers;
use Encore\Admin\Admin;
class Actions extends AbstractDisplayer
{
/**
* @var array
*/
protected $appends = [];
/**
* @var array
*/
protected $prepends = [];
/**
* Default actions.
*
* @var array
*/
protected $actions = ['view', 'edit', 'delete'];
/**
* @var string
*/
protected $resource;
/**
* Append a action.
*
* @param $action
*
* @return $this
*/
public function append($action)
{
array_push($this->appends, $action);
return $this;
}
/**
* Prepend a action.
*
* @param $action
*
* @return $this
*/
public function prepend($action)
{
array_unshift($this->prepends, $action);
return $this;
}
/**
* Disable view action.
*
* @return $this
*/
public function disableView(bool $disable = true)
{
if ($disable) {
array_delete($this->actions, 'view');
} elseif (!in_array('view', $this->actions)) {
array_push($this->actions, 'view');
}
return $this;
}
/**
* Disable delete.
*
* @return $this.
*/
public function disableDelete(bool $disable = true)
{
if ($disable) {
array_delete($this->actions, 'delete');
} elseif (!in_array('delete', $this->actions)) {
array_push($this->actions, 'delete');
}
return $this;
}
/**
* Disable edit.
*
* @return $this.
*/
public function disableEdit(bool $disable = true)
{
if ($disable) {
array_delete($this->actions, 'edit');
} elseif (!in_array('edit', $this->actions)) {
array_push($this->actions, 'edit');
}
return $this;
}
/**
* Set resource of current resource.
*
* @param $resource
*
* @return $this
*/
public function setResource($resource)
{
$this->resource = $resource;
return $this;
}
/**
* Get resource of current resource.
*
* @return string
*/
public function getResource()
{
return $this->resource ?: parent::getResource();
}
/**
* {@inheritdoc}
*/
public function display($callbacks = [])
{
if (!empty($callbacks)) {
foreach ($callbacks as $call) {
if ($call instanceof \Closure) {
$call->call($this, $this);
}
}
}
$actions = $this->prepends;
foreach ($this->actions as $action) {
$method = 'render'.ucfirst($action);
array_push($actions, $this->{$method}());
}
$actions = array_merge($actions, $this->appends);
$actions_str=implode('', $actions);
return <<<EOT
<div class="btn-group">
$actions_str
</div>
EOT;
}
/**
* Render view action.
*
* @return string
*/
protected function renderView()
{
return <<<EOT
<a class="btn btn-default btn-xs" href="{$this->getResource()}/{$this->getKey()}">
<i class="fa fa-eye"></i>
<span class="hidden-xs">详情</span>
</a>
EOT;
}
/**
* Render edit action.
*
* @return string
*/
protected function renderEdit()
{
return <<<EOT
<a class="btn btn-primary btn-xs" href="{$this->getResource()}/{$this->getKey()}/edit">
<i class="fa fa-edit"></i>
<span class="hidden-xs">编辑</span>
</a>
EOT;
}
protected function resume_checkout()
{
$tip = trans('admin.delete_person_resume_confirm');
$resume_checkout = "<label><input type='checkbox' class='resume_checkout' name='resume_checkout' style='width: 18px;height: 13px;position: relative;top:2px;'>{$tip}</label>";
return $resume_checkout;
}
/**
* Render delete action.
*
* @return string
*/
protected function renderDelete()
{
$title = '';
$url_pams = '';
if(strstr($this->getResource(),'firm/company') && !(strstr($this->getResource(),'firm/companyimg'))&& !(strstr($this->getResource(),'firm/companysetmeal'))) {
$deleteConfirm = trans('admin.delete_company_confirm');
}elseif (strstr($this->getResource(),'personal/members')) {
$deleteConfirm = trans('admin.delete_person_confirm');
} elseif (strstr($this->getResource(),'recycle/index')){
$scope = array_get(request()->all(),'scope',1);
$url_pams='?scope='.$scope;
$deleteConfirm = trans('admin.delete_confirm');
}elseif (strstr($this->getResource(),'personal/resume')){
$deleteConfirm = trans('admin.delete_confirm');
$title = $this->resume_checkout();
} else {
$deleteConfirm = trans('admin.delete_confirm');
}
$confirm = trans('admin.confirm');
$cancel = trans('admin.cancel');
$script = <<<SCRIPT
function render_action_delete() {
$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {
var id = $(this).data('id');
swal({
title: "$deleteConfirm",
html: "$title",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "$confirm",
showLoaderOnConfirm: true,
cancelButtonText: "$cancel",
preConfirm: function() {
var is_resume=0;
if($('.resume_checkout').is(':checked')){
var is_resume=1;
}
return new Promise(function(resolve) {
$.ajax({
method: 'post',
url: '{$this->getResource()}/' + id +'$url_pams'+'?is_resume='+is_resume,
data: {
_method:'delete',
_token:LA.token,
},
success: function (data) {
$.pjax.reload('#pjax-container');
resolve(data);
}
});
});
}
}).then(function(result) {
var data = result.value;
if (typeof data === 'object') {
if (data.status) {
swal(data.message, '', 'success');
} else {
swal(data.message, '', 'error');
}
}
});
});
}
render_action_delete();
SCRIPT;
Admin::script($script);
return <<<EOT
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="btn btn-danger btn-xs {$this->grid->getGridRowName()}-delete">
<i class="fa fa-trash"></i>
<span class="hidden-xs">删除</span>
</a>
EOT;
}
}