qa-page-messages.php 4.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/


	File: qa-include/qa-page-message.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Controller for private messaging page


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	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 General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
	require_once QA_INCLUDE_DIR.'qa-app-users.php';
	require_once QA_INCLUDE_DIR.'qa-app-format.php';
	require_once QA_INCLUDE_DIR.'qa-app-limits.php';

	$loginUserId = qa_get_logged_in_userid();
Scott committed
38
	$loginUserHandle = qa_get_logged_in_handle();
39 40 41 42


//	Check which box we're showing (inbox/sent), we're not using Q2A's single-sign on integration and that we're logged in

Scott committed
43 44 45 46 47 48 49
	$req = qa_request_part(1);
	if ($req === null)
		$showBox = 'inbox';
	else if ($req === 'sent')
		$showBox = 'outbox';
	else
		return include QA_INCLUDE_DIR.'qa-page-not-found.php';
50 51 52 53 54 55 56 57 58 59

	if (QA_FINAL_EXTERNAL_USERS)
		qa_fatal_error('User accounts are handled by external code');

	if (!isset($loginUserId)) {
		$qa_content = qa_content_prepare();
		$qa_content['error'] = qa_insert_login_links(qa_lang_html('misc/message_must_login'), qa_request());
		return $qa_content;
	}

Scott committed
60
	if (!qa_opt('allow_private_messages') || !qa_opt('show_message_history'))
61 62 63
		return include QA_INCLUDE_DIR.'qa-page-not-found.php';


Scott committed
64
//	Find the messages for this user
65

Scott committed
66
	$start = qa_get_start();
Scott committed
67
	$pagesize = qa_opt('page_size_pms');
Scott committed
68 69

	// get number of messages then actual messages for this page
Scott committed
70
	$func = 'qa_db_messages_'.$showBox.'_selectspec';
71 72
	$pmSpecCount = qa_db_selectspec_count( $func('private', $loginUserId, true) );
	$pmSpec = $func('private', $loginUserId, true, $start, $pagesize);
Scott committed
73 74 75

	list($numMessages, $userMessages) = qa_db_select_with_pending($pmSpecCount, $pmSpec);
	$count = $numMessages['count'];
76 77 78 79 80


//	Prepare content for theme

	$qa_content = qa_content_prepare();
Scott committed
81
	$qa_content['title'] = qa_lang_html('misc/pm_'.$showBox.'_title');
82
	$qa_content['script_rel'][] = 'qa-content/qa-user.js?'.QA_VERSION;
83 84 85 86

	$qa_content['message_list'] = array(
		'tags' => 'id="privatemessages"',
		'messages' => array(),
87 88 89 90 91 92 93 94 95 96
		'form' => array(
			'tags' => 'name="pmessage" method="post" action="'.qa_self_html().'"',
			'style' => 'tall',
			'hidden' => array(
				'qa_click' => '', // for simulating clicks in Javascript
				'handle' => qa_html($loginUserHandle),
				'start' => qa_html($start),
				'code' => qa_get_form_security_code('pm-'.$loginUserHandle),
			),
		),
97 98 99
	);

	$htmlDefaults = qa_message_html_defaults();
Scott committed
100
	if ($showBox === 'outbox')
101 102
		$htmlDefaults['towhomview'] = true;

Scott committed
103 104 105 106 107 108 109 110
	foreach ($userMessages as $message) {
		$msgFormat = qa_message_html_fields($message, $htmlDefaults);
		$replyHandle = $showBox == 'inbox' ? $message['fromhandle'] : $message['tohandle'];

		$msgFormat['form'] = array(
			'style' => 'light',
			'buttons' => array(
				'reply' => array(
111
					'tags' => 'onclick="window.location.href=\''.qa_path_html('message/'.$replyHandle).'\';return false"',
Scott committed
112 113
					'label' => qa_lang_html('question/reply_button'),
				),
114 115 116 117 118
				'delete' => array(
					'tags' => 'name="m'.qa_html($message['messageid']).'_dodelete" onclick="return qa_pm_click('.qa_js($message['messageid']).', this, '.qa_js($showBox).');"',
					'label' => qa_lang_html('question/delete_button'),
					'popup' => qa_lang_html('profile/delete_pm_popup'),
				),
Scott committed
119 120 121 122 123
			),
		);

		$qa_content['message_list']['messages'][] = $msgFormat;
	}
124

Scott committed
125 126
	$qa_content['page_links'] = qa_html_page_links(qa_request(), $start, $pagesize, $count, qa_opt('pages_prev_next'));

Scott committed
127
	$qa_content['navigation']['sub'] = qa_messages_sub_navigation($showBox);
128 129

	return $qa_content;