Dotclear

source: plugins/pollsFactory/inc/index.polls.php @ 2338

Revision 2338, 12.5 KB checked in by JcDenis, 13 years ago (diff)

pollsFactory 1.3

  • Switched to DC 2.2
  • Fixed minor bugs
Line 
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
13if (!defined('DC_CONTEXT_ADMIN')){return;}
14
15# Polls lists
16class adminPollList extends adminGenericList
17{
18     public function pollDisplay($page,$nb_per_page,$enclose_block='')
19     {
20          $echo = '';
21          if ($this->rs->isEmpty())
22          {
23               $echo .= '<p><strong>'.__('No poll').'</strong></p>';
24          }
25          else
26          {
27               $this->factory = new pollsFactory($this->core);
28
29               $pager = new pager($page,$this->rs_count,$nb_per_page,10);
30               $pager->html_prev = $this->html_prev;
31               $pager->html_next = $this->html_next;
32               $pager->var_page = 'page';
33
34               $html_block =
35               '<table class="clear">'.
36               '<tr>'.
37               '<th colspan="2">'.__('Title').'</th>'.
38               '<th>'.__('Date').'</th>'.
39               '<th>'.__('Author').'</th>'.
40               '<th class="nowrap">'.__('Related posts').'</th>'.
41               '<th>'.__('Votes').'</th>'.
42               '<th>'.__('Status').'</th>'.
43               '<tr>'.
44               '</tr>%s</table>';
45
46               if ($enclose_block) {
47                    $html_block = sprintf($enclose_block,$html_block);
48               }
49
50               $echo .= '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
51
52               $blocks = explode('%s',$html_block);
53
54               $echo .= $blocks[0];
55
56               while ($this->rs->fetch())
57               {
58                    $echo .= $this->pollLine();
59               }
60
61               $echo .= $blocks[1];
62
63               $echo .= '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
64          }
65          return $echo;
66     }
67
68     private function pollLine()
69     {
70          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
71          $p_img = '<img alt="%1$s" title="%1$s" src="index.php?pf=pollsFactory/inc/img/%2$s" />';
72
73          switch ($this->rs->post_status) {
74               case 1:
75                    $status = sprintf($img,__('published'),'check-on.png');
76                    break;
77               case 0:
78                    $status = sprintf($img,__('unpublished'),'check-off.png');
79                    break;
80               case -1:
81                    $status = sprintf($img,__('scheduled'),'scheduled.png');
82                    break;
83               case -2:
84                    $status = sprintf($img,__('pending'),'check-wrn.png');
85                    break;
86          }
87
88          $opened = !$this->rs->post_open_tb ? 
89               sprintf($img,__('closed'),'locker.png') : 
90               '';
91
92          $selected = $this->rs->post_selected ? 
93               sprintf($img,__('selected'),'selected.png') : 
94               '';
95
96          $style = $this->rs->post_status < 1 || !$this->rs->post_open_tb ?
97               ' offline' : 
98               '';
99
100          $results = $this->factory->countVotes($this->rs->post_id);
101         
102          $rel_params['option_type'] = 'pollspost';
103          $rel_params['option_meta'] = $this->rs->post_id;
104          $related = $this->factory->getOptions($rel_params,true)->f(0);
105
106          $res = 
107          '<tr class="line'.$style.'">'.
108          '<td class="nowrap">'.form::checkbox(array('entries[]'),$this->rs->post_id).' </td>'.
109          '<td class="maximal"><a href="plugin.php?p=pollsFactory&amp;tab=poll&amp;id='.$this->rs->post_id.'" title="'.__('Edit poll').'">'.
110          html::escapeHTML($this->rs->post_title).'</a></td>'.
111          '<td class="nowrap">'.dt::dt2str(__('%Y-%m-%d %H:%M'),$this->rs->post_dt,$this->rs->core->auth->getInfo('user_tz')).'</td>'.
112          '<td class="nowrap">'.$this->rs->user_id.'</td>'.
113          '<td class="nowrap">'.$related.'</td>'.
114          '<td class="nowrap status">';
115          if ($results) {
116               $res .= 
117               '<a href="plugin.php?p=pollsFactory&amp;tab=result&amp;poll_id='.$this->rs->post_id.'" title="'.__('View results of this poll').'">'.
118               '<img src="index.php?pf=pollsFactory/inc/img/icon-result.png" alt="'.__('View results of this poll').'" /></a>';
119          }
120          $res .= '&nbsp;'.$results.
121          '</td>'.
122          '<td class="nowrap status">'.$status.' '.$opened.' '.$selected.'</td>'.
123          '</tr>';
124
125          return $res;
126     }
127}
128# Actions
129if (!empty($action) && !empty($_POST['entries']))
130{
131     if (isset($_POST['redir']) && strpos($_POST['redir'],'://') === false) {
132          $redir = sprint($_POST['redir'],$action);
133     }
134     else {
135          $redir = $p_url.'&tab=polls&msg='.$action;
136     }
137
138     foreach ($_POST['entries'] as $k => $v) {
139          $entries[$k] = (integer) $v;
140     }
141     
142     $params['sql'] = 'AND P.post_id IN('.implode(',',$entries).') ';
143     $params['post_type'] = 'pollsfactory';
144     $params['no_content'] = true;
145
146     if (isset($_POST['post_type'])) {
147          $params['post_type'] = $_POST['post_type'];
148     }
149     
150     $posts = $core->blog->getPosts($params);
151     
152     # --BEHAVIOR-- adminPollsFactoryActions
153     $core->callBehavior('adminPollsFactoryActions',$core,$posts,$action,$redir);
154     
155     if (preg_match('/^(publish|unpublish|schedule|pending)$/',$action))
156     {
157          switch ($action) {
158               case 'unpublish' : $status = 0; break;
159               case 'pending' : $status = -2; break;
160               case 'schedule' : $status = -1; break;
161               default : $status = 1; break;
162          }
163         
164          try
165          {
166               while ($posts->fetch()) {
167                    $core->blog->updPostStatus($posts->post_id,$status);
168               }
169               http::redirect($redir);
170          }
171          catch (Exception $e)
172          {
173               $core->error->add($e->getMessage());
174          }
175     }
176     elseif ($action == 'selected' || $action == 'unselected')
177     {
178          try
179          {
180               while ($posts->fetch()) {
181                    $core->blog->updPostSelected($posts->post_id,$action == 'selected');
182               }
183               http::redirect($redir);
184          }
185          catch (Exception $e)
186          {
187               $core->error->add($e->getMessage());
188          }
189     }
190     elseif ($action == 'open' || $action == 'close')
191     {
192          try
193          {
194               while ($posts->fetch()) {
195                    $factory->updPostOpened($posts->post_id,$action == 'open');
196               }
197               http::redirect($redir);
198          }
199          catch (Exception $e)
200          {
201               $core->error->add($e->getMessage());
202          }
203     }
204     elseif ($action == 'delete')
205     {
206          try
207          {
208               while ($posts->fetch()) {
209                    if ($posts->isDeletable()) {
210
211                         # --BEHAVIOR-- adminBeforePollsFactoryDelete
212                         $core->callBehavior('adminBeforePollsFactoryDelete',$posts->post_id);               
213
214                         $core->blog->delPost($posts->post_id);
215                    }
216               }
217               http::redirect($redir);
218          }
219          catch (Exception $e)
220          {
221               $core->error->add($e->getMessage());
222          }
223     }
224}
225
226
227# Getting authors
228try {
229     $users = $core->blog->getPostsUsers('pollsfactory');
230} catch (Exception $e) {
231     $core->error->add($e->getMessage());
232}
233
234# Getting langs
235try {
236     $langs = $core->blog->getLangs(array('post_type'=>'pollsfactory'));
237} catch (Exception $e) {
238     $core->error->add($e->getMessage());
239}
240
241#Combos
242$users_combo = $categories_combo = array();
243$users_combo['-'] = $categories_combo['-'] = '';
244while ($users->fetch())
245{
246     $user_cn = dcUtils::getUserCN($users->user_id,$users->user_name,
247     $users->user_firstname,$users->user_displayname);
248     
249     if ($user_cn != $users->user_id) {
250          $user_cn .= ' ('.$users->user_id.')';
251     }
252     
253     $users_combo[$user_cn] = $users->user_id; 
254}
255
256$lang_combo['-'] = '';
257while ($langs->fetch()) {
258     $lang_combo[$langs->post_lang] = $langs->post_lang;
259}
260
261$status_combo = array('-' => '');
262foreach ($core->blog->getAllPostStatus() as $k => $v) {
263     $status_combo[$v] = (string) $k;
264}
265$selected_combo = array(
266     '-' => '',
267     __('selected') => '1',
268     __('not selected') => '0'
269);
270$opened_combo = array(
271     '-' => '',
272     __('opened') => '1',
273     __('closed') => '0'
274);
275$sortby_combo = array(
276     __('Date') => 'post_dt',
277     __('Title') => 'post_title',
278     __('Author') => 'user_id',
279     __('Status') => 'post_status',
280     __('Selected') => 'post_selected'
281);
282$order_combo = array(
283     __('Descending') => 'desc',
284     __('Ascending') => 'asc'
285);
286
287$combo_action = array();
288if ($core->auth->check('publish,contentadmin',$core->blog->id))
289{
290     $combo_action[__('Status')][__('Publish')] = 'publish';
291     $combo_action[__('Status')][__('Unpublish')] = 'unpublish';
292     $combo_action[__('Status')][__('Schedule')] = 'schedule';
293     $combo_action[__('Status')][__('Mark as pending')] = 'pending';
294     $combo_action[__('Votes')][__('Open voting')] = 'open';
295     $combo_action[__('Votes')][__('Close voting')] = 'close';
296}
297$combo_action[__('Mark')][__('Mark as selected')] = 'selected';
298$combo_action[__('Mark')][__('Mark as unselected')] = 'unselected';
299if ($core->auth->check('delete,contentadmin',$core->blog->id))
300{
301     $combo_action[__('Delete')][__('Delete')] = 'delete';
302}
303
304# Filters
305$user_id = !empty($_GET['user_id']) ?   $_GET['user_id'] : '';
306$lang = !empty($_GET['lang']) ?         $_GET['lang'] : '';
307$status = isset($_GET['status']) ? $_GET['status'] : '';
308$selected = isset($_GET['selected']) ?  $_GET['selected'] : '';
309$opened = isset($_GET['opened']) ? $_GET['opened'] : '';
310$sortby = !empty($_GET['sortby']) ?     $_GET['sortby'] : 'post_dt';
311$order = !empty($_GET['order']) ?       $_GET['order'] : 'desc';
312
313$page = !empty($_GET['page']) ? (integer) $_GET['page'] : 1;
314$nb_per_page =  30;
315
316if (!empty($_GET['nb']) && (integer) $_GET['nb'] > 0) {
317     if ($nb_per_page != $_GET['nb']) {
318          $show_filters = true;
319     }
320     $nb_per_page = (integer) $_GET['nb'];
321}
322
323$params['post_type'] = 'pollsfactory';
324$params['limit'] = array((($page-1)*$nb_per_page),$nb_per_page);
325
326# - User filter
327if ($user_id !== '' && in_array($user_id,$users_combo)) {
328     $params['user_id'] = $user_id;
329     $show_filters = true;
330}
331# - Lang filter
332if ($lang !== '' && in_array($lang,$lang_combo)) {
333     $params['post_lang'] = $lang;
334     $show_filters = true;
335}
336# - Status filter
337if ($status !== '' && in_array($status,$status_combo)) {
338     $params['post_status'] = $status;
339     $show_filters = true;
340}
341# - Selected filter
342if ($selected !== '' && in_array($selected,$selected_combo)) {
343     $params['post_selected'] = $selected;
344     $show_filters = true;
345}
346# - Opened filter
347if ($opened !== '' && in_array($opened,$opened_combo)) {
348     $params['sql'] = 'AND post_open_tb = '.$opened.' ';
349     $show_filters = true;
350}
351# - Sortby and order filter
352if ($sortby !== '' && in_array($sortby,$sortby_combo)) {
353     if ($order !== '' && in_array($order,$order_combo)) {
354          $params['order'] = $sortby.' '.$order;
355     }
356     
357     if ($sortby != 'post_dt' || $order != 'desc') {
358          $show_filters = true;
359     }
360}
361
362# Get polls
363try {
364     $polls = $core->blog->getPosts($params);
365     $counter = $core->blog->getPosts($params,true);
366     $polls_list = new adminPollList($core,$polls,$counter->f(0));
367} catch (Exception $e) {
368     $core->error->add($e->getMessage());
369}
370
371
372/*
373 * Display
374 */
375
376if (!$show_filters) {
377     $header .= dcPage::jsLoad('js/filter-controls.js');
378}
379
380echo '
381<html>
382<head><title>'.__('Polls manager').'</title>'.$header.'</head>
383<body>'.$msg.'
384<h2>'.html::escapeHTML($core->blog->name).
385' &rsaquo; '.__('Polls').
386' - <a class="button" href="'.$p_url.'&amp;tab=poll">'.__('New poll').'</a>'.
387'</h2>';
388
389if (!$show_filters) {
390     echo '<p><a id="filter-control" class="form-control" href="#">'.
391     __('Filters').'</a></p>';
392}
393
394echo 
395'<form action="'.$p_url.'&amp;tab=polls" method="get" id="filters-form">'.
396'<fieldset><legend>'.__('Filters').'</legend>'.
397'<div class="three-cols">'.
398'<div class="col">'.
399'<label>'.__('Status:').
400form::combo('status',$status_combo,$status).'</label> '.
401'<label>'.__('Selected:').
402form::combo('selected',$selected_combo,$selected).'</label> '.
403'<label>'.__('Opened:').
404form::combo('opened',$opened_combo,$opened).'</label> '.
405'</div>'.
406
407'<div class="col">'.
408'<label>'.__('Author:').
409form::combo('user_id',$users_combo,$user_id).'</label> '.
410'<label>'.__('Lang:').
411form::combo('lang',$lang_combo,$lang).'</label> '.
412'</div>'.
413
414'<div class="col">'.
415'<p><label>'.__('Order by:').
416form::combo('sortby',$sortby_combo,$sortby).'</label> '.
417'<label>'.__('Sort:').
418form::combo('order',$order_combo,$order).'</label></p>'.
419'<p><label class="classic">'.form::field('nb',3,3,$nb_per_page).' '.
420__('Polls per page').'</label> '.
421'<input type="submit" value="'.__('filter').'" />'.
422form::hidden(array('p'),'pollsFactory').
423form::hidden(array('tab'),'polls').
424'</p>'.
425'</div>'.
426'</div>'.
427'<br class="clear" />'. //Opera sucks
428'</fieldset>'.
429'</form>'.
430$polls_list->pollDisplay($page,$nb_per_page,
431     '<form action="plugin.php" method="post" id="form-entries">'.
432     '%s'.
433     '<div class="two-cols">'.
434     '<p class="col checkboxes-helpers"></p>'.
435     '<p class="col right">'.__('Selected polls action:').' '.
436     form::combo('action',$combo_action).
437     '<input type="submit" value="'.__('ok').'" /></p>'.
438     form::hidden(array('user_id'),$user_id).
439     form::hidden(array('lang'),$lang).
440     form::hidden(array('status'),$status).
441     form::hidden(array('selected'),$selected).
442     form::hidden(array('sortby'),$sortby).
443     form::hidden(array('order'),$order).
444     form::hidden(array('page'),$page).
445     form::hidden(array('nb'),$nb_per_page).
446     form::hidden(array('p'),'pollsFactory').
447     form::hidden(array('tab'),'polls').
448     $core->formNonce().
449     '</div>'.
450     '</form>'
451);
452
453dcPage::helpBlock('pollsFactory');
454echo $footer.'</body></html>';
455?>
Note: See TracBrowser for help on using the repository browser.

Sites map