PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wysija-newsletters/controllers/back/statistics.php

https://gitlab.com/Gashler/sg
PHP | 203 lines | 143 code | 22 blank | 38 comment | 11 complexity | bd5f70d96d05e64134762cd7d50ee3e5 MD5 | raw file
  1. <?php
  2. defined('WYSIJA') or die('Restricted access');
  3. require_once(WYSIJA_CORE.'module'.DS.'statistics.php'); // @todo
  4. class WYSIJA_control_back_statistics extends WYSIJA_control_back {
  5. /**
  6. * Main model of this controller
  7. * @var string
  8. */
  9. public $model = 'statistics';
  10. /**
  11. * Main view of this controller
  12. * @var string
  13. */
  14. public $view = 'statistics';
  15. /**
  16. * Base URL of all requests
  17. * @var string
  18. */
  19. public $base_url = 'admin.php';
  20. /**
  21. * Load blocks at a same time (FALSE) or one by one (TRUE)
  22. * @var TRUE
  23. */
  24. protected $lazy_load = true;
  25. /**
  26. * list of pre-defined dates
  27. * @var Array
  28. */
  29. protected $pre_defined_dates = array( );
  30. protected $date_format = 'Y/m/d';
  31. protected $js_date_format = 'yy/mm/dd';
  32. /**
  33. * Constructor
  34. */
  35. function WYSIJA_control_back_statistics() {
  36. }
  37. public function defaultDisplay() {
  38. if (!WYSIJA::current_user_can('wysija_stats_dashboard'))
  39. die('Action is forbidden.');
  40. $this->pre_defined_dates = $this->get_pre_defined_dates();
  41. // Define view
  42. $this->viewShow = $this->action = 'main';
  43. $this->js['jquery.core'] = 'jquery/ui/jquery.ui.core';
  44. $this->js['jquery.datepicker'] = 'jquery/ui/jquery.ui.datepicker';
  45. $this->js['wysijalazyload'] = 'wysija-lazyload';
  46. $this->js['admin-statistics-filter'] = 'admin-statistics-filter';
  47. wp_enqueue_style('jquery.core', WYSIJA_URL.'css/jquery/ui/themes/base/jquery.ui.core.min.css', array( ), WYSIJA::get_version());
  48. wp_enqueue_style('jquery.core', WYSIJA_URL.'css/jquery/ui/themes/base/jquery.ui.theme.min.css', array( ), WYSIJA::get_version());
  49. // date filter
  50. $default_duration = $this->get_default_duration();
  51. if (function_exists('date_diff')) {
  52. $this->data['date_interval'] = date_diff(date_create($default_duration->from), date_create($default_duration->to));
  53. }
  54. else {
  55. $duration = strtotime($default_duration->to) - strtotime($default_duration->from);
  56. $helper_toolbox = WYSIJA::get('toolbox', 'helper');
  57. $this->data['date_interval'] = (object)$helper_toolbox->convert_seconds_to_array($duration, false);
  58. }
  59. $this->data['custom_dates'] = $this->pre_defined_dates;
  60. $this->data['default_duration'] = $default_duration;
  61. $this->data['js_date_format'] = $this->js_date_format;
  62. // Process and push data into view
  63. $this->data['lazy_load'] = $this->lazy_load;
  64. $hook_name = 'hook_stats';
  65. $hook_params = array( );
  66. $hook_params['top'] = WYSIJA_module_statistics::DEFAULT_TOP_RECORDS;
  67. $hook_params['from'] = !empty($_REQUEST['filter']['from']) ? $_REQUEST['filter']['from'] : $default_duration->from;
  68. $hook_params['to'] = !empty($_REQUEST['filter']['to']) ? $_REQUEST['filter']['to'] : $default_duration->to;
  69. $hook_params['group_by'] = ($this->data['date_interval']->days == 0 || $this->data['date_interval']->days > WYSIJA_module_statistics::SWITCHING_DATE_TO_MONTH_THRESHOLD) ?
  70. WYSIJA_module_statistics::GROUP_BY_MONTH :
  71. WYSIJA_module_statistics::GROUP_BY_DATE; // $this->data['date_interval']->days == 0, means, no begin date, no end date
  72. // Hack!
  73. $_REQUEST['limit_pp'] = $hook_params['top']; // Pagination, mark current selected value
  74. // Modify TO date to make sure we always count 23:59:59 of that day
  75. $to = new DateTime($hook_params['to']);
  76. $to->modify('+1 day');
  77. $hook_params['to'] = $to->format($this->date_format);
  78. $modules = WYSIJA_module::get_modules_from_hook($hook_name);
  79. $this->data['modules'] = $modules;
  80. $this->data['lazy_load_modules'] = array( );
  81. $this->data['first_module'] = '';
  82. if (!$this->lazy_load) {
  83. $this->data['hooks'][$hook_name] = apply_filters('hook_stats', '', $hook_params);
  84. }
  85. else {
  86. if (!empty($modules)) {
  87. $first_module = array_shift($modules);
  88. // List of lazy loaded modules
  89. $this->data['lazy_load_modules'] = $modules;
  90. // Evenly we are lazy loading, we always load the first module by default
  91. $this->data['first_module'] = apply_filters('custom_module_hook', '', $first_module, $hook_name, $hook_params);
  92. }
  93. }
  94. }
  95. /**
  96. * get pre defined dates (duration)
  97. * @return type
  98. */
  99. protected function get_pre_defined_dates() {
  100. return array(
  101. array(
  102. 'value' => 7,
  103. 'label' => __('Last 7 days', WYSIJA),
  104. 'selected' => false,
  105. 'from' => date($this->date_format, strtotime('-7 days')),
  106. 'to' => date($this->date_format, strtotime('today'))
  107. ),
  108. array(
  109. 'value' => 'last_month',
  110. 'label' => __('Last month', WYSIJA),
  111. 'selected' => false,
  112. 'from' => date($this->date_format, mktime(0, 0, 0, date('m') - 1, 1, date('Y'))),
  113. 'to' => date($this->date_format, mktime(0, 0, 0, date('m'), 0, date('Y')))
  114. ),
  115. array(
  116. 'value' => 30,
  117. 'label' => __('Last 30 days', WYSIJA),
  118. 'selected' => false,
  119. 'from' => date($this->date_format, strtotime('-30 days')),
  120. 'to' => date($this->date_format, strtotime('today'))
  121. ),
  122. array(
  123. 'value' => 90,
  124. 'label' => __('Last 90 days', WYSIJA),
  125. 'selected' => true,
  126. 'from' => date($this->date_format, strtotime('-90 days')),
  127. 'to' => date($this->date_format, strtotime('today'))
  128. ),
  129. array(
  130. 'value' => 180,
  131. 'label' => __('Last 180 days', WYSIJA),
  132. 'selected' => false,
  133. 'from' => date($this->date_format, strtotime('-180 days')),
  134. 'to' => date($this->date_format, strtotime('today'))
  135. ),
  136. array(
  137. 'value' => 365,
  138. 'label' => __('Last 365 days', WYSIJA),
  139. 'selected' => false,
  140. 'from' => date($this->date_format, strtotime('-365 days')),
  141. 'to' => date($this->date_format, strtotime('today'))
  142. ),
  143. array(
  144. 'value' => 0,
  145. 'label' => __('Custom dates', WYSIJA),
  146. 'selected' => false,
  147. 'from' => '',
  148. 'to' => ''
  149. ),
  150. );
  151. }
  152. /**
  153. * Get default duration of stats
  154. * @return WJ_StatsSession
  155. */
  156. protected function get_default_duration() {
  157. $_duration = null;
  158. foreach ($this->pre_defined_dates as $duration) {
  159. if (isset($duration['selected']) && $duration['selected']) {
  160. $_duration = $duration;
  161. break;
  162. }
  163. }
  164. if (empty($_duration))
  165. $_duration = end($this->pre_defined_dates);
  166. $stats_session_manager = new WJ_StatsSessionManager();
  167. $stats_session = new WJ_StatsSession();
  168. $stats_session->last_days = $_duration['value'];
  169. $stats_session->from = $_duration['from'];
  170. $stats_session->to = $_duration['to'];
  171. $stats_session_manager->set_default_selection($stats_session);
  172. $stats_session_manager->set_pre_defined_dates($this->get_pre_defined_dates());
  173. return $stats_session_manager->get_last_selection();
  174. }
  175. function date_diff($time_start, $time_end) {
  176. $result = null;
  177. $duration = $time_end - $time_start;
  178. $result->days = floor($duration / (60 * 60 * 24));
  179. return $result;
  180. }
  181. }