<?php
/**
 * Solwin Infotech
 * Solwin Instagram Extension
 * 
 * @category   Solwin
 * @package    Solwin_Instagram
 * @copyright  Copyright © 2006-2016 Solwin (https://www.solwininfotech.com)
 * @license    https://www.solwininfotech.com/magento-extension-license/
 */
$helper = $this->helper('\Solwin\Instagram\Helper\Data');
$enable = $helper->getConfig('instagramsection/instagramgroup/active');
$userId = $helper->getConfig('instagramsection/instagramgroup/userid');
$accessToken = $helper->getConfig('instagramsection/instagramgroup/accesstoken');
$imageNumber = $this->getData('numberimage');
?>

<?php
if ($enable) {
    ?>
    <div class="cf-title">
        <h3><?php echo $this->getData('title'); ?></h3>
    </div>
    <div class="cf-content">
        <?php
        $url = 'https://api.instagram.com/v1/users/' . $userId . '/media/recent/?access_token=' . $accessToken;
        $args = stream_context_create(array('http' => array('timeout' => 2500,)));
        //for Live server
        $instagramFeedsData = file_get_contents($url, false, $args );
        //for Local Server
        //$instagramFeedsData = $helper->getInstangramData($url);

        $instagramFeeds = json_decode($instagramFeedsData);

        $instangramData = $instagramFeeds->data;
        $count = 1;
        foreach ($instangramData as $instagramFeed) {

            if ($imageNumber < $count) {
                break;
            }
            $instagramImage = $instagramFeed->images->thumbnail->url;
            $count++;
            ?>

            <?php
            $caption_text = 'Instagram';
            if (is_object($instagramFeed->caption)) {
                $caption_text = $instagramFeed->caption->text;
            }
            ?>
            <div class="instangram-feed">

                <a href ="<?php echo $instagramFeed->link; ?>" target="_blank">
                    <img width="80" src="<?php echo $instagramImage; ?>" title="<?php echo $caption_text; ?>" alt="<?php echo $caption_text; ?>"/>
                </a>
            </div>
            <?php
        }
    }
    ?>
</div>
