1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Storgman - Student Organizations Management |
5
|
|
|
* Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This file is part of Storgman. |
8
|
|
|
* |
9
|
|
|
* Storgman is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* Storgman is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with Storgman. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
* @package Storgman |
23
|
|
|
* @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
24
|
|
|
* @license https://github.com/angelov/storgman/blob/master/LICENSE |
25
|
|
|
* @author Dejan Angelov <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace Angelov\Storgman\Members\Repositories; |
29
|
|
|
|
30
|
|
|
use Angelov\Storgman\Core\Repositories\AbstractEloquentRepository; |
31
|
|
|
use Angelov\Storgman\Core\DateTime; |
32
|
|
|
use Angelov\Storgman\Members\Member; |
33
|
|
|
use Angelov\Storgman\Membership\Reports\MembershipStatusReport; |
34
|
|
|
use Angelov\Storgman\Members\Reports\NewMembersPerMonthReport; |
35
|
|
|
use DB; |
36
|
|
|
|
37
|
|
|
class EloquentMembersRepository extends AbstractEloquentRepository implements MembersRepositoryInterface |
38
|
|
|
{ |
39
|
|
|
public function __construct(Member $entity) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($entity); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function store(Member $member) |
45
|
|
|
{ |
46
|
|
|
$member->save(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function countByMembershipStatus() |
50
|
|
|
{ |
51
|
|
|
$currentDate = DateTime::nowAsDateString(); |
52
|
|
|
|
53
|
|
|
// The query works with both MySQL and PostgreSQL |
54
|
|
|
$result = (array)DB::select( |
55
|
|
|
' |
56
|
|
|
SELECT * |
57
|
|
|
FROM |
58
|
|
|
(SELECT count(id) AS total |
59
|
|
|
FROM members) tbl1, |
60
|
|
|
(SELECT count(id) AS active |
61
|
|
|
FROM members |
62
|
|
|
WHERE id IN |
63
|
|
|
(SELECT DISTINCT member_id |
64
|
|
|
FROM fees |
65
|
|
|
WHERE cast(to_date AS DATE) > ?)) tbl2 |
66
|
|
|
', [$currentDate] |
67
|
|
|
)[0]; |
68
|
|
|
|
69
|
|
|
$report = new MembershipStatusReport($result['total'], $result['active']); |
70
|
|
|
|
71
|
|
|
return $report; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getByBirthdayDate(DateTime $date) |
75
|
|
|
{ |
76
|
|
|
$members = Member::whereRaw( |
77
|
|
|
'EXTRACT(DAY from birthday) = ? and EXTRACT(MONTH from birthday) = ?', |
78
|
|
|
[$date->format('d'), $date->format('m')] |
79
|
|
|
)->get()->all(); |
80
|
|
|
|
81
|
|
|
return $members; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function countNewMembersPerMonth(DateTime $from, DateTime $to) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$report = new NewMembersPerMonthReport($from, $to); |
87
|
|
|
|
88
|
|
|
$from = $from->toDateString(); |
89
|
|
|
$to = $to->toDateString(); |
90
|
|
|
|
91
|
|
|
// The query works with both MySQL and PostgreSQL |
92
|
|
|
$res = DB::select( |
93
|
|
|
' |
94
|
|
|
SELECT concat(YEAR, \'-\', lpad(cast(MONTH AS CHAR(2)), 2, \'0\')) AS month, |
95
|
|
|
count(id) AS count |
96
|
|
|
FROM |
97
|
|
|
(SELECT id, |
98
|
|
|
email, |
99
|
|
|
extract(MONTH |
100
|
|
|
FROM joined) AS MONTH, |
101
|
|
|
extract(YEAR |
102
|
|
|
FROM joined) AS YEAR |
103
|
|
|
FROM |
104
|
|
|
(SELECT id, |
105
|
|
|
email, |
106
|
|
|
min(from_date) AS joined |
107
|
|
|
FROM |
108
|
|
|
(SELECT members.id, |
109
|
|
|
members.email, |
110
|
|
|
fees.from_date, |
111
|
|
|
fees.to_date |
112
|
|
|
FROM members, |
113
|
|
|
fees |
114
|
|
|
WHERE members.id = member_id |
115
|
|
|
AND fees.from_date BETWEEN ? AND ?) tbl |
116
|
|
|
GROUP BY id, |
117
|
|
|
email) AS tbl2) tbl3 |
118
|
|
|
GROUP BY concat(MONTH, YEAR), |
119
|
|
|
MONTH, |
120
|
|
|
YEAR; |
121
|
|
|
', |
122
|
|
|
array($from, $to) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
foreach ($res as &$current) { |
126
|
|
|
$current = (array)$current; |
127
|
|
|
$report->addMonth($current["month"], (int)$current["count"]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $report; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getBoardMembers() |
134
|
|
|
{ |
135
|
|
|
return $this->entity->where('board_member', true)->get()->all(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getUnapprovedMembers() |
139
|
|
|
{ |
140
|
|
|
return $this->entity->where('approved', false)->get()->all(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.