<? 
$this->title->elem('a')->cssClass('slideDown')->name($this->Language('Add model'));
$this->title->create($this->Language('Models')); 
?>

<div class="slideDownBox">
<form method="post" name="model" action="<?=$this->Link();?>">
<table class="header">
<tr>
	<th><?=$this->Language('model');?></th>
	<td><input type="text" name="name"></td>
</tr>
<tr>
	<th><?=$this->Language('page');?></th>
	<td><input type="text" name="page"></td>
</tr>
<tr>
	<th><?=$this->Language('menu');?></th>
	<td><input type="text" name="menu"></td>
</tr>
<tr>
	<th><?=$this->Language('Parent menu');?></th>
	<td>
	<select name="model_parent_id">
	<option value=""><?=$this->Language('select');?></option>
	<?
	while($row = $this->menus->Assoc()){

		echo '<option value="'.$row['menu_id'].'">'.$row['name'].'</option>';
	}
	?>
	</select>
	</td>
</tr>
<tr>
	<th><?=$this->Language('position');?></th>
	<td><select class="position" name="position">
	<?
	$optgroup_start = false;
	$optgroup_end = false;
	foreach($this->position->get_position() as $row2){

		if($row2['position'] && !$row2['name'] && $optgroup_start == true){

			$optgroup_end = true;
		}

		if($row2['position'] && !$row2['name']){

			$optgroup_start = true;
			$optgroup_end = false;
		}

		if($optgroup_start == true){

			echo '<optgroup label="'.$row2['position'].'">';
			$optgroup_start = false;
		}
		if($optgroup_end == true){

			echo '</optgroup>';
		}

		if($row2['name']){

			echo '<option value="'.$row2['value'].'" '.($row2['value'] == $row['position'] && $row2['position'] == $row['page_position'] ? 'selected="selected"' : '').'>'.$row2['name'].'</option>';
		}
	}
	?>
	</select></td>
</tr>
<tr>
	<th><?=$this->Language('is static');?></th>
	<td>
		<select name="is_static">
			<option selected="selected" value="0"><?=$this->Language('is not static');?></option>
			<option value="1"><?=$this->Language('is static');?></option>
		</select>
	</td>
</tr>
<tr>
	<th><?=$this->Language('is firstpage');?></th>
	<td>
		<select name="is_first_page">
			<option selected="selected" value="0"><?=$this->Language('is not firstpage');?></option>
			<option value="1"><?=$this->Language('is first page');?></option>
		</select>
	</td>
</tr>
<tr>
	<th><?=$this->Language('page position');?></th>
	<td>
		<select name="page_position">
			<option value="public">public</option>
			<option value="admin">admin</option>
			<option value="public,select">public,select</option>
			<option value="admin,public">admin,public</option>
		</select>
	</td>
</tr>
<tr>
	<th><?=$this->Language('roles');?></th>
	<td>
		<select multiple="" name="roles[]">
			<?
			while($row = $this->roles->Assoc()){

				//NB! do not add selected param
			?>
				<option value="<?=$row['id']?>"><?=$row['name']?></option>
			<?
			}
			?>
		</select>
	</td>
</tr>
<tr>
	<th><?=$this->Language('View strucuture');?></th>
	<td>
	<select name="view">
	<option value=""><?=$this->Language('Select');?></option>
	<?
	foreach($this->File->Files as $v){

		$name = str_replace('.tpl', '', $v['file']);
		echo '<option value="'.$name.'">'.$name.'</option>';
	}
	?>
	</select>
	</td>
</tr>
<tr>
	<th><?=$this->Language('Table');?></th>
	<td>
	<select name="mysql_table">
	<?
	$this->Sql->Query("show tables from ".constant('sqlconf[database]'));
	while($row = mysqli_fetch_assoc($this->Sql->Result)){

		echo '<option value="'.$row['Tables_in_'.constant('sqlconf[database]')].'">'.$row['Tables_in_'.constant('sqlconf[database]')].'</option>';
	}
	?>
	</select>
	</td>
</tr>
<tr>
	<th><?=$this->Language('Make redirect link');?></th>
	<td><input type="checkbox" name="make_redirect_link" value="1"></td>
</tr>
<tr>
	<th></th>
	<td><input type="submit" name="add" value="<?=$this->Language('Add');?>" onclick="$('[name=model]').attr('action', '<?=$this->Link();?>'); "></td>
</tr>
</table>
</form>
</div>


<form method="post" name="model" action="<?=$this->Link('?update_id='.$_GET['id']);?>">
<table class="content">
<tr>
	<th><?=$this->Language('model');?></th>
	<th><?=$this->Language('page');?></th>
	<th><?=$this->Language('menu');?></th>
	<th><?=$this->Language('position');?></th>
	<th><?=$this->Language('is static');?></th>
	<th><?=$this->Language('is firstpage');?></th>
	<th><?=$this->Language('page position');?></th>
	<th><?=$this->Language('roles');?></th>
	<th></th>
	<th></th>
	<th></th>
</tr>

<?PHP
while($row = $this->models->Assoc()){

IF($_GET['action']=='access' && $_GET['id']==$row['id']){ ?>
<tr id="pos_<?=$row["id"];?>">
	<td style="font-size:12px;" colspan="7" height="35px"><?=$row['name'];?></td>
	<td colspan="2" height="35px">
		<select multiple="" name="roles[]">
			<?
			while($row2 = $this->roles->Assoc()){
			?>
				<option value="<?=$row2['id']?>"><?=$row2['name']?></option>
			<?
			}
			?>
		</select><br/>
		<input type="hidden" name="model_name" value="<?=$row['name'];?>"><input type="submit" name="models_access" value="<?=$this->Language('Update');?>"> <a href="<?=$this->Link();?>"><?=$this->Language('Cancel');?></a>
	</td>
	<td></td>
</tr>
<? }else{ ?>

<tr>
	<td style="font-size:12px;" colspan="7" height="35px"><?=$row['name'];?></td>
	<td height="35px"><a href="<?=$this->Link('?action=access&id='.$row['id']);?>"><?=$this->Language('access');?></a></td>
	<td colspan="2"><a href="?type=model&model_name=<?=$row['name'];?>" class="export_db">export</a></td>
	<td><a href="<?=$this->Link('?action=delete_model&name='.$row['name']);?>" onclick="return $.confirm();"><?=$this->Language('Delete');?></a></td>
</tr>

<? } ?>

	<?PHP
	while($row = $this->pages->Assoc()){

		IF($_GET['action']=='edit' && $_GET['id'] == $row['id']){
		?>
			<tr id="pos_<?=$row["id"];?>">
				<td height="25px" align="right"></td>
				<td><input type="hidden" name="id" value="<?=$row['id'];?>"><input type="hidden" name="name" value="<?=$row['name'];?>"><input type="text" name="page" value="<?=$row['page'];?>"></td>
				<td><input type="text" name="menu" value="<?=$row['menu'];?>"></td>
				<td>
				<select class="position" name="position">
				<?
				$optgroup_start = false;
				$optgroup_end = false;
				foreach(get_position() as $row2){

					if($row2['position'] && !$row2['name'] && $optgroup_start == true){

						$optgroup_end = true;
					}

					if($row2['position'] && !$row2['name']){

						$optgroup_start = true;
						$optgroup_end = false;
					}

					if($optgroup_start == true){

						echo '<optgroup label="'.$row2['position'].'">';
						$optgroup_start = false;
					}
					if($optgroup_end == true){

						echo '</optgroup>';
					}

					if($row2['name']){

						echo '<option value="'.$row2['value'].'" '.($row2['value'] == $row['position'] && preg_match('/'.$row2['position'].'/i', $row['page_position']) ? 'selected="selected"' : '').'>'.$row2['name'].'</option>';
					}
				}
				?>
				</select></td>
				<td>
					<select name="is_static">
						<option <?=($row['is_static'] == 0 ? 'selected="selected"' : '');?> value="0"><?=$this->Language('is not static');?></option>
						<option <?=($row['is_static'] == 1 ? 'selected="selected"' : '');?> value="1"><?=$this->Language('is static');?></option>
					</select>
				</td>
				<td>
					<select name="is_first_page">
						<option <?=($row['is_first_page'] == 0 ? 'selected="selected"' : '');?> value="0"><?=$this->Language('is not firstpage');?></option>
						<option <?=($row['is_first_page'] == 1 ? 'selected="selected"' : '');?> value="1"><?=$this->Language('is firstpage');?></option>
					</select>
				</td>
				<td>
					<select name="page_position">
						<option <?=($row['page_position'] == 'public' ? 'selected="selected"' : '');?> value="public">public</option>
						<option <?=($row['page_position'] == 'admin' ? 'selected="selected"' : '');?> value="admin">admin</option>
						<option <?=($row['page_position'] == 'public,select' ? 'selected="selected"' : '');?> value="public,select">public,select</option>
						<option <?=($row['page_position'] == 'admin,public' ? 'selected="selected"' : '');?> value="admin,public">admin,public</option>
					</select>
				</td>
				<td colspan="2">
					<select multiple="" name="roles[]">
						<?
						while($row2 = $this->roles->Assoc()){

							$selected = '';
							foreach($this->permissions->Rows as $row3){

								if($row2['id'] == $row3['role_id']){

									$selected = 'selected="selected"';
									break;
								}
							}
							?>
								<option value="<?=$row2['id']?>" <?=$selected;?>><?=$row2['name']?></option>
							<?
						}
						?>
					</select>
				</td>
				<td></td>
			</tr>

			<tr>
				<td></td>
				<td style="text-align:right;padding-right:5px;"><?=$this->Language('Parent menu');?></td>
				<td>
				<select name="model_parent_id">
				<option value=""><?=$this->Language('select');?></option>
				<?
				while($row2 = $this->menus->Assoc()){

					echo '<option value="'.$row2['menu_id'].'" '.($row['menu_parent_id'] == $row2['menu_id'] ? 'selected' : '').'>'.$row2['name'].'</option>';
				}
				?>
				</select>
				</td>
				<td style="text-align:right;padding-right:5px;"><?=$this->Language('Make redirect link');?></td>
				<td><input type="checkbox" name="make_redirect_link" value="1"></td>
				<td></td>
				<td></td>
				<td colspan="4"><input type="submit" name="update" value="<?=$this->Language('Update');?>"> <a href="<?=$this->Link('?cancel_id='.$_GET['id']);?>"><?=$this->Language('Cancel');?></a></td>
			</tr>

		<? }else{ ?>
			<tr id="pos_<?=$row["id"];?>">
				<td height="25px" align="right"></td>
				<td><a href="<?=$this->Link('?action=edit&id='.$row['id']);?>"><?=$row['page'];?></a></td>
				<td><?=$row['menu'];?>&nbsp;&nbsp;</td>
				<td><?=$row['position'];?></td>
				<td><?=$row['is_static_text'];?></td>
				<td><?=$row['is_first_page_text'];?></td>
				<td><?=$row['page_position'];?></td>
				<td><?=$row['roles'];?><br><br></td>
				<td><a href="?type=page&model_name=<?=$row['name'].'&page_name='.$row['page'];?>" class="export_db">export</a></td>
				<td style="background:<?=($row['is_active'] == 1 ? '#336600' : '#CC0000');?>;"><a href="<?=$this->Link('?action=activate&id='.$row['id']);?>&is_active=<?=$row['is_active'];?>"><?=$this->Language('Activate');?></a></td>
				<td ><a href="<?=$this->Link('?action=delete&id='.$row['id']);?>" onclick="return $.confirm();"><?=$this->Language('Delete');?></a></td>
			</tr>
		<? } ?>
	<? } ?>
<? } ?>
</table>
</form>

<script>
$('#pos_<?=$_GET["id"];?>').scrollTo();
$('#pos_<?=$_GET["update_id"];?>').scrollTo();
</script>