<?php
/**
 * @class html
 * @brief Generate html code for pagination requests
 * @author Xavier Perrissoud
 * @date 30/08/2009
 * @version 0.1
 *
 * This class contains only static functions to generate cells content for the JavaScript controller requests.<br />
 * Each web page using the JavaScript pagination engine has its own function in this class.<br />
 * For simple cells content, you don't need to call this class : you can directly generate the text from the ajax.php file
*/
class html
{
	/**
	 * @brief Generate text / html for a MyHostings page cell
	 * @param cell_index : Zero-based cell index
	 * @param hosting_datas : Datas corresponding to the current hosting entry
	 * @return string with html code
	 *
	 * The first and the fifth column for the "MyHosting" page needs specials texts that have to be generated regarding to the corresponding hosting status.
	 * These links are generated with this method ($cell_index = 0 for the first, and $cell_index = 4 for the fifth).<br />
	 * The function will check if the current hosting is active. If so, the first column will contain a link to the hosting details. If the hosting is suspended, the fifth column will have a link to the support ticket creation page.
	*/
	static public function makeHtmlForMyHostings( $cell_index, $hosting_datas )
	{
		$result=null;
		switch( $cell_index )
		{
			case 0: // Hosting name
				$cell_text = $hosting_datas->full_name;
				if ( $hosting_datas->hosting_active == 'true' ) {
					$result = '<a href="' . TPL_HOST . 'hosting/hosting-' . $hosting_datas->id . '.xhtml">';
					$result .= $cell_text;
					$result .= '</a>';
				} else {
					$result = $cell_text;
				}
				break;
			case 4: // Hosting status
				if ( $hosting_datas->hosting_active == 'true' ) {
					$result = $hosting_datas->statusText;
				} else {
					$result = $hosting_datas->statusText . ' <a href="' . TPL_HOST . 'support-create.xhtml">';
					$result .= '<img src="' . TPL_HOST . 'image/icon/error.png" class="icon" alt="Support" /></a>';
				}
				break;
		}
		return $result;
	} // End of makeHtmlForMyHostings
	
	
	
	
	/**
	 * @brief Generate text / html for a Support page cell
	 * @param cell_index : Zero-based cell index
	 * @param ticket_datas : Datas corresponding to the current ticket entry
	 * @return string with html code
	 * 
	 * The first column for the "Support" page needs a link to get the ticket's detail page.
	 * This link can be generated with this method.
	*/
	static public function makeHtmlForSupport( $cell_index, $ticket_datas )
	{
		$result=null;
		switch( $cell_index )
		{
			case 0: // Open date
				$result = '<a href="' . TPL_HOST . 'support-show-' . $ticket_datas->ticket_id . '.xhtml">';
				$result .= $ticket_datas->label_text . '</a>';
				break;
		}
		return $result;
	}
	
	/**
	 * @brief Generate text / html for a ticket details page cell
	 * @param cell_index : Zero-based cell index
	 * @param reply_datas : Datas corresponding to the current reply entry
	 * @return string with html code
	 * 
	 * The first column for the "Ticket Details" page needs html code showing who made the current reply, and the date of this reply.
	*/
	static public function makeHtmlForTicketDetails( $cell_index, $reply_datas )
	{
		$result=null;
		switch( $cell_index )
		{
			case 0: // Author / Date
				$result = $reply_datas->msg_author . '<br />' . $reply_datas->msg_date;
				break;
		}
		return $result;
	}
	
	/**
	 * @brief Generate text / html for a Admin/Support page cell
	 * @param cell_index : Zero-based cell index
	 * @param ticket_datas : Datas corresponding to the current ticket entry
	 * @return string with html code
	*/
	static public function makeHtmlForAdminSupport( $cell_index, $ticket_datas )
	{
		$result=null;
		switch( $cell_index )
		{
			case 0: // Open date
				$result = '<a href="' . TPL_HOST . 'admin/support-show-' . $ticket_datas->ticket_id . '.xhtml">';
				$result .= $ticket_datas->open_date . '</a>';
				break;
		}
		return $result;
	}
	
	/**
	 * @brief Generate text / html for a Cron page cell
	 * @param cell_index : Zero-based cell index
	 * @param cron_datas : Datas corresponding to the current cron entry
	 * @return string with html code
	 *
	*/
	static public function makeHtmlForCron( $cell_index, $cron_datas )
	{
		$result = null;
		switch ( $cell_index )
		{
			case 0: // Buttons
				// Links for "Start/Stop"
				$on_success="function(response){getPage('tbl_cron', ".$cron_datas->current_page.", ".$cron_datas->max_pages.", 1);}";
				$on_failure="function(){ alert(critical_error); }";
				if ( $cron_datas->is_active == 'true' ) {
					$action = 'cronStopTask';
					$image = 'control_stop';
				} else {
					$action = 'cronStartTask';
					$image = 'control_play';
				}
				$start_stop="doSingleRequest('$action', $cron_datas->id, $on_success, $on_failure, 1);";
				$result = '<a href="javascript:;" onclick="'.$start_stop.'"><img src="'.TPL_HOST.'image/icon/'.$image.'.png" alt="" class="cron_tipsy" title="'.$cron_datas->start_stop_title.'" /></a>';
				// Links for "Delete"
				$delete="doSingleRequest('cronDeleteTask', $cron_datas->id, $on_success, $on_failure, 1);";
				$result .= '<a href="javascript:;" onclick="'.$delete.'"><img src="'.TPL_HOST.'image/icon/cross.png" alt="" class="cron_tipsy" title="'.$cron_datas->delete_title.'" /></a>';
			break;
			
			case 1: // Address
				$display_address = str_replace('http://', '', htmlentities($cron_datas));
				$display_address = str_replace('https://', '', $cron_datas);
				if ( strlen($display_address) >= 60 ) $display_address = substr($display_address, 0, 60) . "..." ;
				$result .= '<a href="javascript:;" onclick="goToCronUrl(\''.htmlentities($cron_datas).'\');">'.$display_address.'</a>';
			break;
		}
		return $result;
	}
	
	
	
}
?>
