forked from moxi9/phpfox-base
/
start.php
287 lines (235 loc) · 8.2 KB
/
start.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
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
<?php
// Load our Base Model for easy access
use \Apps\PHPfox_Base\Model\Base;
// Group all our routes
new Core\Route\Group('/base', function() {
// Load our base Model
$Base = new Base;
/**
* @route /base/
* Index page
*/
new Core\Route('/', function(Core\Controller $Controller) use($Base) {
// Set the pages title, section title, sub-menu and template file
return $Controller->title('PHPfox App Base')
->section('PHPfox App Base', '/base')
->menu($Base->menu())
->render('index.html');
});
/**
* @route /base/forms
* Info on how to display and handle forms.
*/
new Core\Route('/forms', function(Core\Controller $Controller) use($Base) {
/**
* Here we create the validation rules. This is an inline shorthand version.
* When running the make() method it does checks to see if a form has been posted. If it has,
* then it runs the check to see if all the fields meets its requirements.
*/
(new Core\Validator())
->rule('email_input')->email()->required()
->rule('text_input')->required()
->rule('password_input')->required()->min(4)
->make();
$formData = '';
// Check to see if the form has been posted
if (($val = $Controller->request->get('val'))) {
// If the form has been posted build a easy message for developers to see
$formData = '<div class="message"><pre>' . print_r(array_map('htmlspecialchars', $val), true) . '</pre></div>';
// Check if this is the AJAX form being posted
if ($Controller->request->isPost()) {
// If an AJAX form, return jQuery so JavaScript can run it on the client-end
return j('#post_data')->html($formData);
}
}
// Set the pages title, section title, h1 tag, sub-menu and template file
return $Controller->section('PHPfox App Base', '/base')
->h1('Forms', '/base/forms')
->title('Forms')
->menu($Base->menu())
->render('forms.html', [
'formData' => $formData
]);
});
/**
* @route /base/adding-a-feed
* Learn how to add a new feed
*/
(new Core\Route('/adding-a-feed', function(Core\Controller $Controller) use($Base) {
// Check to see if we posted anything
if ($Controller->request->isPost()) {
$Feed = new \Api\Feed();
// Create a new post. Make sure to pass your "type_id", which is your App id
$feed = $Feed->post([
'type_id' => 'PHPfox_Base',
'content' => $Controller->request->get('val')['status']
]);
// Use jquery to output the new feed object
return j('#ajax_output')->html('<div class="message"><pre>' . print_r($feed, true) . '</pre></div>');
}
// Set the pages title, section title, h1 tag, sub-menu and template file
return $Controller->section('PHPfox App Base', '/base')
->h1('Adding a Feed', '/base/adding-a-feed')
->title('Adding a Feed')
->menu($Base->menu())
->render('adding-a-feed.html', [
]);
}))->auth(true);
/**
* @route /base/database
* Small example of how to connect to a Model and run a function
*/
new Core\Route('/database', function(Core\Controller $Controller) use($Base) {
// Load our User Model, which will be located at: /PF.Site/PHPfox_Base/Model/User.php
$User = new \Apps\PHPfox_Base\Model\User();
// Run the random() function provded by the Model
$randomUsers = $User->random();
// Set the pages title, section title, h1 tag, sub-menu and template file
return $Controller->section('PHPfox App Base', '/base')
->h1('Database', '/base/forms')
->title('Database')
->menu($Base->menu())
->render('database.html', [
'users' => $randomUsers
]);
});
/**
* @route /base/external-controller
* In this example we call an external program via our API routine. This can run any flavor of choice.
* For this specific example we use the "api.php" file.
*/
(new Core\Route('/external-controller'))->url(str_replace('index.php/', '', param('core.path')) . 'PF.Site/Apps/PHPfox_Base/api.php');
/**
* @route /base/active-user
* Passing the ->auth(true) makes sure the user is logged in
*/
(new Core\Route('/active-user', function(Core\Controller $Controller) use ($Base) {
// Set the pages title, section title, h1 tag, sub-menu and template file
return $Controller->section('PHPfox App Base', '/base')
->h1('Active User', '/base/active-user')
->title('Active User')
->menu($Base->menu())
->render('active-user.html', [
'name' => $Controller->active->name
]);
}))->auth(true);
/**
* @route /base/popups
* Link to create a popup
*/
new Core\Route('/popups', function(Core\Controller $Controller) use ($Base) {
return $Controller->title('Popups')
->section('PHPfox App Base', '/base')
->h1('AJAX Popups', '/base/popups')
->menu($Base->menu())->render('popup.html');
});
/**
* @route /base/popup-output
* Popup output
*/
new Core\Route('/popup-output', function(Core\Controller $Controller) use ($Base) {
return $Controller->h1('Hello!', '/base/popup-output')
->menu($Base->menu())
->render('popup-output.html');
});
/**
* @route /base/comments-and-likes
* In this example we look into adding Comments & Likes to your items
*/
new Core\Route('/comments-and-likes', function(Core\Controller $Controller) use ($Base) {
// Load the needed classes
$ApiFeed = new \Api\Feed();
$Cache = new \Core\Cache();
// Check if we cached a feed ID# earlier
$feedId = $Cache->get('test_feed_id');
if (!$feedId) {
// Create a new post
$feed = $ApiFeed->post([
'type_id' => 'PHPfox_Base',
'content' => 'Hello! I am a post.'
]);
// Add post to cache so we don't have to create a new post every time
$Cache->set('test_feed_id', $feed->id);
$feedId = $feed->id;
}
/**
* Get a feed. When you get a feed it automatically loads the Comment & Like block routine.
* You just need to output it in the HTML view file using {{ comments() }}
*/
$ApiFeed->get($feedId);
return $Controller->title('Comments & Likes')
->section('PHPfox App Base', '/base')
->h1('Comments & Likes', '/base/comments-and-likes')
->menu($Base->menu())->render('comments-and-likes.html');
});
/**
* @route /base/phrasing
* Working with Phrases
*/
new Core\Route('/phrasing', function(Core\Controller $controller) use($Base) {
return $controller->title('Phrases')
->section('PHPfox App Base', '/base')
->h1('Phrases', '/base/comments-and-likes')
->menu($Base->menu())->render('phrasing.html', [
'thePhrase' => _p('core.sample_phrase')
]);
});
/**
* @route /base/is
* Working with Is.*
*/
new Core\Route('/is', function(Core\Controller $controller) use($Base) {
if (Core\Is::module('blog')) {
// check to see if a module is enabled
}
if (Core\Is::user()) {
// check to see if a user is logged in
}
if ($controller->active->perm('blog.add_new_blog')) {
// check to see if a user has access to a specific user permission.
}
return $controller->title('IS Function')
->section('PHPfox App Base', '/base')
->h1('IS', '/base/is')
->menu($Base->menu())->render('is.html');
});
/**
* @route /base/ajax
* Working with AJAX
*/
new Core\Route('/ajax', function(Core\Controller $controller) use($Base) {
return $controller->title('AJAX')
->section('PHPfox App Base', '/base')
->h1('AJAX', '/base/ajax')
->menu($Base->menu())->render('ajax.html');
});
/**
* @route /base/ajax-get
* Working with AJAX response
*/
new Core\Route('/ajax-get', function(Core\Controller $controller) use($Base) {
return [
'run' => "$('#ajax-response').html('<div class=\"message\">Hello World! Me is AJAX response.</div>');"
];
});
/**
* @route /base/ajax-custom
* Working with AJAX response
*/
new Core\Route('/ajax-custom', function(Core\Controller $controller) use($Base) {
echo '<div class="message">Well, hello again. I too am an AJAX response.</div>';
});
});
/**
* @route /api/PHPfox_Base
* Here we build a route outside our /base group because it needs to connect to the cores API route.
* We use this route to get the sections sub menu, which we have on all other sections.
* Since the @route /base/external-controller is an external route, we don't have the menu locally and have to build
* it with the API
*/
new Core\Route('/api/PHPfox_Base', function(Core\Controller $Controller) {
$Controller->menu((new Base())->menu());
return [
'menu' => $Controller->menu()
];
});