Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Controller.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\Live;
  10. use Piwik\API\Request;
  11. use Piwik\Common;
  12. use Piwik\Config;
  13. use Piwik\Piwik;
  14. use Piwik\Plugins\Live\Reports\GetLastVisitsDetails;
  15. use Piwik\Plugins\Goals\API as APIGoals;
  16. use Piwik\Url;
  17. use Piwik\View;
  18. /**
  19. */
  20. class Controller extends \Piwik\Plugin\Controller
  21. {
  22. const SIMPLE_VISIT_COUNT_WIDGET_LAST_MINUTES_CONFIG_KEY = 'live_widget_visitor_count_last_minutes';
  23. function index()
  24. {
  25. return $this->widget();
  26. }
  27. public function widget()
  28. {
  29. $view = new View('@Live/index');
  30. $view->idSite = $this->idSite;
  31. $view = $this->setCounters($view);
  32. $view->liveRefreshAfterMs = (int)Config::getInstance()->General['live_widget_refresh_after_seconds'] * 1000;
  33. $view->visitors = $this->getLastVisitsStart($fetchPlease = true);
  34. $view->liveTokenAuth = Piwik::getCurrentUserTokenAuth();
  35. return $this->render($view);
  36. }
  37. public function ajaxTotalVisitors()
  38. {
  39. $view = new View('@Live/ajaxTotalVisitors');
  40. $view = $this->setCounters($view);
  41. $view->idSite = $this->idSite;
  42. return $this->render($view);
  43. }
  44. private function render(View $view)
  45. {
  46. $rendered = $view->render();
  47. return $rendered;
  48. }
  49. public function indexVisitorLog()
  50. {
  51. $view = new View('@Live/indexVisitorLog.twig');
  52. $view->filterEcommerce = Common::getRequestVar('filterEcommerce', 0, 'int');
  53. $view->visitorLog = $this->renderReport(new GetLastVisitsDetails());
  54. return $view->render();
  55. }
  56. /**
  57. * Widget
  58. */
  59. public function getVisitorLog()
  60. {
  61. return $this->renderReport(new GetLastVisitsDetails());
  62. }
  63. public function getLastVisitsStart()
  64. {
  65. // hack, ensure we load today's visits by default
  66. $_GET['date'] = 'today';
  67. \Piwik\Period\Factory::checkPeriodIsEnabled('day');
  68. $_GET['period'] = 'day';
  69. $view = new View('@Live/getLastVisitsStart');
  70. $view->idSite = $this->idSite;
  71. $api = new Request("method=Live.getLastVisitsDetails&idSite={$this->idSite}&filter_limit=10&format=php&serialize=0&disable_generic_filters=1");
  72. $visitors = $api->process();
  73. $view->visitors = $visitors;
  74. return $this->render($view);
  75. }
  76. private function setCounters($view)
  77. {
  78. $segment = Request::getRawSegmentFromRequest();
  79. $last30min = API::getInstance()->getCounters($this->idSite, $lastMinutes = 30, $segment);
  80. $last30min = $last30min[0];
  81. $today = API::getInstance()->getCounters($this->idSite, $lastMinutes = 24 * 60, $segment);
  82. $today = $today[0];
  83. $view->visitorsCountHalfHour = $last30min['visits'];
  84. $view->visitorsCountToday = $today['visits'];
  85. $view->pisHalfhour = $last30min['actions'];
  86. $view->pisToday = $today['actions'];
  87. return $view;
  88. }
  89. /**
  90. * Echo's HTML for visitor profile popup.
  91. */
  92. public function getVisitorProfilePopup()
  93. {
  94. $idSite = Common::getRequestVar('idSite', null, 'int');
  95. $view = new View('@Live/getVisitorProfilePopup.twig');
  96. $view->idSite = $idSite;
  97. $view->goals = APIGoals::getInstance()->getGoals($idSite);
  98. $view->visitorData = Request::processRequest('Live.getVisitorProfile', array('checkForLatLong' => true));
  99. $view->exportLink = $this->getVisitorProfileExportLink();
  100. if (Common::getRequestVar('showMap', 1) == 1
  101. && !empty($view->visitorData['hasLatLong'])
  102. && \Piwik\Plugin\Manager::getInstance()->isPluginLoaded('UserCountryMap')
  103. ) {
  104. $view->userCountryMapUrl = $this->getUserCountryMapUrlForVisitorProfile();
  105. }
  106. $this->setWidgetizedVisitorProfileUrl($view);
  107. return $view->render();
  108. }
  109. public function getSingleVisitSummary()
  110. {
  111. $view = new View('@Live/getSingleVisitSummary.twig');
  112. $visits = Request::processRequest('Live.getLastVisitsDetails', array(
  113. 'segment' => 'visitId==' . Common::getRequestVar('visitId'),
  114. 'period' => false,
  115. 'date' => false
  116. ));
  117. $view->visitData = $visits->getFirstRow()->getColumns();
  118. $view->visitReferralSummary = API::getReferrerSummaryForVisit($visits->getFirstRow());
  119. $view->showLocation = true;
  120. $this->setWidgetizedVisitorProfileUrl($view);
  121. $view->exportLink = $this->getVisitorProfileExportLink();
  122. return $view->render();
  123. }
  124. public function getVisitList()
  125. {
  126. $startCounter = Common::getRequestVar('filter_offset', 0, 'int');
  127. $nextVisits = Request::processRequest('Live.getLastVisitsDetails', array(
  128. 'segment' => self::getSegmentWithVisitorId(),
  129. 'filter_limit' => API::VISITOR_PROFILE_MAX_VISITS_TO_SHOW,
  130. 'filter_offset' => $startCounter,
  131. 'period' => false,
  132. 'date' => false
  133. ));
  134. if (empty($nextVisits)) {
  135. return;
  136. }
  137. $view = new View('@Live/getVisitList.twig');
  138. $view->idSite = Common::getRequestVar('idSite', null, 'int');
  139. $view->startCounter = $startCounter + 1;
  140. $view->visits = $nextVisits;
  141. return $view->render();
  142. }
  143. private function getVisitorProfileExportLink()
  144. {
  145. return Url::getCurrentQueryStringWithParametersModified(array(
  146. 'module' => 'API',
  147. 'action' => 'index',
  148. 'method' => 'Live.getVisitorProfile',
  149. 'format' => 'XML',
  150. 'expanded' => 1
  151. ));
  152. }
  153. private function setWidgetizedVisitorProfileUrl($view)
  154. {
  155. if (\Piwik\Plugin\Manager::getInstance()->isPluginLoaded('Widgetize')) {
  156. $view->widgetizedLink = Url::getCurrentQueryStringWithParametersModified(array(
  157. 'module' => 'Widgetize',
  158. 'action' => 'iframe',
  159. 'moduleToWidgetize' => 'Live',
  160. 'actionToWidgetize' => 'getVisitorProfilePopup'
  161. ));
  162. }
  163. }
  164. private function getUserCountryMapUrlForVisitorProfile()
  165. {
  166. $params = array(
  167. 'module' => 'UserCountryMap',
  168. 'action' => 'realtimeMap',
  169. 'segment' => self::getSegmentWithVisitorId(),
  170. 'visitorId' => false,
  171. 'changeVisitAlpha' => 0,
  172. 'removeOldVisits' => 0,
  173. 'realtimeWindow' => 'false',
  174. 'showFooterMessage' => 0,
  175. 'showDateTime' => 0,
  176. 'doNotRefreshVisits' => 1
  177. );
  178. return Url::getCurrentQueryStringWithParametersModified($params);
  179. }
  180. private static function getSegmentWithVisitorId()
  181. {
  182. static $cached = null;
  183. if ($cached === null) {
  184. $segment = Request::getRawSegmentFromRequest();
  185. if (!empty($segment)) {
  186. $segment = urldecode($segment) . ';';
  187. }
  188. $idVisitor = Common::getRequestVar('visitorId', false);
  189. if ($idVisitor === false) {
  190. $idVisitor = Request::processRequest('Live.getMostRecentVisitorId');
  191. }
  192. $cached = urlencode($segment . 'visitorId==' . $idVisitor);
  193. }
  194. return $cached;
  195. }
  196. }