mass actions refactoring

This commit is contained in:
Yuri Kuznetsov
2019-11-08 13:07:07 +02:00
parent befb2327a3
commit ea41d2c40c
3 changed files with 74 additions and 195 deletions
+19 -34
View File
@@ -254,28 +254,28 @@ class Record extends Base
$byWhere = isset($data->byWhere) ? $data->byWhere : false;
$selectData = isset($data->selectData) ? json_decode(json_encode($data->selectData), true) : null;
$params = array();
$actionParams = [];
if ($byWhere) {
$params['selectData'] = $selectData;
$params['where'] = $where;
$actionParams['selectData'] = $selectData;
$actionParams['where'] = $where;
} else {
$params['ids'] = $ids;
$actionParams['ids'] = $ids;
}
if (isset($data->attributeList)) {
$params['attributeList'] = $data->attributeList;
$actionParams['attributeList'] = $data->attributeList;
}
if (isset($data->fieldList)) {
$params['fieldList'] = $data->fieldList;
$actionParams['fieldList'] = $data->fieldList;
}
if (isset($data->format)) {
$params['format'] = $data->format;
$actionParams['format'] = $data->format;
}
return [
'id' => $this->getRecordService()->export($params)
'id' => $this->getRecordService()->export($actionParams),
];
}
@@ -296,21 +296,11 @@ class Record extends Base
throw new Forbidden();
}
$params = array();
if (property_exists($data, 'where') && !empty($data->byWhere)) {
$params['where'] = json_decode(json_encode($data->where), true);
if (property_exists($data, 'selectData')) {
$params['selectData'] = json_decode(json_encode($data->selectData), true);
}
} else if (property_exists($data, 'ids')) {
$params['ids'] = $data->ids;
}
$actionParams = $this->getMassActionParamsFromData($data);
$attributes = $data->attributes;
$idsUpdated = $this->getRecordService()->massUpdate($params, $attributes);
return $idsUpdated;
return $this->getRecordService()->massUpdate($actionParams, $attributes);
}
public function postActionMassDelete($params, $data, $request)
@@ -507,8 +497,7 @@ class Record extends Base
if (property_exists($data, 'selectData')) {
$params['selectData'] = json_decode(json_encode($data->selectData), true);
}
}
if (property_exists($data, 'ids')) {
} else if (property_exists($data, 'ids')) {
$params['ids'] = $data->ids;
}
@@ -538,27 +527,21 @@ class Record extends Base
if (!$this->getAcl()->checkScope($this->name, 'edit')) throw new Forbidden();
if ($this->getAcl()->get('massUpdatePermission') !== 'yes') throw new Forbidden();
$actionParams = $this->getMassActionParamsFromData($data);
$fieldList = $data->fieldList ?? null;
if (!empty($data->field)) {
if (!is_array($fieldList)) $fieldList = [];
$fieldList[] = $data->field;
}
$params = [];
if (property_exists($data, 'where') && !empty($data->byWhere)) {
$params['where'] = json_decode(json_encode($data->where), true);
if (property_exists($data, 'selectData')) {
$params['selectData'] = json_decode(json_encode($data->selectData), true);
}
} else if (property_exists($data, 'ids')) {
$params['ids'] = $data->ids;
}
if (empty($data->currencyRates)) throw new BadRequest();
if (empty($data->targetCurrency)) throw new BadRequest();
if (empty($data->baseCurrency)) throw new BadRequest();
return $this->getRecordService()->massConvertCurrency($params, $data->targetCurrency, $data->baseCurrency, $data->currencyRates, $fieldList);
return $this->getRecordService()->massConvertCurrency(
$actionParams, $data->targetCurrency, $data->baseCurrency, $data->currencyRates, $fieldList
);
}
public function postActionConvertCurrency($params, $data, $request)
@@ -576,6 +559,8 @@ class Record extends Base
if (empty($data->targetCurrency)) throw new BadRequest();
if (empty($data->baseCurrency)) throw new BadRequest();
return $this->getRecordService()->convertCurrency($data->id, $data->targetCurrency, $data->baseCurrency, $data->currencyRates, $fieldList);
return $this->getRecordService()->convertCurrency(
$data->id, $data->targetCurrency, $data->baseCurrency, $data->currencyRates, $fieldList
);
}
}
+42 -161
View File
@@ -1658,7 +1658,7 @@ class Record extends \Espo\Core\Services\Base
{
if ($this->getAcl()->get('massUpdatePermission') !== 'yes') throw new Forbidden();
$updatedIdList = [];
$resultIdList = [];
$repository = $this->getRepository();
$count = 0;
@@ -1666,79 +1666,35 @@ class Record extends \Espo\Core\Services\Base
$data = $data;
$this->filterInput($data);
if (array_key_exists('ids', $params) && is_array($params['ids'])) {
$ids = $params['ids'];
foreach ($ids as $id) {
$entity = $this->getEntity($id);
if ($this->getAcl()->check($entity, 'edit') && $this->checkEntityForMassUpdate($entity, $data)) {
$entity->set($data);
try {
$this->processValidation($entity, $data);
} catch (\Exception $e) {
continue;
}
if ($this->checkAssignment($entity)) {
if ($repository->save($entity, ['massUpdate' => true])) {
$updatedIdList[] = $entity->id;
$count++;
$selectParams = $this->convertMassActionSelectParams($params);
$this->processActionHistoryRecord('update', $entity);
}
$collection = $this->getRepository()->find($selectParams);
foreach ($collection as $entity) {
if ($this->getAcl()->check($entity, 'edit') && $this->checkEntityForMassUpdate($entity, $data)) {
$entity->set($data);
try {
$this->processValidation($entity, $data);
} catch (\Exception $e) {
continue;
}
if ($this->checkAssignment($entity)) {
if ($repository->save($entity, ['massUpdate' => true, 'skipStreamNotesAcl' => true])) {
$resultIdList[] = $entity->id;
$count++;
$this->processActionHistoryRecord('update', $entity);
}
}
}
}
if (array_key_exists('where', $params)) {
$where = $params['where'];
$p = [];
$p['where'] = $where;
$this->afterMassUpdate($resultIdList, $data);
if (!empty($params['selectData']) && is_array($params['selectData'])) {
foreach ($params['selectData'] as $k => $v) {
$p[$k] = $v;
}
}
$result = ['count' => $count];
if (isset($params['ids'])) $result['ids'] = $resultIdList;
$selectParams = $this->getSelectParams($p);
$this->getEntityManager()->getRepository($this->getEntityType())->handleSelectParams($selectParams);
$sql = $this->getEntityManager()->getQuery()->createSelectQuery($this->getEntityType(), $selectParams);
$sth = $this->getEntityManager()->getPdo()->prepare($sql);
$sth->execute();
while ($dataRow = $sth->fetch(\PDO::FETCH_ASSOC)) {
$entity = $this->getEntityManager()->getEntityFactory()->create($this->getEntityType());
$entity->set($dataRow);
$entity->setAsFetched();
if ($this->getAcl()->check($entity, 'edit') && $this->checkEntityForMassUpdate($entity, $data)) {
$entity->set($data);
if ($this->checkAssignment($entity)) {
if ($repository->save($entity, ['massUpdate' => true, 'skipStreamNotesAcl' => true])) {
$updatedIdList[] = $entity->id;
$count++;
$this->processActionHistoryRecord('update', $entity);
}
}
}
}
$this->afterMassUpdate($updatedIdList, $data);
return (object) [
'count' => $count
];
}
$this->afterMassUpdate($updatedIdList, $data);
return (object) [
'count' => $count,
'ids' => $updatedIdList
];
return $result;
}
protected function checkEntityForMassRemove(Entity $entity)
@@ -1758,74 +1714,32 @@ class Record extends \Espo\Core\Services\Base
public function massDelete(array $params)
{
$removedIdList = [];
$resultIdList = [];
$repository = $this->getRepository();
$count = 0;
if (array_key_exists('ids', $params)) {
$ids = $params['ids'];
foreach ($ids as $id) {
$entity = $this->getEntity($id);
if ($entity && $this->getAcl()->check($entity, 'delete') && $this->checkEntityForMassRemove($entity)) {
if ($repository->remove($entity)) {
$removedIdList[] = $entity->id;
$count++;
$selectParams = $this->convertMassActionSelectParams($params);
$selectParams['skipTextColumns'] = true;
$this->processActionHistoryRecord('delete', $entity);
}
$collection = $this->getRepository()->find($selectParams);
foreach ($collection as $entity) {
if ($this->getAcl()->check($entity, 'delete') && $this->checkEntityForMassRemove($entity)) {
if ($repository->remove($entity)) {
$resultIdList[] = $entity->id;
$count++;
$this->processActionHistoryRecord('delete', $entity);
}
}
}
if (array_key_exists('where', $params)) {
$where = $params['where'];
$p = array();
$p['where'] = $where;
$this->afterMassDelete($resultIdList);
if (!empty($params['selectData']) && is_array($params['selectData'])) {
foreach ($params['selectData'] as $k => $v) {
$p[$k] = $v;
}
}
$result = ['count' => $count];
if (isset($params['ids'])) $result['ids'] = $resultIdList;
$selectParams = $this->getSelectParams($p);
$selectParams['skipTextColumns'] = true;
$this->getEntityManager()->getRepository($this->getEntityType())->handleSelectParams($selectParams);
$sql = $this->getEntityManager()->getQuery()->createSelectQuery($this->getEntityType(), $selectParams);
$sth = $this->getEntityManager()->getPdo()->prepare($sql);
$sth->execute();
while ($dataRow = $sth->fetch(\PDO::FETCH_ASSOC)) {
$entity = $this->getEntityManager()->getEntityFactory()->create($this->getEntityType());
$entity->set($dataRow);
$entity->setAsFetched();
if ($this->getAcl()->check($entity, 'delete') && $this->checkEntityForMassRemove($entity)) {
if ($repository->remove($entity)) {
$removedIdList[] = $entity->id;
$count++;
$this->processActionHistoryRecord('delete', $entity);
}
}
}
$this->afterMassDelete($removedIdList);
return [
'count' => $count
];
}
$this->afterMassDelete($removedIdList);
return [
'count' => $count,
'ids' => $removedIdList
];
return $result;
}
public function massRecalculateFormula(array $params)
@@ -1833,24 +1747,8 @@ class Record extends \Espo\Core\Services\Base
if (!$this->getUser()->isAdmin()) throw new Forbidden();
$count = 0;
if (array_key_exists('ids', $params)) {
if (!is_array($params['ids'])) throw new BadRequest();
$selectParams = $this->getSelectParams([]);
$selectParams['whereClause'][] = [
'id' => $params['ids']
];
} else if (array_key_exists('where', $params)) {
$p = ['where' => $params['where']];
if (!empty($params['selectData']) && is_array($params['selectData'])) {
foreach ($params['selectData'] as $k => $v) {
$p[$k] = $v;
}
}
$selectParams = $this->getSelectParams($p);
} else {
throw new BadRequest();
}
$selectParams = $this->convertMassActionSelectParams($params);
$collection = $this->getRepository()->find($selectParams);
foreach ($collection as $entity) {
@@ -1859,7 +1757,7 @@ class Record extends \Espo\Core\Services\Base
}
return [
'count' => $count
'count' => $count,
];
}
@@ -2644,27 +2542,10 @@ class Record extends \Espo\Core\Services\Base
$count = 0;
$idUpdatedList = [];
$repository = $this->getRepository();
if (array_key_exists('where', $params)) {
$where = $params['where'];
$p = [];
$p['where'] = $where;
if (!empty($params['selectData']) && is_array($params['selectData'])) {
foreach ($params['selectData'] as $k => $v) {
$p[$k] = $v;
}
}
$selectParams = $this->getSelectParams($p);
$selectParams['returnSthCollection'] = true;
} else if (array_key_exists('ids', $params)) {
$selectParams = $this->getSelectParams([]);
$selectParams['whereClause'][] = ['id' => $params['ids']];
} else {
throw new Error();
}
$selectParams = $this->convertMassActionSelectParams($params);
$collection = $repository->find($selectParams);
$collection = $this->getRepository()->find($selectParams);
foreach ($collection as $entity) {
$result = $this->convertEntityCurrency($entity, $targetCurrency, $baseCurrency, $rates, $allFields, $fieldList);
@@ -2676,7 +2557,7 @@ class Record extends \Espo\Core\Services\Base
}
return [
'count' => $count
'count' => $count,
];
}
+13
View File
@@ -589,6 +589,12 @@ define('views/record/list', 'view', function (Dep) {
},
massActionRecalculateFormula: function () {
var ids = false;
var allResultIsChecked = this.allResultIsChecked;
if (!allResultIsChecked) {
ids = this.checkedList;
}
this.confirm({
message: this.translate('recalculateFormulaConfirmation', 'messages'),
confirmText: this.translate('Yes')
@@ -599,6 +605,13 @@ define('views/record/list', 'view', function (Dep) {
result = result || {};
this.collection.fetch().then(function () {
Espo.Ui.success(this.translate('Done'));
if (allResultIsChecked) {
this.selectAllResult();
} else {
ids.forEach(function (id) {
this.checkRecord(id);
}, this);
}
}.bind(this));
}.bind(this));
}.bind(this));