Dotclear

source: plugins/myGmaps/addmap.php @ 2824

Revision 2824, 16.2 KB checked in by philippe, 13 years ago (diff)

myGmaps : now works with pages

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of myGmaps, a plugin for Dotclear 2.
5#
6# Copyright (c) 2010 Philippe aka amalgame
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK ------------------------------------
12
13require_once DC_ROOT.'/inc/admin/prepend.php';
14
15dcPage::check('usage,contentadmin');
16
17global $core;
18
19$p_url    = 'plugin.php?p='.basename(dirname(__FILE__));
20$default_tab = isset($_GET['tab']) ? $_GET['tab'] : 'entries-list';
21
22$s =& $core->blog->settings->myGmaps;
23
24$myGmaps_center = $s->myGmaps_center;
25$myGmaps_zoom = $s->myGmaps_zoom;
26$myGmaps_type = $s->myGmaps_type;
27
28$page_title = __('Add a map to entry');
29
30# Getting categories
31try {
32     $categories = $core->blog->getCategories(array('post_type'=>'map'));
33} catch (Exception $e) {
34     $core->error->add($e->getMessage());
35}
36
37# Getting authors
38try {
39     $users = $core->blog->getPostsUsers();
40} catch (Exception $e) {
41     $core->error->add($e->getMessage());
42}
43
44# Getting dates
45try {
46     $dates = $core->blog->getDates(array('type'=>'month','post_type'=>'map'));
47} catch (Exception $e) {
48     $core->error->add($e->getMessage());
49}
50
51# Creating filter combo boxes
52if (!$core->error->flag())
53{
54     # Filter form we'll put in html_block
55     $users_combo = $categories_combo = array();
56     $users_combo['-'] = $categories_combo['-'] = '';
57     while ($users->fetch())
58     {
59          $user_cn = dcUtils::getUserCN($users->user_id,$users->user_name,
60          $users->user_firstname,$users->user_displayname);
61         
62          if ($user_cn != $users->user_id) {
63               $user_cn .= ' ('.$users->user_id.')';
64          }
65         
66          $users_combo[$user_cn] = $users->user_id; 
67     }
68     
69     $categories_combo[__('None')] = 'NULL';
70     while ($categories->fetch()) {
71          $categories_combo[str_repeat('&nbsp;&nbsp;',$categories->level-1).'&bull; '.
72               html::escapeHTML($categories->cat_title).
73               ' ('.$categories->nb_post.')'] = $categories->cat_id;
74     }
75     
76     $status_combo = array(
77     '-' => '',
78     __('pending') => '-2',
79     __('online') => '1'
80     );
81     
82     $post_maps_combo = array(
83     '-' => '',
84     __('none') => 'none',
85     __('point of interest') => 'point of interest',
86     __('polyline') => 'polyline',
87     __('included kml file') => 'included kml file'
88     );
89     
90     # Months array
91     $dt_m_combo['-'] = '';
92     while ($dates->fetch()) {
93          $dt_m_combo[dt::str('%B %Y',$dates->ts())] = $dates->year().$dates->month();
94     }
95     
96     $sortby_combo = array(
97     __('Date') => 'post_dt',
98     __('Title') => 'post_title',
99     __('Category') => 'cat_title',
100     __('Author') => 'user_id',
101     __('Status') => 'post_status'
102     );
103     
104     $order_combo = array(
105     __('Descending') => 'desc',
106     __('Ascending') => 'asc'
107     );
108}
109
110# Actions combo box
111$combo_action = array();
112if ($core->auth->check('publish,contentadmin',$core->blog->id))
113{
114     $combo_action[__('Status')] = array(
115          __('Publish') => 'publish',
116          __('Mark as pending') => 'pending'
117     );
118}
119
120$combo_action[__('Change')] = array(__('Change category') => 'category');
121if ($core->auth->check('admin',$core->blog->id))
122{
123     $combo_action[__('Change')] = array_merge($combo_action[__('Change')],
124          array(__('Change author') => 'author'));
125}
126if ($core->auth->check('delete,contentadmin',$core->blog->id))
127{
128     $combo_action[__('Delete')] = array(__('Delete') => 'delete');
129}
130
131/* Get posts
132-------------------------------------------------------- */ 
133
134$post_id = !empty($_GET['post_id']) ?   $_GET['post_id'] : '';
135$post_type = !empty($_GET['post_type']) ?    $_GET['post_type'] : '';
136$user_id = !empty($_GET['user_id']) ?   $_GET['user_id'] : '';
137$cat_id = !empty($_GET['cat_id']) ?     $_GET['cat_id'] : '';
138$status = isset($_GET['status']) ? $_GET['status'] : '';
139$month = !empty($_GET['month']) ?       $_GET['month'] : '';
140$post_maps = !empty($_GET['post_maps']) ?         $_GET['post_maps'] : '';
141$sortby = !empty($_GET['sortby']) ?     $_GET['sortby'] : 'post_dt';
142$order = !empty($_GET['order']) ? $_GET['order'] : 'desc';
143
144$show_filters = false;
145
146$page = !empty($_GET['page']) ? (integer) $_GET['page'] : 1;
147$nb_per_page =  30;
148
149if (!empty($_GET['nb']) && (integer) $_GET['nb'] > 0) {
150     if ($nb_per_page != $_GET['nb']) {
151          $show_filters = true;
152     }
153     $nb_per_page = (integer) $_GET['nb'];
154}
155
156if ($post_id != '') {
157     $meta =& $GLOBALS['core']->meta;
158     $my_params['post_id'] = $_GET['post_id'];
159     $my_params['no_content'] = true;
160     $my_params['post_type'] = array('post','page');
161                   
162     $rs = $core->blog->getPosts($my_params);
163     while ($rs->fetch()) {
164          $my_post_maps = $meta->getMetaStr($rs->post_meta,'map');
165         
166     }
167     if ($rs->post_type == 'page') {
168          $page_title = __('Add a map to page');
169     }
170     $my_post_maps_options = $meta->getMetaStr($rs->post_meta,'map_options');
171     
172     if ($my_post_maps_options) {
173          $map_options = explode(",",$my_post_maps_options);
174          $myGmaps_center = $map_options[0].','.$map_options[1];
175          $myGmaps_zoom = $map_options[2];
176          $myGmaps_type = $map_options[3];
177     }
178     
179     if ($my_post_maps !='') {
180          $maps_array = explode(",",$my_post_maps);
181          $has_map = true;
182          $page_title = __('Edit map');
183     }
184}
185
186$params['limit'] = array((($page-1)*$nb_per_page),$nb_per_page);
187$params['no_content'] = true;
188$params['post_type'] = 'map';
189$params['post_status'] = '1';
190
191# - User filter
192if ($user_id !== '' && in_array($user_id,$users_combo)) {
193     $params['user_id'] = $user_id;
194     $show_filters = true;
195}
196
197# - Categories filter
198if ($cat_id !== '' && in_array($cat_id,$categories_combo)) {
199     $params['cat_id'] = $cat_id;
200     $show_filters = true;
201}
202
203# - Month filter
204if ($month !== '' && in_array($month,$dt_m_combo)) {
205     $params['post_month'] = substr($month,4,2);
206     $params['post_year'] = substr($month,0,4);
207     $show_filters = true;
208}
209
210# - Map type filter
211if ($post_maps != '' && in_array($post_maps,$post_maps_combo)) {
212     $params['sql'] .= "AND post_meta LIKE '%".$post_maps."%' ";
213     $show_filters = true;
214}
215
216# - Sortby and order filter
217if ($sortby !== '' && in_array($sortby,$sortby_combo)) {
218     if ($order !== '' && in_array($order,$order_combo)) {
219          $params['order'] = $sortby.' '.$order;
220     }
221     
222     if ($sortby != 'post_dt' || $order != 'desc') {
223          $show_filters = true;
224     }
225}
226/* Pager class
227-------------------------------------------------------- */
228class adminGmapList extends adminGenericList
229{
230     public function display($page,$nb_per_page,$enclose_block='')
231     {
232          if ($this->rs->isEmpty())
233          {
234               echo '<p><strong>'.__('No element').'</strong></p>';
235          }
236          else
237          {
238               $pager = new pager($page,$this->rs_count,$nb_per_page,10);
239               $pager->html_prev = $this->html_prev;
240               $pager->html_next = $this->html_next;
241               $pager->var_page = 'page';
242
243               $html_block =
244               '<table class="clear"><tr>'.
245               '<th colspan="2">'.__('Title').'</th>'.
246               '<th>'.__('Date').'</th>'.
247               '<th>'.__('Category').'</th>'.
248               '<th>'.__('Author').'</th>'.
249               '<th class="nowrap">'.__('Map element type').'</th>'.
250               '</tr>%s</table>';
251
252               if ($enclose_block) {
253                    $html_block = sprintf($enclose_block,$html_block);
254               }
255
256               echo '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
257
258               $blocks = explode('%s',$html_block);
259
260               echo $blocks[0];
261               
262               global $core;
263               $meta =& $GLOBALS['core']->meta;
264               $my_params['post_id'] = $_GET['post_id'];
265               $my_params['no_content'] = true;
266               $my_params['post_type'] = array('post','page');
267                             
268               $rs = $core->blog->getPosts($my_params);
269               while ($rs->fetch()) {
270                    $my_post_maps = $meta->getMetaStr($rs->post_meta,'map');
271               }             
272               if ($my_post_maps !='') {
273                    $maps_array = explode(",",$my_post_maps);
274               }
275
276               while ($this->rs->fetch())
277               {
278                    if (!isset($maps_array) || !in_array($this->rs->post_id,$maps_array)) {
279                         echo $this->postLine();
280                    }
281               }
282               
283               echo $blocks[1];
284
285               echo '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
286          }
287     }
288
289     private function postLine()
290     {
291               if ($this->core->auth->check('categories',$this->core->blog->id)) {
292                    $cat_link = '<a href="category.php?id=%s">%s</a>';
293               } else {
294                    $cat_link = '%2$s';
295               }
296               $p_url    = 'plugin.php?p='.basename(dirname(__FILE__));
297               $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
298               switch ($this->rs->post_status) {
299                    case 1:
300                         $img_status = sprintf($img,__('published'),'check-on.png');
301                         break;
302                    case -2:
303                         $img_status = sprintf($img,__('pending'),'check-wrn.png');
304                         break;
305               }
306               if ($this->rs->cat_title) {
307                    $cat_title = sprintf($cat_link,$this->rs->cat_id,
308                    html::escapeHTML($this->rs->cat_title));
309               } else {
310                    $cat_title = __('None');
311               }
312     
313               $res = '<tr class="line'.($this->rs->post_status != 1 ? ' offline' : '').'"'.
314               ' id="p'.$this->rs->post_id.'">';
315               
316               $meta =& $GLOBALS['core']->meta;
317               $meta_rs = $meta->getMetaStr($this->rs->post_meta,'map');
318               
319               $res .=
320               '<td class="nowrap">'.
321               form::checkbox(array('entries[]'),$this->rs->post_id,'','','',!$this->rs->isEditable()).'</td>'.
322               '<td class="maximal"><a href="plugin.php?p=myGmaps&amp;do=edit&amp;id='.$this->rs->post_id.'" title="'.__('Edit map element').' : '.html::escapeHTML($this->rs->post_title).'">'.html::escapeHTML($this->rs->post_title).'</a></td>'.
323               '<td class="nowrap">'.dt::dt2str(__('%Y-%m-%d %H:%M'),$this->rs->post_dt).'</td>'.
324               '<td class="nowrap">'.$cat_title.'</td>'.
325               '<td class="nowrap">'.$this->rs->user_id.'</td>'.
326               '<td class="nowrap">'.__($meta_rs).'</td>'.
327               '</tr>';
328         
329          return $res;
330     }
331}
332
333# Get posts
334try {
335     $posts = $core->blog->getPosts($params);
336     $counter = $core->blog->getPosts($params,true);
337     $post_list = new adminGmapList($core,$posts,$counter->f(0));
338} catch (Exception $e) {
339     $core->error->add($e->getMessage());
340}
341
342# Save activation
343
344if (isset($_POST['addmap'])) {
345  try {
346    $entries = $_POST['entries'];
347     $post_id = $_POST['post_id'];
348     $myGmaps_center = $_POST['myGmaps_center'];
349     $myGmaps_zoom = $_POST['myGmaps_zoom'];
350     $myGmaps_type = $_POST['myGmaps_type'];
351     $meta =& $GLOBALS['core']->meta;
352     $meta->delPostMeta($post_id,'map');
353     $meta->delPostMeta($post_id,'map_options');
354     
355     $entries = implode(',',$entries);
356     foreach ($meta->splitMetaValues($entries) as $tag) {
357          $meta->setPostMeta($post_id,'map',$tag);
358     }
359     $map_options = $myGmaps_center.','.$myGmaps_zoom.','.$myGmaps_type;
360     $meta->setPostMeta($post_id,'map_options',$map_options);
361     
362     if ($_POST['post_type'] == 'page') {
363          http::redirect('plugin.php?p=pages&act=page&id='.$post_id);
364     } else {
365          http::redirect(DC_ADMIN_URL.'post.php?id='.$post_id);
366     }
367     
368     
369  } catch (Exception $e) {
370    $core->error->add($e->getMessage());
371  }
372 
373} elseif (isset($_POST['updmap'])) {
374  try {
375   
376     $post_id = $_POST['post_id'];
377     $myGmaps_center = $_POST['myGmaps_center_upd'];
378     $myGmaps_zoom = $_POST['myGmaps_zoom_upd'];
379     $myGmaps_type = $_POST['myGmaps_type_upd'];
380     $meta =& $GLOBALS['core']->meta;
381     
382     $meta->delPostMeta($post_id,'map_options');
383     
384     $map_options = $myGmaps_center.','.$myGmaps_zoom.','.$myGmaps_type;
385     $meta->setPostMeta($post_id,'map_options',$map_options);
386     
387     if ($_POST['post_type'] == 'page') {
388          http::redirect('plugin.php?p=pages&act=page&id='.$post_id);
389     } else {
390          http::redirect(DC_ADMIN_URL.'post.php?id='.$post_id);
391     }
392     
393  } catch (Exception $e) {
394    $core->error->add($e->getMessage());
395  }
396 
397}
398
399/* DISPLAY
400-------------------------------------------------------- */
401?>
402<html>
403     <head>
404          <title><?php echo $page_title; ?></title>
405          <?php
406          echo
407          dcPage::jsToolMan().
408          dcPage::jsPageTabs($default_tab).
409          dcPage::jsLoad(DC_ADMIN_URL.'?pf=myGmaps/js/_maps_list.js').
410          dcPage::jsLoad(DC_ADMIN_URL.'?pf=myGmaps/js/_addmap_map.js');
411          ?>
412        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
413         
414          <style type="text/css">
415        #map_canvas{height:400px;padding:3px;border:1px solid #999999;margin:-10px 0 1px 0; }
416        </style>
417     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
418     <body class="popup">
419<?php
420
421if (!$core->error->flag())
422{
423     
424     echo '<h2>'.html::escapeHTML($core->blog->name).' &rsaquo; '.__('Google Maps').' &rsaquo; '.$page_title.'</h2>';
425     //
426     echo '<div class="multi-part" id="entries-list" title="'.__('Add elements').'">';
427     
428     if (!$show_filters) {
429          echo 
430          dcPage::jsLoad('js/filter-controls.js').
431          '<p><a id="filter-control" class="form-control" href="#">'.
432          __('Filters').'</a></p>';
433     }
434     
435     echo
436     '<form action="'.$p_url.'" method="get" id="filters-form">'.
437     '<fieldset><legend>'.__('Filters').'</legend>'.
438     '<div class="three-cols">'.
439     '<div class="col">'.
440     '<label>'.__('Author:').
441     form::combo('user_id',$users_combo,$user_id).'</label> '.
442     '<label>'.__('Category:').
443     form::combo('cat_id',$categories_combo,$cat_id).'</label> '.
444     '<label>'.__('Status:').
445     form::combo('status',$status_combo,$status).'</label> '.
446     '</div>'.
447     
448     '<div class="col">'.
449     '<label>'.__('Month:').
450     form::combo('month',$dt_m_combo,$month).'</label> '.
451     '<label>'.__('Map element type:').
452     form::combo('post_maps',$post_maps_combo,$post_maps).'</label> '.
453     '</div>'.
454     
455     '<div class="col">'.
456     '<p><label>'.__('Order by:').
457     form::combo('sortby',$sortby_combo,$sortby).'</label> '.
458     '<label>'.__('Sort:').
459     form::combo('order',$order_combo,$order).'</label></p>'.
460     '<p><label class="classic">'. form::field('nb',3,3,$nb_per_page).' '.
461     __('Maps per page').'</label> '.
462     $core->formNonce().
463     '<input type="submit" value="'.__('filter').'" /></p>'.
464     '<p>'.form::hidden(array('add_map_filters'),'myGmaps').
465     form::hidden(array('post_id'),$post_id).
466     form::hidden(array('p'),'myGmaps').'</p>'.
467     '</div>'.
468     '</div>'.
469     '<br class="clear" />'. //Opera sucks
470     '</fieldset>'.
471     '</form>';
472     
473     # Show posts
474     $post_list->display($page,$nb_per_page,
475     '<form action="'.$p_url.'" method="post" id="form-entries">'.
476     
477     '%s'.
478     
479     '<div class="two-cols">'.
480     '<p class="col checkboxes-helpers"></p>'.
481     
482     '<p class="col right">'.__('Add selected map elements').' '.
483     
484     '<input type="submit" name="addmap" value="'.__('ok').'" /></p>'.
485     '<p>'.form::hidden(array('post_id'),$post_id).
486     form::hidden(array('post_type'),$post_type).
487     form::hidden(array('user_id'),$user_id).
488     form::hidden(array('cat_id'),$cat_id).
489     form::hidden(array('status'),$status).
490     form::hidden(array('month'),$month).
491     form::hidden(array('sortby'),$sortby).
492     form::hidden(array('order'),$order).
493     form::hidden(array('page'),$page).
494     form::hidden(array('nb'),$nb_per_page).
495     $core->formNonce().'</p>'.
496     '</div>'
497     );
498     
499     if (isset($maps_array)) {
500          for ($i = 0; $i < sizeof($maps_array); ++$i) {
501               echo '<p style="display:none">'.form::checkbox(array('entries[]'),$maps_array[$i],'true','','','').'</p>';
502          }
503     }
504     
505     echo
506     '<p><input type="hidden" name="myGmaps_center" id="myGmaps_center" value="'.$myGmaps_center.'" />'.
507     '<input type="hidden" name="myGmaps_zoom" id="myGmaps_zoom" value="'.$myGmaps_zoom.'" />'.
508     '<input type="hidden" name="myGmaps_type" id="myGmaps_type" value="'.$myGmaps_type.'" /></p>'.
509     '</form>';
510     
511     echo '</div>';
512     
513     echo '<div class="multi-part" id="settings" title="'.__('Settings').'">';
514     
515     
516     $meta =& $GLOBALS['core']->meta;
517     $my_params['post_id'] = $post_id;
518     $my_params['no_content'] = true;
519                   
520     $rs = $core->blog->getPosts($my_params);
521     if(isset($post)) {
522          $meta_rs = $meta->getMetaStr($post->post_meta,'map_options');
523          if ($meta_rs) {
524               $map_options = explode(",",$meta_rs);
525               $myGmaps_center = $map_options[0].','.$map_options[1];
526               $myGmaps_zoom = $map_options[2];
527               $myGmaps_type = $map_options[3];
528          }
529     }
530     
531     echo
532     '<form action="'.$p_url.'" method="post" id="map-options">'.
533     '<fieldset><legend>'.__('Map parameters').'</legend>'.
534     '<p>'.__('Choose map center, zoom level and map type.').'</p>'.
535     '</fieldset>'.
536     '<p class="area" id="map_canvas"></p>'.
537     '<p><input type="hidden" name="myGmaps_center_upd" id="myGmaps_center_upd" value="'.$myGmaps_center.'" />'.
538     '<input type="hidden" name="myGmaps_zoom_upd" id="myGmaps_zoom_upd" value="'.$myGmaps_zoom.'" />'.
539     '<input type="hidden" name="myGmaps_type_upd" id="myGmaps_type_upd" value="'.$myGmaps_type.'" /></p>'.
540     $core->formNonce();
541     
542     if (isset($has_map) && $has_map == true) {
543          echo '<p>'.form::hidden('post_id',$post_id).form::hidden('post_type',$post_type).'<input type="submit" value="'.__('Save').'" name="updmap" /></p>';
544     }
545     
546     echo '</form></div>';
547}
548
549dcPage::helpBlock('myGmapsadd');
550?>
551     </body>
552</html>
Note: See TracBrowser for help on using the repository browser.

Sites map