<?php
/**
 * <pre>
 * Invision Power Services
 * Database categories 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_categories extends output
{

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

/**
 * Add or edit a category
 *
 * @access	public
 * @param	string		add|edit
 * @param	array		Database data
 * @param	array 		Category data
 * @param	array 		Categories
 * @param	string		Select options
 * @return	string		HTML
 */
public function categoryForm( $type, $database, $category, $categories, $selectOpts='' )
{
$IPBHTML = "";
//--starthtml--//

//-----------------------------------------
// Mix in our submitted config
//-----------------------------------------

if( is_array($_POST) AND count($_POST) )
{
	$category		= array_merge( $category, $_POST );
}

$name			= $this->registry->output->formInput( 'category_name', $category['category_name'] );
$furl			= $this->registry->output->formInput( 'category_furl_name', $category['category_furl_name'] );
$desc			= $this->registry->output->formTextarea( 'category_description', $category['category_description'], 40, 5, '', '', 'normal' );
$show			= $this->registry->output->formYesNo( 'category_show_records', $category['category_show_records'] );

$metak			= $this->registry->output->formTextarea( 'category_meta_keywords', $category['category_meta_keywords'], 40, 5, '', '', 'normal' );
$metad			= $this->registry->output->formTextarea( 'category_meta_description', $category['category_meta_description'], 40, 5, '', '', 'normal' );

$text			= $type == 'edit' ? $this->lang->words['editing_cat_title'] . $category['category_name'] : $this->lang->words['adding_cat_title'];
$action			= $type == 'edit' ? 'doEdit' : 'doAdd';

//-----------------------------------------
// Forum posting options
//-----------------------------------------

require_once( IPSLib::getAppDir( 'forums' ) . '/sources/classes/forums/class_forums.php' );
require_once( IPSLib::getAppDir( 'forums' ) . '/sources/classes/forums/admin_forum_functions.php' );

$aff = new admin_forum_functions( $this->registry );
$aff->forumsInit();
$_forums = $aff->adForumsForumList(1);

$foverride		= $this->registry->output->formYesNo( 'category_forum_override', intval($category['category_forum_override']), 'category_forum_override', array( 'yes' => "onclick=\"\$('ccs-cat-forum').show();\"", 'no' => "onclick=\"\$('ccs-cat-forum').hide();\"" ) );
	
$fsynch			= $this->registry->output->formYesNo( 'category_forum_record', $category['category_forum_record'] );
$fcomments		= $this->registry->output->formYesNo( 'category_forum_comments', $category['category_forum_comments'] );
$fdelete		= $this->registry->output->formYesNo( 'category_forum_delete', $category['category_forum_delete'] );
$fforum			= $this->registry->output->formDropdown( 'category_forum_forum', $_forums, $category['category_forum_forum'] );
$fprefix		= $this->registry->output->formInput( 'category_forum_prefix', $category['category_forum_prefix'] );
$fsuffix		= $this->registry->output->formInput( 'category_forum_suffix', $category['category_forum_suffix'] );

//-----------------------------------------
// Permissions
//-----------------------------------------

require_once( IPS_ROOT_PATH . 'sources/classes/class_public_permissions.php' );
$permissions = new classPublicPermissions( ipsRegistry::instance() );
$matrix			= $permissions->adminPermMatrix( 'cat', $category );

$override		= $this->registry->output->formYesNo( 'category_has_perms', intval($category['category_has_perms']), 'category_has_perms', array( 'yes' => "onclick=\"\$('ccs-perm-matrix').show();\"", 'no' => "onclick=\"\$('ccs-perm-matrix').hide();\"" ) );

$_rss			= explode( ';', $category['category_rss'] );
$rssEnable		= $this->registry->output->formYesNo( 'category_rss_enable', intval($_rss[0]) );
$rssRecache		= $this->registry->output->formInput( 'category_rss_recache', isset($_rss[1]) ? intval($_rss[1]) : 12 );
$rssTotal		= $this->registry->output->formInput( 'category_rss_count', isset($_rss[2]) ? intval($_rss[2]) : 20 );
$rssExclude		= $this->registry->output->formYesNo( 'category_rss_exclude', intval($_rss[3]) );

$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=categories&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>
<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='category' value='{$category['category_id']}' />

<ul id='tabstrip_ccs_page' class='tab_bar no_title'>
	<li id='tabtab-CCSPAGE|1' class=''>{$this->lang->words['tab__cat_main']}</li>
	<li id='tabtab-CCSPAGE|2' class=''>{$this->lang->words['tab__cat_forum']}</li>
	<li id='tabtab-CCSPAGE|3' class=''>{$this->lang->words['tab__cat_perms']}</li>
</ul>

<div class='acp-box'>
	<div id='tabpane-CCSPAGE|1'>
		<h2 class='tablesubheader' style='font-weight: bold;'>{$this->lang->words['edit_cat_details']}</h2>
		<ul class='acp-form alternate_rows'>
			<li>
				<label>{$this->lang->words['catform__name']}</label>
				{$name}
			</li>
			<li>
				<label>{$this->lang->words['catform__furl']} <span class='desctext'>{$this->lang->words['catform__furl_desc']}</span></label>
				{$furl}
			</li>
			<li>
				<label>{$this->lang->words['catform__desc']}</label>
				{$desc}
			</li>
			<li>
				<label>{$this->lang->words['catform__parent']} <span class='desctext'>{$this->lang->words['catform__p_desc']}</span></label>
				<select name='category_parent_id' id='category_parent_id'>
					<option value='0'>{$this->lang->words['select_cat_one']}</option>
					{$selectOpts}
				</select>
			</li>
			<li>
				<label>{$this->lang->words['catform__records']}</label>
				{$show}
			</li>
			<li>
				<label>{$this->lang->words['dbform__rsscat']} <span class='desctext'>{$this->lang->words['dbform__rsscat_desc']}</span></label>
				{$rssEnable}
			</li>
			<li>
				<label>{$this->lang->words['dbform__rssccat']} <span class='desctext'>{$this->lang->words['dbform__rssccat_desc']}</span></label>
				{$rssTotal}
			</li>
			<li>
				<label>{$this->lang->words['dbform__rssrcat']} <span class='desctext'>{$this->lang->words['dbform__rssrcat_desc']}</span></label>
				{$rssRecache}
			</li>
			<li>
				<label>{$this->lang->words['dbform__rssecat']} <span class='desctext'>{$this->lang->words['dbform__rssecat_desc']}</span></label>
				{$rssExclude}
			</li>
			<li>
				<label>{$this->lang->words['dbform__meta_keywords']}</label>
				{$metak}
			</li>
			<li>
				<label>{$this->lang->words['dbform__meta_description']}</label>
				{$metad}
			</li>
		</ul>
	</div>
	
	<div id='tabpane-CCSPAGE|2'>
		<h2 class='tablesubheader' style='font-weight: bold;'>{$this->lang->words['db_forum_options']}</h2>
		<ul class='acp-form alternate_rows'>
			<li>
				<label>{$this->lang->words['catform__foverride']}</label>
				{$foverride}
			</li>
		</ul>
		<div id='ccs-cat-forum'>
			<ul class='acp-form alternate_rows'>
				<li>
					<label>{$this->lang->words['dbform__fsynch']} <span class='desctext'>{$this->lang->words['dbform__fsynch_desc']}</span></label>
					{$fsynch}
				</li>
				<li>
					<label>{$this->lang->words['dbform__fcomments']} <span class='desctext'>{$this->lang->words['dbform__fcomments_desc']}</span></label>
					{$fcomments}
				</li>
				<li>
					<label>{$this->lang->words['dbform__fforum']} <span class='desctext'>{$this->lang->words['dbform__fforum_desc']}</span></label>
					{$fforum}
				</li>
				<li>
					<label>{$this->lang->words['dbform__fprefix']} <span class='desctext'>{$this->lang->words['dbform__fprefix_desc']}</span></label>
					{$fprefix}
				</li>
				<li>
					<label>{$this->lang->words['dbform__fsuffix']} <span class='desctext'>{$this->lang->words['dbform__fsuffix_desc']}</span></label>
					{$fsuffix}
				</li>
				<li>
					<label>{$this->lang->words['dbform__fdelete']} <span class='desctext'>{$this->lang->words['dbform__fdelete_desc']}</span></label>
					{$fdelete}
				</li>
			</ul>
		</div>
	</div>
	
	<div id='tabpane-CCSPAGE|3'>
		<h2 class='tablesubheader' style='font-weight: bold;'>{$this->lang->words['edit_cat_permissions_tab']}</h2>
		<ul class='acp-form alternate_rows'>
			<li>
				<label>{$this->lang->words['catform__override']}</label>
				{$override}
			</li>
		</ul>
		<div style='padding-top:10px' id='ccs-perm-matrix'>
			{$matrix}
		</div>
	</div>

	<div class="acp-actionbar">
		<input type='submit' value=' {$this->lang->words['button__save']} ' class="button primary" />
	</div>
</div>	
</form>
<script type="text/javascript">
//<![CDATA[
document.observe("dom:loaded",function() 
{
	ipbAcpTabStrips.register('tabstrip_ccs_page');

	if( !$('category_has_perms_yes').checked )
	{
		$('ccs-perm-matrix').hide();
	}

	if( !$('category_forum_override_yes').checked )
	{
		$('ccs-cat-forum').hide();
	}
});
var noShowAlert	= true;
 //]]>
</script>
HTML;

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

/**
 * Show the form to move records when deleting a cat
 *
 * @access	public
 * @param	array 		Database
 * @param	array 		Category
 * @param	array 		Categories
 * @param	string		Select menu
 * @return	string		HTML
 */
public function categoryDeleteForm( $database, $category, $categories=array(), $selectOpts='' )
{
$IPBHTML = "";
//--endhtml--//

$IPBHTML .= <<<HTML
<div class='section_title'>
	<h2>{$this->lang->words['confirm_cat_delete']}</h2>
</div>
<script type='text/javascript' src='{$this->settings['js_app_url']}acp.ccs.js'></script>

<form action='{$this->settings['base_url']}{$this->form_code}&amp;do=delete' method='post'>
<input type='hidden' name='category' value='{$category['category_id']}' />
<div class='acp-box'>
	<h3>{$this->lang->words['confirm_to_ccontinue_d']}</h3>
	<ul class='acp-form alternate_rows'>
		<li>
			<label>{$this->lang->words['cat_to_mvoe_to']}</label>
			<select name='move_to' id='move_to'>{$selectOpts}</select>
		</li>
	</ul>
	<div class="acp-actionbar">
		<input type='submit' value=' {$this->lang->words['button__notd']} ' class="button primary" />
		<input type='button' value=' {$this->lang->words['button__canceld']} ' class="realbutton redbutton" onclick='return acp.redirect("{$this->settings['base_url']}{$this->form_code}");' />
	</div>
</div>	
</form>

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

/**
 * Show the list of categories within a database
 *
 * @access	public
 * @param	array 		Database
 * @param	array 		Categories
 * @param	array 		Parent data (if viewing subcats)
 * @return	string		HTML
 */
public function categories( $database, $categories, $parent=array() )
{
$IPBHTML = "";
//--starthtml--//

$IPBHTML .= <<<HTML
<div class='section_title'>
	<h2>{$this->lang->words['dbcategoriess_title']} {$database['database_name']}</h2>
	<ul class='context_menu'>
		<li>
			<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=add&amp;id={$database['database_id']}' title='{$this->lang->words['add_cat_alt']}'>
				<img src='{$this->settings['skin_acp_url']}/_newimages/icons/add.png' alt='{$this->lang->words['icon']}' />
				{$this->lang->words['add_category_button']}
			</a>
		</li>
		<li>
			<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=recache&amp;id={$database['database_id']}' title='{$this->lang->words['recache_cats_button']}'>
				<img src='{$this->settings['skin_acp_url']}/_newimages/icons/arrow_refresh.png' alt='{$this->lang->words['icon']}' />
				{$this->lang->words['recache_cats_button']}
			</a>
		</li>
		<li>
			<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=fields&amp;id={$database['database_id']}' title='{$this->lang->words['switch_manage_fields']}'>
				<img src='{$this->settings['skin_acp_url']}/_newimages/icons/layout_content.png' alt='{$this->lang->words['icon']}' />
				{$this->lang->words['switch_manage_fields']}
			</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>
	</ul>
</div>
HTML;

if( $this->request['parent'] )
{
	$this->lang->words['all_created_cats_t']	= $this->lang->words['all_created_cats_t1'] . $parent['category_name'];
}

$IPBHTML .= <<<HTML
<script type='text/javascript' src='{$this->settings['js_app_url']}acp.ccs.js'></script>
<script type="text/javascript">
window.onload = function() {
	Sortable.create( 'sortable_handle', { revert: true, format: 'category_([0-9]+)', onUpdate: dropItLikeItsHot, handle: 'draghandle' } );
};

dropItLikeItsHot = function( draggableObject, mouseObject )
{
	var options = {
					method : 'post',
					parameters : Sortable.serialize( 'sortable_handle', { tag: 'li', name: 'category' } )
				};
 
	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>
<div class='acp-box adv_controls'>
	<h3>{$this->lang->words['all_created_cats_t']}</h3>
	<ul id='sortable_handle' class='alternate_rows'>
		<li>
			<table style='width:100%;'>
				<tr>
					<th style='width: 75%'>{$this->lang->words['cat_name_th']}</th>
					<th style='width: 20%'>{$this->lang->words['cat_records_th']}</th>
					<th style='width: 5%'>&nbsp;</th>
				</tr>
			</table>
		</li>
HTML;

if( !count( $categories ) )
{
	$IPBHTML .= <<<HTML
		<li>
			<table style='width:100%;'>
				<tr>
					<td style='width:100%;'>
						<div style='padding: 20px; text-align: center'>
							<em>{$this->lang->words['no_cats_created_yet']} <a href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=add&amp;id={$database['database_id']}'>{$this->lang->words['create_cat_now']}</a></em>
						</div>
					</td>
				</tr>
			</table>
		</li>
HTML;
}
else
{
	foreach( $categories as $category )
	{
		$name	= "<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=edit&amp;id={$database['database_id']}&amp;category={$category['category_id']}'><strong>{$category['category_name']}</strong></a>";
		
		if( $category['category_description'] )
		{
			$name	.= "<div class='desctext'>{$category['category_description']}</div>";
		}
		
		$children	= '';
		
		if( count($category['children'] ) )
		{
			$name	.= "<div class='desctext' style='margin-top: 20px;'><strong>{$this->lang->words['subcat_prefix']}</strong> ";
			$_c		= array();
			
			foreach( $category['children'] as $child )
			{
				$_c[]	= "<a href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=list&amp;id={$database['database_id']}&amp;parent={$child['category_parent_id']}'>{$child['category_name']}</a>";
			}
			
			$name	.= implode( ', ', $_c );
			$name	.= "</div>";
		}

		$IPBHTML .= <<<HTML
		<li id='category_{$category['category_id']}' class='control_row'>
			<table style='width:100%;'>
				<tr>
					<td style='width: 3%'><div class='draghandle'><img src='{$this->settings['skin_acp_url']}/_newimages/drag_icon.png' alt=''></div></td>
					<td style='width: 72%'>{$name}</td>
					<td style='width: 20%'>{$category['category_records']}</td>
					<td style='width: 5%'>
						<ul class='controls'>
							<li class='left'>
								<a class='edit' href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=edit&amp;id={$database['database_id']}&amp;category={$category['category_id']}'>{$this->lang->words['edit_cat_menu']}</a>
							</li>
							<li class='right ipbmenu' id='menu_folder{$category['category_id']}'>
								{$this->lang->words['folder_options_alt']}
							</li>
						</ul>
						<ul class='acp-menu' id='menu_folder{$category['category_id']}_menucontent'>
							<li class='icon delete'><a href='#' onclick="return acp.confirmDelete( '{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=delete&amp;id={$database['database_id']}&amp;category={$category['category_id']}' );">{$this->lang->words['delete_cat_menu']}</a></li>
							<li class='icon refresh'><a href='{$this->settings['base_url']}&amp;module=databases&amp;section=categories&amp;do=recache&amp;id={$database['database_id']}&amp;category={$category['category_id']}'>{$this->lang->words['recache_cat_menu']}</a></li>
						</ul>
					</td>
				</tr>
			</table>
		</li>
HTML;
	}
}

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

}