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

added sort_prio for TableAcl in Storage AclStruct

Piotr Labudda 8 лет назад
Родитель
Сommit
f5813618cc
1 измененных файлов с 156 добавлено и 16 удалено
  1. 156 16
      SE/se-lib/Route/Storage/AclStruct.php

+ 156 - 16
SE/se-lib/Route/Storage/AclStruct.php

@@ -101,7 +101,7 @@ class Route_Storage_AclStruct extends RouteBase {
 
 		$cellZasobList = array();
 		foreach (DB::getPDO()->fetchAllByKey("
-				select z.ID, z.`DESC`, z.`TYPE` as ZASOB_TYPE, z.A_STATUS
+				select z.ID, z.`DESC`, z.`TYPE` as ZASOB_TYPE, z.A_STATUS, z.SORT_PRIO
 				from CRM_LISTA_ZASOBOW z
 				where z.PARENT_ID = '{$idTable}'
 			", $key = 'DESC') as $ind => $row) {
@@ -109,6 +109,7 @@ class Route_Storage_AclStruct extends RouteBase {
 		}
 
 		$emptyItem = array();
+		$emptyItem['sort'] = null;
 		$emptyItem['name'] = '';
 		$emptyItem['id_zasob'] = '';
 		$emptyItem['zasob_type'] = '';
@@ -141,6 +142,7 @@ class Route_Storage_AclStruct extends RouteBase {
 				$cellZasobList[$lowerCellName]['_checked'] = true;
 				$tblItem['id_zasob'] = $tblZasob['ID'];
 				$tblItem['zasob_type'] = $tblZasob['ZASOB_TYPE'];
+				$tblItem['sort'] = $tblZasob['SORT_PRIO'];
 			} else {
 				$tblItem['uwagi'] .= '!Zasob';//'TODO: ADD ZASOB';
 				$ajaxAddZasobLink = Router::getRoute('Storage')->getLink('addCellToZasoby', [ 'idStorage' => $idStorage, 'tblName' => $tblName, 'cellName' => $cellName]);
@@ -160,20 +162,54 @@ class Route_Storage_AclStruct extends RouteBase {
 				$tableList[] = $tblItem;
 			}
 		}
-		usort($tableList, function($rowA, $rowB) {
-			$a = $rowA['name']; $b = $rowB['name'];
-			if ('ID' == $a) return -1;
-			if ('ID' == $b) return 1;
-			if ($a == $b) return 0;
-			$a1 = substr($a, 0, 1); $b1 = substr($b, 0, 1);
-			if (('_' == $a1 || '_' == $b1) && $a1 != $b1) {
-				return ($a1 < $b1) ? 1 : -1;
+
+		{ // sortBy
+			$sortByArgName = 'sortBy';
+			$sortByCol = V::get($sortByArgName, 'name', $_GET);
+			$sortByDir = V::get("{$sortByArgName}Dir", 'asc', $_GET);
+			if ('name' === $sortByCol) {
+				usort($tableList, function($rowA, $rowB) {
+					$a = $rowA['name']; $b = $rowB['name'];
+					// if ('ID' == $a) return -1;
+					// if ('ID' == $b) return 1;
+					if ($a == $b) return 0;
+					$a1 = substr($a, 0, 1); $b1 = substr($b, 0, 1);
+					if (('_' == $a1 || '_' == $b1) && $a1 != $b1) {
+						return ($a1 < $b1) ? 1 : -1;
+					}
+					return ($a < $b) ? -1 : 1;
+				});
 			}
-			return ($a < $b) ? -1 : 1;
-		});
+			else if ('sort' === $sortByCol) {
+				usort($tableList, function ($a, $b) use ($sortByDir) {
+					if (null === $a['sort'] && null === $b['sort']) return 0;
+					if (null === $a['sort'] && null != $b['sort']) return 1;
+					if (null !== $a['sort'] && null === $b['sort']) return -1;
+					if ($a['sort'] > $b['sort']) return 'asc' === $sortByDir ? 1 : -1;
+					if ($a['sort'] < $b['sort']) return 'asc' === $sortByDir ? -1 : 1;
+					return 0;
+				});
+			}
+			echo UI::h('style', [ 'type' => "text/css" ], "
+				.ta-ordering { cursor:pointer }
+				.ta-ordering-desc:after { content:\"\\e252\"; font-family:\"Glyphicons Halflings\"; line-height:1; margin:0 0 0 3px; display:inline-block; color:#bbb }
+				.ta-ordering-asc:after  { content:\"\\e253\"; font-family:\"Glyphicons Halflings\"; line-height:1; margin:0 0 0 3px; display:inline-block; color:#bbb }
+				.ta-sortable-item { }
+				.ta-sortable-item-handle { cursor:pointer }
+			");
+			echo UI::h('script', [ 'src' => "static/URI.js" ]);
+			echo UI::h('script', [], "
+				function p5UI_table_sortBy_get(n, argName, colName, dir) {
+					var newUri = URI(window.location.href).setQuery(argName, colName).setQuery(argName+'Dir', dir).build().toString();
+					window.location.href = newUri
+				}
+			");
+		}
+
 		UI::table([
+			'__html_id' => 'struct_table',
 			'caption' => UI::h('span', [], [
-				"Komórki [{$idTable}] ",
+				"Struktura tabeli {$tblName} [{$idTable}] ",
 				UI::h('a', [
 					'href' => "index.php?_route=ViewTableAjax&namespace=default_db/{$tblName}",
 					'class' => "btn btn-md btn-link"
@@ -194,8 +230,29 @@ class Route_Storage_AclStruct extends RouteBase {
 					'title' => "Uprawnienia - analiza użycia komórek w procesach"
 				], "Uprawnienia (analiza użycia)"),
 			]),
+			'cols_label' => [
+				'sort' => UI::h('nobr', [
+					'class' => "ta-ordering" . ('sort' === $sortByCol ? " ta-ordering-" . ( 'asc' === $sortByDir ? 'asc' : 'desc' ) : '' ),
+					'onClick' => "return p5UI_table_sortBy_get(this, '{$sortByArgName}', 'sort', '" . ('sort' === $sortByCol ? ( 'asc' === $sortByDir ? 'desc' : 'asc' ) : 'asc' ) . "')",
+					'title' => "Sortuj wg pola SORT_PRIO z zasobów",
+				], "sort"),
+				'name' => UI::h('nobr', [
+					'class' => "ta-ordering" . ('name' === $sortByCol ? " ta-ordering-" . ( 'asc' === $sortByDir ? 'asc' : 'desc' ) : '' ),
+					'onClick' => "return p5UI_table_sortBy_get(this, '{$sortByArgName}', 'name', '" . ('name' === $sortByCol ? ( 'asc' === $sortByDir ? 'desc' : 'asc' ) : 'asc' ) . "')",
+					'title' => "Sortuj wg nazwy",
+				], "namespace"),
+			],
 			'cols' => array_keys($emptyItem),
-			'rows' => $tableList
+			'rows' => array_map(function ($item) use ($sortByCol) {
+				if ('sort' === $sortByCol && null !== $item['sort'] && !empty($item['id_zasob'])) {
+					$item['@class'] = "ta-sortable-item";
+					$item['@class[sort]'] = "ta-sortable-item-handle";
+					$item['@data']['sort_prio'] = $item['sort'];
+					$item['@data']['id_zasob'] = $item['id_zasob'];
+					$item['sort'] = ":: {$item['sort']}";
+				}
+				return $item;
+			}, $tableList),
 		]);
 		echo UI::h('script', [], "
 			jQuery(document).on('p5UIBtnAjax:Storage:addCellToZasoby:click', function(e, n, payload) {
@@ -309,6 +366,86 @@ class Route_Storage_AclStruct extends RouteBase {
 				jQuery.notify(payload.msg, payload.type);
 			});
 		");
+
+		if ('sort' === $sortByCol) {
+			echo UI::h('script', [], "
+				jQuery(document).ready(function (){
+					var tblNode$ = jQuery('#struct_table > tbody')
+					var getOrder = function () {
+						var orderIds = []
+						jQuery('#struct_table > tbody').children('.ta-sortable-item').each(function (idx, trNode) {
+							orderIds.push(jQuery(trNode).data('id_zasob'))
+						})
+						return orderIds
+					}
+					var sortOrder = getOrder()
+					console.log('sortOrder - init', sortOrder)
+					tblNode$.sortable({
+						axis: 'y',
+						handle: '.ta-sortable-item-handle',
+						items: '.ta-sortable-item',
+						update: function (event, ui) {
+							sortOrder = getOrder()
+							tblNode$.sortable('disable')
+							tblNode$.find('.ta-sortable-item-handle').css({color:'silver'})
+
+							console.log('TODO: disable sortable while ajax')
+							// window.setTimeout(function () {
+							// 	tblNode$.find('.ta-sortable-item-handle').css({color:'black'})
+							// 	console.log('TODO: ajax fetch result')
+							// 	tblNode$.sortable('enable')
+							// 	tblNode$.sortable('cancel')
+							// }, 1000)
+
+							window.fetch(
+								URI(window.location.href)
+									.setSearch('_task', 'updateAntAclSortAjax')
+									.setSearch('idZasobOrder', sortOrder)
+									.build().toString(),
+								{
+									method: 'POST',
+									headers: {
+										'Content-Type': 'application/x-www-form-urlencoded' // query string
+									},
+									credentials: 'same-origin',
+									body: JSON.stringify({
+										namespace: URI.parseQuery(window.location.search).namespace,
+										sortBy: '{$sortByCol}',
+										sortByDir: '{$sortByDir}',
+										sortOrder: sortOrder,
+									})
+								}
+							).then(function(response) {
+								return response.json()
+							}).then(function(payload) {
+								console.log('updateAntAclSortAjax response', payload);
+								tblNode$.sortable('enable')
+								if ('success' !== payload.type) {
+									tblNode$.sortable('cancel')
+								}
+								tblNode$.sortable('enable')
+								tblNode$.find('.ta-sortable-item-handle').css({color:'black'})
+								p5UI__notifyAjaxCallback({
+									type: payload.type || 'error',
+									msg: payload.msg || 'Request error'
+								});
+								if ('success' === payload.type) {
+									window.location.reload(true)
+								}
+							}).catch(function(e) {
+								tblNode$.sortable('enable')
+								tblNode$.sortable('cancel')
+								tblNode$.find('.ta-sortable-item-handle').css({color:'black'})
+								p5UI__notifyAjaxCallback({
+									type: 'error',
+									msg: 'Request error ' + e
+								});
+							})
+						}
+					});
+				});
+			");
+		}
 	}
 
 	public function objectStructView($item) {
@@ -605,12 +742,12 @@ class Route_Storage_AclStruct extends RouteBase {
 				'namespace' => UI::h('nobr', [
 					'class' => "ta-ordering" . ('namespace' === $sortByCol ? " ta-ordering-" . ( 'asc' === $sortByDir ? 'asc' : 'desc' ) : '' ),
 					'onClick' => "return p5UI_table_sortBy_get(this, '{$sortByArgName}', 'namespace', '" . ('namespace' === $sortByCol ? ( 'asc' === $sortByDir ? 'desc' : 'asc' ) : 'asc' ) . "')",
-					'title' => "Sortuj wg pola SORT_PRIO z zasobów",
+					'title' => "Sortuj wg nazwy pola",
 				], "namespace"),
 			],
-			'rows' => array_map(function ($field) use ($item, $thisGetLink) {
+			'rows' => array_map(function ($field) use ($item, $thisGetLink, $sortByCol) {
 				$tblItem = [];
-				if (null !== $field['SORT_PRIO'] && !empty($field['idZasob'])) {
+				if ('sort' === $sortByCol && null !== $field['SORT_PRIO'] && !empty($field['idZasob'])) {
 					$tblItem['@class'] = "ta-sortable-item";
 					$tblItem['@class[sort]'] = "ta-sortable-item-handle";
 					$tblItem['@data']['sort_prio'] = $field['SORT_PRIO'];
@@ -737,6 +874,9 @@ class Route_Storage_AclStruct extends RouteBase {
 									type: payload.type || 'error',
 									msg: payload.msg || 'Request error'
 								});
+								if ('success' === payload.type) {
+									window.location.reload(true)
+								}
 							}).catch(function(e) {
 								tblNode$.sortable('enable')
 								tblNode$.sortable('cancel')