Completed
Branch master (f1d822)
by tac
03:30 queued 01:02
created

GridController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 69
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Tacone\Bees\Demo\Controllers;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Tacone\Bees\Demo\Models\Article;
7
use Tacone\Bees\Widget\Grid;
8
use View;
9
10
class GridController extends DemoController
11
{
12
    public function __construct()
13
    {
14
        parent::__construct();
15
        \View::share('sidebarText', '
16
        <div class="alert alert-info">
17
            <strong>Fun fact:</strong> if you just <code>return $grid->toArray();</code>
18
            from your controller method, you can use this widget like a mini JSON
19
            API &hearts;
20
        </div>
21
        ');
22
    }
23
24
    /**
25
     * A very simple grid.
26
     */
27
    public function anyIndex()
28
    {
29
        $grid = new Grid(Article::with('categories')->with('author'));
30
        $grid->text('id');
31
        $grid->text('title');
32
        $grid->text('author.fullname');
33
        $grid->text('categories', 'In category')->value(function ($v) {
34
            if ($v instanceof Collection) {
35
                return implode(', ', $v->lists('name'));
36
            }
37
        });
38
        $grid->select('public')->options([
39
            1 => 'Yes',
40
        ]);
41
        $grid->start->before[] = '<p><em>This is a very simple grid</em></p>';
42
43
        return View::make('bees::demo.grid-automatic', compact('grid'));
44
    }
45
//        $grid->select('Publish in')->options([
46
//            'home' => 'Frontpage',
47
//            'blog' => 'Blog',
48
//            'magazine' => 'Magazine',
49
//            'Other destinations' => [
50
//                'newsletter' => 'Newsletter',
51
//                'sponsor' => 'Main sponsor website',
52
//                'drafts' => 'Draft box',
53
//            ],
54
//        ]);
55
    public function anyCallback()
56
    {
57
        $grid = new Grid(new Article());
58
        $grid->text('id');
59
        $grid->text('title');
60
        $grid->text('author.firstname');
61
        $grid->text('author.lastname');
62
        $grid->text('categories.0.name', 'In category');
63
        $grid->start->before[] = '<p><em>Customized with a row callback</em></p>';
64
        $colors = ['success', 'warning', 'info', 'danger'];
65
        $counter = 0;
66
        $grid->prototype->output(function ($row) use (&$counter, $colors) {
67
            $row->class($colors[$counter]);
68
            if ($colors[$counter] == 'danger') {
69
                $row->end->after[] = '<tr><td colspan="1000" class="text-danger danger">
70
Warning: this item has been rejected by the moderators
71
</td></tr>';
72
            }
73
            $counter = ($counter + 1) % count($colors);
74
        });
75
76
        return View::make('bees::demo.grid-automatic', compact('grid'));
77
    }
78
}
79