Rework of blog example to be compliant with version of domframework
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1551 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
@@ -3,6 +3,9 @@
|
|||||||
@package domframework-blog
|
@package domframework-blog
|
||||||
@author Dominique Fournier <dominique@fournier38.fr> */
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
||||||
|
|
||||||
|
require ("domframework/form.php");
|
||||||
|
require_once ("models/model_file.php");
|
||||||
|
|
||||||
error_reporting (E_ALL);
|
error_reporting (E_ALL);
|
||||||
/** The main class : manage all the blogs articles */
|
/** The main class : manage all the blogs articles */
|
||||||
class blog
|
class blog
|
||||||
@@ -93,7 +96,12 @@ class blog
|
|||||||
$renderer->viewClass = "view_blog";
|
$renderer->viewClass = "view_blog";
|
||||||
$renderer->viewMethod = "get";
|
$renderer->viewMethod = "get";
|
||||||
$renderer->layout = "layout";
|
$renderer->layout = "layout";
|
||||||
// $renderer->title = "Super Title";
|
$renderer->title = $res["title"];
|
||||||
|
$renderer->variable = array (
|
||||||
|
"title"=>$res["title"],
|
||||||
|
"content"=>$res["content"],
|
||||||
|
"articleid"=>$articleid,
|
||||||
|
);
|
||||||
$renderer->run ();
|
$renderer->run ();
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
@@ -108,7 +116,6 @@ class blog
|
|||||||
/** Return the listing of all the recorded articles */
|
/** Return the listing of all the recorded articles */
|
||||||
public function listing ()
|
public function listing ()
|
||||||
{
|
{
|
||||||
require_once ("models/model_file.php");
|
|
||||||
$data = new model_file();
|
$data = new model_file();
|
||||||
return $data->listing ();
|
return $data->listing ();
|
||||||
}
|
}
|
||||||
@@ -135,4 +142,57 @@ class blog
|
|||||||
exit;
|
exit;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Edit the provided article $articleid */
|
||||||
|
public function editShow ($articleid=false)
|
||||||
|
{
|
||||||
|
if ($articleid !== false)
|
||||||
|
$res = $this->get ($articleid);
|
||||||
|
else
|
||||||
|
$res = array ("title"=>"", "content"=>"");
|
||||||
|
|
||||||
|
$field = new formfield ("title", "Title");
|
||||||
|
$field->mandatory = true;
|
||||||
|
$formFields[] = $field;
|
||||||
|
$field = new formfield ("content", "Content");
|
||||||
|
$field->mandatory = true;
|
||||||
|
$formFields[] = $field;
|
||||||
|
$field = new formfield ("submit", "Record");
|
||||||
|
$field->type = "submit";
|
||||||
|
$formFields[] = $field;
|
||||||
|
|
||||||
|
$f = new form ();
|
||||||
|
$f->fields ($formFields);
|
||||||
|
|
||||||
|
$renderer = new renderer ();
|
||||||
|
$renderer->output = "html";
|
||||||
|
$renderer->viewClass = "view_blog";
|
||||||
|
$renderer->viewMethod = "edit";
|
||||||
|
$renderer->layout = "layout";
|
||||||
|
$renderer->result = $f->printHTML ("post", $res);
|
||||||
|
$renderer->run ();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Save the informations to edit a new article */
|
||||||
|
public function editSave ($articleid)
|
||||||
|
{
|
||||||
|
$field = new formfield ("title", "Title");
|
||||||
|
$field->mandatory = true;
|
||||||
|
$formFields[] = $field;
|
||||||
|
$field = new formfield ("content", "Content");
|
||||||
|
$field->mandatory = true;
|
||||||
|
$formFields[] = $field;
|
||||||
|
$field = new formfield ("submit", "Record");
|
||||||
|
$field->type = "submit";
|
||||||
|
$formFields[] = $field;
|
||||||
|
|
||||||
|
$f = new form ();
|
||||||
|
$f->fields ($formFields);
|
||||||
|
$values = $f->values();
|
||||||
|
unset ($values["submit"]);
|
||||||
|
$model = new model_file ();
|
||||||
|
$model->set ($values, $articleid);
|
||||||
|
$r = new route ();
|
||||||
|
$r->redirect ("/", "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1 @@
|
|||||||
<h1>Article 5</h1>
|
a:2:{s:5:"title";s:11:"Super title";s:7:"content";s:15:"Mega content !!";}
|
||||||
<p>Lorem ipsum</p>
|
|
||||||
@@ -3,18 +3,67 @@
|
|||||||
@package domframework
|
@package domframework
|
||||||
@author Dominique Fournier <dominique@fournier38.fr> */
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
||||||
|
|
||||||
require_once ("domframework/route.php");
|
require_once ("domframework/route.php");
|
||||||
require_once ("domframework/renderer.php");
|
require_once ("domframework/renderer.php");
|
||||||
$route = new route ();
|
session_start();
|
||||||
$route->debug =0 ;
|
|
||||||
$res = $route->routingCollection (array (
|
error_reporting (E_ALL);
|
||||||
//"" => array ("route", "redirect", "/1", FALSE),
|
// Autoload des controllers
|
||||||
"" => array ("blog", "listingHtml"),
|
spl_autoload_register (
|
||||||
"{page}.xml" => array ("blog", "getXml", "{page}"),
|
function ($class) {
|
||||||
"{page}.html" => array ("blog", "getHtml", "{page}"),
|
if (file_exists ("controllers/controller_$class.php"))
|
||||||
"{page}.csv" => array ("blog", "getCsv", "{page}"),
|
include "controllers/controller_$class.php"; });
|
||||||
"{page}.json" => array ("blog", "getJson", "{page}"),
|
|
||||||
"{page}.txt" => array ("blog", "getTxt", "{page}"),
|
$route = new route ();
|
||||||
"{page}" => array ("blog", "getHtml", "{page}"),
|
$route->debug =0 ;
|
||||||
));
|
$route->get ("",
|
||||||
|
function ()
|
||||||
|
{
|
||||||
|
$a = new blog ();
|
||||||
|
$a->listingHtml();
|
||||||
|
});
|
||||||
|
|
||||||
|
$route->get ("{articleid}.txt",
|
||||||
|
function ($articleid)
|
||||||
|
{
|
||||||
|
$a = new blog ();
|
||||||
|
$a->getTxt ($articleid);
|
||||||
|
});
|
||||||
|
|
||||||
|
$route->get ("{articleid}.xml",
|
||||||
|
function ($articleid)
|
||||||
|
{
|
||||||
|
$a = new blog ();
|
||||||
|
$a->getXml ($articleid);
|
||||||
|
});
|
||||||
|
|
||||||
|
$route->get ("{articleid}.json",
|
||||||
|
function ($articleid)
|
||||||
|
{
|
||||||
|
$a = new blog ();
|
||||||
|
$a->getJson ($articleid);
|
||||||
|
});
|
||||||
|
|
||||||
|
$route->get ("{articleid}/edit",
|
||||||
|
function ($articleid)
|
||||||
|
{
|
||||||
|
$a = new blog ();
|
||||||
|
$a->editShow ($articleid);
|
||||||
|
});
|
||||||
|
|
||||||
|
$route->post ("{articleid}/edit",
|
||||||
|
function ($articleid)
|
||||||
|
{
|
||||||
|
$a = new blog ();
|
||||||
|
$a->editSave ($articleid);
|
||||||
|
});
|
||||||
|
|
||||||
|
$route->get ("{articleid}",
|
||||||
|
function ($articleid)
|
||||||
|
{
|
||||||
|
$a = new blog ();
|
||||||
|
$a->getHtml ($articleid);
|
||||||
|
});
|
||||||
|
|
||||||
|
header ($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
|
||||||
echo "404 !";
|
echo "404 !";
|
||||||
|
|||||||
@@ -24,7 +24,25 @@ class model_file
|
|||||||
throw new Exception (_("Asked article not in data path"), 404);
|
throw new Exception (_("Asked article not in data path"), 404);
|
||||||
if ($fileArticle === FALSE)
|
if ($fileArticle === FALSE)
|
||||||
throw new Exception (_("Article not found"), 404);
|
throw new Exception (_("Article not found"), 404);
|
||||||
return file_get_contents ($fileArticle);
|
return unserialize (file_get_contents ($fileArticle));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Create a new article if $articleid is false or edit an existing article
|
||||||
|
if $articleid is defined */
|
||||||
|
function set ($data, $articleid = FALSE)
|
||||||
|
{
|
||||||
|
$this->dataPath = realpath ($this->dataPath);
|
||||||
|
if ($this->dataPath === FALSE)
|
||||||
|
throw new Exception (_("Folder data not available"), 500);
|
||||||
|
$fileArticle = $this->dataPath."/".$articleid;
|
||||||
|
if (! file_exists ($fileArticle))
|
||||||
|
throw new Exception (_("Article not found"), 404);
|
||||||
|
$fileArticle = realpath ($fileArticle);
|
||||||
|
if (substr ($fileArticle, 0, strlen ($this->dataPath)) !== $this->dataPath)
|
||||||
|
throw new Exception (_("Asked article not in data path"), 404);
|
||||||
|
if ($fileArticle === FALSE)
|
||||||
|
throw new Exception (_("Article not found"), 404);
|
||||||
|
return file_put_contents ($fileArticle, serialize ($data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return a list of all the articles ID */
|
/** Return a list of all the articles ID */
|
||||||
|
|||||||
@@ -8,14 +8,17 @@ class view_blog
|
|||||||
{
|
{
|
||||||
/** Return the datas
|
/** Return the datas
|
||||||
@param array $data The list of titles */
|
@param array $data The list of titles */
|
||||||
function get ($data)
|
public function get ($data, $variable)
|
||||||
{
|
{
|
||||||
return $data;
|
$res = "<a href='".$variable["articleid"]."/edit'>Edit</a><br/>";
|
||||||
|
$res.= "<h1>".$variable["title"]."</h1>\n";
|
||||||
|
$res.= "<p>".$variable["content"]."</p>\n";
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Display the list of titles
|
/** Display the list of titles
|
||||||
@param array $data The list of titles */
|
@param array $data The list of titles */
|
||||||
function listing ($data)
|
public function listing ($data, $variable)
|
||||||
{
|
{
|
||||||
$content = "<ul>\n";
|
$content = "<ul>\n";
|
||||||
foreach ($data as $val)
|
foreach ($data as $val)
|
||||||
@@ -27,4 +30,10 @@ class view_blog
|
|||||||
$content .= "</ul>\n";
|
$content .= "</ul>\n";
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Create/Edit a blog */
|
||||||
|
public function edit ($data, $variable)
|
||||||
|
{
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user