<?php
$show_init_form = false;	/* To display or not the initialization form */
$locations_map = array(
	"Famagusta"		=> "Famagusta",
	"Larnaca"		=> "Larnaca",
	"Limassol"		=> "Limassol",
	"Nicosia"		=> "Nicosia",
	"Paphos"		=> "Paphos"
);
/* 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;

    // Set Defaults

    $filter_id = 0;
    $filter_locations = array_keys($locations_map);
    $filter_title = '';
    $filter_description = '';
    $filter_pagination = 0;
    $filter_range = 10;
	
	// Get domain
	$domainname = "";
	$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']);
		}
	}
	
	$q = "select * from domaincategories where catdom_id='$p_domain_id';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		$domainname = str_replace("www.", "", $row['domainname']);
	}
	
	
	// Get Domain Filters
	$all_filters = array();
	$q = "select * from filters where parent_id='$p_domain_id' AND active='yes' ORDER BY title_en ASC;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while($row = mysql_fetch_assoc($r)) {
            $all_filters[$row['filter_id']] = array_map('stripslashes', $row);
        }
		
	}
	
	
	
	$current_catdom_module_id = $admin_catdom_module_id;

}

if($show_init_form) {
	
	// Try to get any previously saved customization
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
	
	if(1==1) {
		?>
		
		<div id="wb_categs" class="inline_container">
			<form method="POST" action="custom_module_update.php" name="filter_adverts_list_frm" id="filter_adverts_list_frm">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr>
                    <th colspan="2" align="left">
                        Filter Adverts List Customization
                    </th>
                </tr>
                <tr>
                    <td nowrap class="itdh">Filter List Title</td>
                    <td class="itd" width="90%">
                        <input type="text" name="filter_title" value="<?=$filter_title;?>" style="width: 100%;">
                    </td>
                </tr>
                <tr>
                    <td nowrap class="itdh">Filter List Description</td>
                    <td class="itd" width="90%">
                        <textarea name="filter_description" rows="3" cols="50" style="width: 100%;"><?=$filter_description;?></textarea>
                    </td>
                </tr>
				<tr>
					<td nowrap class="itdh">Select Adverts Filter</td>
					<td class="itd" width="90%">
						<select name="filter_id" id="filter_id">
                            <option value="">-</option>
                            <?php
                            foreach($all_filters as $f_id=>$f_data) {
                                $selected = '';
                                if($f_id == $filter_id) $selected = 'selected="selected"';
                                echo '<option value="'.$f_id.'" '.$selected.'>'.$f_data['title_en'].'</option>';
                            }
                            ?>
                        </select>
					</td>
				</tr>
				<tr>
					<td nowrap class="itdh" valign="top">Show adverts <br/>from district:</td>
					<td class="itd">
						<ul id="locations">
						<?php
						foreach ($locations_map as $loc=>$label) {
							$checked = '';
							if(in_array($loc, $filter_locations)) {
								$checked = 'checked="checked"';
							}
							echo '<li><input type="checkbox" name="filter_locations[]" value="' . $loc . '" '. $checked . ' style="vertical-align: middle;"> ' . $label . '</li>';
						}
						
						?>
						</ul>
					</td>
				</tr>
				<tr>
                    <td nowrap class="itdh">Paginate Results?</td>
                    <td class="itd">
                        <input type="radio" name="filter_pagination" value="1" <?php echo intval($filter_pagination) == 1 ? "checked" : ""; ?> style="vertical-align:middle;"/> Yes
                        <input type="radio" name="filter_pagination" value="0" <?php echo intval($filter_pagination) == 0 ? "checked" : ""; ?> style="vertical-align:middle;"/> No
                    </td>
                </tr>
                <tr>
                    <td nowrap class="itdh">Results Per Page</td>
                    <td class="itd">
                        <input type="text" name="filter_range" value="<?=$filter_range;?>" style="width:100px;">
                    </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 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>
		
		<?php
	}

	
	$module_forms = array(
		"form_name" => "filter_adverts_list_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
    $filter_id = 0;
    $filter_locations = array_keys($locations_map);
    $filter_title = '';
    $filter_description = '';
    $filter_pagination = 0;
    $filter_range = 10;

	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
