2016-08-02 22:59:39 -04:00
|
|
|
<?php namespace App\Http\Controllers;
|
2016-01-26 23:20:08 -05:00
|
|
|
|
|
|
|
use App\Http\Requests;
|
|
|
|
use Illuminate\Http\Request;
|
2018-01-11 01:13:58 -05:00
|
|
|
use File;
|
2016-01-26 23:20:08 -05:00
|
|
|
use Image;
|
|
|
|
use Excel;
|
|
|
|
|
|
|
|
use App\Models\Contact;
|
|
|
|
use App\Models\Subscriptions;
|
|
|
|
|
2016-08-02 22:58:40 -04:00
|
|
|
class DashboardController extends Controller {
|
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the application dashboard.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return view('dashboard.home', [
|
|
|
|
'heading' => 'Dashboard Home'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard View
|
|
|
|
*/
|
|
|
|
public function getContact()
|
|
|
|
{
|
|
|
|
return view('dashboard.view', [
|
|
|
|
'heading' => 'Contact Form Submissions',
|
|
|
|
'model' => 'contact',
|
|
|
|
'rows' => Contact::getContactSubmissions(),
|
2018-01-21 19:37:11 -05:00
|
|
|
'columns' => Contact::$dashboard_columns
|
2016-01-26 23:20:08 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubscriptions()
|
|
|
|
{
|
|
|
|
return view('dashboard.view', [
|
|
|
|
'heading' => 'Subscriptions',
|
|
|
|
'model' => 'subscriptions',
|
|
|
|
'rows' => Subscriptions::getSubscriptions(),
|
2018-01-21 19:37:11 -05:00
|
|
|
'columns' => Subscriptions::$dashboard_columns
|
2016-01-26 23:20:08 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard Edit
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard Export: Export data as a spreadsheet
|
|
|
|
*/
|
|
|
|
public function getExport($model)
|
|
|
|
{
|
|
|
|
// set the name of the spreadsheet
|
|
|
|
$sheet_name = ucfirst($model);
|
|
|
|
|
|
|
|
// set the model using the 'model' request argument
|
|
|
|
switch ($model) {
|
|
|
|
case 'contact':
|
|
|
|
$headings = [ 'Date', 'Name', 'Email', 'Message' ];
|
|
|
|
$items = Contact::select('created_at', 'name', 'email', 'message')->get();
|
|
|
|
break;
|
|
|
|
case 'subscriptions':
|
2017-09-26 15:47:18 -04:00
|
|
|
$headings = [ 'Date', 'Email', 'Name' ];
|
|
|
|
$items = Subscriptions::select('created_at', 'email', 'name')->get();
|
2016-01-26 23:20:08 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
Excel::create($sheet_name, function($excel) use($sheet_name, $headings, $items) {
|
|
|
|
$excel->sheet($sheet_name, function($sheet) use($sheet_name, $headings, $items) {
|
|
|
|
$sheet->fromArray($items);
|
|
|
|
$sheet->row(1, $headings);
|
|
|
|
});
|
|
|
|
})->store('xls')->export('xls');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard Image Upload: Upload images
|
|
|
|
*/
|
|
|
|
public function postImageUpload(Request $request)
|
|
|
|
{
|
2018-01-18 22:29:49 -05:00
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'name' => 'required'
|
|
|
|
]);
|
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
if ($request->hasFile('file')) {
|
2018-01-11 01:13:58 -05:00
|
|
|
$directory = base_path() . '/public/uploads/' . $request['model'] . '/img/';
|
|
|
|
file::makeDirectory($directory, 0755, true, true);
|
2016-01-26 23:20:08 -05:00
|
|
|
$image = Image::make($request->file('file'));
|
2018-01-21 20:55:07 -05:00
|
|
|
$image->save($directory . $request['id'] . '-' . $request['name'] . '.jpg');
|
2016-01-26 23:20:08 -05:00
|
|
|
} else {
|
|
|
|
return 'file-upload-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:29:49 -05:00
|
|
|
/**
|
|
|
|
* Dashboard File Upload: Upload files
|
|
|
|
*/
|
|
|
|
public function postFileUpload(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'name' => 'required',
|
|
|
|
'ext' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($request->hasFile('file')) {
|
|
|
|
$directory = base_path() . '/public/uploads/' . $request['model'] . '/files/';
|
|
|
|
file::makeDirectory($directory, 0755, true, true);
|
2018-01-21 20:55:07 -05:00
|
|
|
$request->file('file')->move($directory, $request['id'] . '-' . $request['name'] . '.' . $request['ext']);
|
2018-01-18 22:29:49 -05:00
|
|
|
} else {
|
|
|
|
return 'file-upload-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
/**
|
|
|
|
* Dashboard Edit: Create and edit rows
|
|
|
|
*/
|
|
|
|
public function postEdit(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'columns' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
// store the id request variable for easy access
|
|
|
|
$id = $request['id'];
|
|
|
|
|
|
|
|
// set the model using the 'model' request argument
|
|
|
|
switch ($request['model']) {
|
|
|
|
default:
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate the eloquent object with the remaining items in $request
|
|
|
|
foreach ($request['columns'] as $column) {
|
|
|
|
$item->$column = $request[$column];
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the new or updated item
|
|
|
|
$item->save();
|
|
|
|
|
|
|
|
// return the id number in the format '^id:[0-9][0-9]*$' on success
|
|
|
|
return 'id:' . $item->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard Reorder: Reorder rows
|
|
|
|
*/
|
|
|
|
public function postReorder(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'order' => 'required',
|
|
|
|
'column' => 'required',
|
|
|
|
'model' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$order = $request['order'];
|
|
|
|
$column = $request['column'];
|
|
|
|
|
|
|
|
// set the model using the 'model' request argument
|
|
|
|
switch ($request['model']) {
|
|
|
|
default:
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// update each row with the new order
|
|
|
|
foreach (array_keys($order) as $order_id) {
|
|
|
|
$item = $items::find($order_id);
|
|
|
|
$item->$column = $order[$order_id];
|
|
|
|
$item->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard Delete: Delete rows
|
|
|
|
*/
|
|
|
|
public function deleteDelete(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
// set the model using the 'model' request argument
|
|
|
|
switch ($request['model']) {
|
|
|
|
default:
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the row with the id using the 'id' request argument
|
|
|
|
if ($items::where('id', $request['id'])->exists()) {
|
|
|
|
$items::where('id', $request['id'])->delete();
|
|
|
|
} else {
|
|
|
|
return 'row-delete-fail';
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:13:58 -05:00
|
|
|
// delete associated files if they exist
|
|
|
|
foreach ($items::$dashboard_columns as $column) {
|
|
|
|
if ($column['type'] == 'image') {
|
2018-01-18 22:29:49 -05:00
|
|
|
$image = base_path() . '/public/uploads/' . $request['model'] . '/img/' . $request['id'] . '-' . $column['name'] . '.jpg';
|
2018-01-10 23:58:06 -05:00
|
|
|
|
2018-01-18 22:29:49 -05:00
|
|
|
if (file_exists($image) && !unlink($image)) {
|
2018-01-11 01:13:58 -05:00
|
|
|
return 'image-delete-fail';
|
|
|
|
}
|
2018-01-18 22:29:49 -05:00
|
|
|
} else if ($column['type'] == 'file') {
|
|
|
|
$file = base_path() . '/public/uploads/' . $request['model'] . '/files/' . $request['id'] . '-' . $column['name'] . '.' . $column['ext'];
|
|
|
|
|
|
|
|
if (file_exists($file) && !unlink($file)) {
|
|
|
|
return 'file-delete-fail';
|
|
|
|
}
|
2018-01-11 01:13:58 -05:00
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a success
|
|
|
|
return 'success';
|
|
|
|
}
|
2016-08-02 22:58:40 -04:00
|
|
|
|
2018-01-21 20:55:07 -05:00
|
|
|
/**
|
|
|
|
* Dashboard Image Delete: Delete images
|
|
|
|
*/
|
|
|
|
public function deleteImageDelete(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'name' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$image = base_path() . '/public/uploads/' . $request['model'] . '/img/' . $request['id'] . '-' . $request['name'] . '.jpg';
|
|
|
|
|
|
|
|
if (!file_exists($image)) {
|
|
|
|
return 'image-not-exists-fail';
|
|
|
|
} else if (!unlink($image)) {
|
|
|
|
return 'image-delete-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard File Delete: Delete files
|
|
|
|
*/
|
|
|
|
public function deleteFileDelete(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'name' => 'required',
|
|
|
|
'ext' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$file = base_path() . '/public/uploads/' . $request['model'] . '/files/' . $request['id'] . '-' . $request['name'] . '.' . $request['ext'];
|
|
|
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
return 'file-not-exists-fail';
|
|
|
|
} else if (!unlink($file)) {
|
|
|
|
return 'file-delete-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|