Enhance goals_criteria to parse users, departments, and products from input arrays
- Add parsing logic for users, departments, and products, supporting array inputs with IDs or objects. - Filter out invalid entries and update relevant criteria properties.
This commit is contained in:
@@ -66,7 +66,68 @@ class goals_criteria implements goals_criteria_i
|
||||
if (isset($data['end'])) {
|
||||
$criteria->end = new \DateTime($data['end']);
|
||||
}
|
||||
// Additional parsing for users, departments, products can be added here
|
||||
// Parse users (expects array of customer_numbers or objects with customer_number)
|
||||
if (isset($data['users']) && is_array($data['users'])) {
|
||||
$users = array_map(function ($item) {
|
||||
$customerNumber = null;
|
||||
if (is_array($item)) {
|
||||
if (isset($item['customer_number'])) {
|
||||
$customerNumber = (int)$item['customer_number'];
|
||||
} elseif (isset($item['customerNumber'])) { // allow camelCase
|
||||
$customerNumber = (int)$item['customerNumber'];
|
||||
}
|
||||
} elseif (is_numeric($item)) {
|
||||
$customerNumber = (int)$item;
|
||||
}
|
||||
if ($customerNumber === null) {
|
||||
return null;
|
||||
}
|
||||
return (new \objects\users_o())->getUserByCustomerNumber($customerNumber);
|
||||
}, $data['users']);
|
||||
// Filter out nulls in case of malformed entries
|
||||
$users = array_values(array_filter($users));
|
||||
$criteria->users->set($users);
|
||||
}
|
||||
|
||||
// Parse departments (expects array of IDs or objects with id)
|
||||
if (isset($data['departments']) && is_array($data['departments'])) {
|
||||
$departments = array_map(function ($item) {
|
||||
$id = null;
|
||||
if (is_array($item)) {
|
||||
if (isset($item['id'])) {
|
||||
$id = (int)$item['id'];
|
||||
}
|
||||
} elseif (is_numeric($item)) {
|
||||
$id = (int)$item;
|
||||
}
|
||||
if ($id === null) {
|
||||
return null;
|
||||
}
|
||||
return (new \objects\departments_o())->select($id);
|
||||
}, $data['departments']);
|
||||
$departments = array_values(array_filter($departments));
|
||||
$criteria->departments->set($departments);
|
||||
}
|
||||
|
||||
// Parse products (expects array of IDs or objects with id)
|
||||
if (isset($data['products']) && is_array($data['products'])) {
|
||||
$products = array_map(function ($item) {
|
||||
$id = null;
|
||||
if (is_array($item)) {
|
||||
if (isset($item['id'])) {
|
||||
$id = (int)$item['id'];
|
||||
}
|
||||
} elseif (is_numeric($item)) {
|
||||
$id = (int)$item;
|
||||
}
|
||||
if ($id === null) {
|
||||
return null;
|
||||
}
|
||||
return (new \objects\products_o())->select($id);
|
||||
}, $data['products']);
|
||||
$products = array_values(array_filter($products));
|
||||
$criteria->products->set($products);
|
||||
}
|
||||
|
||||
return $criteria;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user