<?php /*

 ocPortal
 Copyright (c) ocProducts, 2004-2012

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license		http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright	ocProducts Ltd
 * @package		galleries
 */

class Block_main_recent_galleries
{

	/**
	 * Standard modular info function.
	 *
	 * @return ?array	Map of module info (NULL: module is disabled).
	 */
	function info()
	{
		$info=array();
		$info['author']='Chris Graham';
		$info['organisation']='ocProducts';
		$info['hacked_by']=NULL;
		$info['hack_version']=NULL;
		$info['version']=2;
		$info['locked']=false;
		$info['parameters']=array('param','zone');
		return $info;
	}

	/**
	 * Standard modular cache function.
	 *
	 * @return ?array	Map of cache details (cache_on and ttl) (NULL: module is disabled).
	 */
	function cacheing_environment()
	{
		$info=array();
		$info['cache_on']='array(array_key_exists(\'zone\',$map)?$map[\'zone\']:get_module_zone(\'galleries\'),array_key_exists(\'param\',$map)?intval($map[\'param\']):10)';
		$info['ttl']=60;
		return $info;
	}

	/**
	 * Standard modular run function.
	 *
	 * @param  array		A map of parameters.
	 * @return tempcode	The result of execution.
	 */
	function run($map)
	{
		require_code('galleries');
		require_lang('galleries');

		$number=array_key_exists('param',$map)?intval($map['param']):10;
		$zone=array_key_exists('zone',$map)?$map['zone']:get_module_zone('galleries');

		$out=new ocp_tempcode();

		//$order_by='g.add_date DESC';
		$order_by='GREATEST(IFNULL((SELECT MAX(add_date) FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'images i WHERE g.name=i.cat),0),IFNULL((SELECT MAX(add_date) FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'videos v WHERE g.name=v.cat),0)) DESC';

		$sql='SELECT g.* FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'galleries g WHERE ';
		$sql.='name NOT LIKE \''.db_encode_like('download\_%').'\'';
		$sql.=' AND (';
		$sql.='EXISTS(SELECT * FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'images i WHERE g.name=i.cat)';
		$sql.=' OR ';
		$sql.='EXISTS(SELECT * FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'videos v WHERE g.name=v.cat)';
		$sql.=')';
		$sql.=' ORDER BY '.$order_by;
		$rows=$GLOBALS['SITE_DB']->query($sql,$number);

		if (count($rows)==0)
		{
			return do_template('BLOCK_NO_ENTRIES',array('_GUID'=>'e6d16782037d35949646f95656382783','HIGH'=>false,'TITLE'=>do_lang_tempcode('RECENT',make_string_tempcode(integer_format($number)),do_lang_tempcode('GALLERIES')),'MESSAGE'=>do_lang_tempcode('NO_CATEGORIES'),'ADD_NAME'=>'','SUBMIT_URL'=>''));
		} else
		{
			foreach ($rows as $row)
			{
				$out->attach(do_template('GALLERY_SUBGALLERY_WRAP',array('CONTENT'=>show_gallery_box($row,'root',true,$zone,false,true))));
			}
		}

		return do_template('BLOCK_MAIN_RECENT_GALLERIES',array('_GUID'=>'a7fca7d625a3d3af362735dad670f6eb','CONTENT'=>$out,'NUMBER'=>integer_format($number)));
	}

}


