Add department_goals_o object and enhance JSON export for goals module
- Introduce `department_goals_o` for managing department-specific goals with criteria. - Enhance `goals_criteria` with `toArray` and `toJson` methods for canonical JSON export. - Update `goals` to delegate JSON export to `goals_criteria`.
This commit is contained in:
@@ -66,6 +66,11 @@ class goals
|
||||
*/
|
||||
public function exportToJson(): string
|
||||
{
|
||||
// Delegate export to the criteria to ensure canonical schema
|
||||
if (method_exists($this->criteria, 'toJson')) {
|
||||
return $this->criteria->toJson();
|
||||
}
|
||||
// Fallback (should not be used once toJson exists)
|
||||
return json_encode($this->criteria);
|
||||
}
|
||||
|
||||
|
||||
@@ -131,4 +131,41 @@ class goals_criteria implements goals_criteria_i
|
||||
|
||||
return $criteria;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the criteria as an associative array suitable for JSON encoding.
|
||||
* Ensures a canonical schema matching fromJson expectations.
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
// Normalize lists to identifiers only
|
||||
$users = $this->users?->listCustomerNumbers() ?? [];
|
||||
$departments = $this->departments?->listIDs() ?? [];
|
||||
$products = $this->products?->listIDs() ?? [];
|
||||
|
||||
// Remove nulls and duplicates just in case
|
||||
$users = array_values(array_unique(array_filter($users, fn($v) => $v !== null)));
|
||||
$departments = array_values(array_unique(array_filter($departments, fn($v) => $v !== null)));
|
||||
$products = array_values(array_unique(array_filter($products, fn($v) => $v !== null)));
|
||||
|
||||
return [
|
||||
'type' => $this->type?->name ?? goals_criteria_type::NONE->name,
|
||||
'target' => $this->target ?? 0,
|
||||
'start' => ($this->start instanceof \DateTimeInterface) ? $this->start->format(DATE_ATOM) : null,
|
||||
'end' => ($this->end instanceof \DateTimeInterface) ? $this->end->format(DATE_ATOM) : null,
|
||||
'users' => $users,
|
||||
'departments' => $departments,
|
||||
'products' => $products,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the criteria to JSON
|
||||
* @return string
|
||||
*/
|
||||
public function toJson(): string
|
||||
{
|
||||
return json_encode($this->toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace objects;
|
||||
|
||||
use classes\db;
|
||||
use classes\object_property;
|
||||
use classes\selfserve;
|
||||
use Exception;
|
||||
use goals\classes\goals_criteria;
|
||||
use traits\db_object_t;
|
||||
|
||||
class department_goals_o extends db
|
||||
{
|
||||
use db_object_t;
|
||||
public object_property $created_by; // The user_id who created the goal
|
||||
public object_property $departments; // A JSON with the department ids
|
||||
public object_property $criteria; // A JSON with the criteria object
|
||||
public object_property $created_at;
|
||||
public object_property $updated_at;
|
||||
public object_property $deleted_at;
|
||||
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
$this->setTable('goals');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a goal
|
||||
* @param users_o $created_by The user id who created the goal
|
||||
* @param departments_o[] $departments The departments for the goal
|
||||
* @param goals_criteria $criteria The criteria object
|
||||
* @return department_goals_o
|
||||
* @throws Exception If the object was not created successfully
|
||||
*/
|
||||
public function add(users_o $created_by, array $departments, goals_criteria $criteria): department_goals_o
|
||||
{
|
||||
// Add the object
|
||||
$tmp_id = self::add_object([
|
||||
'created_by' => (int)$created_by->id,
|
||||
'departments' => array_map(fn($dept) => (int)$dept->id, $departments),
|
||||
'criteria' => $criteria->toArray(),
|
||||
]);
|
||||
$this->id = $tmp_id;
|
||||
self::getObjectProperties();
|
||||
self::objectChanged();
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getObjectProperties(): void
|
||||
{
|
||||
$this->created_by = new object_property($this->table, $this->id, 'created_by', 'int', false);
|
||||
$this->departments = new object_property($this->table, $this->id, 'departments', 'json', false);
|
||||
$this->criteria = new object_property($this->table, $this->id, 'criteria', 'json', false);
|
||||
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
|
||||
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
|
||||
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
//TODO: Add cache invalidation
|
||||
}
|
||||
|
||||
public function asArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => (int)$this->id,
|
||||
'created_by' => (int)$this->created_by->value(),
|
||||
'departments' => (array)$this->departments->value(),
|
||||
'criteria' => (array)$this->criteria->value(),
|
||||
'created_at' => (string)$this->created_at->value(),
|
||||
'updated_at' => (string)$this->updated_at->value(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user