From 783cccaa1bd4bd877793ef519d2c44988511d8f2 Mon Sep 17 00:00:00 2001 From: yuri Date: Thu, 9 Jun 2016 12:47:17 +0300 Subject: [PATCH] Number field type --- .../Utils/FieldManager/Hooks/NumberType.php | 47 +++++++- application/Espo/Core/Utils/File/Manager.php | 2 +- application/Espo/Core/Utils/Util.php | 13 ++- application/Espo/Entities/NextNumber.php | 35 ++++++ application/Espo/Hooks/Common/NextNumber.php | 104 ++++++++++++++++++ .../metadata/entityDefs/NextNumber.json | 15 +++ client/src/views/fields/number.js | 46 ++++++++ 7 files changed, 257 insertions(+), 5 deletions(-) create mode 100644 application/Espo/Entities/NextNumber.php create mode 100644 application/Espo/Hooks/Common/NextNumber.php create mode 100644 application/Espo/Resources/metadata/entityDefs/NextNumber.json create mode 100644 client/src/views/fields/number.js diff --git a/application/Espo/Core/Utils/FieldManager/Hooks/NumberType.php b/application/Espo/Core/Utils/FieldManager/Hooks/NumberType.php index 65846a8b45..8c3d39f1f6 100644 --- a/application/Espo/Core/Utils/FieldManager/Hooks/NumberType.php +++ b/application/Espo/Core/Utils/FieldManager/Hooks/NumberType.php @@ -6,11 +6,56 @@ class NumberType extends Base { public function onRead($scope, $name, &$defs) { - $defs['nextNumber'] = 2; + $number = $this->getEntityManager()->getRepository('NextNumber')->where(array( + 'entityType' => $scope, + 'fieldName' => $name + ))->findOne(); + + $value = null; + if (!$number) { + $value = 1; + } else { + if (!$number->get('value')) { + $value = 1; + } + } + + if (!$value && $number) { + $value = $number->get('value'); + } + + $defs['nextNumber'] = $value; } public function afterSave($scope, $name, $defs) { + if (!isset($defs['nextNumber'])) return; + $number = $this->getEntityManager()->getRepository('NextNumber')->where(array( + 'entityType' => $scope, + 'fieldName' => $name + ))->findOne(); + + if (!$number) { + $number = $this->getEntityManager()->getEntity('NextNumber'); + + $number->set('entityType', $scope); + $number->set('fieldName', $name); + } + + $number->set('value', $defs['nextNumber']); + $this->getEntityManager()->saveEntity($number); + } + + public function afterRemove($scope, $name) + { + $number = $this->getEntityManager()->getRepository('NextNumber')->where(array( + 'entityType' => $scope, + 'fieldName' => $name + ))->findOne(); + + if (!$number) return; + + $this->getEntityManager()->removeEntity($number); } } \ No newline at end of file diff --git a/application/Espo/Core/Utils/File/Manager.php b/application/Espo/Core/Utils/File/Manager.php index 84ea6c5af9..cf29f944ab 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -336,7 +336,7 @@ class Manager public function unsetContents($path, $unsets, $isJSON = true) { $currentData = $this->getContents($path); - if ($currentData == false) { + if (!isset($currentData) || !$currentData) { $GLOBALS['log']->notice('FileManager::unsetContents: File ['.$this->concatPaths($path).'] does not exist.'); return false; } diff --git a/application/Espo/Core/Utils/Util.php b/application/Espo/Core/Utils/Util.php index a3be4b0468..7346b3bfa2 100644 --- a/application/Espo/Core/Utils/Util.php +++ b/application/Espo/Core/Utils/Util.php @@ -394,11 +394,18 @@ class Util $unsets = (array) $unsets; } - foreach($unsets as $rootKey => $unsetItem){ + if (!isset($content)) { + $e = new \Exception; + var_dump($e->getTraceAsString()); + die; + } + + + foreach ($unsets as $rootKey => $unsetItem) { $unsetItem = is_array($unsetItem) ? $unsetItem : (array) $unsetItem; - foreach($unsetItem as $unsetSett){ - if (!empty($unsetSett)){ + foreach ($unsetItem as $unsetSett) { + if (!empty($unsetSett)) { $keyItems = explode('.', $unsetSett); $currVal = isset($content[$rootKey]) ? "\$content['{$rootKey}']" : "\$content"; diff --git a/application/Espo/Entities/NextNumber.php b/application/Espo/Entities/NextNumber.php new file mode 100644 index 0000000000..019f6e1235 --- /dev/null +++ b/application/Espo/Entities/NextNumber.php @@ -0,0 +1,35 @@ +addDependency('metadata'); + } + + + protected function getMetadata() + { + return $this->getInjection('metadata'); + } + + protected function composeNumberAttribute(Entity $nextNumber) + { + $entityType = $nextNumber->get('entityType'); + $fieldName = $nextNumber->get('fieldName'); + $value = $nextNumber->get('value'); + + $prefix = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $fieldName, 'prefix'], ''); + $padLength = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $fieldName, 'padLength'], 0); + + return $prefix . str_pad(strval($value), $padLength, '0', \STR_PAD_LEFT); + } + + public function beforeSave(Entity $entity, array $options = array()) + { + $fieldDefs = $this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields'], array()); + + foreach ($fieldDefs as $fieldName => $defs) { + if (isset($defs['type']) && $defs['type'] === 'number') { + $nextNumber = $this->getEntityManager()->getRepository('NextNumber')->where(array( + 'fieldName' => $fieldName, + 'entityType' => $entity->getEntityType() + ))->findOne(); + if (!$nextNumber) continue; + $entity->set($fieldName, $this->composeNumberAttribute($nextNumber)); + } + } + } + + public function afterSave(Entity $entity, array $options = array()) + { + $fieldDefs = $this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields'], array()); + + foreach ($fieldDefs as $fieldName => $defs) { + if (isset($defs['type']) && $defs['type'] === 'number') { + $nextNumber = $this->getEntityManager()->getRepository('NextNumber')->where(array( + 'fieldName' => $fieldName, + 'entityType' => $entity->getEntityType() + ))->findOne(); + if (!$nextNumber) continue; + + $value = $nextNumber->get('value'); + if (!$value) { + $value = 1; + } + $value++; + + $nextNumber->set('value', $value); + $this->getEntityManager()->saveEntity($nextNumber); + } + } + } + + +} + diff --git a/application/Espo/Resources/metadata/entityDefs/NextNumber.json b/application/Espo/Resources/metadata/entityDefs/NextNumber.json new file mode 100644 index 0000000000..ac5982e382 --- /dev/null +++ b/application/Espo/Resources/metadata/entityDefs/NextNumber.json @@ -0,0 +1,15 @@ +{ + "fields": { + "entityType": { + "type": "varchar", + "index": true + }, + "fieldName": { + "type": "varchar" + }, + "value": { + "type": "int", + "default": 1 + } + } +} diff --git a/client/src/views/fields/number.js b/client/src/views/fields/number.js new file mode 100644 index 0000000000..fdb4d885e5 --- /dev/null +++ b/client/src/views/fields/number.js @@ -0,0 +1,46 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +Espo.define('views/fields/number', 'views/fields/varchar', function (Dep) { + + return Dep.extend({ + + type: 'number', + + validations: [], + + inlineEditDisabled: true, + + readOnly: true, + + fetch: function () { + return {}; + }, + }); +}); +