-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgt.monthly_report.php
161 lines (137 loc) · 4.13 KB
/
wgt.monthly_report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Membrr Monthly Report Widget
*
* Display monthly report snapshot in widget format.
*
* @package ExpressionEngine
* @subpackage Addons
* @category Widget
* @author Chris Monnat
* @link http://chrismonnat.com
*/
class Wgt_monthly_report
{
public $widget_name = 'Membrr Monthly Report';
public $widget_description = 'Monthly report snapshot as on module CP homepage.';
public $EE;
public $title;
public $wclass;
public $settings;
/**
* Constructor
*/
public function __construct()
{
$this->EE = get_instance();
$this->title = 'Membrr Monthly Report';
$this->wclass = 'contentMenu';
}
// ----------------------------------------------------------------
/**
* Index Function
*
* @param object
* @return string
*/
public function index()
{
// get monthly reports
$result = $this->EE->db->query('SELECT COUNT(recurring_id) AS `new_subscriptions`, MONTH(date_created) AS `date_month`, YEAR(date_created) AS `date_year`
FROM `exp_membrr_subscriptions`
WHERE YEAR(date_created) > 0
GROUP BY YEAR(date_created), MONTH(date_created)
ORDER BY `date_created` DESC');
$months = array();
if($result->num_rows() > 0)
{
foreach($result->result_array() as $month)
{
$month['date_month'] = str_pad($month['date_month'], 2, '0', STR_PAD_LEFT);
$months[$month['date_month'] . $month['date_year']] = array(
'code' => $month['date_month'] . $month['date_year'],
'year' => $month['date_year'],
'month' => date('F', strtotime('2011-' . $month['date_month'] . '-01 12:12:12')),
'new_subscribers' => $month['new_subscriptions'],
'expirations' => '0',
'difference' => '0',
'url' => $this->cp_url('index', array('month' => $month['date_month'] . $month['date_year']))
);
}
}
$result = $this->EE->db->query('SELECT COUNT(recurring_id) AS `expirations`, MONTH(end_date) AS `date_month`, YEAR(end_date) AS `date_year`
FROM `exp_membrr_subscriptions`
WHERE YEAR(end_date) > 0
GROUP BY YEAR(end_date), MONTH(end_date)
ORDER BY `end_date` DESC');
if($result->num_rows() > 0)
{
foreach($result->result_array() as $month)
{
$month['date_month'] = str_pad($month['date_month'], 2, '0', STR_PAD_LEFT);
if(isset($months[$month['date_month'] . $month['date_year']]))
{
// we have a report for this, so we'll add this to it
$months[$month['date_month'] . $month['date_year']]['expirations'] = $month['expirations'];
}
}
}
// calculate difference
reset($months);
foreach($months as $key => $month)
{
$difference = $month['new_subscribers'] - $month['expirations'];
if($difference > 0)
{
$difference = '+' . $difference;
}
$months[$key]['difference'] = $difference;
}
// get specific monthly report
reset($months);
$display = '';
if(count($months) > 0)
{
foreach($months as $month)
{
$display .= '
<tr class="'.alternator('odd','even').'">
<td><a href="'.$month['url'].'">'.$month['month'].', '.$month['year'].'</a></td>
<td>'.$month['new_subscribers'].'</td>
<td>'.$month['expirations'].'</td>
<td>'.$month['difference'].'</td>
</tr>';
}
}
else
{
$display = '<tr><td colspan="3"><center>No data to report on.</center></td></tr>';
}
return '
<table>
<thead><tr><th>Date</th><th>Subs</th><th>Exps</th><th>Diff</th></tr></thead>
<tbody>'.$display.'</tbody>
</table>
';
}
/**
* CP URL
*
* Aids in generation of CP URLs.
*
* @param string $action
* @param array $variables
* @return string
*/
private function cp_url ($action = 'index', $variables = array())
{
$url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp' . AMP . 'module=membrr'.AMP.'method=' . $action;
foreach($variables as $variable => $value)
{
$url .= AMP . $variable . '=' . $value;
}
return $url;
}
}
/* End of file wgt.monthly_report.php */
/* Location: /system/expressionengine/third_party/membrr/widgets/wgt.monthly_report.php */