chartboard/app/Models/DashboardWidgetModel.php
2026-04-09 11:29:09 +05:30

67 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class DashboardWidgetModel extends Model
{
protected $table = 'dashboard_widgets';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $protectFields = true;
protected $allowedFields = [
'dashboard_id',
'chart_id',
'widget_type',
'title',
'grid_x',
'grid_y',
'grid_w',
'grid_h',
'content',
'widget_config',
'sort_order',
];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $validationRules = [
'dashboard_id' => 'required|integer',
'widget_type' => 'required|in_list[chart,text,image,filter_date,filter_dropdown]',
'grid_x' => 'permit_empty|integer|greater_than_equal_to[0]',
'grid_y' => 'permit_empty|integer|greater_than_equal_to[0]',
'grid_w' => 'permit_empty|integer|greater_than_equal_to[1]',
'grid_h' => 'permit_empty|integer|greater_than_equal_to[1]',
];
/**
* @return list<array<string, mixed>>
*/
public function forDashboard(int $dashboardId): array
{
return $this->select('dashboard_widgets.*')
->select('charts.name AS chart_name, charts.chart_type, charts.saved_query_id')
->join('charts', 'charts.id = dashboard_widgets.chart_id', 'left')
->where('dashboard_widgets.dashboard_id', $dashboardId)
->orderBy('dashboard_widgets.grid_y', 'ASC')
->orderBy('dashboard_widgets.grid_x', 'ASC')
->orderBy('dashboard_widgets.sort_order', 'ASC')
->orderBy('dashboard_widgets.id', 'ASC')
->findAll();
}
public function deleteByDashboard(int $dashboardId): void
{
$this->where('dashboard_id', $dashboardId)->delete();
}
}