Просмотр исходного кода

TableMsgs limit 20, load more rows

Piotr Labudda 10 лет назад
Родитель
Сommit
28b08e3c02
1 измененных файлов с 155 добавлено и 14 удалено
  1. 155 14
      SE/se-lib/Route/TableMsgs.php

+ 155 - 14
SE/se-lib/Route/TableMsgs.php

@@ -6,6 +6,8 @@ Lib::loadClass('TypespecialVariable');
 
 class Route_TableMsgs extends RouteBase {
 
+	var $_listLimit = 20;
+
 	public function handleAuth() {
 		if (!User::logged()) {
 			User::authByRequest();
@@ -105,13 +107,13 @@ class Route_TableMsgs extends RouteBase {
 		</ul>
 		<div class="tab-content" style="margin-bottom:15px">
 			<div role="tabpanel" class="tab-pane active" id="odebrane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
-				<?php $this->_printTableMsgsList($msgsList, $idTable, $idRow, 'read'); ?>
+				<?php $this->_printTableMsgsList('inbox', $msgsList, $idTable, $idRow); ?>
 			</div>
 			<div role="tabpanel" class="tab-pane" id="wyslane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
-				<?php $this->_printTableMsgsList($sentMsgsList, $idTable, $idRow, 'view'); ?>
+				<?php $this->_printTableMsgsList('sent', $sentMsgsList, $idTable, $idRow); ?>
 			</div>
 			<div role="tabpanel" class="tab-pane" id="kosz" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
-				<?php $this->_printTableMsgsList($removedMsgsList, $idTable, $idRow, 'view'); ?>
+				<?php $this->_printTableMsgsList('removed', $removedMsgsList, $idTable, $idRow); ?>
 			</div>
 		</div>
 	</div>
@@ -128,6 +130,114 @@ class Route_TableMsgs extends RouteBase {
 		</div>
 	</div>
 </div>
+<script>
+function tblMsgsLoadMoreRows(n) {
+	var nNode = jQuery(n),
+			lastMsgId = nNode.data('last_msg_id'),
+			listType = nNode.data('list_type')
+	;
+	nNode.blur();
+
+	function tblMsgsSetNoMoreRows(btnLoadMoreNode) {
+		btnLoadMoreNode.closest('td').css({color:'silver'}).html('Brak starszych wiadomości');
+	}
+
+	if (lastMsgId <= 0) {
+		tblMsgsSetNoMoreRows(nNode);
+	}
+
+	function tblMsgsAddMsgToList(msg, btnLoadMoreNode, listType) {
+		var tbodyNode = btnLoadMoreNode.closest('tfoot').prev('tbody'),
+				trNode = jQuery('<tr></tr>'),
+				tdIdNode = jQuery('<td></td>'),
+				tdMsgNode = jQuery('<td></td>'),
+				tdDateNode = jQuery('<td style="white-space:nowrap;"></td>'),
+				actionTask = (listType == 'inbox')? 'read' : 'view',
+				msgLink = ''
+		;
+		trNode.addClass('tblMsgsListItem');
+		if (msg['_read']) trNode.addClass('active');
+		if ('read' === actionTask || 'view' === actionTask) {
+			msgLink = '<?php echo Request::getPathUri() . 'index.php?_route=TableMsgs'; ?>';
+			msgLink += '&idTable=<?php echo $idTable; ?>&idRow=<?php echo $idRow; ?>';
+			msgLink += '&id=' + msg['_raw']['ID'];
+			msgLink += '&_task=' + actionTask;
+			trNode.attr('onclick', "window.location.href='" + msgLink + "'");
+		}
+
+		tdIdNode.append(msg['_raw']['ID']);
+		tdIdNode.appendTo(trNode);
+
+		tdMsgNode.append('<div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;">' + msg['message'] + '</div>');
+		msgMetaInfo = 'od ' + msg['_raw']['A_RECORD_CREATE_AUTHOR'] + ' do ';
+		if ('everyone' == msg['_raw']['userTargetType']) {
+			msgMetaInfo += 'wszystkich';
+		} else if ('user' == msg['_raw']['userTargetType']) {
+			msgMetaInfo += msg['_raw']['userTargetName'];
+		} else if ('group' == msg['_raw']['userTargetType']) {
+			msgMetaInfo += 'grupy ' + msg['_raw']['userTargetName'];
+		}
+		tdMsgNode.append('<div class="text-muted" style="font-style:italic;">' + msgMetaInfo + '</div>');
+		tdMsgNode.appendTo(trNode);
+
+		tdDateNode.append(msg['_raw']['A_RECORD_CREATE_DATE']);
+		if (msg['_readDate']) {
+			tdDateNode.append('<div class="text-muted" style="font-style:italic" title="Przeczytano ' + msg['_readDate'] + '">' + msg['_readDate'] + '</div>');
+		} else {
+			tdDateNode.append('<div class="text-muted" style="font-style:italic" title="Wiadomość nie została jeszcze odczytana">nieodczytana</div>');
+		}
+		tdDateNode.appendTo(trNode);
+
+		trNode.hide();
+		trNode.appendTo(tbodyNode);
+		trNode.show('slow');
+	};
+
+	jQuery.ajax({
+		data: {},
+		dataType: 'json',
+		type: "POST",
+		url: 'index.php?_route=TableMsgs&_task=loadMoreRows&listType=' + listType + '&lastMsgId=' + lastMsgId + '&tableName=<?php echo $tableName; ?>' + '&idRow=<?php echo $idRow; ?>'
+	})
+	.done(function(data, textStatus, jqXHR) {
+		var listLimit = <?php echo $this->_listLimit; ?>,
+				i = 0,
+				lastMsgId = 0,
+				hasMore = false
+		;
+		if (!data || !data.msgs || !data.keysOrder) {
+			jQuery.notify('Wystąpiły błędy podczas pobierania listy wiadomości', 'error');
+			return false;
+		}
+		data.keysOrder.forEach(function(key) {
+			if (i < listLimit) {
+				lastMsgId = key;
+				tblMsgsAddMsgToList(data.msgs[key], nNode, listType);
+			} else {
+				hasMore = true;
+			}
+			i++;
+		});
+		if (!hasMore) {
+			tblMsgsSetNoMoreRows(nNode);
+		}
+		nNode.data('last_msg_id', lastMsgId);
+	})
+	.fail(function(jqXHR) {
+		if (jqXHR.responseJSON) {
+			jQuery.notify('Nie udało się pobrać listy wiadomości', 'error');
+		}
+		else {
+			var txt = jqXHR.responseText || 'Nie udało się pobrać listy wiadomości';
+			if (jqXHR.status == 404) {
+				jQuery.notify(jqXHR.responseText, 'error');
+			} else {
+				jQuery.notify(jqXHR.responseText, 'warn');
+			}
+		}
+	});
+}
+</script>
 <?php
 		//DBG::_(true, true, "_POST", $_POST, __CLASS__, __FUNCTION__, __LINE__);
 		//DBG::_(true, true, "tblAcl", $tblAcl, __CLASS__, __FUNCTION__, __LINE__);
@@ -136,8 +246,27 @@ class Route_TableMsgs extends RouteBase {
 		//throw new Exception("TODO: ...");
 	}
 
-	public function _printTableMsgsList($msgsList, $idTable, $idRow, $actionTask = null) {
+	public function loadMoreRowsAction() {
+		$tableName = V::get('tableName', '', $_GET, 'word');
+		$idRow = V::get('idRow', 0, $_GET, 'int');
+		$lastMsgId = V::get('lastMsgId', 0, $_GET, 'int');
+		$listType = V::get('listType', '', $_GET, 'word');
+		if ($idRow <= 0) throw new HttpException("Wrong param id row", 404);
+		if (!$tableName) throw new HttpException("Wrong param table name", 404);
+		if ($lastMsgId <= 0) throw new HttpException("Wrong param lastMsgId", 404);
+		if (!in_array($listType, array('inbox','sent','removed'))) throw new HttpException("Wrong param listType", 404);
+
+		$resultData = new stdClass();
+		$resultData->msgs = $this->_getMsgs($listType, $tableName, $idRow, $lastMsgId);
+		$resultData->keysOrder = array_keys($resultData->msgs);
+		echo json_encode($resultData);
+	}
+
+	public function _printTableMsgsList($listType, $msgsList, $idTable, $idRow) {
 		$msgsTotal = count($msgsList);
+		$listLimit = $this->_listLimit;
+		$lastMsgId = 0;
+		$actionTask = ($listType == 'inbox')? 'read' : 'view';
 		?>
 	<table class="tblMsgsList table table-hovered" style="margin-bottom:0; table-layout:fixed;">
 		<thead>
@@ -147,20 +276,13 @@ class Route_TableMsgs extends RouteBase {
 				<th style="width:130px">data</th>
 			</tr>
 		</thead>
-		<tfoot>
-			<?php if ($msgsTotal > 20) : ?>
-				<tr>
-					<td colspan="3"><em class="text-muted" style="padding-left:60px;">...</em></td>
-				</tr>
-			<?php endif; ?>
-		</tfoot>
 		<tbody>
 			<?php if ($msgsTotal <= 0) : ?>
 				<tr>
 					<td colspan="3"><em class="text-muted" style="padding-left:60px;">Brak wiadomości</em></td>
 				</tr>
 			<?php else : ?>
-				<?php foreach ($msgsList as $msg) : ?>
+				<?php $i = 0; foreach ($msgsList as $idMsg => $msg) : $i++; if ($i > $listLimit) break; $lastMsgId = $idMsg; ?>
 					<?php
 						$onClick = '';
 						$msgLink = Request::getPathUri() . 'index.php?_route=TableMsgs&id=' . $msg['_raw']->ID;
@@ -196,17 +318,32 @@ class Route_TableMsgs extends RouteBase {
 							<?php echo $msg['_raw']->A_RECORD_CREATE_DATE; ?>
 							<?php if ($msg['_readDate']) : ?>
 								<div class="text-muted" style="font-style:italic" title="Przeczytano <?php echo $msg['_readDate']; ?>"><?php echo $msg['_readDate']; ?></div>
+							<?php else : ?>
+								<div class="text-muted" style="font-style:italic" title="Wiadomość nie została jeszcze odczytana">nieodczytana</div>
 							<?php endif; ?>
 						</td>
 					</tr>
 				<?php endforeach; ?>
 			<?php endif; ?>
 		</tbody>
+		<tfoot>
+			<?php if ($msgsTotal > $listLimit) : ?>
+				<tr class="active">
+					<td colspan="3" style="text-align:center">
+						<button class="btn btn-link"
+										data-last_msg_id="<?php echo $lastMsgId; ?>"
+										data-list_type="<?php echo $listType; ?>"
+										onclick="return tblMsgsLoadMoreRows(this);">pobierz starsze wiadomości ...</button>
+					</td>
+				</tr>
+			<?php endif; ?>
+		</tfoot>
 	</table>
 <?php
 	}
 
-	public function _getMsgs($filterType, $tableName, $idRow) {
+	public function _getMsgs($filterType, $tableName, $idRow, $lastMsgId = null) {
+		$lastMsgId = (int)$lastMsgId;
 		$msgsRoute = Router::getRoute('Msgs');
 		//$msgsList = $msgsRoute->getMessagesForTableRecord($tableName, $idRow);
 		$msgsList = array();
@@ -245,13 +382,17 @@ class Route_TableMsgs extends RouteBase {
 		$db = DB::getDB();
 		$tableName = $db->_($tableName);
 
+		if ($lastMsgId > 0) {
+			$sqlWhereAddFilter .= "\n  and m.`ID`<{$lastMsgId}";
+		}
+		$sqlLimit = $this->_listLimit + 1;
 		$sql = "select m.*
 			from `CRM_UI_MSGS` m
 			where m.`uiTargetType`='default_db_table_record'
 				and m.`uiTargetName`='{$tableName}.{$idRow}'
 				{$sqlWhereAddFilter}
 			order by m.`ID` DESC
-			limit 21
+			limit {$sqlLimit}
 		";
 		$db = DB::getDB();
 		$res = $db->query($sql);