websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 511 (differences)
Author: xbright
Log message: * Enhanced roadmap
Change revision:
<?php
//   This file is a part of CodingTeam. Take a look at <http://codingteam.org>.
//   Copyright © 2007-2012 Erwan Briand <erwan@codingteam.net>
//
//   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 $ct_session, $ct_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;
 
        // Security
        if (!$this->ct_session->isLogged())
            $this->error->displayError(i18n('Please log-in…'), 0);
 
        $this->messages = getClass('users.messages', $this->ct_db);
 
        if (!empty($this->page[3]) && !empty($this->page[4]) && $this->page[3] == 'show')
            $this->view = 'show';
        elseif (!empty($this->page[3]) && $this->page[3] == 'new')
            $this->view = 'new';
        else
            $this->view = 'default';
 
        // Meta tags
        $this->metatags = array('title' => i18n('Messages'),
                                'javascript' => 'public/textview-func.js');
    }
 
    function showFeed()
    {
    }
 
    function treatForms()
    {
        // Start error handler
        $this->form_error = 0;  
 
        // Clean POST values
        foreach ($_POST as $key => $value)
            if (!is_scalar($value))
                exit('Error.');
 
        if (!empty($this->page[3]) && !empty($this->page[4]) && $this->page[3] == 'show' && count($_POST) > 0)
        {
            if (is_numeric($this->page[4]))
                $pid = (int)$this->page[4];
            else
                exit('Error.');
 
            $infos = $this->messages->getThreadInfos($pid);
            if ((!$infos) || (!in_array($_SESSION['id'], $infos)))
                exit('Error.');
 
            if ($infos[0] == $_SESSION['id'])
                $recipient = $infos[1];
            else
                $recipient = $infos[0];
 
            $text = $this->ct_db->cleanentry($_POST['text'], FALSE);
 
            if (isset($_POST['allow_nl2br']))
                $allow_nl2br = TRUE;
            else
                $allow_nl2br = FALSE;
 
            if (!empty($text))
                $this->messages->answer(array(
                                            'pid'       => $pid,
                                            'sender'    => $_SESSION['id'],
                                            'recipient' => $recipient,
                                            'datetime'  => date('Y-m-d H:i:s'),
                                            'message'   => $text,
                                            'nl2br'     => $allow_nl2br
                                           ));
 
            Header('Location: '.CT_BASEURL.'users/messages/show/'.$pid);
        }
        elseif (isset($this->page[3]) && $this->page[3] == 'new' && count($_POST) > 0)
        {
            $usernick = $this->ct_db->cleanentry($_POST['nickname'], TRUE);
            $this->form_nickname = $usernick;
 
            $sendto = getUser($usernick, $this->ct_db, 'nickname');
            if (!$sendto)
                $this->form_error = i18n('This member was not found!');
            else
            {
                $recipient = $sendto->getId();
 
                if ($recipient == $_SESSION['id'])
                    $this->form_error = i18n('So you want to talk with… you?');
            }
 
            if (empty($_POST['subject']))
                $this->form_error = i18n('Please write a subject!');
            else
            {
                $subject = $this->ct_db->cleanentry($_POST['subject'], TRUE);
                $this->form_subject = $subject;
            }
 
            if (empty($_POST['text']))
                $this->form_error = i18n('Please write a text!');
            else
            {
                $text = $this->ct_db->cleanentry($_POST['text'], FALSE);
                $this->form_message = $text;
            }
 
            if (isset($_POST['allow_nl2br']))
                $allow_nl2br = TRUE;
            else
                $allow_nl2br = FALSE;
 
            if (!$this->form_error)
            {
                $this->messages->send(array('sender'    => $_SESSION['id'],
                                            'recipient' => $recipient,
                                            'datetime'  => date('Y-m-d H:i:s'),
                                            'subject'   => $subject,
                                            'message'   => $text,
                                            'nl2br'     => $allow_nl2br));
 
                Header('Location: '.CT_BASEURL.'users/messages');
            }
        }
    }
 
    function constructView()
    {
        $textview = getClass('textview');
 
        $construct = array();
        $construct['__tpl__'] = 'messages-'.$this->view.'.tpl';
 
        if ($this->view == 'default')
        {
            if (isset($this->page[3]))
                $page = (int)$this->page[3];
            else
                $page = FALSE;
 
            $max = 10;
            if(!$page)
            {
                $start = 0;
                $page = 0;
            }
            else
                $start = ($page-1) * $max;
            $__mess__ = $this->messages->getMessages($_SESSION['id'],
                                                     'LIMIT '.$start.','.$max);
 
            list($messages, $totalmessages) = $__mess__;
 
            $pagination = generate_pagination($totalmessages, $max, $page, 'users/messages/');
            $write = $pagination[1]['write'];
            $total = $pagination[1]['total'];
            $construct['pagination']['links'] = $pagination[0];
            $construct['pagination']['infos'] = i18n('%(start)d → %(max)d of %(total)d',
                                                     array('start' => $write,
                                                           'max'   => $total,
                                                           'total' => $totalmessages));
 
            $mess = array();
            foreach ($messages as $m)
            {
                if (empty($m['last_answer_datetime']))
                    $date = i18nDate($m['datetime'], $this->lang);
                else
                {
                    $dt = date("Y-m-d H:i:s", $m['last_answer_datetime']);
                    $date = i18nDate($m['datetime'], $this->lang);
                }
 
                $subject = htmlspecialchars($m['subject']);
                if (empty($subject))
                    $subject = i18n('[no subject]');
 
                $message = map_str(strip_tags($m['message']), 70);
 
                if (isset($m['last_answer_sender']))
                    $sender = $m['last_answer_sender'];
                else
                    $sender = $m['sender_id'];
 
                if ($sender == $_SESSION['id'])
                {
                    if (isset($m['last_answer_reads']))
                        $read = $m['last_answer_reads'];
                    else
                        $read = $m['read_sender'];
                }
                else
                {
                    if (isset($m['last_answer_readr']))
                        $read = $m['last_answer_readr'];
                    else
                        $read = $m['read_recipient'];
                }
 
                if ($m['sender_id'] == $_SESSION['id'])
                    $id = $m['recipient_id'];
                else
                    $id = $m['sender_id'];
 
                $sender = show_user_infos('', $id, $this->ct_db, $this->lang,
                                                                      FALSE);
 
                array_push($mess, array('postid'  => $m['id'],
                                        'date'    => $date,
                                        'subject' => $subject,
                                        'message' => $message,
                                        'sender'  => $sender,
                                        'read'    => $read));
            }
 
            $construct['messages'] = $mess;
        }
        elseif ($this->view == 'show')
        {
            if (is_numeric($this->page[4]))
                $pid = (int)$this->page[4];
            else
                exit('Error.');
 
            // Only involved users can see the conversation
            $infos = $this->messages->getThreadInfos($pid);
            if (!is_array($infos) || !in_array($_SESSION['id'], $infos))
                exit('Error.');
 
            $thread = $this->messages->getThread($pid, $_SESSION['id']);
 
            $mess = array();
            $s = '';
            foreach ($thread as $m)
            {
                $date = i18nDate($m['datetime'], $this->lang);
 
                if (array_key_exists('subject', $m))
                    $subject = htmlspecialchars($m['subject']);
                else
                    $subject = '';
 
                $message = $textview->prepare($m['message'], $m['allow_nl2br']);
 
                $sender = show_user_infos('', $m['sender_id'],
                                          $this->ct_db, $this->lang, FALSE);
 
                array_push($mess, array('date'    => $date,
                                        'subject' => $subject,
                                        'message' => $message,
                                        'sender'  => $sender,
                                        'read'    => $m['read']));
 
                $s = $subject;
            }
 
            $construct['messages'] = $mess;
            $construct['thread'] = $s;
            $construct['pid'] = $pid;
            $construct['myself'] = show_user_infos('', $_SESSION['id'],
                                                   $this->ct_db, $this->lang,
                                                   FALSE);
 
            $construct['textview'] = $textview->loadView('', TRUE, FALSE, TRUE);
        }
        else
        {
            $construct['form_error'] = $this->form_error;
 
            if (isset($this->form_message))
                $form_message = $this->form_message;
            else
                $form_message = '';
 
            if (isset($this->form_subject))
                $construct['form_subject'] = htmlspecialchars($this->form_subject);
            else
                $construct['form_subject'] = '';
 
            if (isset($this->form_nickname))
                $construct['form_nickname'] = htmlspecialchars($this->form_nickname);
            elseif (isset($this->page[4]))
                $construct['form_nickname'] = htmlspecialchars($this->page[4]);
            else
                $construct['form_nickname'] = '';
 
            $construct['textview'] = $textview->loadView($form_message, TRUE, FALSE);
        }
 
        return $construct;
    }
}
?>