mirror of
https://github.com/prurigro/hypothetical.git
synced 2024-11-29 09:59:35 -05:00
294 lines
8.4 KiB
PHP
294 lines
8.4 KiB
PHP
<?php namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests;
|
|
use Illuminate\Http\Request;
|
|
use File;
|
|
use Image;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
use App\Models\Contact;
|
|
use App\Models\Subscriptions;
|
|
|
|
class DashboardController extends Controller {
|
|
|
|
/**
|
|
* 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(),
|
|
'columns' => Contact::$dashboard_columns
|
|
]);
|
|
}
|
|
|
|
public function getSubscriptions()
|
|
{
|
|
return view('dashboard.view', [
|
|
'heading' => 'Subscriptions',
|
|
'model' => 'subscriptions',
|
|
'rows' => Subscriptions::getSubscriptions(),
|
|
'columns' => Subscriptions::$dashboard_columns
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Dashboard Edit
|
|
*/
|
|
|
|
/**
|
|
* Dashboard Export: Export data as a spreadsheet
|
|
*/
|
|
public function getExport($model)
|
|
{
|
|
// set the filename of the spreadsheet
|
|
$filename = preg_replace([ '/\ /', '/[^a-z0-9\-]/' ], [ '-', '' ], strtolower(env('APP_NAME'))) . '-' . $model . '-' . date('m-d-Y');
|
|
|
|
// 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()->toArray();
|
|
break;
|
|
case 'subscriptions':
|
|
$headings = [ 'Date', 'Email', 'Name' ];
|
|
$items = Subscriptions::select('created_at', 'email', 'name')->get()->toArray();
|
|
break;
|
|
default:
|
|
abort(404);
|
|
}
|
|
|
|
array_unshift($items, $headings);
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(25);
|
|
$spreadsheet->getActiveSheet()->fromArray($items, NULL, 'A1');
|
|
$writer = new Xlsx($spreadsheet);
|
|
header('Content-Type: application/vnd.ms-excel');
|
|
header('Content-Disposition: attachment;filename="' . $filename . '"');
|
|
header('Cache-Control: max-age=0');
|
|
$writer->save('php://output');
|
|
}
|
|
|
|
/**
|
|
* Dashboard Image Upload: Upload images
|
|
*/
|
|
public function postImageUpload(Request $request)
|
|
{
|
|
$this->validate($request, [
|
|
'id' => 'required',
|
|
'model' => 'required',
|
|
'name' => 'required'
|
|
]);
|
|
|
|
if ($request->hasFile('file')) {
|
|
$directory = base_path() . '/public/uploads/' . $request['model'] . '/img/';
|
|
file::makeDirectory($directory, 0755, true, true);
|
|
$image = Image::make($request->file('file'));
|
|
$image->save($directory . $request['id'] . '-' . $request['name'] . '.jpg');
|
|
} else {
|
|
return 'file-upload-fail';
|
|
}
|
|
|
|
return 'success';
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
$request->file('file')->move($directory, $request['id'] . '-' . $request['name'] . '.' . $request['ext']);
|
|
} else {
|
|
return 'file-upload-fail';
|
|
}
|
|
|
|
return 'success';
|
|
}
|
|
|
|
/**
|
|
* 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';
|
|
}
|
|
|
|
// delete associated files if they exist
|
|
foreach ($items::$dashboard_columns as $column) {
|
|
if ($column['type'] == 'image') {
|
|
$image = base_path() . '/public/uploads/' . $request['model'] . '/img/' . $request['id'] . '-' . $column['name'] . '.jpg';
|
|
|
|
if (file_exists($image) && !unlink($image)) {
|
|
return 'image-delete-fail';
|
|
}
|
|
} 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';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Return a success
|
|
return 'success';
|
|
}
|
|
|
|
/**
|
|
* 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';
|
|
}
|
|
|
|
}
|