From a2ee929485e2a55492cbc161b4270038099b26f8 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Tue, 27 Dec 2011 13:35:09 +0100 Subject: Article now has get/set_tags() and get/set_section() --- ratatoeskr/backend.php | 23 ++++++----- ratatoeskr/frontend.php | 13 +++--- ratatoeskr/sys/models.php | 102 +++++++++++++++++++++++++++++++++------------- 3 files changed, 94 insertions(+), 44 deletions(-) (limited to 'ratatoeskr') diff --git a/ratatoeskr/backend.php b/ratatoeskr/backend.php index 57d87f9..365fe7a 100644 --- a/ratatoeskr/backend.php +++ b/ratatoeskr/backend.php @@ -236,11 +236,11 @@ $backend_subactions = url_action_subactions(array( $article->urlname = $inputs["urlname"]; $article->status = $inputs["article_status"]; $article->timestamp = $inputs["date"]; - $article->section = $inputs["section"]; - $article->tags = maketags($inputs["tags"], $editlang); $article->title [$editlang] = new Translation($inputs["title"], "" ); $article->text [$editlang] = new Translation($inputs["content"], $inputs["content_txtproc"]); $article->excerpt[$editlang] = new Translation($inputs["excerpt"], $inputs["excerpt_txtproc"]); + $article->set_tags(maketags($inputs["tags"], $editlang)); + $article->set_section($inputs["section"]); } if(empty($article)) @@ -316,7 +316,7 @@ $backend_subactions = url_action_subactions(array( $inputs["excerpt_txtproc"] = $translation_obj->texttype; } if(!isset($inputs["tags"])) - $inputs["tags"] = array_map(function($tag) use ($editlang) { return $tag->name; }, $article->tags); + $inputs["tags"] = array_map(function($tag) use ($editlang) { return $tag->name; }, $article->get_tags()); $ste->vars["morelangs"] = array(); $ste->vars["pagetitle"] = $article->title[$editlang]->text; foreach($article->title as $lang => $_) @@ -330,7 +330,7 @@ $backend_subactions = url_action_subactions(array( if(isset($inputs["tags"])) $ste->vars["tags"] = implode(", ", $inputs["tags"]); if(isset($inputs["article_section"])) - $ste->section["article_section"] = $inputs["article_section"]->name; + $ste->vars["article_section"] = $inputs["article_section"]->name; $ste->vars["editlang"] = $editlang; foreach(array( "urlname" => "urlname", @@ -542,14 +542,14 @@ $backend_subactions = url_action_subactions(array( if(!empty($_GET["filter_tag"])) { $searchfor = $_GET["filter_tag"]; - $articles = array_filter($articles, function($a) use ($searchfor) { foreach($a->tags as $t) { if($t->name==$searchfor) return True; } return False; }); + $articles = array_filter($articles, function($a) use ($searchfor) { foreach($a->get_tags() as $t) { if($t->name==$searchfor) return True; } return False; }); $filterquery[] = "filter_tag=" . urlencode($searchfor); $ste->vars["filter_tag"] = $searchfor; } if(!empty($_GET["filter_section"])) { $searchfor = $_GET["filter_section"]; - $articles = array_filter($articles, function($a) use ($searchfor) { return $a->section->name == $searchfor; }); + $articles = array_filter($articles, function($a) use ($searchfor) { return $a->get_section()->name == $searchfor; }); $filterquery[] = "filter_section=" . urlencode($searchfor); $ste->vars["filter_section"] = $searchfor; } @@ -570,7 +570,7 @@ $backend_subactions = url_action_subactions(array( $ste->vars["sortquery"] = "sort_asc=section"; $ste->vars["sort_asc_section"] = True; $ste->vars["sorting"] = array("dir" => "asc", "by" => "section"); - usort($articles, function($a, $b) { return strcmp($a->section->name, $b->section->name); }); + usort($articles, function($a, $b) { return strcmp($a->get_section()->name, $b->get_section()->name); }); break; case "urlname": $ste->vars["sortquery"] = "sort_asc=urlname"; @@ -595,7 +595,7 @@ $backend_subactions = url_action_subactions(array( $ste->vars["sortquery"] = "sort_desc=section"; $ste->vars["sort_desc_section"] = True; $ste->vars["sorting"] = array("dir" => "desc", "by" => "section"); - usort($articles, function($a, $b) { return strcmp($b->section->name, $a->section->name); }); + usort($articles, function($a, $b) { return strcmp($b->get_section()->name, $a->get_section()->name); }); break; case "urlname": $ste->vars["sortquery"] = "sort_desc=urlname"; @@ -622,12 +622,13 @@ $backend_subactions = url_action_subactions(array( foreach($article->title as $lang => $_) $avail_langs[] = $lang; sort($avail_langs); + $a_section = $article->get_section(); return array( "urlname" => $article->urlname, "languages" => $avail_langs, "date" => $article->timestamp, - "tags" => array_map(function($tag) { return $tag->name; }, $article->tags), - "section" => array("id" => $article->section->get_id(), "name" => $article->section->name) + "tags" => array_map(function($tag) { return $tag->name; }, $article->get_tags()), + "section" => array("id" => $a_section->get_id(), "name" => $a_section->name) ); }, $articles); @@ -1119,7 +1120,7 @@ $backend_subactions = url_action_subactions(array( $default_section = Section::by_id($ratatoeskr_settings["default_section"]); foreach($section->get_articles() as $article) { - $article->section = $default_section; + $article->set_section($default_section); $article->save(); } $section->delete(); diff --git a/ratatoeskr/frontend.php b/ratatoeskr/frontend.php index 92197f5..142915c 100644 --- a/ratatoeskr/frontend.php +++ b/ratatoeskr/frontend.php @@ -89,21 +89,22 @@ function article_transform_ste($article, $lang) global $rel_path_to_root; $languages = array(); + $a_section = $article->get_section(); foreach($article->title as $language => $_) - $languages[$language] = "$rel_path_to_root/$language/{$article->section->name}/{$article->urlname}"; + $languages[$language] = "$rel_path_to_root/$language/{$a_section->name}/{$article->urlname}"; return array( "id" => $article->get_id(), "urlname" => $article->urlname, - "fullurl" => htmlesc("$rel_path_to_root/$lang/{$article->section->name}/{$article->urlname}"), + "fullurl" => htmlesc("$rel_path_to_root/$lang/{$a_section->name}/{$article->urlname}"), "title" => htmlesc($article->title[$lang]->text), "text" => textprocessor_apply(str_replace("%root%", $rel_path_to_root, $article->text[$lang]->text), $article->text[$lang]->texttype), "excerpt" => textprocessor_apply(str_replace("%root%", $rel_path_to_root, $article->excerpt[$lang]->text), $article->excerpt[$lang]->texttype), "custom" => $article->custom, "status" => $article->status, - "section" => section_transform_ste($article->section, $lang), + "section" => section_transform_ste($a_section, $lang), "timestamp" => $article->timestamp, - "tags" => array_map(function($tag) use ($lang) { return tag_transform_ste($tag, $lang); }, $article->tags), + "tags" => array_map(function($tag) use ($lang) { return tag_transform_ste($tag, $lang); }, $article->get_tags()), "languages" => $languages, "comments_allowed" => $article->comments_allowed ); @@ -181,12 +182,14 @@ $ste->register_tag("articles_get", function($ste, $params, $sub) $result = Article::by_multi($params); + /* + FIXME: if(isset($params["tag"])) { if(!isset($result)) $result = Article::all(); $result = array_filter($result, function($article) use ($params) { return isset($article->tags[$params["tag"]]); }); - } + }*/ /* Now filter out the articles, where the current language does not exist */ $result = array_filter($result, function($article) use ($lang) { return isset($article->title[$lang]); }); diff --git a/ratatoeskr/sys/models.php b/ratatoeskr/sys/models.php index 78057c5..f24b71c 100644 --- a/ratatoeskr/sys/models.php +++ b/ratatoeskr/sys/models.php @@ -2218,6 +2218,9 @@ class Article extends BySQLRowEnabled { private $id; + private $section_id; + private $section_obj; + /* * Variables: Public class variables * @@ -2229,10 +2232,8 @@ class Article extends BySQLRowEnabled * $custom - Custom fields, is an array * $article_image - The article . If none: NULL * $status - One of the ARTICLE_STATUS_* constants - * $section -
* $timestamp - Timestamp * $allow_comments - Are comments allowed? - * $tags - Arrray of objects */ public $urlname; public $title; @@ -2242,14 +2243,12 @@ class Article extends BySQLRowEnabled public $custom; public $article_image; public $status; - public $section; public $timestamp; public $allow_comments; - public $tags; protected function __construct() { - $this->tags = array(); + $this->section_obj = NULL; } protected function populate_by_sqlrow($sqlrow) @@ -2263,13 +2262,9 @@ class Article extends BySQLRowEnabled $this->custom = unserialize(base64_decode($sqlrow["custom"])); $this->article_image = $sqlrow["article_image"] == 0 ? NULL : Image::by_id($sqlrow["article_image"]); $this->status = $sqlrow["status"]; - $this->section = Section::by_id($sqlrow["section"]); + $this->section_id = $sqlrow["section"]; $this->timestamp = $sqlrow["timestamp"]; $this->allow_comments = $sqlrow["allow_comments"] == 1; - - $result = qdb("SELECT `a`.`id` AS `id`, `a`.`name` AS `name`, `a`.`title` AS `title` FROM `PREFIX_tags` `a` INNER JOIN `PREFIX_article_tag_relations` `b` ON `a`.`id` = `b`.`tag` WHERE `b`.`article` = %d", $this->id); - while($sqlrow = mysql_fetch_assoc($result)) - $this->tags[] = Tag::by_sqlrow($sqlrow); } /* @@ -2306,7 +2301,7 @@ class Article extends BySQLRowEnabled $obj->custom = array(); $obj->article_image = NULL; $obj->status = ARTICLE_STATUS_HIDDEN; - $obj->section = Section::by_id($ratatoeskr_settings["default_section"]); + $obj->section_id = $ratatoeskr_settings["default_section"]; $obj->timestamp = time(); $obj->allow_comments = $ratatoeskr_settings["allow_comments_default"]; @@ -2316,7 +2311,7 @@ class Article extends BySQLRowEnabled $obj->excerpt->get_id(), base64_encode(serialize($obj->custom)), $obj->status, - $obj->section->get_id(), + $obj->section_id, $obj->timestamp, $obj->allow_comments ? 1 : 0); $obj->id = mysql_insert_id(); @@ -2445,19 +2440,31 @@ class Article extends BySQLRowEnabled } /* - * Function: save + * Function: get_tags + * Get all Tags of this Article. + * + * Returns: + * Array of objects. */ - public function save() + public function get_tags() { - $result = qdb("SELECT COUNT(*) AS `n` FROM `PREFIX_articles` WHERE `urlname` = '%s' AND `id` != %d", $this->urlname, $this->id); - $sqlrow = mysql_fetch_assoc($result); - if($sqlrow["n"] > 0) - throw new AlreadyExistsError(); - - $this->title->save(); - $this->text->save(); - $this->excerpt->save(); - foreach($this->tags as $tag) + $rv = array(); + $result = qdb("SELECT `a`.`id` AS `id`, `a`.`name` AS `name`, `a`.`title` AS `title` FROM `PREFIX_tags` `a` INNER JOIN `PREFIX_article_tag_relations` `b` ON `a`.`id` = `b`.`tag` WHERE `b`.`article` = %d", $this->id); + while($sqlrow = mysql_fetch_assoc($result)) + $rv[] = Tag::by_sqlrow($sqlrow); + return $rv; + } + + /* + * Function: set_tags + * Set the Tags that should be associated with this Article. + * + * Parameters: + * $tags - Array of objects. + */ + public function set_tags($tags) + { + foreach($tags as $tag) $tag->save(); qdb("DELETE FROM `PREFIX_article_tag_relations` WHERE `article`= %d", $this->id); @@ -2467,11 +2474,50 @@ class Article extends BySQLRowEnabled if(!empty($this->tags)) qdb( "INSERT INTO `PREFIX_article_tag_relations` (`article`, `tag`) VALUES " . - implode(",", - array_map(function($tag) use ($articleid){ return qdb_fmt("(%d, %d)", $articleid, $tag->get_id()); }, - $this->tags) - ) + implode(",", array_map(function($tag) use ($articleid){ return qdb_fmt("(%d, %d)", $articleid, $tag->get_id()); }, $tags)) ); + } + + /* + * Function: get_section + * Get the section of this article. + * + * Returns: + * A
object. + */ + public function get_section() + { + if($this->section_obj === NULL) + $this->section_obj = Section::by_id($this->section_id); + return $this->section_obj; + } + + /* + * Function: set_section + * Set the section of this article. + * + * Parameters: + * $section - A
object. + */ + public function set_section($section) + { + $this->section_id = $section->get_id(); + $this->section_obj = $section; + } + + /* + * Function: save + */ + public function save() + { + $result = qdb("SELECT COUNT(*) AS `n` FROM `PREFIX_articles` WHERE `urlname` = '%s' AND `id` != %d", $this->urlname, $this->id); + $sqlrow = mysql_fetch_assoc($result); + if($sqlrow["n"] > 0) + throw new AlreadyExistsError(); + + $this->title->save(); + $this->text->save(); + $this->excerpt->save(); qdb("UPDATE `PREFIX_articles` SET `urlname` = '%s', `title` = %d, `text` = %d, `excerpt` = %d, `meta` = '%s', `custom` = '%s', `article_image` = %d, `status` = %d, `section` = %d, `timestamp` = %d, `allow_comments` = %d WHERE `id` = %d", $this->urlname, @@ -2482,7 +2528,7 @@ class Article extends BySQLRowEnabled base64_encode(serialize($this->custom)), $this->article_image === NULL ? 0 : $this->article_image->get_id(), $this->status, - $this->section->get_id(), + $this->section_id, $this->timestamp, $this->allow_comments ? 1 : 0, $this->id -- cgit v1.2.3-70-g09d2