1 | <?php |
---|
2 | /** |
---|
3 | * @package Dotclear |
---|
4 | * @subpackage Backend |
---|
5 | * |
---|
6 | * @copyright Olivier Meunier & Association Dotclear |
---|
7 | * @copyright GPL-2.0-only |
---|
8 | */ |
---|
9 | |
---|
10 | if (!defined('DC_RC_PATH')) {return;} |
---|
11 | |
---|
12 | class dcPostsActionsPage extends dcActionsPage |
---|
13 | { |
---|
14 | public function __construct($core, $uri, $redirect_args = []) |
---|
15 | { |
---|
16 | parent::__construct($core, $uri, $redirect_args); |
---|
17 | $this->redirect_fields = ['user_id', 'cat_id', 'status', |
---|
18 | 'selected', 'attachment', 'month', 'lang', 'sortby', 'order', 'page', 'nb']; |
---|
19 | $this->loadDefaults(); |
---|
20 | } |
---|
21 | |
---|
22 | protected function loadDefaults() |
---|
23 | { |
---|
24 | // We could have added a behavior here, but we want default action |
---|
25 | // to be setup first |
---|
26 | dcDefaultPostActions::adminPostsActionsPage($this->core, $this); |
---|
27 | $this->core->callBehavior('adminPostsActionsPage', $this->core, $this); |
---|
28 | |
---|
29 | } |
---|
30 | |
---|
31 | public function beginPage($breadcrumb = '', $head = '') |
---|
32 | { |
---|
33 | if ($this->in_plugin) { |
---|
34 | echo '<html><head><title>' . __('Entries') . '</title>' . |
---|
35 | dcPage::jsLoad('js/_posts_actions.js') . |
---|
36 | $head . |
---|
37 | '</script></head><body>' . |
---|
38 | $breadcrumb; |
---|
39 | } else { |
---|
40 | dcPage::open( |
---|
41 | __('Entries'), |
---|
42 | dcPage::jsLoad('js/_posts_actions.js') . |
---|
43 | $head, |
---|
44 | $breadcrumb |
---|
45 | ); |
---|
46 | } |
---|
47 | echo '<p><a class="back" href="' . $this->getRedirection(true) . '">' . __('Back to entries list') . '</a></p>'; |
---|
48 | } |
---|
49 | |
---|
50 | public function endPage() |
---|
51 | { |
---|
52 | if ($this->in_plugin) { |
---|
53 | echo '</body></html>'; |
---|
54 | } else { |
---|
55 | dcPage::close(); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | public function error(Exception $e) |
---|
60 | { |
---|
61 | $this->core->error->add($e->getMessage()); |
---|
62 | $this->beginPage(dcPage::breadcrumb( |
---|
63 | [ |
---|
64 | html::escapeHTML($this->core->blog->name) => '', |
---|
65 | $this->getCallerTitle() => $this->getRedirection(true), |
---|
66 | __('Entries actions') => '' |
---|
67 | ]) |
---|
68 | ); |
---|
69 | $this->endPage(); |
---|
70 | } |
---|
71 | |
---|
72 | protected function fetchEntries($from) |
---|
73 | { |
---|
74 | $params = []; |
---|
75 | if (!empty($from['entries'])) { |
---|
76 | $entries = $from['entries']; |
---|
77 | |
---|
78 | foreach ($entries as $k => $v) { |
---|
79 | $entries[$k] = (integer) $v; |
---|
80 | } |
---|
81 | |
---|
82 | $params['sql'] = 'AND P.post_id IN(' . implode(',', $entries) . ') '; |
---|
83 | } else { |
---|
84 | $params['sql'] = 'AND 1=0 '; |
---|
85 | } |
---|
86 | |
---|
87 | if (!isset($from['full_content']) || empty($from['full_content'])) { |
---|
88 | $params['no_content'] = true; |
---|
89 | } |
---|
90 | |
---|
91 | if (isset($from['post_type'])) { |
---|
92 | $params['post_type'] = $from['post_type']; |
---|
93 | } |
---|
94 | |
---|
95 | $posts = $this->core->blog->getPosts($params); |
---|
96 | while ($posts->fetch()) { |
---|
97 | $this->entries[$posts->post_id] = $posts->post_title; |
---|
98 | } |
---|
99 | $this->rs = $posts; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | class dcDefaultPostActions |
---|
104 | { |
---|
105 | public static function adminPostsActionsPage($core, $ap) |
---|
106 | { |
---|
107 | if ($core->auth->check('publish,contentadmin', $core->blog->id)) { |
---|
108 | $ap->addAction( |
---|
109 | [__('Status') => [ |
---|
110 | __('Publish') => 'publish', |
---|
111 | __('Unpublish') => 'unpublish', |
---|
112 | __('Schedule') => 'schedule', |
---|
113 | __('Mark as pending') => 'pending' |
---|
114 | ]], |
---|
115 | ['dcDefaultPostActions', 'doChangePostStatus'] |
---|
116 | ); |
---|
117 | } |
---|
118 | $ap->addAction( |
---|
119 | [__('Mark') => [ |
---|
120 | __('Mark as selected') => 'selected', |
---|
121 | __('Mark as unselected') => 'unselected' |
---|
122 | ]], |
---|
123 | ['dcDefaultPostActions', 'doUpdateSelectedPost'] |
---|
124 | ); |
---|
125 | $ap->addAction( |
---|
126 | [__('Change') => [ |
---|
127 | __('Change category') => 'category' |
---|
128 | ]], |
---|
129 | ['dcDefaultPostActions', 'doChangePostCategory'] |
---|
130 | ); |
---|
131 | $ap->addAction( |
---|
132 | [__('Change') => [ |
---|
133 | __('Change language') => 'lang' |
---|
134 | ]], |
---|
135 | ['dcDefaultPostActions', 'doChangePostLang'] |
---|
136 | ); |
---|
137 | if ($core->auth->check('admin', $core->blog->id)) { |
---|
138 | $ap->addAction( |
---|
139 | [__('Change') => [ |
---|
140 | __('Change author') => 'author']], |
---|
141 | ['dcDefaultPostActions', 'doChangePostAuthor'] |
---|
142 | ); |
---|
143 | } |
---|
144 | if ($core->auth->check('delete,contentadmin', $core->blog->id)) { |
---|
145 | $ap->addAction( |
---|
146 | [__('Delete') => [ |
---|
147 | __('Delete') => 'delete']], |
---|
148 | ['dcDefaultPostActions', 'doDeletePost'] |
---|
149 | ); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | public static function doChangePostStatus($core, dcPostsActionsPage $ap, $post) |
---|
154 | { |
---|
155 | switch ($ap->getAction()) { |
---|
156 | case 'unpublish':$status = 0; |
---|
157 | break; |
---|
158 | case 'schedule':$status = -1; |
---|
159 | break; |
---|
160 | case 'pending':$status = -2; |
---|
161 | break; |
---|
162 | default:$status = 1; |
---|
163 | break; |
---|
164 | } |
---|
165 | $posts_ids = $ap->getIDs(); |
---|
166 | if (empty($posts_ids)) { |
---|
167 | throw new Exception(__('No entry selected')); |
---|
168 | } |
---|
169 | $core->blog->updPostsStatus($posts_ids, $status); |
---|
170 | dcPage::addSuccessNotice(sprintf( |
---|
171 | __( |
---|
172 | '%d entry has been successfully updated to status : "%s"', |
---|
173 | '%d entries have been successfully updated to status : "%s"', |
---|
174 | count($posts_ids) |
---|
175 | ), |
---|
176 | count($posts_ids), |
---|
177 | $core->blog->getPostStatus($status)) |
---|
178 | ); |
---|
179 | $ap->redirect(true); |
---|
180 | } |
---|
181 | |
---|
182 | public static function doUpdateSelectedPost($core, dcPostsActionsPage $ap, $post) |
---|
183 | { |
---|
184 | $posts_ids = $ap->getIDs(); |
---|
185 | if (empty($posts_ids)) { |
---|
186 | throw new Exception(__('No entry selected')); |
---|
187 | } |
---|
188 | $action = $ap->getAction(); |
---|
189 | $core->blog->updPostsSelected($posts_ids, $action == 'selected'); |
---|
190 | if ($action == 'selected') { |
---|
191 | dcPage::addSuccessNotice(sprintf( |
---|
192 | __( |
---|
193 | '%d entry has been successfully marked as selected', |
---|
194 | '%d entries have been successfully marked as selected', |
---|
195 | count($posts_ids) |
---|
196 | ), |
---|
197 | count($posts_ids)) |
---|
198 | ); |
---|
199 | } else { |
---|
200 | dcPage::addSuccessNotice(sprintf( |
---|
201 | __( |
---|
202 | '%d entry has been successfully marked as unselected', |
---|
203 | '%d entries have been successfully marked as unselected', |
---|
204 | count($posts_ids) |
---|
205 | ), |
---|
206 | count($posts_ids)) |
---|
207 | ); |
---|
208 | } |
---|
209 | $ap->redirect(true); |
---|
210 | } |
---|
211 | |
---|
212 | public static function doDeletePost($core, dcPostsActionsPage $ap, $post) |
---|
213 | { |
---|
214 | |
---|
215 | $posts_ids = $ap->getIDs(); |
---|
216 | if (empty($posts_ids)) { |
---|
217 | throw new Exception(__('No entry selected')); |
---|
218 | } |
---|
219 | // Backward compatibility |
---|
220 | foreach ($posts_ids as $post_id) { |
---|
221 | # --BEHAVIOR-- adminBeforePostDelete |
---|
222 | $core->callBehavior('adminBeforePostDelete', (integer) $post_id); |
---|
223 | } |
---|
224 | |
---|
225 | # --BEHAVIOR-- adminBeforePostsDelete |
---|
226 | $core->callBehavior('adminBeforePostsDelete', $posts_ids); |
---|
227 | |
---|
228 | $core->blog->delPosts($posts_ids); |
---|
229 | dcPage::addSuccessNotice(sprintf( |
---|
230 | __( |
---|
231 | '%d entry has been successfully deleted', |
---|
232 | '%d entries have been successfully deleted', |
---|
233 | count($posts_ids) |
---|
234 | ), |
---|
235 | count($posts_ids)) |
---|
236 | ); |
---|
237 | |
---|
238 | $ap->redirect(false); |
---|
239 | } |
---|
240 | |
---|
241 | public static function doChangePostCategory($core, dcPostsActionsPage $ap, $post) |
---|
242 | { |
---|
243 | if (isset($post['new_cat_id'])) { |
---|
244 | $posts_ids = $ap->getIDs(); |
---|
245 | if (empty($posts_ids)) { |
---|
246 | throw new Exception(__('No entry selected')); |
---|
247 | } |
---|
248 | $new_cat_id = $post['new_cat_id']; |
---|
249 | if (!empty($post['new_cat_title']) && $core->auth->check('categories', $core->blog->id)) { |
---|
250 | $cur_cat = $core->con->openCursor($core->prefix . 'category'); |
---|
251 | $cur_cat->cat_title = $post['new_cat_title']; |
---|
252 | $cur_cat->cat_url = ''; |
---|
253 | $title = $cur_cat->cat_title; |
---|
254 | |
---|
255 | $parent_cat = !empty($post['new_cat_parent']) ? $post['new_cat_parent'] : ''; |
---|
256 | |
---|
257 | # --BEHAVIOR-- adminBeforeCategoryCreate |
---|
258 | $core->callBehavior('adminBeforeCategoryCreate', $cur_cat); |
---|
259 | |
---|
260 | $new_cat_id = $core->blog->addCategory($cur_cat, (integer) $parent_cat); |
---|
261 | |
---|
262 | # --BEHAVIOR-- adminAfterCategoryCreate |
---|
263 | $core->callBehavior('adminAfterCategoryCreate', $cur_cat, $new_cat_id); |
---|
264 | } |
---|
265 | |
---|
266 | $core->blog->updPostsCategory($posts_ids, $new_cat_id); |
---|
267 | $title = $core->blog->getCategory($new_cat_id); |
---|
268 | dcPage::addSuccessNotice(sprintf( |
---|
269 | __( |
---|
270 | '%d entry has been successfully moved to category "%s"', |
---|
271 | '%d entries have been successfully moved to category "%s"', |
---|
272 | count($posts_ids) |
---|
273 | ), |
---|
274 | count($posts_ids), |
---|
275 | html::escapeHTML($title->cat_title)) |
---|
276 | ); |
---|
277 | |
---|
278 | $ap->redirect(true); |
---|
279 | } else { |
---|
280 | |
---|
281 | $ap->beginPage( |
---|
282 | dcPage::breadcrumb( |
---|
283 | [ |
---|
284 | html::escapeHTML($core->blog->name) => '', |
---|
285 | $ap->getCallerTitle() => $ap->getRedirection(true), |
---|
286 | __('Change category for this selection') => '' |
---|
287 | ])); |
---|
288 | # categories list |
---|
289 | # Getting categories |
---|
290 | $categories_combo = dcAdminCombos::getCategoriesCombo( |
---|
291 | $core->blog->getCategories() |
---|
292 | ); |
---|
293 | echo |
---|
294 | '<form action="' . $ap->getURI() . '" method="post">' . |
---|
295 | $ap->getCheckboxes() . |
---|
296 | '<p><label for="new_cat_id" class="classic">' . __('Category:') . '</label> ' . |
---|
297 | form::combo(['new_cat_id'], $categories_combo); |
---|
298 | |
---|
299 | if ($core->auth->check('categories', $core->blog->id)) { |
---|
300 | echo |
---|
301 | '<div>' . |
---|
302 | '<p id="new_cat">' . __('Create a new category for the post(s)') . '</p>' . |
---|
303 | '<p><label for="new_cat_title">' . __('Title:') . '</label> ' . |
---|
304 | form::field('new_cat_title', 30, 255) . '</p>' . |
---|
305 | '<p><label for="new_cat_parent">' . __('Parent:') . '</label> ' . |
---|
306 | form::combo('new_cat_parent', $categories_combo) . |
---|
307 | '</p>' . |
---|
308 | '</div>'; |
---|
309 | } |
---|
310 | |
---|
311 | echo |
---|
312 | $core->formNonce() . |
---|
313 | $ap->getHiddenFields() . |
---|
314 | form::hidden(['action'], 'category') . |
---|
315 | '<input type="submit" value="' . __('Save') . '" /></p>' . |
---|
316 | '</form>'; |
---|
317 | $ap->endPage(); |
---|
318 | |
---|
319 | } |
---|
320 | |
---|
321 | } |
---|
322 | public static function doChangePostAuthor($core, dcPostsActionsPage $ap, $post) |
---|
323 | { |
---|
324 | if (isset($post['new_auth_id']) && $core->auth->check('admin', $core->blog->id)) { |
---|
325 | $new_user_id = $post['new_auth_id']; |
---|
326 | $posts_ids = $ap->getIDs(); |
---|
327 | if (empty($posts_ids)) { |
---|
328 | throw new Exception(__('No entry selected')); |
---|
329 | } |
---|
330 | if ($core->getUser($new_user_id)->isEmpty()) { |
---|
331 | throw new Exception(__('This user does not exist')); |
---|
332 | } |
---|
333 | |
---|
334 | $cur = $core->con->openCursor($core->prefix . 'post'); |
---|
335 | $cur->user_id = $new_user_id; |
---|
336 | $cur->update('WHERE post_id ' . $core->con->in($posts_ids)); |
---|
337 | dcPage::addSuccessNotice(sprintf( |
---|
338 | __( |
---|
339 | '%d entry has been successfully set to user "%s"', |
---|
340 | '%d entries have been successfully set to user "%s"', |
---|
341 | count($posts_ids) |
---|
342 | ), |
---|
343 | count($posts_ids), |
---|
344 | html::escapeHTML($new_user_id)) |
---|
345 | ); |
---|
346 | |
---|
347 | $ap->redirect(true); |
---|
348 | } else { |
---|
349 | $usersList = ''; |
---|
350 | if ($core->auth->check('admin', $core->blog->id)) { |
---|
351 | $params = [ |
---|
352 | 'limit' => 100, |
---|
353 | 'order' => 'nb_post DESC' |
---|
354 | ]; |
---|
355 | $rs = $core->getUsers($params); |
---|
356 | $rsStatic = $rs->toStatic(); |
---|
357 | $rsStatic->extend('rsExtUser'); |
---|
358 | $rsStatic = $rsStatic->toExtStatic(); |
---|
359 | $rsStatic->lexicalSort('user_id'); |
---|
360 | while ($rsStatic->fetch()) { |
---|
361 | $usersList .= ($usersList != '' ? ',' : '') . '"' . $rsStatic->user_id . '"'; |
---|
362 | } |
---|
363 | } |
---|
364 | $ap->beginPage( |
---|
365 | dcPage::breadcrumb( |
---|
366 | [ |
---|
367 | html::escapeHTML($core->blog->name) => '', |
---|
368 | $ap->getCallerTitle() => $ap->getRedirection(true), |
---|
369 | __('Change author for this selection') => '']), |
---|
370 | dcPage::jsLoad('js/jquery/jquery.autocomplete.js') . |
---|
371 | '<script type="text/javascript">' . "\n" . |
---|
372 | 'usersList = [' . $usersList . ']' . "\n" . |
---|
373 | "</script>\n" |
---|
374 | ); |
---|
375 | |
---|
376 | echo |
---|
377 | '<form action="' . $ap->getURI() . '" method="post">' . |
---|
378 | $ap->getCheckboxes() . |
---|
379 | '<p><label for="new_auth_id" class="classic">' . __('New author (author ID):') . '</label> ' . |
---|
380 | form::field('new_auth_id', 20, 255); |
---|
381 | |
---|
382 | echo |
---|
383 | $core->formNonce() . $ap->getHiddenFields() . |
---|
384 | form::hidden(['action'], 'author') . |
---|
385 | '<input type="submit" value="' . __('Save') . '" /></p>' . |
---|
386 | '</form>'; |
---|
387 | $ap->endPage(); |
---|
388 | } |
---|
389 | } |
---|
390 | public static function doChangePostLang($core, dcPostsActionsPage $ap, $post) |
---|
391 | { |
---|
392 | $posts_ids = $ap->getIDs(); |
---|
393 | if (empty($posts_ids)) { |
---|
394 | throw new Exception(__('No entry selected')); |
---|
395 | } |
---|
396 | if (isset($post['new_lang'])) { |
---|
397 | $new_lang = $post['new_lang']; |
---|
398 | $cur = $core->con->openCursor($core->prefix . 'post'); |
---|
399 | $cur->post_lang = $new_lang; |
---|
400 | $cur->update('WHERE post_id ' . $core->con->in($posts_ids)); |
---|
401 | dcPage::addSuccessNotice(sprintf( |
---|
402 | __( |
---|
403 | '%d entry has been successfully set to language "%s"', |
---|
404 | '%d entries have been successfully set to language "%s"', |
---|
405 | count($posts_ids) |
---|
406 | ), |
---|
407 | count($posts_ids), |
---|
408 | html::escapeHTML(l10n::getLanguageName($new_lang))) |
---|
409 | ); |
---|
410 | $ap->redirect(true); |
---|
411 | } else { |
---|
412 | $ap->beginPage( |
---|
413 | dcPage::breadcrumb( |
---|
414 | [ |
---|
415 | html::escapeHTML($core->blog->name) => '', |
---|
416 | $ap->getCallerTitle() => $ap->getRedirection(true), |
---|
417 | __('Change language for this selection') => '' |
---|
418 | ])); |
---|
419 | # lang list |
---|
420 | # Languages combo |
---|
421 | $rs = $core->blog->getLangs(['order' => 'asc']); |
---|
422 | $all_langs = l10n::getISOcodes(0, 1); |
---|
423 | $lang_combo = ['' => '', __('Most used') => [], __('Available') => l10n::getISOcodes(1, 1)]; |
---|
424 | while ($rs->fetch()) { |
---|
425 | if (isset($all_langs[$rs->post_lang])) { |
---|
426 | $lang_combo[__('Most used')][$all_langs[$rs->post_lang]] = $rs->post_lang; |
---|
427 | unset($lang_combo[__('Available')][$all_langs[$rs->post_lang]]); |
---|
428 | } else { |
---|
429 | $lang_combo[__('Most used')][$rs->post_lang] = $rs->post_lang; |
---|
430 | } |
---|
431 | } |
---|
432 | unset($all_langs); |
---|
433 | unset($rs); |
---|
434 | |
---|
435 | echo |
---|
436 | '<form action="' . $ap->getURI() . '" method="post">' . |
---|
437 | $ap->getCheckboxes() . |
---|
438 | |
---|
439 | '<p><label for="new_lang" class="classic">' . __('Entry language:') . '</label> ' . |
---|
440 | form::combo('new_lang', $lang_combo); |
---|
441 | |
---|
442 | echo |
---|
443 | $core->formNonce() . $ap->getHiddenFields() . |
---|
444 | form::hidden(['action'], 'lang') . |
---|
445 | '<input type="submit" value="' . __('Save') . '" /></p>' . |
---|
446 | '</form>'; |
---|
447 | $ap->endPage(); |
---|
448 | } |
---|
449 | } |
---|
450 | } |
---|