Skip to content

Commit

Permalink
Merge pull request #7 from bluzphp/develop
Browse files Browse the repository at this point in the history
Created `api/options` endpoint
  • Loading branch information
Anton authored Sep 29, 2017
2 parents b7fa4d0 + e0b0bec commit f2e8055
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 43 deletions.
26 changes: 23 additions & 3 deletions application/models/Options/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,33 @@

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 +46,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 +78,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);
};
42 changes: 25 additions & 17 deletions application/modules/options/views/crud.phtml
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
<?php /** @var \Application\Options\Row $row */ ?>
<?php $uid = uniqid('form_'); ?>
<?php $uid = uniqid('form_', false); ?>
<form id="<?=$uid?>" action="<?=$this->url('options', 'crud')?>" class="form-horizontal ajax" method="POST">
<input type="hidden" name="_method" value="<?=$method?>"/>
<?php if ($method != 'POST') : ?>
<?php if ($method !== 'POST') : ?>
<input type="hidden" name="namespace" value="<?=esc($row->namespace)?:'default'?>"/>
<input type="hidden" name="key" value="<?=esc($row->key)?>"/>
<?php endif; ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title"><?=__('Options')?></h4>
<h4 class="modal-title"><?=__('Options')?></h4>
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
<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 />
<?php if ($method === 'PUT') :?>
<div class="form-group row">
<label class="control-label col-lg-4" for="author"><?=__('Author')?></label>
<div class="col-lg-8">
<input type="text" class="form-control" id="author" name="author" value="<?=\Application\Users\Table::findRow($row->userId)->login;?>" disabled />
</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':''?> />
<?php endif; ?>
<div class="form-group row">
<label class="control-label col-lg-4" for="namespace"><?=__('Namespace')?></label>
<div class="col-lg-8">
<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="value"><?=__('Key value')?></label>
<div class="col-lg-10">
<div class="form-group row">
<label class="control-label col-lg-4" for="key"><?=__('Key name')?></label>
<div class="col-lg-8">
<input type="text" class="form-control" id="key" name="key" value="<?=esc($row->key)?>" <?=($method !== 'POST')?'disabled':''?> />
</div>
</div>
<div class="form-group row">
<label class="control-label col-lg-4" for="value"><?=__('Key value')?></label>
<div class="col-lg-8">
<?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: ?>
<input type="text" class="form-control" id="value" name="value" value="<?=esc($row->value)?>" />
<?php endif; ?>
</div>
</div>
<div class="form-group">
<div class="form-group row">
<div class="col-lg-12">
<label class="control-label" for="description"><?=__('Description')?></label>
<textarea class="form-control" id="description" name="description"><?=esc($row->description)?></textarea>
Expand Down
31 changes: 11 additions & 20 deletions application/modules/options/views/grid.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,33 @@ if (!$grid) return;
// use data-spy="grid" for use AJAX for reload grid
require(['bluz.grid']);
</script>

<nav class="navbar navbar-default" role="navigation">
<div class="col-lg-6">
<a href="<?=$this->url('options', 'crud')?>" class="btn btn-primary navbar-btn dialog" data-ajax-method="get"><?=__('Create')?></a>
</div>
<div class="col-lg-6">

</div>
<nav class="navbar navbar-light bg-light">
<a href="<?=$this->url('options', 'crud')?>" class="btn btn-primary navbar-btn dialog" data-ajax-method="get"><?=__('Create')?></a>
</nav>

<hr/>
<?=$this->partial('grid/total.phtml', ['grid'=>$grid]) ?>
<?=$this->partial('grid/limit.phtml', ['grid'=>$grid]) ?>

<table class="table table-striped table-hover">
<thead>
<tr>
<th width="80px"><a href="<?=$grid->order('namespace')?>"><?=__('Space')?></a></th>
<th width="160px"><a href="<?=$grid->order('key')?>"><?=__('Key')?></a></th>
<th><a href="<?=$grid->order('value')?>"><?=__('Value')?></a></th>
<th width="160px"><a href="<?=$grid->order('created')?>"><?=__('Created')?></a></th>
<th width="160px"><a href="<?=$grid->order('updated')?>"><?=__('Updated')?></a></th>
<th width="72px"></th>
<th width="180px"><a href="<?=$grid->order('created')?>"><?=__('Created')?></a></th>
<th width="180px"><a href="<?=$grid->order('updated')?>"><?=__('Updated')?></a></th>
<th width="120px"></th>
</tr>
</thead>
<tbody>
<?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="badge badge-info"><?=$row['namespace']?></span></td>
<td><span 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 All @@ -66,9 +59,7 @@ if (!$grid) return;
<?php endforeach; ?>
</tbody>
</table>

<?=$this->partial('grid/empty-rows.phtml', ['grid'=>$grid]) ?>
<?=$this->partial('grid/pagination.phtml', ['grid'=>$grid]) ?>
<?=$this->partial('grid/total.phtml', ['grid'=>$grid]) ?>

</div>
</div>
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 f2e8055

Please sign in to comment.