import revert/remove duplicates

This commit is contained in:
yuri
2015-03-30 12:26:41 +03:00
parent fc0d8dffcd
commit 8a7325963b
7 changed files with 145 additions and 37 deletions

View File

@@ -90,7 +90,18 @@ class Import extends \Espo\Core\Controllers\Record
public function actionRevert($params, $data)
{
return $this->getService('Import')->revert($data['entityType'], $data['idsToRemove']);
if (empty($data['id'])) {
throw new BadRequest();
}
return $this->getService('Import')->revert($data['id']);
}
public function actionRemoveDuplicates($params, $data)
{
if (empty($data['id'])) {
throw new BadRequest();
}
return $this->getService('Import')->removeDuplicates($data['id']);
}
public function actionCreate($params, $data)

View File

@@ -148,6 +148,7 @@
"Select All Result": "Select All Result"
},
"messages": {
"pleaseWait": "Please wait...",
"notModified": "You have not modified the record",
"duplicate": "The record you are creating seems to be a duplicate",
"fieldIsRequired": "{field} is required",

View File

@@ -11,10 +11,14 @@
"Updated": "Updated",
"Result": "Result",
"Show records": "Show records",
"Remove Duplicates": "Remove Duplicates"
"Remove Duplicates": "Remove Duplicates",
"importedCount": "Imported (count)",
"duplicateCount": "Duplicates (count)",
"updatedCount": "Updated (count)"
},
"messages": {
"utf8": "Should be UTF-8 encoded"
"utf8": "Should be UTF-8 encoded",
"duplicatesRemoved": "Duplicates removed"
},
"fields": {
"file": "File",

View File

@@ -8,24 +8,6 @@
"list": "Import.List",
"detail": "Import.Detail"
},
"menu": {
"detail": {
"buttons": [
{
"label": "Revert Import",
"action": "revert",
"style": "danger",
"acl": "edit"
},
{
"label": "Remove Duplicates",
"action": "removeDuplicates",
"style": "default",
"acl": "edit"
}
]
}
},
"bottomPanels": {
"detail": [
{

View File

@@ -9,6 +9,21 @@
"type": "file",
"required": true
},
"importedCount": {
"type": "int",
"readOnly": true,
"notStorable": true
},
"duplicateCount": {
"type": "int",
"readOnly": true,
"notStorable": true
},
"updatedCount": {
"type": "int",
"readOnly": true,
"notStorable": true
},
"createdAt": {
"type": "datetime",
"readOnly": true

View File

@@ -25,6 +25,7 @@ namespace Espo\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\BadRequest;
use Espo\ORM\Entity;
@@ -83,6 +84,20 @@ class Import extends \Espo\Services\Record
return $this->injections['serviceFactory'];
}
protected function loadAdditionalFields(Entity $entity)
{
parent::loadAdditionalFields($entity);
$importedCount = $this->getRepository()->countRelated($entity, 'imported');
$duplicateCount = $this->getRepository()->countRelated($entity, 'duplicates');
$updatedCount = $this->getRepository()->countRelated($entity, 'updated');
$entity->set(array(
'importedCount' => $importedCount,
'duplicateCount' => $duplicateCount,
'updatedCount' => $updatedCount,
));
}
public function findLinkedEntities($id, $link, $params)
{
$entity = $this->getRepository()->get($id);
@@ -178,21 +193,68 @@ class Import extends \Espo\Services\Record
return $o;
}
public function revert($scope, array $idsToRemove)
public function revert($id)
{
$ids = array();
if (!empty($scope) && !empty($idsToRemove)) {
foreach ($idsToRemove as $id) {
$entity = $this->getEntityManager()->getEntity($scope, $id);
if ($entity) {
if ($this->getEntityManager()->removeEntity($entity)) {
$ids[] = $id;
}
}
$this->getEntityManager()->getRepository($scope)->deleteFromDb($id);
}
$import = $this->getEntityManager()->getEntity('Import', $id);
if (empty($import)) {
throw new NotFound();
}
return $ids;
$pdo = $this->getEntityManager()->getPDO();
$sql = "SELECT * FROM import_entity WHERE import_id = ".$pdo->quote($import->id) . " AND is_imported = 1";
$sth = $pdo->prepare($sql);
$sth->execute();
while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
if (empty($row['entity_type']) || empty($row['entity_id'])) {
continue;
}
$entityType = $row['entity_type'];
$entityId = $row['entity_id'];
$entity = $this->getEntityManager()->getEntity($entityType, $entityId);
if ($entity) {
$this->getEntityManager()->removeEntity($entity);
}
$this->getEntityManager()->getRepository($scope)->deleteFromDb($entityId);
}
$this->getEntityManager()->removeEntity($import);
return true;
}
public function removeDuplicates($id)
{
$import = $this->getEntityManager()->getEntity('Import', $id);
if (empty($import)) {
throw new NotFound();
}
$pdo = $this->getEntityManager()->getPDO();
$sql = "SELECT * FROM import_entity WHERE import_id = ".$pdo->quote($import->id) . " AND is_duplicate = 1";
$sth = $pdo->prepare($sql);
$sth->execute();
while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
if (empty($row['entity_type']) || empty($row['entity_id'])) {
continue;
}
$entityType = $row['entity_type'];
$entityId = $row['entity_id'];
$entity = $this->getEntityManager()->getEntity($entityType, $entityId);
if ($entity) {
$this->getEntityManager()->removeEntity($entity);
}
$this->getEntityManager()->getRepository($scope)->deleteFromDb($entityId);
}
return true;
}
public function import($scope, array $fields, $attachmentId, array $params = array())

View File

@@ -34,8 +34,32 @@ Espo.define('Views.Import.Detail', 'Views.Detail', function (Dep) {
]);
},
setup: function () {
Dep.prototype.setup.call(this);
if (this.model.get('importedCount')) {
this.menu.buttons.unshift({
"label": "Revert Import",
"action": "revert",
"style": "danger",
"acl": "edit"
});
}
if (this.model.get('duplicateCount')) {
this.menu.buttons.unshift({
"label": "Remove Duplicates",
"action": "removeDuplicates",
"style": "default",
"acl": "edit"
});
}
},
actionRevert: function () {
if (confirm(this.translate('confirmation', 'messages'))) {
$btn = this.$el.find('button[data-action="revert"]');
$btn.addClass('disabled');
Espo.Ui.notify(this.translate('pleaseWait', 'messages'));
$.ajax({
type: 'POST',
url: 'Import/action/revert',
@@ -43,13 +67,20 @@ Espo.define('Views.Import.Detail', 'Views.Detail', function (Dep) {
id: this.model.id
})
}).done(function () {
Espo.Ui.notify(false);
this.getRouter().navigate('#Import/list', {trigger: true});
});
}.bind(this));
}
},
actionRemoveDuplicates: function () {
if (confirm(this.translate('confirmation', 'messages'))) {
$btn = this.$el.find('button[data-action="removeDuplicates"]');
$btn.addClass('disabled');
Espo.Ui.notify(this.translate('pleaseWait', 'messages'));
$.ajax({
type: 'POST',
url: 'Import/action/removeDuplicates',
@@ -57,8 +88,10 @@ Espo.define('Views.Import.Detail', 'Views.Detail', function (Dep) {
id: this.model.id
})
}).done(function () {
this.getRouter().navigate('#Import/list', {trigger: true});
});
$btn.remove();
this.model.fetch();
Espo.Ui.success(this.translate('duplicatesRemoved', 'messages', 'Import'))
}.bind(this));
}
}