diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 1059b8dd05..7200c3d50e 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -29,6 +29,26 @@ abstract class Record extends Base return $entity; } + + public function actionPatch($params, $data) + { + return $this->actionUpdate($params, $data); + } + + public function actionCreate($params, $data) + { + if (!$this->getAcl()->check($this->name, 'edit')) { + throw new Forbidden(); + } + + $service = $this->getService(); + + if ($entity = $service->createEntity($data)) { + return $entity; + } + + throw new Error(); + } public function actionUpdate($params, $data) { @@ -39,7 +59,7 @@ abstract class Record extends Base if (!$this->getAcl()->check($entity, 'edit')) { throw new Forbidden(); } - + if ($service->updateEntity($entity, $data)) { return $entity; } @@ -47,21 +67,6 @@ abstract class Record extends Base throw new Error(); } - public function actionPost($params, $data) - { - if (!$this->getAcl()->check($this->name, 'edit')) { - throw new Forbidden(); - } - - $service = $this->getService(); - - if ($entity = $service->postEntity($data)) { - return $entity; - } - - throw new Error(); - } - public function actionList($params, $where) { if (!$this->getAcl()->check($this->name, 'read')) { @@ -186,4 +191,25 @@ abstract class Record extends Base throw new Error(); } + + public function actionRemoveLink($params) + { + $id = $params['id']; + $link = $params['link']; + $foreignId = $params['foreignId']; + + $service = $this->getService(); + $entity = $service->getEntity($id); + $foreignEntityName = $entity->defs['links'][$link]['entity']; + + if (!$this->getAcl()->check($entity, 'edit')) { + throw new Forbidden(); + } + + if ($service->unlinkEntity($entity, $link, $foreignId)) { + return true; + } + + throw new Error(); + } } diff --git a/application/Espo/Core/EntityManager.php b/application/Espo/Core/EntityManager.php index 580702730a..256a69a17e 100644 --- a/application/Espo/Core/EntityManager.php +++ b/application/Espo/Core/EntityManager.php @@ -32,4 +32,17 @@ class EntityManager extends \Doctrine\ORM\Decorator\EntityManagerDecorator { return $this->wrapped; } + + public function createEntity($entityName) + { + $className = $this->metadata->getEntityPath($entityName); + $entity = new $className(); + return $entity; + } + + public function getEntityName($entity) + { + $className = get_class($entity); + return ltrim($className, '\\'); + } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Account.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Account.json index e1dd2a56ed..672e2da473 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Account.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Account.json @@ -1,8 +1,4 @@ { - "bottomPanels":{ - "detail":[ - ] - }, "sidePanels":{ "detail":[ { @@ -24,17 +20,5 @@ ], "layout":"listSmall" } - }, - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Account", - "link":"#Account/create", - "style":"primary", - "acl":"edit" - } - ] - } } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Call.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Call.json index 2caa59417c..d227a6e565 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Call.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Call.json @@ -30,17 +30,5 @@ } } ] - }, - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Call", - "link":"#Call/create", - "style":"primary", - "acl":"edit" - } - ] - } } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Case.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Case.json index 16974950c3..75450d6d96 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Case.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Case.json @@ -16,17 +16,5 @@ "view":"Crm:Record.Panels.History" } ] - }, - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Case", - "link":"#Case/create", - "style":"primary", - "acl":"edit" - } - ] - } } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Contact.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Contact.json index 037092e105..8f4f11e86b 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Contact.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Contact.json @@ -1,8 +1,4 @@ { - "bottomPanels":{ - "detail":[ - ] - }, "sidePanels":{ "detail":[ { @@ -16,17 +12,5 @@ "view":"Crm:Record.Panels.History" } ] - }, - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Contact", - "link":"#Contact/create", - "style":"primary", - "acl":"edit" - } - ] - } } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/InboundEmail.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/InboundEmail.json index 61414cf120..0967ef424b 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/InboundEmail.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/InboundEmail.json @@ -1,14 +1 @@ -{ - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Inbound Email", - "link":"#InboundEmail/create", - "style":"primary", - "acl":"edit" - } - ] - } - } -} +{} diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json index ead45ce071..1c02ab9b4b 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json @@ -5,10 +5,6 @@ "recordViewMaps":{ "detail":"Crm:Lead.Record.Detail" }, - "bottomPanels":{ - "detail":[ - ] - }, "sidePanels":{ "detail":[ { @@ -22,17 +18,5 @@ "view":"Crm:Record.Panels.History" } ] - }, - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Lead", - "link":"#Lead/create", - "style":"primary", - "acl":"edit" - } - ] - } } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json index 294102f05d..42d3a50ca1 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json @@ -33,17 +33,5 @@ } } ] - }, - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Meeting", - "link":"#Meeting/create", - "style":"primary", - "acl":"edit" - } - ] - } } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Opportunity.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Opportunity.json index 7ea8be1bdf..8f4f11e86b 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Opportunity.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Opportunity.json @@ -1,8 +1,4 @@ { - "bottomPanels":{ - "detail":[ - ] - }, "sidePanels":{ "detail":[ { @@ -16,17 +12,5 @@ "view":"Crm:Record.Panels.History" } ] - }, - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Opportunity", - "link":"#Opportunity/create", - "style":"primary", - "acl":"edit" - } - ] - } } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json index 7a641f4fa0..78f763c675 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json @@ -1,18 +1,5 @@ { - "viewMaps":{ - "detail":"Crm:Prospect.Detail" - }, "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Prospect", - "link":"#Prospect/create", - "style":"primary", - "acl":"edit" - } - ] - }, "detail":{ "buttons":[ { diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Task.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Task.json index d5dd9dc84b..2c63c08510 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Task.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Task.json @@ -1,14 +1,2 @@ { - "menu":{ - "list":{ - "buttons":[ - { - "label":"Create Task", - "link":"#Task/create", - "style":"primary", - "acl":"edit" - } - ] - } - } } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index c1163dbb10..865fd31ca2 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -2,6 +2,7 @@ namespace Espo\Services; +use \Espo\Core\Exceptions\Error; class Record extends \Espo\Core\Services\Base { @@ -9,45 +10,116 @@ class Record extends \Espo\Core\Services\Base 'entityManager', 'user', ); - + private $user; - + private $entityManager; - + protected $entityName; - + public function setEntityName($entityName) { $this->entityName = $entityName; - } - + } + public function setEntityManager($entityManager) { $this->entityManager = $entityManager; } - + public function setUser($user) { $this->user = $user; - } - - public function getEntityManager() + } + + protected function getEntityManager() { return $this->entityManager; } - - public function getUser() + + protected function getUser() { return $this->user; - } - + } + public function getEntity($id) { - return $this->getEntityManager()->find($this->name, $id); + return $this->getEntityManager()->getRepository($this->name)->find($id); } - - public function findEntities($where) + + public function findEntities($params) { - return $this->getEntityManager()->getRepository($this->name)->findBy($where); - } + $collection = $this->getEntityManager()->getRepository($this->name)->findBy(); + $criteria = $this->getCriteriaManager()->createCriteria($params); + return $collection->matching($criteria); + } + + public function createEntity($data) + { + // TODO validate $data + $entity = $this->getEntityManager()->createEntity($this->name); + $entity->fromArray($data); + $this->getEntityManager()->persist($entity); + $this->getEntityManager()->flush(); + return $entity; + } + + public function updateEntity($entity, $data) + { + // TODO validate $data + $entity->fromArray($data); + $this->getEntityManager()->persist($entity); + $this->getEntityManager()->flush(); + return $entity; + } + + public function deleteEntity($entity) + { + $this->getEntityManager()->remove($entity); + $this->getEntityManager()->flush(); + return true; + } + + public function findLinkedEntities($entity, $link, $params) + { + $criteria = $this->getCriteriaManager()->createCriteria($params); + $methodName = 'get' . ucfirst($link); + $collection = $entity->$methodName(); + return $collection->matching($criteria); + } + + public function linkEntity($entity, $link, $foreignId) + { + $entityName = $this->getEntityManager()->getEntityName($entity); + $foreignEntityName = $this->getMetadata()->get('entityDefs.' . $entityName . '.links.' . $link . '.entity'); + + if (empty($foreignEntityName)) { + throw new Error(); + } + + $methodName = 'get' . ucfirst($link); + $foreignEntity = $this->getEntityManager()->getRepository($foreignEntityName)->find($foreignId); + + if (!empty($foreignEntity)) { + + $entity->$methodName()->add($foreignEntity); + return true; + } + } + + public function unlinkEntity($entity, $link, $foreignId) + { + $entityName = $this->getEntityManager()->getEntityName($entity); + $foreignEntityName = $this->getMetadata()->get('entityDefs.' . $entityName . '.links.' . $link . '.entity'); + + if (empty($foreignEntityName)) { + throw new Error(); + } + + $methodName = 'get' . ucfirst($link); + $entity->$methodName()->remove($foreignId); + return true; + } + } +