|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Article\Mapper; |
|
6
|
|
|
|
|
7
|
|
|
use Article\Entity\ArticleType; |
|
8
|
|
|
use Zend\Db\Adapter\Adapter; |
|
9
|
|
|
use Zend\Db\Adapter\AdapterAwareInterface; |
|
10
|
|
|
use Zend\Db\Sql\Expression; |
|
11
|
|
|
use Zend\Db\TableGateway\AbstractTableGateway; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ArticleEventsMapper. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ArticleEventsMapper extends AbstractTableGateway implements |
|
17
|
|
|
AdapterAwareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $table = 'article_events'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Db adapter setter method,. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Adapter $adapter db adapter |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function setDbAdapter(Adapter $adapter) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->adapter = $adapter; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getPaginationSelect($status = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$select = $this->getSql()->select() |
|
39
|
|
|
->columns(['title', 'body', 'longitude', 'latitude']) |
|
40
|
|
|
->join('articles', |
|
41
|
|
|
'article_events.article_uuid = articles.article_uuid') |
|
42
|
|
|
->join( |
|
43
|
|
|
'admin_users', |
|
44
|
|
|
'admin_users.admin_user_uuid = articles.admin_user_uuid', |
|
45
|
|
|
['admin_user_id', 'first_name', 'last_name'] |
|
46
|
|
|
)->where(['articles.type' => ArticleType::EVENT]) |
|
47
|
|
|
->order(['created_at' => 'desc']); |
|
48
|
|
|
|
|
49
|
|
|
if ($status) { |
|
50
|
|
|
$select->where(['articles.status' => (int) $status]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $select; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function get($id) |
|
57
|
|
|
{ |
|
58
|
|
|
$select = $this->getSql()->select() |
|
59
|
|
|
->join('articles', |
|
60
|
|
|
'article_events.article_uuid = articles.article_uuid') |
|
61
|
|
|
->join( |
|
62
|
|
|
'category', 'category.category_uuid = articles.category_uuid', |
|
63
|
|
|
[ |
|
64
|
|
|
'category_slug' => 'slug', |
|
65
|
|
|
'category_name' => 'name', |
|
66
|
|
|
'category_id', |
|
67
|
|
|
], 'left' |
|
68
|
|
|
) |
|
69
|
|
|
->join('admin_users', |
|
70
|
|
|
'admin_users.admin_user_uuid = articles.admin_user_uuid', |
|
71
|
|
|
['admin_user_id'], 'left') |
|
72
|
|
|
->where(['articles.article_id' => $id]); |
|
73
|
|
|
|
|
74
|
|
|
return $this->selectWith($select)->current(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getBySlug($slug) |
|
78
|
|
|
{ |
|
79
|
|
|
$select = $this->getSql()->select() |
|
80
|
|
|
->join('articles', |
|
81
|
|
|
'article_events.article_uuid = articles.article_uuid') |
|
82
|
|
|
->join( |
|
83
|
|
|
'category', 'category.category_uuid = articles.category_uuid', |
|
84
|
|
|
[ |
|
85
|
|
|
'category_slug' => 'slug', |
|
86
|
|
|
'category_name' => 'name', |
|
87
|
|
|
'category_id', |
|
88
|
|
|
], 'left' |
|
89
|
|
|
) |
|
90
|
|
|
->where(['articles.slug' => $slug]); |
|
91
|
|
|
|
|
92
|
|
|
return $this->selectWith($select)->current(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function getLatest($limit = 50) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$select = $this->getSql()->select() |
|
98
|
|
|
->join('articles', |
|
99
|
|
|
'article_events.article_uuid = articles.article_uuid', |
|
100
|
|
|
['article_id', 'slug', 'published_at']) |
|
101
|
|
|
->join('category', |
|
102
|
|
|
'category.category_uuid = articles.category_uuid', |
|
103
|
|
|
['category_slug' => 'slug']) |
|
104
|
|
|
->where(['articles.status' => 1]) |
|
105
|
|
|
->order(['published_at' => 'desc']) |
|
106
|
|
|
->limit($limit); |
|
107
|
|
|
|
|
108
|
|
|
return $this->selectWith($select); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getFuture() |
|
112
|
|
|
{ |
|
113
|
|
|
$select = $this->getSql()->select() |
|
114
|
|
|
->where(['articles.status' => 1]) |
|
115
|
|
|
->join('articles', |
|
116
|
|
|
'articles.article_uuid = article_events.article_uuid', |
|
117
|
|
|
['article_id', 'slug', 'published_at']) |
|
118
|
|
|
->join('category', |
|
119
|
|
|
'category.category_uuid = articles.category_uuid', |
|
120
|
|
|
['category_slug' => 'slug']) |
|
121
|
|
|
->order(new Expression('rand()')); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
$select->where->greaterThanOrEqualTo('end_at', date('Y-m-d H:i:s')); |
|
124
|
|
|
|
|
125
|
|
|
return $this->selectWith($select); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
View Code Duplication |
public function getPastSelect() |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
$select = $this->getSql()->select() |
|
131
|
|
|
->where(['articles.status' => 1]) |
|
132
|
|
|
->join('articles', |
|
133
|
|
|
'articles.article_uuid = article_events.article_uuid', |
|
134
|
|
|
['article_id', 'slug', 'published_at']) |
|
135
|
|
|
->join('category', |
|
136
|
|
|
'category.category_uuid = articles.category_uuid', |
|
137
|
|
|
['category_slug' => 'slug']) |
|
138
|
|
|
->order(['start_at' => 'desc']); |
|
139
|
|
|
$select->where->lessThan('end_at', date('Y-m-d H:i:s')); |
|
140
|
|
|
|
|
141
|
|
|
return $select; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
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.