Sfoglia il codice sorgente

added Storage add zasob cell using p5UI btn

Piotr Labudda 10 anni fa
parent
commit
bdae5186fb
1 ha cambiato i file con 144 aggiunte e 3 eliminazioni
  1. 144 3
      SE/se-lib/Route/Storage.php

+ 144 - 3
SE/se-lib/Route/Storage.php

@@ -146,7 +146,6 @@ jQuery(document).on('p5UI__ButtonAjax:ajaxLoaded', function(e, n, payload) {
 	}
 	}
 	jQuery.notify(payload.msg, payload.type);
 	jQuery.notify(payload.msg, payload.type);
 });
 });
-
 </script>
 </script>
 			<?php
 			<?php
 		} catch (Exception $e) {
 		} catch (Exception $e) {
@@ -190,7 +189,90 @@ jQuery(document).on('p5UI__ButtonAjax:ajaxLoaded', function(e, n, payload) {
 			$this->navView();
 			$this->navView();
 			$storagePdo = DB::getStorage($idStorage);
 			$storagePdo = DB::getStorage($idStorage);
 			$tblStruct = $storagePdo->getTableStruct($tblName);
 			$tblStruct = $storagePdo->getTableStruct($tblName);
-			DBG::table("tblStruct", $tblStruct, __CLASS__, __FUNCTION__, __LINE__);
+
+			$idTable = $this->fetchTableId($idStorage, $tblName);
+			if ($idTable <= 0) {
+				SE_Layout::alert('warning', "Zasob tabela '{$tblName}' nie istnieje");// TODO: add p5UI btn
+				DBG::table("tblStruct", $tblStruct, __CLASS__, __FUNCTION__, __LINE__);
+				throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
+			}
+
+			$cellZasobList = array();
+			foreach (DB::getPDO()->fetchAllByKey("
+					select z.ID, z.`DESC`, z.A_STATUS
+					from CRM_LISTA_ZASOBOW z
+					where z.PARENT_ID = '{$idTable}'
+				", $key = 'DESC') as $ind => $row) {
+				$cellZasobList[strtolower($ind)] = $row;
+			}
+
+			$emptyItem = array();
+			$emptyItem['name'] = '';
+			$emptyItem['dodaj zasób'] = '';
+			$emptyItem['id_zasob'] = '';
+			$emptyItem['uwagi'] = '';
+
+			$emptyItem['type'] = '';
+			$emptyItem['is_nullable'] = '';
+			$emptyItem['default_value'] = '';
+			$emptyItem['default_is_null'] = '';
+			$emptyItem['max_length'] = '';
+			$emptyItem['num_precision'] = '';
+			$emptyItem['num_scale'] = '';
+			$emptyItem['char_encoding'] = '';
+			$emptyItem['char_collation'] = '';
+			$emptyItem['extra'] = '';
+			$emptyItem['raw_storage_type'] = '';
+
+			$tableList = array();
+			foreach ($tblStruct as $row) {
+				$cellName = $row['name'];
+				$tblItem = V::cloneArray($emptyItem);
+				$tblItem['name'] = $cellName;
+				foreach ($row as $fldName => $fldVal) {
+					if (array_key_exists($fldName, $tblItem)) $tblItem[$fldName] = $fldVal;
+				}
+				$tblItem['uwagi'] = '';
+				$lowerCellName = strtolower($cellName);
+				$tblZasob = V::get($lowerCellName, '', $cellZasobList);
+				if ($tblZasob) {
+					$cellZasobList[$lowerCellName]['_checked'] = true;
+					$tblItem['id_zasob'] = $tblZasob['ID'];
+				} else {
+					$tblItem['uwagi'] .= '!Zasob';//'TODO: ADD ZASOB';
+					$ajaxAddZasobLink = Request::getUriDirName() . "/index.php?_route=Storage&_task=addCellToZasoby&idStorage={$idStorage}&tblName={$tblName}&cellName={$cellName}";
+					$onClick = "return p5UI__ButtonAjax(this, { href: '{$ajaxAddZasobLink}' })";
+					$tblItem['dodaj zasób'] = '<a onclick="'.$onClick.'" class="btn btn-xs btn-primary" href="#">TODO: ADD ZASOB</a>';
+				}
+				$tableList[] = $tblItem;
+			}
+			foreach ($cellZasobList as $cellName => $row) {
+				if (!$row['_checked']) {
+					$tblItem = V::cloneArray($emptyItem);
+					$tblItem['name'] = $cellName;
+					$tblItem['id_zasob'] = $row['ID'];
+					$tblItem['uwagi'] = '!DB';//'TODO: nie istnieje w bazie danych';
+					$tableList[] = $tblItem;
+				}
+			}
+			DBG::table("tableList", $tableList, __CLASS__, __FUNCTION__, __LINE__);
+			?>
+<script>
+jQuery(document).on('p5UI__ButtonAjax:click', function(e, n, payload) {
+	console.log('event p5UI__ButtonAjax:click', n, payload);
+});
+jQuery(document).on('p5UI__ButtonAjax:ajaxLoaded', function(e, n, payload) {
+	console.log('event p5UI__ButtonAjax:ajaxLoaded', n, payload);
+	if ('success' == payload.type && payload.body && payload.body.id > 0) {
+		jQuery(n).parents('td').next('td').text(payload.body.id);
+		jQuery(n).remove();
+	}
+	jQuery.notify(payload.msg, payload.type);
+});
+
+</script>
+<?php
+
 		} catch (Exception $e) {
 		} catch (Exception $e) {
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 		}
 		}
@@ -537,7 +619,7 @@ jQuery(document).on('p5UI__ButtonAjax:ajaxLoaded', function(e, n, payload) {
 						and z.`A_STATUS` in('NORMAL','WAITING')
 						and z.`A_STATUS` in('NORMAL','WAITING')
 				");
 				");
 				if (!empty($rows)) {
 				if (!empty($rows)) {
-					$zasobItemFound = $$rows[0]['ID'];
+					$zasobItemFound = $rows[0]['ID'];
 				}
 				}
 			}
 			}
 
 
@@ -577,4 +659,63 @@ jQuery(document).on('p5UI__ButtonAjax:ajaxLoaded', function(e, n, payload) {
 		Response::sendJsonExit($response);
 		Response::sendJsonExit($response);
 	}
 	}
 
 
+	public function addCellToZasobyAction() {// sends JSON
+		$response = new stdClass();
+		try {
+			$idStorage = V::get('storageId', '', $_GET);
+			$tblName = V::get('tblName', '', $_GET, 'word');
+			$cellName = V::get('cellName', '', $_GET, 'word');
+			if (empty($tblName)) throw new HttpException("Wrong table name");
+			if (empty($tblName)) throw new HttpException("Wrong cell name");
+
+			$storage = DB::getStorage($idStorage);
+			$tableStruct = $storage->getTableStruct($tblName);
+			$zasobStorageId = $storage->getZasobId();
+			if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
+			$idTable = $this->fetchTableId($zasobStorageId, $tblName);
+			if ($idTable <= 0) throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
+
+			$acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
+			if (!$acl) throw new Exception("Brak dostępu do tabeli Zasoby");
+
+			$item = array();
+			$item['PARENT_ID'] = $idTable;
+			$item['TYPE'] = 'KOMORKA';
+			$item['DESC'] = $cellName;
+			$item['DESC_PL'] = $cellName;
+			if (DBG::isActive()) $response->_itemToCreate = $item;
+
+			$createdId = $acl->addItem($item);
+			if (!$createdId) throw new Exception("Nie udało się utworzyć nowego rekordu!");
+
+			$response->id = $createdId;
+			$response->record = $acl->getItem($createdId);
+			$response->_replaceButtonNode = "[{$createdId}]";
+			throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$createdId}");
+		} catch (AlertSuccessException $e) {
+			$response->type = 'success';
+			$response->msg = $e->getMessage();
+		} catch (AlertInfoException $e) {
+			$response->type = 'info';
+			$response->msg = $e->getMessage();
+		} catch (Exception $e) {
+			$response->type = 'error';
+			$response->msg = $e->getMessage();
+		}
+
+		Response::sendJsonExit($response);
+	}
+
+	public function fetchTableId($idZasobStorage, $tblName) {
+		$rows = DB::getPDO()->fetchAll("
+			select z.`ID`, z.`DESC`
+			from `CRM_LISTA_ZASOBOW` z
+			where z.`PARENT_ID`='{$idZasobStorage}'
+				and z.`DESC`='{$tblName}'
+				and z.`A_STATUS` in('NORMAL','WAITING')
+		");
+		if (!empty($rows)) return $rows[0]['ID'];
+		return null;
+	}
+
 }
 }