<?php
/**
 * Home Template
 *
 * This template is loaded when on the home/blog page.
 * Es mostren a la home els posts o pages que tinguin marcat el custom field Inici a 1 i s'ordenen pel valor del custom field Ordre, i sino per data publicació.
 * @package Hangar
 * @subpackage Template
 */
?>

<?php get_header(); ?>

	<?php if( is_home( )) : ?>
	<?php // show big widget area if homepage and if active ?>
    <aside class="hero">
      <?php if ( ! dynamic_sidebar( 'secondary-widget-area' ) ) : ?>
      <?php endif; // end secondary widget area ?>
    </aside>
  <?php endif; //end if is_home ?>

	<div id="main" class="maingran">
		<div id="content" class="indexall">

		<?php
			global $wpdb;  //seleccionem els posts pels camps creats Inici = 1 i ordenem els registres pel valor del camp Ordre (meta_key / meta_value)

		 $total = "
			SELECT *
			FROM $wpdb->posts wposts
			INNER JOIN $wpdb->postmeta m1 ON wposts.ID = m1.post_id
			INNER JOIN $wpdb->postmeta m2 ON wposts.ID = m2.post_id
			WHERE (wposts.post_type = 'post' OR wposts.post_type = 'page')
			AND m1.meta_key = 'Inici'
			AND m1.meta_value = '1'
			AND m2.meta_key = 'Ordre_inici'
			AND wposts.post_status = 'publish'
			GROUP BY ID
			ORDER BY CAST( m2.meta_value AS SIGNED ) ASC
		 ";

			$totalposts = $wpdb->get_results($total, OBJECT);
			$ppp = intval(get_query_var('posts_per_page')); //sçon 11 per la resta de pàgines índex però a la home en volem 12
			$ppp = intval(16);
			$wp_query->found_posts = count($totalposts);
			$wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
			$on_page = intval(get_query_var('paged'));
			if($on_page == 0){ $on_page = 1; }
			$offset = ($on_page-1) * $ppp;

		$wp_query->request = "SELECT *
				FROM $wpdb->posts wposts
				INNER JOIN $wpdb->postmeta m1 ON wposts.ID = m1.post_id
				INNER JOIN $wpdb->postmeta m2 ON wposts.ID = m2.post_id
				WHERE (wposts.post_type = 'post' OR wposts.post_type = 'page')
				AND m1.meta_key = 'Inici'
				AND m1.meta_value = '1'
				AND m2.meta_key = 'Ordre_inici'
				AND wposts.post_status = 'publish'
				GROUP BY ID
				ORDER BY CAST( m2.meta_value AS SIGNED ) ASC
			 LIMIT $ppp OFFSET $offset";

		$pageposts = $wpdb->get_results($wp_query->request, OBJECT);


		?>
<?php if ($pageposts): //amb els resultats trobats per la query distingim les categories de les pàgines pq s'han de pintar  diferent
	global $post;
	echo '<ul>';
	foreach ($pageposts as $post):
		setup_postdata($post);
		$category = get_the_category();
			if ($category) :
				$tipus = 'catt' ;
			else:
				if (is_page_child($post, 2)) :
					$tipus = 'art';	//artistes
				elseif (is_page_child($post, 136)) :
					$tipus = 'desc';  //descàrregues
				elseif (is_page_child($post, 74)) :
					$tipus = 'desc';  //equip
				else :
					$tipus = 'pag';
				endif;
			endif;

				 include(TEMPLATEPATH . "/caixes.php");

	 	endforeach; ?>
	</ul>


<?php wp_pagenavi(); ?>

		<?php else : ?>
        <h2 class="center"><?php _e( 'Not found.', 'hangar' ); ?></h2>
        <p class="center"><?php _e( "Sorry, but you are looking for something that isn't here.", 'hangar' ); ?></p>
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>
        <?php endif;
		wp_reset_postdata();
		?>

	</div> <!-- content -->
</div><!-- fi main -->

<?php get_footer(); ?>