middleware('auth');
$this->middleware('permission:messageboard-list');
$this->middleware('permission:messageboard-create', ['only' => ['create', 'store']]);
$this->middleware('permission:messageboard-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:messageboard-delete', ['only' => ['destroy']]);
$this->middleware('permission:messageboard-inactive-list', ['only' => ['inactive']]);
$this->middleware('permission:messageboard-activate', ['only' => ['activate']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$messages = MessageBoard::orderBy('id', 'desc')->paginate(50);
return view('messageboard.index', compact('messages'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('messageboard.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'body' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}
";
}
flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$message = new MessageBoard;
$message->body = $request->body;
$photo = $request->file('photo');
$photo_destination_path = $photo_name = '';
if ($photo) {
$photo_name = date("Ymd_H-i-s") . '_' . $photo->getClientOriginalName();
// $photo_source_path = $photo->getRealPath();
// $photo_size = $photo->getSize();
// $photo_mime_type = $photo->getMimeType();
$photo_destination_path = 'uploads/messageboard/';
$random = '';
do {
$photo_name = date("Ymd_H-i-s") . '_' . Str::slug($photo->getClientOriginalName()) . $random . '.'
. File::extension($photo->getClientOriginalName());
$random = Str::random(6);
} while (File::exists($photo_destination_path . $photo_name));
$photo->move($photo_destination_path, $photo_name);
}
$message->photo = $photo_destination_path . $photo_name;
$message->created_by = $logged_in_user_id;
$message->updated_by = $logged_in_user_id;
try {
$message->save();
flash("Message has been saved")->success();
return redirect("/messageboard/");
} catch (QueryException $e) {
$errorCode = $e->errorInfo[1];
if ($errorCode == 1062) { //error code for duplicate entry to a unique field
flash("Message already exists!")->error();
return back()->withInput();
}
}
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$message = MessageBoard::where(['id' => $id])->first();
if (!$message) {
flash()->error("There is no such message");
return redirect('/messageboard/');
} else {
return view('messageboard.edit', compact('message'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'body' => 'required'
]);
if ($validator->fails()) {
$string = "";
foreach ($validator->errors()->getMessages() as $item) {
$string .= "{$item[0]}
";
}
// flash($string)->error();
return back()->withErrors($validator)->withInput();
} else {
$logged_in_user_id = Auth::user()->id;
$message = MessageBoard::find($id);
$message->body = $request->body;
$photo = $request->file('photo');
$photo_destination_path = $photo_name = '';
if ($photo) {
$photo_name = date("Ymd_H-i-s") . '_' . $photo->getClientOriginalName();
// $photo_source_path = $photo->getRealPath();
// $photo_size = $photo->getSize();
// $photo_mime_type = $photo->getMimeType();
$photo_destination_path = 'uploads/messageboard/';
$random = '';
do {
$photo_name = date("Ymd_H-i-s") . '_' . Str::slug($photo->getClientOriginalName()) . $random . '.'
. File::extension($photo->getClientOriginalName());
$random = Str::random(6);
} while (File::exists($photo_destination_path . $photo_name));
$photo->move($photo_destination_path, $photo_name);
}
$current_photo = $request->current_photo;
$new_photo = $photo_destination_path . $photo_name;
$message->photo = $new_photo ? $new_photo : $current_photo;
$message->updated_by = $logged_in_user_id;
try {
$message->save();
flash("Message has been updated")->success();
return redirect("/messageboard/");
} catch (QueryException $e) {
$errorCode = $e->errorInfo[1];
if ($errorCode == 1062) { //error code for duplicate entry to a unique field
flash("Message already exists!")->error();
return back()->withInput();
}
}
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$message = MessageBoard::find($id);
if ($message->delete()):
flash("Message has been deleted.")->success();
return redirect('/messageboard/');
endif;
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive()
{
$messages = MessageBoard::onlyTrashed()
->orderBy('id', 'desc')
->paginate(15);
if (count($messages) < 1) {
flash()->error("There is no inactive message");
return redirect('/messageboard/');
} else {
return view('messageboard.inactive', compact('messages'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id)
{
$message = MessageBoard::withTrashed()->find($id);
if ($message->restore()):
flash("Message has been activated.")->success();
return redirect('/messageboard/');
endif;
}
}