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;
|
2018-04-02 22:52:09 -04:00
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
2018-04-18 00:38:11 -04:00
|
|
|
use App\Models\Dashboard;
|
2016-01-26 23:20:08 -05:00
|
|
|
|
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
|
|
|
|
*/
|
2018-04-18 00:38:11 -04:00
|
|
|
public function getView($model)
|
2016-01-26 23:20:08 -05:00
|
|
|
{
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($model, 'view');
|
|
|
|
|
|
|
|
if ($model_class != null) {
|
|
|
|
return view('dashboard.view', [
|
|
|
|
'heading' => $model_class::getDashboardHeading($model),
|
|
|
|
'column_headings' => $model_class::getDashboardColumnData('headings'),
|
|
|
|
'model' => $model,
|
|
|
|
'rows' => $model_class::getDashboardData(),
|
|
|
|
'columns' => $model_class::$dashboard_columns
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
abort(404);
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
/**
|
|
|
|
* Dashboard Edit List
|
|
|
|
*/
|
|
|
|
public function getEditList($model)
|
2016-01-26 23:20:08 -05:00
|
|
|
{
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($model, 'edit');
|
|
|
|
|
|
|
|
if ($model_class != null) {
|
|
|
|
return view('dashboard.edit-list', [
|
|
|
|
'heading' => $model_class::getDashboardHeading($model),
|
|
|
|
'model' => $model,
|
|
|
|
'rows' => $model_class::getDashboardData(),
|
|
|
|
'display' => $model_class::$dashboard_display,
|
|
|
|
'button' => $model_class::$dashboard_button,
|
|
|
|
'sortcol' => $model_class::$dashboard_reorder ? $model_class::$dashboard_sort_column : false,
|
|
|
|
'create' => $model_class::$create,
|
|
|
|
'delete' => $model_class::$delete,
|
|
|
|
'filter' => $model_class::$filter,
|
|
|
|
'export' => $model_class::$export
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
abort(404);
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-18 00:38:11 -04:00
|
|
|
* Dashboard Edit Item
|
2016-01-26 23:20:08 -05:00
|
|
|
*/
|
2018-04-18 00:38:11 -04:00
|
|
|
public function getEditItem($model, $id = 'new')
|
|
|
|
{
|
|
|
|
$model_class = Dashboard::getModel($model, 'edit');
|
|
|
|
|
|
|
|
if ($model_class != null) {
|
|
|
|
if ($id == 'new') {
|
|
|
|
$item = null;
|
|
|
|
} else {
|
|
|
|
if ($model_class::where('id', $id)->exists()) {
|
|
|
|
$item = $model_class::find($id);
|
|
|
|
|
|
|
|
if (is_null($item) || !$item->userCheck()) {
|
|
|
|
return view('errors.no-such-record');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return view('errors.no-such-record');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('dashboard.edit-item', [
|
|
|
|
'heading' => $model_class::getDashboardHeading($model),
|
|
|
|
'model' => $model,
|
|
|
|
'id' => $id,
|
|
|
|
'item' => $item,
|
|
|
|
'help_text' => $model_class::$dashboard_help_text,
|
|
|
|
'columns' => $model_class::$dashboard_columns
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard Export: Export data as a spreadsheet
|
|
|
|
*/
|
|
|
|
public function getExport($model)
|
|
|
|
{
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($model);
|
|
|
|
|
|
|
|
if ($model_class != null && $model_class::$export) {
|
|
|
|
$filename = preg_replace([ '/\ /', '/[^a-z0-9\-]/' ], [ '-', '' ], strtolower(env('APP_NAME'))) . '-' . $model . '-' . date('m-d-Y');
|
|
|
|
$headings = $model_class::getDashboardColumnData('headings', false);
|
|
|
|
$items = $model_class::select($model_class::getDashboardColumnData('names', false))->get()->toArray();
|
|
|
|
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');
|
|
|
|
} else {
|
|
|
|
abort(404);
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-18 00:38:11 -04:00
|
|
|
* Dashboard Reorder: Reorder rows
|
2016-01-26 23:20:08 -05:00
|
|
|
*/
|
2018-04-18 00:38:11 -04:00
|
|
|
public function postReorder(Request $request)
|
2016-01-26 23:20:08 -05:00
|
|
|
{
|
2018-01-18 22:29:49 -05:00
|
|
|
$this->validate($request, [
|
2018-04-18 00:38:11 -04:00
|
|
|
'order' => 'required',
|
|
|
|
'column' => 'required',
|
|
|
|
'model' => 'required'
|
2018-01-18 22:29:49 -05:00
|
|
|
]);
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($request['model'], 'edit');
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if ($model_class != null) {
|
|
|
|
$order = $request['order'];
|
|
|
|
$column = $request['column'];
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
// update each row with the new order
|
|
|
|
foreach (array_keys($order) as $order_id) {
|
|
|
|
$item = $model_class::find($order_id);
|
|
|
|
$item->$column = $order[$order_id];
|
|
|
|
$item->save();
|
|
|
|
}
|
2018-01-18 22:29:49 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
return 'success';
|
2018-01-18 22:29:49 -05:00
|
|
|
} else {
|
2018-04-18 00:38:11 -04:00
|
|
|
return 'model-access-fail';
|
2018-01-18 22:29:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
/**
|
2018-04-18 00:38:11 -04:00
|
|
|
* Dashboard Update: Create and update rows
|
2016-01-26 23:20:08 -05:00
|
|
|
*/
|
2018-04-18 00:38:11 -04:00
|
|
|
public function postUpdate(Request $request)
|
2016-01-26 23:20:08 -05:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'columns' => 'required'
|
|
|
|
]);
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($request['model'], 'edit');
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if ($model_class != null) {
|
|
|
|
if ($request['id'] == 'new') {
|
|
|
|
$item = new $model_class;
|
|
|
|
} else {
|
|
|
|
$item = $model_class::find($request['id']);
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if (is_null($item)) {
|
|
|
|
return 'record-access-fail';
|
|
|
|
} else if (!$item->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate the eloquent object with the remaining items in $request
|
|
|
|
foreach ($request['columns'] as $column) {
|
|
|
|
$item->$column = $request[$column];
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
// save the new or updated item
|
|
|
|
$item->save();
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
// return the id number in the format '^id:[0-9][0-9]*$' on success
|
|
|
|
return 'id:' . $item->id;
|
|
|
|
} else {
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-18 00:38:11 -04:00
|
|
|
* Dashboard Image Upload: Upload images
|
2016-01-26 23:20:08 -05:00
|
|
|
*/
|
2018-04-18 00:38:11 -04:00
|
|
|
public function postImageUpload(Request $request)
|
2016-01-26 23:20:08 -05:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
2018-04-18 00:38:11 -04:00
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'name' => 'required'
|
2016-01-26 23:20:08 -05:00
|
|
|
]);
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($request['model'], 'edit');
|
|
|
|
|
|
|
|
if ($model_class != null) {
|
|
|
|
$item = $model_class::find($request['id']);
|
|
|
|
|
|
|
|
if (is_null($item)) {
|
|
|
|
return 'record-access-fail';
|
|
|
|
} else if (!$item->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
} else 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';
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
return 'success';
|
|
|
|
} else {
|
|
|
|
return 'model-access-fail';
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
2018-04-18 00:38:11 -04:00
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
/**
|
|
|
|
* Dashboard File Upload: Upload files
|
|
|
|
*/
|
|
|
|
public function postFileUpload(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'name' => 'required',
|
|
|
|
'ext' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$model_class = Dashboard::getModel($request['model'], 'edit');
|
|
|
|
|
|
|
|
if ($model_class != null) {
|
|
|
|
$item = $model_class::find($request['id']);
|
|
|
|
|
|
|
|
if (is_null($item)) {
|
|
|
|
return 'record-access-fail';
|
|
|
|
} else if (!$item->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
} else 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';
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
return 'success';
|
|
|
|
} else {
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard Delete: Delete rows
|
|
|
|
*/
|
|
|
|
public function deleteDelete(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required'
|
|
|
|
]);
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($request['model'], 'edit');
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if ($model_class != null) {
|
|
|
|
$item = $model_class::find($request['id']);
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if (is_null($item)) {
|
|
|
|
return 'record-access-fail';
|
|
|
|
} else if (!$item->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
}
|
2018-01-10 23:58:06 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
// delete the row
|
|
|
|
$item->delete();
|
2018-01-18 22:29:49 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
// delete associated files if they exist
|
|
|
|
foreach ($model_class::$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';
|
|
|
|
}
|
2018-01-18 22:29:49 -05:00
|
|
|
}
|
2018-01-11 01:13:58 -05:00
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
// Return a success
|
|
|
|
return 'success';
|
|
|
|
} else {
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|
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'
|
|
|
|
]);
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($request['model'], 'edit');
|
2018-01-21 20:55:07 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if ($model_class != null) {
|
|
|
|
$image = base_path() . '/public/uploads/' . $request['model'] . '/img/' . $request['id'] . '-' . $request['name'] . '.jpg';
|
|
|
|
$item = $model_class::find($request['id']);
|
2018-01-21 20:55:07 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if (is_null($item)) {
|
|
|
|
return 'record-access-fail';
|
|
|
|
} else if (!$item->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
} else if (!file_exists($image)) {
|
|
|
|
return 'image-not-exists-fail';
|
|
|
|
} else if (!unlink($image)) {
|
|
|
|
return 'image-delete-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
} else {
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
2018-01-21 20:55:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard File Delete: Delete files
|
|
|
|
*/
|
|
|
|
public function deleteFileDelete(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'id' => 'required',
|
|
|
|
'model' => 'required',
|
|
|
|
'name' => 'required',
|
|
|
|
'ext' => 'required'
|
|
|
|
]);
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
$model_class = Dashboard::getModel($request['model'], 'edit');
|
2018-01-21 20:55:07 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
if ($model_class != null) {
|
|
|
|
$file = base_path() . '/public/uploads/' . $request['model'] . '/files/' . $request['id'] . '-' . $request['name'] . '.' . $request['ext'];
|
|
|
|
$item = $model_class::find($request['id']);
|
|
|
|
|
|
|
|
if (is_null($item)) {
|
|
|
|
return 'record-access-fail';
|
|
|
|
} else if (!$item->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
} else if (!file_exists($file)) {
|
|
|
|
return 'file-not-exists-fail';
|
|
|
|
} else if (!unlink($file)) {
|
|
|
|
return 'file-delete-fail';
|
|
|
|
}
|
2018-01-21 20:55:07 -05:00
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
return 'success';
|
|
|
|
} else {
|
|
|
|
return 'model-access-fail';
|
|
|
|
}
|
2018-01-21 20:55:07 -05:00
|
|
|
}
|
|
|
|
|
2016-01-26 23:20:08 -05:00
|
|
|
}
|