FEATURE: change directory structure
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Widgets;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
/**
|
||||
* Class Widget
|
||||
*/
|
||||
class Widget extends Model
|
||||
{
|
||||
public $timestamps = true;
|
||||
protected $fillable = [
|
||||
'widget_type_handler',
|
||||
'user_id',
|
||||
'name',
|
||||
'settings',
|
||||
'language',
|
||||
'description',
|
||||
'viewed',
|
||||
'deletable'
|
||||
];
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* Create new widget of given type
|
||||
*
|
||||
* @param WidgetType $widgetType
|
||||
* @param $data
|
||||
*
|
||||
* @return Widget
|
||||
*/
|
||||
public function createNew(WidgetType $widgetType, $data)
|
||||
{
|
||||
// convert array or whatever into object
|
||||
if (!is_object($data)) {
|
||||
$data = (object)$data;
|
||||
}
|
||||
|
||||
// create new widget
|
||||
$widget = new Widget();
|
||||
$widget->widget_type_handler = $widgetType->handler;
|
||||
$widget->language = LaravelLocalization::getCurrentLocale();
|
||||
$widget->user_id = \Auth::user()->id;
|
||||
$widget->name = $data->name;
|
||||
$widget->container_class = $data->containerClass;
|
||||
$widget->container_id = empty($data->containerId) ? null : $data->containerId;
|
||||
|
||||
// save description if available
|
||||
if (isset($data->description)) {
|
||||
$widget->description = $data->description;
|
||||
}
|
||||
|
||||
$widget = call_user_func([$widgetType->handler, 'save'], $widget, $data);
|
||||
|
||||
// save into database
|
||||
$widget->save();
|
||||
|
||||
// fire event with newly created widget
|
||||
Event::fire(new \App\Events\CreateNewWidget($widget));
|
||||
|
||||
return $widget;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Widgets;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
/**
|
||||
* Class WidgetArea
|
||||
*/
|
||||
class WidgetArea extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'code',
|
||||
'description',
|
||||
'deletable'
|
||||
];
|
||||
protected $guarded = [];
|
||||
|
||||
public function getWidgets($uniqueCode = false)
|
||||
{
|
||||
$widgets = Widget::select('widgets.*', 'widgets.widget_type_handler', 'widget_areas.code as widget_area_code', 'widgets.name as widget_name', 'widgets.settings')
|
||||
->join('widget_in_widget_area', 'widget_in_widget_area.widget_id', '=', 'widgets.id')
|
||||
->join('widget_areas', 'widget_in_widget_area.widget_area_id', '=', 'widget_areas.id')
|
||||
->where('language', LaravelLocalization::getCurrentLocale())
|
||||
->orderBy('widget_in_widget_area.position');
|
||||
if (!$uniqueCode) {
|
||||
$widgets = $widgets->where('widget_areas.viewed', '>=', setting('preloadFrequentedWidgetAreasTreshold', 10));
|
||||
} else {
|
||||
$widgets = $widgets->where('code', $uniqueCode);
|
||||
}
|
||||
|
||||
return $widgets;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Widgets;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class WidgetInWidgetArea
|
||||
*/
|
||||
class WidgetInWidgetArea extends Model
|
||||
{
|
||||
protected $table = 'widget_in_widget_area';
|
||||
public $timestamps = false;
|
||||
protected $fillable = [
|
||||
'widget_id',
|
||||
'widget_area_id',
|
||||
'position'
|
||||
];
|
||||
protected $guarded = [];
|
||||
|
||||
public static function getWidgetIdsFromArea($widgetAreaId)
|
||||
{
|
||||
$models = WidgetInWidgetArea::where('widget_area_id', $widgetAreaId)
|
||||
->get();
|
||||
|
||||
$ids = [];
|
||||
foreach ($models as $model) {
|
||||
$ids[] = $model->widget_id;
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Widgets;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class WidgetType
|
||||
*/
|
||||
class WidgetType extends Model
|
||||
{
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'handler',
|
||||
'name',
|
||||
'template_id',
|
||||
'module_id',
|
||||
'description'
|
||||
];
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user