<?php
/**
 * <pre>
 * Invision Power Services
 * Database fields skin file
 * Last Updated: $Date: 2009-08-23 21:54:28 -0400 (Sun, 23 Aug 2009) $
 * </pre>
 *
 * @author 		$Author: bfarber $
 * @copyright	(c) 2001 - 2009 Invision Power Services, Inc.
 * @license		http://www.invisionpower.com/community/board/license.html
 * @package		IP.Content
 * @link		http://www.invisionpower.com
 * @since		2nd September 2009
 * @version		$Revision: 67 $
 */
 
class cp_skin_database_fields extends output
{

/**
 * Prevent our main destructor being called by this class
 *
 * @access	public
 * @return	void
 */
public function __destruct()
{
}

/**
 * Add or edit a field
 *
 * @access	public
 * @param	string		add|edit
 * @param	array		Database data
 * @param	array 		Field data
 * @param	array 		Field types
 * @param	array 		Validators
 * @return	string		HTML
 */
public function fieldForm( $type, $database, $field, $types, $validators )
{
$IPBHTML = "";
//--starthtml--//

$name			= $this->registry->output->formInput( 'field_name', $field['field_name'] );
$key			= $this->registry->output->formInput( 'field_key', $field['field_key'] );
$desc			= $this->registry->output->formTextarea( 'field_description', IPSText::br2nl( $field['field_description'] ), 40, 5, '', '', 'normal' );
$extra			= $this->registry->output->formTextarea( 'field_extra', $field['field_extra'], 40, 9, '', '', 'normal' );
$html			= $this->registry->output->formYesNo( 'field_html', $field['field_html'] );

$types			= $this->registry->output->formDropdown( 'field_type', $types, $field['field_type'] );

$required		= $this->registry->output->formYesNo( 'field_required', $field['field_required'] );
$userEdit		= $this->registry->output->formYesNo( 'field_user_editable', $field['field_user_editable'] );
$length			= $this->registry->output->formInput( 'field_max_length', $field['field_max_length'] );
$numeric		= $this->registry->output->formYesNo( 'field_is_numeric', $field['field_is_numeric'] );
$truncate	 	= $this->registry->output->formInput( 'field_truncate', $field['field_truncate'] );
$default		= $this->registry->output->formTextarea( 'field_default_value', $field['field_default_value'], 40, 9, '', '', 'normal' );
$dis_list		= $this->registry->output->formYesNo( 'field_display_listing', $field['field_display_listing'] );
$dis_dis		= $this->registry->output->formYesNo( 'field_display_display', $field['field_display_display'] );

$_available		= array(
						array( 'strtolower', $this->lang->words['ffo__strtolower'] ),
						array( 'strtoupper', $this->lang->words['ffo__strtoupper'] ),
						array( 'ucfirst', $this->lang->words['ffo__ucfirst'] ),
						array( 'ucwords', $this->lang->words['ffo__ucwords'] ),
						array( 'punct', $this->lang->words['ffo__removeexcesspunct'] ),
						array( 'numerical', $this->lang->words['ffo__numericalformat'] ),
						);
$formatopts		= $this->registry->output->formMultiDropdown( 'field_format_opts[]', $_available, explode( ',', $field['field_format_opts'] ) );

$_ex			= explode( ',', $field['field_extra'] );
$fieldtype	 	= $this->registry->output->formDropdown( 'field_rel_type', array( array( 'dropdown', $this->lang->words['relfield_type_d'] ), array( 'multiselect', $this->lang->words['relfield_type_m'] ) ), $_ex[2] );

$validatorOptions	= array( array( 'none', $this->lang->words['validator__none'] ) );
$_validator			= explode( ';_;', $field['field_validator'] );

foreach( $validators as $_valid )
{
	$validatorOptions[]	= array( $_valid['key'], $_valid['language'] );
}

$fieldvalid	 		= $this->registry->output->formDropdown( 'field_validator', $validatorOptions, $_validator[0] );
$fieldvalidCustom	= $this->registry->output->formInput( 'field_validator_custom', htmlspecialchars( $_validator[1], ENT_QUOTES ) );
$fieldvalidError	= $this->registry->output->formInput( 'field_validator_error', $_validator[2] );

$text			= $type == 'edit' ? $this->lang->words['editing_field_title'] . $field['field_name'] : $this->lang->words['adding_field_title'];
$action			= $type == 'edit' ? 'doEdit' : 'doAdd';

$IPBHTML .= <<<HTML
<div class='section_title'>
	<h2>{$text}</h2>
	<ul class='context_menu'>
		<li>
			<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=fields&amp;id={$database['database_id']}' title='{$this->lang->words['cancel_edit_alt']}'>
				<img src='{$this->settings['skin_acp_url']}/_newimages/icons/delete.png' alt='{$this->lang->words['icon']}' />
				{$this->lang->words['button__cancel']}
			</a>
		</li>
	</ul>
</div>

<script type='text/javascript' src='{$this->settings['js_app_url']}acp.ccs.js'></script>
<script type='text/javascript'>
	var fieldDescriptions	= new Hash();
	fieldDescriptions.set( 'standard', "{$this->lang->words['fieldform__extra_desc']}" );
	fieldDescriptions.set( 'date', "{$this->lang->words['fieldform__extra_desc_date']}" );
	fieldDescriptions.set( 'upload', "{$this->lang->words['fieldform__extra_desc_upload']}" );

</script>
<form action='{$this->settings['base_url']}{$this->form_code}&amp;do={$action}&amp;id={$database['database_id']}' method='post' id='adminform' name='adform'>
<input type='hidden' name='field' id='field_edit_id' value='{$field['field_id']}' />
<input type='hidden' name='field_key' value='{$field['field_key']}' />
<div class='acp-box'>
	<h2 class='tablesubheader' style='font-weight: bold;'>{$this->lang->words['edit_field_details']}</h2>
	<ul class='acp-form alternate_rows'>
		<li>
			<label>{$this->lang->words['fieldform__name']}</label>
			{$name}
		</li>
		<!--<li>
			<label>{$this->lang->words['fieldform__key']} <span class='desctext'>{$this->lang->words['fieldform__key_desc']}</span></label>
			{$key}
		</li>-->
		<li>
			<label>{$this->lang->words['fieldform__desc']}</label>
			{$desc}
		</li>
		<li>
			<label>{$this->lang->words['fieldform__type']}</label>
			{$types}
		</li>
		<li>
			<label>{$this->lang->words['fieldform__useredit']} <span class='desctext'>{$this->lang->words['fieldform__useredit_desc']}</span></label>
			{$userEdit}
		</li>
		<li>
			<label>{$this->lang->words['fieldform__numeric']} <span class='desctext'>{$this->lang->words['fieldform__numeric_desc']}</span></label>
			{$numeric}
		</li>
		<li>
			<label>{$this->lang->words['fieldform__default']} <span class='desctext'>{$this->lang->words['fieldform__default_desc']}</span></label>
			{$default}
		</li>
		<li class='field_type_wrapper' id='field_validator_li'>
			<label>{$this->lang->words['fieldform__validator']} <span class='desctext'>{$this->lang->words['fieldform__validator_desc']}</span></label>
			{$fieldvalid}<br />
			<div id='validator_custom'>
				<strong>{$this->lang->words['fieldform_validator_custom']}</strong> {$fieldvalidCustom} <br />
				<strong>{$this->lang->words['fieldform_validator_error']}</strong> {$fieldvalidError}
			</div>
		</li>
		<li>
			<label>{$this->lang->words['fieldform__dislist']} <span class='desctext'>{$this->lang->words['fieldform__dislist_desc']}</span></label>
			{$dis_list}
		</li>
		<li>
			<label>{$this->lang->words['fieldform__disdisplay']} <span class='desctext'>{$this->lang->words['fieldform__disdisplay_desc']}</span></label>
			{$dis_dis}
		</li>
		<li class='field_type_wrapper' id='field_html_li'>
			<label>{$this->lang->words['fieldform__html']}  <span class='desctext'>{$this->lang->words['fieldform__html_desc']}</span></label>
			{$html}
		</li>
		<li class='field_type_wrapper' id='field_required_li'>
			<label>{$this->lang->words['fieldform__req']}</label>
			{$required}
		</li>
		<li class='field_type_wrapper' id='field_length_li'>
			<label>{$this->lang->words['fieldform__length']} <span class='desctext'>{$this->lang->words['fieldform__length_desc']}</span></label>
			{$length}
		</li>
		<li class='field_type_wrapper' id='field_extra_li'>
			<label>{$this->lang->words['fieldform__extra']} <span class='desctext' id='extra_options_text'>{$this->lang->words['fieldform__extra_desc']}</span></label>
			{$extra}
		</li>
		<li class='field_type_wrapper' id='field_formatopts_li'>
			<label>{$this->lang->words['fieldform__formatopts']}  <span class='desctext'>{$this->lang->words['fieldform__formatopts_desc']}</span></label>
			{$formatopts}
		</li>
		<li class='field_type_wrapper' id='field_truncate_li'>
			<label>{$this->lang->words['fieldform__truncate']} <span class='desctext'>{$this->lang->words['fieldform__truncate_desc']}</span></label>
			{$truncate}
		</li>
		<li class='field_type_wrapper' id='field_reltype_li'>
			<label>{$this->lang->words['fieldform__fieldtype']}</label>
			{$fieldtype}
		</li>
		
		<li class='field_type_wrapper' id='field_databases_li'>
			<label>{$this->lang->words['fieldform__dbs']} <span class='desctext' id='extra_options_text'>{$this->lang->words['fieldform__dbs_desc']}</span></label>
			<select id='field_databases_select' name='field_database'></select>
		</li>
		<li class='field_type_wrapper' id='field_fields_li'>
			<label>{$this->lang->words['fieldform__flds']} <span class='desctext' id='extra_options_text'>{$this->lang->words['fieldform__flds_desc']}</span></label>
			<select id='field_fields_select' name='field_fields'></select>
		</li>
	</ul>

	<div class="acp-actionbar">
		<input type='submit' value=' {$this->lang->words['button__save']} ' class="button primary" />
	</div>
</div>	
</form>
HTML;

//--endhtml--//
return $IPBHTML;
}

/**
 * Show the list of fields within a database
 *
 * @access	public
 * @param	array 		Database
 * @param	array 		Fields
 * @return	string		HTML
 */
public function fields( $database, $fields )
{
$IPBHTML = "";
//--starthtml--//

$IPBHTML .= <<<HTML
<div class='section_title'>
	<h2>{$this->lang->words['dbfields_title']} {$database['database_name']}</h2>
	<ul class='context_menu'>
		<li>
			<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=fields&amp;do=add&amp;id={$database['database_id']}' title='{$this->lang->words['add_field_alt']}'>
				<img src='{$this->settings['skin_acp_url']}/_newimages/icons/add.png' alt='{$this->lang->words['icon']}' />
				{$this->lang->words['add_field_button']}
			</a>
		</li>
		<li>
			<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=records&amp;id={$database['database_id']}' title='{$this->lang->words['switch_manage_records']}'>
				<img src='{$this->settings['skin_acp_url']}/_newimages/icons/table_add.png' alt='{$this->lang->words['icon']}' />
				{$this->lang->words['switch_manage_records']}
			</a>
		</li>
		<li>
			<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;id={$database['database_id']}' title='{$this->lang->words['switch_manage_cats']}'>
				<img src='{$this->settings['skin_acp_url']}/_newimages/icons/folder.png' alt='{$this->lang->words['icon']}' />
				{$this->lang->words['switch_manage_cats']}
			</a>
		</li>
	</ul>
</div>
HTML;

$IPBHTML .= <<<HTML
<script type='text/javascript' src='{$this->settings['js_app_url']}acp.ccs.js'></script>
HTML;

if( count($fields) )
{
	$IPBHTML .= <<<HTML
<script type="text/javascript">
window.onload = function() {
	Sortable.create( 'sortable_handle', { revert: true, format: 'field_([0-9]+)', onUpdate: dropItLikeItsHot, handle: 'draghandle' } );
};

dropItLikeItsHot = function( draggableObject, mouseObject )
{
	var options = {
					method : 'post',
					parameters : Sortable.serialize( 'sortable_handle', { tag: 'li', name: 'field' } )
				};
 
	new Ajax.Request( "{$this->settings['base_url']}&{$this->form_code_js}&do=reorder&md5check={$this->registry->adminFunctions->getSecurityKey()}".replace( /&amp;/g, '&' ), options );

	return false;
};
</script>
HTML;
}

$IPBHTML .= <<<HTML
<div class='acp-box'>
	<h3>{$this->lang->words['all_created_fields_t']}</h3>
	<ul id='sortable_handle' class='alternate_rows adv_controls'>
		<li>
			<table style='width:100%;'>
				<tr>
					<th style='width: 56%; text-align: left;'>{$this->lang->words['field_name_th']}</th>
					<!--<th style='width: 15%'>{$this->lang->words['field_key_th']}</th>-->
					<th style='width: 18%; text-align: left;'>{$this->lang->words['field_type_th']}</th>
					<th style='width: 10%; text-align: left;'>{$this->lang->words['field_required_th']}</th>
					<th style='width: 10%; text-align: left;'>{$this->lang->words['field_useredit_th']}</th>
					<th style='width: 6%; text-align: left;'>&nbsp;</th>
				</tr>
			</table>
		</li>
HTML;

if( !count( $fields ) )
{
	$IPBHTML .= <<<HTML
		<li>
			<table style='width:100%;'>
				<tr>
					<td style='width:100%;'>
						<div style='padding: 20px; text-align: center'>
							<em>{$this->lang->words['no_fields_created_yet']} <a href='{$this->settings['base_url']}&amp;module=databases&amp;section=fields&amp;do=add&amp;id={$database['database_id']}'>{$this->lang->words['create_field_now']}</a></em>
						</div>
					</td>
				</tr>
			</table>
		</li>
HTML;
}
else
{
	foreach( $fields as $field )
	{
		$name	= "<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=fields&amp;do=edit&amp;id={$database['database_id']}&amp;field={$field['field_id']}'><strong>{$field['field_name']}</strong></a>";
		
		if( $field['field_description'] )
		{
			$name	.= "<div class='desctext'>{$field['field_description']}</div>";
		}
		
		$_required	= $field['field_required'] ? 'tick.png' : 'cross.png';
		$_useredit	= $field['field_user_editable'] ? 'tick.png' : 'cross.png';
		$_type		= $this->lang->words['field_type__' . $field['field_type'] ];
		
		$IPBHTML .= <<<HTML
		<li id='field_{$field['field_id']}'>
			<table style='width:100%;'>
				<tr class='control_row'>
					<td style='width: 3%'><div class='draghandle'><img src='{$this->settings['skin_acp_url']}/_newimages/drag_icon.png' alt=''></div></td>
					<td style='width: 55%'>{$name}</td>
					<!--<td style='width: 15%'>{$field['field_key']}</td>-->
					<td style='width: 20%'>{$_type}</td>
					<td style='width: 10%'><img src='{$this->settings['skin_acp_url']}/_newimages/icons/{$_required}' alt='{$this->lang->words['icon']}' /></td>
					<td style='width: 10%'><img src='{$this->settings['skin_acp_url']}/_newimages/icons/{$_useredit}' alt='{$this->lang->words['icon']}' /></td>
					<td style='width: 2%'>		
						<ul class='controls no_extra'>
							<li class='left'>
								<a class='edit' href='{$this->settings['base_url']}&amp;module=databases&amp;section=fields&amp;do=edit&amp;id={$database['database_id']}&amp;field={$field['field_id']}'>{$this->lang->words['edit_field_menu']}</a>
							</li>
							<li class='right ipbmenu' id='menu_page_root'>
								<a class='delete' href='#' onclick="return acp.confirmDelete( '{$this->settings['base_url']}&amp;module=databases&amp;section=fields&amp;do=delete&amp;id={$database['database_id']}&amp;field={$field['field_id']}' );">{$this->lang->words['delete_field_menu']}</a>
							</li>
						</ul>
					</td>
				</tr>
			</table>
		</li>
HTML;
	}
}

$IPBHTML .= <<<HTML
	</ul>
</div>
HTML;
//--endhtml--//
return $IPBHTML;
}

}