Browse Source

Improved createList providing a new function to check the results and define a class/style for the row based on that
converted the list of a user's attachments to createList

Signed-off-by: emanuele <[email protected]>

emanuele 12 years ago
parent
commit
e97d02e389

+ 127 - 46
Sources/Profile-View.php

@@ -525,46 +525,97 @@ function showAttachments($memID)
 	if (empty($boardsAllowed))
 		$boardsAllowed = array(-1);
 
-	// Get the total number of attachments they have posted.
-	$request = $smcFunc['db_query']('', '
-		SELECT COUNT(*)
-		FROM {db_prefix}attachments AS a
-			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
-			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
-		WHERE a.attachment_type = {int:attachment_type}
-			AND a.id_msg != {int:no_message}
-			AND m.id_member = {int:current_member}' . (!empty($board) ? '
-			AND b.id_board = {int:board}' : '') . (!in_array(0, $boardsAllowed) ? '
-			AND b.id_board IN ({array_int:boards_list})' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
-			AND m.approved = {int:is_approved}'),
-		array(
-			'boards_list' => $boardsAllowed,
-			'attachment_type' => 0,
-			'no_message' => 0,
-			'current_member' => $memID,
-			'is_approved' => 1,
-			'board' => $board,
-		)
-	);
-	list ($attachCount) = $smcFunc['db_fetch_row']($request);
-	$smcFunc['db_free_result']($request);
-
-	$maxIndex = (int) $modSettings['defaultMaxMessages'];
+	require_once($sourcedir . '/Subs-List.php');
 
-	// What about ordering?
-	$sortTypes = array(
-		'filename' => 'a.filename',
-		'downloads' => 'a.downloads',
-		'subject' => 'm.subject',
-		'posted' => 'm.poster_time',
+	// This is all the information required for a group listing.
+	$listOptions = array(
+		'id' => 'attachments',
+		'width' => '100%',
+		'items_per_page' => $modSettings['defaultMaxMessages'],
+		'no_items_label' => $txt['show_attachments_none'],
+		'base_href' => $scripturl . '?action=profile;area=showposts;sa=attach;sort=filename;u=' . $memID,
+		'default_sort_col' => 'filename',
+		'get_items' => array(
+			'function' => 'list_getAttachments',
+			'params' => array(
+				$boardsAllowed,
+				$memID,
+			),
+		),
+		'get_count' => array(
+			'function' => 'list_getNumAttachments',
+			'params' => array(
+				$boardsAllowed,
+				$memID,
+			),
+		),
+		'data_check' => array(
+			'class' => create_function('$data', '
+				return $data[\'approved\'] ? \'\' : \'approvebg\';
+			')
+		),
+		'columns' => array(
+			'filename' => array(
+				'header' => array(
+					'value' => $txt['show_attach_downloads'],
+					'class' => 'lefttext',
+					'style' => 'width: 25%',
+				),
+				'data' => array(
+					'db' => 'filename',
+				),
+				'sort' => array(
+					'default' => 'a.filename',
+					'reverse' => 'a.filename DESC',
+				),
+			),
+			'downloads' => array(
+				'header' => array(
+					'value' => $txt['show_attach_downloads'],
+					'style' => 'width: 12%',
+				),
+				'data' => array(
+					'db' => 'downloads',
+				),
+				'sort' => array(
+					'default' => 'a.downloads',
+					'reverse' => 'a.downloads DESC',
+				),
+			),
+			'subject' => array(
+				'header' => array(
+					'value' => $txt['message'],
+					'class' => 'lefttext',
+					'style' => 'width: 30%',
+				),
+				'data' => array(
+					'db' => 'subject',
+				),
+				'sort' => array(
+					'default' => 'm.subject',
+					'reverse' => 'm.subject DESC',
+				),
+			),
+			'posted' => array(
+				'header' => array(
+					'value' => $txt['show_attach_posted'],
+					'class' => 'lefttext',
+				),
+				'data' => array(
+					'db' => 'posted',
+				),
+			),
+		),
 	);
-	$context['sort_order'] = isset($_GET['sort']) && isset($sortTypes[$_GET['sort']]) ? $_GET['sort'] : 'posted';
-	$context['sort_direction'] = isset($_GET['asc']) ? 'up' : 'down';
 
-	$sort = $sortTypes[$context['sort_order']];
+	// Create the request list.
+	createList($listOptions);
 
-	// Let's get ourselves a lovely page index.
-	$context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showposts;sa=attach;sort=' . $context['sort_order'] . ($context['sort_direction'] == 'up' ? ';asc' : ''), $context['start'], $attachCount, $maxIndex);
+}
+
+function list_getAttachments($start, $items_per_page, $sort, $boardsAllowed, $memID)
+{
+	global $smcFunc, $board, $modSettings, $context;
 
 	// Retrieve some attachments.
 	$request = $smcFunc['db_query']('', '
@@ -588,21 +639,18 @@ function showAttachments($memID)
 			'current_member' => $memID,
 			'is_approved' => 1,
 			'board' => $board,
-			'sort' => $sort . ' ' . ($context['sort_direction'] == 'down' ? 'DESC' : 'ASC'),
-			'offset' => $context['start'],
-			'limit' => $maxIndex,
+			'sort' => $sort,
+			'offset' => $start,
+			'limit' => $items_per_page,
 		)
 	);
-	$context['attachments'] = array();
+	$attachments = array();
 	while ($row = $smcFunc['db_fetch_assoc']($request))
-	{
-		$row['subject'] = censorText($row['subject']);
-
-		$context['attachments'][] = array(
+		$attachments[] = array(
 			'id' => $row['id_attach'],
 			'filename' => $row['filename'],
 			'downloads' => $row['downloads'],
-			'subject' => $row['subject'],
+			'subject' => censorText($row['subject']),
 			'posted' => timeformat($row['poster_time']),
 			'msg' => $row['id_msg'],
 			'topic' => $row['id_topic'],
@@ -610,8 +658,41 @@ function showAttachments($memID)
 			'board_name' => $row['name'],
 			'approved' => $row['approved'],
 		);
-	}
+
 	$smcFunc['db_free_result']($request);
+
+	return $attachments;
+}
+
+function list_getNumAttachments($boardsAllowed, $memID)
+{
+	global $board, $smcFunc, $modSettings, $context;
+
+	// Get the total number of attachments they have posted.
+	$request = $smcFunc['db_query']('', '
+		SELECT COUNT(*)
+		FROM {db_prefix}attachments AS a
+			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
+			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND {query_see_board})
+		WHERE a.attachment_type = {int:attachment_type}
+			AND a.id_msg != {int:no_message}
+			AND m.id_member = {int:current_member}' . (!empty($board) ? '
+			AND b.id_board = {int:board}' : '') . (!in_array(0, $boardsAllowed) ? '
+			AND b.id_board IN ({array_int:boards_list})' : '') . (!$modSettings['postmod_active'] || $context['user']['is_owner'] ? '' : '
+			AND m.approved = {int:is_approved}'),
+		array(
+			'boards_list' => $boardsAllowed,
+			'attachment_type' => 0,
+			'no_message' => 0,
+			'current_member' => $memID,
+			'is_approved' => 1,
+			'board' => $board,
+		)
+	);
+	list ($attachCount) = $smcFunc['db_fetch_row']($request);
+	$smcFunc['db_free_result']($request);
+
+	return $attachCount;
 }
 
 /**

+ 12 - 1
Sources/Subs-List.php

@@ -167,8 +167,19 @@ function createList($listOptions)
 			$cur_row[$column_id] = $cur_data;
 		}
 
+		$list_context['rows'][$item_id]['class'] = '';
+		$list_context['rows'][$item_id]['style'] = '';
+		// Maybe we wat set a custom class for the row based on the data in the row itself
+		if (isset($listOptions['data_check']))
+		{
+			if (isset($listOptions['data_check']['class']))
+				$list_context['rows'][$item_id]['class'] = ' ' . $listOptions['data_check']['class']($list_item);
+			if (isset($listOptions['data_check']['style']))
+				$list_context['rows'][$item_id]['style'] = ' style="' . $listOptions['data_check']['style']($list_item) . '"';
+		}
+
 		// Insert the row into the list.
-		$list_context['rows'][$item_id] = $cur_row;
+		$list_context['rows'][$item_id]['data'] = $cur_row;
 	}
 
 	// The title is currently optional.

+ 2 - 2
Themes/default/GenericList.template.php

@@ -125,9 +125,9 @@ function template_show_list($list_id = null)
 		foreach ($cur_list['rows'] as $id => $row)
 		{
 			echo '
-				<tr class="windowbg', $alternate ? '2' : '', '" id="list_', $list_id, '_', $id, '">';
+				<tr class="windowbg', $alternate ? '2' : '', $row['class'], '"', $row['style'], ' id="list_', $list_id, '_', $id, '">';
 
-			foreach ($row as $row_data)
+			foreach ($row['data'] as $row_data)
 				echo '
 					<td', empty($row_data['class']) ? '' : ' class="' . $row_data['class'] . '"', empty($row_data['style']) ? '' : ' style="' . $row_data['style'] . '"', '>', $row_data['value'], '</td>';
 

+ 12 - 55
Themes/default/Profile.template.php

@@ -338,9 +338,6 @@ function template_showPosts()
 			<h3 class="catbg">
 				', (!isset($context['attachments']) && empty($context['is_topics']) ? $txt['showMessages'] : (!empty($context['is_topics']) ? $txt['showTopics'] : $txt['showAttachments'])), ' - ', $context['member']['name'], '
 			</h3>
-		</div>
-		<div class="pagesection">
-			<span>', $txt['pages'], ': ', $context['page_index'], '</span>
 		</div>';
 
 	// Button shortcuts
@@ -352,6 +349,11 @@ function template_showPosts()
 	// Are we displaying posts or attachments?
 	if (!isset($context['attachments']))
 	{
+		echo '
+		<div class="pagesection">
+			<span>', $txt['pages'], ': ', $context['page_index'], '</span>
+		</div>';
+
 		// For every post to be displayed, give it its own div, and show the important details of the post.
 		foreach ($context['posts'] as $post)
 		{
@@ -414,54 +416,15 @@ function template_showPosts()
 			</div>
 		</div>';
 		}
+
+		// Show more page numbers.
+		echo '
+		<div class="pagesection" style="margin-bottom: 0;">
+			<span>', $txt['pages'], ': ', $context['page_index'], '</span>
+		</div>';
 	}
 	else
-	{
-		echo '
-		<table border="0" width="100%" cellspacing="1" cellpadding="2" class="table_grid" align="center">
-			<thead>
-				<tr class="titlebg">
-					<th class="first_th lefttext" scope="col" width="25%">
-						<a href="', $scripturl, '?action=profile;u=', $context['current_member'], ';area=showposts;sa=attach;sort=filename', ($context['sort_direction'] == 'down' && $context['sort_order'] == 'filename' ? ';asc' : ''), '">
-							', $txt['show_attach_filename'], '
-							', ($context['sort_order'] == 'filename' ? '<img src="' . $settings['images_url'] . '/sort_' . ($context['sort_direction'] == 'down' ? 'down' : 'up') . '.png" alt="" />' : ''), '
-						</a>
-					</th>
-					<th scope="col" width="12%">
-						<a href="', $scripturl, '?action=profile;u=', $context['current_member'], ';area=showposts;sa=attach;sort=downloads', ($context['sort_direction'] == 'down' && $context['sort_order'] == 'downloads' ? ';asc' : ''), '">
-							', $txt['show_attach_downloads'], '
-							', ($context['sort_order'] == 'downloads' ? '<img src="' . $settings['images_url'] . '/sort_' . ($context['sort_direction'] == 'down' ? 'down' : 'up') . '.png" alt="" />' : ''), '
-						</a>
-					</th>
-					<th class="lefttext" scope="col" width="30%">
-						<a href="', $scripturl, '?action=profile;u=', $context['current_member'], ';area=showposts;sa=attach;sort=subject', ($context['sort_direction'] == 'down' && $context['sort_order'] == 'subject' ? ';asc' : ''), '">
-							', $txt['message'], '
-							', ($context['sort_order'] == 'subject' ? '<img src="' . $settings['images_url'] . '/sort_' . ($context['sort_direction'] == 'down' ? 'down' : 'up') . '.png" alt="" />' : ''), '
-						</a>
-					</th>
-					<th class="last_th lefttext" scope="col">
-						<a href="', $scripturl, '?action=profile;u=', $context['current_member'], ';area=showposts;sa=attach;sort=posted', ($context['sort_direction'] == 'down' && $context['sort_order'] == 'posted' ? ';asc' : ''), '">
-						', $txt['show_attach_posted'], '
-						', ($context['sort_order'] == 'posted' ? '<img src="' . $settings['images_url'] . '/sort_' . ($context['sort_direction'] == 'down' ? 'down' : 'up') . '.png" alt="" />' : ''), '
-						</a>
-					</th>
-				</tr>
-			</thead>
-			<tbody>';
-
-		// Looks like we need to do all the attachments instead!
-		$alternate = false;
-		foreach ($context['attachments'] as $attachment)
-		{
-			echo '
-				<tr class="', $attachment['approved'] ? ($alternate ? 'windowbg' : 'windowbg2') : 'approvebg', '">
-					<td><a href="', $scripturl, '?action=dlattach;topic=', $attachment['topic'], '.0;attach=', $attachment['id'], '">',	$attachment['filename'], '</a>', !$attachment['approved'] ? '&nbsp;<em>(' . $txt['awaiting_approval'] . ')</em>' : '', '</td>
-					<td align="center">', $attachment['downloads'], '</td>
-					<td><a href="', $scripturl, '?topic=', $attachment['topic'], '.msg', $attachment['msg'], '#msg', $attachment['msg'], '" rel="nofollow">', $attachment['subject'], '</a></td>
-					<td>', $attachment['posted'], '</td>
-				</tr>';
-			$alternate = !$alternate;
-		}
+		template_show_list('attachments');
 
 	// No posts? Just end the table with a informative message.
 	if ((isset($context['attachments']) && empty($context['attachments'])) || (!isset($context['attachments']) && empty($context['posts'])))
@@ -475,12 +438,6 @@ function template_showPosts()
 		echo '
 			</tbody>
 		</table>';
-	}
-	// Show more page numbers.
-	echo '
-		<div class="pagesection" style="margin-bottom: 0;">
-			<span>', $txt['pages'], ': ', $context['page_index'], '</span>
-		</div>';
 }
 
 // Template for showing all the buddies of the current user.