Move locale management in specific model

This commit is contained in:
Léo Serre 2018-10-18 20:33:15 +02:00
parent 7e1a24a2db
commit 74eb4c5618
7 changed files with 97 additions and 46 deletions

View File

@ -92,7 +92,7 @@ if(isset($controller->splitted_url[1])) {
} }
break; break;
case 'p': case 'p':
if ($user->rank_is_higher("registered")) { if ($user->rankIsHigher("registered")) {
$userProfile = new User(); $userProfile = new User();
if (!isset($controller->splitted_url[2]) OR $controller->splitted_url[2]=="") { if (!isset($controller->splitted_url[2]) OR $controller->splitted_url[2]=="") {
// WE DISPLAY THE CONNECTED USER PROFILE // WE DISPLAY THE CONNECTED USER PROFILE
@ -107,7 +107,7 @@ if(isset($controller->splitted_url[1])) {
} }
// If we are editing the profile // 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"; $head['js'] = "d.avatar.js";
if (isset($_POST['submit'])) { if (isset($_POST['submit'])) {
$receivedUser = new User(); $receivedUser = new User();
@ -124,7 +124,7 @@ if(isset($controller->splitted_url[1])) {
if($_POST['password']!='') if($_POST['password']!='')
$userProfile->password=sha1($_POST['password']); $userProfile->password=sha1($_POST['password']);
$userProfile->locale=$_POST['locale']; $userProfile->locale=$_POST['locale'];
if($user->rank_is_higher("administrator")) if($user->rankIsHigher("administrator"))
$userProfile->rank = $_POST['rank']; $userProfile->rank = $_POST['rank'];
$userProfile->website=$_POST['website']; $userProfile->website=$_POST['website'];
@ -160,7 +160,7 @@ if(isset($controller->splitted_url[1])) {
} }
// If we are displaying the profile // If we are displaying the profile
else { else {
if (isset($_POST['submit']) && $user->rank_is_higher("registered")) { if (isset($_POST['submit']) && $user->rankIsHigher("registered")) {
// PROCESS DATA FROM CONTACT FORM // PROCESS DATA FROM CONTACT FORM
$message = $_POST['message']; $message = $_POST['message'];
@ -175,7 +175,7 @@ if(isset($controller->splitted_url[1])) {
} }
break; break;
case 'member_list': case 'member_list':
if ($user->rank_is_higher("registered")) { if ($user->rankIsHigher("registered")) {
$rows_per_pages = 50; $rows_per_pages = 50;
// Get the correct page number // 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])) { if (!isset($controller->splitted_url[2]) OR $controller->splitted_url[2]=="" OR $controller->splitted_url[2]=="0" OR !is_numeric($controller->splitted_url[2])) {

View File

@ -6,8 +6,8 @@ $head['css'] = "d.index.css;d.wiki.css";
$wikiPage = new WikiPage(); $wikiPage = new WikiPage();
// Page doesn't exists // 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(isset($controller->splitted_url[1]) && !$wikiPage->checkUrl($controller->splitted_url[1],$user->rankIsHigher('premium')) && $controller->splitted_url[1]!="") {
if($user->rank_is_higher('moderator')) { if($user->rankIsHigher('moderator')) {
// Create new page // Create new page
if(isset($_POST['submit'])) { if(isset($_POST['submit'])) {
$wikiPage->content = $_POST['content']; $wikiPage->content = $_POST['content'];
@ -27,8 +27,8 @@ if(isset($controller->splitted_url[1]) && !$wikiPage->checkUrl($controller->spli
} }
} }
// Page exists // Page exists
else if(isset($controller->splitted_url[1]) && $wikiPage->checkUrl($controller->splitted_url[1],$user->rank_is_higher('premium'))) { 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->rank_is_higher('administrator')) { if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="edit" && $user->rankIsHigher('administrator')) {
// Edit page // Edit page
if(isset($_POST['submit'])) { if(isset($_POST['submit'])) {
$wikiPage->content = $_POST['content']; $wikiPage->content = $_POST['content'];
@ -43,13 +43,13 @@ else if(isset($controller->splitted_url[1]) && $wikiPage->checkUrl($controller->
$head['title'] = $wikiPage->title; $head['title'] = $wikiPage->title;
include ($config['views_folder']."d.wiki.edit.html"); 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 // Delete page
$wikiPage->delete(); $wikiPage->delete();
header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->url); header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->url);
} else { } else {
// Display page // Display page
if($user->rank_is_higher('premium')) { if($user->rankIsHigher('premium')) {
$wikiHistory = new WikiPages(); $wikiHistory = new WikiPages();
$wikiHistory->getHistory($controller->splitted_url[1]); $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])) 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(); $wikiPage->md2html();
$head['title'] = $wikiPage->title; $head['title'] = $wikiPage->title;

62
models/d.locales.php Normal file
View File

@ -0,0 +1,62 @@
<?
/**********************************************************
***********************************************************
**
** This class is to manage Locale object
**
***********************************************************
**********************************************************/
class Locale
{
private $name = 0;
public $display_name = NULL;
public $flag_name = NULL;
/*****
** populate object using name
*****/
public function checkName($name) {
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($name))
or die ("Cannot execute statement\n");
pg_close($con);
if(pg_num_rows($result) == 1) {
$row = pg_fetch_assoc($result);
$this->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;
}
}
?>

View File

@ -8,6 +8,8 @@
*********************************************************** ***********************************************************
**********************************************************/ **********************************************************/
require_once($config['models_folder']."d.locales.php");
$ranks = array( $ranks = array(
"administrator" => array(1000,"Administrateur", "red"), "administrator" => array(1000,"Administrateur", "red"),
"moderator" => array(800,"Modérateur", "orangered"), "moderator" => array(800,"Modérateur", "orangered"),
@ -111,10 +113,13 @@ class User
public function get_id() { public function get_id() {
return $this->id; return $this->id;
} }
public function get_rank() { public function get_rank( $no_html = false ) {
global $ranks; global $ranks;
return '<span class="userrole" style="color: '.$ranks[$this->rank][2].';">'.$ranks[$this->rank][1].'</span>'; if( $no_html )
return $ranks[$this->rank][1];
else
return '<span class="userrole" style="color: '.$ranks[$this->rank][2].';">'.$ranks[$this->rank][1].'</span>';
} }
public function get_avatar() { public function get_avatar() {
if( $this->is_avatar_present == 't') if( $this->is_avatar_present == 't')
@ -124,31 +129,15 @@ class User
} }
public function get_locale() { public function get_locale() {
if( isset($this->locale_loaded) ) { if( isset($this->locale_loaded) ) {
return $this->locale_display_name; return $this->locale_obj->display_name;
} }
else { else {
global $config; $this->locale_obj = new Locale;
$this->locale_loaded = true;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) if( $this->locale_obj->checkName($this->locale) )
or die ("Could not connect to server\n"); return $this->locale_obj->display_name;
else
$query = "SELECT * FROM locales WHERE name=$1"; return false;
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;
} }
} }
public function get_visit_date() { public function get_visit_date() {
@ -161,7 +150,7 @@ class User
/***** /*****
** Returns true if user permissions are higher than $rank ** Returns true if user permissions are higher than $rank
*****/ *****/
public function rank_is_higher($rank) { public function rankIsHigher($rank) {
global $ranks; global $ranks;
return $ranks[$this->rank][0] >= $ranks[$rank][0]; return $ranks[$this->rank][0] >= $ranks[$rank][0];

View File

@ -30,7 +30,7 @@
<? } else { ?> <? } else { ?>
<li><a href="<?=$config['rel_root_folder']?>user/p">Mon profil</a></li> <li><a href="<?=$config['rel_root_folder']?>user/p">Mon profil</a></li>
<li><a href="<?=$config['rel_root_folder']?>user/member_list">Liste des membres</a></li> <li><a href="<?=$config['rel_root_folder']?>user/member_list">Liste des membres</a></li>
<? if($user->rank_is_higher('moderator')) { ?> <? if($user->rankIsHigher('moderator')) { ?>
<li><a href="<?=$config['rel_root_folder']?>admin">Administration</a></li> <li><a href="<?=$config['rel_root_folder']?>admin">Administration</a></li>
<? } ?> <? } ?>
<li><a href="<?=$config['rel_root_folder']?>user/logout">Se déconnecter</a></li> <li><a href="<?=$config['rel_root_folder']?>user/logout">Se déconnecter</a></li>

View File

@ -10,7 +10,7 @@
<section id="profile"> <section id="profile">
<? if ($userProfile->get_id() != 0) { ?> <? if ($userProfile->get_id() != 0) { ?>
<h1><?=$userProfile->name?></h1> <h1><?=$userProfile->name?></h1>
<? if($user->rank_is_higher("moderator") || $user->get_id() == $userProfile->get_id()) { ?> <? if($user->rankIsHigher("moderator") || $user->get_id() == $userProfile->get_id()) { ?>
<a class="subtitle" id="editprofile" href="<?=$config['rel_root_folder']?>user/p/<?=$userProfile->get_id()?>/edit"><i class="fas fa-pencil-alt"></i> Éditer les paramètres du compte</a> <a class="subtitle" id="editprofile" href="<?=$config['rel_root_folder']?>user/p/<?=$userProfile->get_id()?>/edit"><i class="fas fa-pencil-alt"></i> Éditer les paramètres du compte</a>
<? } ?> <? } ?>
<article> <article>
@ -37,10 +37,10 @@
if ($userProfile->get_id() != $user->get_id()) { ?> if ($userProfile->get_id() != $user->get_id()) { ?>
<a href="#" onclick="$('#profilepart').hide(0, function(){$('#contact').show('fast');});">Contacter par mail</a> <a href="#" onclick="$('#profilepart').hide(0, function(){$('#contact').show('fast');});">Contacter par mail</a>
<? } <? }
if ($user->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())) { ?>
&mdash; &mdash;
<? } <? }
if ($user->rank_is_higher("premium")) { ?> if ($user->rankIsHigher("premium")) { ?>
<a href="mailto:<?=$userProfile->email?>"><?=$userProfile->email?></a> <a href="mailto:<?=$userProfile->email?>"><?=$userProfile->email?></a>
<? } ?> <? } ?>
</p> </p>

View File

@ -9,7 +9,7 @@
<section id="wiki_page" <?=!$wikiPage->is_archive()?'class="archive"':''?>> <section id="wiki_page" <?=!$wikiPage->is_archive()?'class="archive"':''?>>
<h1><?=$wikiPage->name?>.</h1> <h1><?=$wikiPage->name?>.</h1>
<? if($user->rank_is_higher('premium')) { ?> <? if($user->rankIsHigher('premium')) { ?>
<span class="subtitle"> <span class="subtitle">
<? if(isset($wikiHistory_list)) { ?> <? if(isset($wikiHistory_list)) { ?>
<select id="wikihistory"> <select id="wikihistory">
@ -20,10 +20,10 @@
} ?> } ?>
</select> </select>
<? } <? }
if ($user->rank_is_higher('moderator') && isset($wikiHistory_list)) { ?> if ($user->rankIsHigher('moderator') && isset($wikiHistory_list)) { ?>
&mdash; &mdash;
<? } <? }
if ($user->rank_is_higher('moderator')) { ?> if ($user->rankIsHigher('moderator')) { ?>
<a href="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/edit"><i class="fa fa-pencil"></i> Éditer la page</a> <a href="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/edit"><i class="fa fa-pencil"></i> Éditer la page</a>
<? if (!$wikiPage->is_archive()) { ?> <? if (!$wikiPage->is_archive()) { ?>
&mdash; &mdash;
@ -42,7 +42,7 @@
<div style="clear: both;"> </div> <div style="clear: both;"> </div>
</section> </section>
<? if($user->rank_is_higher('premium')) { ?> <? if($user->rankIsHigher('premium')) { ?>
<script type="text/javascript"> <script type="text/javascript">
$( "#wikihistory" ).change(function() { $( "#wikihistory" ).change(function() {
window.location.href = "<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/"+$( this ).val(); window.location.href = "<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/"+$( this ).val();