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

added sort fields for AntAcl in Storage AclStruct

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

+ 145 - 14
SE/se-lib/Route/Storage/AclStruct.php

@@ -347,6 +347,20 @@ class Route_Storage_AclStruct extends RouteBase {
 					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
+				}
+			");
 		}
 
 		if ('setFieldRefConfig' === V::get('_postTask', '', $_POST)) {
@@ -571,21 +585,8 @@ class Route_Storage_AclStruct extends RouteBase {
 			]);
 		}
 
-		echo UI::h('script', [ 'src' => "static/URI.js" ]);
-		echo UI::h('script', [], "
-			function p5UI_table_sortBy_get(n, argName, colName, dir) {
-				console.log('TODO p5UI_table_sortBy_get :: window.location', window.location)
-				var newUri = URI(window.location.href).setQuery(argName, colName).setQuery(argName+'Dir', dir).build().toString();
-				console.log('TODO p5UI_table_sortBy_get :: uri', newUri)
-				window.location.href = newUri
-			}
-		");
-		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 }
-		");
 		UI::table([
+			'__html_id' => 'struct_table',
 			'caption' => UI::h('span', [], [
 				UI::h('span', [ 'style' => "margin-right:6px;color:#000" ], "Struktura obiektu '{$item['namespace']}'"),
 			]),
@@ -609,6 +610,13 @@ class Route_Storage_AclStruct extends RouteBase {
 			],
 			'rows' => array_map(function ($field) use ($item, $thisGetLink) {
 				$tblItem = [];
+				if (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'];
+					$tblItem['@data']['id_zasob'] = $field['idZasob'];
+					$field['SORT_PRIO'] = ":: {$field['SORT_PRIO']}";
+				}
 				$tblItem['sort'] = V::get('SORT_PRIO', null, $field);
 				foreach ($field as $k => $v) {
 					if ('SORT_PRIO' === $k) continue;
@@ -667,6 +675,82 @@ class Route_Storage_AclStruct extends RouteBase {
 				}
 			}
 		");
+		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'
+								});
+							}).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
+								});
+							})
+						}
+					});
+				});
+			");
+		}
 		if ($item['isObjectActive']) {
 			echo UI::hButtonAjax("Dodaj podstawowy proces dla obiektu '{$item['namespace']}' - read only (TODO)", 'addObjectBaseProcesAjax', [
 				'class' => "btn btn-xs btn-default",
@@ -680,4 +764,51 @@ class Route_Storage_AclStruct extends RouteBase {
 		DBG::nicePrint($item, '$item');
 	}
 
+	public function updateAntAclSortAjaxAction() {
+		return Response::sendTryCatchJson(array($this, 'updateAntAclSortAjax'), $args = 'JSON_FROM_REQUEST_BODY');
+	}
+	public function updateAntAclSortAjax($args) {
+		DBG::log($args, 'array', "args");
+
+		$namespace = V::get('namespace', '', $args);
+		if (empty($namespace)) throw new Exception("Missing param namespace");
+		$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
+		DBG::log($item, 'array', "\$item");
+
+		$sortByArgName = 'sortBy';
+		$sortByCol = V::get($sortByArgName, 'namespace', $args);
+		$sortByDir = V::get("{$sortByArgName}Dir", 'asc', $args);
+		if ('sort' !== $sortByCol) throw new Exception("Only sort by SORT_PRIO allowed to update");
+		if (!in_array($sortByDir, ['asc', 'desc'])) throw new Exception("Wrong order by dir arg");
+		if ('asc' !== $sortByDir) throw new Exception("Only sort asc is allowed"); // DBG
+
+		$sortPrioIdsFromZasoby = DB::getPDO()->fetchAll("
+			select z.ID, z.SORT_PRIO
+			from CRM_LISTA_ZASOBOW z
+			where z.PARENT_ID = :p_id
+			order by z.SORT_PRIO asc, z.ID asc -- default order in Zasoby tree
+		", [
+			':p_id' => $item['idZasob']
+		]);
+		DBG::log($sortPrioIdsFromZasoby, 'array', "\$sortPrioIdsFromZasoby");
+		$idZasobCurrentOrder = array_map(function ($orderRow) {
+			return (int)$orderRow['ID'];
+		}, $sortPrioIdsFromZasoby);
+		DBG::log($idZasobCurrentOrder, 'array', "\$idZasobCurrentOrder");
+		DBG::log(array_diff_assoc($args['sortOrder'], $idZasobCurrentOrder), 'array', "array_diff_assoc(\$args['sortOrder'], \$idZasobCurrentOrder)");
+		DBG::log(array_diff_assoc($idZasobCurrentOrder, $args['sortOrder']), 'array', "array_diff_assoc(\$idZasobCurrentOrder, \$args['sortOrder'])");
+		$orderDiff = array_diff_assoc($args['sortOrder'], $idZasobCurrentOrder);
+		foreach ($orderDiff as $sortPrio => $idZasob) {
+			DB::getPDO()->update('CRM_LISTA_ZASOBOW', 'ID', $idZasob, [
+				'SORT_PRIO' => $sortPrio,
+			]);
+		}
+
+		return [
+			'type' => 'success',
+			'msg' => "OK",
+			'DBG__args' => $args
+		];
+	}
+
 }