global search
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Controllers;
|
||||
|
||||
use \Espo\Core\Exceptions\Error,
|
||||
\Espo\Core\Exceptions\Forbidden;
|
||||
|
||||
class GlobalSearch extends \Espo\Core\Controllers\Base
|
||||
{
|
||||
public function actionSearch($params, $data, $request)
|
||||
{
|
||||
$query = $params['query'];
|
||||
|
||||
$offset = $request->get('offset');
|
||||
$maxSize = $request->get('maxSize');
|
||||
|
||||
return $this->getService('GlobalSearch')->find($query, $offset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ class Preferences extends \Espo\Core\Controllers\Base
|
||||
$this->handleUserAccess($userId);
|
||||
|
||||
$entity = $this->getEntityManager()->getEntity('Preferences', $userId);
|
||||
$entity->set('name', $this->getUser()->get('name'));
|
||||
if ($entity) {
|
||||
return $entity->toArray();
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ class Acl
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -45,9 +45,8 @@ class Container
|
||||
|
||||
// TODO throw an exception
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private function loadSlim()
|
||||
{
|
||||
//return new \Slim\Slim();
|
||||
@@ -118,6 +117,16 @@ class Container
|
||||
$this
|
||||
);
|
||||
}
|
||||
|
||||
private function loadSelectManagerFactory()
|
||||
{
|
||||
return new \Espo\Core\SelectManagerFactory(
|
||||
$this->get('entityManager'),
|
||||
$this->get('user'),
|
||||
$this->get('acl'),
|
||||
$this->get('metadata')
|
||||
);
|
||||
}
|
||||
|
||||
private function loadMetadata()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Core;
|
||||
|
||||
use \Espo\Core\Exceptions\Error;
|
||||
|
||||
use \Espo\Core\Utils\Util;
|
||||
|
||||
class SelectManagerFactory
|
||||
{
|
||||
private $entityManager;
|
||||
|
||||
private $user;
|
||||
|
||||
private $acl;
|
||||
|
||||
private $metadata;
|
||||
|
||||
public function __construct($entityManager, \Espo\Entities\User $user, Acl $acl, $metadata)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->user = $user;
|
||||
$this->acl = $acl;
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
public function create($entityName)
|
||||
{
|
||||
$className = '\\Espo\\Custom\\SelectManagers\\' . Util::normilizeClassName($entityName);
|
||||
if (!class_exists($className)) {
|
||||
$moduleName = $this->metadata->getScopeModuleName($entityName);
|
||||
if ($moduleName) {
|
||||
$className = '\\Espo\\Modules\\' . $moduleName . '\\SelectManagers\\' . Util::normilizeClassName($entityName);
|
||||
} else {
|
||||
$className = '\\Espo\\SelectManagers\\' . Util::normilizeClassName($entityName);
|
||||
}
|
||||
if (!class_exists($className)) {
|
||||
$className = '\\Espo\\Core\\SelectManagers\\Base';
|
||||
}
|
||||
}
|
||||
|
||||
$selectManager = new $className($this->entityManager, $this->user, $this->acl, $this->metadata);
|
||||
$selectManager->setEntityName($entityName);
|
||||
|
||||
return $selectManager;
|
||||
}
|
||||
}
|
||||
|
||||
+19
-8
@@ -1,10 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Core;
|
||||
namespace Espo\Core\SelectManagers;
|
||||
|
||||
use \Espo\Core\Exceptions\Error;
|
||||
|
||||
class SelectManager
|
||||
use \Espo\Core\Acl;
|
||||
|
||||
class Base
|
||||
{
|
||||
protected $container;
|
||||
|
||||
@@ -18,7 +20,7 @@ class SelectManager
|
||||
|
||||
protected $metadata;
|
||||
|
||||
public function __construct(ORM\EntityManager $entityManager, \Espo\Entities\User $user, Acl $acl, $metadata)
|
||||
public function __construct($entityManager, \Espo\Entities\User $user, Acl $acl, $metadata)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->user = $user;
|
||||
@@ -131,6 +133,7 @@ class SelectManager
|
||||
protected function access(&$result)
|
||||
{
|
||||
if ($this->acl->checkReadOnlyOwn($this->entityName)) {
|
||||
|
||||
if (!array_key_exists('whereClause', $result)) {
|
||||
$result['whereClause'] = array();
|
||||
}
|
||||
@@ -142,13 +145,21 @@ class SelectManager
|
||||
}
|
||||
if (!array_key_exists('joins', $result)) {
|
||||
$result['joins'] = array();
|
||||
if (!in_array('teams', $result['joins'])) {
|
||||
$result['joins'][] = 'teams';
|
||||
}
|
||||
}
|
||||
$result['whereClause']['Team.id'] = $this->user->get('teamsIds');
|
||||
}
|
||||
if (!in_array('teams', $result['joins'])) {
|
||||
$result['joins'][] = 'teams';
|
||||
}
|
||||
|
||||
$result['whereClause']['Team.id'] = $this->user->get('teamsIds');
|
||||
}
|
||||
}
|
||||
|
||||
public function getAclParams()
|
||||
{
|
||||
$result = array();
|
||||
$this->access($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getSelectParams(array $params, $withAcl = false)
|
||||
{
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Espo\Modules\Crm\SelectManagers;
|
||||
|
||||
class CaseObj extends \Espo\Core\SelectManager
|
||||
class CaseObj extends \Espo\Core\SelectManagers\Base
|
||||
{
|
||||
|
||||
protected function getBoolFilterWhereOpen()
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
namespace Espo\Modules\Crm\SelectManagers;
|
||||
|
||||
class Opportunity extends \Espo\Core\SelectManager
|
||||
class Opportunity extends \Espo\Core\SelectManagers\Base
|
||||
{
|
||||
|
||||
protected function getBoolFilterWhereOpen()
|
||||
{
|
||||
return array(
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
},
|
||||
"smtpAuth": {
|
||||
"type": "bool",
|
||||
"default": true
|
||||
"default": false
|
||||
},
|
||||
"smtpSecurity": {
|
||||
"type": "enum",
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"recordViews":{
|
||||
"edit": "Preferences.Record.Edit"
|
||||
},
|
||||
"ciews":{
|
||||
"edit": "Preferences.Edit"
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,16 @@
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/GlobalSearch/:query",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"GlobalSearch",
|
||||
"action":"search",
|
||||
"query": ":query"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/action/:action",
|
||||
"method":"post",
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Services;
|
||||
|
||||
use \Espo\Core\Exceptions\Forbidden;
|
||||
use \Espo\Core\Exceptions\NotFound;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class GlobalSearch extends \Espo\Core\Services\Base
|
||||
{
|
||||
|
||||
protected $dependencies = array(
|
||||
'entityManager',
|
||||
'user',
|
||||
'metadata',
|
||||
'acl',
|
||||
'selectManagerFactory',
|
||||
);
|
||||
|
||||
protected function getSelectManagerFactory()
|
||||
{
|
||||
return $this->injections['selectManagerFactory'];
|
||||
}
|
||||
|
||||
protected function getEntityManager()
|
||||
{
|
||||
return $this->injections['entityManager'];
|
||||
}
|
||||
|
||||
protected function getUser()
|
||||
{
|
||||
return $this->injections['user'];
|
||||
}
|
||||
|
||||
protected function getAcl()
|
||||
{
|
||||
return $this->injections['acl'];
|
||||
}
|
||||
|
||||
protected function getMetadata()
|
||||
{
|
||||
return $this->injections['metadata'];
|
||||
}
|
||||
|
||||
public function find($query, $offset)
|
||||
{
|
||||
$entityNameList = array(
|
||||
'Account',
|
||||
'Contact',
|
||||
'Lead',
|
||||
'Prospect',
|
||||
'Opportunity',
|
||||
);
|
||||
|
||||
$list = array();
|
||||
$count = 0;
|
||||
$total = 0;
|
||||
foreach ($entityNameList as $entityName) {
|
||||
|
||||
if (!$this->getAcl()->check($entityName, 'read')) {
|
||||
continue;
|
||||
}
|
||||
$selectManager = $this->getSelectManagerFactory()->create($entityName);
|
||||
|
||||
$searchParams = array(
|
||||
'whereClause' => array(
|
||||
'OR' => array(
|
||||
'name*' => '%' . $query . '%',
|
||||
)
|
||||
),
|
||||
'offset' => $offset,
|
||||
'limit' => 5,
|
||||
'orderBy' => 'createdAt',
|
||||
'order' => 'DESC',
|
||||
);
|
||||
$selectParams = array_merge_recursive($searchParams, $selectManager->getAclParams());
|
||||
|
||||
$collection = $this->getEntityManager()->getRepository($entityName)->find($selectParams);
|
||||
$count += count($collection);
|
||||
$total += $this->getEntityManager()->getRepository($entityName)->count($selectParams);
|
||||
foreach ($collection as $entity) {
|
||||
$data = $entity->toArray();
|
||||
$data['_scope'] = $entityName;
|
||||
$list[] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'total' => $total,
|
||||
'list' => $list,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ class Record extends \Espo\Core\Services\Base
|
||||
'config',
|
||||
'serviceFactory',
|
||||
'fileManager',
|
||||
'selectManagerFactory',
|
||||
);
|
||||
|
||||
protected $entityName;
|
||||
@@ -51,6 +52,11 @@ class Record extends \Espo\Core\Services\Base
|
||||
{
|
||||
return $this->injections['serviceFactory'];
|
||||
}
|
||||
|
||||
protected function getSelectManagerFactory()
|
||||
{
|
||||
return $this->injections['selectManagerFactory'];
|
||||
}
|
||||
|
||||
protected function getUser()
|
||||
{
|
||||
@@ -143,23 +149,7 @@ class Record extends \Espo\Core\Services\Base
|
||||
|
||||
protected function getSelectManager($entityName)
|
||||
{
|
||||
$className = '\\Espo\\Custom\\SelectManagers\\' . Util::normilizeClassName($entityName);
|
||||
if (!class_exists($className)) {
|
||||
$moduleName = $this->getMetadata()->getScopeModuleName($entityName);
|
||||
if ($moduleName) {
|
||||
$className = '\\Espo\\Modules\\' . $moduleName . '\\SelectManagers\\' . Util::normilizeClassName($entityName);
|
||||
} else {
|
||||
$className = '\\Espo\\SelectManagers\\' . Util::normilizeClassName($entityName);
|
||||
}
|
||||
if (!class_exists($className)) {
|
||||
$className = '\\Espo\\Core\\SelectManager';
|
||||
}
|
||||
}
|
||||
|
||||
$selectManager = new $className($this->getEntityManager(), $this->getUser(), $this->getAcl(), $this->getMetadata());
|
||||
$selectManager->setEntityName($entityName);
|
||||
|
||||
return $selectManager;
|
||||
return $this->getSelectManagerFactory()->create($entityName);
|
||||
}
|
||||
|
||||
protected function storeEntity(Entity $entity)
|
||||
|
||||
Reference in New Issue
Block a user