So finally I got back to this topic, trying something new…
I came up with the idea by using MIGX for my task. So I created a new MIGX-TV called “account-config”.
In this MIGX-TV you can enter a “name” (this shall be the name for a placeholder-output later on), the “account”-numbers (comma separated) and an “operator” for choosing math-calculations (addition/sum, subtraction, multiply, multiply with -1).
So I entered two items for testing:
name: Test1
account: 00400,00545
operator: + (sum/addition)
name: Test2
account: 00598,00945
operator: - (substraction)
Next I have a snippet called “get_account_values”. In this snippet I have this call to get all the MIGX-TV-values, that I have entered before:
$val = $modx->resource->getTVValue('account-config');
This outputs me a json-string like that:
[{"MIGX_id":"1","name":"Test1","account":"00400,00545","operator":"+"},{"MIGX_id":"2","name":"Test2","account":"00598,00945","operator":"-"}]
Then I thought it would be helpful to create an array out of the json-string for further use, so I used this:
$json_result = json_decode($val, true);
which outputs this array called “selected_accounts”:
Array
(
[0] => Array
(
[MIGX_id] => 1
[name] => Test1
[account] => 00400,00545
[operator] => +
)
[1] => Array
(
[MIGX_id] => 2
[name] => Test2
[account] => 00598,00945
[operator] => -
)
)
So far, so good…
In the same script I’m grabbing data from a db-table with the available accounts and their values. I put the data I need in another array called “all_available_accounts”.
The array looks like this:
Array
(
[0] => Array
(
[account] => 00400
[value] => 59204.0000
)
[1] => Array
(
[account] => 00545
[value] => 406351.0700
)
[2] => Array
(
[account] => 00598
[value] => -50177.0300
)
[3] => Array
(
[account] => 00650
[value] => 0.0000
)
[4] => Array
(
[account] => 00945
[value] => 14418.1200
)
[5] => Array
(
[account] => 01100
[value] => -327000.0000
)
...
)
And this is where I get stuck:
I think I need to check in the array “all_available_accounts” which accounts from the array “selected_accounts” are existing and grab their values.
I am able to achieve this by using another array “test” I created earlier while testing around:
Array
(
[0] => 00400
[1] => 00545
)
With this array “test” I grabbed the values out of the array "all_available_accounts with this line:
$only_chosen_accounts = array_intersect_key($all_available_accounts, array_flip($test));
This gives me this output:
Array
(
[00400] => 59204.0000
[00545] => 406351.0700
)
Finally I just needed to calculate these values by this:
$sum_only_chosen_accounts = array_sum($only_chosen_accounts );
which outputs the correct calculation:
465555.07
(I havent used the “operator” here so far, which is what I want to add, when the rest of the script runs like I need it to be)
My problem now is how to use this “array_intersect_key”-code on the array “all_available_accounts”.
I guess I need to use a foreach-loop with the array “selected_accounts”, look for the “account”-values that have been entered and do the “array_intersect_key” on the array "all_available_accounts" separately in the loop?
Finally I want to create a placeholder which is called like the “name” that has been entered in the MIGX-TV-items. So the final result of the calculation can be called with [[+Test1]] and the result gets outputted.
I hope this all makes sense, but it “feels” better than my first approach. Any hint will be highly appreciated!
Greetings to the comunity.