entity manager 2
This commit is contained in:
@@ -24,6 +24,7 @@ namespace Espo\Controllers;
|
||||
|
||||
use \Espo\Core\Exceptions\BadRequest;
|
||||
use \Espo\Core\Exceptions\Forbidden;
|
||||
use \Espo\Core\Exceptions\Error;
|
||||
|
||||
class EntityManager extends \Espo\Core\Controllers\Base
|
||||
{
|
||||
@@ -47,6 +48,9 @@ class EntityManager extends \Espo\Core\Controllers\Base
|
||||
$name = $data['name'];
|
||||
$type = $data['type'];
|
||||
|
||||
$name = filter_var($name, \FILTER_SANITIZE_STRING);
|
||||
$type = filter_var($type, \FILTER_SANITIZE_STRING);
|
||||
|
||||
$params = array();
|
||||
|
||||
if (!empty($data['labelSingular'])) {
|
||||
@@ -59,7 +63,41 @@ class EntityManager extends \Espo\Core\Controllers\Base
|
||||
$params['stream'] = $data['stream'];
|
||||
}
|
||||
|
||||
return $this->getContainer()->get('entityManagerUtil')->create($name, $type, $params);
|
||||
$result = $this->getContainer()->get('entityManagerUtil')->create($name, $type, $params);
|
||||
|
||||
if ($result) {
|
||||
$this->getContainer()->get('dataManager')->rebuild();
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function actionRemoveEntity($params, $data, $request)
|
||||
{
|
||||
if (!$request->isPost()) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if (empty($data['name'])) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
$name = $data['name'];
|
||||
|
||||
$name = filter_var($name, \FILTER_SANITIZE_STRING);
|
||||
|
||||
|
||||
$result = $this->getContainer()->get('entityManagerUtil')->delete($name);
|
||||
|
||||
if ($result) {
|
||||
$this->getContainer()->get('dataManager')->clearCache();
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +79,28 @@ class Container
|
||||
return $className;
|
||||
}
|
||||
|
||||
protected function loadLog()
|
||||
{
|
||||
$logConfig = $this->get('config')->get('logger');
|
||||
|
||||
$log = new \Espo\Core\Utils\Log('Espo');
|
||||
|
||||
$levelCode = $log->getLevelCode($logConfig['level']);
|
||||
|
||||
if ($logConfig['isRotate']) {
|
||||
$handler = new \Espo\Core\Utils\Log\Monolog\Handler\RotatingFileHandler($logConfig['path'], $logConfig['maxRotateFiles'], $levelCode);
|
||||
} else {
|
||||
$handler = new \Espo\Core\Utils\Log\Monolog\Handler\StreamHandler($logConfig['path'], $levelCode);
|
||||
}
|
||||
$log->pushHandler($handler);
|
||||
|
||||
$errorHandler = new \Monolog\ErrorHandler($log);
|
||||
$errorHandler->registerExceptionHandler(null, false);
|
||||
$errorHandler->registerErrorHandler(array(), false);
|
||||
|
||||
return $log;
|
||||
}
|
||||
|
||||
protected function loadContainer()
|
||||
{
|
||||
return $this;
|
||||
|
||||
+2
-24
@@ -20,33 +20,11 @@
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Loaders;
|
||||
namespace Espo\Core\Templates\Services;
|
||||
|
||||
use Espo\Core\Utils,
|
||||
Espo\Core\Utils\Log\Monolog\Handler;
|
||||
|
||||
class Log extends Base
|
||||
class Base extends \Espo\Services\Record
|
||||
{
|
||||
public function load()
|
||||
{
|
||||
$logConfig = $this->getContainer()->get('config')->get('logger');
|
||||
|
||||
$log = new Utils\Log('Espo');
|
||||
|
||||
$levelCode = $log->getLevelCode($logConfig['level']);
|
||||
|
||||
if ($logConfig['isRotate']) {
|
||||
$handler = new Handler\RotatingFileHandler($logConfig['path'], $logConfig['maxRotateFiles'], $levelCode);
|
||||
} else {
|
||||
$handler = new Handler\StreamHandler($logConfig['path'], $levelCode);
|
||||
}
|
||||
$log->pushHandler($handler);
|
||||
|
||||
$errorHandler = new \Monolog\ErrorHandler($log);
|
||||
$errorHandler->registerExceptionHandler(null, false);
|
||||
$errorHandler->registerErrorHandler(array(), false);
|
||||
|
||||
return $log;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* 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/.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Templates\Services;
|
||||
|
||||
|
||||
class Person extends \Espo\Services\Record
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
namespace Espo\Core\Utils;
|
||||
|
||||
use \Espo\Core\Exceptions\Error;
|
||||
use \Espo\Core\Exceptions\Forbidden;
|
||||
use \Espo\Core\Exceptions\Conflict;
|
||||
use \Espo\Core\Utils\Json;
|
||||
|
||||
@@ -78,9 +79,12 @@ class EntityManager
|
||||
|
||||
public function create($name, $type)
|
||||
{
|
||||
if ($this->getMetadata()->get('scope.' . $name)) {
|
||||
if ($this->getMetadata()->get('scopes.' . $name)) {
|
||||
throw new Conflict('Entity ['.$name.'] already exists.');
|
||||
}
|
||||
if (empty($name) || empty($type)) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
$contents = "<" . "?" . "php\n".
|
||||
"namespace Espo\Custom\Entities;\n".
|
||||
@@ -136,12 +140,14 @@ class EntityManager
|
||||
$filePath = "application/Espo/Core/Templates/Metadata/{$type}/clientDefs.json";
|
||||
$clientDefsData = Json::decode($this->getFileManager()->getContents($filePath), true);
|
||||
$this->getMetadata()->set($clientDefsData, 'clientDefs', $name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function update($name, $fieldDef, $scope)
|
||||
{
|
||||
/*Add option to metadata to identify the custom field*/
|
||||
if (!$this->isCore($name, $scope)) {
|
||||
if ($this->isCustom($name, $scope)) {
|
||||
$fieldDef['isCustom'] = true;
|
||||
}
|
||||
|
||||
@@ -167,21 +173,29 @@ class EntityManager
|
||||
return (bool) $res;
|
||||
}
|
||||
|
||||
public function delete($name, $scope)
|
||||
public function delete($name)
|
||||
{
|
||||
if ($this->isCore($name, $scope)) {
|
||||
throw new Error('Cannot delete core field ['.$name.'] in '.$scope);
|
||||
if (!$this->isCustom($name)) {
|
||||
throw new Forbidden;
|
||||
}
|
||||
|
||||
$unsets = array(
|
||||
'fields.'.$name,
|
||||
'links.'.$name,
|
||||
'entityDefs',
|
||||
'clientDefs',
|
||||
'scopes'
|
||||
);
|
||||
$res = $this->getMetadata()->delete($unsets, $this->metadataType, $name);
|
||||
|
||||
$res = $this->getMetadata()->delete($unsets, $this->metadataType, $scope);
|
||||
$res &= $this->deleteLabel($name, $scope);
|
||||
$this->getFileManager()->removeFile("custom/Espo/Custom/Resources/metadata/entityDefs/{$name}.json");
|
||||
$this->getFileManager()->removeFile("custom/Espo/Custom/Resources/metadata/clientDefs/{$name}.json");
|
||||
$this->getFileManager()->removeFile("custom/Espo/Custom/Resources/metadata/scopes/{$name}.json");
|
||||
|
||||
return (bool) $res;
|
||||
$this->getFileManager()->removeFile("custom/Espo/Custom/Entities/{$name}.php");
|
||||
$this->getFileManager()->removeFile("custom/Espo/Custom/Services/{$name}.php");
|
||||
$this->getFileManager()->removeFile("custom/Espo/Custom/Controllers/{$name}.php");
|
||||
$this->getFileManager()->removeFile("custom/Espo/Custom/Repositories/{$name}.php");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function setEntityDefs($name, $fieldDef, $scope)
|
||||
@@ -316,21 +330,8 @@ class EntityManager
|
||||
return $this->isChanged;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a field is core field
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $scope
|
||||
* @return boolean
|
||||
*/
|
||||
protected function isCore($name, $scope)
|
||||
protected function isCustom($name)
|
||||
{
|
||||
$existingField = $this->getFieldDef($name, $scope);
|
||||
if (isset($existingField) && (!isset($existingField['isCustom']) || !$existingField['isCustom'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->getMetadata()->get('scopes.' . $name . '.isCustom');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
"Available Fields": "Available Fields",
|
||||
"Layout": "Layout",
|
||||
"Entity Manager": "Entity Manager",
|
||||
"Relationship Manager": "Relationship Manager",
|
||||
"Add Panel": "Add Panel",
|
||||
"Add Field": "Add Field",
|
||||
"Settings": "Settings",
|
||||
@@ -133,7 +132,6 @@
|
||||
"layoutManager": "Customize layouts (list, detail, edit, search, mass update).",
|
||||
"fieldManager": "Create new fields or customize existing ones.",
|
||||
"entityManager": "Create custom entities. Edit existing ones.",
|
||||
"relationshipManager": "Manage relationships between entities.",
|
||||
"userInterface": "Configure UI.",
|
||||
"authTokens": "Active auth sessions. IP address and last access date.",
|
||||
"authentication": "Authentication settings.",
|
||||
|
||||
@@ -186,7 +186,8 @@
|
||||
"createdAt": "Created At",
|
||||
"modifiedAt": "Modified At",
|
||||
"createdBy": "Created By",
|
||||
"modifiedBy": "Modified By"
|
||||
"modifiedBy": "Modified By",
|
||||
"description": "Description"
|
||||
},
|
||||
"links": {
|
||||
"assignedUser": "Assigned User",
|
||||
@@ -198,10 +199,10 @@
|
||||
"users": "Users"
|
||||
},
|
||||
"dashlets": {
|
||||
"Stream": "Stream"
|
||||
"Stream": "Stream"
|
||||
},
|
||||
"streamMessages": {
|
||||
"create": "{user} created {entityType} {entity}",
|
||||
"create": "{user} created {entityType} {entity}",
|
||||
"createAssigned": "{user} created {entityType} {entity} assigned to {assignee}",
|
||||
"assign": "{user} assigned {entityType} {entity} to {assignee}",
|
||||
"post": "{user} posted on {entityType} {entity}",
|
||||
|
||||
@@ -112,11 +112,6 @@
|
||||
"label":"Entity Manager",
|
||||
"description":"entityManager"
|
||||
},
|
||||
{
|
||||
"url":"#Admin/relationshipManager",
|
||||
"label":"Relationship Manager",
|
||||
"description":"relationshipManager"
|
||||
},
|
||||
{
|
||||
"url":"#Admin/userInterface",
|
||||
"label":"User Interface",
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Controllers;
|
||||
class Test extends \Espo\Core\Templates\Controllers\Base
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Entities;
|
||||
class Test extends \Espo\Core\Templates\Entities\Base
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Repositories;
|
||||
class Test extends \Espo\Core\Templates\Repositories\Base
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
namespace Espo\Custom\Services;
|
||||
class Test extends \Espo\Core\Templates\Services\Base
|
||||
{
|
||||
}
|
||||
@@ -109,7 +109,7 @@ Espo.define('Views.Admin.EntityManager.Index', 'View', function (Dep) {
|
||||
url: 'EntityManager/action/removeEntity',
|
||||
type: 'POST',
|
||||
data: JSON.stringify({
|
||||
scope: scope
|
||||
name: scope
|
||||
})
|
||||
}).done(function () {
|
||||
this.$el.find('table tr[data-scope="'+scope+'"]').remove();
|
||||
|
||||
@@ -133,6 +133,26 @@ Espo.define('Views.Admin.EntityManager.Modals.EditEntity', 'Views.Modal', functi
|
||||
});
|
||||
},
|
||||
|
||||
afterRender: function () {
|
||||
this.getView('name').on('change', function (m) {
|
||||
var name = this.model.get('name');
|
||||
|
||||
name = name.charAt(0).toUpperCase() + name.slice(1);
|
||||
|
||||
this.model.set('labelSingular', name);
|
||||
this.model.set('labelPlural', name + 's') ;
|
||||
if (name) {
|
||||
name = name.replace(/\-/g, ' ').replace(/_/g, ' ').replace(/[^\w\s]/gi, '').replace(/ (.)/g, function(match, g) {
|
||||
return g.toUpperCase();
|
||||
}).replace(' ', '');
|
||||
if (name.length) {
|
||||
name = name.charAt(0).toUpperCase() + name.slice(1);
|
||||
}
|
||||
}
|
||||
this.model.set('name', name);
|
||||
}, this);
|
||||
},
|
||||
|
||||
save: function () {
|
||||
var arr = [
|
||||
'name',
|
||||
|
||||
@@ -17,14 +17,14 @@
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
************************************************************************/
|
||||
************************************************************************/
|
||||
|
||||
Espo.define('Views.Admin.FieldManager.Edit', 'View', function (Dep) {
|
||||
|
||||
|
||||
return Dep.extend({
|
||||
|
||||
|
||||
template: 'admin.field-manager.edit',
|
||||
|
||||
|
||||
data: function () {
|
||||
return {
|
||||
scope: this.scope,
|
||||
@@ -35,7 +35,7 @@ Espo.define('Views.Admin.FieldManager.Edit', 'View', function (Dep) {
|
||||
fieldList: this.fieldList,
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
events: {
|
||||
'click button[data-action="cancel"]': function () {
|
||||
this.getRouter().navigate('#Admin/fieldManager/scope=' + this.scope, {trigger: true});
|
||||
@@ -44,94 +44,90 @@ Espo.define('Views.Admin.FieldManager.Edit', 'View', function (Dep) {
|
||||
this.save();
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
setup: function () {
|
||||
this.scope = this.options.scope;
|
||||
this.field = this.options.field;
|
||||
this.type = this.options.type;
|
||||
this.defs = {};
|
||||
|
||||
|
||||
this.fieldList = [];
|
||||
|
||||
|
||||
this.isNew = false;
|
||||
if (!this.field) {
|
||||
this.isNew = true;
|
||||
}
|
||||
|
||||
|
||||
this.model = new Espo.Model();
|
||||
this.model.name = 'Admin';
|
||||
this.model.urlRoot = 'Admin/fieldManager/' + this.scope;
|
||||
|
||||
|
||||
this.model.defs = {
|
||||
fields: {
|
||||
name: {required: true},
|
||||
label: {required: true},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
if (!this.isNew) {
|
||||
this.model.id = this.field;
|
||||
this.model.set('name', this.field);
|
||||
this.model.set('name', this.field);
|
||||
this.model.set('label', this.getLanguage().translate(this.field, 'fields', this.scope));
|
||||
} else {
|
||||
this.model.set('type', this.type);
|
||||
}
|
||||
|
||||
|
||||
|
||||
this.wait(true);
|
||||
this.getModelFactory().create(this.scope, function (model) {
|
||||
|
||||
|
||||
if (!this.isNew) {
|
||||
this.type = model.getFieldType(this.field);
|
||||
this.defs = model.defs.fields[this.field];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
this.params = this.getFieldManager().getParams(this.type) || [];
|
||||
|
||||
|
||||
this.params.forEach(function (o) {
|
||||
this.model.defs.fields[o.name] = o;
|
||||
}, this);
|
||||
|
||||
this.model.set(this.defs);
|
||||
|
||||
}, this);
|
||||
|
||||
this.model.set(this.defs);
|
||||
|
||||
if (this.isNew) {
|
||||
this.model.populateDefaults();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.createFieldView('varchar', 'name', !this.isNew);
|
||||
this.createFieldView('varchar', 'label');
|
||||
|
||||
|
||||
|
||||
this.getView('name').on('change', function (m) {
|
||||
var name = this.model.get('name');
|
||||
this.model.set('label', name);
|
||||
if (name) {
|
||||
name = name.replace('-', '').replace(/[^\w\s]/gi, '').replace(/ (.)/g, function(match, g) {
|
||||
return g.toUpperCase();
|
||||
}).replace(' ', '');
|
||||
if (name.length) {
|
||||
name = name.charAt(0).toLowerCase() + name.slice(1);
|
||||
}
|
||||
}
|
||||
this.model.set('name', name);
|
||||
}, this);
|
||||
|
||||
this.params.forEach(function (o) {
|
||||
if (o.hidden) {
|
||||
return;
|
||||
}
|
||||
this.createFieldView(o.type, o.name, null, o);
|
||||
}, this);
|
||||
|
||||
|
||||
|
||||
this.wait(false);
|
||||
}.bind(this));
|
||||
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
|
||||
afterRender: function () {
|
||||
this.getView('name').on('change', function (m) {
|
||||
var name = this.model.get('name');
|
||||
this.model.set('label', name);
|
||||
if (name) {
|
||||
name = name.replace('-', '').replace(/[^\w\s]/gi, '').replace(/ (.)/g, function(match, g) {
|
||||
return g.toUpperCase();
|
||||
}).replace(' ', '');
|
||||
if (name.length) {
|
||||
name = name.charAt(0).toLowerCase() + name.slice(1);
|
||||
}
|
||||
}
|
||||
this.model.set('name', name);
|
||||
}, this);
|
||||
},
|
||||
|
||||
createFieldView: function (type, name, readOnly, params) {
|
||||
var viewName = (params || {}).view || this.getFieldManager().getViewName(type);
|
||||
this.createView(name, viewName, {
|
||||
@@ -148,30 +144,30 @@ Espo.define('Views.Admin.FieldManager.Edit', 'View', function (Dep) {
|
||||
});
|
||||
this.fieldList.push(name);
|
||||
},
|
||||
|
||||
|
||||
save: function () {
|
||||
this.fieldList.forEach(function (field) {
|
||||
var view = this.getView(field);
|
||||
if (!view.readOnly) {
|
||||
view.fetchToModel();
|
||||
}
|
||||
}, this);
|
||||
|
||||
}, this);
|
||||
|
||||
var notValid = false;
|
||||
this.fieldList.forEach(function (field) {
|
||||
notValid = this.getView(field).validate() || notValid;
|
||||
}, this);
|
||||
|
||||
|
||||
if (notValid) {
|
||||
this.notify('Not valid', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
this.listenToOnce(this.model, 'sync', function () {
|
||||
}
|
||||
|
||||
this.listenToOnce(this.model, 'sync', function () {
|
||||
this.getMetadata().data['entityDefs'][this.scope]['fields'][this.model.get('name')] = this.model.toJSON();
|
||||
this.getMetadata().storeToCache();
|
||||
this.notify('Saved', 'success');
|
||||
|
||||
|
||||
var data = this.getLanguage().data;
|
||||
if (this.scope in data) {
|
||||
if (!('fields' in data[this.scope])) {
|
||||
@@ -179,7 +175,7 @@ Espo.define('Views.Admin.FieldManager.Edit', 'View', function (Dep) {
|
||||
}
|
||||
data[this.scope]['fields'][this.model.get('name')] = this.model.get('label');
|
||||
|
||||
|
||||
|
||||
if (this.model.get('type') == 'enum' && this.model.get('translatedOptions')) {
|
||||
if (!('options' in data[this.scope])) {
|
||||
data[this.scope]['options'] = {};
|
||||
@@ -187,14 +183,14 @@ Espo.define('Views.Admin.FieldManager.Edit', 'View', function (Dep) {
|
||||
data[this.scope]['options'][this.model.get('name')] = this.model.get('translatedOptions') || {};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.getRouter().navigate('#Admin/fieldManager/scope=' + this.scope + '&field=' + this.model.get('name'), {trigger: true});
|
||||
}.bind(this));
|
||||
|
||||
|
||||
this.notify('Saving...');
|
||||
this.model.save();
|
||||
},
|
||||
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user