Ver código fonte

added sort by sort_prio in AntAclBase

Piotr Labudda 8 anos atrás
pai
commit
615e285c57
1 arquivos alterados com 63 adições e 1 exclusões
  1. 63 1
      SE/se-lib/AntAclBase.php

+ 63 - 1
SE/se-lib/AntAclBase.php

@@ -17,6 +17,7 @@ class AntAclBase extends Core_AclBase {
 		$this->_rootNamespace = '';
 		$this->_primaryKey = '';
 		$this->_fields = [];
+		$this->_zasobyInfoFetched = false;
 	}
 	public function getDB() { return $this->_db; }
 	public function getName() { return $this->_name; }
@@ -28,7 +29,68 @@ class AntAclBase extends Core_AclBase {
 	public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
 	public function getVirtualFieldListByIdZasob() { return []; }
 	// public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } // TODO: get visible fields
+
+	public function _fetchInfoFromZasobyIfNeeded() {
+		DBG::log($this->_fields, 'array', "DBG sort fields - \$this->_fields");
+		$fields = $this->getRealFieldListByIdZasob();
+		DBG::log($fields, 'array', "DBG sort fields - \$fields");
+
+		if (!$this->_zasobyInfoFetched) {
+			$zasobyIds = array_filter(
+				array_map(function ($field) {
+					return (int)$field['idZasob'];
+				}, $this->_fields),
+				function ($id) { return $id > 0; }
+			);
+			if (!empty($zasobyIds)) {
+				DBG::log("DBG sort fields - TODO: ids [".implode(",", $zasobyIds)."]");
+				$zasobyInfo = DB::getPDO()->fetchAllByKey("
+					select z.ID, z.DESC_PL, z.OPIS, z.SORT_PRIO
+					from CRM_LISTA_ZASOBOW z
+					where z.ID in(".implode(",", $zasobyIds).")
+				", 'ID');
+				DBG::log($zasobyInfo, 'array', "DBG sort fields - zasobyInfo");
+				$maxSortPrio = 0;
+				array_map(function ($zInfo) use (&$maxSortPrio) {
+					if ($zInfo['SORT_PRIO'] > 0 && $zInfo['SORT_PRIO'] > $maxSortPrio) {
+						$maxSortPrio = $zInfo['SORT_PRIO'];
+					}
+				}, $zasobyInfo);
+				foreach ($this->_fields as $idx => $field) {
+					if ($field['idZasob'] > 0 && array_key_exists($field['idZasob'], $zasobyInfo)) {
+						$this->_fields[$idx]['sort_prio'] = $zasobyInfo[ $field['idZasob'] ]['SORT_PRIO'];
+						if (!empty($zasobyInfo[ $field['idZasob'] ]['DESC_PL'])) $this->_fields[$idx]['label'] = $zasobyInfo[ $field['idZasob'] ]['DESC_PL'];
+						if (!empty($zasobyInfo[ $field['idZasob'] ]['OPIS'])) $this->_fields[$idx]['opis'] = $zasobyInfo[ $field['idZasob'] ]['OPIS'];
+					} else { // !$field['idZasob'] => generate sortPrio
+						$this->_fields[$idx]['sort_prio'] = ++$maxSortPrio;
+					}
+				}
+			}
+			$this->_zasobyInfoFetched = true;
+		}
+		usort($this->_fields, array($this, '_sortFieldsCallback'));
+		DBG::log($this->_fields, 'array', "DBG sort fields - sorted \$this->_fields");
+	}
+	public function _sortFieldsCallback($a, $b) {
+		if ($a['name'] == 'ID') {
+			return -1;
+		}
+		else if ($b['name'] == 'ID') {
+			return 1;
+		}
+		else if ($a['sort_prio'] < $b['sort_prio']) {
+			return -1;
+		}
+		else if ($a['sort_prio'] > $b['sort_prio']) {
+			return 1;
+		}
+		else {
+			return 0;
+		}
+	}
+
 	public function getVisibleFieldListByIdZasob() {
+		$this->_fetchInfoFromZasobyIfNeeded();
 		$fields = $this->getRealFieldListByIdZasob();
 		$pkField = $this->getPrimaryKeyField();
 		$cols = array();
@@ -38,7 +100,7 @@ class AntAclBase extends Core_AclBase {
 				break;
 			}
 		}
-		$cols[$id] = 'ID';
+		$cols[$id] = 'ID'; // TODO: why rename primary key field to ID?
 		foreach ($fields as $kFieldID => $fieldName) {
 			if ($pkField === $fieldName) continue;
 			$cols[$kFieldID] = $fieldName;