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;
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])) {

View File

@ -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;

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(
"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 '<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() {
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];

View File

@ -30,7 +30,7 @@
<? } else { ?>
<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>
<? 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']?>user/logout">Se déconnecter</a></li>

View File

@ -10,7 +10,7 @@
<section id="profile">
<? if ($userProfile->get_id() != 0) { ?>
<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>
<? } ?>
<article>
@ -37,10 +37,10 @@
if ($userProfile->get_id() != $user->get_id()) { ?>
<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;
<? }
if ($user->rank_is_higher("premium")) { ?>
if ($user->rankIsHigher("premium")) { ?>
<a href="mailto:<?=$userProfile->email?>"><?=$userProfile->email?></a>
<? } ?>
</p>

View File

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