Completed
Push — master ( 71797f...8f0aef )
by Song
02:24
created

src/Grid/Displayers/Modal.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid\Simple;
7
use Illuminate\Contracts\Support\Renderable;
8
9
class Modal extends AbstractDisplayer
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $renderable;
15
16
    /**
17
     * @param int $multiple
18
     *
19
     * @return string
20
     */
21
    protected function getLoadUrl()
22
    {
23
        $renderable = str_replace('\\', '_', $this->renderable);
24
25
        return route('admin.handle-renderable', compact('renderable'));
26
    }
27
28
    protected function addRenderableModalScript()
29
    {
30
        $script = <<<SCRIPT
31
;(function () {
32
    var modal = $('.grid-modal');
33
    var modalBody = modal.find('.modal-body');
34
    
35
    
36
    var load = function (url) {
37
    
38
        modalBody.html("<div class='loading text-center' style='height:200px;'>\
39
                <i class='fa fa-spinner fa-pulse fa-3x fa-fw' style='margin-top: 80px;'></i>\
40
            </div>");
41
    
42
        $.get(url, function (data) {
43
            modalBody.html(data);
44
        });
45
    };
46
47
    modal.on('show.bs.modal', function (e) {
48
        var key = $(e.relatedTarget).data('key');        
49
        load('{$this->getLoadUrl()}'+'&key='+key);
50
    }).on('click', '.page-item a, .filter-box a', function (e) {
51
        load($(this).attr('href'));
52
        e.preventDefault();
53
    }).on('submit', '.box-header form', function (e) {  
54
        load($(this).attr('action')+'&'+$(this).serialize());
55
        return false;
56
    });
57
})();
58
SCRIPT;
59
60
        Admin::script($script);
61
    }
62
63
    protected function addGridStyle()
64
    {
65
        $style = <<<STYLE
66
.box.grid-box {
67
    box-shadow: none;
68
    border-top: none;
69
}
70
71
.grid-box .box-header:first-child {
72
    display: none;
73
}
74
STYLE;
75
76
        Admin::style($style);
77
    }
78
79
    public function display($callback = null)
80
    {
81
        if (func_num_args() == 2) {
82
            list($title, $callback) = func_get_args();
83
        } elseif (func_num_args() == 1) {
84
            $title = $this->trans('title');
85
        }
86
87
        if (is_subclass_of($callback, Renderable::class)) {
88
            $html = '';
89
            $this->renderable = $callback;
90
            $this->addRenderableModalScript();
91
92
            if (is_subclass_of($callback, Simple::class)) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Encore\Admin\Grid\Simple::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
93
                $this->addGridStyle();
94
            }
95
96
        } else {
97
            $callback = $callback->bindTo($this->row);
98
            $html = call_user_func_array($callback, [$this->row]);
99
        }
100
101
        $key = $this->getKey().'-'.str_replace('.', '_', $this->getColumn()->getName());
102
103
        return <<<EOT
104
<span data-toggle="modal" data-target="#grid-modal-{$key}" data-key="{$this->getKey()}">
105
   <a href="javascript:void(0)"><i class="fa fa-clone"></i>&nbsp;&nbsp;{$this->value}</a>
106
</span>
107
108
<div class="modal grid-modal fade" id="grid-modal-{$key}" tabindex="-1" role="dialog">
109
  <div class="modal-dialog modal-lg" role="document">
110
    <div class="modal-content" style="border-radius: 5px;">
111
      <div class="modal-header">
112
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
113
        <h4 class="modal-title">{$title}</h4>
114
      </div>
115
      <div class="modal-body">
116
        {$html}
117
      </div>
118
    </div>
119
  </div>
120
</div>
121
122
EOT;
123
    }
124
}
125