Skip to content

Commit

Permalink
removed session serializer in order to add closures #1228, this fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Mar 11, 2017
1 parent b4747b8 commit 13510d3
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 144 deletions.
2 changes: 1 addition & 1 deletion modules/admin/src/components/AdminMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function getItems()
*/
public function getApiDetail($api = null)
{
$items = $this->items;
$items = $this->getItems();

$key = array_search($api, array_column($items, 'permssionApiEndpoint'));

Expand Down
80 changes: 0 additions & 80 deletions modules/admin/src/controllers/NgrestController.php

This file was deleted.

15 changes: 0 additions & 15 deletions modules/admin/src/ngrest/NgRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,6 @@ public function render(RenderInterface $render)
$this->render->setConfig($this->config);
return $this->render->render();
}

public static function findConfig($ngRestConfigHash)
{
// decode the session, find the hash, if yes return the
$session = Yii::$app->session->get($ngRestConfigHash);
// valid session usnerialize and return
if ($session) {
return unserialize($session);
}
}

public function __destruct()
{
yii::$app->session->set($this->config->hash, serialize($this->config));
}

public static function createPluginObject($className, $name, $alias, $i18n, $args = [])
{
Expand Down
59 changes: 44 additions & 15 deletions modules/admin/src/ngrest/base/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use luya\admin\base\RestActiveController;
use luya\admin\components\AdminUser;
use luya\admin\models\UserOnline;
use luya\admin\ngrest\render\RenderActiveWindow;
use luya\admin\ngrest\render\RenderActiveWindowCallback;
use luya\admin\ngrest\NgRest;
use luya\cms\admin\helpers\MenuHelper;

/**
* The RestActiveController for all NgRest implementations.
Expand All @@ -23,20 +27,6 @@
*/
class Api extends RestActiveController
{
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
$actions['view']['class'] = 'luya\admin\ngrest\base\actions\ViewAction';
$actions['index']['class'] = 'luya\admin\ngrest\base\actions\IndexAction';
$actions['create']['class'] = 'luya\admin\ngrest\base\actions\CreateAction';
$actions['update']['class'] = 'luya\admin\ngrest\base\actions\UpdateAction';
$actions['delete']['class'] = 'luya\admin\ngrest\base\actions\DeleteAction';
return $actions;
}

/**
* @var string Defines the related model for the NgRest Controller. The full qualiefied model name
* is required.
Expand Down Expand Up @@ -76,6 +66,20 @@ public function init()
}
}

/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
$actions['view']['class'] = 'luya\admin\ngrest\base\actions\ViewAction';
$actions['index']['class'] = 'luya\admin\ngrest\base\actions\IndexAction';
$actions['create']['class'] = 'luya\admin\ngrest\base\actions\CreateAction';
$actions['update']['class'] = 'luya\admin\ngrest\base\actions\UpdateAction';
$actions['delete']['class'] = 'luya\admin\ngrest\base\actions\DeleteAction';
return $actions;
}

private $_model = null;

/**
Expand Down Expand Up @@ -195,6 +199,26 @@ public function actionFilter($filterName)
]);
}

public function actionActiveWindowCallback()
{
$config = $this->model->getNgRestConfig();
$render = new RenderActiveWindowCallback();
$ngrest = new NgRest($config);

return $ngrest->render($render);
}

public function actionActiveWindowRender()
{
$config = $this->model->getNgRestConfig();
$render = new RenderActiveWindow();
$render->setItemId(Yii::$app->request->post('itemId', false));
$render->setActiveWindowHash(Yii::$app->request->post('activeWindowHash', false));
$ngrest = new NgRest($config);

return $ngrest->render($render);
}

/**
* Prepare a temp file to
* @todo added very basic csv support, must be stored as class, just a temp solution
Expand Down Expand Up @@ -262,11 +286,16 @@ public function actionExport()

$store = FileHelper::writeFile('@runtime/'.$key.'.tmp', $tempData);

$menu = Yii::$app->adminmenu->getApiDetail($this->model->ngRestApiEndpoint());

$route = $menu['route'];
$route = str_replace("/index", "/export-download", $route);

if ($store) {
Yii::$app->session->set('tempNgRestFileName', Inflector::slug($this->model->tableName()));
Yii::$app->session->set('tempNgRestKey', $key);
return [
'url' => Url::toRoute(['/admin/ngrest/export-download', 'key' => base64_encode($key)]),
'url' => Url::toRoute(['/'.$route, 'key' => base64_encode($key)]),
];
}

Expand Down
50 changes: 22 additions & 28 deletions modules/admin/src/ngrest/base/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use yii\base\InvalidConfigException;
use luya\admin\ngrest\NgRest;
use luya\admin\ngrest\render\RenderCrud;
use luya\helpers\FileHelper;

/**
* Base Controller for all NgRest Controllers.
Expand Down Expand Up @@ -60,18 +61,6 @@ public function actionIndex($inline = false, $relation = false, $arrayIndex = fa
$apiEndpoint = $this->model->ngRestApiEndpoint();

$config = $this->model->getNgRestConfig();

/*
// partial concept integration for
$configClass = $this->module->getLinkedNgRestConfig($apiEndpoint);
if ($configClass) {
// $class = Yii::createObject($configClass, ['apiEndpoint' => '', 'primaryKey' => '..']);
$config = false;
} else {
$config = $this->model->getNgRestConfig();
}
*/

if (!$config) {
throw new Exception("Provided NgRest config for controller '' is invalid.");
Expand All @@ -81,34 +70,39 @@ public function actionIndex($inline = false, $relation = false, $arrayIndex = fa
$config->relationCall = ['id' => $relation, 'arrayIndex' => $arrayIndex, 'modelClass' => $modelClass];
}

// apply config informations
$config->filters = $this->model->ngRestFilters();
$config->defaultOrder = $this->model->ngRestListOrder();

$userSortSettings = Yii::$app->adminuser->identity->setting->get('ngrestorder.admin/'.$apiEndpoint, false);

if ($userSortSettings && is_array($userSortSettings)) {
$config->defaultOrder = [$userSortSettings['field'] => $userSortSettings['sort']];
$config->defaultOrder = [$userSortSettings['field'] => $userSortSettings['sort']];
}

$config->attributeGroups = $this->model->ngRestAttributeGroups();
$config->groupByField = $this->model->ngRestGroupByField();

$rel = [];
foreach ($this->model->ngRestRelations() as $key => $item) {
$rel[] = ['label' => $item['label'], 'apiEndpoint' => $item['apiEndpoint'], 'arrayIndex' => $key, 'modelClass' => base64_encode($this->model->className())];
}

$config->inline = (int) $inline;
$config->relations = $rel;
$config->tableName = $this->model->tableName();

$ngrest = new NgRest($config);

$crud = new RenderCrud();
if ($relation) {
$crud->viewFile = '@admin/views/ngrest/render/crud_relation.php';
}
return $ngrest->render($crud);
}



public function actionExportDownload($key)
{
$sessionkey = Yii::$app->session->get('tempNgRestKey');
$fileName = Yii::$app->session->get('tempNgRestFileName');

if ($sessionkey !== base64_decode($key)) {
throw new Exception('Invalid Export download key.');
}

$content = FileHelper::getFileContent('@runtime/'.$sessionkey.'.tmp');

Yii::$app->session->remove('tempNgRestKey');
Yii::$app->session->remove('tempNgRestFileName');
@unlink(Yii::getAlias('@runtime/'.$sessionkey.'.tmp'));

return Yii::$app->response->sendContentAsFile($content, $fileName . '-export-'.date("Y-m-d-H-i").'.csv', ['mimeType' => 'application/csv']);
}
}
27 changes: 26 additions & 1 deletion modules/admin/src/ngrest/base/NgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,39 @@ public function ngRestConfigDefine(ConfigBuilder $config, $assignedType, array $
public function getNgRestConfig()
{
if ($this->_config == null) {
$config = Yii::createObject(['class' => Config::class, 'apiEndpoint' => static::ngRestApiEndpoint(), 'primaryKey' => $this->getNgRestPrimaryKey()]);
//$config = Yii::createObject(['class' => Config::class, 'apiEndpoint' => static::ngRestApiEndpoint(), 'primaryKey' => $this->getNgRestPrimaryKey()]);

$config = new Config();
$config->apiEndpoint = static::ngRestApiEndpoint();
$config->primaryKey = $this->getNgRestPrimaryKey();

$configBuilder = new ConfigBuilder(static::class);
$this->ngRestConfig($configBuilder);
$config->setConfig($configBuilder->getConfig());
foreach ($this->i18n as $fieldName) {
$config->appendFieldOption($fieldName, 'i18n', true);
}



// COPY FROM NgRestController to Builder of the config
// ensure what must be removed and added from outside
$config->filters = $this->ngRestFilters();
$config->defaultOrder = $this->ngRestListOrder();


$config->attributeGroups = $this->ngRestAttributeGroups();
$config->groupByField = $this->ngRestGroupByField();

$rel = [];
foreach ($this->ngRestRelations() as $key => $item) {
$rel[] = ['label' => $item['label'], 'apiEndpoint' => $item['apiEndpoint'], 'arrayIndex' => $key, 'modelClass' => base64_encode($this->model->className())];
}


$config->relations = $rel;
$config->tableName = $this->tableName();

$this->_config = $config;
}

Expand Down
10 changes: 7 additions & 3 deletions modules/admin/src/ngrest/render/RenderCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ public function render()
'canDelete' => $this->can(Auth::CAN_DELETE),
//'crud' => $this,
'config' => $this->config,
'activeWindowCallbackUrl' => 'admin/ngrest/callback',
'activeWindowRenderUrl' => $this->getRestUrl('active-window-render'),
'activeWindowCallbackUrl' => $this->getRestUrl('active-window-callback'),
), $this);
}

public function getRestUrl()
public function getRestUrl($append = null)
{
return 'admin/'.$this->config->apiEndpoint;
if ($append) {
$append = '/' . ltrim($append, '/');
}
return 'admin/'.$this->config->apiEndpoint . $append;
}

public function getPrimaryKey()
Expand Down
2 changes: 1 addition & 1 deletion modules/admin/src/resources/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
}

$scope.getActiveWindow = function (activeWindowId, id, $event) {
$http.post('admin/ngrest/render', $.param({ itemId : id, activeWindowHash : activeWindowId , ngrestConfigHash : $scope.config.ngrestConfigHash }), {
$http.post($scope.config.activeWindowRenderUrl, $.param({ itemId : id, activeWindowHash : activeWindowId , ngrestConfigHash : $scope.config.ngrestConfigHash }), {
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
})
.success(function(data) {
Expand Down
1 change: 1 addition & 0 deletions modules/admin/src/views/ngrest/render/crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$scope.config.update = <?php echo $this->context->getFieldsJson('update'); ?>;
$scope.config.ngrestConfigHash = '<?php echo $config->hash; ?>';
$scope.config.activeWindowCallbackUrl = '<?php echo $activeWindowCallbackUrl; ?>';
$scope.config.activeWindowRenderUrl = '<?= $activeWindowRenderUrl; ?>';
$scope.config.pk = '<?php echo $this->context->getPrimaryKey(); ?>';
$scope.config.inline = <?= (int) $config->inline; ?>;
$scope.config.orderBy = '<?= $config->getDefaultOrderDirection() . $config->getDefaultOrderField(); ?>';
Expand Down

0 comments on commit 13510d3

Please sign in to comment.