<?php

/**
 * @name          : Joomla Social Pinboard
 ** @version	  : 2.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 Component categories model
 * @Creation Date : March 2013
 * @Modified Date : March 2013
 * */
// No direct Access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');
jimport('joomla.user.helper');

class wishlogueModelallcatalogues extends WishlogueModel {

    ///rgs.
	static function getBoardNames($catID)
	{
        $db = JFactory::getDBO();
		$user = JFactory::getUser();
        $userId = $user->id;

		$query = "SELECT `subcatalogue_id`, `subcatalogue_name`, '1' AS section_type 
					FROM `#__pin_subcatalogues` WHERE `status`=1 AND `catalogue_id`=$catID ORDER BY `subcatalogue_name` ";
		$db->setQuery($query);
		$getSubcatalogues = $db->loadObjectList();

//		if( $userId > 0 && $userId != null && $userId != '' ) {
//			$query = "SELECT board_id AS subcatalogue_id, board_name AS subcatalogue_name, '2' AS section_type 
//						FROM #__pin_boards 
//						WHERE board_category_id = $catID AND status = 1 AND user_id = $userId AND admin_subcatalogue = 0 
//						ORDER BY board_name ";
//			$db->setQuery($query);
//			$getBoards = $db->loadObjectList();
//			$array = array_merge( $getSubcatalogues, $getBoards );
//
//			$sorter = array();
//			$getSubcatalogues = array();
//			foreach ($array as $ii => $va) {
//				$sorter[$ii]=$va->subcatalogue_name;
//			}
//			natcasesort($sorter);
//			$i = 0;
//			foreach ($sorter as $ii => $va) {
//				$getSubcatalogues[$i++]=$array[$ii];
//			}
//		}

        return $getSubcatalogues;
    }

	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;
    }
	///rgs..

    function getCategories() {
        $db = $this->getDBO();
        $query = "select category_id,category_name from #__pin_categories where status = 1 ORDER BY category_name";
        $db->setQuery($query);
        $categories = $db->loadObjectList();
        return $categories;
    }

    static function getPins($catid) {
//        $db = Jfactory::getDBO();
//        $pageno = 0;
//        $length = 4;
//        $query = "SELECT distinct(a.pin_id),a.pin_image,a.link_type,a.pin_url
//                          FROM #__pin_pins a
//                          INNER JOIN #__pin_user_settings d on a.pin_user_id=d.user_id
//                          INNER JOIN #__pin_boards e on e.board_id=a.pin_board_id
//                          INNER JOIN #__pin_categories b on e.board_category_id=b.category_id
//                          WHERE b.category_id = '$catid' 
//                          AND a.pin_repin_id='0' AND a.status='1' AND e.status=1
//                          ORDER BY  a.created_date desc
//                          LIMIT $pageno,$length";
//        $db->setQuery($query);
//        $pins = $db->loadObjectList();
//		if( count($pins) < 4 ) {
//			for( $i = count($pins); $i < 4; $i++ ) {
//				$object = new stdClass();
//				$object->pin_id = -1;
//				$object->pin_image = 'no-image.jpg';
//				$object->link_type = '';
//				$object->pin_url = '';
//				$pins[$i] = $object;
//			}
//		}
//		return $pins;

		$db = Jfactory::getDBO();
		$query = "SELECT pin_id,pin_url,pin_image,link_type 
                  FROM #__pin_pins 
                  WHERE status=1 AND pin_real_pin_id = 0 
                  AND pin_category_id=$catid 
                  ORDER BY pin_id DESC limit 0,8";
		$db->setQuery($query);
		$pins = $db->loadObjectList();

		$pinsCount = count($pins);

		for ($i = 7; $i >= $pinsCount; $i--) {
			$tmp = new stdClass();
			$tmp->pin_id = 0;
			$tmp->pin_url = '';
			$tmp->pin_image = '';
			$tmp->link_type = '';
			$pins[] = $tmp;
		}

		return $pins;
    }
}

?>