websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 332 (differences)
Author: xbright
Log message: * Welcome 2010 :)
Change revision:
<?php
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007-2010 CodingTeam (See AUTHORS and THANKS for details)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, version 3 only.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
class View {
    private $session, $db, $page, $error, $langlist, $lang;
    public $metatags, $maincontent;
    
    function __construct($session, $db, $page, $error, $langlist, $lang)
    {
        // Variables
        $this->ct_session = $session;
        $this->ct_db = $db;
        $this->page = $page;
        $this->error = $error;
        $this->langlist = $langlist;
        $this->lang = $lang;
 
        // Meta tags
        $title = i18n('Manage - Users - Administration');
        $this->metatags = array('title' => $title);
 
        // Only admins
        verify_user_level($_SESSION['id'], 'administrator',
                          $this->ct_session, $this->error, $this->ct_db);
    }
 
    function treatForms()
    {
        // Clean POST values
        foreach ($_POST as $key => $value)
            if (!is_scalar($value))
                exit('Error.');
 
        if (count($_POST) && isset($_POST['level']) && isset($_POST['user-id']))
        {
            // Retrieve data
            $this->form_new_level = $this->ct_db->cleanentry($_POST['level'], TRUE);
            $this->form_user_id = $this->ct_db->cleanentry($_POST['user-id'], TRUE);
 
            if (in_array($this->form_new_level, array('administrator', 'member',
                                                      'banned', 'deleted')))
            {
                $user = getUser($this->form_user_id, $this->ct_db);
                if ($user)
                {
                    if ($this->form_new_level == 'deleted')
                    {
                        $user->setSurname('');
                        $user->setName('');
                        $user->setDate('');
                        $user->setEmail('');
                        $user->setJid('');
                        $user->setUrl('');
                        $user->setAvatar('!deleted!');
                        $user->setKeyid(0);
                        $user->setLang('en');
                    }
 
                    $user->setLevel($this->form_new_level);
                }
            }
            else
                exit('Error.');
        }
        elseif (count($_POST) && isset($_POST['nickfilter']))
        {
            // Retrieve data
            $this->form_nick = $this->ct_db->cleanentry($_POST['nickfilter'], TRUE);
 
            if (!empty($this->form_nick))
                // Redirect
                Header('Location: '.CT_BASEURL.'admin/users/manage/filter:'.
                                    $this->form_nick);
        }
    }
 
    function constructView()
    {
        $construct = array();
        $construct['__tpl__'] = 'admin-manage.tpl';
 
        // Test if the URL contains nickname filter and/or pagination
        if (!empty($this->page[4]) && is_numeric($this->page[4]))
            $pagination = $this->page[4];
        elseif (!empty($this->page[4]) && mb_substr($this->page[4], 0, 7) == 'filter:')
        {
            $filter = mb_substr($this->page[4], 7, mb_strlen($this->page[4]));
 
            // Pagination
            if (!empty($this->page[5]) && is_numeric($this->page[5]))
                $pagination = $this->page[5];
 
            // Nick filter
            $construct['form_nick'] = htmlspecialchars($filter);
        }
        else
            $construct['form_nick'] = '';
 
        // Select
        if (!empty($pagination) && ereg("^[0-9]+$", $pagination))
            $page = ($pagination - 1);
 
        $max = 50;
        if(!isset($page))
        {
            $start = 0;
            $page = 0;
        }
        else
            $start = $page * $max;
 
        $user = getClass('users.user', $this->ct_db);
        if (!empty($filter))
        {
            // Filter nickname
            $where = array('nickname' => array($filter, 'LIKE'));
            $users_list = $user->getUsersList($where, 'LIMIT '.$start.','.$max);
        }
        else
            $users_list = $user->getUsersList(FALSE, 'LIMIT '.$start.','.$max);
 
        // Pagination
        $construct['pagination_list'] = '';
        $i = 0;
        if($users_list[0] > $max)
        {
            while($i < ($users_list[0] / $max))
            {
                if($i != $page)
                {
                    $construct['pagination_list'] .= '&nbsp;<a href="admin/users/manage/'.
                                ((!empty($filter)) ? 'filter:'.$filter.'/' : '').($i + 1);
                    $construct['pagination_list'] .= '">'.($i + 1).'</a>';
                }
                else
                {
                    $construct['pagination_list'] .= '&nbsp;<b>'.($i + 1).'</b>';
                }
 
                $i++;
            }
        }
 
        if ($i > ($page + 1))
            $total = $max * ($page + 1);
        else
            $total = $users_list[0];
 
        if ($users_list[0] == 0)
            $write = $start;
        else
            $write = ($start + 1);
 
        $construct['pagination_infos'] = i18n('%(start)d → %(max)d of %(total)d',
                                              array('start' => $write,
                                                    'max' => $total,
                                                    'total' => $users_list[0]));
 
        // Select
        $construct['users'] = array();
 
        foreach ($users_list[1] as $user)
        {
            $_user = array('nickname' => htmlspecialchars($user['nickname']),
                           'date' => i18nDate($user['date'], $this->lang),
                           'level' => $user['level'],
                           'id' => $user['id']);
 
            array_push($construct['users'], $_user);
        }
 
        return $construct;
    }
}
?>