Add JSON import/export functionality for goals module

- Implement `exportToJson` and `importFromJson` methods in `goals` for serialization and deserialization.
- Add `fromJson` method in `goals_criteria` to parse criteria from JSON input.
- Extend `exampleRoute` for debugging with sample JSON-based goal creation.
- Update `index.php` to autoload new `goals` module classes and dependencies.
This commit is contained in:
Jeppe Bundgaard
2026-01-26 16:43:59 +01:00
parent 51b8198a67
commit 9312f9003e
4 changed files with 77 additions and 1 deletions
@@ -43,6 +43,31 @@ class goals_criteria implements goals_criteria_i
$this->products = new goals_criteria_products();
$this->start = new \DateTime();
$this->end = new \DateTime();
}
/**
* @throws \Exception If the JSON is invalid
*/
public static function fromJson(string $json): goals_criteria
{
$data = json_decode($json, true);
$criteria = new goals_criteria();
if (isset($data['type'])) {
$criteria->type = goals_criteria_type::tryFrom($data['type']);
}
if (isset($data['target'])) {
$criteria->target = $data['target'];
}
if (isset($data['start'])) {
$criteria->start = new \DateTime($data['start']);
}
if (isset($data['end'])) {
$criteria->end = new \DateTime($data['end']);
}
// Additional parsing for users, departments, products can be added here
return $criteria;
}
}