Skip to content

Instantly share code, notes, and snippets.

@AleksKu
Last active June 30, 2016 09:16
Show Gist options
  • Save AleksKu/d3ad6a9c4c5c2902bed9aeb7abc51908 to your computer and use it in GitHub Desktop.
Save AleksKu/d3ad6a9c4c5c2902bed9aeb7abc51908 to your computer and use it in GitHub Desktop.
processStatistics
/**
* @param Request $request
* @param string $groupBy
* @param $groupByList
* @param Form $filterForm
* @return Response
*/
private function processStatistics(Request $request, $groupBy, $groupByList, Form $filterForm)
{
$filterForm->handleRequest($request);
$filterData = $filterForm->getData();
$page = $request->query->getInt('page', 1);
$offset = ($page - 1) * $this->statisticRowsPage;
$orderBy = $request->query->get('orderBy');
$direction = $request->query->get('direction');
$orderData = compact('orderBy', 'direction');
list($statistics, $totalCount) = $this->getDoctrine()->getRepository(Statistics::class)
->createStatisticsQueryBuilder($groupBy, $this->getUser(), $filterData, $orderData, $offset, $this->statisticRowsPage);
$pagination = $this->get('knp_paginator')->paginate([]);
$pagination->setItemNumberPerPage($this->statisticRowsPage);
$pagination->setCurrentPageNumber($page);
$pagination->setTotalItemCount($totalCount);
$pagination->setItems($statistics);
$showIds = $this->isGranted('ROLE_ADMIN');
if ('offerTargetId' == $groupBy) {
$offerIds = [];
foreach ($pagination as $statisticRow) {
$offerIds[] = $statisticRow['offer_id'];
}
$offerTargets = $this->getDoctrine()->getRepository(OfferTarget::class)
->findAllByOfferId($offerIds);
$dictionary = [];
foreach ($offerTargets as $offerTarget) {
$dictionary[$offerTarget->getId()] = $offerTarget->getTitle();
}
$statisticsOfferTarget = $this->getDoctrine()->getRepository(Statistics::class)->getGroupedByOfferTargetStatistics($this->getUser(), $filterData);
} elseif ('location' == $groupBy) {
$dictionary = ['' => ''];
foreach ($this->getDoctrine()->getRepository(Country::class)->findAll() as $location) {
$dictionary[$location->getCode()] = $location->getName();
}
} elseif ('webmaster' == $groupBy || 'advertiser' == $groupBy) {
$dictionary = [];
foreach ($this->getDoctrine()->getRepository(AbstractUser::class)->findAll() as $entity) {
$groupInfo = ($showIds ? $entity->getId() . ": " : '') . $entity->getUsername();
if ($showIds) {
$uri = $this->generateUrl('sonata_user_edit', ['id' => $entity->getId()]);
}
if (isset($uri)) {
$groupInfo = [
'title' => $groupInfo,
'uri' => $uri,
];
}
$dictionary[$entity->getId()] = $groupInfo;
}
} elseif ('site' == $groupBy) {
$dictionary = [];
foreach ($this->getDoctrine()->getRepository(Site::class)->findAll() as $entity) {
$groupInfo = ($showIds ? $entity->getId() . ": " : '') . $entity->getTitle();
$uri = null;
if ($entity->getWebmaster() == $this->getUser()) {
$uri = $this->generateUrl('site_edit', ['site' => $entity->getId()]);
} elseif ($showIds) {
$uri = $this->generateUrl('sonata_site_edit', ['id' => $entity->getId()]);
}
if (isset($uri)) {
$groupInfo = [
'title' => $groupInfo,
'uri' => $uri,
];
}
$dictionary[$entity->getId()] = $groupInfo;
}
} elseif ('stream' == $groupBy) {
$dictionary = [];
foreach ($this->getDoctrine()->getRepository(Stream::class)->findAll() as $entity) {
$groupInfo = ($showIds ? $entity->getId() . ": " : '') . $entity->getTitle();
$uri = null;
if ($this->isGranted('edit', $entity)) {
$uri = $this->generateUrl('stream_edit', ['stream' => $entity->getId()]);
} elseif ($showIds) {
$uri = $this->generateUrl('admin_app_stream_edit', ['id' => $entity->getId()]);
}
if (isset($uri)) {
$groupInfo = [
'title' => $groupInfo,
'uri' => $uri,
];
}
$dictionary[$entity->getId()] = $groupInfo;
}
} elseif ('hour' == $groupBy) {
for ($h = 0; $h <= 23; $h++) {
$dictionary[$h] = sprintf('%02d:00', $h);
}
}
$this->initializeStatisticsPathGenerator($request, $filterForm);
$groupTitle = !empty($filterData['sub'])
? $filterData['sub']
: $groupBy;
if ('sub' == $groupBy && empty($filterData['sub'])) {
$statisticsFirstHeader = [
['rowspan' => 2, 'title' => 'sub1', 'orderBy' => 'sub1'],
['rowspan' => 2, 'title' => 'sub2', 'orderBy' => 'sub2'],
['rowspan' => 2, 'title' => 'sub3', 'orderBy' => 'sub3'],
['rowspan' => 2, 'title' => 'sub4', 'orderBy' => 'sub4'],
['rowspan' => 2, 'title' => 'sub5', 'orderBy' => 'sub5'],
];
} else {
$statisticsFirstHeader = [
['rowspan' => 2, 'title' => "statistics.grouping.field.$groupTitle", 'orderBy' => $groupTitle],
];
}
$statisticsFirstHeader = array_merge($statisticsFirstHeader, [
['colspan' => 2, 'class' => 'colspaned', 'title' => 'table_header.traffic'],
['colspan' => 3, 'class' => 'colspaned', 'title' => 'table_header.pendings'],
['colspan' => 3, 'class' => 'colspaned', 'title' => 'table_header.factuals'],
['colspan' => 3, 'class' => 'colspaned', 'title' => 'table_header.finances'],
]);
if ('advertiser' === $groupBy) {
$statisticsSecondHeader = [
['title' => 'table_header.unique', 'orderBy' => 'redirectCount'],
['title' => 'table_header.trafback', 'orderBy' => 'traffbackCount'],
['title' => 'table_header.leads', 'orderBy' => 'targetPendingCount'],
['title' => 'table_header.ratio'],
['title' => 'Ecpc'],
['title' => 'table_header.leads', 'orderBy' => 'targetApprovedCount'],
['title' => 'table_header.ratio'],
['title' => 'Ecpc'],
['title' => 'table_header.costs'],
['title' => 'table_header.revenue'],
['title' => 'table_header.profit'],
];
} else {
$statisticsSecondHeader = [
['title' => 'table_header.unique', 'orderBy' => 'redirectCount'],
['title' => 'table_header.trafback', 'orderBy' => 'traffbackCount'],
['title' => 'table_header.leads', 'orderBy' => 'targetPendingCount'],
['title' => 'table_header.ratio'],
['title' => 'Ecpc'],
['title' => 'table_header.leads', 'orderBy' => 'targetApprovedCount'],
['title' => 'table_header.ratio'],
['title' => 'Ecpc'],
['title' => 'table_header.pending', 'orderBy' => 'sumPending'],
['title' => 'table_header.approved', 'orderBy' => 'sumApproved'],
['title' => 'table_header.cancelled', 'orderBy' => 'sumRejected'],
];
}
$statisticsHeader = [
$statisticsFirstHeader,
$statisticsSecondHeader,
];
return $this->render(':statistics:index.html.twig', [
'stat_group_by_list' => $groupByList,
'group_title' => $groupTitle,
'groupBy' => $groupBy,
'orderBy' => $orderBy,
'direction' => $direction,
'sub' => !empty($filterData['sub']) ? $filterData['sub'] : '',
'currency' => $filterData['currency'],
'statistics_header' => $statisticsHeader,
'statistics' => $pagination,
'statisticsOfferTarget' => isset($statisticsOfferTarget) ? $statisticsOfferTarget : [],
'dictionary' => isset($dictionary) ? $dictionary : [],
'formFilter' => $filterForm->createView(),
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment