This commit is contained in:
Yuri Kuznetsov
2014-01-15 18:06:46 +02:00
parent 6def7520ae
commit 7206e6978c
10 changed files with 125 additions and 35 deletions

View File

@@ -29,3 +29,4 @@ class Stream extends \Espo\Core\Controllers\Base
);
}
}

View File

@@ -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);
}
}

View File

@@ -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, ':')) {

View File

@@ -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')
);
}

View File

@@ -46,8 +46,7 @@ class ControllerManager
} else {
$controllerClassName = '\\Espo\\Controllers\\' . Util::normilizeClassName($controllerName);
}
}
}
if ($data) {
$data = json_decode($data, true);

View File

@@ -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)

View File

@@ -171,7 +171,7 @@ abstract class Mapper implements IMapper
}
if (!empty($customJoin)) {
$joinsPart .= ' ' . $customJoin;
$joinsPart .= ' ' . $customJoin . ' ';
}
if (!empty($joins) && is_array($joins)) {

View File

@@ -41,6 +41,16 @@
}
},
{
"route":"/Stream",
"method":"get",
"params":{
"controller":"Stream",
"action":"list",
"scope": "User"
}
},
{
"route":"/:controller/action/:action",
"method":"post",

View File

@@ -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) . "

View File

@@ -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)) {