UserPosts.php 8.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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
*/

namespace Q2A\Controllers\User;

21 22
use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
23
use Q2A\Http\Exceptions\PageNotFoundException;
24

25
class UserPosts extends BaseController
26 27 28 29
{
	protected $userid;
	protected $userhtml;

30
	public function __construct(DbConnection $db)
31 32 33 34 35
	{
		require_once QA_INCLUDE_DIR . 'db/users.php';
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		require_once QA_INCLUDE_DIR . 'app/users.php';
		require_once QA_INCLUDE_DIR . 'app/format.php';
36 37

		parent::__construct($db);
38 39
	}

40 41 42 43 44 45
	/**
	 * @param string $handle
	 *
	 * @return array
	 * @throws PageNotFoundException
	 */
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	public function activity($handle)
	{
		$this->userHtml($handle);

		// Find the recent activity for this user

		$loginuserid = qa_get_logged_in_userid();
		$identifier = QA_FINAL_EXTERNAL_USERS ? $this->userid : $handle;

		list($useraccount, $questions, $answerqs, $commentqs, $editqs) = qa_db_select_with_pending(
			QA_FINAL_EXTERNAL_USERS ? null : qa_db_user_account_selectspec($handle, false),
			qa_db_user_recent_qs_selectspec($loginuserid, $identifier, qa_opt_if_loaded('page_size_activity')),
			qa_db_user_recent_a_qs_selectspec($loginuserid, $identifier),
			qa_db_user_recent_c_qs_selectspec($loginuserid, $identifier),
			qa_db_user_recent_edit_qs_selectspec($loginuserid, $identifier)
		);

63 64 65
		if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) { // check the user exists
			throw new PageNotFoundException();
		}
66 67 68 69 70 71 72 73 74 75 76
		// Get information on user references

		$questions = qa_any_sort_and_dedupe(array_merge($questions, $answerqs, $commentqs, $editqs));
		$questions = array_slice($questions, 0, qa_opt('page_size_activity'));
		$usershtml = qa_userids_handles_html(qa_any_get_userids_handles($questions), false);


		// Prepare content for theme

		$qa_content = qa_content_prepare(true);

77
		if (count($questions)) {
78
			$qa_content['title'] = qa_lang_html_sub('profile/recent_activity_by_x', $this->userhtml);
79
		} else {
80
			$qa_content['title'] = qa_lang_html_sub('profile/no_posts_by_x', $this->userhtml);
81
		}
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101


		// Recent activity by this user

		$qa_content['q_list']['form'] = array(
			'tags' => 'method="post" action="' . qa_self_html() . '"',

			'hidden' => array(
				'code' => qa_get_form_security_code('vote'),
			),
		);

		$qa_content['q_list']['qs'] = array();

		$htmldefaults = qa_post_html_defaults('Q');
		$htmldefaults['whoview'] = false;
		$htmldefaults['voteview'] = false;
		$htmldefaults['avatarsize'] = 0;

		foreach ($questions as $question) {
102 103 104 105 106 107 108 109
			$qa_content['q_list']['qs'][] = qa_any_to_q_html_fields(
				$question,
				$loginuserid,
				qa_cookie_get(),
				$usershtml,
				null,
				array('voteview' => false) + qa_post_html_options($question, $htmldefaults)
			);
110 111 112 113 114 115 116 117 118 119 120 121
		}


		// Sub menu for navigation in user pages

		$ismyuser = isset($loginuserid) && $loginuserid == (QA_FINAL_EXTERNAL_USERS ? $this->userid : $useraccount['userid']);
		$qa_content['navigation']['sub'] = qa_user_sub_navigation($handle, 'activity', $ismyuser);


		return $qa_content;
	}

122 123 124 125 126 127
	/**
	 * @param string $handle
	 *
	 * @return array
	 * @throws PageNotFoundException
	 */
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	public function questions($handle)
	{
		$this->userHtml($handle);

		$start = qa_get_start();

		// Find the questions for this user

		$loginuserid = qa_get_logged_in_userid();
		$identifier = QA_FINAL_EXTERNAL_USERS ? $this->userid : $handle;

		list($useraccount, $userpoints, $questions) = qa_db_select_with_pending(
			QA_FINAL_EXTERNAL_USERS ? null : qa_db_user_account_selectspec($handle, false),
			qa_db_user_points_selectspec($identifier),
			qa_db_user_recent_qs_selectspec($loginuserid, $identifier, qa_opt_if_loaded('page_size_qs'), $start)
		);

145 146 147
		if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) { // check the user exists
			throw new PageNotFoundException();
		}
148 149 150 151 152 153 154 155 156 157 158 159 160 161


		// Get information on user questions

		$pagesize = qa_opt('page_size_qs');
		$count = (int)@$userpoints['qposts'];
		$questions = array_slice($questions, 0, $pagesize);
		$usershtml = qa_userids_handles_html($questions, false);


		// Prepare content for theme

		$qa_content = qa_content_prepare(true);

162
		if (count($questions)) {
163
			$qa_content['title'] = qa_lang_html_sub('profile/questions_by_x', $this->userhtml);
164
		} else {
165
			$qa_content['title'] = qa_lang_html_sub('profile/no_questions_by_x', $this->userhtml);
166
		}
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185


		// Recent questions by this user

		$qa_content['q_list']['form'] = array(
			'tags' => 'method="post" action="' . qa_self_html() . '"',

			'hidden' => array(
				'code' => qa_get_form_security_code('vote'),
			),
		);

		$qa_content['q_list']['qs'] = array();

		$htmldefaults = qa_post_html_defaults('Q');
		$htmldefaults['whoview'] = false;
		$htmldefaults['avatarsize'] = 0;

		foreach ($questions as $question) {
186 187 188 189 190 191 192 193
			$qa_content['q_list']['qs'][] = qa_post_html_fields(
				$question,
				$loginuserid,
				qa_cookie_get(),
				$usershtml,
				null,
				qa_post_html_options($question, $htmldefaults)
			);
194 195 196 197 198 199 200 201 202 203 204 205 206 207
		}

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


		// Sub menu for navigation in user pages

		$ismyuser = isset($loginuserid) && $loginuserid == (QA_FINAL_EXTERNAL_USERS ? $this->userid : $useraccount['userid']);
		$qa_content['navigation']['sub'] = qa_user_sub_navigation($handle, 'questions', $ismyuser);


		return $qa_content;
	}

208 209 210 211 212 213
	/**
	 * @param string $handle
	 *
	 * @return array
	 * @throws PageNotFoundException
	 */
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	public function answers($handle)
	{
		$this->userHtml($handle);

		$start = qa_get_start();


		// Find the questions for this user

		$loginuserid = qa_get_logged_in_userid();
		$identifier = QA_FINAL_EXTERNAL_USERS ? $this->userid : $handle;

		list($useraccount, $userpoints, $questions) = qa_db_select_with_pending(
			QA_FINAL_EXTERNAL_USERS ? null : qa_db_user_account_selectspec($handle, false),
			qa_db_user_points_selectspec($identifier),
			qa_db_user_recent_a_qs_selectspec($loginuserid, $identifier, qa_opt_if_loaded('page_size_activity'), $start)
		);

232 233 234
		if (!QA_FINAL_EXTERNAL_USERS && !is_array($useraccount)) { // check the user exists
			throw new PageNotFoundException();
		}
235 236 237 238 239 240 241 242 243 244 245 246 247 248


		// Get information on user questions

		$pagesize = qa_opt('page_size_activity');
		$count = (int)@$userpoints['aposts'];
		$questions = array_slice($questions, 0, $pagesize);
		$usershtml = qa_userids_handles_html($questions, false);


		// Prepare content for theme

		$qa_content = qa_content_prepare(true);

249
		if (count($questions)) {
250
			$qa_content['title'] = qa_lang_html_sub('profile/answers_by_x', $this->userhtml);
251
		} else {
252
			$qa_content['title'] = qa_lang_html_sub('profile/no_answers_by_x', $this->userhtml);
253
		}
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277


		// Recent questions by this user

		$qa_content['q_list']['form'] = array(
			'tags' => 'method="post" action="' . qa_self_html() . '"',

			'hidden' => array(
				'code' => qa_get_form_security_code('vote'),
			),
		);

		$qa_content['q_list']['qs'] = array();

		$htmldefaults = qa_post_html_defaults('Q');
		$htmldefaults['whoview'] = false;
		$htmldefaults['avatarsize'] = 0;
		$htmldefaults['ovoteview'] = true;
		$htmldefaults['answersview'] = false;

		foreach ($questions as $question) {
			$options = qa_post_html_options($question, $htmldefaults);
			$options['voteview'] = qa_get_vote_view('A', false, false);

278
			$qa_content['q_list']['qs'][] = qa_other_to_q_html_fields($question, $loginuserid, qa_cookie_get(), $usershtml, null, $options);
279 280 281 282 283 284 285 286 287 288 289 290 291 292
		}

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


		// Sub menu for navigation in user pages

		$ismyuser = isset($loginuserid) && $loginuserid == (QA_FINAL_EXTERNAL_USERS ? $this->userid : $useraccount['userid']);
		$qa_content['navigation']['sub'] = qa_user_sub_navigation($handle, 'answers', $ismyuser);


		return $qa_content;
	}

293 294 295 296 297 298
	/**
	 * Return the HTML to display for the handle, and if we're using external users, determine the userid.
	 *
	 * @param string $handle
	 * @throws PageNotFoundException
	 */
299 300 301 302
	private function userHtml($handle)
	{
		if (QA_FINAL_EXTERNAL_USERS) {
			$this->userid = qa_handle_to_userid($handle);
303 304 305
			if (!isset($this->userid)) { // check the user exists
				throw new PageNotFoundException();
			}
306 307 308

			$usershtml = qa_get_users_html(array($this->userid), false, qa_path_to_root(), true);
			$this->userhtml = @$usershtml[$this->userid];
309
		} else {
310
			$this->userhtml = qa_html($handle);
311
		}
312 313
	}
}