<?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		tickets
 */

class Hook_rss_tickets
{
	/**
	 * Standard modular run function for RSS hooks.
	 *
	 * @param  string			A list of categories we accept from
	 * @param  TIME			Cutoff time, before which we do not show results from
	 * @param  string			Prefix that represents the template set we use
	 * @set    RSS_ ATOM_
	 * @param  string			The standard format of date to use for the syndication type represented in the prefix
	 * @param  integer		The maximum number of entries to return, ordering by date
	 * @return ?array			A pair: The main syndication section, and a title (NULL: error)
	 */
	function run($_filters,$cutoff,$prefix,$date_string,$max)
	{
		if (!addon_installed('tickets')) return NULL;

		if (!has_actual_page_access(get_member(),'tickets')) return NULL;

		if (is_guest()) return NULL;

		require_code('tickets');
		require_code('tickets2');

		$ticket_types=ocfilter_to_idlist_using_callback($_filters,'',NULL,NULL,NULL,NULL,false);
		if (count($ticket_types)!=0)
		{
			$rows=array();
			foreach ($ticket_types as $ticket_type)
			{
				if (!has_category_access(get_member(),'tickets',get_translated_text($ticket_type))) continue;
				$rows=array_merge($rows,get_tickets(get_member(),$ticket_type,false,false,true));
			}
		}
		else $rows=get_tickets(get_member(),NULL,false,false,true);

		require_code('feedback');

		$content=new ocp_tempcode();
		foreach ($rows as $i=>$row)
		{
			if ($i==$max) break;

			if ($row['lasttime']<$cutoff) continue;

			$ticket_id=extract_topic_identifier($row['description']);
			$ticket_type=$GLOBALS['SITE_DB']->query_value_null_ok('tickets','ticket_type',array('ticket_id'=>$ticket_id));

			$author=$row['firstusername'];
			$date=date($date_string,$row['firsttime']);
			$edit_date=date($date_string,$row['lasttime']);

			$title=xmlentities(escape_html($row['firsttitle']));
			$summary=xmlentities($row['firstpost']->evaluate());

			$category='';
			$category_raw='';
			if (!is_null($ticket_type))
			{
				$category=get_translated_text($ticket_type);
				$category_raw=strval($ticket_type);
			}

			$view_url=build_url(array('page'=>'tickets','type'=>'ticket','id'=>$ticket_id),get_module_zone('tickets'),NULL,false,false,true);

			if (($prefix=='RSS_') && (get_option('is_on_comments')=='1'))
			{
				$if_comments=do_template('RSS_ENTRY_COMMENTS',array('_GUID'=>'32c536b95de70994d0a13cfed18aa6ec','COMMENT_URL'=>$view_url,'ID'=>strval($ticket_id)));
			} else $if_comments=new ocp_tempcode();

			$content->attach(do_template($prefix.'ENTRY',array('VIEW_URL'=>$view_url,'SUMMARY'=>$summary,'EDIT_DATE'=>$edit_date,'IF_COMMENTS'=>$if_comments,'TITLE'=>$title,'CATEGORY_RAW'=>$category_raw,'CATEGORY'=>$category,'AUTHOR'=>$author,'ID'=>$ticket_id,'NEWS'=>'','DATE'=>$date)));
		}

		require_lang('tickets');
		return array($content,do_lang('SUPPORT_TICKETS'));
	}

}


