Commit 09201358 by Scott

Extract favorites processing logic to functions

parent 1fc64caf
......@@ -75,6 +75,121 @@
}
function qa_favorite_q_list_view($questions, $usershtml)
/*
Returns content to set in $qa_content['q_list'] for a user's favorite $questions. Pre-generated
user HTML in $usershtml.
*/
{
$q_list = array(
'qs' => array(),
);
if (count($questions) === 0)
return $q_list;
$q_list['form'] = array(
'tags' => 'method="post" action="'.qa_self_html().'"',
'hidden' => array(
'code' => qa_get_form_security_code('vote'),
),
);
$defaults = qa_post_html_defaults('Q');
foreach ($questions as $question) {
$q_list['qs'][] = qa_post_html_fields($question, qa_get_logged_in_userid(), qa_cookie_get(),
$usershtml, null, qa_post_html_options($question, $defaults));
}
return $q_list;
}
function qa_favorite_users_view($users, $usershtml)
/*
Returns content to set in $qa_content['ranking_users'] for a user's favorite $users. Pre-generated
user HTML in $usershtml.
*/
{
if (QA_FINAL_EXTERNAL_USERS)
return null;
require_once QA_INCLUDE_DIR.'qa-app-users.php';
$ranking = array(
'items' => array(),
'rows' => ceil(count($users)/qa_opt('columns_users')),
'type' => 'users',
);
foreach ($users as $user) {
$avatarhtml = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'],
$user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true);
$ranking['items'][] = array(
'avatar' => $avatarhtml,
'label' => $usershtml[$user['userid']],
'score' => qa_html(number_format($user['points'])),
'raw' => $user,
);
}
return $ranking;
}
function qa_favorite_tags_view($tags)
/*
Returns content to set in $qa_content['ranking_tagss'] for a user's favorite $tags.
*/
{
$ranking = array(
'items' => array(),
'rows' => ceil(count($tags)/qa_opt('columns_tags')),
'type' => 'tags'
);
foreach ($tags as $tag) {
$ranking['items'][] = array(
'label' => qa_tag_html($tag['word'], false, true),
'count' => number_format($tag['tagcount']),
);
}
return $ranking;
}
function qa_favorite_categories_view($categories)
/*
Returns content to set in $qa_content['nav_list_categories'] for a user's favorite $categories.
*/
{
$nav_list_categories = array(
'nav' => array(),
'type' => 'browse-cat',
);
foreach ($categories as $category) {
$cat_url = qa_path_html( 'questions/' . implode( '/', array_reverse(explode('/', $category['backpath'])) ) );
$cat_anchor = $category['qcount'] == 1
? qa_lang_html_sub('main/1_question', '1', '1')
: qa_lang_html_sub('main/x_questions', number_format($category['qcount']));
$cat_descr = strlen($category['content']) ? qa_html(' - '.$category['content']) : '';
$nav_list_categories['nav'][$category['categoryid']] = array(
'label' => qa_html($category['title']),
'state' => 'open',
'favorited' => true,
'note' => ' - <a href="'.$cat_url.'">'.$cat_anchor.'</a>'.$cat_descr,
);
}
return $nav_list_categories;
}
/*
Omit PHP closing tag to help avoid accidental output
*/
\ No newline at end of file
......@@ -31,6 +31,7 @@
require_once QA_INCLUDE_DIR.'qa-db-selects.php';
require_once QA_INCLUDE_DIR.'qa-app-format.php';
require_once QA_INCLUDE_DIR.'qa-app-favorites.php';
// Check that we're logged in
......@@ -62,94 +63,31 @@
// Favorite questions
$qa_content['q_list'] = array(
'title' => count($questions) ? qa_lang_html('main/nav_qs') : qa_lang_html('misc/no_favorite_qs'),
'qs' => array(),
);
if (count($questions)) {
$qa_content['q_list']['form'] = array(
'tags' => 'method="post" action="'.qa_self_html().'"',
'hidden' => array(
'code' => qa_get_form_security_code('vote'),
),
);
$defaults = qa_post_html_defaults('Q');
foreach ($questions as $question) {
$qa_content['q_list']['qs'][] = qa_post_html_fields($question, $userid, qa_cookie_get(),
$usershtml, null, qa_post_html_options($question, $defaults));
}
}
$qa_content['q_list'] = qa_favorite_q_list_view($questions, $usershtml);
$qa_content['q_list']['title'] = count($questions) ? qa_lang_html('main/nav_qs') : qa_lang_html('misc/no_favorite_qs');
// Favorite users
if (!QA_FINAL_EXTERNAL_USERS) {
$qa_content['ranking_users'] = array(
'title' => count($users) ? qa_lang_html('main/nav_users') : qa_lang_html('misc/no_favorite_users'),
'items' => array(),
'rows' => ceil(count($users)/qa_opt('columns_users')),
'type' => 'users'
);
foreach ($users as $user) {
$avatarhtml = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'],
$user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true);
$qa_content['ranking_users']['items'][] = array(
'avatar' => $avatarhtml,
'label' => $usershtml[$user['userid']],
'score' => qa_html(number_format($user['points'])),
'raw' => $user,
);
}
$qa_content['ranking_users'] = qa_favorite_users_view($users, $usershtml);
$qa_content['ranking_users']['title'] = count($users) ? qa_lang_html('main/nav_users') : qa_lang_html('misc/no_favorite_users');
}
// Favorite tags
if (qa_using_tags()) {
$qa_content['ranking_tags'] = array(
'title' => count($tags) ? qa_lang_html('main/nav_tags') : qa_lang_html('misc/no_favorite_tags'),
'items' => array(),
'rows' => ceil(count($tags)/qa_opt('columns_tags')),
'type' => 'tags'
);
foreach ($tags as $tag) {
$qa_content['ranking_tags']['items'][] = array(
'label' => qa_tag_html($tag['word'], false, true),
'count' => number_format($tag['tagcount']),
);
}
$qa_content['ranking_tags'] = qa_favorite_tags_view($tags);
$qa_content['ranking_tags']['title'] = count($tags) ? qa_lang_html('main/nav_tags') : qa_lang_html('misc/no_favorite_tags');
}
// Favorite categories
if (qa_using_categories()) {
$qa_content['nav_list_categories'] = array(
'title' => count($categories) ? qa_lang_html('main/nav_categories') : qa_lang_html('misc/no_favorite_categories'),
'nav' => array(),
'type' => 'browse-cat',
);
foreach ($categories as $category) {
$cat_url = qa_path_html( 'questions/' . implode( '/', array_reverse(explode('/', $category['backpath'])) ) );
$cat_anchor = $category['qcount'] == 1
? qa_lang_html_sub('main/1_question', '1', '1')
: qa_lang_html_sub('main/x_questions', number_format($category['qcount']));
$cat_descr = strlen($category['content']) ? qa_html(' - '.$category['content']) : '';
$qa_content['nav_list_categories']['nav'][$category['categoryid']] = array(
'label' => qa_html($category['title']),
'state' => 'open',
'favorited' => true,
'note' => ' - <a href="'.$cat_url.'">'.$cat_anchor.'</a>'.$cat_descr,
);
}
$qa_content['nav_list_categories'] = qa_favorite_categories_view($categories);
$qa_content['nav_list_categories']['title'] = count($categories) ? qa_lang_html('main/nav_categories') : qa_lang_html('misc/no_favorite_categories');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment