From 7206e6978cdbeae5ec2062a57849125286c0f91c Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 15 Jan 2014 18:06:46 +0200 Subject: [PATCH] dev --- application/Espo/Controllers/Stream.php | 1 + application/Espo/Core/Acl.php | 16 ++++--- application/Espo/Core/Application.php | 3 +- application/Espo/Core/Container.php | 4 +- application/Espo/Core/ControllerManager.php | 3 +- application/Espo/Core/Controllers/Record.php | 38 +++++++++++++--- application/Espo/ORM/DB/Mapper.php | 2 +- application/Espo/Resources/routes.json | 10 +++++ application/Espo/Services/Record.php | 46 ++++++++++++-------- application/Espo/Services/Stream.php | 37 +++++++++++++++- 10 files changed, 125 insertions(+), 35 deletions(-) diff --git a/application/Espo/Controllers/Stream.php b/application/Espo/Controllers/Stream.php index bd3b0bdcef..c1900be590 100644 --- a/application/Espo/Controllers/Stream.php +++ b/application/Espo/Controllers/Stream.php @@ -29,3 +29,4 @@ class Stream extends \Espo\Core\Controllers\Base ); } } + diff --git a/application/Espo/Core/Acl.php b/application/Espo/Core/Acl.php index f887d20c6d..751deb0623 100644 --- a/application/Espo/Core/Acl.php +++ b/application/Espo/Core/Acl.php @@ -11,20 +11,26 @@ class Acl private $actionList = array('read', 'edit', 'delete'); private $levelList = array('all', 'team', 'own', 'no'); + + private $fileManager; - public function __construct(\Espo\Entities\User $user) + public function __construct(\Espo\Entities\User $user, $config, $fileManager) { $this->user = $user; + $this->fileManager = $fileManager; + $this->user->loadLinkMultipleField('teams'); - + $this->cacheFile = 'data/cache/application/acl/' . $user->id; - if (file_exists($this->cacheFile)) { + if ($config->get('useCache') && file_exists($this->cacheFile)) { $cached = include $this->cacheFile; } else { $this->load(); $this->initSolid(); - $this->buildCache(); + if ($config->get('useCache')) { + $this->buildCache(); + } } } @@ -211,7 +217,7 @@ class Acl private function buildCache() { $contents = '<' . '?'. 'php return ' . var_export($this->data, true) . ';'; - file_put_contents($this->cacheFile, $contents); + $this->fileManager()->setContent($this->cacheFile, $contents); } } diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 73526345b7..4c190c9899 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -100,8 +100,7 @@ class Application } $params = $route->getParams(); - $data = $slim->request()->getBody(); - + $data = $slim->request()->getBody(); foreach ($routeOptions as $key => $value) { if (strstr($value, ':')) { diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index c7891624e0..01c2c8e9d9 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -140,7 +140,9 @@ class Container private function loadAcl() { return new \Espo\Core\Acl( - $this->get('user') + $this->get('user'), + $this->get('config'), + $this->get('fileManager') ); } diff --git a/application/Espo/Core/ControllerManager.php b/application/Espo/Core/ControllerManager.php index 77272d2933..b25e3664dd 100644 --- a/application/Espo/Core/ControllerManager.php +++ b/application/Espo/Core/ControllerManager.php @@ -46,8 +46,7 @@ class ControllerManager } else { $controllerClassName = '\\Espo\\Controllers\\' . Util::normilizeClassName($controllerName); } - } - + } if ($data) { $data = json_decode($data, true); diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 489de0f749..5220ea7c8b 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -12,7 +12,12 @@ abstract class Record extends Base public static $defaultAction = 'list'; - public function getRecordService() + protected function getEntityManager() + { + return $this->getContainer()->get('entityManager'); + } + + protected function getRecordService() { $moduleName = $this->getMetadata()->getScopeModuleName($this->name); if ($moduleName) { @@ -145,6 +150,8 @@ abstract class Record extends Base public function actionExport($params, $data, $request) { + // TODO move to service + if (!$this->getAcl()->check($this->name, 'read')) { throw new Forbidden(); } @@ -166,15 +173,34 @@ abstract class Record extends Base $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'); + + $fp = fopen('php://temp', 'w'); fputcsv($fp, array_keys($arr[0])); foreach ($arr as $row) { fputcsv($fp, $row); } - fclose($fp); - die; + rewind($fp); + $csv = stream_get_contents($fp); + fclose($fp); + + $fileName = "Export_{$this->name}.csv"; + + $attachment = $this->getEntityManager()->getEntity('Attachment'); + $attachment->set('name', $fileName); + $attachment->set('extension', 'csv'); + $attachment->set('type', 'text/csv'); + + $this->getEntityManager()->saveEntity($attachment); + + if (!empty($attachment->id)) { + $this->getContainer()->get('fileManager')->setContent($csv, 'data/upload/' . $attachment->id); + + // TODO cron job to remove file + + return $attachment->id; + } + + throw new Error(); } public function actionMassUpdate($params, $data) diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index 2506d5daa5..5139b984c7 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -171,7 +171,7 @@ abstract class Mapper implements IMapper } if (!empty($customJoin)) { - $joinsPart .= ' ' . $customJoin; + $joinsPart .= ' ' . $customJoin . ' '; } if (!empty($joins) && is_array($joins)) { diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json index e5bd2b8251..69778cd263 100644 --- a/application/Espo/Resources/routes.json +++ b/application/Espo/Resources/routes.json @@ -41,6 +41,16 @@ } }, + { + "route":"/Stream", + "method":"get", + "params":{ + "controller":"Stream", + "action":"list", + "scope": "User" + } + }, + { "route":"/:controller/action/:action", "method":"post", diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index c81a8a865e..196e077bdf 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -2,6 +2,7 @@ namespace Espo\Services; +use \Espo\ORM\Entity; use \Espo\Core\Exceptions\Error; use \Espo\Core\Exceptions\Forbidden; use \Espo\Core\Utils\Util; @@ -71,15 +72,34 @@ class Record extends \Espo\Core\Services\Base return $entity; } - protected function loadIsFollowed(Entity $entity) + protected function checkIsFollowed(Entity $entity, $userId = null) { + if (empty($userId)) { + $userId = $this->getUser()->id; + } + $pdo = $this->getEntityManager()->getPDO(); $sql = " SELECT id FROM subscription - WHERE entity_id = " . $pdo->quote($entity->get('id')) . " AND " . $entity->getEntityName(); - - if ($pdo->prepare($sql)->execute()) { + WHERE + entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . " AND + user_id = " . $pdo->quote($userId) . " + "; + + $sth = $pdo->prepare($sql); + $sth->execute(); + if ($sth->fetchAll()) { + return true; + } + return false; + } + + protected function loadIsFollowed(Entity $entity) + { + if ($this->checkIsFollowed($entity)) { $entity->set('isFollowed', true); + } else { + $entity->set('isFollowed', false); } } @@ -300,22 +320,13 @@ class Record extends \Espo\Core\Services\Base } $pdo = $this->getEntityManager()->getPDO(); - $sql = " - SELECT id FROM subscription - WHERE - entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . " AND - user_id = " . $pdo->quote($userId) . " - "; - - $sth = $pdo->prepare($sql); - $sth->execute(); - if (!$sth->fetchAll()) { + if (!$this->checkIsFollowed($entity, $userId)) { $sql = " INSERT INTO subscription (entity_id, entity_type, user_id) VALUES - (".$pdo->quote($entity->id).", ".$pdo->quote($entity->getEntityName()).", ".$pdo->quote($userId).") + (".$pdo->quote($entity->id) . ", " . $pdo->quote($entity->getEntityName()) . ", " . $pdo->quote($userId).") "; $sth = $pdo->prepare($sql)->execute(); } @@ -335,11 +346,12 @@ class Record extends \Espo\Core\Services\Base if (empty($userId)) { $userId = $this->getUser()->id; - } + } + + $pdo = $this->getEntityManager()->getPDO(); $sql = " DELETE FROM subscription - (entity_id, entity_type, user_id) WHERE entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . " AND user_id = " . $pdo->quote($userId) . " diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index 35e21b61a0..612bff5886 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -34,8 +34,43 @@ class Stream extends \Espo\Core\Services\Base return $this->injections['metadata']; } - public function find($scope, $id, $params) + public function findUserStream($params = array()) { + $selectParams = array( + 'offset' => $params['offset'], + 'limit' => $params['maxSize'], + 'orderBy' => 'createdAt', + 'order' => 'DESC', + 'customJoin' => " + JOIN subscription ON + note.parent_type = subscription.entity_type AND + note.parent_id = subscription.entity_id AND + subscription.user_id = '" . $this->getUser()->id . "' + " + ); + + $collection = $this->getEntityManager()->getRepository('Note')->find($selectParams); + + foreach ($collection as $e) { + if ($e->get('type') == 'Post' && $e->get('parentId') && $e->get('parentType')) { + $entity = $this->getEntityManager()->getEntity($e->get('parentType'), $e->get('parentId')); + $e->set('parentName', $entity->get('name')); + } + } + + $count = $this->getEntityManager()->getRepository('Note')->count($selectParams); + + return array( + 'total' => $count, + 'collection' => $collection, + ); + } + + public function find($scope, $id, $params = array()) + { + if ($scope == 'User') { + return $this->findUserStream($params); + } $entity = $this->getEntityManager()->getEntity($scope, $id); if (empty($entity)) {