Dotclear

source: plugins/pollsFactory/inc/index.content.php @ 2139

Revision 2139, 17.7 KB checked in by JcDenis, 14 years ago (diff)

pollsFactory 1.0:

  • Rewrited plugin
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$poll_id = isset($_REQUEST['poll_id']) ? (integer) $_REQUEST['poll_id'] : -1;
16if (empty($poll_id)) $poll_id = -1;
17$has_poll = 0 < $poll_id;
18
19$query_id = isset($_REQUEST['query_id']) ? (integer) $_REQUEST['query_id'] : -1;
20if (empty($query_id)) $query_id = -1;
21$has_query = 0 < $query_id;
22
23$selection_id = isset($_REQUEST['selection_id']) ? (integer) $_REQUEST['selection_id'] : -1;
24if (empty($selection_id)) $selection_id = -1;
25$has_selection = 0 < $selection_id;
26
27$query_title = isset($_POST['query_title']) ? $_POST['query_title'] : '';
28$new_query_desc = isset($_POST['new_query_desc']) ? $_POST['new_query_desc'] : '';
29$edit_query_desc = isset($_POST['edit_query_desc']) ? $_POST['edit_query_desc'] : '';
30$query_type = isset($_POST['query_type']) ? $_POST['query_type'] : '';
31
32$section = isset($_REQUEST['section']) ? $_REQUEST['section'] : '';
33
34$action_redir = $p_url.'&tab=content&poll_id=%s&query_id=%s&selection_id=%s&msg='.$action.'&section=%s';
35
36if (!$has_poll) {
37     $core->error->add('No such poll ID');
38}
39
40# Get poll
41$poll_params = array();
42$poll_params['post_type'] = 'pollsfactory';
43$poll_params['post_id'] = $poll_id;
44$poll = $core->blog->getPosts($poll_params);
45if ($poll->isEmpty()) { $poll_id = -1; $has_poll = false; }
46
47# Get old queries
48$queries_params['option_type'] = 'pollsquery';
49$queries_params['post_id'] = $poll_id;
50$queries_params['order'] = 'option_position ASC ';
51$old_queries = $factory->getOptions($queries_params);
52$has_old_queries = !$old_queries->isEmpty();
53
54# Delete old queries
55if ($has_old_queries && $action == 'deletequery' 
56 && !empty($_POST['query_del']) && is_array($_POST['query_del']))
57{
58     try {
59          foreach($_POST['query_del'] as $id)
60          {
61               # Delete selections
62               $del_selections_params['option_type'] = 'pollsselection';
63               $del_selections_params['post_id'] = $poll_id;
64               $del_selections_params['option_meta'] = $id;
65               $del_selections = $factory->getOptions($del_selections_params);
66               while($del_selections->fetch()) {
67                    $factory->delOption($del_selections->option_id);
68               }
69               # Delete responses
70               $del_responses_params['option_type'] = 'pollsresponse';
71               $del_responses_params['post_id'] = $poll_id;
72               $del_responses_params['option_meta'] = $id;
73               $del_responses = $factory->getOptions($del_responses_params);
74               while($del_responses->fetch()) {
75                    $factory->delOption($del_responses->option_id);
76               }
77               # Delete query
78               $factory->delOption($id);
79          }
80
81          http::redirect(sprintf($action_redir,$poll_id,'','','query-list'));
82     }
83     catch (Exception $e) {
84          $core->error->add($e->getMessage());
85     }
86}
87
88# Reorder old queries
89if ($has_old_queries && $action == 'reorderquery')
90{
91     $order = array();
92     # Whitout js
93     if (empty($_REQUEST['queries_order_js']) && !empty($_REQUEST['queries_order_html'])) {
94          $order = $_REQUEST['queries_order_html'];
95          asort($order);
96          $order = array_keys($order);
97     }
98     # With js
99     elseif (!empty($_REQUEST['queries_order_js'])) {
100          $order = explode(',',$_REQUEST['queries_order_js']);
101     }
102     # There is something to sort
103     if (!empty($order)) {
104          # Arrange order
105          $new_order = array();
106          foreach ($order as $pos => $id) {
107               $pos = ((integer) $pos)+1;
108               if (!empty($pos) && !empty($id)) {
109                    $new_order[$pos] = $id;
110               }
111          }
112          try {
113               # Update order
114               foreach($new_order as $pos => $id) {
115                    $factory->updOptionPosition($id,$pos);
116               }
117
118               http::redirect(sprintf($action_redir,$poll_id,'','','query-list'));
119          }
120          catch (Exception $e) {
121               $core->error->add($e->getMessage());
122          }
123     }
124}
125
126# Create or edit query
127if ($action == 'createquery' && $has_poll && !$has_query 
128 || $action == 'editquery' && $has_poll && $has_query)
129{
130     $query_title = isset($_POST['query_title']) ? $_POST['query_title'] : '';
131     $query_type = isset($_POST['query_type']) ? $_POST['query_type'] : '';
132
133     try {
134          if (empty($query_title)) {
135               throw new Exception(__('You must specify query title'));
136          }
137
138          $cur = $factory->open();
139          $cur->post_id = $poll->post_id;
140          $cur->option_title = $query_title;
141
142          if ($action == 'createquery') {
143
144               if (empty($query_type)) {
145                    throw new Exception(__('You must specify query type'));
146               }
147               $new_query_desc = isset($_POST['new_query_desc']) ? $_POST['new_query_desc'] : '';
148
149               $cur->option_type = 'pollsquery';
150               $cur->option_lang = $poll->post_lang;
151               $cur->option_meta = $query_type;
152               $cur->option_content = $new_query_desc;
153               $cur->option_position = $factory->nextPosition($cur->post_id,null,$cur->option_type);
154
155               $query_id = $factory->addOption($cur);
156
157               if (in_array($query_type,array('field','textarea'))) {
158                    $cur->clean();
159                    $cur->post_id = $poll->post_id;
160                    $cur->option_type = 'pollsselection';
161                    $cur->option_lang = $poll->post_lang;
162                    $cur->option_meta = $query_id;
163                    $cur->option_title = '-';
164                    $cur->option_position = $factory->nextPosition($cur->post_id,$cur->option_meta,$cur->option_type);
165
166                    $factory->addOption($cur);
167               }
168          }
169          else {
170               $edit_query_desc = isset($_POST['edit_query_desc']) ? $_POST['edit_query_desc'] : '';
171               $cur->option_content = $edit_query_desc;
172               
173               $factory->updOption($query_id,$cur);
174          }
175
176          http::redirect(sprintf($action_redir,$poll_id,'','','query-list'));
177     }
178     catch (Exception $e) {
179          $core->error->add($e->getMessage());
180     }
181}
182
183# Get query
184$query_params = $queries_params;
185$query_params['option_id'] = $query_id;
186$query = $factory->getOptions($query_params);
187if ($query->isEmpty()) {
188     $query_id = -1;
189     $has_query = false;
190}
191else {
192     $query_id = $query->option_id;
193     $has_query = true;
194}
195
196# get old selections
197$selections_params['option_type'] = 'pollsselection';
198$selections_params['post_id'] = $poll_id;
199$selections_params['option_meta'] = $query_id;
200$selections_params['order'] = 'option_position ASC ';
201$old_selections = $factory->getOptions($selections_params);
202$has_old_selections = !$old_selections->isEmpty();
203
204# Delete old selections
205if ($has_old_selections && $action == 'deleteselection' 
206 && !empty($_POST['selection_del']) && is_array($_POST['selection_del']))
207{
208     try {
209          foreach($_POST['selection_del'] as $id)
210          {
211               $factory->delOption($id);
212          }
213
214          http::redirect(sprintf($action_redir,$poll_id,$query_id,'','selection-list'));
215     }
216     catch (Exception $e) {
217          $core->error->add($e->getMessage());
218     }
219}
220
221# Reorder old selections
222if ($has_old_selections && $action == 'reorderselection')
223{
224     $order = array();
225     # Whitout js
226     if (empty($_REQUEST['selections_order_js']) && !empty($_REQUEST['selections_order_html'])) {
227          $order = $_REQUEST['selections_order_html'];
228          asort($order);
229          $order = array_keys($order);
230     }
231     # With js
232     elseif (!empty($_REQUEST['selections_order_js'])) {
233          $order = explode(',',$_REQUEST['selections_order_js']);
234     }
235     # There is something to sort
236     if (!empty($order)) {
237          # Arrange order
238          $new_order = array();
239          foreach ($order as $pos => $id) {
240               $pos = ((integer) $pos)+1;
241               if (!empty($pos) && !empty($id)) {
242                    $new_order[$pos] = $id;
243               }
244          }
245          try {
246               # Update order
247               foreach($new_order as $pos => $id) {
248                    $factory->updOptionPosition($id,$pos);
249               }
250
251               http::redirect(sprintf($action_redir,$poll_id,$query_id,'','selection-list'));
252          }
253          catch (Exception $e) {
254               $core->error->add($e->getMessage());
255          }
256     }
257}
258
259# Create or edit selection
260if ($action == 'createselection' && $has_poll && $has_query && !$has_selection
261 || $action == 'editselection' && $has_poll && $has_query && $has_selection)
262{
263     $selection_text = isset($_POST['selection_text']) ? $_POST['selection_text'] : '';
264     
265     try {
266          if (empty($selection_text)) {
267               throw new Exception(__('You must specify selection text'));
268          }
269
270          $cur = $factory->open();
271               $cur->post_id = $poll->post_id;
272          $cur->option_title = $selection_text;
273
274          if ($action == 'createselection') {
275               $cur->option_type = 'pollsselection';
276               $cur->option_lang = $poll->post_lang;
277               $cur->option_meta = $query_id;
278               $cur->option_position = $factory->nextPosition($cur->post_id,$cur->option_meta,$cur->option_type);
279               $selection_id = $factory->addOption($cur);
280          }
281          else {
282               $factory->updOption($selection_id,$cur);
283          }
284
285          http::redirect(sprintf($action_redir,$poll_id,$query_id,'','selection-list'));
286     }
287     catch (Exception $e) {
288          $core->error->add($e->getMessage());
289     }
290}
291
292# Get selection
293$selection_params = $selections_params;
294$selection_params['option_id'] = $selection_id;
295$selection = $factory->getOptions($selection_params);
296if ($selection->isEmpty()) {
297     $selection_id = -1;
298     $has_selection = false;
299}
300else {
301     $selection_id = $selection->option_id;
302     $has_selection = true;
303}
304
305# Remove entries from poll
306if ($action == 'removeentries' && !empty($_POST['entries']) && $has_poll)
307{
308     try {
309          foreach($_POST['entries'] as $k => $id)
310          {
311               $factory->delPost($id,$poll_id);
312          }
313          http::redirect(sprintf($action_redir,$poll_id,$query_id,$selection_id,$section));
314     }
315     catch (Exception $e) {
316          $core->error->add($e->getMessage());
317     }
318}
319
320
321/*
322 * Display
323 */
324
325echo '<html>
326<head><title>'.__('Polls manager').'</title>'.$header.
327dcPage::jsModal().
328dcPage::jsToolMan().
329dcPage::jsToolBar().
330dcPage::jsDatePicker().
331dcPage::jsModal().
332dcPage::jsLoad('index.php?pf=pollsFactory/js/content.js').
333"<script type=\"text/javascript\">\n//<![CDATA[\n".
334dcPage::jsVar('jcToolsBox.prototype.section',$section).
335"\n//]]>\n</script>\n".
336'</head>
337<body>'.$msg.'
338<h2>'.html::escapeHTML($core->blog->name).
339' &rsaquo; <a href="'.$p_url.'&amp;tab=polls">'.__('Polls').'</a>'.
340' &rsaquo; '.__('Edit content');
341
342if ($has_poll) {
343     $preview_url =
344     $core->blog->url.$core->url->getBase('pollsFactoryPagePreview').'/'.
345     $core->auth->userID().'/'.
346     http::browserUID(DC_MASTER_KEY.$core->auth->userID().$core->auth->getInfo('user_pwd')).
347     '/'.$poll->post_url;
348     echo ' - <a id="poll-preview" href="'.$preview_url.'" class="button nowait">'.__('Preview poll').'</a>';
349}
350
351echo 
352' - <a class="button" href="'.$p_url.'&amp;tab=poll">'.__('New poll').'</a>'.
353'</h2>';
354
355if ($has_poll) {
356     echo '<p><a href="'.$core->getPostAdminURL($poll->post_type,$poll->post_id).'">&#171; '.
357          sprintf(__('Back to "%s"'),html::escapeHTML($poll->post_title)).'</a></p>';
358}
359
360echo
361'<h3>'.html::escapeHTML($poll->post_title);
362if ($has_query) { echo ' &rsaquo; '.html::escapeHTML($query->option_title); }
363if ($has_selection) { echo ' &rsaquo; '.html::escapeHTML($selection->option_title); }
364echo '</h3><div id="poll-content">';
365
366# List old queries
367if ($has_old_queries)
368{
369     $lis = '';
370     $i = 1;
371     while ($old_queries->fetch())
372     {
373          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" /> %3$s';
374          $query_count = $factory->getOptions(array('option_type'=>'pollsselection','option_meta'=>$old_queries->option_id),true)->f(0);
375          $query_complete = $query_count ? 
376               sprintf($img,__('complete'),'check-on.png',$query_count) :
377               sprintf($img,__('uncomplete'),'check-off.png',$query_count);
378         
379          $lis .=
380          '<tr class="line" id="l_'.$old_queries->option_id.'">'.
381          '<td class="handle minimal">'.form::field(array('queries_order_html['.$old_queries->option_id.']'),2,5,$old_queries->option_position,'',2).'</td>'.
382          '<td class="minimal">
383          '.form::checkbox(array('query_del[]'),$old_queries->option_id,0,'',2).'
384          </td>
385          <td>'.$old_queries->option_position.' <a href="'.sprintf($action_redir,$poll_id,$old_queries->option_id,'','query-edit').'" title="'.__('Edit query').'">'.html::escapeHTML($old_queries->option_title).'</a></td>
386          <td>'.array_search($old_queries->option_meta,$factory->getQueryTypes()).' </td>
387          <td>'.$query_complete.'</td>
388          </tr>';
389          $i++;
390     }
391     echo addpollform(
392          'query-list',__('Queries'),$poll_id,$query_id,$selection_id,
393          __('Action on existing queries:'),array(
394               __('reorder queries') => 'reorderquery',
395               __('delete queries') => 'deletequery'
396          ),
397//        ($has_query ? '<p><a href="'.sprintf($action_redir,$poll_id,'','','query-new').'" title="'.__('New query').'">'.__('New query').'</a></p>' : '').
398          '<table class="maximal dragable">'.
399          '<thead><tr>'.
400          '<th colspan="3">'.__('Query').form::hidden('queries_order_js','').'</th>'.
401          '<th>'.__('Type').'</th>'.
402          '<th>'.__('selections').'</th>'.
403          '</tr></thead>'.
404          '<tbody id="queries-list">'.
405          $lis.
406          '</tbody>'.
407          '</table>',
408          2
409     );
410}
411
412# New query
413if ($has_poll)//show all// && !$has_query)
414{
415     echo addpollform(
416          'query-new',__('New query'),$poll_id,'','',
417          __('save'),'createquery','
418          <p><label class="required" for="query_title">'.__('Title:').'</label>'.
419          form::field('query_title',20,255,html::escapeHTML($query_title),'maximal',2).'</p>
420          <p class="area" id="new-query-area"><label for="new_query_desc">'.__('Description:').'</label>'.
421          form::textarea(array('new_query_desc','query_desc'),50,5,html::escapeHTML($new_query_desc),'maximal '.$poll->post_format,2).'</p>
422          <p class="area"><label for="query_type">'.__('Type:').'</label>'.
423          form::combo('query_type',$factory->getQueryTypes(),$query_type,'',2).'</p>',
424          2
425     );
426}
427# Edit query
428if ($has_poll && $has_query)
429{
430     echo addpollform(
431          'query-edit',__('Edit query'),$poll_id,$query_id,'',
432          __('edit'),'editquery','
433          <p><label class="required" for="query_title">'.__('Title:').'</label>'.
434          form::field('query_title',20,255,html::escapeHTML($query->option_title),'maximal',2).form::hidden(array('query_id'),$query->option_id).'</p>
435          <p class="area" id="edit-query-area"><label for="edit_query_desc">'.__('Description:').'</label>'.
436          form::textarea(array('edit_query_desc','query_desc'),50,5,html::escapeHTML($query->option_content),'maximal '.$poll->post_format,2).'</p>
437          <p class="area"><label for="query_type">'.__('Type:').'</label>'.
438          form::combo('query_type_disabled',$factory->getQueryTypes(),$query->option_meta,'',2,true).'</p>',
439          2
440     );
441}
442
443# List old selections
444if ($has_old_selections && !in_array($query->option_meta,array('field','textarea')))
445{
446     $lis = '';
447     $i = 1;
448     while ($old_selections->fetch())
449     {
450          $lis .=
451          '<tr class="line" id="l_'.$old_selections->option_id.'">'.
452          '<td class="handle minimal">'.form::field(array('selections_order_html['.$old_selections->option_id.']'),2,5,$old_selections->option_position,'',2).'</td>'.
453          '<td class="minimal">
454          '.form::checkbox(array('selection_del[]'),$old_selections->option_id,0,'',2).'
455          </td>
456          <td>'.$old_selections->option_position.' <a href="'.sprintf($action_redir,$poll_id,$query_id,$old_selections->option_id,'selection-edit').'" title="'.__('Edit this selection').'">'.html::escapeHTML($old_selections->option_title).'</a></td>
457          </tr>';
458          $i++;
459     }
460     echo addpollform(
461          'selection-list',__('selections'),$poll_id,$query_id,$selection_id,
462          __('Action on existing selections:'),array(
463               __('reorder selections') => 'reorderselection',
464               __('delete selections') => 'deleteselection'
465          ),
466//        ($has_selection ? '<p><a href="'.sprintf($action_redir,$poll_id,$query_id,'','selection-new').'" title="'.__('New selection').'">'.__('New selection').'</a></p>' : '').
467          '<table class="maximal dragable">'.
468          '<thead><tr>'.
469          '<th colspan="3">'.__('selection').form::hidden('selections_order_js','').'</th>'.
470          '</tr></thead>'.
471          '<tbody id="selections-list">'.
472          $lis.
473          '</tbody>'.
474          '</table>',
475          2
476     );
477}
478
479# New selection
480if ($has_query && !in_array($query->option_meta,array('field','textarea')))//show all// && !$has_selection)
481{
482     echo addpollform(
483          'selection-new',__('New selection'),$poll_id,$query_id,$selection_id,
484          __('save'),'createselection','
485          <p><label for="_selection_text">'.__('Text:').'</label>'.
486          form::field('selection_text',20,255,'','maximal',2).'</p>',
487          2
488     );
489}
490
491# Edit selection
492if ($has_query && $has_selection && !in_array($query->option_meta,array('field','textarea')))
493{
494     echo addpollform(
495          'selection-edit',__('Edit selection'),$poll_id,$query_id,$selection_id,
496          __('edit'),'editselection','
497          <p><label for="_selection_text">'.__('Text:').'</label>'.
498          form::field('selection_text',20,255,html::escapeHTML($selection->option_title),'maximal',2).'</p>',
499          2
500     );
501}
502echo '</div>'.dcPage::helpBlock('pollsFactory').$footer.'</body></html>';
503
504function addpollForm($section,$title,$poll_id,$query_id,$selection_id,$submit,$action,$content,$tabindex=2,$accesskey='',$sidebar=false)
505{
506
507     if ($sidebar) {
508          $h = $title ? '<h3 id="'.$section.'">'.$title.'</h3>%s' : '%s';
509     }
510     else {
511          $id = !is_array($action) ? ' id="'.$action.'" ' : '';
512          $h = $title ? '<fieldset id="'.$section.'"><legend'.$id.'>'.$title.'</legend>%s</fieldset>' : '%s';
513     }
514     $a = $accesskey ? ' accesskey="'.$accesskey.'"' : '';
515     $r = 
516     '<form method="post" action="plugin.php">'.
517     $content.
518     '<div class="two-cols">';
519     
520     if (is_array($action)) {
521          $r .= 
522          '<p class="col checkboxes-helpers"></p>'.
523          '<p class="col right">'.$submit.' '.
524          form::combo('action',$action,'',$tabindex).
525          '<input type="submit"'.$a.' value="'.__('ok').'" tabindex="'.$tabindex.'"/></p>';
526     }
527     else {
528          $r .= 
529          '<p class="col"><input type="submit" name="save"'.$a.' value="'.$submit.'" tabindex="'.$tabindex.'" />'.
530          form::hidden(array('action'),$action);
531     }
532     $r .=
533     $GLOBALS['core']->formNonce().
534     form::hidden(array('poll_id'),$poll_id).
535     form::hidden(array('query_id'),$query_id).
536     form::hidden(array('selection_id'),$selection_id).
537     form::hidden(array('p'),'pollsFactory').
538     form::hidden(array('tab'),'content').
539     form::hidden(array('section'),$section).'
540     </div>
541     </form>';
542
543     return sprintf($h,$r);
544}
545?>
Note: See TracBrowser for help on using the repository browser.

Sites map