2018-04-18 00:38:11 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Auth;
|
2021-07-29 16:40:55 -04:00
|
|
|
use File;
|
|
|
|
use Image;
|
2024-03-19 17:11:58 -04:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-04-25 01:22:33 -04:00
|
|
|
use App\Traits\Timestamp;
|
2018-04-18 00:38:11 -04:00
|
|
|
|
|
|
|
class DashboardModel extends Model
|
|
|
|
{
|
2018-04-25 01:22:33 -04:00
|
|
|
use Timestamp;
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
/*
|
|
|
|
* The dashboard page type
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $dashboard_type = 'view';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dashboard heading
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $dashboard_heading = null;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether the model can be exported
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $export = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether new rows can be created
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $create = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether new rows can be deleted
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $delete = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether rows can be filtered
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $filter = true;
|
|
|
|
|
2020-04-24 00:22:42 -04:00
|
|
|
/*
|
|
|
|
* Number of items per page (0 for unlimited)
|
|
|
|
*
|
|
|
|
* @var number
|
|
|
|
*/
|
|
|
|
public static $items_per_page = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Query parameters to remember
|
|
|
|
*
|
|
|
|
* @var number
|
|
|
|
*/
|
|
|
|
public static $valid_query_params = [];
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
/*
|
|
|
|
* Dashboard help text
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $dashboard_help_text = '';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Array of columns to display in the dashboard edit list
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $dashboard_display = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to allow click-and-drag reordering
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $dashboard_reorder = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dashboard sort column
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $dashboard_sort_column = 'created_at';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dashboard sort direction (only when $dashboard_reorder == false)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $dashboard_sort_direction = 'desc';
|
|
|
|
|
|
|
|
/**
|
2020-04-24 00:22:42 -04:00
|
|
|
* The dashboard button
|
2018-04-18 00:38:11 -04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $dashboard_button = [];
|
|
|
|
|
2020-04-24 00:22:42 -04:00
|
|
|
/**
|
|
|
|
* The dashboard id link
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $dashboard_id_link = [];
|
|
|
|
|
2022-06-14 01:36:21 -04:00
|
|
|
/**
|
|
|
|
* The default image extension when none is set
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $default_image_ext = 'jpg';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functionality to run when various events occur
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
*/
|
2022-06-20 18:27:07 -04:00
|
|
|
public static function boot()
|
|
|
|
{
|
2022-06-14 01:36:21 -04:00
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::deleting(function($item) {
|
|
|
|
// delete associated images and files if they exist
|
|
|
|
foreach ($item::$dashboard_columns as $column) {
|
|
|
|
if ($column['type'] == 'image') {
|
|
|
|
$item->deleteImage($column['name'], false);
|
|
|
|
} else if ($column['type'] == 'file') {
|
|
|
|
$item->deleteFile($column['name'], false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
/**
|
|
|
|
* Returns the dashboard heading
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-07-29 16:40:55 -04:00
|
|
|
public function getDashboardHeading()
|
2018-04-18 00:38:11 -04:00
|
|
|
{
|
2021-07-29 16:40:55 -04:00
|
|
|
return static::$dashboard_heading == null ? ucfirst($this->getTable()) : static::$dashboard_heading;
|
2018-04-18 00:38:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-29 16:40:55 -04:00
|
|
|
* Return the upload path for a given type
|
|
|
|
*
|
2022-07-19 15:03:04 -04:00
|
|
|
* @return string
|
2021-07-29 16:40:55 -04:00
|
|
|
*/
|
|
|
|
public function getUploadsPath($type)
|
|
|
|
{
|
|
|
|
if ($type == 'image') {
|
|
|
|
return '/uploads/' . $this->getTable() . '/img/';
|
2023-05-24 00:02:27 -04:00
|
|
|
} else if ($type == 'thumb') {
|
|
|
|
return '/uploads/' . $this->getTable() . '/thumbnails/';
|
2021-07-29 16:40:55 -04:00
|
|
|
} else if ($type == 'file') {
|
|
|
|
return '/uploads/' . $this->getTable() . '/files/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 21:15:43 -04:00
|
|
|
/**
|
|
|
|
* Convert line breaks to <br />s
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function lineBreakToBr($string)
|
|
|
|
{
|
|
|
|
return preg_replace("/<br[^>]*>/", "<br> ", str_replace([ "\r\n", "\r", "\n" ], [ "", "", "" ], nl2br($string)));
|
|
|
|
}
|
|
|
|
|
2023-05-24 00:02:27 -04:00
|
|
|
/**
|
|
|
|
* Find the desired image dimensions based on a maximum width and height
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function maxImageSize($max_width, $max_height, $image_width, $image_height)
|
|
|
|
{
|
|
|
|
$new_width = null;
|
|
|
|
$new_height = null;
|
|
|
|
|
|
|
|
if ($max_width > 0 && $max_height > 0) {
|
|
|
|
if ($image_width > $max_width || $image_height > $max_height) {
|
|
|
|
$new_width = $max_width;
|
|
|
|
$new_height = ($new_width / $image_width) * $image_height;
|
|
|
|
|
|
|
|
if ($new_height > $max_height) {
|
|
|
|
$new_height = $max_height;
|
|
|
|
$new_width = ($new_height / $image_height) * $image_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($max_width > 0) {
|
|
|
|
if ($image_width > $max_width) {
|
|
|
|
$new_width = $max_width;
|
|
|
|
$new_height = ($new_width / $image_width) * $image_height;
|
|
|
|
}
|
|
|
|
} else if ($image_height > $max_height) {
|
|
|
|
$new_height = $max_height;
|
|
|
|
$new_width = ($new_height / $image_height) * $image_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ $new_width, $new_height ];
|
|
|
|
}
|
|
|
|
|
2021-07-29 16:40:55 -04:00
|
|
|
/**
|
|
|
|
* Save an image
|
|
|
|
*
|
2022-07-19 15:03:04 -04:00
|
|
|
* @return string
|
2021-07-29 16:40:55 -04:00
|
|
|
*/
|
|
|
|
public function saveImage($name, $file)
|
|
|
|
{
|
|
|
|
// Fail if the user doesn't have permission
|
|
|
|
if (!$this->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
$max_width = 0;
|
|
|
|
$max_height = 0;
|
|
|
|
|
|
|
|
// Retrieve the column
|
|
|
|
$column = static::getColumn($name);
|
|
|
|
|
|
|
|
// Return an error if no column is found
|
|
|
|
if ($column == null) {
|
|
|
|
return 'no-such-column-fail';
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:36:21 -04:00
|
|
|
// Use the configured image extension or fall back on the default if none is set
|
2021-07-29 16:40:55 -04:00
|
|
|
if (array_key_exists('ext', $column)) {
|
|
|
|
$main_ext = $column['ext'];
|
2022-06-14 01:36:21 -04:00
|
|
|
} else {
|
|
|
|
$main_ext = $this::$default_image_ext;
|
2021-07-29 16:40:55 -04:00
|
|
|
}
|
|
|
|
|
2023-05-24 00:02:27 -04:00
|
|
|
// Create the image directory if it doesn't exist
|
|
|
|
$image_directory = public_path($this->getUploadsPath('image'));
|
|
|
|
File::makeDirectory($image_directory, 0755, true, true);
|
2021-07-29 16:40:55 -04:00
|
|
|
|
|
|
|
// Set the base file path (including the file name but not the extension)
|
2023-05-24 00:02:27 -04:00
|
|
|
$base_image_filename = $image_directory . $this->id . '-' . $name . '.';
|
2021-07-29 16:40:55 -04:00
|
|
|
|
|
|
|
if ($main_ext == 'svg') {
|
|
|
|
// Save the image provided it's an SVG
|
|
|
|
if (gettype($file) == 'string') {
|
|
|
|
if (!preg_match('/\.svg$/i', $file)) {
|
|
|
|
return 'incorrect-format-fail';
|
|
|
|
}
|
|
|
|
|
2023-05-24 00:02:27 -04:00
|
|
|
copy($file, $base_image_filename . $main_ext);
|
2021-07-29 16:40:55 -04:00
|
|
|
} else {
|
|
|
|
if ($file->extension() != 'svg') {
|
|
|
|
return 'incorrect-format-fail';
|
|
|
|
}
|
|
|
|
|
2023-05-24 00:02:27 -04:00
|
|
|
$file->move($image_directory, $base_image_filename . $main_ext);
|
2021-07-29 16:40:55 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Update the maximum width if it's been configured
|
|
|
|
if (array_key_exists('max_width', $column)) {
|
|
|
|
$max_width = $column['max_width'];
|
|
|
|
}
|
2022-06-16 16:00:54 -04:00
|
|
|
|
2021-07-29 16:40:55 -04:00
|
|
|
// Update the maximum height if it's been configured
|
|
|
|
if (array_key_exists('max_height', $column)) {
|
|
|
|
$max_height = $column['max_height'];
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:11:58 -04:00
|
|
|
// Load and scale the image
|
|
|
|
$image = Image::read($file);
|
2021-07-29 16:40:55 -04:00
|
|
|
|
|
|
|
if ($max_width > 0 || $max_height > 0) {
|
2023-05-24 00:02:27 -04:00
|
|
|
$new_image_size = self::maxImageSize($max_width, $max_height, $image->width(), $image->height());
|
2021-07-29 16:40:55 -04:00
|
|
|
|
2023-05-24 00:02:27 -04:00
|
|
|
if (!is_null($new_image_size[0]) || !is_null($new_image_size[1])) {
|
2024-03-19 17:11:58 -04:00
|
|
|
$image->scaleDown($new_image_size[0], $new_image_size[0]);
|
2021-07-29 16:40:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 00:02:27 -04:00
|
|
|
// Save the image
|
|
|
|
$image->save($base_image_filename . $main_ext);
|
2023-05-31 23:21:14 -04:00
|
|
|
|
|
|
|
if ($main_ext !== 'webp') {
|
|
|
|
$image->save($base_image_filename . 'webp');
|
|
|
|
}
|
2023-05-24 00:02:27 -04:00
|
|
|
|
|
|
|
// Create the thumbnail directory if it doesn't exist
|
|
|
|
$thumb_directory = public_path($this->getUploadsPath('thumb'));
|
|
|
|
File::makeDirectory($thumb_directory, 0755, true, true);
|
|
|
|
|
|
|
|
// Set the base file path (including the file name but not the extension)
|
|
|
|
$base_thumb_filename = $thumb_directory . $this->id . '-' . $name . '.';
|
|
|
|
|
2024-03-19 17:11:58 -04:00
|
|
|
// Load and scale the thumbnail
|
|
|
|
$thumb = Image::read($file);
|
2023-05-24 00:02:27 -04:00
|
|
|
$new_thumb_size = self::maxImageSize(800, 600, $thumb->width(), $thumb->height());
|
|
|
|
|
|
|
|
if (!is_null($new_thumb_size[0]) || !is_null($new_thumb_size[1])) {
|
2024-03-19 17:11:58 -04:00
|
|
|
$thumb->scaleDown($new_thumb_size[0], $new_thumb_size[0]);
|
2023-05-24 00:02:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the thumbnail
|
|
|
|
$thumb->save($base_thumb_filename . $main_ext);
|
2023-05-31 23:21:14 -04:00
|
|
|
|
|
|
|
if ($main_ext !== 'webp') {
|
|
|
|
$thumb->save($base_thumb_filename . 'webp');
|
|
|
|
}
|
2021-07-29 16:40:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete an image
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function deleteImage($name, $not_exist_fail)
|
|
|
|
{
|
|
|
|
// Fail if the user doesn't have permission
|
|
|
|
if (!$this->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up our variables
|
|
|
|
$extensions = [];
|
|
|
|
|
|
|
|
// Retrieve the column
|
|
|
|
$column = static::getColumn($name);
|
|
|
|
|
|
|
|
// Return an error if no column is found
|
|
|
|
if ($column == null) {
|
|
|
|
return 'no-such-column-fail';
|
|
|
|
}
|
|
|
|
|
2022-06-14 01:36:21 -04:00
|
|
|
// Use the configured image extension or fall back on the default if none is set
|
2021-07-29 16:40:55 -04:00
|
|
|
if (array_key_exists('ext', $column)) {
|
|
|
|
$main_ext = $column['ext'];
|
2022-06-14 01:36:21 -04:00
|
|
|
} else {
|
|
|
|
$main_ext = $this::$default_image_ext;
|
2021-07-29 16:40:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the set of extensions to delete
|
|
|
|
array_push($extensions, $main_ext);
|
|
|
|
|
2023-05-31 23:21:14 -04:00
|
|
|
// If the image extension isn't svg or webp also delete the webp copy
|
|
|
|
if ($main_ext != 'svg' && $main_ext != 'webp') {
|
2021-07-29 16:40:55 -04:00
|
|
|
array_push($extensions, 'webp');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete each image
|
|
|
|
foreach ($extensions as $ext) {
|
|
|
|
// Get the full path of the image
|
|
|
|
$image = public_path($this->getUploadsPath('image') . $this->id . '-' . $name . '.' . $ext);
|
|
|
|
|
|
|
|
// Try to delete the image
|
|
|
|
if (file_exists($image)) {
|
|
|
|
if (!unlink($image)) {
|
|
|
|
return 'image-delete-fail';
|
|
|
|
}
|
|
|
|
} else if ($not_exist_fail) {
|
|
|
|
return 'image-not-exists-fail';
|
|
|
|
}
|
2023-05-31 23:21:14 -04:00
|
|
|
|
|
|
|
// Get the full path of the thumbnail
|
|
|
|
$thumb = public_path($this->getUploadsPath('thumb') . $this->id . '-' . $name . '.' . $ext);
|
|
|
|
|
|
|
|
// Try to delete the thumbnail
|
|
|
|
if (file_exists($thumb)) {
|
|
|
|
if (!unlink($thumb)) {
|
|
|
|
return 'thumb-delete-fail';
|
|
|
|
}
|
|
|
|
} else if ($not_exist_fail) {
|
|
|
|
return 'thumb-not-exists-fail';
|
|
|
|
}
|
2021-07-29 16:40:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a file
|
|
|
|
*
|
2022-07-19 15:03:04 -04:00
|
|
|
* @return string
|
2021-07-29 16:40:55 -04:00
|
|
|
*/
|
|
|
|
public function saveFile($name, $file)
|
|
|
|
{
|
|
|
|
// Fail if the user doesn't have permission
|
|
|
|
if (!$this->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the column
|
|
|
|
$column = static::getColumn($name);
|
|
|
|
|
|
|
|
// Return an error if no column is found
|
|
|
|
if ($column == null) {
|
|
|
|
return 'no-such-column-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if an ext hasn't been declared
|
|
|
|
if (!array_key_exists('ext', $column)) {
|
|
|
|
return 'no-configured-extension-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the extension
|
|
|
|
$ext = $column['ext'];
|
|
|
|
|
|
|
|
// Create the directory if it doesn't exist
|
|
|
|
$directory = public_path($this->getUploadsPath('file'));
|
|
|
|
File::makeDirectory($directory, 0755, true, true);
|
|
|
|
|
2021-09-18 00:57:11 -04:00
|
|
|
// Set the base file path (including the file name but not the extension)
|
|
|
|
$base_filename = $directory . $this->id . '-' . $name . '.';
|
|
|
|
|
2021-07-29 16:40:55 -04:00
|
|
|
// Save the file provided it's the correct extension
|
|
|
|
if (gettype($file) == 'string') {
|
|
|
|
if (!preg_match("/\.$ext/i", $file)) {
|
|
|
|
return 'incorrect-format-fail';
|
|
|
|
}
|
|
|
|
|
2021-09-18 00:57:11 -04:00
|
|
|
copy($file, $base_filename . $ext);
|
2021-07-29 16:40:55 -04:00
|
|
|
} else {
|
|
|
|
if ($file->extension() != $ext) {
|
|
|
|
return 'incorrect-format-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
$file->move($directory, $this->id . '-' . $name . '.' . $ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete a file
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function deleteFile($name, $not_exist_fail)
|
|
|
|
{
|
|
|
|
// Fail if the user doesn't have permission
|
|
|
|
if (!$this->userCheck()) {
|
|
|
|
return 'permission-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the column
|
|
|
|
$column = static::getColumn($name);
|
|
|
|
|
|
|
|
// Return an error if no column is found
|
|
|
|
if ($column == null) {
|
|
|
|
return 'no-such-column-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if an ext hasn't been declared
|
|
|
|
if (!array_key_exists('ext', $column)) {
|
|
|
|
return 'no-configured-extension-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the extension
|
|
|
|
$ext = $column['ext'];
|
|
|
|
|
|
|
|
// Get the full path of the file
|
|
|
|
$file = public_path($this->getUploadsPath('file') . $this->id . '-' . $name . '.' . $ext);
|
|
|
|
|
|
|
|
// Try to delete the file
|
|
|
|
if (file_exists($file)) {
|
|
|
|
if (!unlink($file)) {
|
|
|
|
return 'file-delete-fail';
|
|
|
|
}
|
|
|
|
} else if ($not_exist_fail) {
|
|
|
|
return 'file-not-exists-fail';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether a user column exists and whether it matches the current user if it does
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function userCheck()
|
|
|
|
{
|
|
|
|
$user_check = true;
|
|
|
|
|
|
|
|
foreach (static::$dashboard_columns as $column) {
|
|
|
|
if (array_key_exists('type', $column) && $column['type'] == 'user') {
|
|
|
|
if ($this->{$column['name']} != Auth::id()) {
|
|
|
|
$user_check = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user_check;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file extension for an image
|
|
|
|
*
|
2022-07-19 15:03:04 -04:00
|
|
|
* @return string|null
|
2021-07-29 16:40:55 -04:00
|
|
|
*/
|
|
|
|
public static function getColumn($name)
|
|
|
|
{
|
|
|
|
foreach (static::$dashboard_columns as $column) {
|
|
|
|
if ($column['name'] == $name) {
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array of column 'headings' or 'names'
|
2018-04-18 00:38:11 -04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getDashboardColumnData($type, $all_columns = true)
|
|
|
|
{
|
|
|
|
$column_data = [];
|
|
|
|
|
|
|
|
foreach (static::$dashboard_columns as $column) {
|
|
|
|
if ($all_columns || !array_key_exists('type', $column) || !preg_match('/^(hidden|user|image|file)$/', $column['type'])) {
|
|
|
|
if ($type == 'headings') {
|
|
|
|
if (array_key_exists('title', $column)) {
|
|
|
|
array_push($column_data, $column['title']);
|
|
|
|
} else {
|
|
|
|
array_push($column_data, ucfirst($column['name']));
|
|
|
|
}
|
|
|
|
} else if ($type == 'names') {
|
|
|
|
array_push($column_data, $column['name']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $column_data;
|
|
|
|
}
|
|
|
|
|
2020-04-24 00:22:42 -04:00
|
|
|
/**
|
2021-07-29 16:40:55 -04:00
|
|
|
* Perform a search against the columns in $dashboard_display
|
2020-04-24 00:22:42 -04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function searchDisplay($term, $query = null)
|
|
|
|
{
|
|
|
|
if (static::$filter) {
|
|
|
|
$first = true;
|
|
|
|
|
|
|
|
if ($query === null) {
|
|
|
|
$query = self::orderBy(static::$dashboard_sort_column, static::$dashboard_sort_direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (static::$dashboard_display as $display) {
|
|
|
|
$type = '';
|
|
|
|
|
|
|
|
foreach (static::$dashboard_columns as $column) {
|
|
|
|
if ($column['name'] === $display) {
|
|
|
|
$type = $column['type'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type !== '' && $type !== 'image') {
|
|
|
|
if ($first) {
|
|
|
|
$query->where($display, 'LIKE', '%' . $term . '%');
|
|
|
|
} else {
|
|
|
|
$query->orWhere($display, 'LIKE', '%' . $term . '%');
|
|
|
|
}
|
|
|
|
|
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
/**
|
2021-07-29 16:40:55 -04:00
|
|
|
* Return data for the dashboard
|
2018-04-18 00:38:11 -04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-04-24 00:22:42 -04:00
|
|
|
public static function getDashboardData($include_param_display = false)
|
2018-04-18 00:38:11 -04:00
|
|
|
{
|
|
|
|
$sort_direction = static::$dashboard_reorder ? 'desc' : static::$dashboard_sort_direction;
|
2018-05-01 19:58:37 -04:00
|
|
|
$query = self::orderBy(static::$dashboard_sort_column, $sort_direction);
|
2020-04-24 00:22:42 -04:00
|
|
|
$query_param_display = [];
|
2018-04-18 00:38:11 -04:00
|
|
|
|
|
|
|
foreach (static::$dashboard_columns as $column) {
|
|
|
|
if (array_key_exists('type', $column) && $column['type'] == 'user') {
|
|
|
|
$query->where($column['name'], Auth::id());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 00:22:42 -04:00
|
|
|
if (count(static::$valid_query_params) > 0) {
|
|
|
|
foreach (static::$valid_query_params as $param) {
|
|
|
|
if (request()->query($param['key'], null) != null) {
|
|
|
|
if ($include_param_display) {
|
|
|
|
$query_param_model = 'App\\Models\\' . $param['model'];
|
|
|
|
$query_param_column = $query_param_model::find(request()->query($param['key']));
|
|
|
|
|
|
|
|
if ($query_param_column !== null) {
|
|
|
|
array_push($query_param_display, [
|
|
|
|
'title' => $param['title'],
|
|
|
|
'value' => $query_param_column[$param['display']]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->where($param['column'], request()->query($param['key']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static::$items_per_page === 0) {
|
|
|
|
$results = $query->get();
|
|
|
|
} else {
|
|
|
|
if (static::$filter && request()->query('search', null) != null) {
|
|
|
|
$query = static::searchDisplay(request()->query('search'), $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = $query->paginate(static::$items_per_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($include_param_display) {
|
|
|
|
return [
|
|
|
|
'rows' => $results,
|
|
|
|
'paramdisplay' => $query_param_display
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-29 16:40:55 -04:00
|
|
|
* Retrieve the current query string containing valid query parameters
|
2020-04-24 00:22:42 -04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getQueryString()
|
|
|
|
{
|
|
|
|
$valid_query_params = static::$valid_query_params;
|
|
|
|
$string = '';
|
|
|
|
|
|
|
|
if (static::$items_per_page !== 0 && static::$filter) {
|
|
|
|
array_push($valid_query_params, [ 'key' => 'search' ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($valid_query_params as $param) {
|
|
|
|
if (request()->query($param['key'], null) != null) {
|
|
|
|
if ($string !== '') {
|
|
|
|
$string .= '&';
|
|
|
|
}
|
|
|
|
|
|
|
|
$string .= $param['key'] . '=' . request()->query($param['key']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
2018-04-18 00:38:11 -04:00
|
|
|
}
|
|
|
|
}
|