From 596e59d667feae2ddbb997cb32b2c560cab6f178 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 17 Jan 2014 17:49:58 +0200 Subject: [PATCH] stream --- application/Espo/Core/Application.php | 128 +------------------- application/Espo/Core/Container.php | 7 ++ application/Espo/Core/ControllerManager.php | 7 +- application/Espo/Core/Controllers/Base.php | 7 +- application/Espo/Core/HookManager.php | 20 +-- application/Espo/Core/Hooks/Base.php | 6 +- application/Espo/Core/ORM/Repository.php | 10 ++ application/Espo/Core/ServiceFactory.php | 2 +- application/Espo/Hooks/Common/Stream.php | 66 ++++++++++ application/Espo/ORM/Entity.php | 3 +- application/Espo/Services/Record.php | 2 +- application/Espo/Services/Stream.php | 51 +++++++- 12 files changed, 158 insertions(+), 151 deletions(-) create mode 100644 application/Espo/Hooks/Common/Stream.php diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 4c190c9899..4691e48c9c 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -8,8 +8,6 @@ class Application private $metadata; private $container; - - private $serviceFactory; private $slim; @@ -27,7 +25,6 @@ class Application date_default_timezone_set('UTC'); - $this->serviceFactory = new ServiceFactory($this->container); $this->slim = $this->container->get('slim'); $this->metadata = $this->container->get('metadata'); @@ -54,11 +51,6 @@ class Application return $this->log; } - public function getServiceFactory() - { - return $this->serviceFactory; - } - public function run($name = 'default') { $this->routeHooks(); @@ -78,12 +70,11 @@ class Application { $container = $this->getContainer(); $slim = $this->getSlim(); - $serviceFactory = $this->getServiceFactory(); $auth = new \Espo\Core\Utils\Api\Auth($container->get('entityManager'), $container); $this->getSlim()->add($auth); - $this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container, $serviceFactory) { + $this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container) { $route = $slim->router()->getCurrentRoute(); $conditions = $route->getConditions(); @@ -122,7 +113,7 @@ class Application } try { - $controllerManager = new \Espo\Core\ControllerManager($container, $serviceFactory); + $controllerManager = new \Espo\Core\ControllerManager($container); $result = $controllerManager->process($controllerName, $actionName, $params, $data, $slim->request()); $container->get('output')->render($result); } catch (\Exception $e) { @@ -140,10 +131,9 @@ class Application protected function initRoutes() { $routes = new \Espo\Core\Utils\Route($this->getContainer()->get('config'), $this->getContainer()->get('fileManager')); - $crudList = array_keys( (array) $this->getContainer()->get('config')->get('crud') ); - foreach($routes->getAll() as $route) { + foreach ($routes->getAll() as $route) { $method = strtolower($route['method']); if (!in_array($method, $crudList)) { @@ -159,117 +149,7 @@ class Application $currentRoute->conditions($route['conditions']); } } - - /* //todo remove when it is tested - $this->getSlim()->get('/', function() { - return $template = "

EspoCRM REST API

"; - }); - - $this->getSlim()->get('/App/user/', function() { - return '{"user":{"modified_by_name":"Administrator","created_by_name":"","id":"1","user_name":"admin","user_hash":"","system_generated_password":"0","pwd_last_changed":"","authenticate_id":"","sugar_login":"1","first_name":"","last_name":"Administrator","full_name":"Administrator","name":"Administrator","is_admin":"1","external_auth_only":"0","receive_notifications":"1","description":"","date_entered":"2013-06-13 12:18:44","date_modified":"2013-06-13 12:19:48","modified_user_id":"1","created_by":"","title":"Administrator","department":"","phone_home":"","phone_mobile":"","phone_work":"","phone_other":"","phone_fax":"","status":"Active","address_street":"","address_city":"","address_state":"","address_country":"","address_postalcode":"","UserType":"","deleted":"0","portal_only":"0","show_on_employees":"1","employee_status":"Active","messenger_id":"","messenger_type":"","reports_to_id":"","reports_to_name":"","email1":"test@letrium.com","email_link_type":"","is_group":"0","c_accept_status_fields":" ","m_accept_status_fields":" ","accept_status_id":"","accept_status_name":""},"preferences":{}}'; - }); - - - $this->getSlim()->get('/Metadata/', function() { - return array( - 'controller' => 'Metadata', - ); - }); - - $this->getSlim()->get('/Settings/', function() { - return array( - 'controller' => 'Settings', - ); - })->conditions( array('auth' => false) ); - $this->getSlim()->map('/Settings/', function() { - return array( - 'controller' => 'Settings', - ); - })->via('PATCH'); - - $this->getSlim()->get('/:controller/layout/:name/', function() { - return array( - 'controller' => 'Layout', - 'scope' => ':controller', - ); - }); - $this->getSlim()->put('/:controller/layout/:name/', function() { - return array( - 'controller' => 'Layout', - 'scope' => ':controller', - ); - }); - $this->getSlim()->map('/:controller/layout/:name/', function() { - return array( - 'controller' => 'Layout', - 'scope' => ':controller', - ); - })->via('PATCH'); - - $this->getSlim()->get('/Admin/rebuild/', function() { - return array( - 'controller' => 'Admin', - 'action' => 'rebuild', - ); - }); - - $this->getSlim()->get('/:controller/:id', function() { - return array( - 'controller' => ':controller', - 'action' => 'read', - 'id' => ':id' - ); - }); - - $this->getSlim()->get('/:controller', function() { - return array( - 'controller' => ':controller', - 'action' => 'index', - ); - }); - - $this->getSlim()->post('/:controller', function() { - return array( - 'controller' => ':controller', - 'action' => 'create' - ); - }); - - $this->getSlim()->put('/:controller/:id', function() { - return array( - 'controller' => ':controller', - 'action' => 'update', - 'id' => ':id' - ); - }); - - $this->getSlim()->map('/:controller/:id', function() { - return array( - 'controller' => ':controller', - 'action' => 'patch', - 'id' => ':id' - ); - })->via('PATCH'); - - $this->getSlim()->get('/:controller/:id/:link', function() { - return array( - 'controller' => ':controller', - 'action' => 'listLinked', - 'id' => ':id', - 'link' => ':link', - ); - }); - - $this->getSlim()->get('/:controller/:id/:link/:foreignId', function() { - return array( - 'controller' => ':controller', - 'action' => 'readRelated', - 'id' => ':id', - 'link' => ':link', - 'foreignId' => ':foreignId' - ); - }); */ - } } + diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 01c2c8e9d9..a4636d0f82 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -99,6 +99,13 @@ class Container $this->get('slim') ); } + + private function loadServiceFactory() + { + return new \Espo\Core\ServiceFactory( + $this + ); + } private function loadMetadata() { diff --git a/application/Espo/Core/ControllerManager.php b/application/Espo/Core/ControllerManager.php index b25e3664dd..91aa8c092e 100644 --- a/application/Espo/Core/ControllerManager.php +++ b/application/Espo/Core/ControllerManager.php @@ -12,16 +12,13 @@ class ControllerManager private $metadata; private $container; - - private $serviceFactory; - public function __construct(\Espo\Core\Container $container, \Espo\Core\ServiceFactory $serviceFactory) + public function __construct(\Espo\Core\Container $container) { $this->container = $container; $this->config = $this->container->get('config'); $this->metadata = $this->container->get('metadata'); - $this->serviceFactory = $serviceFactory; } protected function getConfig() @@ -56,7 +53,7 @@ class ControllerManager throw new NotFound("Controller '$controllerName' is not found"); } - $controller = new $controllerClassName($this->container, $this->serviceFactory); + $controller = new $controllerClassName($this->container); if ($actionName == 'index') { $actionName = $controllerClassName::$defaultAction; diff --git a/application/Espo/Core/Controllers/Base.php b/application/Espo/Core/Controllers/Base.php index 40dbec1e2f..23a0e9a61a 100644 --- a/application/Espo/Core/Controllers/Base.php +++ b/application/Espo/Core/Controllers/Base.php @@ -12,14 +12,11 @@ abstract class Base private $container; - private $serviceFactory; - public static $defaultAction = 'index'; - public function __construct(Container $container, ServiceFactory $serviceFactory) + public function __construct(Container $container) { $this->container = $container; - $this->serviceFactory = $serviceFactory; if (empty($this->name)) { $name = get_class($this); @@ -64,7 +61,7 @@ abstract class Base protected function getServiceFactory() { - return $this->serviceFactory; + return $this->container->get('serviceFactory'); } protected function getService($className) diff --git a/application/Espo/Core/HookManager.php b/application/Espo/Core/HookManager.php index f1e98957eb..917a0a77a4 100644 --- a/application/Espo/Core/HookManager.php +++ b/application/Espo/Core/HookManager.php @@ -23,6 +23,8 @@ class HookManager protected $hookList = array( 'beforeSave', 'afterSave', + 'beforeRemove', + 'afterRemove', ); public function __construct(Container $container) @@ -52,9 +54,8 @@ class HookManager $this->data = $this->getHookData( array('Espo/Hooks', 'Espo/Custom/Hooks') ); foreach ($metadata->getModuleList() as $moduleName) { - $this->data = array_merge($this->data, $this->getHookData( array('Espo/Modules/'.$moduleName.'/Hooks', 'Espo/Custom/Modules/'.$moduleName.'/Hooks') )); - } - + $this->data = array_merge($this->data, $this->getHookData(array('Espo/Modules/'.$moduleName.'/Hooks', 'Espo/Custom/Modules/'.$moduleName.'/Hooks'))); + } if ($this->getConfig()->get('useCache')) { $this->getFileManager()->setContentPHP($this->data, $this->cacheFile); @@ -73,7 +74,10 @@ class HookManager $hook->$hookName($injection); } } - } + } + if ($scope != 'Common') { + $this->process('Common', $hookName, $injection); + } } public function createHookByClassName($className) @@ -81,7 +85,7 @@ class HookManager if (class_exists($className)) { $hook = new $className(); $dependencies = $hook->getDependencyList(); - foreach ($dependencies as $name) { + foreach ($dependencies as $name) { $hook->inject($name, $this->container->get($name)); } return $hook; @@ -100,13 +104,13 @@ class HookManager { $hooks = array(); - foreach($hookDirs as $hookDir) { + foreach ($hookDirs as $hookDir) { $fullHookDir = 'application/'.$hookDir; if (file_exists($fullHookDir)) { $fileList = $this->getFileManager()->getFileList($fullHookDir, 1, '\.php$', 'file'); - foreach($fileList as $scopeName => $hookFiles) { + foreach ($fileList as $scopeName => $hookFiles) { $hookScopeDirPath = Util::concatPath($hookDir, $scopeName); @@ -123,7 +127,7 @@ class HookManager } //sort hooks by order - foreach($scopeHooks as $hookName => $hookList) { + foreach ($scopeHooks as $hookName => $hookList) { ksort($hookList); $sortedHookList = array(); diff --git a/application/Espo/Core/Hooks/Base.php b/application/Espo/Core/Hooks/Base.php index a300922f85..59ca808eab 100644 --- a/application/Espo/Core/Hooks/Base.php +++ b/application/Espo/Core/Hooks/Base.php @@ -6,9 +6,7 @@ use \Espo\Core\Interfaces\Injectable; class Base implements Injectable { - protected $dependencies = array(); - - protected $injections = array( + protected $dependencies = array( 'entityManager', 'config', 'metadata', @@ -16,6 +14,8 @@ class Base implements Injectable 'user', ); + protected $injections = array(); + public static $order = 9; public function __construct() diff --git a/application/Espo/Core/ORM/Repository.php b/application/Espo/Core/ORM/Repository.php index 185ee85586..3b4fd80c30 100644 --- a/application/Espo/Core/ORM/Repository.php +++ b/application/Espo/Core/ORM/Repository.php @@ -35,6 +35,16 @@ class Repository extends \Espo\ORM\Repository ) ); } + } + + public function remove(Entity $entity) + { + $this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeRemove', $entity); + + $result = parent::remove($entity); + + $this->getEntityManager()->getHookManager()->process($this->entityName, 'afterRemove', $entity); + return $result; } public function save(Entity $entity) diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index a18742b76d..a2c41fefe4 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -11,7 +11,7 @@ class ServiceFactory public function __construct(Container $container) { $this->container = $container; - } + } public function createByClassName($className) { diff --git a/application/Espo/Hooks/Common/Stream.php b/application/Espo/Hooks/Common/Stream.php new file mode 100644 index 0000000000..63ff3d397a --- /dev/null +++ b/application/Espo/Hooks/Common/Stream.php @@ -0,0 +1,66 @@ +dependencies[] = 'serviceFactory'; + } + + protected function getServiceFactory() + { + return $this->getInjection('serviceFactory'); + } + + protected function checkHasStream(Entity $entity) + { + $entityName = $entity->getEntityName(); + return $this->getMetadata()->get("scopes.{$entityName}.stream"); + } + + public function afterRemove(Entity $entity) + { + if ($this->checkHasStream($entity)) { + $this->getStreamService()->unfollowAllUsersFromEntity($entity); + } + } + + public function afterSave(Entity $entity) + { + $entityName = $entity->getEntityName(); + + if ($this->checkHasStream($entity)) { + if (!$entity->getFetchedValue('id')) { + $this->noteCreate($entity); + } + } + } + + protected function getStreamService() + { + if (empty($this->streamService)) { + $this->streamService = $this->getServiceFactory()->createByClassName('\\Espo\\Services\\Stream'); + } + return $this->streamService; + } + + protected function noteCreate(Entity $entity) + { + $this->getStreamService()->noteCreate($entity); + } +} + diff --git a/application/Espo/ORM/Entity.php b/application/Espo/ORM/Entity.php index 14f42a6cf1..9daa4d69df 100644 --- a/application/Espo/ORM/Entity.php +++ b/application/Espo/ORM/Entity.php @@ -39,8 +39,7 @@ abstract class Entity implements IEntity /** * @var EntityManager Entity Manager. */ - protected $entityManager; - + protected $entityManager; public function __construct($defs = array(), EntityManager $entityManager = null) diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index dea26d08ea..b6fa33e3c7 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -310,7 +310,7 @@ class Record extends \Espo\Core\Services\Base return $idsUpdated; // TODO update $where - } + } public function follow($id, $userId = null) { diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index d3e8bf8f73..4f4691d5c7 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -5,6 +5,8 @@ namespace Espo\Services; use \Espo\Core\Exceptions\Forbidden; use \Espo\Core\Exceptions\NotFound; +use Espo\ORM\Entity; + class Stream extends \Espo\Core\Services\Base { protected $dependencies = array( @@ -34,6 +36,21 @@ class Stream extends \Espo\Core\Services\Base return $this->injections['metadata']; } + public function unfollowAllUsersFromEntity(Entity $entity) + { + if (empty($entity->id)) { + return; + } + + $pdo = $this->getEntityManager()->getPDO(); + $sql = " + DELETE FROM subscription + WHERE + entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . " + "; + $sth = $pdo->prepare($sql)->execute(); + } + public function findUserStream($params = array()) { $selectParams = array( @@ -58,9 +75,11 @@ class Stream extends \Espo\Core\Services\Base } foreach ($collection as $e) { - if ($e->get('type') == 'Post' && $e->get('parentId') && $e->get('parentType')) { + if ($e->get('parentId') && $e->get('parentType')) { $entity = $this->getEntityManager()->getEntity($e->get('parentType'), $e->get('parentId')); - $e->set('parentName', $entity->get('name')); + if ($entity) { + $e->set('parentName', $entity->get('name')); + } } } @@ -115,5 +134,33 @@ class Stream extends \Espo\Core\Services\Base 'collection' => $collection, ); } + + protected function loadAssignedUserName(Entity $entity) + { + $user = $this->getEntityManager()->getEntity('User', $entity->get('assignedUserId')); + if ($user) { + $entity->set('assignedUserName', $user->get('name')); + } + } + + public function noteCreate(Entity $entity) + { + $note = $this->getEntityManager()->getEntity('Note'); + + $note->set('type', 'Create'); + $note->set('parentId', $entity->id); + $note->set('parentType', $entity->getEntityName()); + + if ($entity->get('assignedUserId') != $entity->get('createdById')) { + if (!$entity->has('assignedUserName')) { + $this->loadAssignedUserName($entity); + } + $note->set('data', json_encode(array( + 'assignedUserId' => $entity->get('assignedUserId'), + 'assignedUserName' => $entity->get('assignedUserName'), + ))); + } + $this->getEntityManager()->saveEntity($note); + } }