<?php

/**
 * @name          : Joomla Social Pinboard
 * @version	  : 1.5.0
 * @package       : apptha
 * @since         : Joomla 1.5
 * @author        : Apptha - http://www.apptha.com
 * @copyright     : Copyright (C) 2011 Powered by Apptha
 * @license       : http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
 * @abstract      : Social Pinboard Module header
 * @Creation Date : August 2013
 * @Modified Date : August 2013
 * */

defined('_JEXEC') or die;

class ModRelatedPinsHelper {

    public static function GetRelatedPins() {
        $db = JFactory::getDBO();

        $query = "SELECT category_id, category_name 
					FROM #__pin_categories WHERE status=1 AND published!=-2 
					ORDER BY created_date DESC LIMIT 1";
		$db->setQuery($query);
        $category = $db->loadObject();
		$category_id = $category->category_id;

		$query = "SELECT COUNT(pin_id) 
					FROM #__pin_pins 
					WHERE pin_category_id=$category_id AND status=1 AND published!=-2";
		$db->setQuery($query);
        $pin_count = $db->loadResult();

        $query = "SELECT pin_id, pin_image, link_type 
                    FROM #__pin_pins 
					WHERE pin_category_id = $category_id 
					ORDER BY created_date DESC 
                    LIMIT 4";
        $db->setQuery($query);
        $related_pins = $db->loadObjectList();
		$related_pins_count = count($related_pins);
		for($i=3; $i>=$related_pins_count; $i--) {
			$related_pins[$i]->pin_id = -1;
            $related_pins[$i]->pin_image = 'no-image.jpg';
            $related_pins[$i]->link_type = '';
		}

        $rows[0] = $related_pins;
        $rows[1] = $category;
        $rows[2] = $pin_count;
		return $rows;
    }

	static function getImageResolution($imgSrc, $maxHeight, $maxWidth) {

		$size = getimagesize($imgSrc);

		$width = $size[0];
		$height = $size[1];

		$return = array('width' => $maxWidth, 'height' => $maxHeight);

		if ($width > $maxWidth) {
			$return['width'] = $maxWidth;
			$return['height'] = floor($maxWidth / $width * $height);
			$width = $return['width'];
			$height = $return['height'];
		}
		if ($height > $maxHeight) {
			$return['width'] = floor($maxHeight / $height * $width);
			$return['height'] = $maxHeight;
		}

		return $return;
    }

}