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

	File: qa-include/qa-ajax-click-answer.php
	Description: Server-side response to Ajax single clicks on answer


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

Scott committed
23 24 25 26 27 28 29
require_once QA_INCLUDE_DIR . 'app/cookies.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'pages/question-view.php';
require_once QA_INCLUDE_DIR . 'pages/question-submit.php';
require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
30 31


Scott committed
32
// Load relevant information about this answer
Scott committed
33

Scott committed
34 35
$answerid = qa_post_text('answerid');
$questionid = qa_post_text('questionid');
Scott committed
36

Scott committed
37
$userid = qa_get_logged_in_userid();
Scott committed
38

Scott committed
39 40 41 42 43 44
list($answer, $question, $qchildposts, $achildposts) = qa_db_select_with_pending(
	qa_db_full_post_selectspec($userid, $answerid),
	qa_db_full_post_selectspec($userid, $questionid),
	qa_db_full_child_posts_selectspec($userid, $questionid),
	qa_db_full_child_posts_selectspec($userid, $answerid)
);
Scott committed
45 46


Scott committed
47
// Check if there was an operation that succeeded
Scott committed
48

Scott committed
49 50
if (@$answer['basetype'] == 'A' && @$question['basetype'] == 'Q') {
	$answers = qa_page_q_load_as($question, $qchildposts);
Scott committed
51

Scott committed
52 53
	$question = $question + qa_page_q_post_rules($question, null, null, $qchildposts); // array union
	$answer = $answer + qa_page_q_post_rules($answer, $question, $qchildposts, $achildposts);
Scott committed
54

Scott committed
55 56 57 58 59
	if (qa_page_q_single_click_a($answer, $question, $answers, $achildposts, false, $error)) {
		list($answer, $question) = qa_db_select_with_pending(
			qa_db_full_post_selectspec($userid, $answerid),
			qa_db_full_post_selectspec($userid, $questionid)
		);
Scott committed
60 61


Scott committed
62
		// If so, page content to be updated via Ajax
Scott committed
63

Scott committed
64
		echo "QA_AJAX_RESPONSE\n1\n";
Scott committed
65 66


Scott committed
67
		// Send back new count of answers
Scott committed
68

Scott committed
69
		$countanswers = $question['acount'];
Scott committed
70

Scott committed
71 72 73 74
		if ($countanswers == 1)
			echo qa_lang_html('question/1_answer_title');
		else
			echo qa_lang_html_sub('question/x_answers_title', $countanswers);
Scott committed
75 76


Scott committed
77
		// If the answer was not deleted....
Scott committed
78

Scott committed
79 80 81
		if (isset($answer)) {
			$question = $question + qa_page_q_post_rules($question, null, null, $qchildposts); // array union
			$answer = $answer + qa_page_q_post_rules($answer, $question, $qchildposts, $achildposts);
Scott committed
82

Scott committed
83
			$commentsfollows = qa_page_q_load_c_follows($question, $qchildposts, $achildposts);
Scott committed
84

Scott committed
85 86 87
			foreach ($commentsfollows as $key => $commentfollow) {
				$commentsfollows[$key] = $commentfollow + qa_page_q_post_rules($commentfollow, $answer, $commentsfollows, null);
			}
Scott committed
88

Scott committed
89 90
			$usershtml = qa_userids_handles_html(array_merge(array($answer), $commentsfollows), true);
			qa_sort_by($commentsfollows, 'created');
Scott committed
91

Scott committed
92 93
			$a_view = qa_page_q_answer_view($question, $answer, ($answer['postid'] == $question['selchildid'] && $answer['type'] == 'A'),
				$usershtml, false);
Scott committed
94

Scott committed
95
			$a_view['c_list'] = qa_page_q_comment_follow_list($question, $answer, $commentsfollows, false, $usershtml, false, null);
Scott committed
96

Scott committed
97 98
			$themeclass = qa_load_theme_class(qa_get_site_theme(), 'ajax-answer', null, null);
			$themeclass->initialize();
Scott committed
99 100


Scott committed
101
			// ... send back the HTML for it
Scott committed
102

Scott committed
103
			echo "\n";
Scott committed
104

Scott committed
105
			$themeclass->a_list_item($a_view);
Scott committed
106 107
		}

Scott committed
108 109 110
		return;
	}
}
Scott committed
111 112


Scott committed
113
echo "QA_AJAX_RESPONSE\n0\n"; // fall back to non-Ajax submission if something failed