-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
298 lines (258 loc) · 8.1 KB
/
Controller.php
File metadata and controls
298 lines (258 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace Module\SampleName;
use Sydes\AdminMenu;
use Sydes\Route;
class Controller
{
public static function routes(Route $r)
{
$r->get('/', 'SampleName@index');
$r->get('/admin/sample', 'SampleName@index');
$r->get('/admin/sample/add', 'SampleName@create');
$r->get('/admin/sample/another', 'SampleName@myMethod');
$r->get('/item/{id:[0-9]+}', 'SampleName@item');
$r->get('/string.txt', 'SampleName@textString');
$r->get('/html', 'SampleName@asHtml');
$r->get('/view', 'SampleName@view');
$r->get('/api.json', 'SampleName@forAjax');
$r->get('/null', 'SampleName@returnNull');
$r->get('/export', 'SampleName@export');
$r->get('/download', 'SampleName@download');
$r->get('/not-found', 'SampleName@notFound');
$r->get('/forbidden', 'SampleName@forbidden');
$r->get('/error', 'SampleName@error');
$r->get('/moved', 'SampleName@moved');
$r->get('/back', 'SampleName@back');
$r->get('/ajax-notify', 'SampleName@ajaxNotify');
$r->get('/ajax-alert', 'SampleName@ajaxAlert');
$r->get('/update', 'SampleName@notifyAfterRedirect');
$r->get('/ajax-update', 'SampleName@notifyAfterRedirect');
$r->get('/save', 'SampleName@alertAfterRedirect');
$r->get('/ajax-save', 'SampleName@alertAfterRedirect');
$r->get('/ajax-random', 'SampleName@random');
$r->get('/sub-module', 'SampleName/SubModule@method');
$r->get('/ajax-modal', 'SampleName@modal');
$r->post('/csrf', 'SampleName@csrf');
}
public function __construct()
{
// Define properties and services for every method
}
public function install(AdminMenu $menu)
{
// Create tables, if needed
$menu->addGroup('sample', [
'title' => 'menu_sample',
'icon' => 'star'
], 120)
->addItem('sample/page', [
'title' => 'sample_page',
'url' => '/admin/sample',
'quick_add' => true,
], 10)
->addItem('sample/other', [
'title' => 'another_page',
'url' => '/admin/sample/another'
], 20);
}
public function uninstall(AdminMenu $menu)
{
$menu->removeGroup('sample');
// Remove tables and config, if used
}
public function index()
{
$links = [
'/' => 'Front main page for module',
'/admin/sample' => 'Admin main page for module',
'/item/42' => 'Page with id',
'/string.txt' => 'Text response',
'/html' => 'Html response',
'/view' => 'Response with rendered module view',
'/api.json' => 'JSON response',
'/null' => 'Null returned',
'/export' => 'Export any content',
'/download' => 'Force downloading',
'/not-found' => 'Error 404',
'/forbidden' => 'Error 403',
'/error' => 'Page with error in code',
'/moved' => 'Redirect to true answer',
'/back' => 'Redirects back',
'/ajax-notify' => 'Ajax notification',
'/ajax-alert' => 'Ajax alert',
'/update' => 'Notify after redirect',
'/ajax-update' => 'Notify after redirect for ajax',
'/save' => 'Alert after redirect',
'/ajax-save' => 'Alert after redirect for ajax',
'/ajax-random' => 'Random response',
'/ajax-nowhere' => 'Ajax 404 response',
'/sub-module' => 'Sub-module works too',
'/ajax-modal' => 'Modal example',
];
$d = document([
'title' => 'Index page of module',
'content' => '{links} {view_sample}',
'links' => \H::flatNav($links, false, ['id' => 'sample']),
]);
$d->data['view_sample'] = view('sample-name/main', [
'key' => 'for index',
]);
$d->addScript('my', "$('#sample a').click(function(){
if ($(this).attr('href').indexOf('/ajax') == 0){
$.get($(this).attr('href'));
return false;
}
})");
$d->addContextMenu('left', 'edit', [
'weight' => 10,
'title' => 'edit_item',
'url' => '/admin/sample',
]);
$d->addContextMenuItem('left', 'edit', [
'weight' => 10,
'title' => 'add_item',
'url' => '/admin/sample/add',
]);
return $d;
}
public function create()
{
$d = document([
'title' => 'Add item',
'content' => 'Here will be form',
]);
return $d;
}
public function myMethod()
{
$d = document([
'title' => 'Another page',
'content' => sampleHello().' Content for <strong>/admin/sample/another</strong><br>
<a href="/admin/sample">Back</a>',
]);
return $d;
}
public function item($id)
{
$d = document([
'content' => '<p>some html content for {item_id}nd item</p>
<p>This is iblocks: </p>{iblock:sample} and {iblock:other}',
'item_id' => $id,
'meta_keywords' => 'key, another key',
'title' => 'Page title',
'meta_title' => 'Overridden title',
]);
$d->addScript('my', "console.log('Answer always {$id}')");
alert('You\'ve got a "message"', 'info');
return $d;
}
public function textString()
{
return "This is <strong>string</strong><br>\nTrust me";
}
public function asHtml()
{
return html("This is <strong>string</strong><br>\nTrust me");
}
public function view()
{
return view('sample-name/main', [
'key' => 'value',
]);
}
public function forAjax()
{
return [
'status' => 'ok',
];
}
public function returnNull()
{
return;
}
public function export()
{
$content = '"id";"title"'."\r\n".'"1";"Test page"';
return downloadContent($content, 'export.'.time().'.csv');
}
public function download()
{
return download(DIR_THEME.'/default/assets/images/logo.png', 'image.png');
}
public function notFound()
{
abort(404, 'Error: This thing can\'t be found');
}
public function forbidden()
{
abort(403);
}
public function error()
{
trigger_error("This can be real error in code", E_USER_ERROR);
return 'place that can\'t reach';
}
public function moved()
{
return redirect('/item/42');
}
public function back()
{
return back();
}
public function ajaxNotify()
{
return notify('Sample notification', 'warning');
}
public function ajaxAlert()
{
return alert('Sample alert', 'info');
}
public function notifyAfterRedirect()
{
notify('Updated');
return back();
}
public function alertAfterRedirect()
{
alert('Not saved', 'danger');
return redirect('/item/42');
}
public function random()
{
$rand = rand(0, 3);
switch ($rand) {
case 0:
return notify('Notify');
break;
case 1:
return alert('Alert');
break;
case 2:
return back();
break;
case 3:
return redirect('/item/42');
break;
default:
return null;
}
}
public function modal()
{
return [
'modal' => [
'title' => 'Hello Modal!',
'body' => '<p>This is body text</p>',
'footer' => '<button type="button" class="btn btn-primary" data-dismiss="modal">Okay</button>',
'size' => 'modal-sm',
],
];
}
public function csrf()
{
return [
'console' => 'Hello, console!'
];
}
}