<?php

session_register('queryTemp', 'amenu', 'dumps', 'session_data');

include_once "forms/config/main.php";
include_once "forms/generator/generator.php";
include_once "forms/session/session.php";

$Config = Config::read();

foreach(scandir('models/') as $model){
	if(strpos($model, ".php")){
		include "models/$model";
		$model = str_replace(".php", "", $model);
		$objModel = str_replace("_", " ", $model);
		$objModel = ucwords($objModel);
		$objModel = str_replace(" ", "", $objModel);
		if(!class_exists($objModel)){
			print "KumbiaError: No se encontró la Clase \"$objModel\". Es necesario definir una clase en el modelo '$model' llamado '$objModel' para que esto funcione correctamente.";
			return;
		} else {
			$GLOBALS['modelNotNulls'] = array();
			$GLOBALS['modelLengths'] = array();
			$GLOBALS[$objModel] = new $objModel($model, false);
			$GLOBALS[$objModel]->source = $model;
			if(count($GLOBALS['modelNotNulls'])){
				if(!$GLOBALS[$objModel]->validates_presence_of($GLOBALS['modelNotNulls'])){
					return;
				}
			}
			if(count($GLOBALS['modelLengths'])){
				if(!$GLOBALS[$objModel]->validates_length_of($GLOBALS['modelLengths'])){
					return;
				}
			}
			unset($GLOBALS['modelNotNulls']);
			unset($GLOBALS['modelLengths']);
			global $$objModel;
			$GLOBALS[$objModel]->dump();
			if(isset($GLOBALS[$objModel]->persistent)){
				if($GLOBALS[$objModel]->persistent)
				$persis[] = $objModel;
			}
			$GLOBALS[$objModel]->source = $model;
			$GLOBALS['KUMBIA_MODELS'][] = $objModel;
		}
	}
}

if(isset($_SERVER['SERVER_SOFTWARE'])){
	header('Location: index.php');
	exit;
}

function create_controller($a=''){
	if(!$a){
		print "Error: Debe especificar el nombre del Controlador!\n";
	} else {
		if(file_exists("controllers/$a"."_controller.php")){
			print "Error: El controlador $a ya existe!\n";
		} else {
			$file = "<?php
			
	class ".ucwords(strtolower($a))."Controller extends ApplicationController {
	
	}
	
?>\n";		file_put_contents("controllers/$a"."_controller.php", $file);
			print "El controlador se creo correctamente!";
		}
	}
}

$fp = fopen("php://stdin", "r");
print "Welcome to Interactive Kumbia 0.1\n\n";
print "iphp> ";
while($c = fgets($fp)){
	if(rtrim($c)=="quit"){
		exit;
	}
	if(trim($c)){
		$a = eval("return ".trim($c).";");
		if($a===null){
			print "NULL";
		} else {
			if($a===false){
				print "FALSE";
			} else {
				if($a===true){
					print "TRUE";
				} else {
					print_r($a);
				}
			}
		}
		print "\niphp> ";
	} else {
		print "iphp> ";
	}
}
fclose($fp);

?>
