فهرست منبع

added order Typespecial by user hist

Piotr Labudda 9 سال پیش
والد
کامیت
a8f44c2866
2فایلهای تغییر یافته به همراه114 افزوده شده و 22 حذف شده
  1. 3 0
      SE/se-lib/TableAjax.php
  2. 111 22
      SE/se-lib/Typespecial.php

+ 3 - 0
SE/se-lib/TableAjax.php

@@ -5720,6 +5720,9 @@ jQuery(document).ready(function(){
 				if (!empty($vItem->exports)) {
 					$itemJson->exports = $vItem->exports;
 				}
+				if (!empty($vItem->{'$order'})) {
+					$itemJson->{'$order'} = $vItem->{'$order'};
+				}
 				$jsonData[] = $itemJson;
 			}
 		}

+ 111 - 22
SE/se-lib/Typespecial.php

@@ -81,6 +81,10 @@ class Typespecial extends TypespecialBase {
 		$this->fldID = $fldID;
 		$this->param_out = V::get('param_out', null, $cacheData);
 		$this->sqlQuery = V::get('sqlQuery', null, $cacheData);
+		if (empty($this->sqlQuery->from) && !empty($this->sqlQuery->form)) {
+			$this->sqlQuery->from = $this->sqlQuery->form;
+			unset($this->sqlQuery->form);
+		}
 		$this->tblAliasList = V::get('tblAliasList', null, $cacheData);
 		if(V::get('DBG_TS', 0, $_GET) > 3){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial cacheData (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($cacheData);echo'</pre>';}
 
@@ -494,12 +498,12 @@ if(V::get('DBG_TS', 0, $_GET) > 0){echo "\nTODO: SQL_EQUAL count(sqlValues) < 2\
 	public function buildSqlQuery() {
 		$this->sqlQuery = new stdClass();
 		$this->sqlQuery->select = array();
-		$this->sqlQuery->form = array();
+		$this->sqlQuery->from = array();
 		$this->sqlQuery->where = '';
 		$this->sqlQuery->orderBy = '';
 		$this->sqlQuery->limit = '';
 		foreach ($this->fldAliasList as $kTblID => $vFields) {
-			$this->sqlQuery->form[] = "`{$this->tblAliasList[$kTblID]}` as t_{$kTblID}";
+			$this->sqlQuery->from[] = "`{$this->tblAliasList[$kTblID]}` as t_{$kTblID}";
 			foreach ($vFields as $kFldID => $vFldName) {
 				$this->sqlQuery->select[] = "`t_{$kTblID}`.`{$vFldName}` as f_{$kFldID}";
 			}
@@ -709,7 +713,7 @@ jQuery('#typeahead-{$fName}').typeahead({
 		valueField: 'id',
 		labelField: 'id',
 		searchField: 'name',
-		sortField: 'name',
+		// sortField: 'name',
 		create: {$jsonAllowCreate},
 		delimiter: ';',
 		dataAttr: 'typespecial',
@@ -760,6 +764,7 @@ jQuery('#typeahead-{$fName}').typeahead({
 			}
 		},
 		score: function(query) {
+			// console.log('Typespecial({$fName})::score: q(', query, ')');
 			var score = this.getScoreFunction(query);
 			return function(item) {
 				var retScore = 0, lName = ('' + item.name).toLowerCase(), lQuery = query.toLowerCase();
@@ -775,7 +780,7 @@ jQuery('#typeahead-{$fName}').typeahead({
 						retScore = 0;
 					}
 				}
-				//if (retScore > 0) console.log('score: q(', lQuery, ') , retScore(', retScore, '), score(', score(item), '), item.name(', item.name, ') item.id:', item.id);
+				// console.log('Typespecial({$fName})::score: q(', lQuery, ') , retScore(', retScore, '), score(', score(item), '), item.name(', item.name, ') item.id:', item.id);
 				return retScore;
 			};
 		},
@@ -790,13 +795,18 @@ jQuery('#typeahead-{$fName}').typeahead({
 				},
 				success: function(res) {
 					var i, prefix, prefixLen;
+					// res.sort(function(a, b) {// TODO: use \$order from reqeust
+					// 	if (!a['\$order'])
+					// })
 					for (i in res) {
 						prefix = '' + res[i].id;
 						prefixLen = prefix.length;
 						if (prefixLen > 0 && prefix !== res[i].name.substr(0, prefixLen)) {
 							res[i].name = res[i].id + ': ' + res[i].name;
 						}
+						res[i]['\$order'] = 1 + (parseInt(i) || 0);// set order from request
 					}
+					// console.log('Typespecial({$fName})::res',res);
 					callback(res);
 				}
 			});
@@ -812,8 +822,8 @@ jQuery('#typeahead-{$fName}').typeahead({
 	public function _getValues($query) {
 		$rows = array();
 		$db = DB::getDB();// TODO: get from DB by zasoby ID
-if(V::get('DBG_TS', 0, $_GET) > 1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">sqlTablesInfo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SESSION['Typespecial_Cache']['sqlTablesInfo']);echo'</pre>';}// TODO: RMME
-if(V::get('DBG_TS', 0, $_GET) > 1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">this (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this);echo'</pre>';}// TODO: RMME
+		DBG::log(['msg'=>"session", $_SESSION['Typespecial_Cache']['sqlTablesInfo']]);
+		DBG::log(['msg'=>"this", $this]);
 		$query = trim($query);
 		$sqlWhereAdd = "";
 		if (!empty($query)) {
@@ -825,7 +835,7 @@ if(V::get('DBG_TS', 0, $_GET) > 1){echo'<pre style="max-height:200px;overflow:au
 					$qArr[] = $q;
 				}
 			}
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">qArr (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($qArr);echo'</pre>';}// TODO: RMME
+			DBG::log(['msg'=>"qArr", $qArr]);
 
 			if (!empty($qArr)) {
 				$sqlWhereAnd = array();
@@ -856,34 +866,110 @@ if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 		if ($sqlLimit <= 0) {
 			$sqlLimit = 20;
 		}
-		$sqlLimit = "limit {$sqlLimit}";
+
+		$histRows = array();
+		{
+			if (empty($query)) {
+				if (V::get('TEST_SORT', '', $_GET)) echo '{' . "\n";
+				if (V::get('TEST_SORT', '', $_GET)) echo '"this": ' . json_encode($this) . ",";
+				if (!empty($this->fldID) && is_numeric($this->fldID)) {
+					$fldName = $this->param_out->alias[ $this->param_out->retID ];
+					$zasobCellInfo = DB::getPDO()->fetchFirst("
+						select z.`DESC` as fldName, t.`DESC` as tblName
+						from CRM_LISTA_ZASOBOW z
+							join CRM_LISTA_ZASOBOW t on(t.ID = z.PARENT_ID)
+						where z.ID = '{$this->fldID}'
+							and z.`TYPE` = 'KOMORKA'
+							and t.`TYPE` = 'TABELA'
+					");
+					if (!empty($zasobCellInfo)) {
+						$fldName = $zasobCellInfo['fldName'];
+						$tblName = $zasobCellInfo['tblName'];
+						if (V::get('TEST_SORT', '', $_GET)) echo '"field_name": "'.$fldName.'",';
+						if (V::get('TEST_SORT', '', $_GET)) echo '"table_name": "'.$tblName.'",';
+						$userLogin = V::get('TEST_SORT_LOGIN', User::getLogin(), $_REQUEST);
+						try {
+							$histRows = DB::getPDO()->fetchAll("
+								select h.`{$fldName}`
+										, count(1) as cnt
+								from `{$tblName}_HIST` h
+								where h.A_RECORD_UPDATE_AUTHOR = '{$userLogin}'
+									and h.`{$fldName}` != 'N/S;'
+									and h.`{$fldName}` != ''
+									and h.`{$fldName}` != '0'
+									and h.`{$fldName}` IS NOT NULL
+									and h.`{$fldName}` != 'NULL'
+								group by h.`{$fldName}`
+								order by cnt desc
+								limit 10
+							");
+						} catch (Exception $e) {
+							if (V::get('TEST_SORT', '', $_GET)) echo '"Exception": ' . json_encode($e) . ',';
+						}
+						if (V::get('TEST_SORT', '', $_GET)) echo '"hist_rows_stats": ' . json_encode($histRows) . ',';
+					}
+				}
+			}
+		}
 		$sql = "select " . implode("\n\t\t,", $this->sqlQuery->select) . "
-			from " . implode(", ", $this->sqlQuery->form) . "
+			from " . implode(", ", $this->sqlQuery->from) . "
 			where {$this->sqlQuery->where}
 				{$sqlWhereAdd}
-			{$sqlLimit}
+			limit {$sqlLimit}
 		";
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
+		DBG::log($sql, 'sql');
 		$res = $db->query($sql);
 		while ($r = $db->fetch($res)) {
 			$retID = V::get("f_{$this->param_out->retID}", '', $r);
 			$rows[$retID] = $r;
 		}
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">rows (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows);echo'</pre>';}
-
+		if (!empty($histRows)) {
+			$sordedRows = array();
+			$orderIndex = 1000;
+			foreach ($histRows as $histRow) {
+				$retValue = $histRow[$fldName];
+				if (array_key_exists($retValue, $rows)) {
+					$sordedRows[$retValue] = $rows[$retValue];
+					$sordedRows[$retValue]->{'$order'} = $orderIndex--;
+					unset($rows[$retValue]);
+				} else {
+					try {
+						$sordedRows[$retValue] = (object)DB::getPDO()->fetchFirst("
+							select " . implode("\n\t\t,", $this->sqlQuery->select) . "
+							from " . implode(", ", $this->sqlQuery->from) . "
+							where {$this->sqlQuery->where}
+								{$sqlWhereAdd}
+							having `f_{$this->param_out->retID}` = '{$retValue}'
+						");
+						if (V::get('TEST_SORT', '', $_GET)) echo '"test_fetch_'.$retValue.'":' . json_encode($sordedRows[$retValue]) . ",";
+						if (!empty($sordedRows[$retValue])) {
+							$sordedRows[$retValue]->{'$order'} = $orderIndex--;
+						}
+					} catch (Exception $e) {
+						DBG::log($e);
+					}
+				}
+			}
+			foreach ($rows as $key => $val) {
+				$sordedRows[$key] = $val;
+			}
+			$rows = $sordedRows;
+		}
+		if (V::get('TEST_SORT', '', $_GET)) echo '"rows":' . json_encode($rows) . ",";
+		if (V::get('TEST_SORT', '', $_GET)) die('"end":"end"' . "\n" . '}');
+		DBG::log(['msg'=>"rows", $rows]);
 		return $rows;
 	}
 
 	public function getValues($query) {
 		$rows = $this->_getValues($query);
-
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">param_out (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->param_out);echo'</pre>';}
+		DBG::log(['msg'=>"param_out", $this->param_out]);
 		{// convert param_out
 			$format = $this->param_out->format;
 			foreach ($this->param_out->values as $kFldName => $vFldID) {
 				$format = str_replace("{{$kFldName}}", "{f_{$vFldID}}", $format);
 			}
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">format (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($format);echo'</pre>';}
+			DBG::log(['msg'=>"format", $format]);
 		}
 
 		$rows_out = array();
@@ -894,20 +980,20 @@ if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 			}
 			$rows_out[$ind] = $row_out;
 		}
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">rows_out (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows_out);echo'</pre>';}
+		DBG::log(['msg'=>"rows_out", $rows_out]);
 		return $rows_out;
 	}
 
 	public function getValuesWithExports($query) {
 		$rows = $this->_getValues($query);
 
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">param_out (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->param_out);echo'</pre>';}
+		DBG::log(['msg'=>"param_out", $this->param_out]);
 		{// convert param_out
 			$format = $this->param_out->format;
 			foreach ($this->param_out->values as $kFldName => $vFldID) {
 				$format = str_replace("{{$kFldName}}", "{f_{$vFldID}}", $format);
 			}
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">format (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($format);echo'</pre>';}
+			DBG::log(['msg'=>"format", $format]);
 		}
 
 		$retObjList = array();
@@ -932,9 +1018,12 @@ if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 					$vObj->exports[$kExportID] = $vExportOut;
 				}
 			}
+			if (!empty($r->{'$order'})) {
+				$vObj->{'$order'} = $r->{'$order'};
+			}
 			$retObjList[$ind] = $vObj;
 		}
-if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid orange;text-align:left;">retObjList (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($retObjList);echo'</pre>';}
+		DBG::log(['msg'=>"retObjList", $retObjList]);
 		return $retObjList;
 	}
 
@@ -975,7 +1064,7 @@ if(V::get('DBG_TS', 0, $_GET) > 1){echo'<pre style="max-height:200px;overflow:au
 		}
 //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">$vTblInfo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($vTblInfo);echo'</pre>';
 
-		$sqlFrom = $this->sqlQuery->form;
+		$sqlFrom = $this->sqlQuery->from;
 		$sqlSelect = $this->sqlQuery->select;
 		if ($this->param_out->retID > 0) {
 			$sqlWhereAdd[] = "`tr_{$tblId}`.`ID`={$rowId}";
@@ -1078,7 +1167,7 @@ if(V::get('DBG_TS', 0, $_GET) > 1){echo'<pre style="max-height:200px;overflow:au
 		$sqlLimit = "limit {$sqlLimit}";
 		$sql = "select " . implode("\n\t\t,", $this->sqlQuery->select) . "
 				, `t_{$tblId}`.`ID`
-			from " . implode(", ", $this->sqlQuery->form) . "
+			from " . implode(", ", $this->sqlQuery->from) . "
 			where {$this->sqlQuery->where}
 				{$sqlWhereAdd}
 			{$sqlLimit}