@owner Deutsche Gesellschaft für International Zusammenarbeit (GIZ) Gmb
@copyright Copyright (C) 2015. All Rights Reserved
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
/-------------------------------------------------------------------------------------------------------/
Cost Benefit Projection Tool.
/------------------------------------------------------------------------------------------------------*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.helper');
/**
* Costbenefitprojection Ajax Model
*/
class CostbenefitprojectionModelAjax extends JModelList
{
protected $app_params;
public function __construct()
{
parent::__construct();
// get params
$this->app_params = JComponentHelper::getParams('com_costbenefitprojection');
}
// [8924] Used in company
public function getCalculatedResult($id,$data)
{
return CostbenefitprojectionHelper::calculate($id,$data);
}
// [8924] Used in intervention
public function getInterventionBuildTable($idName,$oject,$cluster)
{
if (CostbenefitprojectionHelper::isJson($oject) && CostbenefitprojectionHelper::checkString($idName))
{
$array = json_decode($oject, true);
$targetHeaders = array(
'causerisk' => JText::_('COM_COSTBENEFITPROJECTION_CAUSERISK'),
'cpe' => JText::_('COM_COSTBENEFITPROJECTION_COST_PER_EMPLOYEE'),
'mbr' => JText::_('COM_COSTBENEFITPROJECTION_MORBIDITY_REDUCTION'),
'mtr' => JText::_('COM_COSTBENEFITPROJECTION_MORTALITY_REDUCTION'));
if (CostbenefitprojectionHelper::checkArray($array))
{
$table = '
';
// return the table
return $table;
}
}
return false;
}
public function getClusterData($idName,$cluster)
{
// we first build the json object from the cluster ids, then pass it to the builder
$oject = '';
$oject_table = '';
if (CostbenefitprojectionHelper::isJson($cluster) && CostbenefitprojectionHelper::checkString($idName))
{
$array = json_decode($cluster, true);
// get te set intervention data
$interventions = array();
if (CostbenefitprojectionHelper::checkArray($array))
{
foreach ($array as $intervention)
{
$interventions[$intervention] = CostbenefitprojectionHelper::getVar('intervention', $intervention, 'id', 'intervention');
}
}
// sort the data
$bucket = array();
if (CostbenefitprojectionHelper::checkArray($interventions))
{
foreach ($interventions as $inter => $set)
{
if (CostbenefitprojectionHelper::isJson($set))
{
$set = json_decode($set, true);
if (CostbenefitprojectionHelper::checkArray($set))
{
foreach ($set as $option => $values)
{
foreach ($values as $nr => $value)
{
$bucket[$inter][$nr][$option] = $value;
}
}
}
}
}
}
// combine the data
$combine = array();
if (CostbenefitprojectionHelper::checkArray($bucket))
{
foreach ($bucket as $pool)
{
if (CostbenefitprojectionHelper::checkArray($pool))
{
foreach ($pool as $headers)
{
if (CostbenefitprojectionHelper::checkArray($headers))
{
// check if this cause is already targeted
if (isset($combine[$headers['causerisk']]))
{
// combine
$temp = $combine[$headers['causerisk']];
$temp['cpe'] = $this->combineValues($temp['cpe'], $headers['cpe']);
$temp['mbr'] = $this->combineValues($temp['mbr'], $headers['mbr']);
$temp['mtr'] = $this->combineValues($temp['mtr'], $headers['mtr']);
// update the data
$combine[$headers['causerisk']] = $temp;
}
else
{
// set for first time
$combine[$headers['causerisk']] = array(
'cpe' => $headers['cpe'],
'mbr' => $headers['mbr'],
'mtr' => $headers['mtr']
);
}
}
}
}
}
}
// setup the object
if (CostbenefitprojectionHelper::checkArray($combine))
{
$oject = array();
foreach ($combine as $causerisk => $vals)
{
if (CostbenefitprojectionHelper::checkArray($vals))
{
$oject['causerisk'][] = $causerisk;
foreach ($vals as $header => $v)
{
// set placeholder
$oject[$header][] = $v;
}
}
}
// done at last
$oject = json_encode($oject);
}
}
// return the table and values
return array('table' => $this->getInterventionBuildTable($idName,$oject,'ja'), 'values' => $oject);
}
protected function combineValues($old,$new)
{
$old = trim($old);
$new = trim($new);
if ($old != $new)
{
if (strpos($old, '&') !== false)
{
$oldArray = array_map('trim',explode('&', $old));
foreach ($oldArray as $nr => $oldValue)
{
if ($oldValue == $new)
{
unset($oldArray[$nr]);
}
}
$old = implode(' & ', $oldArray);
}
return $old.' & '.$new;
}
return $new;
}
}