84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class ChartModel extends Model
|
|
{
|
|
protected $table = 'charts';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = true;
|
|
protected $protectFields = true;
|
|
|
|
protected $allowedFields = [
|
|
'workspace_id',
|
|
'data_source_id',
|
|
'saved_query_id',
|
|
'name',
|
|
'description',
|
|
'chart_type',
|
|
'query_type',
|
|
'raw_sql',
|
|
'visual_config',
|
|
'api_endpoint',
|
|
'api_params',
|
|
'response_path',
|
|
'field_map',
|
|
'x_field',
|
|
'y_field',
|
|
'group_field',
|
|
'value_field',
|
|
'display_config',
|
|
'refresh_interval',
|
|
'cache_ttl',
|
|
'is_public',
|
|
'public_token',
|
|
'created_by',
|
|
];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected $useTimestamps = true;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
protected $validationRules = [
|
|
'workspace_id' => 'required|integer',
|
|
'data_source_id' => 'required|integer',
|
|
'name' => 'required|min_length[2]|max_length[200]',
|
|
'chart_type' => 'required|in_list[bar,line,area,pie,donut,scatter,table,kpi_card,funnel,gauge,heatmap,combo,spline,stepline,radar,bubble,polar_area,ranked_progress]',
|
|
'query_type' => 'permit_empty|in_list[visual,raw_sql,api]',
|
|
];
|
|
|
|
public function forWorkspace(int $workspaceId): array
|
|
{
|
|
return $this->select('charts.*, data_sources.name as data_source_name, saved_queries.name as saved_query_name')
|
|
->join('data_sources', 'data_sources.id = charts.data_source_id', 'left')
|
|
->join('saved_queries', 'saved_queries.id = charts.saved_query_id', 'left')
|
|
->where('charts.workspace_id', $workspaceId)
|
|
->orderBy('charts.id', 'DESC')
|
|
->findAll();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public function duplicateRow(array $source, int $userId): int|false
|
|
{
|
|
$copy = $source;
|
|
unset($copy['id'], $copy['created_at'], $copy['updated_at'], $copy['deleted_at']);
|
|
$copy['name'] = (string) $copy['name'] . ' (copy)';
|
|
$copy['public_token'] = null;
|
|
$copy['is_public'] = 0;
|
|
$copy['created_by'] = $userId;
|
|
|
|
return $this->insert($copy, true);
|
|
}
|
|
}
|