Browse Source

added Storage add zasob table using p5UI btn

Piotr Labudda 10 years ago
parent
commit
4c5f1c7ac6
1 changed files with 93 additions and 1 deletions
  1. 93 1
      SE/se-lib/Route/Storage.php

+ 93 - 1
SE/se-lib/Route/Storage.php

@@ -3,6 +3,7 @@
 
 Lib::loadClass('RouteBase');
 Lib::loadClass('Schema_TableFactory');
+Lib::loadClass('Response');
 
 /*
 # Storage:
@@ -98,7 +99,9 @@ class Route_Storage extends RouteBase {
 					$tblItem['id_zasob'] = $tblZasob['ID'];
 				} else {
 					$tblItem['uwagi'] .= 'TODO: ADD ZASOB';
-					$tblItem['dodaj zasób'] = '<a href="#">TODO: ADD ZASOB</a>';
+					$ajaxAddZasobLink = Request::getUriDirName() . "/index.php?_route=Storage&_task=addTableToZasoby&idStorage={$idStorage}&tblName={$tblName}";
+					$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;
 			}
@@ -130,6 +133,22 @@ class Route_Storage extends RouteBase {
 				}
 			}
 			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').next('td').next('td').next('td').text(payload.body.id);
+		jQuery(n).remove();
+	}
+	jQuery.notify(payload.msg, payload.type);
+});
+
+</script>
+			<?php
 		} catch (Exception $e) {
 			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 		}
@@ -485,4 +504,77 @@ class Route_Storage extends RouteBase {
 		return $storageList;
 	}
 
+	public function addTableToZasobyAction() {// sends JSON
+		$response = new stdClass();
+		try {
+			$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);
+
+			//$storage = Core_StorageFactory::getStorage($idStorage);
+			$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;
+			{
+				$rows = DB::getPDO()->fetchAll("
+					select z.`ID`, z.`DESC`
+					from `CRM_LISTA_ZASOBOW` z
+					where z.`PARENT_ID`='{$zasobStorageId}'
+						and z.`DESC`='{$tblName}'
+						and z.`A_STATUS` in('NORMAL','WAITING')
+				");
+				if (!empty($rows)) {
+					$zasobItemFound = $$rows[0]['ID'];
+				}
+			}
+
+			if ($zasobItemFound > 0) {
+				$response->_replaceButtonNode = "[{$zasobItemFound}]";
+				throw new AlertInfoException("Zasob tabela '{$tblName}' już istnieje - nr '{$zasobItemFound}'");
+			}
+
+			$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'] = $zasobStorageId;
+			$item['TYPE'] = 'TABELA';
+			$item['DESC'] = $tblName;
+			$item['DESC_PL'] = $tblName;
+			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);
+	}
+
 }