<?php

    $limit = @$_REQUEST['recordLimit'] ? @$_REQUEST['recordLimit'] : 10;
    $offset = @$_REQUEST['offset'] ? @$_REQUEST['offset'] : 0;

    $moments = mysql_select("feed", "`section` = 'photo_uploads' OR `section` = 'photo_likes' ORDER BY `action_date` DESC LIMIT $limit OFFSET $offset");
?>
<?php

    foreach($moments as $moment):
        $momentData     = json_decode($moment['json'], true);    
        $activityName = $moment['section'];
        // get listings photo
        if( array_key_exists('listingsNum', $momentData) ){

            $momentData['listings'] = mysql_get('listings', $momentData['listingsNum'], null, "`name`,`num`");
            // add listings recent photos
            $imageRecords = mysql_select('photo_uploads', "`listingsNum` = ".intval($momentData['listingsNum'])." AND `for_photo` = 'photo' AND `flagged` = 0 ORDER BY `createdDate` DESC LIMIT 0,1");
            foreach($imageRecords as $photoKey => $photo){ 
              $imageFiles = getUploadRecords('photo_uploads', 'photos', $photo['num']);
                foreach($imageFiles as $file){
                    $momentData['listings']['recent_photo'] = $file['thumbUrlPath3'];
                }
            }
        }

        
        $momentData['members'] = mysql_get('members', $momentData['membersNum'], null, "`facebook_picture`, `facebook_first_name`, `facebook_last_name`, `username`, `num`, `isAdmin`");      
        // add members pics who liked photo
        $momentData['members_likes_photo'] = mysql_select_join('photo_likes', 'members', "`photo_uploadsNum` = ".intval(@$momentData['photos'][0]['recordNum']),"membersNum",array('facebook_picture'), 3);

        $momentData['members_likes_count'] = strval(mysql_count('photo_likes', "`photo_uploadsNum` = ".intval(@$momentData['photos'][0]['recordNum'])));
        
        $userName = $momentData['members']['facebook_first_name'];
        if(@$momentData['members']['facebook_last_name']){
            $userName .= " ".@$momentData['members']['facebook_last_name'][0].".";
        }
?>
    <?php if($activityName == 'photo_uploads'):

        $actionText = "Snaps a moment";

        if($momentData['members']['isAdmin']){
            $actionText = "Shares a moment";
            $userName = $momentData['listings']['name'];
            $momentData['members']['facebook_picture'] = $momentData['listings']['recent_photo'];
        }

    ?>
        <div class="feed_section stats_section clearfix">
            <div class="user_info_wrap padding-lr-10 clearfix">
                <div class="user_img">
                    <img width="30" height="30" src="<?php echo $momentData['members']['facebook_picture']; ?>">
                </div>
                <div class="comment_info">
                    <div class="user_name">
                        <span><?php echo $userName ?></span>
                    </div>
                    <div class="user_action">
                        <span class="user_action_txt"><?php echo $actionText; ?></span>
                        <span class="time"><?php echo shortPrettyDate($moment['action_date']); ?></span>
                    </div>
                </div>
                <div class="feed_points" style="float: right;">
                    <span class="check_in_icon" style="margin-right: 5px;">
                        <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/camera-small.png">
                    </span>
                    <span>25</span>
                </div>
            </div>
            <div class="snapped_img" style="background: url('<?php echo $momentData['photos'][0]['thumbUrlPath2']; ?>');background-repeat: no-repeat;background-size:cover;background-position: center;"></div>
            <?php if($momentData['members_likes_count'] > 0): ?>
            <div class="photo_likes_info">

                <div class="likes_user_img_wrap">
                <?php foreach($momentData['members_likes_photo'] as $index => $memberLikedPhoto): ?>
                    <div class='like_user_img' style='margin-right: <?php echo $index == 0 ? '5' : '-7';?>px;'><img src="<?php echo $memberLikedPhoto['facebook_picture']; ?>"/></div> 
                <?php endforeach; ?>
                </div>
                <span class="likes_count_text">
                <?php echo $momentData['members_likes_count']; ?>
                </span>
            </div>
            <?php endif; ?>
        </div>
        
        <hr style="margin:0;width: 100%;border: 3px solid #ddd;">
    <?php endif; ?>
    <?php if($activityName == 'photo_likes'): 

        $momentData['uploaded_by'] = mysql_select_join('photo_uploads', 'members', "urbanhal_photo_uploads.`num` = ".intval($momentData['photo_uploadsNum']),"membersNum",array('facebook_first_name','facebook_last_name','num','isAdmin'), 1);

        $uploadByUsername = $momentData['uploaded_by'][0]['facebook_first_name']." ".@$momentData['uploaded_by'][0]['facebook_last_name'][0];

        if($momentData['uploaded_by'][0]['isAdmin']){
            $photoData = mysql_get('photo_uploads', null, "`num` = ".intval($momentData['photo_uploadsNum']));

            if($photoData){
                $listing = mysql_get('listings', $photoData['listingsNum'], null, "`name`, `num`");
                
                $uploadByUsername = $listing['name'];
            }
        }

    ?>
        <div class="feed_section stats_section clearfix">
            <div class="user_info_wrap padding-lr-10 clearfix">
                <div class="user_img">
                    <img width="30" height="30" src="<?php echo $momentData['members']['facebook_picture']; ?>">
                </div>
                <div class="comment_info">
                    <div class="user_name">
                        <span><?php echo $userName ?></span>
                    </div>
                    <div class="user_action">
                        <span class="user_action_txt">Loves <a><?php echo $uploadByUsername; ?></a>'s moment</span>
                        <span class="time"><?php echo shortPrettyDate($moment['action_date']); ?></span>
                    </div>
                </div>
                <div class="feed_points" style="float: right;">
                    <span class="check_in_icon" style="margin-right: 5px;">
                        <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/like-unfilled-small.png">
                    </span>
                    <span>1</span>
                </div>
            </div>
            <div class="snapped_img" style="background: url('<?php echo !empty($momentData['photo_uploads.photos']) ? $momentData['photo_uploads.photos'][0]['thumbUrlPath2'] : ''; ?>');background-repeat: no-repeat;background-size: cover;background-position: center;"></div>
            <?php if($momentData['members_likes_count'] > 0): ?>
            <div class="photo_likes_info">

                <div class="likes_user_img_wrap">
                <?php foreach($momentData['members_likes_photo'] as $index => $memberLikedPhoto): ?>
                    <div class='like_user_img' style='float: right;border-radius: 50%;margin-right: <?php echo $index == 0 ? '5' : '-7';?>px;'>
                        <img src="<?php echo $memberLikedPhoto['facebook_picture']; ?>"/>
                    </div> 
                <?php endforeach; ?>
                </div>
                <span class="likes_count_text">
                <?php echo $momentData['members_likes_count']; ?>
                </span>
            </div>
            <?php endif; ?>
        </div>
        
        <hr style="margin:0;width: 100%;border: 3px solid #ddd;">
    <?php endif; ?>
<?php
    endforeach;
?>        