236 lines
11 KiB
PHP
236 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
class ActionsheetController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'formreminders';
|
|
$this->_module = 'action_sheet';
|
|
$this->_viewname = 'actionsheet';
|
|
$this->_title = 'Action Sheets';
|
|
$this->_base_url = '/actionsheet';
|
|
}
|
|
|
|
protected function get_list_fields()
|
|
{
|
|
return [
|
|
'actions' => ['title' => 'Actions', 'buttons' => ['edit', 'delete']],
|
|
'title' => ['title' => 'Title', 'type' => 'text'],
|
|
'period' => ['title' => 'Period', 'type' => 'text'],
|
|
'interval' => ['title' => 'Interval', 'type' => 'text'],
|
|
'sort_order' => ['title' => 'Sort Order', 'type' => 'number'],
|
|
'show_popup' => ['title' => 'Show Popup', 'type' => 'text'],
|
|
'status' => ['title' => 'Status', 'type' => 'select'],
|
|
];
|
|
}
|
|
|
|
protected function get_form_fields($type, $values)
|
|
{
|
|
$roles = $this->getlambda('get', 'ums', 'roles', 'dropdown');
|
|
$roles_dd = [];
|
|
foreach (($roles->data->items ?? []) as $role) {
|
|
$roles_dd[$role->id] = $role->name;
|
|
}
|
|
|
|
$metrics = $this->getlambda('get', 'metric', 'metric', 'get_all');
|
|
$metrics_dd = [];
|
|
foreach (($metrics->data->items ?? []) as $metric) {
|
|
$name = $metric->name ?? ($metric->title ?? $metric->id);
|
|
$metrics_dd[$metric->id] = $name;
|
|
}
|
|
|
|
return [
|
|
'id' => ['title' => '', 'type' => 'hidden'],
|
|
'title' => ['title' => 'Title', 'type' => 'text', 'required' => true],
|
|
'sort_order' => ['title' => 'Sort Order', 'type' => 'number', 'required' => false],
|
|
'entity_id' => ['title' => 'Entity ID', 'type' => 'text', 'required' => false],
|
|
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'], 'default' => 'active'],
|
|
'interval_start' => ['title' => 'Interval Start', 'type' => 'date', 'required' => true],
|
|
'interval_end' => ['title' => 'Interval End', 'type' => 'date', 'required' => true],
|
|
'period' => ['title' => 'Period', 'type' => 'select', 'required' => true, 'values' => [
|
|
'daily' => 'Daily',
|
|
'weekly' => 'Weekly',
|
|
'monthly' => 'Monthly',
|
|
'yearly' => 'Yearly',
|
|
'custom' => 'Custom',
|
|
'overall' => 'Overall',
|
|
], 'default' => 'monthly'],
|
|
'custom_start_date' => ['title' => 'Custom Start Date', 'type' => 'date', 'required' => false],
|
|
'custom_end_date' => ['title' => 'Custom End Date', 'type' => 'date', 'required' => false],
|
|
'visible_roles' => ['title' => 'Visible Roles', 'type' => 'multiselect', 'values' => $roles_dd, 'required' => true],
|
|
'reporting_roles' => ['title' => 'Reporting Roles', 'type' => 'multiselect', 'values' => $roles_dd, 'required' => true],
|
|
'model_name' => ['title' => 'Model', 'type' => 'select', 'required' => true, 'values' => [
|
|
'metric_aggregation' => 'Metric Aggregation',
|
|
'rapport_roles_leaderboard' => 'Roles Leaderboard',
|
|
'rapport_metric_views' => 'Metric Views',
|
|
'rapport_campaign_view' => 'Campaign View',
|
|
'rapport_milestones' => 'Milestones',
|
|
], 'default' => 'metric_aggregation'],
|
|
'aggregation_type' => ['title' => 'Aggregation Type', 'type' => 'select', 'required' => true, 'values' => ['sum' => 'Sum', 'count' => 'Count', 'avg' => 'Average'], 'default' => 'sum'],
|
|
'show_aggregation_data' => ['title' => 'Show Aggregation Data', 'type' => 'select', 'required' => true, 'values' => ['login' => 'Login hierarchy', 'metric' => 'All reporting roles'], 'default' => 'login'],
|
|
'leaderboard_chart' => ['title' => 'Leaderboard Chart', 'type' => 'text', 'required' => false],
|
|
'leaderboard_type' => ['title' => 'Leaderboard Type', 'type' => 'text', 'required' => false],
|
|
'metrics' => ['title' => 'Metrics', 'type' => 'sheet_metrics', 'required' => false, 'fullwidth' => true, 'metrics' => $metrics_dd],
|
|
'html_format' => ['title' => 'HTML Format', 'type' => 'textarea', 'required' => false, 'fullwidth' => true],
|
|
'show_popup' => ['title' => 'Show Popup', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No'], 'default' => 'no'],
|
|
'repeat_popup' => ['title' => 'Repeat Popup', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No'], 'default' => 'no'],
|
|
'repeat_value' => ['title' => 'Repeat Value', 'type' => 'number', 'required' => false, 'default' => 1],
|
|
'repeat_period' => ['title' => 'Repeat Period', 'type' => 'select', 'required' => false, 'values' => ['hourly' => 'Hourly', 'daily' => 'Daily', 'weekly' => 'Weekly', 'monthly' => 'Monthly'], 'default' => 'daily'],
|
|
];
|
|
}
|
|
|
|
protected function _beforeform($type, $values)
|
|
{
|
|
if ($type !== 'edit' || !is_object($values)) {
|
|
return $values;
|
|
}
|
|
|
|
$toArray = function ($value) {
|
|
if (!isset($value) || $value === null || $value === '') {
|
|
return [];
|
|
}
|
|
if (is_array($value)) {
|
|
return array_values(array_filter($value, function ($item) {
|
|
return $item !== '' && $item !== null;
|
|
}));
|
|
}
|
|
if (is_object($value)) {
|
|
return array_values((array) $value);
|
|
}
|
|
return array_values(array_filter(explode('#', (string) $value), function ($item) {
|
|
return $item !== '';
|
|
}));
|
|
};
|
|
|
|
$values->repeat_popup = $values->repeat_popup ?? 'no';
|
|
$values->repeat_value = $values->repeat_value ?? 1;
|
|
$values->repeat_period = $values->repeat_period ?? 'daily';
|
|
|
|
$values->visible_roles = $toArray($values->visible_roles ?? null);
|
|
$values->reporting_roles = $toArray($values->reporting_roles ?? null);
|
|
|
|
$parts = explode('_', (string) ($values->interval ?? ''), 2);
|
|
$values->interval_start = $parts[0] ?? '';
|
|
$values->interval_end = $parts[1] ?? '';
|
|
|
|
$aggregation = json_decode(json_encode($values->aggregation ?? new \stdClass()), true) ?: [];
|
|
$values->aggregation_type = $aggregation['aggregation_type'] ?? 'sum';
|
|
$values->show_aggregation_data = $aggregation['show_aggregation_data'] ?? 'login';
|
|
$values->leaderboard_chart = $aggregation['leaderboard_chart'] ?? '';
|
|
$values->leaderboard_type = $aggregation['leaderboard_type'] ?? '';
|
|
|
|
$metrics = json_decode(json_encode($values->metrics ?? []), true) ?: [];
|
|
$values->metrics = array_values(array_map(function ($row) use ($toArray) {
|
|
if (!is_array($row)) {
|
|
$row = [];
|
|
}
|
|
$row['metric_ids'] = $toArray($row['metric_ids'] ?? []);
|
|
return $row;
|
|
}, $metrics));
|
|
|
|
return $values;
|
|
}
|
|
|
|
protected function _beforesave($type, $request)
|
|
{
|
|
$data = $request->except('_token');
|
|
|
|
if ($type === 'edit') {
|
|
if (empty($data['id'])) {
|
|
$data['id'] = $request->query('id') ?: $request->input('id');
|
|
}
|
|
} else {
|
|
unset($data['id']);
|
|
}
|
|
|
|
$normalizeList = function ($value) {
|
|
if (!isset($value) || $value === null || $value === '') {
|
|
return [];
|
|
}
|
|
if (!is_array($value)) {
|
|
$value = explode('#', (string) $value);
|
|
}
|
|
return array_values(array_filter($value, function ($item) {
|
|
return $item !== '' && $item !== null;
|
|
}));
|
|
};
|
|
|
|
$data['sort_order'] = isset($data['sort_order']) && $data['sort_order'] !== '' ? (int) $data['sort_order'] : 0;
|
|
$data['entity_id'] = $data['entity_id'] ?? '';
|
|
$data['status'] = $data['status'] ?? 'active';
|
|
$data['show_popup'] = $data['show_popup'] ?? 'no';
|
|
$data['repeat_popup'] = $data['repeat_popup'] ?? 'no';
|
|
$data['repeat_value'] = isset($data['repeat_value']) && $data['repeat_value'] !== '' ? (int) $data['repeat_value'] : 1;
|
|
$data['repeat_period'] = $data['repeat_period'] ?? 'daily';
|
|
$data['html_format'] = $data['html_format'] ?? '';
|
|
$data['interval'] = $this->toIsoDate($data['interval_start'] ?? '') . '_' . $this->toIsoDate($data['interval_end'] ?? '');
|
|
$data['custom_start_date'] = $this->toIsoDate($data['custom_start_date'] ?? '');
|
|
$data['custom_end_date'] = $this->toIsoDate($data['custom_end_date'] ?? '');
|
|
|
|
$data['visible_roles'] = $normalizeList($data['visible_roles'] ?? []);
|
|
$data['reporting_roles'] = $normalizeList($data['reporting_roles'] ?? []);
|
|
|
|
$data['aggregation'] = [
|
|
'aggregation_type' => $data['aggregation_type'] ?? 'sum',
|
|
'show_aggregation_data' => $data['show_aggregation_data'] ?? 'login',
|
|
'leaderboard_chart' => $data['leaderboard_chart'] ?? '',
|
|
'leaderboard_type' => $data['leaderboard_type'] ?? '',
|
|
];
|
|
|
|
$metrics = [];
|
|
foreach (($data['metrics'] ?? []) as $row) {
|
|
if (!is_array($row)) {
|
|
continue;
|
|
}
|
|
$id = trim((string) ($row['id'] ?? ''));
|
|
$title = trim((string) ($row['title'] ?? ''));
|
|
$label = trim((string) ($row['label'] ?? ''));
|
|
$metricIds = $normalizeList($row['metric_ids'] ?? []);
|
|
if ($id === '' && $title === '' && $label === '' && empty($metricIds)) {
|
|
continue;
|
|
}
|
|
$metrics[] = [
|
|
'id' => $id,
|
|
'title' => $title,
|
|
'label' => $label,
|
|
'metric_ids' => $metricIds,
|
|
];
|
|
}
|
|
$data['metrics'] = $metrics;
|
|
|
|
unset(
|
|
$data['interval_start'],
|
|
$data['interval_end'],
|
|
$data['aggregation_type'],
|
|
$data['show_aggregation_data'],
|
|
$data['leaderboard_chart'],
|
|
$data['leaderboard_type']
|
|
);
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function toIsoDate($value)
|
|
{
|
|
$value = trim((string) $value);
|
|
if ($value === '') {
|
|
return '';
|
|
}
|
|
foreach (['Y-m-d', 'm-d-Y', 'm/d/Y', 'd-m-Y'] as $format) {
|
|
try {
|
|
$date = Carbon::createFromFormat($format, $value);
|
|
if ($date !== false) {
|
|
return $date->format('Y-m-d');
|
|
}
|
|
} catch (\Exception $e) {
|
|
}
|
|
}
|
|
return $value;
|
|
}
|
|
}
|