diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index a44892c7de..e8e088ee33 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -231,27 +231,39 @@ class Record extends Base public function actionCreateLink($params, $data) { + if (empty($params['id']) || empty($params['link'])) { + throw BadRequest(); + } + $id = $params['id']; $link = $params['link']; - $foreignIds = array(); - if (isset($data['id'])) { - $foreignIds[] = $data['id']; - } - if (isset($data['ids']) && is_array($data['ids'])) { - foreach ($data['ids'] as $foreignId) { - $foreignIds[] = $foreignId; + if (!empty($data['massRelate'])) { + if (empty($data['where'])) { + throw new BadRequest(); + } + $where = json_decode(json_encode($data['where']), true); + return $this->getRecordService()->linkEntityMass($id, $link, $where); + } else { + $foreignIds = array(); + if (isset($data['id'])) { + $foreignIds[] = $data['id']; + } + if (isset($data['ids']) && is_array($data['ids'])) { + foreach ($data['ids'] as $foreignId) { + $foreignIds[] = $foreignId; + } } - } - $result = false; - foreach ($foreignIds as $foreignId) { - if ($this->getRecordService()->linkEntity($id, $link, $foreignId)) { - $result = $result || true; + $result = false; + foreach ($foreignIds as $foreignId) { + if ($this->getRecordService()->linkEntity($id, $link, $foreignId)) { + $result = true; + } + } + if ($result) { + return true; } - } - if ($result) { - return true; } throw new Error(); @@ -262,6 +274,10 @@ class Record extends Base $id = $params['id']; $link = $params['link']; + if (empty($params['id']) || empty($params['link'])) { + throw BadRequest(); + } + $foreignIds = array(); if (isset($data['id'])) { $foreignIds[] = $data['id']; diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index 18939f973c..fdf1562586 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -427,7 +427,9 @@ abstract class Mapper implements IMapper $sql = "INSERT INTO `".$relTable."` (".$fieldsPart.") (".$subSql.") ON DUPLICATE KEY UPDATE deleted = '0'"; - return $this->pdo->query($sql); + if ($this->pdo->query($sql)) { + return true; + } break; } diff --git a/application/Espo/ORM/Repositories/RDB.php b/application/Espo/ORM/Repositories/RDB.php index 10d549f3c9..fbc186729b 100644 --- a/application/Espo/ORM/Repositories/RDB.php +++ b/application/Espo/ORM/Repositories/RDB.php @@ -274,6 +274,10 @@ class RDB extends \Espo\ORM\Repository return false; } + public function massRelate(Entity $entity, $relationName, array $params = array()) + { + return $this->getMapper()->massRelate($entity, $relationName, $params); + } public function getAll() { diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 65f5aed428..ef51e72cce 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -598,6 +598,34 @@ class Record extends \Espo\Core\Services\Base } } + public function linkEntityMass($id, $link, $where) + { + $entity = $this->getEntity($id); + + $entityType = $entity->getEntityType(); + $foreignEntityType = $entity->relations[$link]['entity']; + + if (!$this->getAcl()->check($entity, 'edit')) { + throw new Forbidden(); + } + if (empty($foreignEntityType)) { + throw new Error(); + } + if (!$this->getAcl()->check($foreignEntityType, 'edit')) { + throw new Forbidden(); + } + + if (!is_array($where)) { + $where = array(); + } + $params['where'] = $where; + + + $selectParams = $this->getRecordService($foreignEntityType)->getSelectParams($params); + + return $this->getRepository()->massRelate($entity, $link, $selectParams); + } + public function massUpdate($attributes = array(), $ids = array(), $where = array()) { $idsUpdated = array();