浏览代码

fixed _rootTableName in TableAcl

Piotr Labudda 9 年之前
父节点
当前提交
653d6aee21
共有 3 个文件被更改,包括 66 次插入28 次删除
  1. 1 0
      SE/se-lib/Route/Debug.php
  2. 16 14
      SE/se-lib/Route/Storage.php
  3. 49 14
      SE/se-lib/TableAcl.php

+ 1 - 0
SE/se-lib/Route/Debug.php

@@ -261,6 +261,7 @@ class Route_Debug extends RouteBase {
           var pre = document.createElement('pre')
           pre.appendChild( document.createTextNode(n.title) )
           pre.style.maxWidth = '600px'
+          pre.style.fontSize = 'x-small'
           n.parentNode.appendChild(pre)
         }
       }

+ 16 - 14
SE/se-lib/Route/Storage.php

@@ -1408,23 +1408,25 @@ jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(
 			$idStorage = V::get('storageId', '', $_GET);
 			$tblName = V::get('tblName', '', $_GET, 'word');
 			if (empty($tblName)) throw new HttpException("Wrong table name");
-			// $response->zasobTblId = $zasobItemFound->TABLE_ID;
-			// $response->zasobId = $zasobItemFound->ID;
 
 			$storage = DB::getStorage($idStorage);
 
+			$sqlTableName = DB::getPDO($idStorage)->quote($tblName, PDO::PARAM_STR);
+			$dbName = DB::getPDO($idStorage)->getDatabaseName();
+			$rootTableName = DB::getPDO($idStorage)->fetchValue("
+				select t.TABLE_NAME
+				from `information_schema`.`TABLES` t
+				where t.TABLE_SCHEMA = '{$dbName}'
+					and t.TABLE_NAME LIKE {$sqlTableName}
+			");
+			if (!$rootTableName) throw new Exception("Table '{$tblName}' not exists!");
+
 			$tableStruct = $storage->getTableStruct($tblName);
 
 			$zasobStorageId = $storage->getZasobId();
 			if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
 
-			$zasobItem = array();
-			$zasobItem['PARENT_ID'] = $zasobStorageId;
-			$zasobItem['TYPE'] = 'TABELA';
-			$zasobItem['DESC'] = $tblName;
-			$zasobItem['DESC_PL'] = $tblName;
-
-			$zasobItemFound = null;
+			$foundZasobItem = null;
 			{
 				$rows = DB::getPDO()->fetchAll("
 					select z.`ID`, z.`DESC`
@@ -1434,13 +1436,13 @@ jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(
 						and z.`A_STATUS` in('NORMAL','WAITING')
 				");
 				if (!empty($rows)) {
-					$zasobItemFound = $rows[0]['ID'];
+					$foundZasobItem = $rows[0]['ID'];
 				}
 			}
 
-			if ($zasobItemFound > 0) {
-				$response->_replaceButtonNode = "[{$zasobItemFound}]";
-				throw new AlertInfoException("Zasob tabela '{$tblName}' już istnieje - nr '{$zasobItemFound}'");
+			if ($foundZasobItem > 0) {
+				$response->_replaceButtonNode = "[{$foundZasobItem}]";
+				throw new AlertInfoException("Zasob tabela '{$tblName}' już istnieje - nr '{$foundZasobItem}'");
 			}
 
 			try {
@@ -1452,7 +1454,7 @@ jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(
 			$item = array();
 			$item['PARENT_ID'] = $zasobStorageId;
 			$item['TYPE'] = 'TABELA';
-			$item['DESC'] = $tblName;
+			$item['DESC'] = $rootTableName;
 			$item['DESC_PL'] = $tblName;
 			if (DBG::isActive()) $response->_itemToCreate = $item;
 

+ 49 - 14
SE/se-lib/TableAcl.php

@@ -30,23 +30,50 @@ Lib::loadClass('DataSourceFactory');
  */
 class TableAcl extends Core_AclBase {
 
-	private $_zasobID = '';
-	private $_db = '';
-	private $_name = '';
-	private $_label = '';
-	private $_opis = '';
-	private $_fields = array();
-	private $_types = array();
-	private $_virtualFieldsIdList = array();
-	private $_schemaLoaded = false;
+	public $_rootTableName = null;
+	public $_zasobID = '';
+	public $_db = '';
+	public $_name = '';
+	public $_label = '';
+	public $_opis = '';
+	public $_fields = array();
+	public $_types = array();
+	public $_virtualFieldsIdList = array();
+	public $_schemaLoaded = false;
 
 	public function __construct($zasobID) { $this->_zasobID = $zasobID; }
 	public function getNamespace() { return 'default_db/' . $this->getName(); }
 	public function getSourceName() { return 'default_db'; }
 	public function getName() { return $this->_name; }
-	public function getRootTableName() { return $this->_name; }
+	public function getRootTableName() {
+		if (empty($this->_name)) throw new Exception("Table name not defined");
+		if ($this->_rootTableName) return $this->_rootTableName;
+		$dbName = DB::getPDO()->getDatabaseName();
+		$this->_rootTableName = DB::getPDO()->fetchValue("
+			select t.TABLE_NAME
+			from `information_schema`.`TABLES` t
+			where t.TABLE_SCHEMA = '{$dbName}'
+				and t.TABLE_NAME LIKE '{$this->_name}'
+		");
+		if (!$this->_rootTableName) throw new Exception("Table '{$this->_name}' not exists!");
+		DBG::log("TableAcl({$this->_zasobID})->getRootTableName (\$this->_name='{$this->_name}', \$this->_rootTableName='{$this->_rootTableName}')");
+		if ($this->_rootTableName != $this->_name) {
+			if ($this->_zasobID) {
+				$affected = DB::getPDO()->update('CRM_LISTA_ZASOBOW', 'ID', $this->_zasobID, [
+					'DESC' => $this->_rootTableName
+				]);
+				DBG::log("TableAcl({$this->_zasobID})->getRootTableName fixed zasob table name (\$affected={$affected})");
+			}
+			$this->_name = $this->_rootTableName;
+			$this->save();
+		}
+		return $this->_rootTableName;
+	}
 	public function getID() { return $this->_zasobID; }
-	public function setName($name) { $this->_name = $name; }
+	public function setName($name) {// TODO: used only by setNameByTableId
+		DBG::log("setName('{$name}')");
+		$this->_name = $name;
+	}
 	public function setOpis($opis) { $this->_opis = $opis; }
 	public function getOpis() { return $this->_opis; }
 	public function setLabel($label) { $this->_label = $label; }
@@ -939,6 +966,7 @@ class TableAcl extends Core_AclBase {
 		}
 		$obj = new TableAcl($idTable);
 		$obj->fromArray($tableConfig);
+		$obj->getRootTableName();
 		$obj->save();
 		$_cache[$idTable] = $obj;
 		return $_cache[$idTable];
@@ -957,7 +985,7 @@ class TableAcl extends Core_AclBase {
 			$this->_types = array();// clear _types @see $this->isInitialized
 			$userAcl = User::getAcl();
 			$fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
-			DBG::_('DBG_SCH', '>1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__ );
+			DBG::log(['msg'=>"INIT({$this->_zasobID})::initFieldsFromConfig \$fieldsConfig", '$fieldsConfig'=>$fieldsConfig]);
 			$this->initFieldsFromConfig($fieldsConfig);
 			//DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $this, __CLASS__, __FUNCTION__, __LINE__ );
 		}
@@ -966,13 +994,16 @@ class TableAcl extends Core_AclBase {
 			return;
 		}
 
+		DBG::log(['msg'=>"INIT({$this->_zasobID}):: \$this", '$this'=>$this, 'name'=>$this->getName()]);
 		$ds = $this->getDataSource();
+		DBG::log(['msg'=>"INIT({$this->_zasobID})::getDataSource \$ds", '$ds'=>$ds]);
 		$this->_types = $ds->getFieldTypes();
+		DBG::log(['msg'=>"INIT({$this->_zasobID})::getFieldTypes \$this->_types", '$this->_types'=>$this->_types]);
 
 		uasort($this->_fields, array($this, 'sortFieldsCallback'));
 
 		$this->_fixTypes();
-		DBG::_('DBG_SCH', '>1', "INIT::after fixTypes({$this->_zasobID})", $this->_types, __CLASS__, __FUNCTION__, __LINE__ );
+		DBG::log(['msg'=>"INIT({$this->_zasobID})::after fixTypes \$this->_types", '$this->_types'=>$this->_types]);
 
 		$this->save();
 	}
@@ -1164,6 +1195,7 @@ class TableAcl extends Core_AclBase {
 	 */
 	function save() {
 		$_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
+		DBG::log("save(name='{$_SESSION['TableAcl_cache'][$this->_zasobID]['name']}')");
 	}
 
 	public function getFieldTypeById($fieldID) {
@@ -1338,8 +1370,10 @@ class TableAcl extends Core_AclBase {
 	}
 
 	public function fromArray($arr) {
+		DBG::log("fromArray(name='{$arr['name']}')");
 		$this->_db = $arr['db'];
 		$this->_name = $arr['name'];
+		$this->_rootTableName = V::get('_rootTableName', null, $arr);
 		$this->_label = $arr['label'];
 		$this->_opis = $arr['opis'];
 		$this->_fields = V::get('fields', array(), $arr);
@@ -1351,6 +1385,7 @@ class TableAcl extends Core_AclBase {
 		$arr = array();
 		$arr['db'] = $this->_db;
 		$arr['name'] = $this->_name;
+		$arr['_rootTableName'] = $this->_rootTableName;
 		$arr['label'] = $this->_label;
 		$arr['opis'] = $this->_opis;
 		$arr['fields'] = $this->_fields;
@@ -1587,7 +1622,7 @@ class TableAcl extends Core_AclBase {
 	private function _getDataSource($cols) {
 		$dsConfig = array();
 		$dsConfig['source_id'] = $this->getDB();
-		$dsConfig['object_name'] = $this->getName();
+		$dsConfig['object_name'] = $this->getRootTableName();
 		$dsConfig['fields'] = $cols;
 		$dsConfig['field_types'] = $this->getTypes();
 		$dsConfig['fields_virtual'] = $this->getVirtualFieldListByIdZasob();