Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\User;
  7. use App\Website;
  8. use App\Zone;
  9. use App\Feed;
  10. use App\StoryGroup;
  11. use App\Story;
  12. use Auth;
  13. use Storage;
  14. use Image;
  15.  
  16. class StoryController extends Controller
  17. {
  18. //
  19.  
  20. public function __construct() {
  21. $this->middleware('auth')->except(['retrieveStories']);
  22. }
  23.  
  24. public function retrieveStories(Request $request, $zone_id, $zone_type) {
  25. $referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_SCHEME) . '://' . parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
  26.  
  27. if ($zone_type === "static") {
  28. // @todo: Controllo se effettivamente è static
  29. $story_groups = StoryGroup::whereHas('zones', function ($q) use ($zone_id, $referrer) {
  30. $q->where('id', $zone_id)
  31. ->whereHas('website', function($q) use ($referrer) {
  32. $q->where('url', $referrer);
  33. });
  34. })->with("stories")->get();
  35.  
  36. return $story_groups;
  37.  
  38. } else if ($zone_type === "dynamic") {
  39. // @todo: Controllo se effettivamente è dynamic
  40. $story_groups = StoryGroup::whereHas('zones', function ($q) use ($zone_id, $referrer) {
  41. $q->where('id', $zone_id)
  42. ->whereHas('website', function($q) use ($referrer) {
  43. $q->where('url', $referrer);
  44. });
  45. })->with('feed', 'feed.stories')->get();
  46.  
  47. return $story_groups;
  48. }
  49. }
  50.  
  51. // Controller per single story group -> /story/{story_group_id/create}
  52.  
  53. public function getCreateStory($story_group_id = null)
  54. {
  55. if (!empty($story_group_id)) {
  56. return view('story.create')->with(compact('story_group_id'));
  57. } else {
  58. $story_groups = StoryGroup::all();
  59. return view('story.createw')->with(compact('story_groups'));
  60. }
  61. }
  62.  
  63. public function postCreateStory(Request $request, $story_group_id = null)
  64. {
  65. $story = Story::create([
  66. 'name'=>$request->input('name'),
  67. 'title'=>$request->input('title'),
  68. 'destination_url'=>$request->input('destination_url'),
  69. 'type'=>$request->input('types')
  70. ]);
  71. if (!Storage::disk('public')->exists('stories')) {
  72. Storage::makeDirectory('public/stories');
  73. }
  74. Image::make($request->file('picture_url'))->save(storage_path('app/public/stories/' . $story->id . '.jpg'));
  75. $story->picture_url = '/stories/' . $story->id . '.jpg';
  76. $story->save();
  77.  
  78. if (!empty($story_group_id)) {
  79. $story->storyGroups()->sync($story_group_id);
  80. return redirect('/story/'.$story_group_id.'/'.$story->id);
  81. } else {
  82. $story_groups = $request->input('story_group', []);
  83. $story_groups_a = [];
  84. foreach ($story_groups as $k => $story_group) {
  85. $story_groups_a[] = $k;
  86. }
  87. $story->storyGroups()->sync($story_groups_a);
  88. return redirect('/story/'.$story->id);
  89. }
  90. }
  91.  
  92. public function getUpdateStory($story_id, $story_group_id = null)
  93. {
  94. $story = Story::findOrFail($story_id);
  95. return view('story.update')->with(compact('story_group_id','story'));
  96. }
  97.  
  98. public function getUpdateStoryForSingleStoryGroup($story_group_id, $story_id)
  99. {
  100. $story = Story::findOrFail($story_id);
  101. return view('story.update')->with(compact('story_group_id','story'));
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. public function postUpdateStoryForSingleStoryGroup(Request $request, $story_group_id, $story_id)
  112. {
  113. $story = Story::find($story_id);
  114.  
  115. $story->name = $request->input('name');
  116. $story->title = $request->input('title');
  117. $story->destination_url = $request->input('destination_url');
  118. $story->type = $request->input('types');
  119.  
  120. if (!Storage::disk('public')->exists('stories')) {
  121. Storage::makeDirectory('public/stories');
  122. }
  123. if (!empty($request->file('picture_url'))) {
  124. Image::make($request->file('picture_url'))->save(storage_path('app/public/stories/' . $story->id . '.jpg'));
  125. $story->picture_url = '/stories/' . $story->id . '.jpg';
  126. }
  127.  
  128. $story->save();
  129.  
  130. $story->storyGroups()->sync($story_group_id);
  131.  
  132. return back();
  133. }
  134.  
  135. public function deleteStoryForSingleStoryGroup(Request $request, $story_group_id, $story_id)
  136. {
  137. Story::destroy($story_id);
  138.  
  139. return redirect('/story/' . $story_group_id . '/create');
  140. }
  141.  
  142.  
  143. // Controller per story/create (senza story_group_id)
  144.  
  145. public function postUpdateStory(Request $request, $story_id)
  146. {
  147. $story = Story::find($story_id);
  148.  
  149. $story->name = $request->input('name');
  150. $story->title = $request->input('title');
  151. $story->destination_url = $request->input('destination_url');
  152. $story->type = $request->input('types');
  153.  
  154. if (!Storage::disk('public')->exists('stories')) {
  155. Storage::makeDirectory('public/stories');
  156. }
  157. if (!empty($request->file('picture_url'))) {
  158. Image::make($request->file('picture_url'))->save(storage_path('app/public/stories/' . $story->id . '.jpg'));
  159. $story->picture_url = '/stories/' . $story->id . '.jpg';
  160. }
  161.  
  162. $story->save();
  163.  
  164. $story_groups = $request->input('story_group', []);
  165. $story_groups_a = [];
  166. foreach ($story_groups as $k => $story_group) {
  167. $story_groups_a[] = $k;
  168. }
  169. $story->storyGroups()->sync($story_groups_a);
  170.  
  171. return back();
  172. }
  173.  
  174. public function deleteStory(Request $request, $story_id)
  175. {
  176. Story::destroy($story_id);
  177.  
  178. return redirect('/story/create');
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement