-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagesController.php
86 lines (66 loc) · 2.22 KB
/
PagesController.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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Page;
class PagesController extends Controller
{
//Set a home page variable
//
//@return a redirect to routing method with parametr of home page
public function index()
{
$page='Home';
return redirect()->action('PagesController@pages', $page);
}
//Get a view of requested page with a dynamic menu and content or throw a 404-error page
//
//@param string $page page from the route.php
//@return view with parameters string $current_page, string $content, array $menu
public function pages($page)
{
$menu = $this->buildmenu();//building a dynamic menu
if(Page::where('title', $page)->first())
{
$current_page = Page::where('title', $page)->first();
$content = 'The content of ' . $current_page->title;
return view('pages.index')->with('current_page', $current_page)
->with('page_content', $content)
->with('pages', $menu);
} else
{
return view('errors.404')->with('pages', $menu);
}
}
//Get a collection of pages as objects
//
// @return an array of App\Page - an Eloquant model, with a subarray of subpages as well
public function buildmenu()
{
$pages = Page::where('published', 1)->where('ischild', '0')->get();
foreach ($pages as $page)
{
if ($page->issection!=0)//checking if an object has subpages
{
if (Page::where('parent_id', $page->id)->get())
{
$children = Page::where('parent_id', $page->id)->get();
}
$count=0;
foreach ($children as $child)//building array of subpages
{
if (Page::find($child->id))
{
if (Page::find($child->id)->published!=1)
{
unset($children[$count]);
}
}
$count++;
}
$page->children = $children;
}
}
return $pages;
}
}