PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/app/Laravel/Controllers/Backoffice/PageContentController.php

https://bitbucket.org/cityserv/techreportph
PHP | 175 lines | 127 code | 32 blank | 16 comment | 15 complexity | d1dfcd5bb4640a5584a8a2031b14a7fc MD5 | raw file
  1<?php namespace App\Laravel\Controllers\Backoffice;
  2
  3/**
  4*
  5* Models used for this controller
  6*/
  7use App\Laravel\Models\PageContent;
  8
  9/**
 10*
 11* Requests used for validating inputs
 12*/
 13use App\Laravel\Requests\Backoffice\PageContentRequest;
 14
 15/**
 16*
 17* Classes used for this controller
 18*/
 19use App\Http\Requests\Request;
 20use Input, Helper, Carbon, Session, Str, File, Image, ImageUploader;
 21
 22class PageContentController extends Controller{
 23
 24	/**
 25	*
 26	* @var Array $data
 27	*/
 28	protected $data;
 29
 30	public function __construct () {
 31		parent::__construct();
 32		$view = Input::get('view','table');
 33		array_merge($this->data, parent::get_data());
 34		$this->data['page_title'] = "Page Content";
 35		$this->data['page_description'] = "This is the general information about ".$this->data['page_title'].".";
 36		$this->data['route_file'] = "page_content";
 37
 38		$this->data['page_locations'] = ['' => "Choose page location", 'asean_bac' => "ASEAN BAC", 'aba' => "ABA", 'about_abac_footer' => "ABOUT ABAC FOOTER", 'members' => "MEMBERS", 'abis' => "ABIS"];
 39	}
 40
 41	public function index () {
 42		$this->data['page_content'] = PageContent::orderBy('created_at',"DESC")->get();
 43		return view('backoffice.'.$this->data['route_file'].'.index',$this->data);
 44	}
 45
 46	public function create () {
 47		return view('backoffice.'.$this->data['route_file'].'.create',$this->data);
 48	}
 49
 50	public function store (PageContentRequest $request) {
 51		try {
 52			$new_page_content = new PageContent;
 53			$new_page_content->fill($request->all());
 54
 55			if($request->hasFile('file')){
 56				$upload = ImageUploader::upload($request->file,'storage/page_content');
 57				$new_page_content->path = $upload["path"];
 58				$new_page_content->directory = $upload["directory"];
 59				$new_page_content->filename = $upload["filename"];
 60			}
 61
 62			if($new_page_content->save()) {
 63				Session::flash('notification-status','success');
 64				Session::flash('notification-msg',"New page content has been added.");
 65				return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
 66			}
 67
 68			Session::flash('notification-status','failed');
 69			Session::flash('notification-msg','Something went wrong.');
 70
 71			return redirect()->back();
 72		} catch (Exception $e) {
 73			Session::flash('notification-status','failed');
 74			Session::flash('notification-msg',$e->getMessage());
 75			return redirect()->back();
 76		}
 77	}
 78
 79	public function edit ($id = NULL) {
 80		$page_content = PageContent::find($id);
 81
 82		if (!$page_content) {
 83			Session::flash('notification-status',"failed");
 84			Session::flash('notification-msg',"Record not found.");
 85			return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
 86		}
 87
 88		$this->data['page_content'] = $page_content;
 89		return view('backoffice.'.$this->data['route_file'].'.edit',$this->data);
 90	}
 91
 92	public function update (PageContentRequest $request, $id = NULL) {
 93		try {
 94			$page_content = PageContent::find($id);
 95
 96			if (!$page_content) {
 97				Session::flash('notification-status',"failed");
 98				Session::flash('notification-msg',"Record not found.");
 99				return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
100			}
101
102			$page_content->fill($request->all());
103
104			if($request->hasFile('file')){
105				$upload = ImageUploader::upload($request->file,'storage/page_content');
106				if($upload){	
107					if (File::exists("{$page_content->directory}/{$page_content->filename}")){
108						File::delete("{$page_content->directory}/{$page_content->filename}");
109					}
110					if (File::exists("{$page_content->directory}/resized/{$page_content->filename}")){
111						File::delete("{$page_content->directory}/resized/{$page_content->filename}");
112					}
113					if (File::exists("{$page_content->directory}/thumbnails/{$page_content->filename}")){
114						File::delete("{$page_content->directory}/thumbnails/{$page_content->filename}");
115					}
116				}
117				
118				$page_content->path = $upload["path"];
119				$page_content->directory = $upload["directory"];
120				$page_content->filename = $upload["filename"];
121			}
122
123			if($page_content->save()) {
124				Session::flash('notification-status','success');
125				Session::flash('notification-msg',"A page content has been updated.");
126				return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
127			}
128
129			Session::flash('notification-status','failed');
130			Session::flash('notification-msg','Something went wrong.');
131
132		} catch (Exception $e) {
133			Session::flash('notification-status','failed');
134			Session::flash('notification-msg',$e->getMessage());
135			return redirect()->back();
136		}
137	}
138
139	public function destroy ($id = NULL) {
140		try {
141			$page_content = PageContent::find($id);
142
143			if (!$page_content) {
144				Session::flash('notification-status',"failed");
145				Session::flash('notification-msg',"Record not found.");
146				return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
147			}
148
149			if (File::exists("{$page_content->directory}/{$page_content->filename}")){
150				File::delete("{$page_content->directory}/{$page_content->filename}");
151			}
152			if (File::exists("{$page_content->directory}/resized/{$page_content->filename}")){
153				File::delete("{$page_content->directory}/resized/{$page_content->filename}");
154			}
155			if (File::exists("{$page_content->directory}/thumbnails/{$page_content->filename}")){
156				File::delete("{$page_content->directory}/thumbnails/{$page_content->filename}");
157			}
158
159			if($page_content->save() AND $page_content->delete()) {
160				Session::flash('notification-status','success');
161				Session::flash('notification-msg',"A page content has been deleted.");
162				return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
163			}
164
165			Session::flash('notification-status','failed');
166			Session::flash('notification-msg','Something went wrong.');
167
168		} catch (Exception $e) {
169			Session::flash('notification-status','failed');
170			Session::flash('notification-msg',$e->getMessage());
171			return redirect()->back();
172		}
173	}
174
175}