diff --git a/Conjuro/Conjuro.php b/Conjuro/Conjuro.php index 6ef0519..b9d2439 100644 --- a/Conjuro/Conjuro.php +++ b/Conjuro/Conjuro.php @@ -1,4 +1,28 @@ conf=$conf; @@ -112,16 +137,21 @@ return json_decode($json, true); } + //////////////////////////////////////////////////// MODULE HANDLING + //////////////////////////////////////////////////////////////////// + /** * Returns a loaded module or load */ public function module($name){ if (!isset($name) || strpos($name,".")!==false || strpos($name,"/")!==false){ - throw new Exception("Module name error"); + throw new Exception("Module name error: '$name'."); } if (!isset($this->modules[$name])){ $conf=$this->conf; $m=include "content/modules/$name.php"; + if ($m===false) + throw new Exception("Module '$name' does not exists."); if (method_exists($m,"init")){ $m->init($this); } @@ -131,6 +161,19 @@ } /** + * Return the module name knowing the instance + */ + public function getModuleName($mod){ + foreach($this->modules as $k => $v) + if ($v===$mod) + return $k; + return null; + } + + /////////////////////////////////////////////////// CONTROL HANDLING + //////////////////////////////////////////////////////////////////// + + /** * Load a control library */ private function loadControlLibrary($name){ @@ -194,9 +237,12 @@ unset($this->mapi[$name]); } - public function call($name,&...$attrs){ + public function do($name="",&...$attrs){ + if (!$name) + $name="main"; if (!isset($this->mapi[$name])){ - throw new Exception("Function '$name 'does not exist"); + $modname=c()->getModuleName($this); + throw new Exception("Action '$name' does not exist in module '$modname'."); } $this->mapi[$name](...$attrs); } diff --git a/content/fragment/layout.php b/content/fragment/layout.php new file mode 100644 index 0000000..3198fd1 --- /dev/null +++ b/content/fragment/layout.php @@ -0,0 +1,14 @@ + + + + <?=$this->value("title")?> + + + + +
showContent("body");?>
+ + + \ No newline at end of file diff --git a/content/modules/Control.php b/content/modules/Control.php new file mode 100644 index 0000000..a5786b2 --- /dev/null +++ b/content/modules/Control.php @@ -0,0 +1,38 @@ +register("main",function(){ + if (isset($_GET["meta"])){ + $meta=json_decode($_GET["meta"], true); + } + if (isset($_GET["data"])){ + $data=json_decode($_GET["data"], true); + } + $id=$_GET["id"]; + c()->control($id)->render($meta,$data); + }); + } +}?> \ No newline at end of file diff --git a/content/modules/Page.php b/content/modules/Page.php index 4207799..68e1b14 100644 --- a/content/modules/Page.php +++ b/content/modules/Page.php @@ -1,48 +1,44 @@ name=$name; - $this->basedir="content/pages/$name/"; - $this->meta=Conjuro::loadJson(self::filePath("page.json")); + $this->meta=Conjuro::loadJson("content/pages/$name.json"); if(!isset($this->meta["title"])) $this->meta["title"]=$conf["site-title"]; if(!isset($this->meta["layout"])) $this->meta["layout"]=$conf["default-layout"]; - // Sections - if (isset($this->meta["sections"])){ - $this->c_header=$this->meta["sections"]["header"]; - $this->c_body=$this->meta["sections"]["body"]; - $this->c_footer=$this->meta["sections"]["footer"]; - } - if ($this->c_header===NULL) - $this->c_header=[$conf["default-header"]]; - if ($this->c_body==NULL) - $this->c_body=[]; - if ($this->c_footer===NULL) - $this->c_footer=[$conf["default-footer"]]; } - // Renders an entire section - private function showSection($secc){ - foreach($secc as $i => $v){ - $this->showFragment($v); - } - } - - // META:TITLE - protected function getTitle(){ - return $this->meta["title"]; - } // META:LAYOUT protected function getLayoutFile(){ - return "content/layout/".$this->meta["layout"].".php"; + return "content/fragment/".$this->meta["layout"].".php"; } // META:NAME protected function getName(){ @@ -52,39 +48,28 @@ // Renders a fragmet (but in child class context) abstract protected function showFragment($name); - // Returns the path of a file relative to page - protected function filePath($file){ - if ($file=="") - return ""; - if ($file[0]=="#"){ - $fl=substr($file,1); - return "content/common/$fl"; + /** + * Return the value defined for a page meta key + */ + public function value($name){ + return $this->meta[$name]; + } + + /** + * Show the content defined for a section on page meta + */ + public function showContent($name){ + if (isset($this->meta[$name])){ + foreach($this->meta[$name] as $ci){//ControlInfo + c()->control($ci["@"])->render($ci["meta"]); //TODO: Data here when implemented + } } - return $this->basedir.$file; } - // Renders the header - protected function renderHeader(){ - self::showSection($this->c_header); - } - - // Renders the body - protected function renderBody(){ - self::showSection($this->c_body); - } - - // Renders the footer - protected function renderFooter(){ - self::showSection($this->c_footer); - } - - public function needSession(){ + public function needAuth(){ return isset($this->meta["auth"]); } - public function auth(){ - - } } class Page extends PageBase{ @@ -110,7 +95,8 @@ public function showPage(){ $this->processRequest(); $page=$this->getPage($this->currentPage()); - if ($page->needSession() && !$_SESSION['user']){ + if ($page->needAuth() && !$_SESSION['user']){ + //TODO Goes to Auth module $page=$this->getPage($this->conf["login-page"]); } $page->showPage(); @@ -132,7 +118,7 @@ else $this->page=$this->conf["index-page"]; $_SESSION["page"]=$this->page; - // Session + // Session (TODO: Goes to Auth Module) if ($_POST['user']) $_SESSION['user']=$this->doLogin($_POST['user'],$_POST['pwd']); else if ($_SESSION['user']) // refresh user data @@ -141,6 +127,7 @@ session_reset(); } + //TODO Goes to Auth module private function doLogin($user,$access){ return $this->db->selectOne("users",null,'where user=? and pass=sha1(?)','ss',$user,$access); } @@ -151,6 +138,9 @@ } public function init(){ + $this->register("main",function(){ + $this->showPage(); + }); $this->register("showPage",function(){ $this->showPage(); }); diff --git a/content/pages/index.json b/content/pages/index.json new file mode 100644 index 0000000..cbd8a62 --- /dev/null +++ b/content/pages/index.json @@ -0,0 +1,21 @@ +{ + "title": "Bienvenido a Conjuro", + "layout": "layout", + "custom-css": ".conj{font-family:sans-serif;font-size:50px;font-weight:bold;color:#009900}", + "header":[ + { + "@": "html", + "meta": { + "html": "Welcome to Conjuro!!" + } + } + ], + "body": [ + { + "@": "html", + "meta": { + "html": "Welcome to Conjuro!!" + } + } + ] +} \ No newline at end of file diff --git a/index.php b/index.php index 81a8ae1..9180207 100644 --- a/index.php +++ b/index.php @@ -1,14 +1,40 @@ call("showPage"); - /* - c()->control("html")->render(["html"=>"
Hola caracola
"]); - var_dump(c()->listControls()); - c()->control("builder")->render(,["page"=>"index"]); - */ + + $module="Page"; + if (isset($_GET["m"])) + $module=$_GET["m"]; + try{ + m($module)->do(); + }catch (Exception $e) { + echo "
".$e->getMessage()."
"; + } ?> \ No newline at end of file