1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Event\Statistics; |
5
|
|
|
use Cake\Cache\Cache; |
6
|
|
|
use Cake\Core\Configure; |
7
|
|
|
use Cake\Event\Event; |
8
|
|
|
use Cake\Network\Exception\NotFoundException; |
9
|
|
|
|
10
|
|
|
class PagesController extends AppController |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Initialization hook method. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function initialize() |
19
|
|
|
{ |
20
|
|
|
parent::initialize(); |
21
|
|
|
|
22
|
|
|
$this->loadComponent('RequestHandler'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Beforefilter. |
27
|
|
|
* |
28
|
|
|
* @param Event $event The beforeFilter event that was fired. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function beforeFilter(Event $event) |
33
|
|
|
{ |
34
|
|
|
parent::beforeFilter($event); |
35
|
|
|
|
36
|
|
|
$this->Auth->allow(['home', 'maintenance', 'acceptCookie', 'lang', 'terms']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Home page. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function home() |
45
|
|
|
{ |
46
|
|
|
$this->loadModel('BlogArticles'); |
47
|
|
|
$this->loadModel('BlogArticlesComments'); |
48
|
|
|
|
49
|
|
|
$articles = $this->BlogArticles |
|
|
|
|
50
|
|
|
->find() |
51
|
|
|
->contain([ |
52
|
|
|
'BlogCategories', |
53
|
|
|
'Users' |
54
|
|
|
]) |
55
|
|
|
->order([ |
56
|
|
|
'BlogArticles.created' => 'desc' |
57
|
|
|
]) |
58
|
|
|
->limit(Configure::read('Home.articles')) |
59
|
|
|
->where([ |
60
|
|
|
'BlogArticles.is_display' => 1 |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$comments = $this->BlogArticlesComments |
|
|
|
|
64
|
|
|
->find() |
65
|
|
|
->contain([ |
66
|
|
|
'BlogArticles' => function ($q) { |
67
|
|
|
return $q |
68
|
|
|
->select([ |
69
|
|
|
'title' |
70
|
|
|
]); |
71
|
|
|
}, |
72
|
|
|
'Users' => function ($q) { |
73
|
|
|
return $q->find('medium'); |
74
|
|
|
} |
75
|
|
|
]) |
76
|
|
|
->order([ |
77
|
|
|
'BlogArticlesComments.created' => 'desc' |
78
|
|
|
]) |
79
|
|
|
->limit(Configure::read('Home.comments')) |
80
|
|
|
->where([ |
81
|
|
|
'BlogArticles.is_display' => 1 |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$statistics = $this->_buildStats([ |
85
|
|
|
'Users' => 'Model.Users.register', |
86
|
|
|
'Articles' => 'Model.BlogArticles.new', |
87
|
|
|
'ArticlesLikes' => 'Model.BlogArticlesLikes.new', |
88
|
|
|
'ArticlesComments' => 'Model.BlogArticlesComments.new' |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$this->set(compact('articles', 'comments', 'statistics')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Build the statistics for the home page. |
96
|
|
|
* |
97
|
|
|
* @param array $array The array of statistics to build. |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
protected function _buildStats(array $array) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$statistics = []; |
104
|
|
|
|
105
|
|
|
foreach ($array as $type => $event) { |
106
|
|
|
$statistics[$type] = Cache::remember($type, function () { |
107
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
108
|
|
|
$event = new Event($event); |
|
|
|
|
109
|
|
|
$this->eventManager()->dispatch($event); |
110
|
|
|
|
111
|
|
|
return $event->result; |
112
|
|
|
}, 'statistics'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $statistics; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* The user accept the use of cookies. |
120
|
|
|
* |
121
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function acceptCookie() |
126
|
|
|
{ |
127
|
|
|
if (!$this->request->is('ajax')) { |
128
|
|
|
throw new NotFoundException(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->Cookie->configKey('allowCookies', [ |
132
|
|
|
'expires' => '+1 year', |
133
|
|
|
'httpOnly' => true |
134
|
|
|
]); |
135
|
|
|
$this->Cookie->write('allowCookies', 'true'); |
136
|
|
|
|
137
|
|
|
$json = []; |
138
|
|
|
$json['message'] = __('Thanks for accepting to use the cookies !'); |
139
|
|
|
$this->set(compact('json')); |
140
|
|
|
|
141
|
|
|
$this->set('_serialize', 'json'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Redirect to the referer. |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
public function lang() |
150
|
|
|
{ |
151
|
|
|
$this->redirect($this->referer()); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Display the maintenance page or a 404 if the site isn't in maintenance. |
156
|
|
|
* |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function maintenance() |
160
|
|
|
{ |
161
|
|
|
if (Configure::read('Site.maintenance') === false) { |
162
|
|
|
throw new NotFoundException(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Display the Terms page. |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function terms() |
172
|
|
|
{ |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.