<?php

$show_init_form = false;	/* To display or not the initialization form */


/* Try to see if the call is from administration panel */
if(isset($admin_catdom_module_id) && $admin_catdom_module_id>0 && isset($_REQUEST['action']) && $_REQUEST['action'] == "custom_module") {
	$show_init_form = true;


	// Get domain
	$p_catdom_id = intval($_REQUEST['catdom_id']);
	$p_domain_id = $p_catdom_id;
	$q = "select * from domaincategories where catdom_id='$p_catdom_id';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		if(intval($row['parent_id'])>0) $p_domain_id = intval($row['parent_id']);
	}
	
	// Get Top Article Category
	$p_parent_categ = 0;
	$q = "select * from main_article_categ where catdom_id='$p_domain_id' and parent_id='0';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		$p_parent_categ = intval($row['cat_id']);
	}
	
	
	// Initialize defaults

	$side_parent_id = array();
	$sides_article_list_title = "";
	$sides_article_list_title_none = 0;
	$sides_article_list_max = 20;
	$current_catdom_module_id = $admin_catdom_module_id;

}

if($show_init_form) {
	
	?>
	<style type="text/css">
		#selected_categs ul {
			margin:4px;
			padding:0;
		}
		
		#selected_categs ul li {
			list-style:none;
			margin: 3px;
		}
	
	</style>
	<?php
	
	require_once(dirname(__FILE__) . "/../../admin/modules/contents/functions.php");
	require_once(dirname(__FILE__) . "/../../admin/functions/root_display.php");
	require_once(dirname(__FILE__) . "/../../admin/classes/tree_checkbox_class.php");

	//print_r($module_params);
	// Try to get any previously saved customization
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
	
	$side_parent_id_ar = array();
	if(isset($side_parent_id) && count($side_parent_id)>0) {
		foreach ($side_parent_id as $art_id) {
			$side_parent_id_ar[$art_id] = "==not found==";
		}
	}
	?>
	<form method="POST" action="custom_module_update.php" name="sides_article_list_frm" id="sides_article_list_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<th colspan="2" align="left">
					Article/Categories Sides Display Customization
				</th>
			</tr>
			<tr>
				<td colspan="2">
					<div class="inline_container">
						<table cellpadding="0" cellspacing="0" border="0" width="100%">
							<tr>
								<td class="itd" align="left" nowrap width="20%"><strong>Module Title:</strong></td>
								<td class="itd" align="left" width="80%"><input type="text" name="sides_article_list_title" id="sides_article_list_title" value="<?php echo $sides_article_list_title;?>" maxlength="64" style="width: 95%;"></td>
							</tr>
							<tr>
								<td class="itd" align="left" nowrap width="20%"><strong>Hide Title:</strong></td>
								<td class="itd" align="left" width="80%"><input type="checkbox" name="sides_article_list_title_none" id="sides_article_list_title_none" value="1" <?php echo (intval($sides_article_list_title_none) ==1) ? 'checked="checked"' : '';?> style="vertical-align: middle;"></td>
							</tr>
							<tr>
								<td class="itd" align="left" nowrap width="20%"><strong>MAX Elements:</strong></td>
								<td class="itd" align="left" width="80%"><input type="text" name="sides_article_list_max" id="sides_article_list_max" value="<?php echo $sides_article_list_max;?>" maxlength="3" size="3"> Put 0 (zero) to show all</td>
							</tr>
						</table>	
					</div>
				</td>
			</tr>
			<tr>
				<td valign="top" align="left" width="50%">
					<div class="inline_container">
						<table cellpadding="0" cellspacing="0" border="0" width="100%">
							<tr>
								<th nowrap align="left"><span class="red">*</span> Select Article Categories:</td>
							</tr>
							<tr>
								<td class="itd" align="left">
									
										<div id="local_menu" style="margin:0;">
										<a href="#" onclick="expandAll();return false">Expand all nodes</a> | 
										<a href="#" onclick="collapseAll();return false">Collapse all nodes</a>
										</div>
										<?php
										$nodes = explode(",", getCategoryBreadcrumbs($p_parent_categ));
										$nodes = array_reverse($nodes);
										$nodes_str = implode(",", $nodes);
										
										
										
										$tree = new dhtmlgoodies_checkbox_tree();
										$_COOKIE[$tree->nameOfCookie] = $nodes_str;
										$tree->automatic_init = false;
										$tree->rootCheckboxName = "all_side_parent_id";
										$tree->selectable_root = false;
										$tree->input_class = "side_arts";
										$tree->container_height = "345";
										$tree->input_rel = true;
										$q = "select * from main_article_categ where 1=1 ";
										$current_catdom_id = getCategoryCatdomId(getRootCateg($p_parent_categ));
										$q .= " and catdom_id='$current_catdom_id'";
										$q .= " order by order_seq asc";
										
										//echo $q;
										
										$r = mysql_query($q) or die("<b>$q</b>".mysql_error());
										$no_categs = false;
										$under_root = false;
										if($r && mysql_num_rows($r)>0) {
											while ($row = mysql_fetch_array($r)) {
												$ID = $row['cat_id'];
												$TITLE = stripslashes($row['name']);
												
												$parentID = $row['parent_id'];
												$LINK = trim($row['url']);
												$element_name = "all_side_parent_id";
												$checked = "";
												$add_to_tree = true;
												
												if(isset($side_parent_id) && count($side_parent_id)> 0) {
												
													if(in_array($ID, $side_parent_id)) {
														$side_parent_id_ar[$ID] = $TITLE;
														$checked = "checked";
														$TITLE = "<span class=\"item_text\">$TITLE</span>";
													}
													else {
														$TITLE = "<span class=\"item_text\">$TITLE</span>";
													}
													
												}
												
												
												if($add_to_tree ) {
													$tree->addToArray($ID, $TITLE, $parentID, "", "", "adminimages/tree/dhtmlgoodies_folder.gif", "$element_name", "$checked");
												}
											}
										}
										else {
											$no_categs = true;
											$no_categs_message = t("There are no categories. The category you will add would be a main category.");
										}
										$tree->writeCSS();
										$tree->writeJavascript();
										if(!$no_categs) $tree->drawTree($under_root);
										else $tree->drawTree($no_categs_message);
										?>
									
								</td>
							</tr>
						</table>
					</div>
				</td>
				<td valign="top" align="right" width="50%">
					<div class="inline_container">
					<table cellpadding="0" cellspacing="0" border="0" width="100%">
						<tr>
							<th colspan="2" align="left">
								Selected Categories
							</th>
						</tr>
						<tr>
							<td colspan="2" class="itd">
								<div id="selected_categs" style="height:360px; overflow-y:auto;">
									<ul id="selected_locations_list">
										<?php 
										if(count($side_parent_id_ar)>0) {
											foreach ($side_parent_id_ar as $aid=>$aname) {
												$aname = (mb_strlen($aname) > 43) ? mb_substr($aname, 0, 40) . '..' : $aname;
												echo '<li id="' .$aid. '" class="ui-state-default"><input type="checkbox" name="side_parent_id[]" class="sel_art" id="art_'.$aid.'" style="vertical-align: middle;" value="'.$aid.'" checked="checked"> <strong>'.$aname. '</strong></li>';
											}
										}
										?>
									</ul>
								</div>
							
							</td>
						</tr>
					</table>
					</div>
				</td>
			</tr>
			
			<tr>
				<td colspan="2" align="right" class="footer">
					<input type="reset" value="Reset" name="submit" class="submit_button"/>
					<input type="submit" value="Save" name="submit" class="submit_button"/>
					<input type="hidden" value="<?php echo $current_catdom_module_id; ?>" name="admin_catdom_module_id" id="admin_catdom_module_id"/>
				</td>
			</tr>
		</table>
	</form>

	<script type="text/javascript">
		$(document).ready(function(){
			
			initTree();
			
			$('#selected_locations_list').sortable();
			
			$('input.side_arts').each(function(){
				var $art = $(this);
				$art.click(function(){
					var params = $(this).attr('rel').split('|');
					var art_id = params[0];
					var art_name = params[1];
					var checked = $(this).prop('checked');
					if(checked) {
						$('<li id="' + art_id + '" class="ui-state-default"><input type="checkbox" name="side_parent_id[]" class="sel_art" id="art_'+art_id+'" style="vertical-align: middle;" value="'+art_id+'" checked="checked"> <strong>'+art_name + '</strong></li>').appendTo($('#selected_locations_list'));
					}
					else {
						$('#' + art_id).remove();
					}
				});
				
			});
			
			$('input.sel_art').live('click', function(){
				var $art = $(this);
				
				var params = $art.attr('id').split('_');
				var art_id = params[1];

				var checked = $art.prop('checked');
				if(!checked) {
					$('#' + art_id).remove();
					$('#gen_' + art_id).prop('checked', false);
				}
			});
			
		});
	</script>
	<?php
	
	$module_forms = array(
		"form_name" => "sides_article_list_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$side_parent_id = isset($side_parent_id) && is_array($side_parent_id) ? $side_parent_id : array();
	$sides_article_list_title = "";
	$sides_article_list_title_none = 0;
	$sides_article_list_max = 20;
	
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		
		if(isset($_REQUEST['debug'])) {
			echo '<pre>', var_export($module_params), '</pre>';
		}
		
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
