1
|
|
|
<?php |
2
|
|
|
namespace Chp\TextContent; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Text content controller |
6
|
|
|
* Made by Rasmus Berg (c) 2014-2017 |
7
|
|
|
* |
8
|
|
|
* @Property Object $di Anax-MVC class handler |
9
|
|
|
* @Property Object $request Anax-MVC $_POST, $_GET and $_SERVER handler class |
10
|
|
|
* @Property Object $response Anax-MVC Php Header class |
11
|
|
|
* @Property Object $url Anax-MVC url-handler class |
12
|
|
|
* @Property Object $theme Anax-MVC theme-handler class |
13
|
|
|
* @Property Object $views Anax-MVC views-handler class |
14
|
|
|
* @Property Object $textFilter Anax-MVC textformat-handler class |
15
|
|
|
* @Property Object $db PDO database class |
16
|
|
|
*/ |
17
|
|
|
class ContentController implements \Anax\DI\IInjectionAware |
18
|
|
|
{ |
19
|
|
|
use \Anax\DI\TInjectable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Properties |
23
|
|
|
*/ |
24
|
|
|
private $content = null; |
25
|
|
|
private $limitListPerPage = null; // Limit contents on list page |
26
|
|
|
private $minimumLength = 3; // Minimum length on text-fields (ex. ingress, title etc) |
27
|
|
|
private $types = [ // Content types |
28
|
|
|
'blog-post' => ['url' => 'blog/read/', 'field' => 'slug', 'perfix' => '', 'title' => 'Blog'], |
29
|
|
|
'page' => ['url' => 'page/page/', 'field' => 'url', 'perfix' => '', 'title' => 'Page'] |
30
|
|
|
]; |
31
|
|
|
private $filters = array('bbcode','clickable','markdown', 'nl2br', 'shortcode'); |
32
|
|
|
private $urlPrefix = "content.php/"; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize the controller |
36
|
|
|
* |
37
|
|
|
* @Return Void |
38
|
|
|
*/ |
39
|
10 |
|
public function initialize(){ |
40
|
10 |
|
$this->content = new \Chp\TextContent\Content(); |
41
|
10 |
|
$this->content->setDI($this->di); |
42
|
10 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Index content - use listAction |
46
|
|
|
* |
47
|
|
|
* @Returns Void |
48
|
|
|
*/ |
49
|
|
|
public function indexAction(){ |
50
|
|
|
$this->listAction(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Setup content (If it allready exist it will restore database to begining) |
55
|
|
|
* |
56
|
|
|
* @Return Void |
57
|
|
|
*/ |
58
|
|
|
public function setupAction(){ |
59
|
|
|
$toDo = "Restore or setup"; |
60
|
|
|
$toWhat = "content database tables"; |
61
|
|
|
$title = "{$toDo} {$toWhat}"; |
62
|
|
|
$form = $this->confirmForm($this->url->create($this->urlPrefix . 'content/')); |
63
|
|
|
$status = $form->check(); |
64
|
|
|
|
65
|
|
|
if($status === true){ |
66
|
|
|
|
67
|
|
|
$this->db->dropTableIfExists('content')->execute(); |
68
|
|
|
|
69
|
|
|
$this->db->createTable( |
70
|
|
|
'content', |
71
|
|
|
[ |
72
|
|
|
'id' => ['integer', 'primary key', 'not null', 'auto_increment'], |
73
|
|
|
'slug' => ['char(80)'], |
74
|
|
|
'url' => ['char(80)'], |
75
|
|
|
'type' => ['char(80)'], |
76
|
|
|
'title' => ['varchar(80)'], |
77
|
|
|
'ingress' => ['text'], |
78
|
|
|
'text' => ['text'], |
79
|
|
|
'filters' => ['char(80)'], |
80
|
|
|
'author' => ['integer', 'not null'], |
81
|
|
|
'published' => ['datetime'], |
82
|
|
|
'created' => ['datetime'], |
83
|
|
|
'updated' => ['datetime'], |
84
|
|
|
'deleted' => ['datetime'] |
85
|
|
|
] |
86
|
|
|
)->execute(); |
87
|
|
|
|
88
|
|
|
$this->db->insert( |
89
|
|
|
'content', |
90
|
|
|
['slug', 'url', 'type', 'title', 'ingress', 'text', 'filters', 'author', 'published', 'created'] |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$now = date('Y-m-d H:i:s'); |
94
|
|
|
|
95
|
|
|
$this->db->execute([ |
96
|
|
|
'welcome_to_your_new_blog', |
97
|
|
|
NULL, |
98
|
|
|
'blog-post', |
99
|
|
|
'Welcome to your new blog', |
100
|
|
|
'This is example-blogg to show your new blog system!', |
101
|
|
|
'You can start blog by visit [url=' . $this->url->create($this->urlPrefix . "content/add/") . ']Add content[/url]', |
102
|
|
|
'bbcode', |
103
|
|
|
3, |
104
|
|
|
$now, |
105
|
|
|
$now |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$this->db->execute([ |
109
|
|
|
'welcome_to_your_new_page', |
110
|
|
|
'example-page', |
111
|
|
|
'page', |
112
|
|
|
'Welcome to your new page', |
113
|
|
|
'This is example-page to show your new page system!', |
114
|
|
|
'You can start makeing pages by visit [url=' . $this->url->create($this->urlPrefix . "content/add/") . ']Add content[/url]', |
115
|
|
|
'bbcode', |
116
|
|
|
3, |
117
|
|
|
$now, |
118
|
|
|
$now |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$this->db->dropTableIfExists('content_tags')->execute(); |
122
|
|
|
|
123
|
|
|
$this->db->createTable( |
124
|
|
|
'content_tags', |
125
|
|
|
[ |
126
|
|
|
'idContent' => ['integer', 'not null'], |
127
|
|
|
'tag' => ['varchar(150)', 'not null'], |
128
|
|
|
'slug' => ['varchar(150)', 'not null'] |
129
|
|
|
] |
130
|
|
|
)->execute(); |
131
|
|
|
|
132
|
|
|
$this->db->insert( |
133
|
|
|
'content_tags', |
134
|
|
|
['idContent', 'tag', 'slug'] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->db->execute([ |
138
|
|
|
1, |
139
|
|
|
'New blog', |
140
|
|
|
'new_blog' |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
$this->db->execute([ |
144
|
|
|
1, |
145
|
|
|
'Blog information', |
146
|
|
|
'blog_information' |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
$this->views->add('text-content/action-finish', [ |
150
|
|
|
'title' => $title, |
151
|
|
|
'msg' => "Content and content tags database tables " . strtolower($toDo). " was successful!" |
152
|
|
|
], 'main'); |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
else{ |
156
|
|
|
$this->views->add('text-content/action', [ |
157
|
|
|
'title' => $title, |
158
|
|
|
'toDo' => strtolower($toDo), |
159
|
|
|
'toWhat' => $toWhat, |
160
|
|
|
'form' => $form->getHTML(['novalidate' => true]) |
161
|
|
|
], 'main'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->theme->setTitle($title); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* List content (by type) |
169
|
|
|
* |
170
|
|
|
* @Param String $type Type to list |
171
|
|
|
* @Param Integer $page Page that paging is on |
172
|
|
|
* @Return Void |
173
|
|
|
*/ |
174
|
|
|
public function listAction($type = null, $published = false, $page = null){ |
175
|
|
|
$type = ($this->checkType($type)) ? $type : null; |
176
|
|
|
$title = "All Content {$type} listed"; |
177
|
|
|
$published = boolval($published); |
178
|
|
|
|
179
|
|
|
$form = $this->listForm($type, $published); |
180
|
|
|
$form->check(); |
181
|
|
|
|
182
|
|
|
if(!is_null($type)) |
183
|
|
|
$contents = $this->content->getAllContentOfType($type, $page, $this->limitListPerPage, $published); |
184
|
|
|
else |
185
|
|
|
$contents = $this->content->getAllContent($page, $this->limitListPerPage, $published); |
186
|
|
|
$contents = $this->prepareListContent($contents); |
187
|
|
|
|
188
|
|
|
$this->theme->setTitle($title); |
189
|
|
|
$this->views->add('text-content/list', |
190
|
|
|
[ |
191
|
|
|
'title' => $title, |
192
|
|
|
'form' => $form->getHTML(array('novalidate' => true)), |
193
|
|
|
'contents' => $contents, |
194
|
|
|
'addUrl' => $this->url->create($this->urlPrefix . 'content/add/'), |
195
|
|
|
'setupUrl' => $this->url->create($this->urlPrefix . 'content/setup/') |
196
|
|
|
] |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Add content to database |
203
|
|
|
* |
204
|
|
|
* @Return Void |
205
|
|
|
*/ |
206
|
|
|
public function addAction(){ |
207
|
|
|
$action = "Add"; |
208
|
|
|
$title = "{$action} content"; |
209
|
|
|
|
210
|
|
|
$form = $this->contentForm($action); |
211
|
|
|
$form->check(); |
212
|
|
|
|
213
|
|
|
$this->theme->setTitle($title); |
214
|
|
|
$this->views->add('text-content/post', |
215
|
|
|
[ |
216
|
|
|
'title' => $title, |
217
|
|
|
'action' => $action, |
218
|
|
|
'form' => $form->getHTML(array('novalidate' => true)) |
219
|
|
|
]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Edit content in database |
224
|
|
|
* |
225
|
|
|
* @Param Integer $id Index for content to edit |
226
|
|
|
* @Return Void |
227
|
|
|
*/ |
228
|
|
|
public function editAction($id = null){ |
229
|
|
|
$action = "Edit"; |
230
|
|
|
$title = "{$action} content"; |
231
|
|
|
$url = $this->url->create($this->urlPrefix . 'content/'); |
232
|
|
|
|
233
|
|
|
if(is_null($id) || !is_numeric($id)) |
234
|
|
|
$this->response->redirect($url); |
235
|
|
|
|
236
|
|
|
$content = $this->content->getContentById($id, false); |
237
|
|
|
|
238
|
|
|
if(is_null($content->id)) |
239
|
|
|
$this->response->redirect($url); |
240
|
|
|
|
241
|
|
|
$form = $this->contentForm(strtolower($action), $content); |
242
|
|
|
$form->check(); |
243
|
|
|
|
244
|
|
|
$this->theme->setTitle($title); |
245
|
|
|
|
246
|
|
|
$this->views->add('text-content/post', |
247
|
|
|
[ |
248
|
|
|
'title' => $title, |
249
|
|
|
'action' => $action, |
250
|
|
|
'form' => $form->getHTML(array('novalidate' => true)) |
251
|
|
|
] |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Remove content |
257
|
|
|
* |
258
|
|
|
* @Param Integer $id Index to content to remove |
259
|
|
|
* @Return String $error Database-error msg |
260
|
|
|
*/ |
261
|
|
|
public function removeAction($id = null){ |
262
|
|
|
$action = "Delete"; |
263
|
|
|
$title = "Delete content"; |
264
|
|
|
$url = $this->url->create($this->urlPrefix . "content/"); |
265
|
|
|
|
266
|
|
|
if(is_null($id) || !is_numeric($id)){ |
267
|
|
|
$this->response->redirect($url); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$content = $this->content->find($id); |
271
|
|
|
|
272
|
|
|
if(is_null($content->id)){ |
273
|
|
|
$this->response->redirect($url); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$form = $this->confirmForm($url); |
277
|
|
|
$status = $form->Check(); |
278
|
|
|
|
279
|
|
|
if ($status === true) { |
280
|
|
|
$now = date('Y-m-d H:i:s'); |
281
|
|
|
|
282
|
|
|
$content->deleted = $now; |
283
|
|
|
$content->save(); |
284
|
|
|
|
285
|
|
|
$this->response->redirect($url); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$this->theme->setTitle($title); |
289
|
|
|
$this->views->add('text-content/action', [ |
290
|
|
|
'title' => $title, |
291
|
|
|
'toDo' => strtolower($action), |
292
|
|
|
'toWhat' => strtolower($this->getTypeTitle($content->type)), |
293
|
|
|
'which' => htmlentities($content->title), |
294
|
|
|
'form' => $form->getHTML(['novalidate' => true]), |
295
|
|
|
], 'main'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Prepare form to add or edit content |
300
|
|
|
* |
301
|
|
|
* @Param String $type Selected content-type |
302
|
|
|
* @Param Boolean $published If content should be published already |
303
|
|
|
* @Return Object $form CForm object |
304
|
|
|
*/ |
305
|
|
|
private function listForm($type = null, $published = false){ |
306
|
|
|
|
307
|
|
|
$type_options = array_merge([0 => "Select a type of content"], $this->getTypes()); |
308
|
|
|
|
309
|
|
|
$form = new \Mos\HTMLForm\CForm([], [ |
310
|
|
|
'type' => [ |
311
|
|
|
'type' => 'select', |
312
|
|
|
'label' => 'Type of content:', |
313
|
|
|
'options' => $type_options, |
314
|
|
|
'value' => (isset($type)) ? $type : '' |
315
|
|
|
], |
316
|
|
|
'published' => [ |
317
|
|
|
'type' => 'checkbox', |
318
|
|
|
'label' => 'Published', |
319
|
|
|
'checked' => $published, |
320
|
|
|
'value' => 1 |
321
|
|
|
], |
322
|
|
|
'submit' => [ |
323
|
|
|
'value' => 'Filter', |
324
|
|
|
'type' => 'submit', |
325
|
|
|
'callback' => function ($form) { |
326
|
|
|
$published = ($this->request->getPost('published')) ? 1 : 0; |
327
|
|
|
$type = $form->Value('type'); |
328
|
|
|
|
329
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . "content/list/{$type}/{$published}")); |
330
|
|
|
} |
331
|
|
|
] |
332
|
|
|
]); |
333
|
|
|
|
334
|
|
|
return $form; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Prepare form to add or edit content |
339
|
|
|
* |
340
|
|
|
* @Param String $action What to do (add or edit) |
341
|
|
|
* @Param Object $values Content values to add form elements |
342
|
|
|
* @Return Object $form CForm object |
343
|
|
|
*/ |
344
|
1 |
|
private function contentForm($action, $values = null){ |
345
|
|
|
if(isset($values) && is_object($values)){ |
346
|
|
|
$valArr = get_object_vars($values); |
347
|
|
|
extract($valArr); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$slug = (isset($slug)) ? $slug : NULL; |
351
|
|
|
|
352
|
|
|
$type_options = $this->getTypes(); |
353
|
|
|
|
354
|
|
|
$form = new \Mos\HTMLForm\CForm([], [ |
355
|
|
|
'id' => [ |
356
|
|
|
'type' => 'hidden', |
357
|
|
|
'value' => (isset($id)) ? $id : 0 |
358
|
|
|
], |
359
|
|
|
'title' => [ |
360
|
|
|
'type' => 'text', |
361
|
|
|
'label' => 'Title: (Between ' . $this->minimumLength . ' to 80 chars)', |
362
|
|
|
'maxlength' => 80, |
363
|
|
|
'required' => true, |
364
|
|
|
'value' => (isset($title)) ? $title : '', |
365
|
|
|
'validation' => [ |
366
|
|
|
'custom_test' => array( |
367
|
|
|
'message' => 'Minmum length is ' . $this->minimumLength . ' chars.', |
368
|
|
|
'test' => array($this, 'minimumLength') |
369
|
|
|
) |
370
|
|
|
] |
371
|
|
|
], |
372
|
|
|
'url' => [ |
373
|
|
|
'type' => 'text', |
374
|
|
|
'label' => 'Url:', |
375
|
|
|
'maxlength' => 80, |
376
|
|
|
'value' => (isset($url)) ? $url : '', |
377
|
|
|
'required' => false, |
378
|
1 |
|
'validation' => [ |
379
|
|
|
'custom_test' => array( |
380
|
|
|
'message' => 'Url is not in accepted-format for url:s (accepted characters is \'a-z0-9-_()\').', |
381
|
|
|
'test' => array($this, 'validateSlug') |
382
|
|
|
) |
383
|
|
|
] |
384
|
|
|
], |
385
|
|
|
'ingress' => [ |
386
|
|
|
'type' => 'textarea', |
387
|
|
|
'label' => 'Ingress: (minimum 3 chars)', |
388
|
|
|
'value' => (isset($ingress)) ? $ingress : '', |
389
|
|
|
'required' => true, |
390
|
|
|
'validation' => [ |
391
|
|
|
'custom_test' => array( |
392
|
|
|
'message' => 'Minmum length is ' . $this->minimumLength . ' chars.', |
393
|
|
|
'test' => array($this, 'minimumLength') |
394
|
|
|
) |
395
|
|
|
] |
396
|
|
|
], |
397
|
|
|
'text' => [ |
398
|
|
|
'type' => 'textarea', |
399
|
|
|
'label' => 'Text: (minimum 3 chars)', |
400
|
|
|
'value' => (isset($text)) ? $text : '', |
401
|
|
|
'required' => true, |
402
|
|
|
'validation' => [ |
403
|
|
|
'custom_test' => array( |
404
|
|
|
'message' => 'Minmum length is ' . $this->minimumLength . ' chars.', |
405
|
|
|
'test' => array($this, 'minimumLength') |
406
|
|
|
) |
407
|
|
|
] |
408
|
|
|
], |
409
|
|
|
'type' => [ |
410
|
|
|
'type' => 'select', |
411
|
|
|
'label' => 'Type of content:', |
412
|
|
|
'options' => $type_options, |
413
|
|
|
'value' => (isset($type)) ? $type : '', |
414
|
|
|
'required' => true, |
415
|
|
|
'validation' => [ |
416
|
|
|
'not_empty', |
417
|
|
|
'custom_test' => array( |
418
|
|
|
'message' => 'You need to select a existing type.', |
419
|
|
|
'test' => array($this, 'checkType') |
420
|
|
|
) |
421
|
|
|
] |
422
|
|
|
], |
423
|
|
|
'tags' => [ |
424
|
|
|
'type' => 'text', |
425
|
|
|
'label' => 'Tags: (Seperate by \',\')', |
426
|
|
|
'value' => (isset($tags)) ? $tags : '', |
427
|
|
|
'required' => false |
428
|
|
|
], |
429
|
|
|
'filters' => [ |
430
|
|
|
'type' => 'checkbox-multiple', |
431
|
|
|
'label' => 'Text filter:', |
432
|
|
|
'values' => $this->filters, |
433
|
|
|
'checked' => (isset($filters)) ? explode(',', $filters) : array(), |
434
|
|
|
'validation' => [ |
435
|
|
|
'custom_test' => array( |
436
|
|
|
'message' => 'You need to select a existing type.', |
437
|
|
|
'test' => array($this, 'checkFilter') |
438
|
|
|
) |
439
|
|
|
] |
440
|
|
|
], |
441
|
|
|
'published' => [ |
442
|
|
|
'type' => 'datetime', |
443
|
|
|
'label' => 'Published: (YYYY-MM-DD HH:MM:SS)', |
444
|
|
|
'value' => (isset($published)) ? $published : '', |
445
|
|
|
'validation' => [ |
446
|
|
|
'custom_test' => array( |
447
|
|
|
'message' => 'It need to be in a correct date and time with format: YYYY-MM-DD HH:MM:SS.', |
448
|
|
|
'test' => array($this, 'checkDatetime') |
449
|
|
|
) |
450
|
|
|
] |
451
|
|
|
], |
452
|
|
|
'publishedNow' => [ |
453
|
|
|
'type' => 'checkbox', |
454
|
|
|
'label' => 'Publish now:', |
455
|
|
|
'checked' => (!isset($published) && !isset($id)), |
456
|
|
|
'value' => 'yes' |
457
|
|
|
], |
458
|
|
|
'submit' => [ |
459
|
|
|
'value' => 'Save', |
460
|
|
|
'type' => 'submit', |
461
|
|
|
'callback' => function ($form) use($slug, $action) { |
462
|
|
|
return $this->saveContent($form, $slug, $action); |
463
|
|
|
} |
464
|
|
|
] |
465
|
|
|
]); |
466
|
|
|
|
467
|
|
|
return $form; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Prepare confirmation form |
472
|
|
|
* |
473
|
|
|
* @Param String $returnUrl Return url |
474
|
|
|
* @Return Object $form Form-object |
475
|
|
|
*/ |
476
|
|
|
public function confirmForm($returnUrl = null){ |
477
|
|
|
$returnUrl = (isset($returnUrl)) ? $returnUrl : $this->request->getBaseUrl(); |
478
|
|
|
|
479
|
|
|
$form = new \Mos\HTMLForm\CForm([], [ |
480
|
|
|
'submit' => [ |
481
|
|
|
'type' => 'submit', |
482
|
|
|
'value' => 'Yes', |
483
|
|
|
'callback' => function() { |
484
|
|
|
return true; |
485
|
|
|
} |
486
|
|
|
], |
487
|
|
|
'submit-no' => [ |
488
|
|
|
'type' => "submit", |
489
|
|
|
'value' => 'No', |
490
|
|
|
'callback' => function() use($returnUrl) { |
491
|
|
|
$this->response->redirect($returnUrl); |
492
|
|
|
} |
493
|
|
|
] |
494
|
|
|
] |
495
|
|
|
); |
496
|
|
|
|
497
|
|
|
// Check the status of the form |
498
|
|
|
return $form; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Save content to database |
503
|
|
|
* |
504
|
|
|
* @Param Object $form Form object |
505
|
|
|
* @Param String $slug Old slug for content to compare |
506
|
|
|
* @Param String $action Edit or make new content |
507
|
|
|
* @Return Boolean false If saving fail, return false |
508
|
|
|
*/ |
509
|
|
|
private function saveContent($form, $slug = null, $action){ |
|
|
|
|
510
|
|
|
// Prepare content for saving |
511
|
|
|
$content = $this->prepareSaveContent($form, $slug); |
512
|
|
|
|
513
|
|
|
// Save content |
514
|
|
|
$content = $this->content->save($content); |
515
|
|
|
|
516
|
|
|
// Saving fail |
517
|
|
|
if(!$this->content->id) |
518
|
|
|
return false; |
519
|
|
|
// Save tags for content to database |
520
|
|
|
else if($content['id'] != 0) |
521
|
|
|
$this->saveTags($form->Value('tags'), $this->content->id); |
522
|
|
|
|
523
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . "content/")); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Save tags for content to database |
528
|
|
|
* |
529
|
|
|
* @Param String $tags Tags for content |
530
|
|
|
* @Param Int $id Content index |
531
|
|
|
*/ |
532
|
|
|
private function saveTags($tags, $id){ |
533
|
|
|
$this->db->delete( |
534
|
|
|
'content_tags', |
535
|
|
|
'idContent = ?' |
536
|
|
|
)->execute([$id]); |
537
|
|
|
|
538
|
|
|
$this->db->insert( |
539
|
|
|
'content_tags', |
540
|
|
|
['idContent', 'tag', 'slug'] |
541
|
|
|
); |
542
|
|
|
|
543
|
|
|
if(isset($tags) && !is_null($tags)){ |
544
|
|
|
$tagsArr = explode(",", $tags); |
545
|
|
|
|
546
|
|
|
foreach($tagsArr as $tag){ |
547
|
|
|
$tag = trim($tag); |
548
|
|
|
$this->db->execute([ |
549
|
|
|
$id, |
550
|
|
|
$tag, |
551
|
|
|
$this->slugify($tag) |
552
|
|
|
]); |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Prepare contents for show in list view |
559
|
|
|
* |
560
|
|
|
* @Param Object $contents Object with content objects |
561
|
|
|
* @Return Array $results Array with prepare content objects |
562
|
|
|
*/ |
563
|
1 |
|
public function prepareListContent($contents){ |
564
|
1 |
|
$results = array(); |
565
|
|
|
|
566
|
1 |
|
foreach($contents AS $key => $content){ |
567
|
1 |
|
$available = $this->checkIfAvailable($content->published); |
568
|
1 |
|
$results[$key] = (object)[]; |
569
|
|
|
|
570
|
1 |
|
foreach($content as $key2 => $value){ |
571
|
1 |
|
$results[$key]->{$key2} = $value; |
572
|
1 |
|
} |
573
|
|
|
|
574
|
1 |
|
$results[$key]->typeTxt = $this->getTypeTitle($content->type); |
575
|
1 |
|
$results[$key]->title = htmlspecialchars($content->title, ENT_QUOTES); |
576
|
1 |
|
$results[$key]->editUrl = $this->url->create($this->urlPrefix . "content/edit/{$content->id}"); |
577
|
1 |
|
$results[$key]->removeUrl = $this->url->create($this->urlPrefix . "content/remove/{$content->id}"); |
578
|
1 |
|
$results[$key]->showUrl = $this->getUrlToContent($content); |
579
|
1 |
|
$results[$key]->available = ((!$available) ? "not-" : null) . "published"; |
580
|
1 |
|
$results[$key]->publishedTxt = ($available) ? $contents[$key]->published : "Not published yet"; |
581
|
1 |
|
} |
582
|
|
|
|
583
|
1 |
|
return $results; |
584
|
|
|
} |
585
|
|
|
/** |
586
|
|
|
* Prepare save of content to database |
587
|
|
|
* |
588
|
|
|
* @Param Object $form Form object |
589
|
|
|
* @Param String $oldSlug Old slug for content to compare |
590
|
|
|
* @Return Array $content Prepare content array |
591
|
|
|
*/ |
592
|
1 |
|
public function prepareSaveContent($form, $oldSlug = null){ |
593
|
1 |
|
$now = date('Y-m-d H:i:s'); |
594
|
|
|
|
595
|
|
|
// Prepare new slug |
596
|
1 |
|
$newSlug = $this->prepareNewSlug($form->Value('title'), $form->Value('type'), $oldSlug); |
597
|
|
|
|
598
|
|
|
$content = array( |
599
|
1 |
|
'title' => $form->Value('title'), |
600
|
1 |
|
'slug' => $newSlug, |
601
|
1 |
|
'url' => $this->slugify($form->Value('url')), |
602
|
1 |
|
'ingress' => $form->Value('ingress'), |
603
|
1 |
|
'text' => $form->Value('text'), |
604
|
1 |
|
'type' => $form->Value('type'), |
605
|
1 |
|
'filters' => ($form->Value('filters')) ? implode(",", $form->Value('filters')) : '', |
606
|
1 |
|
'published' => ($form->Value('publishedNow') && $form->Value('publishedNow') == 'yes') ? $now : $form->Value('published') |
607
|
1 |
|
); |
608
|
|
|
|
609
|
1 |
|
$id = ($form->Value('id')) ? intval($form->Value('id')) : 0; |
610
|
|
|
|
611
|
1 |
|
if($id != 0){ |
612
|
1 |
|
$content['updated'] = $now; |
613
|
1 |
|
$content['id'] = $id; |
614
|
1 |
|
} |
615
|
|
|
else{ |
616
|
1 |
|
$content['created'] = $now; |
617
|
1 |
|
$content['author'] = 0;//$this->user->getUserId(); |
618
|
|
|
} |
619
|
|
|
|
620
|
1 |
|
return $content; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* Prepare new slug for content by title |
625
|
|
|
* |
626
|
|
|
* @Param String $title Content title to make slug by |
627
|
|
|
* @Param String $type Content type |
628
|
|
|
* @Param String $oldSlug Old slug for content to compare |
629
|
|
|
* @Return String $newSlug New unique slug for content |
630
|
|
|
*/ |
631
|
1 |
|
public function prepareNewSlug($title, $type, $oldSlug = null){ |
632
|
1 |
|
$newSlug = $this->slugify($title); |
633
|
|
|
|
634
|
1 |
|
if($oldSlug != $newSlug && isset($newSlug)) |
635
|
1 |
|
$newSlug = $this->content->makeSlugToContent($newSlug, $type); |
636
|
|
|
|
637
|
1 |
|
return $newSlug; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Check if content is published |
642
|
|
|
* |
643
|
|
|
* @Param String $datetime When it will be published |
644
|
|
|
* @Return Boolean True/false Validate result |
645
|
|
|
*/ |
646
|
2 |
|
public function checkIfAvailable($datetime){ |
647
|
2 |
|
return ($datetime <= date('Y-m-d H:i:s')) ? true : false; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* Create a link to the content, based on its type. |
652
|
|
|
* |
653
|
|
|
* @Param Object $content Content to link to |
654
|
|
|
* @Return String With url for content |
655
|
|
|
*/ |
656
|
2 |
|
public function getUrlToContent($content) { |
657
|
2 |
|
if(isset($this->types[$content->type])){ |
658
|
2 |
|
$type = $this->types[$content->type]; // Get type from type index |
659
|
|
|
|
660
|
2 |
|
return $this->url->create($this->urlPrefix . "{$type['url']}{$type['perfix']}{$content->{$type['field']}}"); |
661
|
|
|
} |
662
|
|
|
|
663
|
1 |
|
return null; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* Return array with all content types title and keys (Use for content-type select) |
668
|
|
|
* |
669
|
|
|
* @Return Array $types Array with the types title and keys |
670
|
|
|
*/ |
671
|
1 |
|
public function getTypes(){ |
672
|
1 |
|
$types = array(); |
673
|
|
|
|
674
|
|
|
// Loop through and save types key as key and title as value in a new array |
675
|
1 |
|
foreach($this->types AS $key => $value){ |
676
|
1 |
|
$types[$key] = $value['title']; |
677
|
1 |
|
} |
678
|
|
|
|
679
|
1 |
|
return $types; |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* Return name of one specific type |
684
|
|
|
* |
685
|
|
|
* @Params String $type Type key |
686
|
|
|
* @Return String Type title |
687
|
|
|
*/ |
688
|
1 |
|
public function getTypeTitle($type){ |
689
|
1 |
|
return $this->types[$type]['title']; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Create a slug of a string, to be used as url. |
694
|
|
|
* |
695
|
|
|
* @Param String $str String to format as slug. |
696
|
|
|
* @Return String $str Formatted slug. |
697
|
|
|
*/ |
698
|
2 |
|
public function slugify($str) { |
699
|
2 |
|
$str = mb_strtolower(trim($str)); |
700
|
|
|
|
701
|
2 |
|
$str = str_replace(array("å","ä","ö"), array("a","a","o"), utf8_decode(utf8_encode($str))); |
702
|
|
|
|
703
|
2 |
|
$str = preg_replace('/[^a-z0-9-_()]/', '_', $str); |
704
|
2 |
|
$str = trim(preg_replace('/_+/', '_', $str), '_'); |
705
|
2 |
|
return $str; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Check so the choosed type exist. |
710
|
|
|
* |
711
|
|
|
* @Param String $type Choosed type on content |
712
|
|
|
* @Returns Boolean Validate result |
713
|
|
|
*/ |
714
|
1 |
|
public function checkType($type){ |
715
|
1 |
|
return isset($this->types[$type]); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Validate posted datetime so it is correct |
720
|
|
|
* |
721
|
|
|
* @Param String $datetime Posted datetime to check |
722
|
|
|
* @Return Boolean True/false Validate status |
723
|
|
|
*/ |
724
|
1 |
|
public function checkDatetime($datetime){ |
725
|
1 |
|
if(isset($datetime) && !empty($datetime)){ |
726
|
1 |
|
$format = 'Y-m-d H:i:s'; |
727
|
1 |
|
$d = \DateTime::createFromFormat($format, $datetime); |
728
|
1 |
|
return $d && $d->format($format) == $datetime; |
729
|
|
|
} |
730
|
1 |
|
return true; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Minimum length (set by $this->minimumLength) |
735
|
|
|
* |
736
|
|
|
* @Param String $value Value from form-element to validate |
737
|
|
|
* @Return Boolean True/false Validate result |
738
|
|
|
*/ |
739
|
1 |
|
public function minimumLength($value){ |
740
|
1 |
|
return (strlen($value) >= $this->minimumLength); |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* Validate slug url |
745
|
|
|
* |
746
|
|
|
* @Param String $url Url to validate |
747
|
|
|
* @Return Boolean True/false True if valid otherwish false |
748
|
|
|
*/ |
749
|
1 |
|
public function validateSlug($url){ |
750
|
1 |
|
return ($this->slugify($url) == $url); |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
/** |
754
|
|
|
* Check so the select filters exist. |
755
|
|
|
* |
756
|
|
|
* @Param Array $filters Array with select filters |
757
|
|
|
* @Return Boolean $result Return the result of test |
758
|
|
|
*/ |
759
|
1 |
|
public function checkFilter($filter = null){ |
760
|
1 |
|
if(!empty($filter)){ |
761
|
|
|
// For each filter, check if the filter exist |
762
|
1 |
|
foreach($this->filters as $val){ |
763
|
1 |
|
if($val == $filter) |
764
|
1 |
|
return true; |
765
|
1 |
|
} |
766
|
1 |
|
return false; |
767
|
|
|
} |
768
|
1 |
|
return true; |
769
|
|
|
} |
770
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.