add controllers for metadata, settings

This commit is contained in:
Taras Machyshyn
2013-11-07 12:19:09 +02:00
parent ebc09b8ac8
commit 9f2dd08a6a
9 changed files with 261 additions and 177 deletions
+82 -63
View File
@@ -11,16 +11,16 @@ require 'vendor/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
/* END: remove for composer */
//$app = new \Slim\Slim();
//$routes = new \Slim\Slim();
$app = new \Slim\Slim(array(
$routes = new \Slim\Slim(array(
'mode' => 'development'
));
$app->add(new Api\Auth());
$routes->add(new Api\Auth());
//convert all url params to camel case format
$app->hook('slim.before.dispatch', function () use ($app) {
$routeParams= $app->router()->getCurrentRoute()->getParams();
$routes->hook('slim.before.dispatch', function () use ($routes) {
$routeParams= $routes->router()->getCurrentRoute()->getParams();
if (!empty($routeParams)) {
$baseUtils= new Utils\BaseUtils();
@@ -28,36 +28,36 @@ $app->hook('slim.before.dispatch', function () use ($app) {
$param= $baseUtils->toCamelCase($param);
}
$app->router()->getCurrentRoute()->setParams($routeParams);
$routes->router()->getCurrentRoute()->setParams($routeParams);
}
});
//END: convert all url params to camel case format
$app->hook('slim.before.dispatch', function () use ($app) {
$routes->hook('slim.before.dispatch', function () use ($routes) {
$currentRoute = $app->router()->getCurrentRoute();
$currentRoute = $routes->router()->getCurrentRoute();
$conditions = $currentRoute->getConditions();
if (isset($conditions['useController']) && !$conditions['useController']) {
if (isset($conditions['useController']) && $conditions['useController'] == false) {
return;
}
$espoController = call_user_func( $app->router()->getCurrentRoute()->getCallable() );
$espoController = call_user_func( $routes->router()->getCurrentRoute()->getCallable() );
$espoKeys = array_keys($espoController);
if (!in_array('controller', $espoKeys) || !in_array('action', $espoKeys) || !in_array('scope', $espoKeys)) {
if (!in_array('controller', $espoKeys)) {
return;
}
$ControllerManager = new Utils\Controllers\Manager();
$params = $currentRoute->getParams();
$data = $app->request()->getBody();
$data = $routes->request()->getBody();
//prepare controller Params
$controllerParams = array();
$controllerParams['HttpMethod'] = strtolower($app->request()->getMethod());
$controllerParams['HttpMethod'] = strtolower($routes->request()->getMethod());
foreach($espoController as $key => $val) {
if (strstr($val, ':')) {
@@ -75,63 +75,107 @@ $app->hook('slim.before.dispatch', function () use ($app) {
//return json response
$app->hook('slim.after.router', function () use (&$app) {
$app->contentType('application/json');
//$app->contentType('text/javascript');
$routes->hook('slim.after.router', function () use (&$routes) {
$routes->contentType('application/json');
//$routes->contentType('text/javascript');
});
//END: return json response
//Setup routes
$app->get('/', '\Espo\Utils\Api\Rest::main');
$routes->get('/', '\Espo\Utils\Api\Rest::main')->conditions( array('useController' => false) );
$routes->get('/app/user/', '\Espo\Utils\Api\Rest::getAppUser')->conditions( array('useController' => false) );
$app->get('/metadata/', '\Espo\Utils\Api\Rest::getMetadata');
$app->put('/metadata/:type/:scope/', '\Espo\Utils\Api\Rest::putMetadata');
//METADATA
$routes->get('/metadata/', function() {
return array(
'controller' => 'Metadata',
);
});
$app->get('/settings/', '\Espo\Utils\Api\Rest::getSettings')->conditions( array('auth' => false) );
$app->map('/settings/', '\Espo\Utils\Api\Rest::patchSettings')->via('PATCH');
//$app->get('/settings/', '\Espo\Utils\Api\Rest::getSettings')->conditions( array('auth' => false) );
$routes->put('/metadata/:type/:scope/', function() {
return array(
'controller' => 'Metadata',
'scope' => ':scope',
'action' => ':type',
);
});
//END: METADATA
//$app->get('/:controller/layout/:name/', '\Espo\Utils\Api\Rest::getLayout');
//$app->put('/:controller/layout/:name/', '\Espo\Utils\Api\Rest::putLayout');
//$app->map('/:controller/layout/:name/', '\Espo\Utils\Api\Rest::patchLayout')->via('PATCH');
//SETTINGS
$routes->get('/settings/', function() {
return array(
'controller' => 'Settings',
);
})->conditions( array('auth' => false) );
$app->get('/app/user/', '\Espo\Utils\Api\Rest::getAppUser');
$routes->map('/settings/', function() {
return array(
'controller' => 'Settings',
);
})->via('PATCH');
//END: SETTINGS
//LAYOUT
$routes->get('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
});
$routes->put('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
});
$routes->map('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
})->via('PATCH');
//END: LAYOUT
/*$app->get('/:controller/:id', function() {
/*$routes->get('/:controller/:id', function() {
return array(
'controller' => ':controller',
'action' => 'read',
'id' => ':id'
);
})->conditions( array('useController' => true) );
});
$app->post('/:controller', function() {
$routes->post('/:controller', function() {
return array(
'controller' => ':controller',
'action' => 'create',
);
})->conditions( array('useController' => true) );
});
$app->put('/:controller/:id', function() {
$routes->put('/:controller/:id', function() {
return array(
'controller' => ':controller',
'action' => 'update',
'id' => ':id'
);
})->conditions( array('useController' => true) );
});
$app->patch('/:controller/:id', function() {
$routes->patch('/:controller/:id', function() {
return array(
'controller' => ':controller',
'action' => 'patch',
'id' => ':id'
);
})->conditions( array('useController' => true) );
});
$app->get('/:controller/:id/:link/:foreignId', function() {
$routes->get('/:controller/:id/:link/:foreignId', function() {
return array(
'controller' => ':controller',
'action' => 'readRelated',
@@ -139,35 +183,10 @@ $app->get('/:controller/:id/:link/:foreignId', function() {
'link' => ':link',
'foreignId' => ':foreignId'
);
})->conditions( array('useController' => true) ); */
//Layout
$app->get('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
})->conditions( array('useController' => true) );
$app->put('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
})->conditions( array('useController' => true) );
$app->map('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
})->via('PATCH')->conditions( array('useController' => true) );
//END: Layout
}); */
$app->run();
$routes->run();
?>
View File
View File
+2 -32
View File
@@ -37,7 +37,7 @@ EOT;
public function getMetadata()
{
global $base;
$devMode= !$base->config->get('useCache');
$devMode= !$base->config->get('useCache');
$metadata= new Utils\Metadata();
$data= $metadata->getMetadata(true, $devMode);
@@ -56,7 +56,7 @@ EOT;
$app= Slim::getInstance();
$data = $app->request()->getBody();
$metadata = new Utils\Metadata();
$metadata = new Utils\Metadata();
$result = $metadata->setMetadata($data, $type, $scope);
if ($result===false) {
@@ -138,36 +138,6 @@ EOT;
//return Api\Helper::output($data, 'Cannot get this layout', 404);
} */
public function getLayout($controller, $name)
{
return Api\Helper::output('Rest-get', 'Cannot get layout');
}
/**
* Add or change layout
*
* @return void
*/
public function patchLayout($controller, $name)
{
return Api\Helper::output('Rest-patch', 'Cannot get layout');
}
/**
* Add or change layout
*
* @return void
*/
public function putLayout($controller, $name)
{
return Api\Helper::output('Rest-put', 'Cannot get layout');
}
}
+1 -2
View File
@@ -90,7 +90,6 @@ class BaseUtils
'Role' => '',
'Team' => '',
'User' => '',
'OutboundEmail' => '',
'Product' => 'Crm',
'Account' => 'Crm',
'Contact' => 'Crm',
@@ -361,4 +360,4 @@ return '.var_export($content, true).';
}
?>
?>
+87 -79
View File
@@ -25,107 +25,111 @@ class Manager
*/
public function call($controllerParams, $params, $data = '')
{
$baseUtils = new Utils\BaseUtils();
$baseUtils = new Utils\BaseUtils();
$espoPath = $baseUtils->getObject('Configurator')->get('espoPath');
$controllerPath= $baseUtils->concatPath($espoPath, 'Utils/Controllers');
$espoPath = $baseUtils->getObject('Configurator')->get('espoPath');
$controllerPath= $baseUtils->concatPath($espoPath, 'Utils/Controllers');
$crud = $baseUtils->getObject('Configurator')->get('crud');
$baseAction = $crud->$controllerParams['HttpMethod'];
if (empty($baseAction)) {
return $this->response(false, 'Cannot find action for HTTP Method ['.$controllerParams['HttpMethod'].']', 404);
}
$crud = $baseUtils->getObject('Configurator')->get('crud');
$baseAction = $crud->$controllerParams['HttpMethod'];
if (empty($baseAction)) {
return $this->response(false, 'Cannot find action for HTTP Method ['.$controllerParams['HttpMethod'].']', 404);
}
$controller = (object) array(
'name' => strtolower($controllerParams['controller']),
'baseAction' => $baseAction,
'scope' => $controllerParams['scope'],
'action' => $controllerParams['action'],
);
$controller = (object) array(
'name' => strtolower($controllerParams['controller']),
'baseAction' => $baseAction,
'scope' => isset($controllerParams['scope']) ? $controllerParams['scope'] : '',
'action' => isset($controllerParams['action']) ? $controllerParams['action'] : '',
);
if (!$baseUtils->isScopeExists($controller->scope)) {
return $this->response(false, 'Controller for Scope ['.$controller->scope.'] does not exist.', 404);
}
//define default values
$classInfo = new \stdClass();
$classInfo->name = $this->getClassName($controllerPath, 'Controller');
$classInfo->path = $this->getClassPath($classInfo->name);
$classInfo->method = $this->getDefinedMethod($classInfo->name, $controller->baseAction, $controller->action);
if (!empty($controller->scope) && !$baseUtils->isScopeExists($controller->scope)) {
return $this->response(false, 'Controller for Scope ['.$controller->scope.'] does not exist.', 404);
}
//Espo\Utils\Controlles\Layout and Custom\Espo\Utils\Controlles\Layout
$controllerClass = $this->getClassName($controllerPath, $controller->name);
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller);
//END: Espo\Utils\Controlles\Layout and Custom\Espo\Utils\Controlles\Layout
//define default values
$classInfo = new \stdClass();
$classInfo->name = $this->getClassName($controllerPath, 'Controller');
$classInfo->path = $this->getClassPath($classInfo->name);
$classInfo->method = $this->getDefinedMethod($classInfo->name, $controller->baseAction, $controller->action);
//path in Modules dir
$controllerDir = $baseUtils->concatPath( $baseUtils->getScopePath($controller->scope), $baseUtils->getObject('Configurator')->get('controllerPath') );
//ex. Modules\Crm\Controllers\Layout and Cusom\Modules\Crm\Controllers\Layout
$controllerClass = $this->getClassName($controllerDir, $controller->name);
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller);
//END: ex. Modules\Crm\controllers\Layout and Cusom\Modules\Crm\controllers\Layout
//Espo\Utils\Controlles\Layout and Custom\Espo\Utils\Controlles\Layout
$controllerClass = $this->getClassName($controllerPath, $controller->name);
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller);
//END: Espo\Utils\Controlles\Layout and Custom\Espo\Utils\Controlles\Layout
//ex. Modules\Crm\Controllers\AccountLayout and Custom\Modules\Crm\Controllers\AccountLayout
$controllerClass = $this->getClassName($controllerDir, $controller->scope.'-'.$controller->name);
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller);
//END: ex. Modules\Crm\Controllers\AccountLayout and Custom\Modules\Crm\Controllers\AccountLayout
if (!empty($controller->scope)) {
//path in Modules dir
$controllerDir = $baseUtils->concatPath( $baseUtils->getScopePath($controller->scope), $baseUtils->getObject('Configurator')->get('controllerPath') );
//ex. Modules\Crm\Controllers\Layout and Cusom\Modules\Crm\Controllers\Layout
$controllerClass = $this->getClassName($controllerDir, $controller->name);
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller);
//END: ex. Modules\Crm\controllers\Layout and Cusom\Modules\Crm\controllers\Layout
//CHECK ACTION
if (empty($classInfo->method)) {
return $this->response(false, 'Actions ['.$controller->baseAction.'] and ['.$controller->action.'] do not exist.', 404);
}
//END: CHECK ACTION
//ex. Modules\Crm\Controllers\AccountLayout and Custom\Modules\Crm\Controllers\AccountLayout
$controllerClass = $this->getClassName($controllerDir, $controller->scope.'-'.$controller->name);
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller);
//END: ex. Modules\Crm\Controllers\AccountLayout and Custom\Modules\Crm\Controllers\AccountLayout
}
//call class method
$className = $classInfo->name;
$classMethod = $classInfo->method;
//CHECK ACTION
if (empty($classInfo->method)) {
return $this->response(false, 'Actions ['.$controller->baseAction.'] and ['.$controller->action.'] do not exist.', 404);
}
//END: CHECK ACTION
require_once($classInfo->path);
$class = new $className();
//call before method if exists: beforeRead, beforeDetailSmall, beforeReadDetailSmall
$beforeMethod = $this->getDefinedMethod($className, $controller->baseAction, $controller->action, 'before');
if ( !empty($beforeMethod) ) {
//call class method
$className = $classInfo->name;
$classMethod = $classInfo->method;
require_once($classInfo->path);
$class = new $className();
//call before method if exists: beforeRead, beforeDetailSmall, beforeReadDetailSmall
$beforeMethod = $this->getDefinedMethod($className, $controller->baseAction, $controller->action, 'before');
if ( !empty($beforeMethod) ) {
$class->$beforeMethod($params, $data);
} //END: call before method if exists
} //END: call before method if exists
$result = $class->$classMethod($params, $data);
$result = $class->$classMethod($params, $data);
//call after method if exists: afterRead, afterDetailSmall, afterReadDetailSmall
$afterMethod = $this->getDefinedMethod($className, $controller->baseAction, $controller->action, 'after');
if ( !empty($afterMethod) ) {
//call after method if exists: afterRead, afterDetailSmall, afterReadDetailSmall
$afterMethod = $this->getDefinedMethod($className, $controller->baseAction, $controller->action, 'after');
if ( !empty($afterMethod) ) {
try {
$class->$afterMethod($params, $data, $result);
$class->$afterMethod($params, $data, $result);
} catch (\Exception $e) {
$class->$afterMethod($params, $data);
$class->$afterMethod($params, $data);
}
} //END: call after method if exists
} //END: call after method if exists
if (is_array($result)) {
if (is_array($result)) {
$returnResult = array_values($result);
if (!empty($returnResult[2])) {
return $this->response($returnResult[0], $returnResult[1], $returnResult[2]);
}
if (!empty($returnResult[1])) {
return $this->response($returnResult[0], $returnResult[1]);
}
if (!empty($returnResult[0])) {
return $this->response($returnResult[0]);
$returnResult = array_values($result);
if (!empty($returnResult[2])) {
return $this->response($returnResult[0], $returnResult[1], $returnResult[2]);
}
if (!empty($returnResult[1])) {
return $this->response($returnResult[0], $returnResult[1]);
}
if (!empty($returnResult[0])) {
return $this->response($returnResult[0]);
}
return $this->response(false, 'Cannot find requested controller', 404);
}
return $this->response(false, 'Cannot find requested controller', 404);
}
return $this->response($result);
return $this->response($result);
}
@@ -154,19 +158,23 @@ class Manager
$classMethod = $prefixBaseAction;
}
if (empty($action)) {
return $classMethod;
}
//method as 'detailSmall'
$prefixAction = $baseUtils->toCamelCase($prefix.$action);
$prefixAction = $baseUtils->toCamelCase($prefix.$action);
if ( method_exists($className, $prefixAction) ) {
$classMethod = $prefixAction;
$classMethod = $prefixAction;
}
//method as 'readDetailSmall'
$fullAction = $baseUtils->toCamelCase($prefix.$baseAction.'-'.$action);
if (method_exists($className, $fullAction)) {
$classMethod = $fullAction;
$classMethod = $fullAction;
}
return $classMethod;
return $classMethod;
}
/**
@@ -191,10 +199,10 @@ class Manager
}
if ($isCustom) {
$baseUtils = new Utils\BaseUtils();
$baseUtils = new Utils\BaseUtils();
$espoCustomDir = $baseUtils->getObject('Configurator')->get('espoCustomPath');
$controllerClass = $espoCustomDir.'\\'.$className;
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller, false);
$classInfo = $this->setClassInfo($controllerClass, $classInfo, $controller, false);
}
return $classInfo;
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Espo\Utils\Controllers;
use Espo\Utils as Utils;
class Metadata extends Controller
{
public function read($params, $data)
{
$metadata= new Utils\Metadata();
$devMode= !$metadata->getObject('Configurator')->get('useCache');
$data= $metadata->getMetadata(true, $devMode);
return array($data, 'Cannot reach metadata data');
}
public function update($params, $data)
{
$metadata = new Utils\Metadata();
$result = $metadata->setMetadata($data, $params['type'], $params['scope']);
if ($result===false) {
return array($result, 'Cannot save metadata data');
}
$data= $metadata->getMetadata(true, true);
return array($data, 'Cannot get metadata data');
}
}
?>
@@ -0,0 +1,50 @@
<?php
namespace Espo\Utils\Controllers;
use Espo\Utils as Utils;
class Settings extends Controller
{
public function read($params, $data)
{
global $base;
$config= new Utils\Configurator();
$isAdmin= false;
if(isset($base->currentUser) && is_object($base->currentUser)) {
$isAdmin= $base->currentUser->isAdmin();
}
$data= $config->getJSON($isAdmin);
return array($data, 'Cannot get settings');
}
public function patch($params, $data)
{
global $base;
$config= new Utils\Configurator();
$isAdmin= false;
if(isset($base->currentUser) && is_object($base->currentUser)) {
$isAdmin= $base->currentUser->isAdmin();
}
$result= $config->setJSON($data, $isAdmin);
if ($result===false) {
return array($result, 'Cannot save settings');
}
$data= $config->getJSON($isAdmin);
return array($data, 'Cannot get settings');
}
}
?>
+1 -1
View File
@@ -44,7 +44,7 @@ class MetadataTest extends \PHPUnit_Framework_TestCase
$data= '{"module":"Test"}';
$this->assertTrue($this->fixture->isSuccess( $data ));
$this->fixture->setUrl('/metadata/custom-test/test');
$this->fixture->setUrl('/metadata/custom-test/custom-test');
$data= '{"module":"Test","var1":{"subvar1":"NEWsubval1","subvar55":"subval55"}}';
$this->assertTrue($this->fixture->isSuccess( $data ));
}