Skip to content

Commit

Permalink
Create api/options endpoint
Browse files Browse the repository at this point in the history
Added `userId` to table
  • Loading branch information
Anton Shevchuk authored and Anton Shevchuk committed Sep 26, 2017
1 parent 27226dc commit da1c278
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 12 deletions.
27 changes: 24 additions & 3 deletions application/models/Options/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@

namespace Application\Options;

use Bluz\Proxy\Auth;
use Bluz\Validator\Traits\Validator;
use Application\Users;

/**
* Options Row
*
* @package Application\Options
*
* @property string $namespace
* @property string $key
* @property integer $userId
* @property string $value
* @property string $description
* @property string $created
* @property string $updated
*
* @category Application
* @package Options
* @SWG\Definition(definition="options", title="option", required={"namespace", "key"})
* @SWG\Property(property="namespace", type="string", description="Options namespace", example="default")
* @SWG\Property(property="key", type="string", description="Key", example="Some key")
* @SWG\Property(property="userId", type="integer", description="Author ID", example=2)
* @SWG\Property(property="value", type="string", description="Value", example="Some Value")
* @SWG\Property(property="description", type="string", description="Description", example="Some description for key")
* @SWG\Property(property="created", type="string", format="date-time", description="Created date",
* example="2017-03-17 19:06:28")
* @SWG\Property(property="updated", type="string", format="date-time", description="Last updated date",
* example="2017-03-17 19:06:28")
*/
class Row extends \Bluz\Db\Row
{
Expand All @@ -33,7 +47,7 @@ class Row extends \Bluz\Db\Row
protected function afterRead()
{
if ($this->value) {
$this->value = unserialize($this->value);
$this->value = unserialize($this->value, ['allowed_classes' => false]);
}
}

Expand Down Expand Up @@ -65,6 +79,13 @@ function () {
},
'Key name is already exists'
);

/* @var \Application\Users\Row $user */
if ($user = Auth::getIdentity()) {
$this->userId = $user->id;
} else {
$this->userId = Users\Table::SYSTEM_USER;
}
}

/**
Expand Down
83 changes: 83 additions & 0 deletions application/modules/api/controllers/options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* REST controller for Options model
*
* @author dev
* @created 2017-09-25 12:24:17
*/

/**
* @namespace
*/
namespace Application;

use Application\Options;
use Bluz\Controller\Controller;
use Bluz\Controller\Mapper\Rest;

/**
* Manipulate with options
*
* @SWG\Get(
* path="/api/options/{optionNamespace}-{optionKey}",
* tags={"options"},
* operationId="getOptionByKey",
* summary="Find option by namespace and key",
* @SWG\Parameter(ref="#/parameters/Auth-Token"),
* @SWG\Parameter(
* name="optionNamespace",
* in="path",
* type="string",
* required=true,
* description="Namespace of option"
* ),
* @SWG\Parameter(
* name="optionKey",
* in="path",
* type="string",
* required=true,
* description="Key of option"
* ),
* @SWG\Response(@SWG\Schema(ref="#/definitions/options"), response=200, description="Given option found"),
* @SWG\Response(@SWG\Schema(ref="#/definitions/error"), response=404, description="Page not found")
* )
*
* @SWG\Get(
* path="/api/options/",
* tags={"options"},
* method="GET",
* operationId="getOptionsCollection",
* summary="Collection of items",
* @SWG\Parameter(ref="#/parameters/Auth-Token"),
* @SWG\Parameter(ref="#/parameters/offset"),
* @SWG\Parameter(ref="#/parameters/limit"),
* @SWG\Response(response=200, description="Collection present"),
* @SWG\Response(response=206, description="Collection present")
* )
*
* @accept JSON
*
* @acl Options/Read
* @acl Options/Edit
*
* @return mixed
*/
return function () {
/**
* @var Controller $this
*/
$rest = new Rest(Options\Crud::getInstance());

$rest->get('system', 'rest/get')
->acl('Options/Read');
$rest->post('system', 'rest/post')
->acl('Options/Edit');
$rest->put('system', 'rest/put')
->acl('Options/Edit');
$rest->patch('system', 'rest/put')
->acl('Options/Edit');
$rest->delete('system', 'rest/delete')
->acl('Options/Edit');

return $rest->run();
};
4 changes: 2 additions & 2 deletions application/modules/options/controllers/crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
$crud = new Crud(Options\Crud::getInstance());

$crud->get('system', 'crud/get');
$crud->post('options', 'crud/post');
$crud->put('system', 'crud/put');
$crud->post('options', 'crud/post')->fields(['namespace', 'key', 'value', 'description']);
$crud->put('system', 'crud/put')->fields(['value', 'description']);
$crud->delete('system', 'crud/delete');

return $crud->run();
Expand Down
2 changes: 1 addition & 1 deletion application/modules/options/controllers/crud/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
try {
$crud->createOne($data);

Messages::addSuccess('Record was created');
Messages::addSuccess('The record was successfully created');

return [
'row' => $data,
Expand Down
1 change: 1 addition & 0 deletions application/modules/options/controllers/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
]
);
$grid = new Options\Grid();
$grid->processSource();

$this->assign('grid', $grid);
};
14 changes: 11 additions & 3 deletions application/modules/options/views/crud.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@
<h4 class="modal-title"><?=__('Options')?></h4>
</div>
<div class="modal-body">
<?php if ($method === 'PUT') :?>
<div class="form-group">
<label class="control-label col-lg-2" for="author"><?=__('Author')?></label>
<div class="col-lg-10">
<input type="text" class="form-control" id="author" name="author" value="<?=\Application\Users\Table::findRow($row->userId)->login;?>" disabled />
</div>
</div>
<?php endif; ?>
<div class="form-group">
<label class="control-label col-lg-2" for="namespace"><?=__('Namespace')?></label>
<div class="col-lg-10">
<input type="text" class="form-control" id="namespace" name="namespace" value="<?=esc($row->namespace)?:'default'?>" <?=($method != 'POST')?'disabled':''?> required />
<input type="text" class="form-control" id="namespace" name="namespace" value="<?=esc($row->namespace)?:'default'?>" <?=($method !== 'POST')?'disabled':''?> required />
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="key"><?=__('Key name')?></label>
<div class="col-lg-10">
<input type="text" class="form-control" id="key" name="key" value="<?=esc($row->key)?>" <?=($method!='POST')?'disabled':''?> />
<input type="text" class="form-control" id="key" name="key" value="<?=esc($row->key)?>" <?=($method !== 'POST')?'disabled':''?> />
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="value"><?=__('Key value')?></label>
<div class="col-lg-10">
<?php $value = $row->value; ?>
<?php if (is_array($value)) : ?>
<input type="text" class="form-control" id="value" name="value" disabled value="<?=__('Array (length=%d)', sizeof($value));?>"/>
<input type="text" class="form-control" id="value" name="value" disabled value="<?=__('Array (length=%d)', count($value));?>"/>
<?php elseif (is_object($value)): ?>
<input type="text" class="form-control" id="value" name="value" disabled value="<?=__('Object of `%s` class', get_class($value));?>"/>
<?php else: ?>
Expand Down
6 changes: 3 additions & 3 deletions application/modules/options/views/grid.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ if (!$grid) return;
<?php foreach($grid->getData() as $row) : ?>
<tr>
<td><span class="label label-info"><?=$row['namespace']?></span></td>
<td><a href="#" class="bluz-tooltip" data-toggle="tooltip" title="<?=esc($row['description'])?>"><?=esc($row['key'])?></a></td>
<td><span class="bluz-tooltip" data-toggle="tooltip" title="<?=esc($row['description'])?>"><?=esc($row['key'])?></span></td>
<td>
<?php
$value = unserialize($row['value']);
$value = unserialize($row['value'], ['allow_classes' => false]);
if (is_array($value)) {
echo __('Array (length=%d)', sizeof($value));
echo __('Array (length=%d)', count($value));
} elseif (is_object($value)) {
echo __('Object of `%s` class', get_class($value));
} else {
Expand Down
5 changes: 5 additions & 0 deletions data/migrations/20170317111112_module_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public function change()
{
$table = $this->table('options', ['id' => false, 'primary_key' => ['namespace', 'key']]);
$table
->addColumn('userId', 'integer')
->addColumn('namespace', 'string', ['length' => 255, 'default' => 'default'])
->addColumn('key', 'string', ['length' => 255])
->addColumn('value', 'text')
->addColumn('description', 'text', ['null' => true])
->addTimestamps('created', 'updated')
->addForeignKey('userId', 'users', 'id', [
'delete' => 'CASCADE',
'update' => 'CASCADE'
])
->create();

$data = [
Expand Down

0 comments on commit da1c278

Please sign in to comment.