diff --git a/application/Espo/Core/ControllerManager.php b/application/Espo/Core/ControllerManager.php index 75de8709c6..b14fb006b4 100644 --- a/application/Espo/Core/ControllerManager.php +++ b/application/Espo/Core/ControllerManager.php @@ -55,9 +55,9 @@ class ControllerManager if (!class_exists($controllerClassName)) { throw new NotFound("Controller '$controllerName' is not found"); } - + $controller = new $controllerClassName($this->container, $this->serviceFactory); - + if ($actionName == 'index') { $actionName = $controller->defaultAction; } diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 6f0aa77d30..04f4ee322d 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -82,7 +82,7 @@ abstract class Record extends Base throw new Error(); } - public function actionList($params, $where, $request) + public function actionList($params, $data, $request) { if (!$this->getAcl()->check($this->name, 'read')) { throw new Forbidden(); @@ -142,17 +142,52 @@ abstract class Record extends Base } throw new Error(); } + + public function actionExport($params, $data, $request) + { + if (!$this->getAcl()->check($this->name, 'read')) { + throw new Forbidden(); + } + + $ids = $request->get('ids'); + $where = $request->get('where'); + + + if (!empty($ids)) { + $where = array( + array( + 'type' => 'in', + 'field' => 'id', + 'value' => $ids + ) + ); + } + + $result = $this->getRecordService()->findEntities(array('where' => $where)); + $arr = $result['collection']->toArray(); + + header('Content-Type: text/csv'); + header('Content-Disposition: filename=' . $this->name . '.csv'); + $fp = fopen('php://output', 'w'); + fputcsv($fp, array_keys($arr[0])); + foreach ($arr as $row) { + fputcsv($fp, $row); + } + fclose($fp); + die; + } public function actionMassUpdate($params, $data) { if (!$this->getAcl()->check($this->name, 'edit')) { throw new Forbidden(); - } + } $ids = $data['ids']; $where = $data['where']; + $attributes = $data['attributes']; - $idsUpdated = $this->getRecordService()->massUpdate($ids, $where); + $idsUpdated = $this->getRecordService()->massUpdate($attributes, $ids, $where); return $idsUpdated; } diff --git a/application/Espo/ORM/EntityCollection.php b/application/Espo/ORM/EntityCollection.php index b003f7518f..3545d24c1b 100644 --- a/application/Espo/ORM/EntityCollection.php +++ b/application/Espo/ORM/EntityCollection.php @@ -12,7 +12,7 @@ class EntityCollection implements \Iterator, \Countable, \ArrayAccess, \Seekable protected $container = array(); - public function __construct($data, $seed = null, EntityFactory $entityFactory = null) + public function __construct($data = array(), $seed = null, EntityFactory $entityFactory = null) { $this->container = $data; diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index c0de700342..71205a9520 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -88,5 +88,12 @@ class EntityManager { return $name; } + + public function createCollection($entityName, $data = array()) + { + $seed = $this->getEntity($entityName); + $collection = new EntityCollection($data, $seed, $this->entityFactory); + return $collection; + } } diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json index 933ad48cef..0aa460e770 100644 --- a/application/Espo/Resources/routes.json +++ b/application/Espo/Resources/routes.json @@ -40,6 +40,33 @@ "controller":"Settings" } }, + + { + "route":"/:controller/action/:action", + "method":"post", + "params":{ + "controller":":controller", + "action":":action" + } + }, + + { + "route":"/:controller/action/:action", + "method":"put", + "params":{ + "controller":":controller", + "action":":action" + } + }, + + { + "route":"/:controller/action/:action", + "method":"get", + "params":{ + "controller":":controller", + "action":":action" + } + }, { "route":"/:controller/layout/:name", diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index e68a2def97..02723131c2 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -11,7 +11,8 @@ class Record extends \Espo\Core\Services\Base 'entityManager', 'user', 'metadata', - 'acl' + 'acl', + 'config' ); protected $entityName; @@ -36,6 +37,11 @@ class Record extends \Espo\Core\Services\Base return $this->injections['acl']; } + protected function getConfig() + { + return $this->injections['config']; + } + protected function getMetadata() { return $this->injections['metadata']; @@ -240,6 +246,28 @@ class Record extends \Espo\Core\Services\Base return true; } } + + public function massUpdate($attributes = array(), $ids = array(), $where = array()) + { + $idsUpdated = array(); + $repository = $this->getRepository(); + + if (!empty($ids)) { + foreach ($ids as $id) { + $entity = $this->getEntity($id); + if ($this->getAcl()->check($entity, 'edit')) { + $entity->set($attributes); + if ($repository->save($entity)) { + $idsUpdated[] = $id; + } + } + } + } + + return $idsUpdated; + + // TODO update $where + } }