1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of pollsFactory, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | if (!defined('DC_CONTEXT_ADMIN')){return;} |
---|
14 | |
---|
15 | require_once dirname(__FILE__).'/_widgets.php'; |
---|
16 | |
---|
17 | $_menu['Blog']->addItem( |
---|
18 | __('Polls manager'), |
---|
19 | 'plugin.php?p=pollsFactory','index.php?pf=pollsFactory/icon.png', |
---|
20 | preg_match('/plugin.php\?p=pollsFactory(&.*)?$/',$_SERVER['REQUEST_URI']), |
---|
21 | $core->auth->check('admin',$core->blog->id) |
---|
22 | ); |
---|
23 | |
---|
24 | # Posts actions |
---|
25 | if ($core->blog->settings->pollsFactory->pollsFactory_active |
---|
26 | && $core->auth->check('admin',$core->blog->id)) |
---|
27 | { |
---|
28 | $core->addBehavior('adminPostFormSidebar',array('adminPollsFactory','adminPostFormSidebar')); |
---|
29 | $core->addBehavior('adminPageFormSidebar',array('adminPollsFactory','adminPostFormSidebar')); |
---|
30 | $core->addBehavior('adminGalleryFormSidebar',array('adminPollsFactory','adminPostFormSidebar')); |
---|
31 | |
---|
32 | $core->addBehavior('adminPostHeaders',array('adminPollsFactory','adminPostHeaders')); |
---|
33 | $core->addBehavior('adminPageHeaders',array('adminPollsFactory','adminPostHeaders')); |
---|
34 | $core->addBehavior('adminGalleryHeaders',array('adminPollsFactory','adminPostHeaders')); |
---|
35 | |
---|
36 | $core->addBehavior('adminAfterPostCreate',array('adminPollsFactory','adminAfterPostCreate')); |
---|
37 | $core->addBehavior('adminAfterPageCreate',array('adminPollsFactory','adminAfterPostCreate')); |
---|
38 | $core->addBehavior('adminAfterGalleryCreate',array('adminPollsFactory','adminAfterPostCreate')); |
---|
39 | |
---|
40 | $core->addBehavior('adminAfterPostUpdate',array('adminPollsFactory','adminAfterPostUpdate')); |
---|
41 | $core->addBehavior('adminAfterPageUpdate',array('adminPollsFactory','adminAfterPostUpdate')); |
---|
42 | $core->addBehavior('adminAfterGalleryUpdate',array('adminPollsFactory','adminAfterPostUpdate')); |
---|
43 | |
---|
44 | $core->addBehavior('adminPostsActionsCombo',array('adminPollsFactory','adminPostsActionsCombo')); |
---|
45 | $core->addBehavior('adminPagesActionsCombo',array('adminPollsFactory','adminPostsActionsCombo')); |
---|
46 | $core->addBehavior('adminGalleriesActionsCombo',array('adminPollsFactory','adminPostsActionsCombo')); |
---|
47 | |
---|
48 | $core->addBehavior('adminPostsActionsContent',array('adminPollsFactory','adminPostsActionsContent')); |
---|
49 | // plugin pages is common with post |
---|
50 | // plugin gallery is common with post |
---|
51 | |
---|
52 | $core->addBehavior('adminPostsActions',array('adminPollsFactory','adminPostsActions')); |
---|
53 | $core->addBehavior('adminPagesActions',array('adminPollsFactory','adminPostsActions')); |
---|
54 | $core->addBehavior('adminGalleriesActions',array('adminPollsFactory','adminPostsActions')); |
---|
55 | } |
---|
56 | $core->addBehavior('adminBeforePostDelete',array('adminPollsFactory','adminBeforePostDelete')); |
---|
57 | $core->addBehavior('adminBeforePageDelete',array('adminPollsFactory','adminBeforePostDelete')); |
---|
58 | // plugin gallery has no delete behavoir: used adminGalleriesActions |
---|
59 | |
---|
60 | # Extra poll |
---|
61 | $core->addBehavior('adminBeforePollsFactoryCreate',array('adminPollsFactory','adminBeforePollSave')); |
---|
62 | $core->addBehavior('adminBeforePollsFactoryUpdate',array('adminPollsFactory','adminBeforePollSave')); |
---|
63 | $core->addBehavior('adminBeforePollsFactoryDelete',array('adminPollsFactory','adminBeforePollDelete')); |
---|
64 | |
---|
65 | # Rest methods |
---|
66 | $core->rest->addFunction('getPollsOfPost',array('pollsFactoryRestMethods','getPollsOfPost')); |
---|
67 | $core->rest->addFunction('getPostsOfPoll',array('pollsFactoryRestMethods','getPostsOfPoll')); |
---|
68 | $core->rest->addFunction('getOtherPolls',array('pollsFactoryRestMethods','getOtherPolls')); |
---|
69 | $core->rest->addFunction('addPollPost',array('pollsFactoryRestMethods','addPollPost')); |
---|
70 | $core->rest->addFunction('removePollPost',array('pollsFactoryRestMethods','removePollPost')); |
---|
71 | |
---|
72 | # Admin posts/polls actions behaviors |
---|
73 | class adminPollsFactory |
---|
74 | { |
---|
75 | # Check extra field for post to poll conversion |
---|
76 | public static function adminBeforePollSave(&$cur,$post_id=null) |
---|
77 | { |
---|
78 | if ($cur->post_type != 'pollsfactory') return; |
---|
79 | |
---|
80 | if ($cur->post_dt === '') { |
---|
81 | $offset = dt::getTimeOffset($GLOBALS['core']->auth->getInfo('user_tz')); |
---|
82 | $now = time() + $offset; |
---|
83 | $cur->post_dt = date('Y-m-d H:i:00',$now); |
---|
84 | } |
---|
85 | if ($cur->post_content === '') { |
---|
86 | $cur->post_content = null; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | # Delete options related to a poll |
---|
91 | public static function adminBeforePollDelete($post_id) |
---|
92 | { |
---|
93 | global $core; |
---|
94 | $id = (integer) $post_id; |
---|
95 | |
---|
96 | $factory = new pollsFactory($core); |
---|
97 | $factory->delOption(null,'pollsquery',$id); |
---|
98 | $factory->delOption(null,'pollsselection',$id); |
---|
99 | $factory->delOption(null,'pollsresponse',$id); |
---|
100 | $factory->delOption(null,'pollspost',null,$id); |
---|
101 | } |
---|
102 | |
---|
103 | # List of polls link to a post in post page sidabar |
---|
104 | public static function adminPostFormSidebar($post) |
---|
105 | { |
---|
106 | # Always display something in order to place js features |
---|
107 | if ($post === null ) { |
---|
108 | echo '<div id="pollsfactory-form"></div>'; |
---|
109 | } |
---|
110 | else { |
---|
111 | global $core; |
---|
112 | |
---|
113 | $factory = new pollsFactory($core); |
---|
114 | $opts_params['option_type'] = 'pollspost'; |
---|
115 | $opts_params['post_id'] = $post->post_id; |
---|
116 | $opts = $factory->getOptions($opts_params); |
---|
117 | |
---|
118 | echo '<h3>'.__('Polls manager').'</h3><div id="pollsfactory-form">'; |
---|
119 | if (!$opts->isEmpty()) { |
---|
120 | echo '<ul>'; |
---|
121 | while($opts->fetch()) { |
---|
122 | $poll_params['no_content'] = true; |
---|
123 | $poll_params['post_type'] = 'pollsfactory'; |
---|
124 | $poll_params['post_id'] = $opts->option_meta; |
---|
125 | $poll_params['limit'] = 1; |
---|
126 | $poll = $core->blog->getPosts($poll_params); |
---|
127 | |
---|
128 | if (!$poll->isEmpty()) |
---|
129 | { |
---|
130 | echo |
---|
131 | '<li>'. |
---|
132 | form::hidden(array('oldpollspostlist[]'),$poll->post_id). |
---|
133 | '<label class="classic">'. |
---|
134 | form::checkbox(array('pollspostlist[]'),$poll->post_id,1,'',3).' '. |
---|
135 | $poll->post_title.'</label></li>'; |
---|
136 | } |
---|
137 | } |
---|
138 | echo '</ul>'; |
---|
139 | } |
---|
140 | echo |
---|
141 | '<p><a title="'.__('add polls').'" '. |
---|
142 | 'href="plugin.php?p=pollsFactory&tab=post&post_id='. |
---|
143 | $post->post_id.'">'.__('add polls').'</a></p></div>'; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | # JS of post page |
---|
148 | public static function adminPostHeaders() |
---|
149 | { |
---|
150 | return |
---|
151 | dcPage::jsLoad('index.php?pf=pollsFactory/js/_post.js'). |
---|
152 | "<script type=\"text/javascript\">\n//<![CDATA[\n". |
---|
153 | dcPage::jsVar('pollsFactoryPostEditor.prototype.poll_url','plugin.php?p=pollsFactory&tab=poll&id='). |
---|
154 | dcPage::jsVar('pollsFactoryPostEditor.prototype.text_title',__('Polls manager')). |
---|
155 | dcPage::jsVar('pollsFactoryPostEditor.prototype.text_confirm_remove_poll',__('Are you sure you want to remove this poll?')). |
---|
156 | dcPage::jsVar('pollsFactoryPostEditor.prototype.text_choose_poll',__('Choose from list')). |
---|
157 | dcPage::jsVar('pollsFactoryPostEditor.prototype.text_edit_poll',__('edit poll')). |
---|
158 | dcPage::jsVar('pollsFactoryPostEditor.prototype.text_add_poll',__('add poll')). |
---|
159 | dcPage::jsVar('pollsFactoryPostEditor.prototype.text_remove_poll',__('remove poll')). |
---|
160 | dcPage::jsVar('pollsFactoryPostEditor.prototype.text_no_poll',__('No more poll')). |
---|
161 | "\n//]]>\n". |
---|
162 | "</script>\n". |
---|
163 | '<link rel="stylesheet" type="text/css" href="index.php?pf=pollsFactory/style.css" />'; |
---|
164 | } |
---|
165 | |
---|
166 | # On new post create polls/post relation |
---|
167 | public static function adminAfterPostCreate($cur,$post_id) |
---|
168 | { |
---|
169 | if (empty($_POST['pollspostlist'])) return; |
---|
170 | |
---|
171 | global $core; |
---|
172 | $factory = new pollsFactory($core); |
---|
173 | |
---|
174 | $cur = $factory->open(); |
---|
175 | foreach($_POST['pollspostlist'] as $k => $poll_id) { |
---|
176 | $cur->clean(); |
---|
177 | $cur->option_type = 'pollspost'; |
---|
178 | $cur->post_id = $post_id; |
---|
179 | $cur->option_meta = $poll_id; |
---|
180 | $factory->addOption($cur); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | # If javascript is disabled, update polls/post relation |
---|
185 | public static function adminAfterPostUpdate($cur,$post_id) |
---|
186 | { |
---|
187 | if (empty($_POST['oldpollspostlist'])) return; |
---|
188 | $pollentries = !empty($_POST['pollspostlist']) ? $_POST['pollspostlist'] : array(); |
---|
189 | |
---|
190 | global $core; |
---|
191 | $factory = new pollsFactory($core); |
---|
192 | |
---|
193 | foreach($_POST['oldpollspostlist'] as $k => $poll_id) { |
---|
194 | if (!in_array($poll_id,$pollentries)) { |
---|
195 | $factory->delOption(null,'pollspost',$post_id,$poll_id); |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | # Delete relation between post and polls when a post is deleted |
---|
201 | public static function adminBeforePostDelete($post_id) |
---|
202 | { |
---|
203 | if ($post_id === null) return; |
---|
204 | |
---|
205 | global $core; |
---|
206 | |
---|
207 | $factory = new pollsFactory($core); |
---|
208 | $factory->delOption(null,'pollspost',$post_id); |
---|
209 | } |
---|
210 | |
---|
211 | # Actions can be made on posts list |
---|
212 | public static function adminPostsActionsCombo($args) |
---|
213 | { |
---|
214 | global $core; |
---|
215 | |
---|
216 | if ($core->auth->check('publish,contentadmin',$core->blog->id)) |
---|
217 | { |
---|
218 | $args[0][__('Polls manager')][__('Add polls')] = 'addpolls'; |
---|
219 | $args[0][__('Polls manager')][__('Remove polls')] = 'removepolls'; |
---|
220 | $args[0][__('Polls manager')][__('Open voting')] = 'openpolls'; |
---|
221 | $args[0][__('Polls manager')][__('Close voting')] = 'closepolls'; |
---|
222 | $args[0][__('Polls manager')][__('Publish')] = 'publishpolls'; |
---|
223 | $args[0][__('Polls manager')][__('Unpublish')] = 'unpublishpolls'; |
---|
224 | $args[0][__('Polls manager')][__('Mark as pending')] = 'pendingpolls'; |
---|
225 | } |
---|
226 | $args[0][__('Polls manager')][__('Mark as selected')] = 'selectedpolls'; |
---|
227 | $args[0][__('Polls manager')][__('Mark as unselected')] = 'unselectedpolls'; |
---|
228 | if ($core->auth->check('delete,contentadmin',$core->blog->id)) |
---|
229 | { |
---|
230 | $args[0][__('Polls manager')][__('Delete')] = 'deletepolls'; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | # Advanced option on posts list actions |
---|
235 | public static function adminPostsActionsContent($core,$action,$hidden_fields) |
---|
236 | { |
---|
237 | $entries = is_array($hidden_fields['entries']) ? $hidden_fields['entries'] : array(); |
---|
238 | $msg = $c = ''; |
---|
239 | $factory = new pollsFactory($core); |
---|
240 | |
---|
241 | switch($action) { |
---|
242 | |
---|
243 | # Add polls to selected entries |
---|
244 | case 'addpolls': |
---|
245 | |
---|
246 | $polls_params['post_type'] = 'pollsfactory'; |
---|
247 | $polls = $core->blog->getPosts($polls_params); |
---|
248 | |
---|
249 | while($polls->fetch()) |
---|
250 | { |
---|
251 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries[]'),$polls->post_id,0).' '.$polls->post_title.'</label></li>'; |
---|
252 | } |
---|
253 | if (empty($c)) { |
---|
254 | $msg = '<p>'.__('There is no polls').'</p>'; |
---|
255 | } |
---|
256 | else { |
---|
257 | $c = '<h2>'.__('add polls to selected entries').'</h2><ul>'.$c.'</ul>'; |
---|
258 | } |
---|
259 | break; |
---|
260 | |
---|
261 | # Remove polls from selected entries |
---|
262 | case 'removepolls': |
---|
263 | |
---|
264 | $rels_params = array(); |
---|
265 | $rels_params['option_type'] = 'pollspost'; |
---|
266 | $rels_params['post_id'] = $entries; |
---|
267 | $rels = $factory->getOptions($rels_params); |
---|
268 | |
---|
269 | while($rels->fetch()) |
---|
270 | { |
---|
271 | $post_params['post_id'] = $rels->post_id; |
---|
272 | $post_params['no_content'] = true; |
---|
273 | $post_params['limit'] = 1; |
---|
274 | $post = $core->blog->getPosts($post_params); |
---|
275 | |
---|
276 | $poll_params['post_type'] = 'pollsfactory'; |
---|
277 | $poll_params['post_id'] = $rels->option_meta; |
---|
278 | $poll_params['no_content'] = true; |
---|
279 | $poll_params['limit'] = 1; |
---|
280 | $poll = $core->blog->getPosts($poll_params); |
---|
281 | |
---|
282 | if (!$post->isEmpty() && !$poll->isEmpty()) { |
---|
283 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries['.$post->post_id.']'),$poll->post_id,0).' '.$post->post_title.' : '.$poll->post_title.'</label></li>'; |
---|
284 | } |
---|
285 | } |
---|
286 | if (empty($c)) { |
---|
287 | $msg = '<p>'.__('There is no poll for selected entries').'</p>'; |
---|
288 | } |
---|
289 | else { |
---|
290 | $c = '<h2>'.__('select polls to remove').'</h2><ul>'.$c.'</ul>'; |
---|
291 | } |
---|
292 | break; |
---|
293 | |
---|
294 | # Publish polls related to selected entries |
---|
295 | case 'publishpolls': |
---|
296 | |
---|
297 | $rels_params = array(); |
---|
298 | $rels_params['option_type'] = 'pollspost'; |
---|
299 | $rels_params['post_id'] = $entries; |
---|
300 | $rels_params['sql'] = 'GROUP BY option_meta '; |
---|
301 | $rels_params['order'] = 'option_meta ASC'; |
---|
302 | $rels = $factory->getOptions($rels_params); |
---|
303 | |
---|
304 | while($rels->fetch()) |
---|
305 | { |
---|
306 | $poll_params['post_type'] = 'pollsfactory'; |
---|
307 | $poll_params['post_id'] = $rels->option_meta; |
---|
308 | $poll_params['sql'] = "AND post_status != '1' "; |
---|
309 | $poll_params['no_content'] = true; |
---|
310 | $poll_params['limit'] = 1; |
---|
311 | $poll = $core->blog->getPosts($poll_params); |
---|
312 | |
---|
313 | if (!$poll->isEmpty()) { |
---|
314 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries[]'),$poll->post_id,0).' '.$poll->post_title.'</label></li>'; |
---|
315 | } |
---|
316 | } |
---|
317 | if (empty($c)) { |
---|
318 | $msg = '<p>'.__('There is no poll that can be published for selected entries').'</p>'; |
---|
319 | } |
---|
320 | else { |
---|
321 | $c = '<h2>'.__('select polls to publish').'</h2><ul>'.$c.'</ul>'; |
---|
322 | } |
---|
323 | break; |
---|
324 | |
---|
325 | # Unpublish polls related to selected entries |
---|
326 | case 'unpublishpolls': |
---|
327 | |
---|
328 | $rels_params = array(); |
---|
329 | $rels_params['option_type'] = 'pollspost'; |
---|
330 | $rels_params['post_id'] = $entries; |
---|
331 | $rels_params['sql'] = 'GROUP BY option_meta '; |
---|
332 | $rels_params['order'] = 'option_meta ASC'; |
---|
333 | $rels = $factory->getOptions($rels_params); |
---|
334 | |
---|
335 | while($rels->fetch()) |
---|
336 | { |
---|
337 | $poll_params['post_type'] = 'pollsfactory'; |
---|
338 | $poll_params['post_id'] = $rels->option_meta; |
---|
339 | $poll_params['sql'] = "AND post_status != '0' "; |
---|
340 | $poll_params['no_content'] = true; |
---|
341 | $poll_params['limit'] = 1; |
---|
342 | $poll = $core->blog->getPosts($poll_params); |
---|
343 | |
---|
344 | if (!$poll->isEmpty()) { |
---|
345 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries[]'),$poll->post_id,0).' '.$poll->post_title.'</label></li>'; |
---|
346 | } |
---|
347 | } |
---|
348 | if (empty($c)) { |
---|
349 | $msg = '<p>'.__('There is no poll that can be unpublished for selected entries').'</p>'; |
---|
350 | } |
---|
351 | else { |
---|
352 | $c = '<h2>'.__('select polls to unpublish').'</h2><ul>'.$c.'</ul>'; |
---|
353 | } |
---|
354 | break; |
---|
355 | |
---|
356 | # Mark as pending polls related to selected entries |
---|
357 | case 'pendingpolls': |
---|
358 | |
---|
359 | $rels_params = array(); |
---|
360 | $rels_params['option_type'] = 'pollspost'; |
---|
361 | $rels_params['post_id'] = $entries; |
---|
362 | $rels_params['sql'] = 'GROUP BY option_meta '; |
---|
363 | $rels_params['order'] = 'option_meta ASC'; |
---|
364 | $rels = $factory->getOptions($rels_params); |
---|
365 | |
---|
366 | while($rels->fetch()) |
---|
367 | { |
---|
368 | $poll_params['post_type'] = 'pollsfactory'; |
---|
369 | $poll_params['post_id'] = $rels->option_meta; |
---|
370 | $poll_params['sql'] = "AND post_status != '-2' "; |
---|
371 | $poll_params['no_content'] = true; |
---|
372 | $poll_params['limit'] = 1; |
---|
373 | $poll = $core->blog->getPosts($poll_params); |
---|
374 | |
---|
375 | if (!$poll->isEmpty()) { |
---|
376 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries[]'),$poll->post_id,0).' '.$poll->post_title.'</label></li>'; |
---|
377 | } |
---|
378 | } |
---|
379 | if (empty($c)) { |
---|
380 | $msg = '<p>'.__('There is no poll that can be marked as pending for selected entries').'</p>'; |
---|
381 | } |
---|
382 | else { |
---|
383 | $c = '<h2>'.__('select polls to mark as pending').'</h2><ul>'.$c.'</ul>'; |
---|
384 | } |
---|
385 | break; |
---|
386 | |
---|
387 | # Open polls related to selected entries |
---|
388 | case 'openpolls': |
---|
389 | |
---|
390 | $rels_params = array(); |
---|
391 | $rels_params['option_type'] = 'pollspost'; |
---|
392 | $rels_params['post_id'] = $entries; |
---|
393 | $rels_params['sql'] = 'GROUP BY option_meta '; |
---|
394 | $rels_params['order'] = 'option_meta ASC'; |
---|
395 | $rels = $factory->getOptions($rels_params); |
---|
396 | |
---|
397 | while($rels->fetch()) |
---|
398 | { |
---|
399 | $poll_params['post_type'] = 'pollsfactory'; |
---|
400 | $poll_params['post_id'] = $rels->option_meta; |
---|
401 | $poll_params['sql'] = "AND post_open_tb = 0 "; |
---|
402 | $poll_params['no_content'] = true; |
---|
403 | $poll_params['limit'] = 1; |
---|
404 | $poll = $core->blog->getPosts($poll_params); |
---|
405 | |
---|
406 | if (!$poll->isEmpty()) { |
---|
407 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries[]'),$poll->post_id,0).' '.$poll->post_title.'</label></li>'; |
---|
408 | } |
---|
409 | } |
---|
410 | if (empty($c)) { |
---|
411 | $msg = '<p>'.__('There is no poll that can be opened for selected entries').'</p>'; |
---|
412 | } |
---|
413 | else { |
---|
414 | $c = '<h2>'.__('select polls to open').'</h2><ul>'.$c.'</ul>'; |
---|
415 | } |
---|
416 | break; |
---|
417 | |
---|
418 | # Close polls related to selected entries |
---|
419 | case 'closepolls': |
---|
420 | |
---|
421 | $rels_params = array(); |
---|
422 | $rels_params['option_type'] = 'pollspost'; |
---|
423 | $rels_params['post_id'] = $entries; |
---|
424 | $rels_params['sql'] = 'GROUP BY option_meta '; |
---|
425 | $rels_params['order'] = 'option_meta ASC'; |
---|
426 | $rels = $factory->getOptions($rels_params); |
---|
427 | |
---|
428 | while($rels->fetch()) |
---|
429 | { |
---|
430 | $poll_params['post_type'] = 'pollsfactory'; |
---|
431 | $poll_params['post_id'] = $rels->option_meta; |
---|
432 | $poll_params['sql'] = "AND post_open_tb = 1 "; |
---|
433 | $poll_params['no_content'] = true; |
---|
434 | $poll_params['limit'] = 1; |
---|
435 | $poll = $core->blog->getPosts($poll_params); |
---|
436 | |
---|
437 | if (!$poll->isEmpty()) { |
---|
438 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries[]'),$poll->post_id,0).' '.$poll->post_title.'</label></li>'; |
---|
439 | } |
---|
440 | } |
---|
441 | if (empty($c)) { |
---|
442 | $msg = '<p>'.__('There is no poll that can be closed for selected entries').'</p>'; |
---|
443 | } |
---|
444 | else { |
---|
445 | $c = '<h2>'.__('select polls to close').'</h2><ul>'.$c.'</ul>'; |
---|
446 | } |
---|
447 | break; |
---|
448 | |
---|
449 | # Delete polls related to selected entries |
---|
450 | case 'deletepolls': |
---|
451 | |
---|
452 | $rels_params = array(); |
---|
453 | $rels_params['option_type'] = 'pollspost'; |
---|
454 | $rels_params['post_id'] = $entries; |
---|
455 | $rels_params['sql'] = 'GROUP BY option_meta '; |
---|
456 | $rels_params['order'] = 'option_meta ASC'; |
---|
457 | $rels = $factory->getOptions($rels_params); |
---|
458 | |
---|
459 | while($rels->fetch()) |
---|
460 | { |
---|
461 | $poll_params['post_type'] = 'pollsfactory'; |
---|
462 | $poll_params['post_id'] = $rels->option_meta; |
---|
463 | $poll_params['no_content'] = true; |
---|
464 | $poll_params['limit'] = 1; |
---|
465 | $poll = $core->blog->getPosts($poll_params); |
---|
466 | |
---|
467 | if (!$poll->isEmpty()) { |
---|
468 | $c .= '<li><label class="classic">'.form::checkbox(array('pollentries[]'),$poll->post_id,0).' '.$poll->post_title.'</label></li>'; |
---|
469 | } |
---|
470 | } |
---|
471 | if (empty($c)) { |
---|
472 | $msg = '<p>'.__('There is no poll on selected entries').'</p>'; |
---|
473 | } |
---|
474 | else { |
---|
475 | $c = '<h2>'.__('select polls to delete').'</h2><ul>'.$c.'</ul>'; |
---|
476 | } |
---|
477 | break; |
---|
478 | |
---|
479 | } |
---|
480 | |
---|
481 | if (empty($msg) && !empty($c)) { |
---|
482 | echo |
---|
483 | '<form method="post" action="posts_actions.php">'. |
---|
484 | $c. |
---|
485 | '<p>'. |
---|
486 | $hidden_fields. |
---|
487 | $core->formNonce(). |
---|
488 | form::hidden(array('action'),$action). |
---|
489 | '<input type="submit" value="'.__('save').'" /></p>'. |
---|
490 | '</form>'; |
---|
491 | } |
---|
492 | elseif(!empty($msg)) { |
---|
493 | echo $msg; |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | # Do actions on posts list actions |
---|
498 | public static function adminPostsActions($core,$posts,$action,$redir) |
---|
499 | { |
---|
500 | //special for gallery delete |
---|
501 | if ($action == 'delete' && !$posts->isEmpty()) { |
---|
502 | try { |
---|
503 | while($posts->fetch()) |
---|
504 | { |
---|
505 | if ($posts->post_type == 'gal') { |
---|
506 | self::adminBeforePollDelete($posts->post_id); |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | catch (Exception $e) { |
---|
511 | $core->error->add($e->getMessage()); |
---|
512 | } |
---|
513 | return; |
---|
514 | } |
---|
515 | |
---|
516 | $pollentries = isset($_POST['pollentries']) && is_array($_POST['pollentries']) ? $_POST['pollentries'] : array(); |
---|
517 | foreach ($pollentries as $k => $id) { |
---|
518 | $pollentries[(integer) $k] = (integer) $id; |
---|
519 | } |
---|
520 | |
---|
521 | if (empty($pollentries)) return; |
---|
522 | |
---|
523 | try { |
---|
524 | $factory = new pollsFactory($core); |
---|
525 | |
---|
526 | switch($action) { |
---|
527 | |
---|
528 | # Add polls to selected entries |
---|
529 | case 'addpolls': |
---|
530 | while ($posts->fetch()) { |
---|
531 | |
---|
532 | # Add relations selected polls to entries |
---|
533 | $cur = $factory->open(); |
---|
534 | foreach($pollentries as $k => $id) { |
---|
535 | |
---|
536 | # First delete relations between post and polls if exists |
---|
537 | $factory->delOption(null,'pollspost',$posts->post_id,$id); |
---|
538 | |
---|
539 | $cur->clean(); |
---|
540 | $cur->option_type = 'pollspost'; |
---|
541 | $cur->post_id = $posts->post_id; |
---|
542 | $cur->option_meta = $id; |
---|
543 | $factory->addOption($cur); |
---|
544 | } |
---|
545 | } |
---|
546 | http::redirect($redir); |
---|
547 | break; |
---|
548 | |
---|
549 | # Remove selected polls from selected entries |
---|
550 | case 'removepolls': |
---|
551 | foreach($pollentries as $k => $id) { |
---|
552 | $factory->delOption(null,'pollspost',$k,$id); |
---|
553 | } |
---|
554 | http::redirect($redir); |
---|
555 | break; |
---|
556 | |
---|
557 | # Opened selected polls |
---|
558 | case 'openpolls': |
---|
559 | foreach($pollentries as $k => $id) { |
---|
560 | $factory->updPostOpened($id,1); |
---|
561 | } |
---|
562 | http::redirect($redir); |
---|
563 | break; |
---|
564 | |
---|
565 | # Closed selected polls |
---|
566 | case 'closepolls': |
---|
567 | foreach($pollentries as $k => $id) { |
---|
568 | $factory->updPostOpened($id,0); |
---|
569 | } |
---|
570 | http::redirect($redir); |
---|
571 | break; |
---|
572 | |
---|
573 | # Published selected polls |
---|
574 | case 'publishpolls': |
---|
575 | foreach($pollentries as $k => $id) { |
---|
576 | $core->blog->updPostStatus($id,1); |
---|
577 | } |
---|
578 | http::redirect($redir); |
---|
579 | break; |
---|
580 | |
---|
581 | # Unpublished selected polls |
---|
582 | case 'unpublishpolls': |
---|
583 | foreach($pollentries as $k => $id) { |
---|
584 | $core->blog->updPostStatus($id,0); |
---|
585 | } |
---|
586 | http::redirect($redir); |
---|
587 | break; |
---|
588 | |
---|
589 | # Marked as pending selected polls |
---|
590 | case 'pendingpolls': |
---|
591 | foreach($pollentries as $k => $id) { |
---|
592 | $core->blog->updPostStatus($id,-2); |
---|
593 | } |
---|
594 | http::redirect($redir); |
---|
595 | break; |
---|
596 | } |
---|
597 | } |
---|
598 | catch (Exception $e) { |
---|
599 | $core->error->add($e->getMessage()); |
---|
600 | } |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | # Admin rest methods |
---|
605 | class pollsFactoryRestMethods |
---|
606 | { |
---|
607 | # Get polls that is not link to a post |
---|
608 | public static function getOtherPolls() |
---|
609 | { |
---|
610 | global $core; |
---|
611 | |
---|
612 | $rsp = new xmlTag('polls'); |
---|
613 | $polls_params['sql'] = ''; |
---|
614 | $polls_params['post_type'] = 'pollsfactory'; |
---|
615 | |
---|
616 | # can set it to 0 to have all polls (when post was not created) |
---|
617 | $id = isset($_GET['id']) ? $_GET['id'] : 0; |
---|
618 | $id = abs((integer) $id); |
---|
619 | |
---|
620 | # Get existing posts |
---|
621 | if (0 < $id) |
---|
622 | { |
---|
623 | $factory = new pollsFactory($core); |
---|
624 | $opt_params['option_type'] = 'pollspost'; |
---|
625 | $opt_params['post_id'] = $id; |
---|
626 | $opts = $factory->getOptions($opt_params); |
---|
627 | while($opts->fetch()) |
---|
628 | { |
---|
629 | $polls_params['sql'] .= 'AND P.post_id != '.((integer) $opts->option_meta).' '; |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | # Get polls |
---|
634 | $polls = $core->blog->getPosts($polls_params); |
---|
635 | |
---|
636 | if ($polls->isEmpty()) return $rsp; |
---|
637 | |
---|
638 | while($polls->fetch()) |
---|
639 | { |
---|
640 | $xp = new xmlTag('poll'); |
---|
641 | $xp->id = $polls->post_id; |
---|
642 | $xp->CDATA($polls->post_title); |
---|
643 | $rsp->insertNode($xp); |
---|
644 | } |
---|
645 | |
---|
646 | return $rsp; |
---|
647 | } |
---|
648 | |
---|
649 | # Get lists of polls related to a post |
---|
650 | public static function getPollsOfPost() |
---|
651 | { |
---|
652 | global $core; |
---|
653 | |
---|
654 | $id = isset($_GET['id']) ? $_GET['id'] : 0; |
---|
655 | $id = abs((integer) $id); |
---|
656 | |
---|
657 | if (1 > $id) { |
---|
658 | throw new Exception(__('No ID given')); |
---|
659 | } |
---|
660 | |
---|
661 | $rsp = new xmlTag('polls'); |
---|
662 | $factory = new pollsFactory($core); |
---|
663 | |
---|
664 | $polls_params['option_type'] = 'pollspost'; |
---|
665 | $polls_params['post_id'] = $id; |
---|
666 | $polls = $factory->getOptions($polls_params); |
---|
667 | |
---|
668 | if ($polls->isEmpty()) return $rsp; |
---|
669 | |
---|
670 | while($polls->fetch()) |
---|
671 | { |
---|
672 | $poll_params['no_content'] = true; |
---|
673 | $poll_params['post_type'] = 'pollsfactory'; |
---|
674 | $poll_params['post_id'] = $polls->option_meta; |
---|
675 | $poll_params['limit'] = 1; |
---|
676 | $poll = $core->blog->getPosts($poll_params); |
---|
677 | |
---|
678 | if (!$poll->isEmpty()) |
---|
679 | { |
---|
680 | $xp = new xmlTag('poll'); |
---|
681 | $xp->id = $poll->post_id; |
---|
682 | $xp->CDATA($poll->post_title); |
---|
683 | $rsp->insertNode($xp); |
---|
684 | } |
---|
685 | } |
---|
686 | |
---|
687 | return $rsp; |
---|
688 | } |
---|
689 | |
---|
690 | # Get list of posts related to a poll |
---|
691 | public static function getPostsOfPoll() |
---|
692 | { |
---|
693 | global $core; |
---|
694 | |
---|
695 | $id = isset($_GET['id']) ? $_GET['id'] : 0; |
---|
696 | $id = abs((integer) $id); |
---|
697 | |
---|
698 | if (1 > $id) { |
---|
699 | throw new Exception(__('No ID given')); |
---|
700 | } |
---|
701 | |
---|
702 | $rsp = new xmlTag('polls'); |
---|
703 | $factory = new pollsFactory($core); |
---|
704 | |
---|
705 | $posts_params['option_type'] = 'pollspost'; |
---|
706 | $posts_params['option_meta'] = $id; |
---|
707 | $posts = $factory->getOptions($posts_params); |
---|
708 | |
---|
709 | if ($posts->isEmpty()) return $rsp; |
---|
710 | |
---|
711 | while ($posts->fetch()) |
---|
712 | { |
---|
713 | $post_params['no_content'] = true; |
---|
714 | $post_params['post_id'] = $posts->post_id; |
---|
715 | $post_params['post_type'] = ''; |
---|
716 | $post_params['limit'] = 1; |
---|
717 | $post = $core->blog->getPosts($post_params); |
---|
718 | |
---|
719 | if (!$post->isEmpty()) |
---|
720 | { |
---|
721 | $xp = new xmlTag('post'); |
---|
722 | $xp->id = $post->post_id; |
---|
723 | $xp->url = $core->getPostAdminURL($post->post_type,$post->post_id); |
---|
724 | $xp->type = $post->post_type; |
---|
725 | $xp->CDATA($post->post_title); |
---|
726 | $rsp->insertNode($xp); |
---|
727 | } |
---|
728 | } |
---|
729 | |
---|
730 | return $rsp; |
---|
731 | } |
---|
732 | |
---|
733 | # Remove relation between a poll and a post |
---|
734 | public static function removePollPost() |
---|
735 | { |
---|
736 | global $core; |
---|
737 | |
---|
738 | $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : 0; |
---|
739 | $post_id = abs((integer) $post_id); |
---|
740 | $poll_id = isset($_POST['poll_id']) ? $_POST['poll_id'] : 0; |
---|
741 | $poll_id = abs((integer) $poll_id); |
---|
742 | |
---|
743 | if (1 > $post_id || 1 > $poll_id) { |
---|
744 | throw new Exception(__('No ID given')); |
---|
745 | } |
---|
746 | |
---|
747 | $rsp = new xmlTag('rsp'); |
---|
748 | $factory = new pollsFactory($core); |
---|
749 | |
---|
750 | $opt_params['option_type'] = 'pollspost'; |
---|
751 | $opt_params['post_id'] = $post_id; |
---|
752 | $opt_params['option_meta'] = $poll_id; |
---|
753 | |
---|
754 | $opts = $factory->getOptions($opt_params); |
---|
755 | |
---|
756 | if ($opts->isEmpty()) { |
---|
757 | throw new Exception(__('No such relation')); |
---|
758 | } |
---|
759 | $factory->delOption($opts->option_id); |
---|
760 | |
---|
761 | return $rsp; |
---|
762 | } |
---|
763 | |
---|
764 | # Add relation between a poll and a post |
---|
765 | public static function addPollPost() |
---|
766 | { |
---|
767 | global $core; |
---|
768 | |
---|
769 | $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : 0; |
---|
770 | $post_id = abs((integer) $post_id); |
---|
771 | $poll_id = isset($_POST['poll_id']) ? $_POST['poll_id'] : 0; |
---|
772 | $poll_id = abs((integer) $poll_id); |
---|
773 | |
---|
774 | if (1 > $post_id || 1 > $poll_id) { |
---|
775 | throw new Exception(__('No ID given')); |
---|
776 | } |
---|
777 | |
---|
778 | $rsp = new xmlTag('rsp'); |
---|
779 | |
---|
780 | $factory = new pollsFactory($core); |
---|
781 | $cur = $factory->open(); |
---|
782 | $cur->post_id = $post_id; |
---|
783 | $cur->option_type = 'pollspost'; |
---|
784 | $cur->option_meta = $poll_id; |
---|
785 | $factory->addOption($cur); |
---|
786 | |
---|
787 | return $rsp; |
---|
788 | } |
---|
789 | } |
---|
790 | ?> |
---|