|
|
@@ -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) {
|