<?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($_GET['action']) && $_GET['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
	$module_show_parent_title = 1;
	$module_number_of_columns = 2;
	$module_use_defaults = 1;
	$middle_parent_id = 0;
	$module_skin = "skin-vista";
	$module_show_icons = 0;
	$module_show_expander = 1;
	$current_catdom_module_id = $admin_catdom_module_id;

}

if($show_init_form) {
	
	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_radio_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;
		}
	}
	?>
	<div class="inline_container">
	<form method="POST" action="custom_module_update.php" name="article_list_middle_frm" id="article_list_middle_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<th colspan="2" align="left">
					Article Categories Middle Display Customization
				</th>
			</tr>
			<tr>
				<td nowrap class="itdh">Show Parent Title ?</td>
				<td class="itd">
					<input type="radio" name="module_show_parent_title" value="1" <?php echo ($module_show_parent_title == 1) ? "checked" : ""; ?> style="vertical-align:middle;"/> Yes 
					<input type="radio" name="module_show_parent_title" value="0" <?php echo ($module_show_parent_title == 0) ? "checked" : ""; ?> style="vertical-align:middle;"/> No
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Number Of Columns:</td>
				<td class="itd">
					<input type="text" name="module_number_of_columns" value="<?php echo $module_number_of_columns;?>" size="2"/>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Skin:</td>
				<td class="itd">
					<select name="module_skin">
						<option value="skin" <?php echo $module_skin == "skin" ? 'selected="selected"' : '';?>>Default Style</option>
						<option value="skin-vista" <?php echo $module_skin == "skin-vista" ? 'selected="selected"' : '';?>>Vista Style</option>
					</select>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Show Folder/Article Icons:</td>
				<td class="itd">
					<select name="module_show_icons">
						<option value="1" <?php echo intval($module_show_icons) == 1 ? 'selected="selected"' : '';?>>Show</option>
						<option value="0" <?php echo intval($module_show_icons) == 0 ? 'selected="selected"' : '';?>>Hide</option>
					</select>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Show Expander Icons:</td>
				<td class="itd">
					<select name="module_show_expander">
						<option value="1" <?php echo intval($module_show_expander) == 1 ? 'selected="selected"' : '';?>>Show</option>
						<option value="0" <?php echo intval($module_show_expander) == 0 ? 'selected="selected"' : '';?>>Hide</option>
					</select>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Use defaults?</td>
				<td class="itd">
					<input type="radio" name="module_use_defaults" value="1" <?php echo ($module_use_defaults == 1) ? "checked" : ""; ?> style="vertical-align:middle;"/> Yes 
					<input type="radio" name="module_use_defaults" value="0" <?php echo ($module_use_defaults == 0) ? "checked" : ""; ?> style="vertical-align:middle;"/> No
				</td>
			</tr>
			<tr>
				<td class="itdh" valign="top" nowrap align="right"><span class="red">*</span> Select Article Category:</td>
				<td class="itd">
					<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_radio_tree();
					$_COOKIE[$tree->nameOfCookie] = $nodes_str;
					$tree->automatic_init = false;
					$tree->rootCheckboxName = "middle_parent_id";
					$tree->selectable_root = false;
					$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 name 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 = "middle_parent_id";
							$checked = "";
							$add_to_tree = true;
							
							if($middle_parent_id > 0) {
							
								$q_c = "select cat_id, parent_id from main_article_categ where cat_id='$middle_parent_id'";
								$r_c = mysql_query($q_c);
								$row_c = mysql_fetch_array($r_c);
								$c_parent_categ = $row_c['parent_id'];
								//$middle_parent_id = $p_parent_categ;
								
								if($c_parent_categ == 0 ) $under_root = true;
								
								$id = $row_c['cat_id'];
								if($middle_parent_id == $ID) {
									$checked = "checked";
								}
								
								if($ID == $middle_parent_id) {
									//$element_name = "";
									//$TITLE = "<span class=\"current_none\">$TITLE</span>";
									$TITLE = "<span class=\"item_text\">$TITLE</span>";
								}
								else {
									$TITLE = "<span class=\"item_text\">$TITLE</span>";
								}
								
								if(is_descendent($ID, $middle_parent_id)) {
									//$add_to_tree = false;
								}
							}
							else {
								if(isset($middle_parent_id)) {
									$req_parent_id = $middle_parent_id;
									if($req_parent_id == $ID) {
										$checked = "checked";
									}
									
									if($req_parent_id == 0) $under_root = true;
								}
							}
							/*
							if($LINK != "") {
							$element_name = "";
							//$add_to_tree = false;
							}
							*/
							
							if($add_to_tree ) {
								$tree->addToArray($ID, $TITLE, $parentID, "", "", "adminimages/tree/dhtmlgoodies_folder.gif", "$element_name", "$checked");
							}
						}
					}
					else {
						$no_categs = true;
						$no_categs_message = "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>
			<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>
	</div>
	<script type="text/javascript">
		$(document).ready(function(){
			initTree();
		});
	</script>
	<?php
	
	$module_forms = array(
		"form_name" => "article_list_middle_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$module_show_parent_title = 1;// (isset($module_show_parent_title) && intval($module_show_parent_title) >= 0) ? $module_show_parent_title : 0;
	$module_number_of_columns = 2;
	$module_use_defaults = 1;
	$module_skin = "skin-vista";
	$module_show_icons = 0;
	$module_show_expander = 1;
	$middle_parent_id = isset($middle_parent_id) && intval($middle_parent_id) > 0 ? $middle_parent_id : 0;
	
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
