Advertisement
Guest User

spaghetti

a guest
Sep 29th, 2016
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.13 KB | None | 0 0
  1. public function lsGraphsAction(Request $request)
  2.     {
  3.         $conn = $this->getDoctrine()->getManager()->getConnection();
  4.  
  5.         $options = [
  6.             'start' => new \DateTime('-3 days'),
  7.             'end'   => new \Datetime(),
  8.             'type' => 1
  9.         ];
  10.  
  11.         $form = $this->createFormBuilder($options)
  12.                 ->setMethod('GET')
  13.                 ->add('start', DateType::class)
  14.                 ->add('end', DateType::class)
  15.                 ->add('type', ChoiceType::class, [
  16.                     'choices' => [
  17.                         1 => 'Total Revenue',
  18.                         2 => '# Transactions',
  19.                         3 => 'Total Logins'
  20.                     ]
  21.                 ])
  22.                 ->add('browserType', ChoiceType::class, [
  23.                     'choices' => [
  24.                         0 => 'All',
  25.                         1 => 'Desktop',
  26.                         2 => 'Mobile'
  27.                     ]
  28.                 ])
  29.                 ->getForm();
  30.  
  31.         $form->handleRequest($request);
  32.  
  33.         if($form['type']->getData() == 3) {
  34.             $sql = "
  35.                 select
  36.                     ulh.ls as loginSource,
  37.                     DATE(ulh.login_date) as dateInterval,
  38.                     DATE_FORMAT(SEC_TO_TIME(TIME_TO_SEC(ulh.login_date) - TIME_TO_SEC(ulh.login_date) % (15 * 60)), '%H:%i') as timeInterval,
  39.                     count(ulh.id) as revenue
  40.                 from users_login_history ulh
  41.                 where
  42.                     ulh.login_date between :start and :end             
  43.             ";
  44.  
  45.             if ($form['browserType']->getData() != 0)
  46.                 $sql .= "AND ulh.cwt_type_id = :browserType ";
  47.  
  48.             $sql .= "group by loginSource, dateInterval, timeInterval";
  49.         } else {
  50.             $sql = "
  51.                 select
  52.                     ulh.ls as loginSource,
  53.                     DATE(t.date) as dateInterval,
  54.                     DATE_FORMAT(SEC_TO_TIME(TIME_TO_SEC(t.date) - TIME_TO_SEC(t.date) % (15 * 60)), '%H:%i') as timeInterval,
  55.                     " . ($form['type']->getData() == 1 ? 'sum(t.mpg_amount) as revenue' : 'count(t.id) as revenue') . "
  56.                 from transactions t
  57.                 left join users_login_history ulh on ulh.id = t.ulh_id
  58.                 where
  59.                     t.type = 1
  60.                     and t.status = 1
  61.                     and t.mpg_pid not in (800,801,802,803,804,805,806,807,808,809)
  62.                     and t.date between :start and :end             
  63.             ";
  64.  
  65.             if ($form['browserType']->getData() != 0)
  66.                 $sql .= "AND ulh.cwt_type_id = :browserType ";
  67.  
  68.             $sql .= "group by loginSource, dateInterval, timeInterval";
  69.         }
  70.         $results = $conn->fetchAll($sql, [
  71.             'start' => $form['start']->getData()->format('Y-m-d') . ' 00:00:00',
  72.             'end'   => $form['end']->getData()->format('Y-m-d') . ' 23:59:59',
  73.             'browserType'   => $form['browserType']->getData()
  74.         ]);
  75.  
  76.         $stats = [];
  77.         $loginSources = [];
  78.  
  79.         $dateRange = $this->calculateDateRangesForCharts2($form['start']->getData(), $form['end']->getData());
  80.         foreach($results as $r)
  81.         {
  82.             // alphanumeric keys only
  83.             $r['loginSource'] = preg_replace('/[^\da-z]/i', '', $r['loginSource']);
  84.             if(is_null($r['loginSource']) || $r['loginSource'] == '') $r['loginSource'] = 'none';
  85.  
  86.  
  87.             foreach($dateRange as $dt)
  88.             {
  89.                 if(!isset($stats[$r['loginSource']][$dt]))
  90.                 $stats[$r['loginSource']][$dt] = [];
  91.             }
  92.  
  93.             $stats[$r['loginSource']][$r['dateInterval']][$r['timeInterval']] = ['current' => $r['revenue'], 'total' => 0];
  94.             // populate array of all login sources for easier chart implementation
  95.             if(!in_array($r['loginSource'], $loginSources)) $loginSources[] = $r['loginSource'];
  96.             // if a login source exists but had not traffic today, it still exists..
  97.             if(!array_key_exists($r['loginSource'], $stats)) $stats[$r['loginSource']][$r['dateInterval']] = [];
  98.         }
  99.  
  100.  
  101.         foreach($stats as $ls => &$lsStat)
  102.         {
  103.             foreach($lsStat as $dt => $stat)
  104.             {
  105.                 foreach($this->getIntervalLabels2() as $timeInterval) {
  106.  
  107.                     if(!isset($lsStat[$dt][$timeInterval]) && time() >= strtotime($dt . ' ' . $timeInterval))
  108.                         $lsStat[$dt][$timeInterval]['current'] = 0;
  109.                 }
  110.                 ksort($lsStat[$dt]);
  111.             }
  112.         }
  113.  
  114.         foreach($stats as $k => &$lsStat)
  115.         {
  116.             foreach($lsStat as $dt => $stat)
  117.             {
  118.                 $runningTotal = 0;
  119.                 foreach($stat as $timeInterval => $s)
  120.                 {
  121.                     $runningTotal += floatval($s['current']);
  122.                     $lsStat[$dt][$timeInterval]['total'] = $runningTotal;
  123.                 }
  124.             }
  125.         }
  126.  
  127.         return $this->render('AppBundle:Reports:RevenueComparison\ls_graphs.html.twig', [
  128.             "stats"        => $stats,
  129.             'loginSources' => $loginSources,
  130.             'form'         => $form->createView(),
  131.             'fullDayRange' => range(strtotime("00:00:00"),strtotime("23:59:59"),15*60),
  132.             'dateRange'    => $dateRange
  133.         ]);
  134.  
  135.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement