Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function lsGraphsAction(Request $request)
- {
- $conn = $this->getDoctrine()->getManager()->getConnection();
- $options = [
- 'start' => new \DateTime('-3 days'),
- 'end' => new \Datetime(),
- 'type' => 1
- ];
- $form = $this->createFormBuilder($options)
- ->setMethod('GET')
- ->add('start', DateType::class)
- ->add('end', DateType::class)
- ->add('type', ChoiceType::class, [
- 'choices' => [
- 1 => 'Total Revenue',
- 2 => '# Transactions',
- 3 => 'Total Logins'
- ]
- ])
- ->add('browserType', ChoiceType::class, [
- 'choices' => [
- 0 => 'All',
- 1 => 'Desktop',
- 2 => 'Mobile'
- ]
- ])
- ->getForm();
- $form->handleRequest($request);
- if($form['type']->getData() == 3) {
- $sql = "
- select
- ulh.ls as loginSource,
- DATE(ulh.login_date) as dateInterval,
- DATE_FORMAT(SEC_TO_TIME(TIME_TO_SEC(ulh.login_date) - TIME_TO_SEC(ulh.login_date) % (15 * 60)), '%H:%i') as timeInterval,
- count(ulh.id) as revenue
- from users_login_history ulh
- where
- ulh.login_date between :start and :end
- ";
- if ($form['browserType']->getData() != 0)
- $sql .= "AND ulh.cwt_type_id = :browserType ";
- $sql .= "group by loginSource, dateInterval, timeInterval";
- } else {
- $sql = "
- select
- ulh.ls as loginSource,
- DATE(t.date) as dateInterval,
- DATE_FORMAT(SEC_TO_TIME(TIME_TO_SEC(t.date) - TIME_TO_SEC(t.date) % (15 * 60)), '%H:%i') as timeInterval,
- " . ($form['type']->getData() == 1 ? 'sum(t.mpg_amount) as revenue' : 'count(t.id) as revenue') . "
- from transactions t
- left join users_login_history ulh on ulh.id = t.ulh_id
- where
- t.type = 1
- and t.status = 1
- and t.mpg_pid not in (800,801,802,803,804,805,806,807,808,809)
- and t.date between :start and :end
- ";
- if ($form['browserType']->getData() != 0)
- $sql .= "AND ulh.cwt_type_id = :browserType ";
- $sql .= "group by loginSource, dateInterval, timeInterval";
- }
- $results = $conn->fetchAll($sql, [
- 'start' => $form['start']->getData()->format('Y-m-d') . ' 00:00:00',
- 'end' => $form['end']->getData()->format('Y-m-d') . ' 23:59:59',
- 'browserType' => $form['browserType']->getData()
- ]);
- $stats = [];
- $loginSources = [];
- $dateRange = $this->calculateDateRangesForCharts2($form['start']->getData(), $form['end']->getData());
- foreach($results as $r)
- {
- // alphanumeric keys only
- $r['loginSource'] = preg_replace('/[^\da-z]/i', '', $r['loginSource']);
- if(is_null($r['loginSource']) || $r['loginSource'] == '') $r['loginSource'] = 'none';
- foreach($dateRange as $dt)
- {
- if(!isset($stats[$r['loginSource']][$dt]))
- $stats[$r['loginSource']][$dt] = [];
- }
- $stats[$r['loginSource']][$r['dateInterval']][$r['timeInterval']] = ['current' => $r['revenue'], 'total' => 0];
- // populate array of all login sources for easier chart implementation
- if(!in_array($r['loginSource'], $loginSources)) $loginSources[] = $r['loginSource'];
- // if a login source exists but had not traffic today, it still exists..
- if(!array_key_exists($r['loginSource'], $stats)) $stats[$r['loginSource']][$r['dateInterval']] = [];
- }
- foreach($stats as $ls => &$lsStat)
- {
- foreach($lsStat as $dt => $stat)
- {
- foreach($this->getIntervalLabels2() as $timeInterval) {
- if(!isset($lsStat[$dt][$timeInterval]) && time() >= strtotime($dt . ' ' . $timeInterval))
- $lsStat[$dt][$timeInterval]['current'] = 0;
- }
- ksort($lsStat[$dt]);
- }
- }
- foreach($stats as $k => &$lsStat)
- {
- foreach($lsStat as $dt => $stat)
- {
- $runningTotal = 0;
- foreach($stat as $timeInterval => $s)
- {
- $runningTotal += floatval($s['current']);
- $lsStat[$dt][$timeInterval]['total'] = $runningTotal;
- }
- }
- }
- return $this->render('AppBundle:Reports:RevenueComparison\ls_graphs.html.twig', [
- "stats" => $stats,
- 'loginSources' => $loginSources,
- 'form' => $form->createView(),
- 'fullDayRange' => range(strtotime("00:00:00"),strtotime("23:59:59"),15*60),
- 'dateRange' => $dateRange
- ]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement