From cf2a4a79eb8344ab1c3303d4bdbb80c91d94925b Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Tue, 27 Dec 2011 01:36:09 +0100 Subject: Section's Styles are nowonlyavailable by a getter. Also fixed many MySQL Queries. These SELECT ... WHERE foo = (SELECT...) have not worked as expected, so now there are many ugly joins... --- ratatoeskr/backend.php | 12 ++--- ratatoeskr/frontend.php | 2 +- ratatoeskr/setup/create_tables.php | 6 ++- ratatoeskr/sys/models.php | 97 ++++++++++++++++++++++++-------------- 4 files changed, 72 insertions(+), 45 deletions(-) diff --git a/ratatoeskr/backend.php b/ratatoeskr/backend.php index c5ae9c9..57d87f9 100644 --- a/ratatoeskr/backend.php +++ b/ratatoeskr/backend.php @@ -1095,8 +1095,8 @@ $backend_subactions = url_action_subactions(array( try { $section = Section::by_name($_GET["rmfrom"]); - $style = $_GET["rmstyle"]; - $section->styles = array_filter($section->styles, function($s) use ($style) { return $s->name != $style; }); + $style = Style::by_name($_GET["rmstyle"]); + $section->remove_style($style); $section->save(); $ste->vars["success"] = $translation["style_removed"]; } @@ -1175,11 +1175,7 @@ $backend_subactions = url_action_subactions(array( { $section = Section::by_name($_POST["section_select"]); $style = Style::by_name($_POST["style_to_add"]); - if(!in_array($style, $section->styles)) - { - $section->styles[] = $style; - $section->save(); - } + $section->add_style($style); $ste->vars["success"] = $translation["successfully_added_style"]; } catch(DoesNotExistError $e) @@ -1236,7 +1232,7 @@ $backend_subactions = url_action_subactions(array( "name" => $section->name, "title" => $titles, "template" => $section->template, - "styles" => array_map(function($style) { return $style->name; }, $section->styles), + "styles" => array_map(function($style) { return $style->name; }, $section->get_styles()), "default" => ($section->get_id() == $ratatoeskr_settings["default_section"]) ); }, $sections); diff --git a/ratatoeskr/frontend.php b/ratatoeskr/frontend.php index f022958..92197f5 100644 --- a/ratatoeskr/frontend.php +++ b/ratatoeskr/frontend.php @@ -783,7 +783,7 @@ function frontend_url_handler(&$data, $url_now, &$url_next) if(!isset($section)) $section = $default_section; - foreach($section->styles as $style) + foreach($section->get_styles() as $style) $ste->vars["current"]["styles"][] = $style->name; echo $ste->exectemplate("/usertemplates/" . $section->template); } diff --git a/ratatoeskr/setup/create_tables.php b/ratatoeskr/setup/create_tables.php index dd37d5a..0f2f6b5 100644 --- a/ratatoeskr/setup/create_tables.php +++ b/ratatoeskr/setup/create_tables.php @@ -92,7 +92,6 @@ CREATE TABLE `PREFIX_sections` ( `name` text COLLATE utf8_unicode_ci NOT NULL, `title` int(11) NOT NULL, `template` text COLLATE utf8_unicode_ci NOT NULL, - `styles` text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; @@ -108,6 +107,11 @@ CREATE TABLE `PREFIX_styles` ( PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +CREATE TABLE `PREFIX_section_style_relations` ( + `section` int(11) NOT NULL, + `style` int(11) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + CREATE TABLE `PREFIX_tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text COLLATE utf8_unicode_ci NOT NULL, diff --git a/ratatoeskr/sys/models.php b/ratatoeskr/sys/models.php index c4e0991..78057c5 100644 --- a/ratatoeskr/sys/models.php +++ b/ratatoeskr/sys/models.php @@ -259,7 +259,7 @@ class User extends BySQLRowEnabled public function get_groups() { $rv = array(); - $result = qdb("SELECT `id`, `name` FROM `PREFIX_groups` WHERE `id` = (SELECT `group` FROM `PREFIX_group_members` WHERE `user` = %d)", $this->id); + $result = qdb("SELECT `a`.`id` AS `id`, `a`.`name` AS `name` FROM `PREFIX_groups` `a` INNER JOIN `PREFIX_group_members` `b` ON `a`.`id` = `b`.`group` WHERE `b`.`user` = %d", $this->id); while($sqlrow = mysql_fetch_assoc($result)) $rv[] = Group::by_sqlrow($sqlrow); return $rv; @@ -427,9 +427,11 @@ class Group extends BySQLRowEnabled public function get_members() { $rv = array(); - $result = qdb("SELECT `id`, `username`, `pwhash`, `mail`, `fullname`, `language` FROM `PREFIX_users` WHERE `id` = (SELECT `user` FROM `PREFIX_group_members` WHERE `group` = %d)", $this->id); + $result = qdb("SELECT `a`.`id` AS `id`, `a`.`username` AS `username`, `a`.`pwhash` AS `pwhash`, `a`.`mail` AS `mail`, `a`.`fullname` AS `fullname`, `a`.`language` AS `language` +FROM `PREFIX_users` `a` INNER JOIN `PREFIX_group_members` `b` ON `a`.`id` = `b`.`user` +WHERE `b`.`group` = %d", $this->id); while($sqlrow = mysql_fetch_assoc($result)) - $rv[] = User::by_sqlrow($result); + $rv[] = User::by_sqlrow($sqlrow); return $rv; } @@ -1185,6 +1187,7 @@ class Style extends BySQLRowEnabled public function delete() { qdb("DELETE FROM `PREFIX_styles` WHERE `id` = %d", $this->id); + qdb("DELETE FROM `PREFIX_section_style_relations` WHERE `style` = %d", $this->id); } } @@ -1391,12 +1394,10 @@ class Section extends BySQLRowEnabled * $name - The name of the section. * $title - The title of the section (a object). * $template - Name of the template. - * $styles - List of