[24677] | 1 | <?php |
---|
| 2 | /*********************************************** |
---|
| 3 | * File : admin_batchmanager.php |
---|
| 4 | * Project : piwigo-openstreetmap |
---|
| 5 | * Descr : handle batch manager |
---|
| 6 | * Base on : RV Maps & Earth plugin |
---|
| 7 | * |
---|
| 8 | * Created : 4.06.2013 |
---|
| 9 | * |
---|
| 10 | * Copyright 2012-2013 <xbgmsharp@gmail.com> |
---|
| 11 | * |
---|
| 12 | * |
---|
| 13 | * This program is free software: you can redistribute it and/or modify |
---|
| 14 | * it under the terms of the GNU General Public License as published by |
---|
| 15 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 16 | * (at your option) any later version. |
---|
| 17 | * |
---|
| 18 | * This program is distributed in the hope that it will be useful, |
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 21 | * GNU General Public License for more details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License |
---|
| 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 25 | * |
---|
| 26 | ************************************************/ |
---|
| 27 | |
---|
| 28 | // Check whether we are indeed included by Piwigo. |
---|
| 29 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
| 30 | |
---|
| 31 | // Hook to add a new filter in the batch mode |
---|
| 32 | add_event_handler('get_batch_manager_prefilters', 'osm_get_batch_manager_prefilters'); |
---|
| 33 | function osm_get_batch_manager_prefilters($prefilters) |
---|
| 34 | { |
---|
| 35 | $prefilters[] = array('ID' => 'osm0', 'NAME' => l10n('OSM Geotagged')); |
---|
| 36 | $prefilters[] = array('ID' => 'osm1', 'NAME' => l10n('OSM Not geotagged')); |
---|
| 37 | return $prefilters; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | // Hook to perfom the filter in the batch mode |
---|
| 41 | add_event_handler('perform_batch_manager_prefilters', 'osm_perform_batch_manager_prefilters', 50, 2); |
---|
| 42 | function osm_perform_batch_manager_prefilters($filter_sets, $prefilter) |
---|
| 43 | { |
---|
| 44 | if ($prefilter==="osm0") |
---|
| 45 | $filter = "`lat` IS NOT NULL and `lon` IS NOT NULL"; |
---|
| 46 | else if ($prefilter==="osm1") |
---|
| 47 | $filter = "`lat` IS NULL OR `lon` IS NULL"; |
---|
| 48 | |
---|
| 49 | if ( isset($filter) ) |
---|
| 50 | { |
---|
| 51 | $query = "SELECT id FROM ".IMAGES_TABLE." WHERE ".$filter; |
---|
| 52 | $filter_sets[] = array_from_query($query, 'id'); |
---|
| 53 | } |
---|
| 54 | return $filter_sets; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // Hook to show action when selected |
---|
| 58 | add_event_handler('loc_end_element_set_global', 'osm_loc_end_element_set_global'); |
---|
| 59 | function osm_loc_end_element_set_global() |
---|
| 60 | { |
---|
| 61 | global $template; |
---|
| 62 | $template->append('element_set_global_plugins_actions', |
---|
| 63 | array('ID' => 'openstreetmap', 'NAME'=>l10n('OSM GeoTag'), 'CONTENT' => ' |
---|
| 64 | <label>'.l10n('Latitude').' (-90=S to 90=N) |
---|
| 65 | <input type="text" size="8" name="osmlat"> |
---|
| 66 | </label> |
---|
| 67 | <label>'.l10n('Longitude').' (-180=W to 180=E) |
---|
| 68 | <input type="text" size="9" name="osmlon"> |
---|
| 69 | </label> (Empty values will erase coordinates) |
---|
| 70 | ')); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | // Hook to perform the action on in global mode |
---|
| 74 | add_event_handler('element_set_global_action', 'osm_element_set_global_action', 50, 2); |
---|
| 75 | function osm_element_set_global_action($action, $collection) |
---|
| 76 | { |
---|
| 77 | if ($action!=="openstreetmap") |
---|
| 78 | return; |
---|
| 79 | |
---|
| 80 | global $page; |
---|
| 81 | |
---|
| 82 | $lat = trim($_POST['osmlat']); |
---|
| 83 | $lon = trim($_POST['osmlon']); |
---|
| 84 | if ( strlen($lat)>0 and strlen($lon)>0 ) |
---|
| 85 | { |
---|
| 86 | if ( (double)$lat<=90 and (double)$lat>=-90 |
---|
| 87 | and (double)$lon<=180 and (double)$lon>=-180 ) |
---|
| 88 | $update_query = 'lat='.$lat.', lon='.$lon; |
---|
| 89 | else |
---|
| 90 | $page['errors'][] = 'Invalid lat or lon value'; |
---|
| 91 | } |
---|
| 92 | elseif ( strlen($lat)==0 and strlen($lon)==0 ) |
---|
| 93 | $update_query = 'lat=NULL, lon=NULL'; |
---|
| 94 | else |
---|
| 95 | $page['errors'][] = 'Both lat/lon must be empty or not empty'; |
---|
| 96 | |
---|
| 97 | if (isset($update_query)) |
---|
| 98 | { |
---|
| 99 | $update_query = 'UPDATE '.IMAGES_TABLE.' SET '.$update_query.' |
---|
| 100 | WHERE id IN ('.implode(',',$collection).')'; |
---|
| 101 | pwg_query($update_query); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | // Hook to perform the action on in single mode |
---|
| 106 | add_event_handler('loc_begin_element_set_unit', 'osm_loc_begin_element_set_unit'); |
---|
| 107 | function osm_loc_begin_element_set_unit() |
---|
| 108 | { |
---|
| 109 | global $page; |
---|
| 110 | |
---|
| 111 | if (!isset($_POST['submit'])) |
---|
| 112 | return; |
---|
| 113 | |
---|
| 114 | $collection = explode(',', $_POST['element_ids']); |
---|
| 115 | $query = "SELECT `id`, `lat`, `lon` |
---|
| 116 | FROM ".IMAGES_TABLE." |
---|
| 117 | WHERE id IN (".implode(',',$collection).")"; |
---|
| 118 | |
---|
| 119 | $datas = array(); |
---|
| 120 | $errors = array(); |
---|
| 121 | $form_errors = 0; |
---|
| 122 | |
---|
| 123 | $result = pwg_query($query); |
---|
| 124 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 125 | { |
---|
| 126 | if (!isset($_POST['osmlat-'.$row['id']])) |
---|
| 127 | { |
---|
| 128 | $form_errors++; |
---|
| 129 | continue; |
---|
| 130 | } |
---|
| 131 | $error = false; |
---|
| 132 | $data = array( |
---|
| 133 | 'id' => $row['id'], |
---|
| 134 | 'lat' => trim($_POST['osmlat-'.$row['id']]), |
---|
| 135 | 'lon' => trim($_POST['osmlon-'.$row['id']]) |
---|
| 136 | ); |
---|
| 137 | |
---|
| 138 | if ( strlen($data['lat'])>0 and strlen($data['lon'])>0 ) |
---|
| 139 | { |
---|
| 140 | if ( (double)$data['lat']>90 or (double)$data['lat']<-90 |
---|
| 141 | or (double)$data['lon']>180 or (double)$data['lon']<-180 ) |
---|
| 142 | $error = true; |
---|
| 143 | } |
---|
| 144 | elseif ( strlen($data['lat'])==0 and strlen($data['lon'])==0 ) |
---|
| 145 | { |
---|
| 146 | // nothing |
---|
| 147 | } |
---|
| 148 | else |
---|
| 149 | { |
---|
| 150 | $error = true; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | if ($error) |
---|
| 154 | $errors[] = $row['name']; |
---|
| 155 | else |
---|
| 156 | $datas[] = $data; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | mass_updates( |
---|
| 160 | IMAGES_TABLE, |
---|
| 161 | array( |
---|
| 162 | 'primary' => array('id'), |
---|
| 163 | 'update' => array('lat', 'lon') |
---|
| 164 | ), |
---|
| 165 | $datas |
---|
| 166 | ); |
---|
| 167 | |
---|
| 168 | if (count($errors)>0) |
---|
| 169 | { |
---|
| 170 | $page['errors'][] = 'Invalid lat or lon value for files: '.implode(', ', $errors); |
---|
| 171 | } |
---|
| 172 | if ($form_errors) |
---|
| 173 | $page['errors'][] = 'OpenStreetMap: Invalid form submission for '.$form_errors.' photos'; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | // Hoook for batch manager in single mode |
---|
| 177 | add_event_handler('loc_end_element_set_unit', 'osm_loc_end_element_set_unit'); |
---|
| 178 | function osm_loc_end_element_set_unit() |
---|
| 179 | { |
---|
| 180 | global $template, $conf, $page, $is_category, $category_info; |
---|
| 181 | $template->set_prefilter('batch_manager_unit', 'osm_prefilter_batch_manager_unit'); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | function osm_prefilter_batch_manager_unit($content) |
---|
| 185 | { |
---|
| 186 | $needle = '</table>'; |
---|
| 187 | $pos = strpos($content, $needle); |
---|
| 188 | if ($pos!==false) |
---|
| 189 | { |
---|
| 190 | $add = '<tr><td><strong>{\'OSM Geotag\'|@translate}</strong></td> |
---|
| 191 | <td> |
---|
| 192 | <label>{\'Latitude\'|@translate} |
---|
| 193 | <input type="text" size="8" name="osmlat-{$element.id}" value="{$element.lat}"> |
---|
| 194 | </label> |
---|
| 195 | <label>{\'Longitude\'|@translate} |
---|
| 196 | <input type="text" size="9" name="osmlon-{$element.id}" value="{$element.lon}"> |
---|
| 197 | </label> |
---|
| 198 | </td> |
---|
| 199 | </tr>'; |
---|
| 200 | $content = substr_replace($content, $add, $pos, 0); |
---|
| 201 | } |
---|
| 202 | return $content; |
---|
| 203 | } |
---|