|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Controller\Admin; |
|
3
|
|
|
|
|
4
|
|
|
use App\Controller\AppController; |
|
5
|
|
|
use Cake\I18n\I18n; |
|
6
|
|
|
|
|
7
|
|
|
class ArticlesController extends AppController |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Helpers. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
public $helpers = ['I18n']; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Display all articles. |
|
18
|
|
|
* |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
|
|
public function index() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->loadModel('BlogArticles'); |
|
24
|
|
|
|
|
25
|
|
|
$this->paginate = [ |
|
26
|
|
|
'maxLimit' => 15 |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
$articles = $this->BlogArticles |
|
|
|
|
|
|
30
|
|
|
->find() |
|
31
|
|
|
->contain([ |
|
32
|
|
|
'BlogCategories' => function ($q) { |
|
33
|
|
|
return $q |
|
34
|
|
|
->select([ |
|
35
|
|
|
'id', |
|
36
|
|
|
'title' |
|
37
|
|
|
]); |
|
38
|
|
|
}, |
|
39
|
|
|
'Users' => function ($q) { |
|
40
|
|
|
return $q->find('short'); |
|
41
|
|
|
} |
|
42
|
|
|
]) |
|
43
|
|
|
->order([ |
|
44
|
|
|
'BlogArticles.created' => 'desc' |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$articles = $this->paginate($articles); |
|
48
|
|
|
$this->set(compact('articles')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Add an article. |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Cake\Network\Response|void |
|
55
|
|
|
*/ |
|
56
|
|
|
public function add() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->loadModel('BlogArticles'); |
|
59
|
|
|
|
|
60
|
|
|
$this->BlogArticles->locale(I18n::defaultLocale()); |
|
|
|
|
|
|
61
|
|
|
$article = $this->BlogArticles->newEntity($this->request->data); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
View Code Duplication |
if ($this->request->is('post')) { |
|
|
|
|
|
|
64
|
|
|
$article->user_id = $this->Auth->user('id'); |
|
65
|
|
|
$article->setTranslations($this->request->data); |
|
66
|
|
|
|
|
67
|
|
|
if ($this->BlogArticles->save($article)) { |
|
|
|
|
|
|
68
|
|
|
$this->Flash->success(__d('admin', 'Your article has been created successfully !')); |
|
69
|
|
|
|
|
70
|
|
|
return $this->redirect(['action' => 'index']); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$categories = $this->BlogArticles->BlogCategories->find('list'); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$this->set(compact('article', 'categories')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Edit an Article. |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Cake\Network\Response|void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function edit() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->loadModel('BlogArticles'); |
|
87
|
|
|
|
|
88
|
|
|
$this->BlogArticles->locale(I18n::defaultLocale()); |
|
|
|
|
|
|
89
|
|
|
$article = $this->BlogArticles |
|
|
|
|
|
|
90
|
|
|
->find('translations') |
|
91
|
|
|
->where([ |
|
92
|
|
|
'BlogArticles.id' => $this->request->id |
|
93
|
|
|
]) |
|
94
|
|
|
->contain([ |
|
95
|
|
|
'BlogAttachments', |
|
96
|
|
|
'BlogCategories', |
|
97
|
|
|
'Users' => function ($q) { |
|
98
|
|
|
return $q->find('short'); |
|
99
|
|
|
} |
|
100
|
|
|
]) |
|
101
|
|
|
->first(); |
|
102
|
|
|
|
|
103
|
|
|
//Check if the article is found. |
|
104
|
|
|
if (empty($article)) { |
|
105
|
|
|
$this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.')); |
|
106
|
|
|
|
|
107
|
|
|
return $this->redirect(['action' => 'index']); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
if ($this->request->is('put')) { |
|
|
|
|
|
|
111
|
|
|
$this->BlogArticles->patchEntity($article, $this->request->data()); |
|
|
|
|
|
|
112
|
|
|
$article->setTranslations($this->request->data); |
|
113
|
|
|
|
|
114
|
|
|
if ($this->BlogArticles->save($article)) { |
|
|
|
|
|
|
115
|
|
|
$this->Flash->success(__d('admin', 'This article has been updated successfully !')); |
|
116
|
|
|
|
|
117
|
|
|
return $this->redirect(['action' => 'index']); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$categories = $this->BlogArticles->BlogCategories->find('list'); |
|
|
|
|
|
|
122
|
|
|
$this->set(compact('article', 'categories')); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Delete an Article and all his comments and likes. |
|
127
|
|
|
* |
|
128
|
|
|
* @return \Cake\Network\Response |
|
129
|
|
|
*/ |
|
130
|
|
View Code Duplication |
public function delete() |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$this->loadModel('BlogArticles'); |
|
133
|
|
|
|
|
134
|
|
|
$article = $this->BlogArticles |
|
|
|
|
|
|
135
|
|
|
->find() |
|
136
|
|
|
->where([ |
|
137
|
|
|
'BlogArticles.id' => $this->request->id |
|
138
|
|
|
]) |
|
139
|
|
|
->first(); |
|
140
|
|
|
|
|
141
|
|
|
//Check if the article is found. |
|
142
|
|
|
if (empty($article)) { |
|
143
|
|
|
$this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.')); |
|
144
|
|
|
|
|
145
|
|
|
return $this->redirect(['action' => 'index']); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($this->BlogArticles->delete($article)) { |
|
|
|
|
|
|
149
|
|
|
$this->Flash->success(__d('admin', 'This article has been deleted successfully !')); |
|
150
|
|
|
|
|
151
|
|
|
return $this->redirect(['action' => 'index']); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->Flash->error(__d('admin', 'Unable to delete this article.')); |
|
155
|
|
|
|
|
156
|
|
|
return $this->redirect(['action' => 'index']); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
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@propertyannotation 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.