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

DemoController::__construct()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
rs 9.4286
c 1
b 0
f 0
cc 3
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Tacone\Bees\Demo\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use DB;
7
use Illuminate\Database\QueryException;
8
use Schema;
9
use Tacone\Bees\Demo\Documenter;
10
use Tacone\Bees\Demo\Models\Article;
11
use View;
12
13
class DemoController extends Controller
14
{
15
    public $views = [];
16
    public $widget = null;
17
18
    public function __construct()
19
    {
20
        error_reporting(-1);
21
        $me = $this;
22
        app()['events']->listen('composing:*', function ($view) use ($me) {
23
            if (!empty($view['form'])) {
24
                $this->widget = $view['form'];
25
            }
26
            if (!empty($view['grid'])) {
27
                $this->widget = $view['grid'];
28
            }
29
            $me->views[] = $view->getPath();
30
        });
31
        \View::share('demo', $this);
32
    }
33
34
    public function anyIndex()
35
    {
36
        try {
37
            Article::find(1);
38
        } catch (QueryException $e) {
39
            return '
40
         <div style="text-align: center;margin-top: 100px;">
41
<p><strong>Database error!</strong></p>
42
<p>&nbsp;</p>
43
44
<ol style="text-align: left;width: 300px; display: inline-block;">
45
    <li>create a database that matches your config files</li>
46
    <li><a href="/demo/setup">seed the DEMO database</a></li>
47
</ol>
48
49
</div>
50
            ';
51
        }
52
53
        return \Redirect::action('\Tacone\Bees\Demo\Controllers\FormController@anyIndex');
54
    }
55
56
    public function source()
57
    {
58
        list($controller, $method) = explode('@', \Route::current()->getAction()['controller']);
59
60
        $source = Documenter::showMethod($controller, [$method]);
61
        foreach ($this->views as $v) {
62
            if (!str_is('*/layout/*', $v)) {
63
                $source .= Documenter::showCode($v);
64
            }
65
        }
66
67
        return $source;
68
    }
69
70
71
    /**
72
     * Wipe out the demo data, so you can try out the demo
73
     * with an empty database.
74
     */
75
    public function getWipe()
76
    {
77
        DB::statement('SET foreign_key_checks=0');
78
        DB::table('demo_users')->truncate();
79
        DB::table('demo_articles')->truncate();
80
        DB::table('demo_article_detail')->truncate();
81
        DB::table('demo_comments')->truncate();
82
        DB::table('demo_categories')->truncate();
83
        DB::table('demo_article_category')->truncate();
84
        DB::statement('SET foreign_key_checks=1');
85
    }
86
87
    /**
88
     * Seed the data for the demo.
89
     */
90
    public function getSetup()
91
    {
92
        Schema::dropIfExists('demo_users');
93
        Schema::dropIfExists('demo_articles');
94
        Schema::dropIfExists('demo_article_detail');
95
        Schema::dropIfExists('demo_comments');
96
        Schema::dropIfExists('demo_categories');
97
        Schema::dropIfExists('demo_article_category');
98
99
        //create all tables
100
        Schema::table('demo_users', function ($table) {
101
            $table->create();
102
            $table->increments('id');
103
            $table->string('firstname', 100);
104
            $table->string('lastname', 100);
105
            $table->timestamps();
106
        });
107
        Schema::table('demo_articles', function ($table) {
108
            $table->create();
109
            $table->increments('id');
110
            $table->integer('author_id')->unsigned();
111
            $table->string('title', 200);
112
            $table->text('body');
113
            $table->string('photo', 200);
114
            $table->boolean('public');
115
            $table->timestamp('publication_date');
116
            $table->timestamps();
117
        });
118
        Schema::table('demo_article_detail', function ($table) {
119
            $table->create();
120
            $table->increments('id');
121
            $table->integer('article_id')->unsigned();
122
            $table->text('note');
123
            $table->string('note_tags', 200);
124
        });
125
        Schema::table('demo_comments', function ($table) {
126
            $table->create();
127
            $table->increments('id');
128
            $table->integer('user_id')->unsigned();
129
            $table->integer('article_id')->unsigned();
130
            $table->text('comment');
131
            $table->timestamps();
132
        });
133
        Schema::table('demo_categories', function ($table) {
134
            $table->create();
135
            $table->increments('id');
136
            $table->integer('parent_id')->unsigned();
137
            $table->string('name', 100);
138
            $table->timestamps();
139
        });
140
        Schema::table('demo_article_category', function ($table) {
141
            $table->create();
142
            $table->integer('article_id')->unsigned();
143
            $table->integer('category_id')->unsigned();
144
            $table->timestamps();
145
        });
146
147
        //populate all tables
148
        $users = DB::table('demo_users');
149
        $users->insert(array('firstname' => 'Jhon', 'lastname' => 'Doe'));
150
        $users->insert(array('firstname' => 'Jane', 'lastname' => 'Doe'));
151
152
        $categories = DB::table('demo_categories');
153
        for ($i = 1; $i <= 5; ++$i) {
154
            $categories->insert(array(
155
                    'name' => 'Category '.$i,
156
                )
157
            );
158
        }
159
        $articles = DB::table('demo_articles');
160
        for ($i = 1; $i <= 20; ++$i) {
161
            $articles->insert(array(
162
                    'author_id' => rand(1, 2),
163
                    'title' => 'Article '.$i,
164
                    'body' => 'Body of article '.$i,
165
                    'publication_date' => date('Y-m-d'),
166
                    'public' => true,
167
                )
168
            );
169
        }
170
        $categories = DB::table('demo_article_category');
171
        $categories->insert(array('article_id' => 1, 'category_id' => 1));
172
        $categories->insert(array('article_id' => 1, 'category_id' => 2));
173
        $categories->insert(array('article_id' => 20, 'category_id' => 2));
174
        $categories->insert(array('article_id' => 20, 'category_id' => 3));
175
176
        $comments = DB::table('demo_comments');
177
        $comments->insert(array(
178
                'user_id' => 1,
179
                'article_id' => 2,
180
                'comment' => 'Comment for Article 2',
181
            )
182
        );
183
184
        $files = glob(public_path().'/uploads/demo/*');
185
        foreach ($files as $file) {
186
            if (is_file($file)) {
187
                @unlink($file);
188
            }
189
        }
190
191
        echo 'All set!';
192
    }
193
194
    public function activeLink($action, $title = null, $parameters = array(), $attributes = array())
195
    {
196
        $same = $action == \Route::currentRouteAction();
197
        $same = $same && array_values(\Route::getCurrentRoute()->parameters()) == array_values($parameters);
198
        $active = $same ? ' class="active"' : '';
199
200
        return "<li$active>"
201
        .link_to_action($action, $title, $parameters, $attributes)
202
        .'</li>';
203
    }
204
}
205