Преглед на файлове

added type p5:price; fixed parse user input in TableAjax

Piotr Labudda преди 9 години
родител
ревизия
e2f9937e01

+ 1 - 0
SE/schema/gui/core/default_db.test_perms.php

@@ -6,6 +6,7 @@ class Schema__Core__default_db__test_perms extends Schema_TableBase {
 		parent::initTypes();
 		$this->_types['WWW'] = 'p5:www_link';
 		$this->_restrictions['WWW'] = array('maxLength' => 255);
+		$this->_types['test_price'] = 'p5:price';
 	}
 
 }

+ 3 - 0
SE/schema/gui/core/default_db.zaliczka_info_view.php

@@ -7,6 +7,9 @@ class Schema__Core__default_db__zaliczka_info_view extends Schema_TableBase {
 
 	public function initTypes() {
 		parent::initTypes();
+		$this->_types['KWOTA'] = 'p5:price';
+		$this->_types['NIEROZLICZONA_KWOTA'] = 'p5:price';
+		$this->_types['rozliczona_kwota'] = 'p5:price';
 	}
 
   public function getPrimaryKeyField() {

+ 2 - 2
SE/se-lib/Route/DealsSales.php

@@ -175,7 +175,7 @@ class Route_DealsSales extends RouteBase {
 		$primaryKeyFieldName = 'ID';
 		$primaryKey = V::get($primaryKeyFieldName, 0, $record);
 
-		$item = new stdClass();//$tblAcl->convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f');
+		$item = new stdClass();
 		$item->T_TELBOX_NEIGHBOUR_IN = $telbox;
 		$item->SALES_YEAR = $year;
 		$item->SALES_MONTH = $month;
@@ -569,7 +569,7 @@ class Route_DealsSales extends RouteBase {
 	</form>
 
 	<?php if (empty($obroty)) : ?>
-		
+
 	<?php else : ?>
 
 		<hr>

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

@@ -1395,22 +1395,72 @@ class TableAcl extends Core_AclBase {
 		return $arr;
 	}
 
-	public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
-		$item = array();
-		$fields = $this->getFields();
-		foreach ($fields as $kID => $vField) {
-			$vFieldName = $vField['name'];
-			if (array_key_exists("f{$kID}", $args)) {
-				$value = $args["f{$kID}"];
-
-				if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
-					$value = $this->fixEmptyValueFromUser($kID);
-				}
-				$item[$vFieldName] = $value;
-			}
+	public function convertObjectFromUserInput($userItem, $type = 'array_by_id', $prefix = 'f') {// TODO: rename / Legacy
+		$item = $this->parseUserItem($userItem, $type = 'array_by_id', $prefix = 'f');
+		foreach ($item as $fieldName => $value) {
+			$item[$fieldName] = $this->validateAndFixField($fieldName, $value);
+		}
+		DBG::log(['userItem' => $userItem, 'item' => $item], '', "after parseUserItem, validateAndFixField");
+		return $item;
+	}
+	public function parseUserItem($userItem, $type = 'array_by_id', $prefix = 'f') {
+		$item = [];
+		foreach ($this->getFieldsMap() as $userKey => $fieldName) {
+			if (!array_key_exists("f{$userKey}", $userItem)) continue;
+			$item[$fieldName] = $userItem["f{$userKey}"];
 		}
 		return $item;
 	}
+	public function getFieldsMap() {// [ $userKey => $fieldName ] // TODO: $this->_fieldsMap
+		$fieldsMap = [];
+		foreach ($this->getFields() as $id => $field) $fieldsMap[ $id ] = $field['name'];
+		return $fieldsMap;
+	}
+	public function validateAndFixField($fieldName, $value) {
+		if (empty($value) && 0 === strlen($value)) {// TODO: fixEmptyValueFromUser
+			return $this->fixFieldEmptyValue($fieldName);
+		}
+		$xsdType = $this->getXsdFieldType($fieldName);
+		switch ($xsdType) {
+			case 'xsd:decimal': {
+				return str_replace([',', ' '], ['.', ''], $value);
+			} break;
+			case 'p5:price': {
+				return V::convert($value, 'price');
+			} break;
+		}
+	}
+	public function fixFieldEmptyValue($fieldName) {// TODO: legacy
+		$type = $this->getFieldType($fieldName);
+		if (!$type) return '';
+		if ('date' == $type['type']) {
+			$value = $type['default'];
+		}
+		if (substr($type['type'], 0, 3) == 'int'
+				|| substr($type['type'], 0, 7) == 'tinyint'
+				|| substr($type['type'], 0, 8) == 'smallint'
+				|| substr($type['type'], 0, 6) == 'bigint'
+		) {
+			$value = intval($type['default']);
+		}
+		// fix bug when field is unique and is null allowed: change empty string to null
+		if ($type['null']) {
+			$value = 'NULL';
+		}
+		// fix bug when field is enum and is set to '0': for php '0' is empty
+		if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
+			// if (false !== strpos($type['type'], "''")) {
+			// 	// enum('', '1','2')
+			// 	$value = '';
+			// } else if (false !== strpos($type['type'], "'0'")) {
+			// 	// enum('0', '1','2')
+			// 	$value = '0';
+			// } else {
+				$value = $type['default'];
+			// }
+		}
+		return $value;
+	}
 
 	public function getItem($primaryKey, $params = []) {
 		$ds = $this->getDataSource();
@@ -1477,7 +1527,7 @@ class TableAcl extends Core_AclBase {
 		}
 		$ds = $this->getDataSource();
 
-		// from convertObjectFromUserInput
+		// from convertObjectFromUserInput - fixEmptyValueFromUser
 		$item = array();
 		$fields = $this->getFields();
 		foreach ($fields as $kID => $vField) {

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

@@ -951,6 +951,18 @@ var p5UI_TableAjax_generateFunctionNode = function(funObj, rowPK, props) {
 						}
 					}(fldName, fieldProps));
 					break;
+				case "p5:price":// @return String
+					fieldWidget = (function(fldName, fieldProps) {
+						var _fieldName = fldName,
+								_fieldProps = fieldProps,
+								_format = _.get(_fieldProps, 'format', '<span style="text-align:right">{0}</span>');
+						// console.log('FieldWidget: generate function to render field('+_fieldName+') ', fieldProps);
+						return function(val, fieldPK, row) {
+							// console.log('FieldWidget: pk('+fieldPK+') run function to render field('+_fieldName+') with value('+val+') ', fieldProps);
+							return _format.f(val);
+						}
+					}(fldName, fieldProps));
+					break;
 				case "p5:www_link":// @return Element
 					fieldWidget = (function(fldName, fieldProps) {
 						var _fieldName = fldName,
@@ -1659,6 +1671,7 @@ var p5UI_TableAjax_generateFunctionNode = function(funObj, rowPK, props) {
 								elem = $('<input class="filter indeterminate" checked type="checkbox" />');
 								elem.on('click', {column: column}, priv.filterChanged);
 								break;
+						case "p5:price":
 						case "p5:www_link":
 						case "string":
 								if (placeHolder == undefined) placeHolder = priv.options.types.string.placeHolder;