|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Page\Service; |
|
6
|
|
|
|
|
7
|
|
|
use MysqlUuid\Formats\Binary; |
|
8
|
|
|
use MysqlUuid\Uuid as MysqlUuid; |
|
9
|
|
|
use Page\Filter\PageFilter; |
|
10
|
|
|
use Page\Mapper\PageMapper; |
|
11
|
|
|
use Ramsey\Uuid\Uuid; |
|
12
|
|
|
use Std\FilterException; |
|
13
|
|
|
use UploadHelper\Upload; |
|
14
|
|
|
use Zend\Paginator\Paginator; |
|
15
|
|
|
|
|
16
|
|
|
class PageService |
|
17
|
|
|
{ |
|
18
|
|
|
private $pageFilter; |
|
19
|
|
|
private $pageMapper; |
|
20
|
|
|
private $pagination; |
|
21
|
|
|
private $upload; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(PageFilter $pageFilter, PageMapper $pageMapper, Paginator $pagination, Upload $upload) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->pageFilter = $pageFilter; |
|
26
|
|
|
$this->pageMapper = $pageMapper; |
|
27
|
|
|
$this->pagination = $pagination; |
|
28
|
|
|
$this->upload = $upload; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getPagination($page = 1, $limit = 10) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->pagination->setCurrentPageNumber($page); |
|
34
|
|
|
$this->pagination->setItemCountPerPage($limit); |
|
35
|
|
|
|
|
36
|
|
|
return $this->pagination; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $pageId |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Page\Entity\Page|null |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getPage($pageId) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->pageMapper->select(['page_id' => $pageId])->current(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $urlSlug |
|
51
|
|
|
* |
|
52
|
|
|
* @return \Page\Entity\Page|null |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getPageBySlug($urlSlug) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->pageMapper->getActivePage($urlSlug)->current(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return \Page\Entity\Page|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getHomepage() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->pageMapper->select(['is_homepage' => true])->current(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $data |
|
69
|
|
|
* |
|
70
|
|
|
* @throws FilterException |
|
71
|
|
|
* |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
|
|
public function createPage($data) |
|
75
|
|
|
{ |
|
76
|
|
|
$filter = $this->pageFilter->getInputFilter()->setData($data); |
|
77
|
|
|
|
|
78
|
|
|
if (!$filter->isValid()) { |
|
79
|
|
|
throw new FilterException($filter->getMessages()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$data = $filter->getValues() |
|
83
|
|
|
+ ['main_img' => $this->upload->uploadImage($data, 'main_img')]; |
|
84
|
|
|
$data['page_id'] = Uuid::uuid1()->toString(); |
|
85
|
|
|
$data['page_uuid'] |
|
86
|
|
|
= (new MysqlUuid($data['page_id']))->toFormat(new Binary()); |
|
87
|
|
|
|
|
88
|
|
|
if ($data['is_homepage']) { |
|
89
|
|
|
$this->pageMapper->update(['is_homepage' => false]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->pageMapper->insert($data); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function updatePage($data, $pageId) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!($page = $this->getPage($pageId))) { |
|
98
|
|
|
throw new \Exception('Page object not found. Page ID:'.$pageId); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$filter = $this->pageFilter->getInputFilter()->setData($data); |
|
102
|
|
|
|
|
103
|
|
|
if (!$filter->isValid()) { |
|
104
|
|
|
throw new FilterException($filter->getMessages()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$data = $filter->getValues() |
|
108
|
|
|
+ ['main_img' => $this->upload->uploadImage($data, 'main_img')]; |
|
109
|
|
|
|
|
110
|
|
|
// We don't want to force user to re-upload image on edit |
|
111
|
|
|
if (!$data['main_img']) { |
|
112
|
|
|
unset($data['main_img']); |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->upload->deleteFile($page->getMainImg()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($data['is_homepage']) { |
|
118
|
|
|
$this->pageMapper->update(['is_homepage' => false]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->pageMapper->update($data, ['page_id' => $pageId]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function delete($pageId) |
|
125
|
|
|
{ |
|
126
|
|
|
if (!($page = $this->getPage($pageId))) { |
|
127
|
|
|
throw new \Exception('Page not found'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->upload->deleteFile($page->getMainImg()); |
|
131
|
|
|
|
|
132
|
|
|
return (bool) $this->pageMapper->delete(['page_id' => $pageId]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getForSelect() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->pageMapper->select(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|