diff options
author | Kevin Chabowski <kevin@kch42.de> | 2011-12-27 22:16:29 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2011-12-27 22:16:29 +0100 |
commit | edad8581bb6ec2cb0e198f1cc5e415ce526147b7 (patch) | |
tree | ccf1fb19ef8da877a3e33a400db330a254857443 /ratatoeskr/sys | |
parent | a2ee929485e2a55492cbc161b4270038099b26f8 (diff) | |
download | ratatoeskr-cms-edad8581bb6ec2cb0e198f1cc5e415ce526147b7.tar.gz ratatoeskr-cms-edad8581bb6ec2cb0e198f1cc5e415ce526147b7.tar.bz2 ratatoeskr-cms-edad8581bb6ec2cb0e198f1cc5e415ce526147b7.zip |
Article::by_multi improved and frontend modifications.
* Article::by_multi now has more criterias and can perform sorting,
paging and limiting. Also only generates full articles when needed.
* In the frontend there are now some "hidden" __obj STE variables, so
some objects do not need to be created multiple times.
Diffstat (limited to 'ratatoeskr/sys')
-rw-r--r-- | ratatoeskr/sys/models.php | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/ratatoeskr/sys/models.php b/ratatoeskr/sys/models.php index f24b71c..cefe79d 100644 --- a/ratatoeskr/sys/models.php +++ b/ratatoeskr/sys/models.php @@ -2366,33 +2366,69 @@ class Article extends BySQLRowEnabled * Get Articles by multiple criterias * * Parameters: - * $criterias - Array that can have these keys: id (int) , urlname (string), section (<Section> object), status (int) + * $criterias - Array that can have these keys: id (int) , urlname (string), section (<Section> object), status (int), onlyvisible, langavail(string), tag (<Tag> object) + * $sortby - Sort by this field (id, urlname, timestamp or title) + * $sortdir - Sorting directory (ASC or DESC) + * $count - How many entries (NULL for unlimited) + * $offset - How many entries should be skipped (NULL for none) + * $perpage - How many entries per page (NULL for no paging) + * $page - Page number (starting at 1, NULL for no paging) + * &$maxpage - Number of pages will be written here, if paging is activated. * * Returns: * Array of Article objects */ - public function by_multi($criterias) + public function by_multi($criterias, $sortby, $sortdir, $count, $offset, $perpage, $page, &$maxpage) { $subqueries = array(); foreach($criterias as $k => $v) { switch($k) { - case "id": $subqueries[] = qdb_fmt("`id` = %d", $v); break; - case "urlname": $subqueries[] = qdb_fmt("`urlname` = '%s'", $v); break; - case "section": $subqueries[] = qdb_fmt("`section` = %d", $v->get_id()); break; - case "status": $subqueries[] = qdb_fmt("`status` = %d", $v); break; + case "id": $subqueries[] = qdb_fmt("`a`.`id` = %d", $v); break; + case "urlname": $subqueries[] = qdb_fmt("`a`.`urlname` = '%s'", $v); break; + case "section": $subqueries[] = qdb_fmt("`a`.`section` = %d", $v->get_id()); break; + case "status": $subqueries[] = qdb_fmt("`a`.`status` = %d", $v); break; + case "onlyvisible": $subqueries[] = "`a`.`status` != 0"; break; + case "langavail": $subqueries[] = qdb_fmt("`b`.`language` = '%s'", $v); break; + case "tag": $subqueries[] = qdb_fmt("`c`.`tag` = %d", $v->get_id()); break; default: continue; } } - if(empty($subqueries)) - return self::all(); /* No limiting criterias, so we take them all... */ + if(($sortdir != "ASC") and ($sortdir != "DESC")) + $sortdir = "ASC"; + $sorting = ""; + switch($sortby) + { + case "id": $sorting = "ORDER BY `a`.`id` $sortdir"; break; + case "urlname": $sorting = "ORDER BY `a`.`urlname` $sortdir"; break; + case "timestamp": $sorting = "ORDER BY `a`.`timestamp` $sortdir"; break; + case "title": $sorting = "ORDER BY `b`.`text` $sortdir"; break; + } - $result = qdb("SELECT `id`, `urlname`, `title`, `text`, `excerpt`, `meta`, `custom`, `article_image`, `status`, `section`, `timestamp`, `allow_comments` FROM `PREFIX_articles` WHERE " . implode(" AND ", $subqueries)); - $rv = array(); + $result = qdb("SELECT `a`.`id` AS `id`, `a`.`urlname` AS `urlname`, `a`.`title` AS `title`, `a`.`text` AS `text`, `a`.`excerpt` AS `excerpt`, `a`.`meta` AS `meta`, `a`.`custom` AS `custom`, `a`.`article_image` AS `article_image`, `a`.`status` AS `status`, `a`.`section` AS `section`, `a`.`timestamp` AS `timestamp`, `a`.`allow_comments` AS `allow_comments` FROM `PREFIX_articles` `a` +INNER JOIN `PREFIX_translations` `b` ON `a`.`title` = `b`.`multilingual` +LEFT OUTER JOIN `PREFIX_article_tag_relations` `c` ON `a`.`id` = `c`.`article` +WHERE " . implode(" AND ", $subqueries) . " $sorting"); + + $rows = array(); while($sqlrow = mysql_fetch_assoc($result)) - $rv[] = self::by_sqlrow($sqlrow); + $rows[] = $sqlrow; + + if($count !== NULL) + $rows = array_slice($rows, 0, $count); + if($offset !== NULL) + $rows = array_slice($rows, $offset); + if(($perpage !== NULL) and ($page !== NULL)) + { + $maxpage = ceil(count($rows) / $perpage); + $rows = array_slice($rows, $perpage * ($page - 1), $perpage); + } + + $rv = array(); + foreach($rows as $r) + $rv[] = self::by_sqlrow($r); return $rv; } |