This commit is contained in:
yuri
2015-04-28 17:53:31 +03:00
parent c9a1e3733c
commit 1e67fe69e1
14 changed files with 423 additions and 12 deletions

View File

@@ -34,6 +34,8 @@ class Record extends Base
public static $defaultAction = 'list';
protected $defaultRecordServiceName = 'Record';
protected function getEntityManager()
{
return $this->getContainer()->get('entityManager');
@@ -48,7 +50,7 @@ class Record extends Base
if ($this->getServiceFactory()->checkExists($name)) {
$service = $this->getServiceFactory()->create($name);
} else {
$service = $this->getServiceFactory()->create('Record');
$service = $this->getServiceFactory()->create($this->defaultRecordServiceName);
$service->setEntityName($name);
}

View File

@@ -0,0 +1,60 @@
<?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\Controllers;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Exceptions\BadRequest;
use \Espo\Core\Utils\Util;
class RecordTree extends Record
{
public static $defaultAction = 'list';
protected $defaultRecordServiceName = 'RecordTree';
public function actionListTree($params, $data, $request)
{
if (!$this->getAcl()->check($this->name, 'read')) {
throw new Forbidden();
}
$where = $request->get('where');
$parentId = $request->get('parentId');
$asc = $request->get('asc') === 'true';
$sortBy = $request->get('sortBy');
$collection = $this->getRecordService()->getTree($parentId, array(
'where' => $where,
'asc' => $asc,
'sortBy' => $sortBy,
));
return array(
'list' => $collection->toArray(),
'path' => $this->getRecordService()->getTreeItemPath($parentId)
);
}
}

View File

@@ -0,0 +1,43 @@
<?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\Entities;
class TreeItem extends \Espo\Core\ORM\Entity
{
public function toArray()
{
$data = parent::toArray();
$childList = $this->get('childList');
if (is_null($childList)) {
$data['childList'] = null;
} else {
$arr = [];
foreach ($childList as $entity) {
$arr[] = $entity->toArray();
}
$data['childList'] = $arr;
}
return $data;
}
}

View File

@@ -0,0 +1,74 @@
<?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\Services;
use \Espo\ORM\Entity;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\BadRequest;
use \Espo\Core\Exceptions\NotFound;
class RecordTree extends Record
{
const MAX_DEPTH = 2;
public function getTree($parentId = null, $params = array(), $level = 0)
{
if ($level == self::MAX_DEPTH) {
return null;
}
$selectParams = $this->getSelectParams($params);
$selectParams['whereClause'][] = array(
'parentId' => $parentId
);
$collection = $this->getRepository()->find($selectParams);
foreach ($collection as $entity) {
$childList = $this->getTree($entity->id, $params, $level + 1);
$entity->set('childList', $childList);
}
return $collection;
}
public function getTreeItemPath($parentId = null)
{
$arr = [];
while (1) {
if (empty($parentId)) {
break;
}
$parent = $this->getEntityManager()->getEntity($this->entityType, $parentId);
if ($parent) {
$parentId = $parent->get('parentId');
array_unshift($arr, $parent->id);
} else {
$parentId = null;
}
}
return $arr;
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Espo\Custom\Controllers;
class ArticleCategory extends \Espo\Core\Controllers\RecordTree
{
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Espo\Custom\Entities;
class ArticleCategory extends \Espo\Core\Entities\TreeItem
{
protected $entityType = "ArticleCategory";
}

View File

@@ -0,0 +1,5 @@
<?php
namespace Espo\Custom\Repositories;
class ArticleCategory extends \Espo\Core\Templates\Repositories\Base
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Espo\Custom\Services;
class ArticleCategory extends \Espo\Services\RecordTree
{
}

View File

@@ -1,8 +1,8 @@
<li data-id="{{model.id}}" class="list-group-item">
<li data-id="{{model.id}}" class="list-group-item">
<% if (layout.right) { %>
<div class="pull-right right">
{{{<%= layout.right.name %>}}}
</div>
</div>
<% } %>
<% _.each(layout.rows, function (row, key) { %>
<div class="expanded-row">
@@ -10,7 +10,7 @@
<span class="cell cell-<%= defs.name %>"><%
var tag = 'tag' in defs ? defs.tag : false;
if (tag) {
print( '<' + tag);
print( '<' + tag);
if ('id' in defs) {
print(' id="'+defs.id+'"');
}
@@ -18,7 +18,7 @@
print(' class="'+defs.class+'"');
};
print('>');
}
}
%>{{{<%= defs.name %>}}}<%
if (tag) {
print( '</' + tag + '>');

View File

@@ -0,0 +1,42 @@
/************************************************************************
* 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/.
************************************************************************/
Espo.define('Controllers.RecordTree', 'Controllers.Record', function (Dep) {
return Dep.extend({
defaultAction: 'listTree',
beforeListTree: function () {
this.handleCheckAccess('read');
},
listTree: function (options) {
this.getCollection(function (collection) {
this.main(this.getViewName('listTree'), {
scope: this.name,
collection: collection
});
});
}
});
});

View File

@@ -0,0 +1,36 @@
/************************************************************************
* 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/.
************************************************************************/
Espo.define('Views.ListTree', 'Views.List', function (Dep) {
return Dep.extend({
searchPanel: false,
createButton: false,
getRecordViewName: function () {
return this.getMetadata().get('clientDefs.' + this.scope + '.recordViews.listTree') || 'Record.ListTree';
}
});
});

View File

@@ -113,10 +113,14 @@ Espo.define('Views.List', ['Views.Main', 'SearchManager'], function (Dep, Search
}
},
getRecordViewName: function () {
return this.getMetadata().get('clientDefs.' + this.scope + '.recordViews.list') || 'Record.List';
},
afterRender: function () {
this.notify('Loading...');
var listViewName = this.getMetadata().get('clientDefs.' + this.scope + '.recordViews.list') || 'Record.List';
var listViewName = this.getRecordViewName();
this.listenToOnce(this.collection, 'sync', function () {
this.createView('list', listViewName, {

View File

@@ -0,0 +1,125 @@
/************************************************************************
* 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/.
************************************************************************/
Espo.define('Views.Record.ListTree', 'Views.Record.List', function (Dep) {
return Dep.extend({
template: 'record.list-tree',
checkboxes: false,
selectable: false,
rowActionsView: false,
_internalLayoutType: 'list-row-expanded',
presentationType: 'tree',
header: false,
listContainerEl: '.list > ul',
_loadListLayout: function (callback) {
var type = this.type + 'Expanded';
this._helper.layoutManager.get(this.collection.name, type, function (listLayout) {
callback(listLayout);
});
},
_convertLayout: function (listLayout, model) {
model = model || this.collection.model.prototype;
var layout = {
rows: [],
right: false,
};
for (var i in listLayout.rows) {
var row = listLayout.rows[i];
var layoutRow = [];
for (var j in row) {
var e = row[j];
var type = e.type || model.getFieldType(e.name) || 'base';
var item = {
name: e.name,
view: e.view || model.getFieldParam(e.name, 'view') || this.getFieldManager().getViewName(type),
options: {
defs: {
name: e.name,
params: e.params || {}
},
mode: 'list'
}
};
if (e.link) {
item.options.mode = 'listLink';
}
layoutRow.push(item);
}
layout.rows.push(layoutRow);
}
if ('right' in listLayout) {
if (listLayout.right != false) {
layout.right = {
name: listLayout.right.name || 'right',
view: listLayout.right.view,
options: {
defs: {
params: {
width: listLayout.right.width || '7%'
}
}
}
};
}
} else {
if (this.rowActionsView) {
layout.right = this.getRowActionsDefs();
}
}
return layout;
},
getRowSelector: function (id) {
return 'li[data-id="' + id + '"]';
},
getItemEl: function (model, item) {
return this.options.el + ' li[data-id="' + model.id + '"] span.cell-' + item.name;
},
prepareInterbalLayout: function (internalLayout, model) {
var rows = internalLayout.rows || [];
rows.forEach(function (row) {
row.forEach(function (col) {
col.el = this.getItemEl(model, col);
}, this);
}, this);
},
});
});

View File

@@ -740,9 +740,7 @@ Espo.define('Views.Record.List', 'View', function (Dep) {
this.checkedList = [];
this.rows = [];
if (this.collection.length > 0) {
var i = 0;
var c = !this.pagination ? 1 : 2;
var func = function () {
@@ -756,10 +754,10 @@ Espo.define('Views.Record.List', 'View', function (Dep) {
this.wait(true);
this.getInternalLayout(function (internalLayout) {
var count = this.collection.models.length;
var modelList = this.collection.models;
var count = modelList.length;
var built = 0;
for (var i in this.collection.models) {
var model = this.collection.models[i];
modelList.forEach(function (model) {
this.buildRow(i, model, function () {
built++;
if (built == count) {
@@ -767,7 +765,7 @@ Espo.define('Views.Record.List', 'View', function (Dep) {
this.wait(false);
}
}.bind(this));
}
}, this);
}.bind(this));
if (this.pagination) {