<?php
/**
 * Yelp API v2.0 code sample.
 *
 * This program demonstrates the capability of the Yelp API version 2.0
 * by using the Search API to query for businesses by a search term and location,
 * and the Business API to query additional information about the top result
 * from the search query.
 * 
 * Please refer to http://www.yelp.com/developers/documentation for the API documentation.
 * 
 * This program requires a PHP OAuth2 library, which is included in this branch and can be
 * found here:
 *      http://oauth.googlecode.com/svn/code/php/
 * 
 * Sample usage of the program:
 * `php sample.php --term="bars" --location="San Francisco, CA"`
 */
// Enter the path that the oauth library is in relation to the php file
require_once(__DIR__ . '/../lib/yelp/OAuth.php');

$widget = $wnt; // MySQL response array from where this file was included

$CONSUMER_KEY = $widget['yelp_consumer_key'];
$CONSUMER_SECRET = $widget['yelp_consumer_secret'];
$TOKEN = $widget['yelp_token'];
$TOKEN_SECRET = $widget['yelp_token_secret'];
$API_HOST = 'api.yelp.com';
$BUSINESS_PATH = '/v2/business/';

//use Taste developer API keys if not populated
if($CONSUMER_KEY==""){ $CONSUMER_KEY= "LWj_Tucc9BDH0OMeO1Yxsg";}
if($CONSUMER_SECRET==""){ $CONSUMER_SECRET= "4FquH7a_05BjzmmXlqvQA6Kyq1k";}
if($TOKEN==""){ $TOKEN= "GS0ogthXswBfqAAdysCIZic4zvnrn_wN";}
if($TOKEN_SECRET==""){ $TOKEN_SECRET= "J_8J3y2CjGGmmWQxzy8r2tKM2TQ";}



/** 
 * Makes a request to the Yelp API and returns the response
 * 
 * @param    $host    The domain host of the API 
 * @param    $path    The path of the APi after the domain
 * @return   The JSON response from the request      
 */
function request($host, $path) {
    $unsigned_url = "https://" . $host . $path;
    // Token object built using the OAuth library
    $token = new OAuthToken($GLOBALS['TOKEN'], $GLOBALS['TOKEN_SECRET']);
    // Consumer object built using the OAuth library
    $consumer = new OAuthConsumer($GLOBALS['CONSUMER_KEY'], $GLOBALS['CONSUMER_SECRET']);
    // Yelp uses HMAC SHA1 encoding
    $signature_method = new OAuthSignatureMethod_HMAC_SHA1();
    $oauthrequest = OAuthRequest::from_consumer_and_token(
        $consumer, 
        $token, 
        'GET', 
        $unsigned_url
    );
    
    // Sign the request
    $oauthrequest->sign_request($signature_method, $consumer, $token);
    
    // Get the signed URL
    $signed_url = $oauthrequest->to_url();
    
    // Send Yelp API Call
    $ch = curl_init($signed_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    
    return $data;
}
/**
 * Query the Business API by business_id
 * 
 * @param    $business_id    The ID of the business to query
 * @return   The JSON response from the request 
 */
function get_business($business_id) {
    $business_path = $GLOBALS['BUSINESS_PATH'] . $business_id;
    
    return request($GLOBALS['API_HOST'], $business_path);
}

$business = json_decode(get_business($widget['yelp_business_id']));

if (isset($business->error)) {
    $business = null;
}

if ($business) {
    $recentReview = count($business->reviews) ? $business->reviews[0] : null;
}

?>
<?php if ($business) : ?>
    <div class="yelp-widget">
        <h4 class="business-name"><?php echo $business->name; ?></h4>
        <div>
            <a href="<?php echo $business->url; ?>" target="_blank">
                <img src="<?php echo $business->image_url; ?>" alt="">
            </a>
        </div>
        <div>
            <img class="business-rating-img" src="<?php echo $business->rating_img_url_large; ?>" alt="">
        </div>
        <p><?php echo $business->review_count; ?> Reviews</p>

        <?php if ($widget['yelp_show_review'] && $recentReview) : ?>
            <div class="recent-review">
                <h5>Recent Review:</h5>
                <img class="review-user-image" src="<?php echo $recentReview->user->image_url; ?>" alt="" style="float: left; padding-right: 10px;">
                <div class="review-user-name">
                    <strong><?php echo $recentReview->user->name; ?></strong>
                </div>
                <img class="review-rating-img" src="<?php echo $recentReview->rating_image_url; ?>" alt="">
                <p class="review-excerpt">
                    <?php echo $recentReview->excerpt; ?>
                    <a href="<?php echo $business->url; ?>#hrid:<?php echo $recentReview->id; ?>" target="_blank">Read More</a>
                </p>
            </div>
        <?php endif; ?>
    </div>
<?php endif; ?>