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

/**
 * Add a flagrant text message.
 *
 * @param  SHORT_TEXT	The message
 * @param  integer		The number of days to display for
 * @param  LONG_TEXT		Notes
 * @param  BINARY			Whether the message is for immediate use
 * @return AUTO_LINK		The ID of the newly added message
 */
function add_flagrant($message,$days,$notes,$validated)
{
	$order_time=time();
	$id=$GLOBALS['SITE_DB']->query_insert('text',array('notes'=>$notes,'activation_time'=>NULL,'active_now'=>0,'the_message'=>insert_lang_comcode($message,2),'days'=>$days,'order_time'=>$order_time,'user_id'=>get_member()),true);
	log_it('ADD_FLAGRANT',strval($id),$message);
	if ($validated==1) choose_flagrant($id);
	return $id;
}

/**
 * Edit a flagrant text message.
 *
 * @param  AUTO_LINK		The ID of what to edit
 * @param  SHORT_TEXT	The message
 * @param  LONG_TEXT		Notes
 * @param  BINARY			Whether the message is the active message
 */
function edit_flagrant($id,$message,$notes,$validated)
{
	$_message=$GLOBALS['SITE_DB']->query_value('text','the_message',array('id'=>$id));
	log_it('EDIT_FLAGRANT',strval($id),$message);
	$GLOBALS['SITE_DB']->query_update('text',array('notes'=>$notes,'the_message'=>lang_remap_comcode($_message,$message),'active_now'=>$validated),array('id'=>$id),'',1);
	if ($validated==1) choose_flagrant($id); else persistent_cache_delete('FLAGRANT');
}

/**
 * Delete a flagrant text message.
 *
 * @param  AUTO_LINK		The ID of the flagrant text message to delete
 */
function delete_flagrant($id)
{
	$message=$GLOBALS['SITE_DB']->query_value('text','the_message',array('id'=>$id));
	$_message=get_translated_text($message);
	log_it('DELETE_FLAGRANT',strval($id),$_message);
	$GLOBALS['SITE_DB']->query_delete('text',array('id'=>$id),'',1);
	delete_lang($message);

	persistent_cache_delete('FLAGRANT'); // In case it was active
}

/**
 * Choose a flagrant text message.
 *
 * @param  AUTO_LINK		The ID of the flagrant text message to choose
 */
function choose_flagrant($id)
{
	$message=$GLOBALS['SITE_DB']->query_value('text','the_message',array('id'=>$id));
	$message=get_translated_text($message);
	log_it('CHOOSE_FLAGRANT',strval($id),$message);
	$GLOBALS['SITE_DB']->query_update('text',array('active_now'=>0));
	$GLOBALS['SITE_DB']->query_update('text',array('activation_time'=>time(),'active_now'=>1),array('id'=>$id),'',1);

	persistent_cache_delete('FLAGRANT');
}

/**
 * Get a list of flagrant text messages to choose from.
 *
 * @return tempcode		The list of flagrant text messages
 */
function nice_get_flagrant()
{
	$rows=$GLOBALS['SITE_DB']->query_select('text',array('*'),NULL,'ORDER BY order_time ASC');
	$time=$GLOBALS['SITE_DB']->query_value_null_ok('text','activation_time',array('active_now'=>1));
	$out=new ocp_tempcode();
	foreach ($rows as $row)
	{
		$selected=false;
		if ($row['activation_time']<$time)
		{
			$status=do_lang_tempcode('USED_PREVIOUSLY');
		}
		elseif (is_null($row['activation_time']))
		{
			$status=do_lang_tempcode('NOT_USED_PREVIOUSLY');
		} else
		{
			$status=do_lang_tempcode('CURRENT');
			$selected=true;
		}
		$message=get_translated_text($row['the_message']);
		$text=do_template('FLAGRANT_STORE_LIST_LINE',array('_GUID'=>'e4a5d54fa6cdc7848bd95bc017c60469','MESSAGE'=>$message,'STATUS'=>$status));
		$out->attach(form_input_list_entry(strval($row['id']),$selected,protect_from_escaping($text->evaluate())));
	}
	return $out;
}

