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/sys/models.php | 102 +++++++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 28 deletions(-) (limited to 'ratatoeskr/sys') 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-54-g00ecf