WYSIJA_control_back_statistics

The MailPoet Newsletters WYSIJA control back statistics class.

Defined (2)

The class is defined in the following location(s).

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