Skip to content

EspoCRM – Open Source CRM Application

create category action

Yuri Kuznetsovbef5f4c76260
diff --git a/application/Espo/Core/Repositories/CategoryTree.php b/application/Espo/Core/Repositories/CategoryTree.php
index cb5a4c3b92..242d5d7f4c 100644
--- a/application/Espo/Core/Repositories/CategoryTree.php
+++ b/application/Espo/Core/Repositories/CategoryTree.php
@@ -31,6 +31,8 @@ namespace Espo\Core\Repositories;
use Espo\ORM\Entity;
use Espo\ORM\Mapper\BaseMapper;
+use Espo\ORM\Name\Attribute;
+use Espo\ORM\Query\Part\Order;
/**
* @template TEntity of Entity
@@ -38,6 +40,18 @@ use Espo\ORM\Mapper\BaseMapper;
*/
class CategoryTree extends Database
{
+ private const ATTR_ORDER = 'order';
+ private const ATTR_PARENT_ID = 'parentId';
+
+ protected function beforeSave(Entity $entity, array $options = [])
+ {
+ if ($entity->get(self::ATTR_ORDER) === null) {
+ $this->setOrderToEnd($entity);
+ }
+
+ parent::beforeSave($entity, $options);
+ }
+
/**
* @param array<string, mixed> $options
* @return void
@@ -46,7 +60,7 @@ class CategoryTree extends Database
{
parent::afterSave($entity, $options);
- $parentId = $entity->get('parentId');
+ $parentId = $entity->get(self::ATTR_PARENT_ID);
$em = $this->entityManager;
@@ -102,7 +116,7 @@ class CategoryTree extends Database
return;
}
- if (!$entity->isAttributeChanged('parentId')) {
+ if (!$entity->isAttributeChanged(self::ATTR_PARENT_ID)) {
return;
}
@@ -181,4 +195,24 @@ class CategoryTree extends Database
$mapper->deleteFromDb($entity->getEntityType(), $entity->getId());
}
+
+ private function setOrderToEnd(Entity $entity): void
+ {
+ $parentId = $entity->get(self::ATTR_PARENT_ID);
+
+ $where = [self::ATTR_PARENT_ID => $parentId];
+
+ if (!$entity->isNew()) {
+ $where[Attribute::ID . '!='] = $entity->getId();
+ }
+
+ $last = $this
+ ->where($where)
+ ->order(self::ATTR_ORDER, Order::DESC)
+ ->findOne();
+
+ $order = $last ? ($last->get(self::ATTR_ORDER) + 1) : 1;
+
+ $entity->set(self::ATTR_ORDER, $order);
+ }
}
diff --git a/application/Espo/Core/Templates/Metadata/CategoryTree/entityDefs.json b/application/Espo/Core/Templates/Metadata/CategoryTree/entityDefs.json
index 1e45699e5a..9644c12ba1 100644
--- a/application/Espo/Core/Templates/Metadata/CategoryTree/entityDefs.json
+++ b/application/Espo/Core/Templates/Metadata/CategoryTree/entityDefs.json
@@ -7,8 +7,7 @@
},
"order": {
"type": "int",
- "required": true,
- "default": 1
+ "minValue": 1
},
"description": {
"type": "text"
diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseCategory.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseCategory.json
index 8849c144ed..03388e9453 100644
--- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseCategory.json
+++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/KnowledgeBaseCategory.json
@@ -34,7 +34,7 @@
},
"order": {
"type": "int",
- "required": true,
+ "minValue": 1,
"disableFormatting": true,
"textFilterDisabled": true
},
diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/TargetListCategory.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/TargetListCategory.json
index 4799100599..3edce22786 100644
--- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/TargetListCategory.json
+++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/TargetListCategory.json
@@ -7,7 +7,6 @@
"order": {
"type": "int",
"minValue": 1,
- "required": true,
"textFilterDisabled": true
},
"description": {
diff --git a/application/Espo/Resources/metadata/entityDefs/EmailTemplateCategory.json b/application/Espo/Resources/metadata/entityDefs/EmailTemplateCategory.json
index 5ba3393a96..e177cd40f3 100644
--- a/application/Espo/Resources/metadata/entityDefs/EmailTemplateCategory.json
+++ b/application/Espo/Resources/metadata/entityDefs/EmailTemplateCategory.json
@@ -7,7 +7,6 @@
"order": {
"type": "int",
"minValue": 1,
- "required": true,
"textFilterDisabled": true
},
"description": {
diff --git a/client/res/templates/record/list-nested-categories.tpl b/client/res/templates/record/list-nested-categories.tpl
index 37d43081f7..fdbdde43bd 100644
--- a/client/res/templates/record/list-nested-categories.tpl
+++ b/client/res/templates/record/list-nested-categories.tpl
@@ -7,6 +7,17 @@
</a>
<ul class="dropdown-menu dropdown-menu-with-icons">
+ {{#if showCreate}}
+ <li>
+ <a
+ href="{{createLink}}"
+ class="action"
+ data-action="createCategory"
+ >
+ <span class="fas fa-folder-plus fa-sm"></span><span class="item-text">{{createCategoryLabel}}</span>
+ </a>
+ </li>
+ {{/if}}
{{#if showEditLink}}
<li>
<a
diff --git a/client/src/collections/tree.js b/client/src/collections/tree.js
index 437dcd0520..88dc6561df 100644
--- a/client/src/collections/tree.js
+++ b/client/src/collections/tree.js
@@ -57,6 +57,7 @@ class TreeCollection extends Collection {
this.path = response.path;
/**
* @type {{
+ * id: string,
* name: string,
* upperId?: string,
* upperName?: string,
diff --git a/client/src/controllers/record-tree.js b/client/src/controllers/record-tree.js
index dba2dca06b..c16ad1c0a0 100644
--- a/client/src/controllers/record-tree.js
+++ b/client/src/controllers/record-tree.js
@@ -59,6 +59,20 @@ class RecordTreeController extends RecordController {
});
});
}
+
+ async create(options = {}) {
+ if (options.parentId) {
+ options.attributes ??= {};
+
+ options.attributes.parentId = options.parentId;
+ options.attributes.parentName = options.parentName;
+
+ delete options.parentId;
+ delete options.parentName;
+ }
+
+ return super.create(options);
+ }
}
export default RecordTreeController;
diff --git a/client/src/controllers/record.js b/client/src/controllers/record.js
index 43a82f3b9a..ec166498be 100644
--- a/client/src/controllers/record.js
+++ b/client/src/controllers/record.js
@@ -308,36 +308,34 @@ class RecordController extends Controller {
* attributes?: Record,
* } | Record} [options]
*/
- create(options) {
- options = options || {};
-
+ async create(options = {}) {
const optionsOptions = options.options || {};
- this.getModel().then(model => {
- if (options.relate) {
- model.setRelate(options.relate);
- }
+ const model = await this.getModel();
- const o = {
- scope: this.name,
- model: model,
- returnUrl: options.returnUrl,
- returnDispatchParams: options.returnDispatchParams,
- params: options,
- };
+ if (options.relate) {
+ model.setRelate(options.relate);
+ }
- for (const k in optionsOptions) {
- o[k] = optionsOptions[k];
- }
+ const o = {
+ scope: this.name,
+ model: model,
+ returnUrl: options.returnUrl,
+ returnDispatchParams: options.returnDispatchParams,
+ params: options,
+ };
- if (options.attributes) {
- model.set(options.attributes);
- }
+ for (const k in optionsOptions) {
+ o[k] = optionsOptions[k];
+ }
+
+ if (options.attributes) {
+ model.set(options.attributes);
+ }
- this.prepareModelCreate(model, options);
+ this.prepareModelCreate(model, options);
- this.main(this.getViewName('edit'), o);
- });
+ this.main(this.getViewName('edit'), o);
}
/**
diff --git a/client/src/router.js b/client/src/router.js
index 1c6f8714e7..07c5268d57 100644
--- a/client/src/router.js
+++ b/client/src/router.js
@@ -540,7 +540,7 @@ const Router = Backbone.Router.extend(/** @lends Router# */ {
options[p[0]] = true;
if (p.length > 1) {
- options[p[0]] = p[1];
+ options[p[0]] = decodeURIComponent(p[1]);
}
});
}
diff --git a/client/src/views/record/list-nested-categories.js b/client/src/views/record/list-nested-categories.js
index a78f1725ad..d7ce7b72f5 100644
--- a/client/src/views/record/list-nested-categories.js
+++ b/client/src/views/record/list-nested-categories.js
@@ -29,6 +29,7 @@
/** @module views/record/list-nested-categories */
import View from 'view';
+import RecordModalHelper from 'helpers/record-modal';
class ListNestedCategoriesRecordView extends View {
@@ -74,6 +75,17 @@ class ListNestedCategoriesRecordView extends View {
*/
subjectEntityType
+ /**
+ * @protected
+ * @type {string}
+ */
+ categoryEntityType
+
+ /**
+ * @private
+ */
+ showCreate
+
data() {
const data = {};
@@ -90,10 +102,30 @@ class ListNestedCategoriesRecordView extends View {
data.showFolders = !this.isExpanded;
data.hasExpandedToggler = this.options.hasExpandedToggler;
data.showEditLink = this.options.showEditLink;
+ data.showCreate = this.showCreate;
data.hasNavigationPanel = this.hasNavigationPanel;
+ data.createCategoryLabel =
+ this.translate(`Create ${this.categoryEntityType}`, 'labels', this.categoryEntityType);
+
const categoryData = this.collection.categoryData || {};
+ if (this.showCreate) {
+ data.createLink = `#${this.categoryEntityType}/create`;
+
+ let createReturnUrl = `#${this.subjectEntityType}`;
+
+ if (categoryData.id) {
+ createReturnUrl += `/list/categoryId=${categoryData.id}`;
+ }
+
+ data.createLink += `?returnUrl=${encodeURIComponent(createReturnUrl)}`;
+
+ if (categoryData.id) {
+ data.createLink += `&parentId=${categoryData.id}&parentName=${categoryData.name}`;
+ }
+ }
+
data.upperLink = categoryData.upperId ?
'#' + this.subjectEntityType + '/list/categoryId=' + categoryData.upperId:
'#' + this.subjectEntityType;
@@ -158,8 +190,14 @@ class ListNestedCategoriesRecordView extends View {
this.hasNavigationPanel = this.options.hasNavigationPanel;
this.itemCollection = this.options.itemCollection;
+ this.categoryEntityType = this.collection.entityType;
+
+ this.showCreate = this.getAcl().check(this.categoryEntityType, 'create');
+
this.listenTo(this.collection, 'sync', () => this.reRender());
this.listenTo(this.itemCollection, 'sync', () => this.reRender());
+
+ this.addActionHandler('createCategory', () => this.actionCreateCategory());
}
// noinspection JSUnusedGlobalSymbols
@@ -171,6 +209,27 @@ class ListNestedCategoriesRecordView extends View {
more: true,
});
}
+
+ /**
+ * @private
+ */
+ async actionCreateCategory() {
+ const categoryData = this.collection.categoryData || {};
+
+ const view = await new RecordModalHelper().showCreate(this, {
+ entityType: this.categoryEntityType,
+ attributes: {
+ parentId: categoryData.id ?? null,
+ parentName: categoryData.name ?? null,
+ },
+ rootUrl: this.getRouter().getCurrentUrl(),
+ afterSave: () => {
+ this.collection.fetch();
+ },
+ });
+
+ await view.render();
+ }
}
// noinspection JSUnusedGlobalSymbols