From 588f9bb1026cef27945b56ce9109c2c251429295 Mon Sep 17 00:00:00 2001 From: Franzz Date: Sun, 15 Apr 2018 13:14:37 +0200 Subject: [PATCH] code cleaning --- inc/spot.php | 127 ++++++++++++++++++++++++++++---------------- masks/messages.html | 28 ++++++++-- 2 files changed, 103 insertions(+), 52 deletions(-) diff --git a/inc/spot.php b/inc/spot.php index e90acdd..f73509d 100755 --- a/inc/spot.php +++ b/inc/spot.php @@ -192,6 +192,57 @@ class Spot extends Main return $asMessages; } + private function getPictures() + { + $asPicPaths = glob('files/*.{jpg,JPG,jpeg,JPEG,png,PNG}', GLOB_BRACE); + + $asPics = array(); + foreach($asPicPaths as $sPicPath) + { + //Finding picture timestamp + $asPicInfo = self::getPicInfo($sPicPath); + $iPicTimeStamp = $asPicInfo['timestamp']; + + //Preparing pictures sorting key and related info + if( Settings::MODE != self::MODE_HISTO || + ($iPicTimeStamp >= strtotime(Settings::HISTO_SPAN['from']) && $iPicTimeStamp <= strtotime(Settings::HISTO_SPAN['to']))) { + + $asPics[] = array( + 'path' => $sPicPath, + 'rotate' => $asPicInfo['rotate'], + 'timestamp' => $iPicTimeStamp, + 'formatted_time'=> date(self::FORMAT_TIME, $iPicTimeStamp), + 'relative_time' => Toolbox::getDateTimeDesc($iPicTimeStamp) + ); + } + } + return $asPics; + } + + private function getPosts() + { + $asInfo = array('from'=>self::POST_TABLE); + if(Settings::MODE==self::MODE_HISTO) { + $asInfo['constraint'] = array('led'=>Settings::HISTO_SPAN); + $asInfo['constOpe'] = array('led'=>"BETWEEN"); + } + + $asPosts = $this->oDb->selectRows($asInfo); + foreach($asPosts as &$asPost) { + $iUnixTimeStamp = strtotime($asPost['led']); + $asPost['relative_time'] = Toolbox::getDateTimeDesc($iUnixTimeStamp); + $asPost['formatted_time'] = date(self::FORMAT_TIME, $iUnixTimeStamp); + $asPost['formatted_name'] = Toolbox::mb_ucwords($asPost['name']); + } + + return $asPosts; + } + + private static function getJsonId($iTimeStamp, $sPriority='0', $iDbId=0) + { + return ($iTimeStamp * -1).'.'.$sPriority.$iDbId; + } + public function getNewsFeed($iChunk=0) { $asFeed = array(); @@ -200,62 +251,36 @@ class Spot extends Main $asMessages = $this->getSpotMessages(); foreach($asMessages as $asMessage) { - $iId = ($asMessage['unix_timestamp']*-1).'.0'.$asMessage[Db::getId(self::MSG_TABLE)]; - $asFeed[$iId] = $asMessage;// array('type'=>'message', 'time'=>$asMessage['relative_time'], 'text'=>$asMessage['relative_time']); + $iMsgId = $asMessage[Db::getId(self::MSG_TABLE)]; + $iId = self::getJsonId($asMessage['unix_timestamp'], '0', $iMsgId); + $asFeed[$iId] = $asMessage; $asFeed[$iId]['type'] = 'message'; - $asFeed[$iId]['id'] = $asMessage[Db::getId(self::MSG_TABLE)]; + $asFeed[$iId]['displayed_id'] = $iMsgId; } //Pictures - $asPicPaths = glob('files/*.{jpg,JPG,jpeg,JPEG,png,PNG}', GLOB_BRACE); - foreach($asPicPaths as $iKey=>$sPicPath) - { - //Finding picture timestamp - $asPicInfo = self::getPicInfo($sPicPath); - $iPicTimeStamp = $asPicInfo['timestamp']; - - //Preparing pictures sorting key and related info - if( Settings::MODE!=self::MODE_HISTO || - ($iPicTimeStamp>=strtotime(Settings::HISTO_SPAN['from']) && $iPicTimeStamp<=strtotime(Settings::HISTO_SPAN['to']))) { - - $asPics[($iPicTimeStamp*-1).'.1'.$iKey] = array( - 'type' => 'picture', - 'path' => $sPicPath, - 'rotate' => $asPicInfo['rotate'], - 'formatted_time'=> date(self::FORMAT_TIME, $iPicTimeStamp), - 'relative_time' => Toolbox::getDateTimeDesc($iPicTimeStamp) - ); - } - } - ksort($asPics); - $iCount = count($asPics); + $asPics = $this->getPictures(); foreach($asPics as $iKey=>$asPic) { - $asFeed[$iKey] = $asPic; - $asFeed[$iKey]['id'] = $iCount--; + $iId = self::getJsonId($asPic['timestamp'], '1', $iKey); + $asFeed[$iId] = $asPic; + $asFeed[$iId]['type'] = 'picture'; } //Post - $asInfo = array('from'=>self::POST_TABLE); - if(Settings::MODE==self::MODE_HISTO) { - $asInfo['constraint'] = array('led'=>Settings::HISTO_SPAN); - $asInfo['constOpe'] = array('led'=>"BETWEEN"); - } - $asPosts = $this->oDb->selectRows($asInfo); + $asPosts = $this->getPosts(); foreach($asPosts as $asPost) { - $iUnixTimeStamp = strtotime($asPost['led']); - $iId = ($iUnixTimeStamp*-1).'.2'.$asPost[Db::getId(self::POST_TABLE)]; - $asFeed[$iId] = array('name'=>Toolbox::mb_ucwords($asPost['name']), 'post'=>$asPost['content'], 'type'=>'post');; - $asFeed[$iId]['relative_time'] = Toolbox::getDateTimeDesc($iUnixTimeStamp); - $asFeed[$iId]['formatted_time'] = date(self::FORMAT_TIME, $iUnixTimeStamp); - //$asFeed[$iId]['id'] = $asPost[Db::getId(self::POST_TABLE)]; + $iId = self::getJsonId(strtotime($asPost['led']), '2', $asPost[Db::getId(self::POST_TABLE)]); + $asFeed[$iId] = $asPost; + $asFeed[$iId]['type'] = 'post'; } + //Sort by key ksort($asFeed); - //split chunks - $asFeed = array_slice($asFeed, $iChunk*self::FEED_CHUNK_SIZE, self::FEED_CHUNK_SIZE); + //Split chunks + //$asFeed = array_slice($asFeed, $iChunk*self::FEED_CHUNK_SIZE, self::FEED_CHUNK_SIZE); return self::getJsonResult(true, '', $asFeed); } @@ -277,13 +302,16 @@ class Spot extends Main public static function getPicInfo($sPicPath) { - $iPicTimeStamp = 0; + $iPicTimeStamp = $iPicTakenTimeStamp = $iPicFileTimeStamp = 0; $asExif = @exif_read_data($sPicPath, 0, true); - //Timestamp - if(!empty($asExif) && array_key_exists('DateTimeOriginal', $asExif['EXIF'])) $iPicTimeStamp = strtotime($asExif['EXIF']['DateTimeOriginal']); - else $iPicTimeStamp = $asExif['FILE']['FileDateTime']; - + //Timestamps + if(!empty($asExif) && array_key_exists('DateTimeOriginal', $asExif['EXIF'])) $iPicTakenTimeStamp = strtotime($asExif['EXIF']['DateTimeOriginal']); + if(!empty($asExif) && array_key_exists('FileDateTime', $asExif['FILE'])) $iPicFileTimeStamp = $asExif['FILE']['FileDateTime']; + + //Merge timestamps + $iPicTimeStamp = ($iPicTakenTimeStamp > 0)?$iPicTakenTimeStamp:$iPicFileTimeStamp; + //Time Zone //if($iPicTimeStamp >= self::HONEYMOON_DATE_BEG && $iPicTimeStamp <= self::HONEYMOON_DATE_END) $iPicTimeStamp -= 60*60*(12+1); //timezone + daylight saving @@ -300,7 +328,12 @@ class Spot extends Main } else $sRotate = '0'; - return array('timestamp'=>$iPicTimeStamp, 'rotate'=>$sRotate); + return array( + 'timestamp' => $iPicTimeStamp, + 'taken_ts' => $iPicTimeStamp, + 'file_ts' => $iPicTimeStamp, + 'rotate' => $sRotate + ); } } diff --git a/masks/messages.html b/masks/messages.html index a88cadc..a662ad2 100755 --- a/masks/messages.html +++ b/masks/messages.html @@ -118,6 +118,10 @@ oSpot.pageInit = function(asHash) oMarker.addListener('mouseout', function(){ oInfoWindow.close(oMap, oMarker); }); + oMarker.addListener('click', function(){ + self.tmp('map').panTo(oMarker.getPosition()); + self.tmp('map').setZoom(15); + }); }); //Recenter map once loaded to be at the center of 70% (iMapRatio) of the page @@ -239,10 +243,10 @@ function getPost(asPost) { { case 'message': $Body = $('
') - .append($('

').addIcon('fa-compass', true).append(asPost.latitude+' | '+asPost.longitude)) + .append($('

').addIcon('fa-compass', true).append('Latitude '+asPost.latitude+', Longitude '+asPost.longitude)) .append($('

').addIcon('fa-clock-o', true).append(sAbsTime)) .append( - $('', {'class':'staticmap', src:'https://maps.googleapis.com/maps/api/staticmap?center='+asPost.latitude+','+asPost.longitude+'&zoom=13&size=400x300&maptype=satellite&markers=color:green|label:|'+asPost.latitude+','+asPost.longitude+'&key='+self.vars('google_api')}) + $('', {'class':'staticmap', title: 'Click pour zoomer', src: getStaticMapUrl(asPost.latitude, asPost.longitude)}) .data('lat', asPost.latitude) .data('lng', asPost.longitude) .click(function(){ @@ -260,8 +264,8 @@ function getPost(asPost) { break; case 'post': $Body = $('

') - .append($('

', {'class':'message'}).text(asPost.post)) - .append($('

', {'class':'signature'}).text('-- '+asPost.name)); + .append($('

', {'class':'message'}).text(asPost.content)) + .append($('

', {'class':'signature'}).text('-- '+asPost.formatted_name)); sClass = 'comment'; break; case 'poster': @@ -278,10 +282,24 @@ function getPost(asPost) { .append($('', {'class':'time', 'title':sAbsTime}).text(sRelTime))) .append($('

', {'class':'body'}).append($Body)); - if(asPost.id) $Post.find('.index').append(' '+asPost.id); + if(asPost.displayed_id) $Post.find('.index').append(' '+asPost.displayed_id); //if(asPost.type=='picture' && asPost.rotate!='0') $Body.height($Image.height()); return $Post; } + +function getStaticMapUrl(oCenterLat, oCenterLng){ + var sDomain = 'https://maps.googleapis.com/maps/api/staticmap'; + var asParams = { + center: oCenterLat+','+oCenterLng, + zoom: '13', + size: '400x300', + maptype: 'satellite', + markers: 'color:green|label:|'+oCenterLat+','+oCenterLng, + key: self.vars('google_api') + }; + + return sDomain+'?'+Object.keys(asParams).map(k => k+'='+asParams[k]).join('&'); +} \ No newline at end of file