Преглед изворни кода

Ahmes Reklamacje: save create form

Piotr Labudda пре 11 година
родитељ
комит
e6db1b69c0
1 измењених фајлова са 93 додато и 11 уклоњено
  1. 93 11
      SE/superedit-AHMES_REKLAMACJE.php

+ 93 - 11
SE/superedit-AHMES_REKLAMACJE.php

@@ -57,8 +57,8 @@ function AHMES_REKLAMACJE() {
 	$tblAcl->init($forceTblAclInit);
 	$korespAcl->init($forceTblAclInit);
 
-	$tbl = new Ahmes_Reklamacje($tblAcl, $korespAcl, $_GET);
-	echo $tbl->render();
+	$module = new Ahmes_Reklamacje($tblAcl, $korespAcl, $_GET);
+	$module->run();
 }
 
 
@@ -75,6 +75,7 @@ class Ahmes_Reklamacje {
 		$this->_args = $args;
 		$this->_tbl = $tblAcl->getName();
 		$this->_acl = $tblAcl;
+		$this->_korespAcl = $korespAcl;
 		$this->_zasobID = $tblAcl->getID();
 		Lib::loadClass('Data_Source');
 		$this->_dataSource = new Data_Source($tblAcl->getDB());
@@ -96,7 +97,7 @@ class Ahmes_Reklamacje {
 		}
 	}
 
-	public function render() {
+	public function run() {
 		Lib::loadClass('SE_Layout');
 		SE_Layout::menu();
 		$task = V::get('task', '', $this->_args);
@@ -164,19 +165,80 @@ class Ahmes_Reklamacje {
 		}
 
 		if ('1' == V::get('frm_sent', '', $_POST)) {
-			$this->saveCreateProblemForm($id_koresp, $_POST);
+			$frmSaved = $this->saveCreateProblemForm($koresp, $_POST);
+			if ($frmSaved) return;
 		}
-		$this->renderCreateProblemForm($id_koresp);
+		$this->renderCreateProblemForm($koresp);
 	}
 
-	public function saveCreateProblemForm($id_koresp, $args) {
-		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save('.$id_koresp.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';
+	public function saveCreateProblemForm($koresp, $args) {
+		$DBG = ('1' == V::get('DBG', '', $_REQUEST));
+
+		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save('.$koresp->ID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';}
+		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">acl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_acl);echo'</pre>';}
+		$dbID = $this->_acl->getDB();
+		$db = DB::getDB($dbID);
+
+		if (!$db) return false;
+
+		$tblName = $this->_acl->getName();
+
+		$sqlObj = new stdClass();
+		$fields = $this->_acl->getFields();
+		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">fields (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($fields);echo'</pre>';}
+		foreach ($fields as $kID => $vField) {
+			if (!$this->_acl->isAllowed($kID, 'C')) {
+				continue;
+			}
+			if (array_key_exists("f{$kID}", $args)) {
+				$sqlObj->{$vField['name']} = $args["f{$kID}"];
+
+				if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
+					$sqlObj->{$vField['name']} = $this->_acl->fixEmptyValueFromUser($kID);
+				}
+			}
+		}
+		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sqlObj);echo'</pre>';}
+
+		$retID = $db->ADD_NEW_OBJ($tblName, $sqlObj);
+
+		if ($retID > 0) {
+			?>
+			<div class="alert alert-success">
+				Utworzono pomyślnie rekord Nr <?php echo $retID; ?>
+			</div>
+			<?php
+			return true;
+		}
+
+		?>
+		<div class="alert alert-error">
+			Wystąpił błąd podczas tworzenia rekordu
+			<?php
+			if ($db->has_errors()) {
+				$outArr = array();
+				$errorsSql = $db->get_errors();
+				foreach ($errorsSql as $vErr) {
+					if (substr($vErr, 0, 18) == 'SQL QUERY FAILED: ') {
+						$vErr = substr($vErr, 18);
+						// Duplicate entry '123456-1' for key 'P_NIP'
+						if (substr($vErr, 0, 16) == 'Duplicate entry ') {
+						}
+					}
+					$outArr[] = $vErr;
+				}
+				echo implode('<br>', $outArr);
+			}
+			?>
+		</div>
+		<?php
+		return false;
 	}
 
 	/*
 	 * @from TableAjax::sendAjaxCreate($args)
 	 */
-	public function renderCreateProblemForm($id_koresp) {
+	public function renderCreateProblemForm($koresp) {
 		$DBG = ('1' == V::get('DBG', '', $_REQUEST));
 
 		$cols = array();
@@ -206,14 +268,34 @@ class Ahmes_Reklamacje {
 			$fieldsList[$kID]['label'] = (!empty($vCol['label']))? $vCol['label'] : $vCol['name'];
 		}
 
+		$korespFldsList = $this->_korespAcl->getFields();
+		$korespFldsList = array_filter($korespFldsList, function($fld) {
+			$korespFldsName = array();
+			$korespFldsName[] = 'ID';
+			$korespFldsName[] = 'K_OD_KOGO';
+			$korespFldsName[] = 'K_TYP_KORESP';
+			$korespFldsName[] = 'K_TYP_RODZAJ';
+			$korespFldsName[] = 'K_DATA_OTRZYMANEJ_KORESP';
+			$korespFldsName[] = 'K_DATA_OTRZYM_KORESP';
+			if (in_array($fld['name'], $korespFldsName)) {
+				return true;
+			}
+			return false;
+		});
+
 		Lib::loadClass('SE_Layout');
 		?>
 		<div class="container AjaxFrmHorizontalEdit">
 		<form class="form-horizontal" action="" method="post">
 			<input type="hidden" name="frm_sent" value="1">
 			<fieldset>
-				<legend>Dodaj nowy Problem/Reklamację na podstawie pisma nr <?php echo $id_koresp; ?></legend>
+				<legend>Dodaj nowy Problem/Reklamację na podstawie pisma nr <?php echo $koresp->ID; ?></legend>
 
+				<blockquote>
+					<?php foreach ($korespFldsList as $fldId => $vFld) : ?>
+						<p><small><b><?php echo $vFld['label']; ?>: </b> <?php echo $koresp->{$vFld['name']}; ?></small></p>
+					<?php endforeach; ?>
+				</blockquote>
 				<?php $tabindex = 0; foreach ($fieldsList as $kID => $vCol) : ?>
 					<?php if ($this->_acl->isAllowed($kID, 'C')) : ?>
 						<div class="control-group">
@@ -259,11 +341,11 @@ jQuery(document).ready(function(){
 	}
 
 	public function renderCreateKorespOutForm($id_problem) {
-		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: render frm('.$id_koresp.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_POST);echo'</pre>';
+		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: render frm('.$id_problem.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_POST);echo'</pre>';
 	}
 
 	public function saveCreateKorespOutForm($id_problem, $args) {
-		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save('.$id_koresp.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';
+		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save('.$id_problem.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';
 	}
 
 	public function ___sendAjaxCreateSave($args) {