Skip to content

Commit cc6d448

Browse files
authored
Merge pull request #24 from nadja97/master
Pages from (many to many relationship) to (one to many relationship)
2 parents 8040f08 + 434cdda commit cc6d448

19 files changed

+238
-316
lines changed

app/Http/Controllers/Admin/Pages/PageContentController.php

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@
77
use App\Http\Requests;
88
use App\Models\Content;
99
use App\Models\PageContent;
10+
use Illuminate\Http\Request;
1011
use Illuminate\Http\UploadedFile;
1112
use App\Http\Controllers\Admin\AdminController;
1213
use Titan\Models\Traits\ImageThumb;
1314

1415
class PageContentController extends AdminController
1516
{
17+
18+
/**
19+
* Display a listing of content.
20+
*
21+
* @param Page $page
22+
* @return Response
23+
*/
24+
public function index(Page $page)
25+
{
26+
save_resource_url();
27+
28+
$page->load('sections');
29+
30+
return $this->view('pages.components.page_components')->with('page', $page);
31+
}
32+
1633
/**
1734
* Show the form for creating a new content.
1835
*
@@ -21,12 +38,35 @@ class PageContentController extends AdminController
2138
*/
2239
public function create(Page $page)
2340
{
24-
// create new content (used to link media and documents to)
25-
$item = PageContent::create();
26-
2741
return $this->view('pages.components.content')
28-
->with('page', $page)
29-
->with('item', $item);
42+
->with('page', $page);
43+
}
44+
45+
/**
46+
* Store a newly created news in storage.
47+
*
48+
* @param Request $request
49+
* @return array
50+
*/
51+
public function store(Request $request)
52+
{
53+
54+
if (is_null(request()->file('media'))) {
55+
$attributes = request()->validate(array_except(PageContent::$rules, 'media'),
56+
PageContent::$messages);
57+
}
58+
else {
59+
$attributes = request()->validate(PageContent::$rules, PageContent::$messages);
60+
61+
$media = $this->moveAndCreatePhoto($attributes['media']);
62+
if ($media) {
63+
$attributes['media'] = $media;
64+
}
65+
}
66+
67+
$pageContent = $this->createEntry(PageContent::class, $attributes);
68+
69+
return redirect('admin/pages/'.$request->page_id.'/sections/content/'.$pageContent->id.'/edit');
3070
}
3171

3272
/**
@@ -38,6 +78,7 @@ public function create(Page $page)
3878
*/
3979
public function edit(Page $page, PageContent $content)
4080
{
81+
4182
return $this->view('pages.components.content')
4283
->with('page', $page)
4384
->with('item', $content);
@@ -68,11 +109,48 @@ public function update(Page $page, PageContent $content)
68109
unset($attributes['page_id']);
69110
$item = $this->updateEntry($content, $attributes);
70111

71-
$page->attachComponent($item);
112+
return redirect_to_resource();
113+
}
114+
115+
/**
116+
* Remove the specified content from storage.
117+
*
118+
* @param Page $page
119+
* @param PageContent $section
120+
* @return Response
121+
* @internal param $page_section
122+
*/
123+
public function destroy(Page $page, PageContent $section)
124+
{
125+
// delete page_content
126+
$this->deleteEntry($section, request());
127+
128+
log_activity('Page Component Deleted', 'A Page Content was successfully removed from the Page', $section);
72129

73130
return redirect_to_resource();
74131
}
75132

133+
/**
134+
* @param Page $page
135+
* @return array
136+
*/
137+
public function updateOrder(Page $page)
138+
{
139+
$items = json_decode(request('list'), true);
140+
141+
foreach ($items as $key => $item) {
142+
143+
$row = PageContent::find($item['id']);
144+
if($row) {
145+
$row->update([
146+
'list_order' => ($key + 1)
147+
]);
148+
}
149+
}
150+
151+
return ['result' => 'success'];
152+
}
153+
76154
/**
77155
* Save Image in Storage, crop image and save in public/uploads/images
78156
* @param UploadedFile $file

app/Http/Controllers/Admin/Pages/SectionsController.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

app/Models/Page.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ public static function getAllList()
7171
*/
7272
public function sections()
7373
{
74-
return $this->hasMany(PageSection::class, 'page_id', 'id')->orderBy('list_order');
74+
return $this->hasMany(PageContent::class, 'page_id', 'id')->orderBy('list_order');
7575
}
7676

7777
/**
7878
* Get the components
7979
*/
8080
public function components()
8181
{
82-
return $this->hasMany(PageSection::class, 'page_id', 'id')->orderBy('list_order');
82+
return $this->hasMany(PageContent::class, 'page_id', 'id')->orderBy('list_order');
8383
}
8484

8585
/**
@@ -97,27 +97,4 @@ public function pageContent()
9797
{
9898
return $this->belongsToMany(PageContent::class);
9999
}
100-
101-
/**
102-
* Create and Attach a page component to this page
103-
* @param $item
104-
* @return bool
105-
*/
106-
public function attachComponent($item)
107-
{
108-
// if already attached
109-
$found = PageSection::where('page_id', $this->id)
110-
->where('component_id', $item->id)
111-
->where('component_type', get_class($item))
112-
->first();
113-
if ($found) {
114-
return true;
115-
}
116-
117-
PageSection::create([
118-
'page_id' => $this->id,
119-
'component_id' => $item->id,
120-
'component_type' => get_class($item),
121-
]);
122-
}
123100
}

app/Models/PageContent.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public function getSummaryAttribute()
6767
*/
6868
public function pages()
6969
{
70-
return $this->belongsToMany(Page::class);
70+
return $this->belongsTo(Page::class);
71+
}
72+
73+
/**
74+
* Get the Page many to many
75+
*/
76+
public function component()
77+
{
78+
return $this->belongsTo(Page::class);
7179
}
7280
}

app/Models/PageSection.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)