[3447] | 1 | <?php |
---|
| 2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | function parse_fract( $f ) |
---|
| 5 | { |
---|
| 6 | $nd = explode( '/', $f ); |
---|
| 7 | return $nd[0]/$nd[1]; |
---|
| 8 | } |
---|
| 9 | |
---|
| 10 | function parse_lat_lon( $arr ) |
---|
| 11 | { |
---|
| 12 | $v=0; |
---|
| 13 | $v += parse_fract( $arr[0] ); |
---|
| 14 | $v += parse_fract( $arr[1] )/60; |
---|
| 15 | $v += parse_fract( $arr[2] )/3600; |
---|
| 16 | return $v; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | if ( isset($_POST['submit']) ) |
---|
| 20 | $sync_options = array( |
---|
| 21 | // 'no_overwrite' => isset($_POST['no_overwrite']), |
---|
| 22 | 'simulate' => is_adviser() ? true : isset($_POST['simulate']), |
---|
| 23 | 'cat_id' => isset($_POST['cat_id']) ? (int)$_POST['cat_id'] : 0, |
---|
| 24 | 'subcats_included' => isset($_POST['subcats_included']), |
---|
| 25 | ); |
---|
| 26 | else |
---|
| 27 | $sync_options = array( |
---|
| 28 | // 'no_overwrite' => true, |
---|
| 29 | 'simulate' => true, |
---|
| 30 | 'cat_id' => 0, |
---|
| 31 | 'subcats_included' => true, |
---|
| 32 | ); |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | if ( isset($_POST['submit']) ) |
---|
| 36 | { |
---|
| 37 | $where_clauses = array(); |
---|
| 38 | if ( $sync_options['cat_id']!=0 ) |
---|
| 39 | { |
---|
| 40 | $query=' |
---|
| 41 | SELECT id FROM '.CATEGORIES_TABLE.' |
---|
| 42 | WHERE '; |
---|
| 43 | if ( $sync_options['subcats_included']) |
---|
| 44 | $query .= 'uppercats REGEXP \'(^|,)'.$sync_options['cat_id'].'(,|$)\''; |
---|
| 45 | else |
---|
| 46 | $query .= 'id='.$sync_options['cat_id']; |
---|
| 47 | $cat_ids = array_from_query($query, 'id'); |
---|
| 48 | |
---|
| 49 | $query=' |
---|
| 50 | SELECT id,path,lat,lon |
---|
| 51 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
---|
| 52 | WHERE category_id IN ('.implode(',', $cat_ids).') |
---|
| 53 | GROUP BY id'; |
---|
| 54 | } |
---|
| 55 | else |
---|
| 56 | { |
---|
| 57 | $query=' |
---|
| 58 | SELECT id,path,lat,lon |
---|
| 59 | FROM '.IMAGES_TABLE; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | $images = hash_from_query( $query, 'id'); |
---|
| 63 | $datas = array(); |
---|
| 64 | $errors = array(); |
---|
| 65 | foreach ($images as $image) |
---|
| 66 | { |
---|
| 67 | $exif = @read_exif_data( $image['path'] ); |
---|
| 68 | if ( empty($exif) ) |
---|
| 69 | continue; |
---|
| 70 | $exif = array_intersect_key( $exif, array_flip( array('GPSLatitudeRef', 'GPSLatitude', 'GPSLongitudeRef', 'GPSLongitude') ) ); |
---|
| 71 | if ( count($exif)!=4 ) |
---|
| 72 | continue; |
---|
| 73 | if ( !in_array($exif['GPSLatitudeRef'], array('S', 'N') ) ) |
---|
| 74 | { |
---|
| 75 | $errors[] = $image['path']. ': GPSLatitudeRef not S or N'; |
---|
| 76 | continue; |
---|
| 77 | } |
---|
| 78 | if ( !in_array($exif['GPSLongitudeRef'], array('W', 'E') ) ) |
---|
| 79 | { |
---|
| 80 | $errors[] = $image['path']. ': GPSLongitudeRef not W or E'; |
---|
| 81 | continue; |
---|
| 82 | } |
---|
| 83 | if (!is_array($exif['GPSLatitude']) or !is_array($exif['GPSLongitude']) ) |
---|
| 84 | { |
---|
| 85 | $errors[] = $image['path']. ': GPSLatitude and GPSLongitude are not arrays'; |
---|
| 86 | continue; |
---|
| 87 | } |
---|
| 88 | $lat = parse_lat_lon( $exif['GPSLatitude'] ); |
---|
| 89 | if ( $exif['GPSLatitudeRef']=='S' ) |
---|
| 90 | $lat = -$lat; |
---|
| 91 | $lon = parse_lat_lon( $exif['GPSLongitude'] ); |
---|
| 92 | if ( $exif['GPSLongitudeRef']=='W' ) |
---|
| 93 | $lon = -$lon; |
---|
| 94 | $datas[] = array ( |
---|
| 95 | 'id' => $image['id'], |
---|
| 96 | 'lat' => $lat, |
---|
| 97 | 'lon' => $lon, |
---|
| 98 | ); |
---|
| 99 | } |
---|
| 100 | $template->assign( 'sync_errors', $errors ); |
---|
| 101 | |
---|
| 102 | if ( count($datas)>0 and !$sync_options['simulate'] ) |
---|
| 103 | { |
---|
| 104 | mass_updates( |
---|
| 105 | IMAGES_TABLE, |
---|
| 106 | array( |
---|
| 107 | 'primary' => array('id'), |
---|
| 108 | 'update' => array('lat', 'lon') |
---|
| 109 | ), |
---|
| 110 | $datas |
---|
| 111 | ); |
---|
| 112 | rvm_invalidate_cache(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | $template->assign( |
---|
| 116 | 'metadata_result', |
---|
| 117 | array( |
---|
| 118 | 'NB_ELEMENTS_DONE' => count($datas), |
---|
| 119 | 'NB_ELEMENTS_CANDIDATES' => count($images), |
---|
| 120 | 'NB_ERRORS' => count($errors), |
---|
| 121 | )); |
---|
| 122 | |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | $query = ' |
---|
| 127 | SELECT id, |
---|
| 128 | CONCAT(name, IF(dir IS NULL, " (V)", "") ) AS name, |
---|
| 129 | uppercats, global_rank |
---|
| 130 | FROM '.CATEGORIES_TABLE; |
---|
| 131 | display_select_cat_wrapper($query, |
---|
| 132 | array( $sync_options['cat_id'] ), |
---|
| 133 | 'categories', |
---|
| 134 | false); |
---|
| 135 | $template->assign( |
---|
| 136 | array( |
---|
| 137 | 'SUBCATS_INCLUDED_CHECKED' => $sync_options['subcats_included'] ? 'checked="checked"' : '' |
---|
| 138 | ) |
---|
| 139 | ); |
---|
| 140 | |
---|
| 141 | ?> |
---|