<?php
$path='../../../';
require($path . 'includes/header.inc.php');
require('../classes/class.simplemodelmodulebuilder.php'); // MODEL SETTINGS
require('../includes/settings.php'); // MODEL SETTINGS

$model_class_name = MODEL_CLASS_NAME;

$q = $model_class_name::where('id','>','0');

if ( isset($_GET['term']) && !empty($_GET['term']) ) {
	if ( MODEL_NAME_ATTRIBUTE == "name" || MODEL_NAME_ATTRIBUTE == "title" ) {
		$q = $q->where(MODEL_NAME_ATTRIBUTE, 'LIKE', $_GET['term']."%");
	}
}
if ( isset($_GET['limit']) && is_numeric($_GET['limit']) ) {
	$q = $q->take($_GET['limit']);
}

if ( MODEL_ORDER_COLUMN !== false ) {
	$q = $q->orderBy('order', 'desc');
}

$model_list = $q->get();

$result = array();

foreach ($model_list as $key => $model_list_item) {
	ob_start();
	?>
	<tr id="rorder-<?=$model_list_item->id?>" style="height:45px;">
		<?php

		if ( in_array("Order", $model_list_action_columns) && MODEL_ORDER_COLUMN ) {
			echo '<td>&nbsp;<i class="fa fa-ellipsis-v"></i></td>';
		}

		foreach ($model_list_columns as $key => $model_list_column) {

			if ( is_array($model_list_column) ) {
				if ( $model_list_column['type'] == 'options' || $model_list_column['type'] == 'checkbox' ) {

					if ( $model_list_column['type'] == 'checkbox' && !isset($model_list_column['options']) ) {
						$model_list_column['options'] = array(0=>'No',1=>'Yes');
					}

					if ( is_array($model_list_column['options']) ) {
						$value = $model_list_column['options'][$model_list_item->{$key}];
					} else {
						// tries to fetch name
						$value = $model_list_column['options']::find($model_list_item->{$key})->name;
						// if name is null
						if ( $value === null ) {
							// fetches title
							$value = $model_list_column['options']::find($model_list_item->{$key})->title;
						}
					}
				} elseif ( $model_list_column['type'] == 'image' ) {
					$value = "";
					if ( !empty($model_list_item->{$key}) ) {

						$image_path = "/".$image_folders['cms_images']['small']['path']."/";
						if ( isset($model_list_column['path']) ) {
							$image_path = $model_list_column['path'];
						}

						$value = "<img
							src ='".$image_path.$model_list_item->{$key}."'
							alt=''
						/>
						";

					}
				} elseif ( $model_list_column['type'] == 'datetime' ) {
					$value = "";
					if ( !empty($model_list_item->{$key}) ) {
	                    $datetime = date_create_from_format('Y-m-d H:i:s', $model_list_item->{$key});
	                    $value = $datetime->format('d/m/Y H:i:s');
	                }
				} else {
					$value = $model_list_item->{$key};
				}
			} else {
				$value = $model_list_item->{$key};
			}

			echo '<td>';
				if ( isset($model_list_column['link']) ) {
					$link = $model_list_column['link'];
					$link = str_replace("{id}", $model_list_item->id, $link);
					$link = str_replace("{value}", $value, $link);
					echo '<a href="'.$link.'" title="">';
				}
				echo $value;
				if ( isset($model_list_column['link']) ) {
					echo '</a>';
				}
			echo '</td>';
		}
		foreach ($model_list_action_columns as $key => $value) {
			$action = $value;
			if ( is_array($value) ) {}
			if ( $action == "Edit" ) {
				?>
				<td class="center">
					<a href="edit.php?id=<?=$model_list_item->id?>" class="edit picto" title="Edit <?=ucwords(MODEL_SINGULAR)?>">
						<i class="fa fa-pencil"></i>
					</a>
				</td>
				<?php
			} elseif ( $action == "Delete" ) {
				?>
				<td class="center">
					<a href="<?=SimpleModuleModelBuilder::generateNondestructiveUrl(array('action'=>'delete','delete_id'=>$model_list_item->id))?>" class="alert error" title="Are you sure you want to delete this?">
						<i class="fa fa-times-circle"></i>
					</a>
				</td>
				<?php
			} elseif ( $action == "Order" && MODEL_ORDER_COLUMN ) {
				?>
				<td class="center">
					<a href="<?=SimpleModuleModelBuilder::generateNondestructiveUrl(array('action'=>'order','direction'=>'up','item_id'=>$model_list_item->id))?>" class="" title="Move up.">
						<i class="fa fa-arrow-up"></i>
					</a>
					<a href="<?=SimpleModuleModelBuilder::generateNondestructiveUrl(array('action'=>'order','direction'=>'down','item_id'=>$model_list_item->id))?>" class="" title="Move down.">
						<i class="fa fa-arrow-down"></i>
					</a>
				</td>
				<?php
			}
		}
		?>
	</tr>
	<?php
	$result[]['data'] = ob_get_contents(); ob_end_clean();
}

echo(json_encode($result));
