Guest User

Untitled

a guest
Dec 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.31 KB | None | 0 0
  1. --
  2. -- Table structure for table `contents`
  3. --
  4.  
  5. CREATE TABLE `contents` (
  6. `id` int(10) UNSIGNED NOT NULL,
  7. `body` text COLLATE utf8mb4_unicode_ci,
  8. `area` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  9. `lang` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  10. `contentable_id` int(11) NOT NULL,
  11. `contentable_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  12. `created_at` timestamp NULL DEFAULT NULL,
  13. `updated_at` timestamp NULL DEFAULT NULL
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  15.  
  16. --
  17. -- Dumping data for table `contents`
  18. --
  19.  
  20. INSERT INTO `contents` (`id`, `body`, `area`, `lang`, `contentable_id`, `contentable_type`, `created_at`, `updated_at`) VALUES
  21. (5, 'en title', 'title', 'en', 2, 'App\Room', '2017-12-16 22:00:05', '2017-12-16 22:00:05'),
  22. (6, '<p>en des</p>', 'description', 'en', 2, 'App\Room', '2017-12-16 22:00:05', '2017-12-16 22:00:05'),
  23. (7, 'zh title', 'title', 'zh', 2, 'App\Room', '2017-12-16 22:00:05', '2017-12-16 22:00:05'),
  24. (8, '<p>zh des</p>', 'description', 'zh', 2, 'App\Room', '2017-12-16 22:00:05', '2017-12-16 22:00:05');
  25.  
  26. -- --------------------------------------------------------
  27.  
  28. --
  29. -- Table structure for table `images`
  30. --
  31.  
  32. CREATE TABLE `images` (
  33. `id` int(10) UNSIGNED NOT NULL,
  34. `title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  35. `path` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  36. `imageable_id` int(11) NOT NULL,
  37. `imageable_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  38. `created_at` timestamp NULL DEFAULT NULL,
  39. `updated_at` timestamp NULL DEFAULT NULL
  40. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  41.  
  42. --
  43. -- Dumping data for table `images`
  44. --
  45.  
  46. INSERT INTO `images` (`id`, `title`, `path`, `imageable_id`, `imageable_type`, `created_at`, `updated_at`) VALUES
  47. (4, '2-1', '/storage/rooms/2/1.jpeg', 2, 'App\Room', '2017-12-16 22:00:05', '2017-12-16 22:00:05'),
  48. (5, '2-2', '/storage/rooms/2/2.jpeg', 2, 'App\Room', '2017-12-16 22:00:05', '2017-12-16 22:00:05'),
  49. (6, '2-3', '/storage/rooms/2/3.jpeg', 2, 'App\Room', '2017-12-16 22:00:05', '2017-12-16 22:00:05');
  50.  
  51. -- --------------------------------------------------------
  52.  
  53. namespace AppHttpControllers;
  54.  
  55. use IlluminateHttpRequest;
  56. use DB;
  57. use AppRoom;
  58. use AppContent;
  59. use AppImage;
  60. use IlluminateSupportFacadesStorage;
  61. use IlluminateSupportFacadesFile;
  62.  
  63. use Imagick;
  64.  
  65. class RoomController extends Controller
  66. {
  67. /**
  68. * Display a listing of the resource.
  69. *
  70. * @return IlluminateHttpResponse
  71. */
  72. public function index(Request $request)
  73. {
  74. $rooms = Room::orderBy('id','ASC')->paginate(5);
  75. return view('room.index',compact('rooms'))
  76. ->with('i', ($request->input('page', 1) - 1) * 5);
  77. }
  78.  
  79. /**
  80. * Show the form for creating a new resource.
  81. *
  82. * @return IlluminateHttpResponse
  83. */
  84. public function create()
  85. {
  86. return view('room.create');
  87. }
  88.  
  89.  
  90. /**
  91. * Store a newly created resource in storage.
  92. *
  93. * @param IlluminateHttpRequest $request
  94. * @return IlluminateHttpResponse
  95. */
  96. public function store(Request $request)
  97. {
  98. $this->validate($request, [
  99. 'title' => 'required',
  100. 'description' => 'required',
  101. 'units' => 'required',
  102. 'sleeps' => 'required',
  103. 'image_1' => 'mimes:jpeg,png,jpg,gif,svg',
  104. 'image_2' => 'mimes:jpeg,png,jpg,gif,svg',
  105. 'image_3' => 'mimes:jpeg,png,jpg,gif,svg'
  106. ]);
  107. // Create a room
  108. $data = new Room;
  109. $data->title = $request->title;
  110. $data->description = $request->description;
  111. $data->units = $request->units;
  112. $data->sleeps = $request->sleeps;
  113. $data->sharing = $request->sharing;
  114. $data->save();
  115. // Save content and translations
  116. // English
  117. $titleE = $this->save_content($request->title, 'title', 'en');
  118. $data->contents()->save($titleE);
  119. $descrE = $this->save_content($request->description, 'description', 'en');
  120. $data->contents()->save($descrE);
  121. // Chinese
  122. $titleC = $this->save_content($request->title_zh, 'title', 'zh');
  123. $data->contents()->save($titleC);
  124. $descrC = $this->save_content($request->description_zh, 'description', 'zh');
  125. $data->contents()->save($descrC);
  126.  
  127. // Save Images
  128. //Storage::makeDirectory('rooms/'.$data->id);
  129. if(!File::isDirectory(public_path().'/storage/rooms/'.$data->id))
  130. {
  131. $result = File::makeDirectory(public_path().'/storage/rooms/'.$data->id, 0775, true);
  132. }
  133. // Image 1
  134. if ($request->hasFile('image_1')) {
  135. $this->write_image($request->image_1->path(), $data->id, 1, $request->image_1->extension());
  136. $image = $this->save_image($data->id, 1, $request->image_1->extension());
  137. $data->images()->save($image);
  138. }
  139. // Image 2
  140. if ($request->hasFile('image_2')) {
  141. $this->write_image($request->image_2->path(), $data->id, 2, $request->image_2->extension());
  142. $image = $this->save_image($data->id, 2, $request->image_2->extension());
  143. $data->images()->save($image);
  144. }
  145. // Image 3
  146. if ($request->hasFile('image_3')) {
  147. $this->write_image($request->image_3->path(), $data->id, 3, $request->image_3->extension());
  148. $image = $this->save_image($data->id, 3, $request->image_3->extension());
  149. $data->images()->save($image);
  150. }
  151. return redirect()->route('dashboard.room.index')
  152. ->with('success','Room created successfully');
  153. }
  154.  
  155. /**
  156. * Display the specified resource.
  157. *
  158. * @param int $id
  159. * @return IlluminateHttpResponse
  160. */
  161. public function show($id)
  162. {
  163. $room = Room::find($id);
  164. $content = $room->contents;
  165. $room->title_zh = $content[$this->get_key($content, 'title', 'zh', 'area', 'lang')]['body'];
  166. $room->description_zh = $content[$this->get_key($content, 'description', 'zh', 'area', 'lang')]['body'];
  167. return view('room.show', compact('room'));
  168. }
  169.  
  170. /**
  171. * Show the form for editing the specified resource.
  172. *
  173. * @param int $id
  174. * @return IlluminateHttpResponse
  175. */
  176. public function edit($id)
  177. {
  178. $room = Room::find($id);
  179. $content = $room->contents;
  180. $image = $room->images;
  181. $room->image_1 = $image[$this->get_key($image, $id.'-1', $id, 'title', 'imageable_id')]['path'];
  182. $room->image_2 = $image[$this->get_key($image, $id.'-2', $id, 'title', 'imageable_id')]['path'];
  183. $room->image_3 = $image[$this->get_key($image, $id.'-3', $id, 'title', 'imageable_id')]['path'];
  184. $room->title_zh = $content[$this->get_key($content, 'title', 'zh', 'area', 'lang')]['body'];
  185. $room->description_zh = $content[$this->get_key($content, 'description', 'zh', 'area', 'lang')]['body'];
  186. return view('room.edit', compact('room'));
  187. }
  188.  
  189. /**
  190. * Update the specified resource in storage.
  191. *
  192. * @param IlluminateHttpRequest $request
  193. * @param int $id
  194. * @return IlluminateHttpResponse
  195. */
  196. public function update(Request $request, $id)
  197. {
  198. $this->validate($request, [
  199. 'title' => 'required',
  200. 'description' => 'required',
  201. 'units' => 'required',
  202. 'sleeps' => 'required',
  203. 'image_1' => 'nullable|mimes:JPG,JPEG,jpeg,png,jpg,gif,svg',
  204. 'image_2' => 'nullable|mimes:JPG,JPEG,jpeg,png,jpg,gif,svg',
  205. 'image_3' => 'nullable|mimes:JPG,JPEG,jpeg,png,jpg,gif,svg'
  206. ]);
  207.  
  208. $data = Room::find($id);
  209. $data->title = $request->title;
  210. $data->description = $request->description;
  211. $data->units = $request->units;
  212. $data->sleeps = $request->sleeps;
  213. $data->sharing = $request->sharing;
  214. $data->save();
  215.  
  216. $this->update_content('title', 'en', 'AppRoom', $id, $request->title);
  217. $this->update_content('description', 'en', 'AppRoom', $id, $request->description);
  218. $this->update_content('title', 'zh', 'AppRoom', $id, $request->title_zh);
  219. $this->update_content('description', 'zh', 'AppRoom', $id, $request->description_zh);
  220.  
  221. // Image 1
  222. if ($request->hasFile('image_1')) {
  223. $this->write_image($request->image_1->path(), $id, 1, $request->image_1->extension());
  224.  
  225. }
  226. // Image 2
  227. if ($request->hasFile('image_2')) {
  228. $this->write_image($request->image_2->path(), $id, 2, $request->image_2->extension());
  229.  
  230. }
  231. // Image 3
  232. if ($request->hasFile('image_3')) {
  233. $this->write_image($request->image_3->path(), $id, 3, $request->image_3->extension());
  234. }
  235.  
  236. return redirect()->route('dashboard.room.index')
  237. ->with('success','Room updated successfully');
  238. }
  239. /**
  240. * Remove the specified resource from storage.
  241. *
  242. * @param int $id
  243. * @return IlluminateHttpResponse
  244. */
  245. public function destroy($id)
  246. {
  247. $room = Room::find($id);
  248. $room->images()->delete();
  249. $room->contents()->delete();
  250. File::deleteDirectory(public_path().'/storage/rooms/'.$id);
  251. $room->delete();
  252. return redirect()->route('dashboard.room.index')
  253. ->with('success','Room deleted successfully');
  254. }
  255.  
  256. // write images to storage
  257. private function write_image($path, $id, $filename, $ext){
  258. $img = new Imagick($path);
  259. $img->scaleImage(0,200);
  260. $img->writeImage(public_path('storage/rooms/'.$id.'/'.$filename.'.'.$ext));
  261. $img->destroy();
  262. }
  263.  
  264. private function save_content($body, $area, $lan){
  265. $content = new Content;
  266. $content->body = $body;
  267. $content->area = $area;
  268. $content->lang = $lan;
  269. return $content;
  270. }
  271.  
  272. private function update_content($area, $lang, $type, $id, $body){
  273. $content = Content::where(
  274. [
  275. ['area', $area],
  276. ['lang', $lang],
  277. ['contentable_type', $type],
  278. ['contentable_id', $id]
  279. ]
  280. )->first();
  281. $content->body = $body;
  282. $content->save();
  283. }
  284.  
  285. private function save_image($id, $fName, $fExt){
  286. $image = new Image;
  287. $image->title = $id.'-'.$fName;
  288. $image->path = '/storage/rooms/'.$id.'/'.$fName.'.'.$fExt;
  289. return $image;
  290. }
  291.  
  292. private function get_key($array, $value1, $value2, $key1, $key2){
  293. $key = false;
  294. $search = [$key1 => $value1, $key2 => $value2];
  295. foreach ($array as $k => $v) {
  296. if ($v[$key1] == $search[$key1] && $v[$key2] == $search[$key2]) {
  297. $key = $k;
  298. // key found - break the loop
  299. break;
  300. }
  301. }
  302. return $key;
  303. }
  304.  
  305. }
  306.  
  307. namespace App;
  308.  
  309. use IlluminateDatabaseEloquentModel;
  310.  
  311. class Room extends Model
  312. {
  313.  
  314. /**
  315. * Get all of the room's images.
  316. */
  317. public function images()
  318. {
  319. return $this->morphMany('AppImage', 'imageable');
  320. }
  321.  
  322. /**
  323. * Get all of the room's content.
  324. */
  325. public function contents()
  326. {
  327. return $this->morphMany('AppContent', 'contentable');
  328. }
  329.  
  330. /**
  331. * The bookings that belong to the room.
  332. */
  333. public function bookings()
  334. {
  335. return $this->belongsToMany('AppBooking');
  336. }
  337. }
  338.  
  339. namespace App;
  340.  
  341. use IlluminateDatabaseEloquentModel;
  342.  
  343. class Content extends Model
  344. {
  345.  
  346. /**
  347. * Get all of the owning contentable models.
  348. */
  349. public function contentable()
  350. {
  351. return $this->morphTo();
  352. }
  353.  
  354. /**
  355. * Get all of the rooms that are assigned this content.
  356. */
  357. public function rooms()
  358. {
  359. return $this->morphedByMany('AppRoom', 'contentable');
  360. }
  361.  
  362. }
  363.  
  364. namespace App;
  365.  
  366. use IlluminateDatabaseEloquentModel;
  367.  
  368. class Image extends Model
  369. {
  370. //
  371.  
  372.  
  373. /**
  374. * Get all of the owning imageable models.
  375. */
  376. public function imageable()
  377. {
  378. return $this->morphTo();
  379. }
  380.  
  381. /**
  382. * Get all of the rooms that are assigned this image.
  383. */
  384. public function rooms()
  385. {
  386. return $this->morphedByMany('AppRoom', 'imageable');
  387. }
  388.  
  389. }
Add Comment
Please, Sign In to add comment