|
| 1 | +<?php |
| 2 | +/*********************************************** |
| 3 | +* File : admin_place.php |
| 4 | +* Project : piwigo-openstreetmap |
| 5 | +* Descr : Create place for reuse |
| 6 | +* |
| 7 | +* Created : 07.07.2015 |
| 8 | +* |
| 9 | +* Copyright 2013-2015 <xbgmsharp@gmail.com> |
| 10 | +* |
| 11 | +* This program is free software: you can redistribute it and/or modify |
| 12 | +* it under the terms of the GNU General Public License as published by |
| 13 | +* the Free Software Foundation, either version 3 of the License, or |
| 14 | +* (at your option) any later version. |
| 15 | +* |
| 16 | +* This program is distributed in the hope that it will be useful, |
| 17 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +* GNU General Public License for more details. |
| 20 | +* |
| 21 | +* You should have received a copy of the GNU General Public License |
| 22 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | +* |
| 24 | +************************************************/ |
| 25 | + |
| 26 | +// Check whether we are indeed included by Piwigo. |
| 27 | +if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
| 28 | + |
| 29 | +// Check access and exit when user status is not ok |
| 30 | +check_status(ACCESS_ADMINISTRATOR); |
| 31 | + |
| 32 | +// Setup plugin Language |
| 33 | +load_language('plugin.lang', OSM_PATH); |
| 34 | + |
| 35 | +// Fetch the template. |
| 36 | +global $template, $conf, $lang, $prefixeTable; |
| 37 | +// Easy access |
| 38 | +define('osm_place_table', $prefixeTable.'osm_places'); |
| 39 | + |
| 40 | +/* Table to hold osm places details */ |
| 41 | +$q = 'CREATE TABLE IF NOT EXISTS `'.osm_place_table.'` ( |
| 42 | + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, |
| 43 | + `latitude` double(8,6) NOT NULL, |
| 44 | + `longitude` double(8,6) NOT NULL, |
| 45 | + `name` varchar(255) DEFAULT NULL, |
| 46 | + `parentId` mediumint(8), |
| 47 | + PRIMARY KEY (id) |
| 48 | + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
| 49 | + ;'; |
| 50 | +pwg_query($q); |
| 51 | + |
| 52 | + |
| 53 | +// +-----------------------------------------------------------------------+ |
| 54 | +// | edit places | |
| 55 | +// +-----------------------------------------------------------------------+ |
| 56 | + |
| 57 | +if (isset($_POST['edit_submit']) and isset($_POST['edit_list'])) |
| 58 | +{ |
| 59 | + $query = ' |
| 60 | +SELECT name |
| 61 | + FROM '.osm_place_table.' |
| 62 | +;'; |
| 63 | + $existing_names = array_from_query($query, 'name'); |
| 64 | + |
| 65 | + $current_name_of = array(); |
| 66 | + $query = ' |
| 67 | +SELECT id, name, latitude, longitude |
| 68 | + FROM '.osm_place_table.' |
| 69 | + WHERE id IN ('.$_POST['edit_list'].') |
| 70 | +;'; |
| 71 | + $result = pwg_query($query); |
| 72 | + while ($row = pwg_db_fetch_assoc($result)) |
| 73 | + { |
| 74 | + $current_name_of[ $row['id'] ] = $row['name']; |
| 75 | + } |
| 76 | + |
| 77 | + $updates = array(); |
| 78 | + // we must not rename tag with an already existing name |
| 79 | + foreach (explode(',', $_POST['edit_list']) as $place_id) |
| 80 | + { |
| 81 | + $place_name = stripslashes($_POST['place_name-'.$place_id]); |
| 82 | + $place_lat = stripslashes($_POST['place_lat-'.$place_id]); |
| 83 | + $place_lon = stripslashes($_POST['place_lon-'.$place_id]); |
| 84 | + |
| 85 | + if ($place_name != $current_name_of[$place_id]) |
| 86 | + { |
| 87 | + if (in_array($place_name, $existing_names)) |
| 88 | + { |
| 89 | + $page['errors'][] = l10n('Place "%s" already exists', $place_name); |
| 90 | + } |
| 91 | + else if (!empty($place_name)) |
| 92 | + { |
| 93 | + $updates[] = array( |
| 94 | + 'id' => $place_id, |
| 95 | + 'name' => addslashes($place_name), |
| 96 | + 'latitude' => $place_lat, |
| 97 | + 'longitude' => $place_lon, |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + mass_updates( |
| 103 | + osm_place_table, |
| 104 | + array( |
| 105 | + 'primary' => array('id'), |
| 106 | + 'update' => array('name', 'latitude', 'longitude'), |
| 107 | + ), |
| 108 | + $updates |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +// +-----------------------------------------------------------------------+ |
| 113 | +// | delete places | |
| 114 | +// +-----------------------------------------------------------------------+ |
| 115 | + |
| 116 | +if (isset($_POST['delete']) and isset($_POST['places'])) |
| 117 | +{ |
| 118 | + $query = ' |
| 119 | +SELECT name |
| 120 | + FROM '.osm_place_table.' |
| 121 | + WHERE id IN ('.implode(',', $_POST['places']).') |
| 122 | +;'; |
| 123 | + $place_names = array_from_query($query, 'name'); |
| 124 | + |
| 125 | + $query = ' |
| 126 | +DELETE |
| 127 | + FROM '.osm_place_table.' |
| 128 | + WHERE id IN ('.implode(',', $_POST['places']).') |
| 129 | +;'; |
| 130 | + pwg_query($query); |
| 131 | + |
| 132 | + $page['infos'][] = l10n_dec( |
| 133 | + 'The following place was deleted', 'The %d following places were deleted', |
| 134 | + count($place_names) |
| 135 | + ) |
| 136 | + .' : '.implode(', ', $place_names); |
| 137 | +} |
| 138 | + |
| 139 | +// +-----------------------------------------------------------------------+ |
| 140 | +// | add a place | |
| 141 | +// +-----------------------------------------------------------------------+ |
| 142 | +if (isset($_POST['add']) and !empty($_POST['add_place'])) |
| 143 | +{ |
| 144 | + $query = "INSERT INTO `".osm_place_table."` (`name`, `latitude`, `longitude`) VALUE ('". $_POST['add_place'] ."', '". $_POST['add_lat'] ."', '". $_POST['add_lon'] ."');"; |
| 145 | + $result = pwg_query($query); |
| 146 | +} |
| 147 | + |
| 148 | +// all places |
| 149 | +$query = 'SELECT * FROM `'.osm_place_table.'`;'; |
| 150 | +$result = pwg_query($query); |
| 151 | +$all_places = array(); |
| 152 | +while ($place = pwg_db_fetch_assoc($result)) |
| 153 | +{ |
| 154 | + $all_places[] = $place; |
| 155 | +} |
| 156 | + |
| 157 | +// Send value to templates |
| 158 | +$template->assign( |
| 159 | + array( |
| 160 | + 'OSM_PATH' => OSM_PATH, |
| 161 | + 'all_places' => $all_places, |
| 162 | + ) |
| 163 | +); |
| 164 | + |
| 165 | +if (isset($_POST['edit']) and isset($_POST['places'])) |
| 166 | +{ |
| 167 | + $list_name = 'EDIT_PLACES_LIST'; |
| 168 | + if (isset($_POST['duplicate'])) |
| 169 | + { |
| 170 | + $list_name = 'DUPLIC_TAGS_LIST'; |
| 171 | + } |
| 172 | + elseif (isset($_POST['merge'])) |
| 173 | + { |
| 174 | + $list_name = 'MERGE_TAGS_LIST'; |
| 175 | + } |
| 176 | + |
| 177 | + $template->assign($list_name, implode(',', $_POST['places'])); |
| 178 | + |
| 179 | + $query = ' |
| 180 | +SELECT id, name, latitude, longitude |
| 181 | + FROM '.osm_place_table.' |
| 182 | + WHERE id IN ('.implode(',', $_POST['places']).') |
| 183 | +;'; |
| 184 | + $result = pwg_query($query); |
| 185 | + while ($row = pwg_db_fetch_assoc($result)) |
| 186 | + { |
| 187 | + $template->append( |
| 188 | + 'places', |
| 189 | + array( |
| 190 | + 'ID' => $row['id'], |
| 191 | + 'NAME' => $row['name'], |
| 192 | + 'LATITUDE' => $row['latitude'], |
| 193 | + 'LONGITUDE' => $row['longitude'], |
| 194 | + ) |
| 195 | + ); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +?> |
0 commit comments