From 74eb4c5618022ba31b7d9fd77fecc7062b6a91bb Mon Sep 17 00:00:00 2001 From: leosw Date: Thu, 18 Oct 2018 20:33:15 +0200 Subject: [PATCH] Move locale management in specific model --- controllers/d.users.php | 10 +++---- controllers/d.wiki.php | 14 ++++----- models/d.locales.php | 62 +++++++++++++++++++++++++++++++++++++++ models/d.users.php | 41 ++++++++++---------------- views/blocks/d.nav.html | 2 +- views/d.user.profile.html | 6 ++-- views/d.wiki.view.html | 8 ++--- 7 files changed, 97 insertions(+), 46 deletions(-) create mode 100644 models/d.locales.php diff --git a/controllers/d.users.php b/controllers/d.users.php index 4b9b055..a16f9fc 100755 --- a/controllers/d.users.php +++ b/controllers/d.users.php @@ -92,7 +92,7 @@ if(isset($controller->splitted_url[1])) { } break; case 'p': - if ($user->rank_is_higher("registered")) { + if ($user->rankIsHigher("registered")) { $userProfile = new User(); if (!isset($controller->splitted_url[2]) OR $controller->splitted_url[2]=="") { // WE DISPLAY THE CONNECTED USER PROFILE @@ -107,7 +107,7 @@ if(isset($controller->splitted_url[1])) { } // If we are editing the profile - if(isset($controller->splitted_url[3]) && $controller->splitted_url[3]=="edit" && ($user->rank_is_higher("moderator") || $user->id == $userProfile->id)) { + if(isset($controller->splitted_url[3]) && $controller->splitted_url[3]=="edit" && ($user->rankIsHigher("moderator") || $user->id == $userProfile->id)) { $head['js'] = "d.avatar.js"; if (isset($_POST['submit'])) { $receivedUser = new User(); @@ -124,7 +124,7 @@ if(isset($controller->splitted_url[1])) { if($_POST['password']!='') $userProfile->password=sha1($_POST['password']); $userProfile->locale=$_POST['locale']; - if($user->rank_is_higher("administrator")) + if($user->rankIsHigher("administrator")) $userProfile->rank = $_POST['rank']; $userProfile->website=$_POST['website']; @@ -160,7 +160,7 @@ if(isset($controller->splitted_url[1])) { } // If we are displaying the profile else { - if (isset($_POST['submit']) && $user->rank_is_higher("registered")) { + if (isset($_POST['submit']) && $user->rankIsHigher("registered")) { // PROCESS DATA FROM CONTACT FORM $message = $_POST['message']; @@ -175,7 +175,7 @@ if(isset($controller->splitted_url[1])) { } break; case 'member_list': - if ($user->rank_is_higher("registered")) { + if ($user->rankIsHigher("registered")) { $rows_per_pages = 50; // Get the correct page number if (!isset($controller->splitted_url[2]) OR $controller->splitted_url[2]=="" OR $controller->splitted_url[2]=="0" OR !is_numeric($controller->splitted_url[2])) { diff --git a/controllers/d.wiki.php b/controllers/d.wiki.php index f574418..0d4f56f 100755 --- a/controllers/d.wiki.php +++ b/controllers/d.wiki.php @@ -6,8 +6,8 @@ $head['css'] = "d.index.css;d.wiki.css"; $wikiPage = new WikiPage(); // Page doesn't exists -if(isset($controller->splitted_url[1]) && !$wikiPage->checkUrl($controller->splitted_url[1],$user->rank_is_higher('premium')) && $controller->splitted_url[1]!="") { - if($user->rank_is_higher('moderator')) { +if(isset($controller->splitted_url[1]) && !$wikiPage->checkUrl($controller->splitted_url[1],$user->rankIsHigher('premium')) && $controller->splitted_url[1]!="") { + if($user->rankIsHigher('moderator')) { // Create new page if(isset($_POST['submit'])) { $wikiPage->content = $_POST['content']; @@ -27,8 +27,8 @@ if(isset($controller->splitted_url[1]) && !$wikiPage->checkUrl($controller->spli } } // Page exists -else if(isset($controller->splitted_url[1]) && $wikiPage->checkUrl($controller->splitted_url[1],$user->rank_is_higher('premium'))) { - if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="edit" && $user->rank_is_higher('administrator')) { +else if(isset($controller->splitted_url[1]) && $wikiPage->checkUrl($controller->splitted_url[1],$user->rankIsHigher('premium'))) { + if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="edit" && $user->rankIsHigher('administrator')) { // Edit page if(isset($_POST['submit'])) { $wikiPage->content = $_POST['content']; @@ -43,13 +43,13 @@ else if(isset($controller->splitted_url[1]) && $wikiPage->checkUrl($controller-> $head['title'] = $wikiPage->title; include ($config['views_folder']."d.wiki.edit.html"); } - } else if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="delete" && $user->rank_is_higher('moderator')) { + } else if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="delete" && $user->rankIsHigher('moderator')) { // Delete page $wikiPage->delete(); header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->url); } else { // Display page - if($user->rank_is_higher('premium')) { + if($user->rankIsHigher('premium')) { $wikiHistory = new WikiPages(); $wikiHistory->getHistory($controller->splitted_url[1]); @@ -62,7 +62,7 @@ else if(isset($controller->splitted_url[1]) && $wikiPage->checkUrl($controller-> } } if (isset($controller->splitted_url[2]) && is_numeric($controller->splitted_url[2])) - $wikiPage->checkUrl($controller->splitted_url[1], $user->rank_is_higher('premium'), $controller->splitted_url[2]); + $wikiPage->checkUrl($controller->splitted_url[1], $user->rankIsHigher('premium'), $controller->splitted_url[2]); $wikiPage->md2html(); $head['title'] = $wikiPage->title; diff --git a/models/d.locales.php b/models/d.locales.php new file mode 100644 index 0000000..6630b8c --- /dev/null +++ b/models/d.locales.php @@ -0,0 +1,62 @@ +populate($row); + return 1; + } + else { + return 0; + } + } + + /***** + ** Populate the object using raw data from SQL + *****/ + private function populate($row) { + $this->name = $row['name']; + $this->display_name = $row['display_name']; + $this->flag_name = $row['flag_name']; + } + + /***** + ** Simple return only functions + *****/ + public function get_id() { + return $this->id; + } +} + +?> \ No newline at end of file diff --git a/models/d.users.php b/models/d.users.php index 8c78317..5dbf984 100755 --- a/models/d.users.php +++ b/models/d.users.php @@ -8,6 +8,8 @@ *********************************************************** **********************************************************/ +require_once($config['models_folder']."d.locales.php"); + $ranks = array( "administrator" => array(1000,"Administrateur", "red"), "moderator" => array(800,"Modérateur", "orangered"), @@ -111,10 +113,13 @@ class User public function get_id() { return $this->id; } - public function get_rank() { + public function get_rank( $no_html = false ) { global $ranks; - return ''.$ranks[$this->rank][1].''; + if( $no_html ) + return $ranks[$this->rank][1]; + else + return ''.$ranks[$this->rank][1].''; } public function get_avatar() { if( $this->is_avatar_present == 't') @@ -124,31 +129,15 @@ class User } public function get_locale() { if( isset($this->locale_loaded) ) { - return $this->locale_display_name; + return $this->locale_obj->display_name; } else { - global $config; - - $con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) - or die ("Could not connect to server\n"); - - $query = "SELECT * FROM locales WHERE name=$1"; - - pg_prepare($con, "prepare1", $query) - or die ("Cannot prepare statement\n"); - $result = pg_execute($con, "prepare1", array($this->locale)) - or die ("Cannot execute statement\n"); - - pg_close($con); - - if(pg_num_rows($result) == 1) { - $row = pg_fetch_assoc($result); - $this->locale_loaded = true; - $this->locale_display_name = $row['display_name']; - $this->locale_flag_name = $row['flag_name']; - return $this->locale_display_name; - } - return false; + $this->locale_obj = new Locale; + $this->locale_loaded = true; + if( $this->locale_obj->checkName($this->locale) ) + return $this->locale_obj->display_name; + else + return false; } } public function get_visit_date() { @@ -161,7 +150,7 @@ class User /***** ** Returns true if user permissions are higher than $rank *****/ - public function rank_is_higher($rank) { + public function rankIsHigher($rank) { global $ranks; return $ranks[$this->rank][0] >= $ranks[$rank][0]; diff --git a/views/blocks/d.nav.html b/views/blocks/d.nav.html index 36d639b..a0eefcd 100755 --- a/views/blocks/d.nav.html +++ b/views/blocks/d.nav.html @@ -30,7 +30,7 @@
  • Mon profil
  • Liste des membres
  • - rank_is_higher('moderator')) { ?> + rankIsHigher('moderator')) { ?>
  • Administration
  • Se déconnecter
  • diff --git a/views/d.user.profile.html b/views/d.user.profile.html index 548dbdf..151a3d2 100755 --- a/views/d.user.profile.html +++ b/views/d.user.profile.html @@ -10,7 +10,7 @@
    get_id() != 0) { ?>

    name?>

    - rank_is_higher("moderator") || $user->get_id() == $userProfile->get_id()) { ?> + rankIsHigher("moderator") || $user->get_id() == $userProfile->get_id()) { ?> Éditer les paramètres du compte
    @@ -37,10 +37,10 @@ if ($userProfile->get_id() != $user->get_id()) { ?> Contacter par mail rank_is_higher("premium") AND ($userProfile->website != "" OR $userProfile->get_id() != $user->get_id())) { ?> + if ($user->rankIsHigher("premium") AND ($userProfile->website != "" OR $userProfile->get_id() != $user->get_id())) { ?> — rank_is_higher("premium")) { ?> + if ($user->rankIsHigher("premium")) { ?> email?>

    diff --git a/views/d.wiki.view.html b/views/d.wiki.view.html index cfb0278..0fc1ba5 100755 --- a/views/d.wiki.view.html +++ b/views/d.wiki.view.html @@ -9,7 +9,7 @@
    is_archive()?'class="archive"':''?>>

    name?>.

    - rank_is_higher('premium')) { ?> + rankIsHigher('premium')) { ?> rank_is_higher('moderator') && isset($wikiHistory_list)) { ?> + if ($user->rankIsHigher('moderator') && isset($wikiHistory_list)) { ?> — rank_is_higher('moderator')) { ?> + if ($user->rankIsHigher('moderator')) { ?> Éditer la page is_archive()) { ?> — @@ -42,7 +42,7 @@
    -rank_is_higher('premium')) { ?> +rankIsHigher('premium')) { ?>