public function prepareData()
{
$items = array();
foreach($this->configs as $config)
{
/** @var WidgetConfig $config */
$name = $config->getName();
if($name === '')
{
$name = strval(count($items) + 1);
}
$title = $config->getTitle();
if($title === '')
{
$title = $name;
}
$this->filter->setExtras($config->getFilterParams());
$selectField = $config->getSelectField();
$aggregate = $config->getAggregate();
$groupField = $this->groupField !== '' ? $this->groupField : $config->getGroupField();
$sourceSettings = $config->getDataSourceSettings();
if(!DataSourceFactory::checkSettings($sourceSettings))
{
$values = array();
}
else
{
$source = DataSourceFactory::create($sourceSettings, $this->userID);
$source->setFilterContextData($this->getFilterContextData());
//Disable permission check for not found context
$enablePermissionCheck = $source->getDataContext() === DataContext::FUND
? $this->enablePermissionCheck : false;
$source->enablePermissionCheck($enablePermissionCheck);
$values = $source->getList(
array(
'filter' => $this->filter,
'select' => array(array('name' => $selectField, 'aggregate' => $aggregate)),
'group' => $groupField,
'sort' => array(array('name' => $selectField, 'order' => 'desc'))
)
);
}
$positions = array();
$key = "{$groupField}_ID";
$qty = count($values);
if($this->nomineeID > 0)
{
for($i = 0; $i < $qty; $i++)
{
$value = $values[$i];
$curID = isset($value[$key]) ? (int)$value[$key] : 0;
if($curID !== $this->nomineeID)
{
continue;
}
if($i > 0)
{
if($qty > ($i + 1))
{
$positions[] = $this->preparePosition($values, $i - 1, $key, $selectField, $config);
$positions[] = $this->preparePosition($values, $i, $key, $selectField, $config);
$positions[] = $this->preparePosition($values, $i + 1, $key, $selectField, $config);
}
else
{
if($i >= 2)
{
$positions[] = $this->preparePosition($values, $i - 2, $key, $selectField, $config);
}
$positions[] = $this->preparePosition($values, $i - 1, $key, $selectField, $config);
$positions[] = $this->preparePosition($values, $i, $key, $selectField, $config);
}
}
else
{
$positions[] = $this->preparePosition($values, 0, $key, $selectField, $config);
if($qty > 1)
{
$positions[] = $this->preparePosition($values, 1, $key, $selectField, $config);
if($qty > 2)
{
$positions[] = $this->preparePosition($values, 2, $key, $selectField, $config);
}
}
}
break;
}
}
if($this->nomineeID <= 0 || empty($positions))
{
$qty = min($qty, 3);
for($i = 0; $i < $qty; $i++)
{
$positions[] = $this->preparePosition($values, $i, $key, $selectField, $config);
}
}
$items[] = array(
'name' => $name,
'title' => $title,
'nomineeId' => $this->nomineeID,
'positions' => $positions
);
}
return array('items' => $items);
}