-
Notifications
You must be signed in to change notification settings - Fork 4
/
routes.php
161 lines (131 loc) · 3.83 KB
/
routes.php
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
<?php
// get the before each page filter from config
$before = Config::get('scribe::security.page_filter');
/*
|--------------------------------------------------------------------------
| VIEW LIST OF REGIONS
|--------------------------------------------------------------------------
*/
Route::get('(:bundle)', array(
'as' => 'scribe_list',
'before' => $before,
'do' =>
function() {
// show list of paginated content
return View::make('scribe::list')
->with('regions', Scribe_Content::paginate(8));
}));
/*
|--------------------------------------------------------------------------
| SHOW CREATE NEW REGION FORM
|--------------------------------------------------------------------------
*/
Route::get('(:bundle)/new', array(
'as' => 'scribe_new',
'before' => $before,
'do' =>
function() {
// show the new content region form
return View::make('scribe::new');
}));
/*
|--------------------------------------------------------------------------
| HANDLE CREATE NEW REGION FORM
|--------------------------------------------------------------------------
*/
Route::post('(:bundle)/new', array(
'before' => $before,
'do' =>
function() {
// get insertion array safely from post data
$new = array(
'nickname' => Input::get('nickname'),
'name' => Input::get('name'),
'content' => Input::get('content'),
'hidden' => Input::has('hidden')
);
// validation rules for a new content region
$rules = array(
'nickname' => 'required|min:3|max:128',
'name' => 'required|min:3|max:128|alpha_dash',
'content' => 'required',
);
// create the validator
$v = Validator::make($new, $rules);
// check the result
if($v->fails())
{
// redirect to the form, with errors and old input
return Redirect::to_route('scribe_new')
->with_errors($v)
->with_input();
}
// create a new object, fill it and save
$c = new Scribe_Content($new);
$c->save();
// redirect to the listing, with created flash indication
return Redirect::to_route('scribe_list')
->with('created', $c->nickname);
}));
/*
|--------------------------------------------------------------------------
| SHOW EDIT EXISTING REGION FORM
|--------------------------------------------------------------------------
*/
Route::get('(:bundle)/edit/(:any)', array(
'as' => 'scribe_edit',
'before' => $before,
'do' =>
function($name) {
// find a content region by name
$single = Scribe_Content::where_name($name)->first();
// show the content region edit form
return View::make('scribe::edit')
->with('single', $single);
}));
/*
|--------------------------------------------------------------------------
| HANDLE EDIT EXISTING REGION FORM
|--------------------------------------------------------------------------
*/
Route::post('(:bundle)/edit', array(
'as' => 'scribe_edited',
'before' => $before,
'do' =>
function() {
// get the hidden id field
$id = Input::get('id');
// create the insertion array safely from post
$new = array(
'nickname' => Input::get('nickname'),
'name' => Input::get('name'),
'content' => Input::get('content'),
'hidden' => Input::has('hidden')
);
// set validation rules for edited item
$rules = array(
'nickname' => 'required|min:3|max:128',
'name' => 'required|min:3|max:128|alpha_dash',
'content' => 'required',
);
// create the validator isntance
$v = Validator::make($new, $rules);
// get the old content region
$old = Scribe_Content::find($id);
// check the validation result
if($v->fails())
{
// on failure redirect to edit form with old region,
// errors and old input
return Redirect::to_route('scribe_edit', $old->name)
->with('single', $old)
->with_errors($v)
->with_input();
}
// update and save teh content region
$old->fill($new);
$old->save();
// return to region listing with an updated flash notify
return Redirect::to_route('scribe_list')
->with('updated', $old->nickname);
}));