Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/FriendsOfCake/crud-view
Browse files Browse the repository at this point in the history
* 'master' of git://github.com/FriendsOfCake/crud-view:
  wrapping the calendar icon in a label instead of span
  Correctly redirect after saving entity
  Rename scaffold.mergeFields to scaffold.autoFields
  Add new config "scaffold.mergeFields".
  Fixed defining actions
  Corrected misspellings and clarified method usage
  Fix not-instanceof check
  Fixed improve this doc URL to correct repo
  Fixed confg typo to config
  Pass input values as options to Form Helper
  Allow overrideing "role" attr for DateTimeWidget
  Remove duplicate ID in page title
  Add configure value for time zone aware DateTimeWidget
  Brought back browser timezone, but now (almost) configurable
  Bumped datetimepicker version in asset_compress.ini as well
  Bumped bootstrap-datetimepicker version
  Stop using locale_get_primary_language() on locale
  Stop using timestamp to pass date to datepicker
  Allow disabling contain of all relations by passing false to 'scaffold.relations'
  • Loading branch information
jippi committed Dec 1, 2015
2 parents 16476a7 + 876f68b commit 8719787
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 32 deletions.
4 changes: 2 additions & 2 deletions config/asset_compress.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[crudview.css]
files[]=https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css
files[]=https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.14.30/css/bootstrap-datetimepicker.min.css
files[]=https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css
files[]=https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/css/selectize.bootstrap3.min.css
files[]=plugin:CrudView:css/local.css

[crudview_head.js]
files[]=https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js
files[]=https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js
files[]=https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment-with-locales.min.js
files[]=https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.14.30/js/bootstrap-datetimepicker.min.js
files[]=https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js
files[]=https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/js/standalone/selectize.js
files[]=https://cdn.jsdelivr.net/jquery.dirtyforms/1.2.2/jquery.dirtyforms.min.js

Expand Down
8 changes: 4 additions & 4 deletions config/defaults.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

return [
'CrudView' => [
'brand' => 'Crud View',
'css' => [
'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css',
'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.14.30/css/bootstrap-datetimepicker.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/css/selectize.bootstrap3.min.css',
'CrudView.local'
],
Expand All @@ -14,13 +13,14 @@
'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment-with-locales.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.14.30/js/bootstrap-datetimepicker.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.1/js/standalone/selectize.js',
'https://cdn.jsdelivr.net/jquery.dirtyforms/1.2.2/jquery.dirtyforms.min.js'
],
'script' => [
'CrudView.local'
]
]
],
'timezoneAwareDateTimeWidget' => false,
]
];
2 changes: 1 addition & 1 deletion docs/_themes/cakephp/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<div class="document columns nine push-three">
<div class="body">
{%- if pagename != 'search' -%}
<a class="button pale improve" href="https://github.com/friendsofcake/crud/edit/{{ branch }}/docs/{{ pagename }}.rst" target="_blank">Improve this Doc</a>
<a class="button pale improve" href="https://github.com/friendsofcake/crud-view/edit/{{ branch }}/docs/{{ pagename }}.rst" target="_blank">Improve this Doc</a>
{%- endif -%}
{% block body %} {% endblock %}
</div>
Expand Down
36 changes: 33 additions & 3 deletions docs/customizing-templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,39 @@ Despite ``CrudView`` being quite smart at guessing how to display your data and
having great defaults, it is very often that you need to customize the look and
feel of your Admin application.


Formatting fields
-----------------

The easiest way to modify your fields is to pass options in the ``scaffold.fields``
configuration key. ``CrudView`` makes use of the ``FormHelper::inputs()`` method
and will pass your array values as options when generating the fields. You can
pass any properties that ``FormHelper::inputs()`` supports.

.. code-block:: php
<?php
...
class ArticlesController extends AppController
{
public function index()
{
$action = $this->Crud->action();
$action->config('scaffold.fields', [
'title',
'thread_id' => [
'type' => 'text'
],
'featured' => [
'checked' => 'checked'
]
]);
return $this->Crud->execute();
}
}
Formating using a Formatter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The most immediate changes you can do in the way data is displayed is by
applying formatters to any of your fields. Whenever you use the
``scaffold.fields`` configuration key, you can specify a formatter to be used.
Expand All @@ -21,7 +51,7 @@ applying formatters to any of your fields. Whenever you use the
public function index()
{
$action = $this->Crud->action();
$action->confg('scaffold.fields', [
$action->config('scaffold.fields', [
'title',
'published_time' => [
'formatter' => function ($name, Time $value) {
Expand Down Expand Up @@ -130,7 +160,7 @@ In your ``index()`` action use the ``scaffold.fields`` configuration to set the
public function index()
{
$action = $this->Crud->action();
$action->confg('scaffold.fields', [
$action->config('scaffold.fields', [
'author_id' => ['title' => 'Author Name'],
... // The rest of the fields to display here
]);
Expand All @@ -153,7 +183,7 @@ title for any of the fields by using the ``scaffold.fields`` configuration
public function add()
{
$action = $this->Crud->action();
$action->confg('scaffold.fields', [
$action->config('scaffold.fields', [
'author_id' => ['label' => 'Author Name'],
... // The rest of the fields to display here
]);
Expand Down
27 changes: 19 additions & 8 deletions src/Listener/ViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ protected function _getRelatedModels($relations = [])
{
$models = $this->_action()->config('scaffold.relations');

if ($models === false) {
return [];
}

if (empty($models)) {
$associations = $this->_associations();

Expand Down Expand Up @@ -250,7 +254,7 @@ protected function _getPageTitle()
}

$displayFieldValue = $this->_displayFieldValue();
if ($displayFieldValue === null) {
if ($displayFieldValue === null || $this->_table()->displayField() == $this->_table()->primaryKey()) {
return sprintf('%s %s #%s', $actionName, $controllerName, $primaryKeyValue);
}

Expand All @@ -266,21 +270,25 @@ protected function _getPageTitle()
protected function _scaffoldFields(array $associations = [])
{
$action = $this->_action();
$configuredFields = $action->config('scaffold.fields');
if (!empty($configuredFields)) {
$scaffoldFields = Hash::normalize($configuredFields);
} else {
$scaffoldFields = (array)$action->config('scaffold.fields');
if (!empty($scaffoldFields)) {
$scaffoldFields = Hash::normalize($scaffoldFields);
}

if (empty($scaffoldFields) || $action->config('scaffold.autoFields')) {
$cols = $this->_table()->schema()->columns();
$scaffoldFields = Hash::normalize($cols);
$cols = Hash::normalize($cols);

$scope = $action->config('scope');
if ($scope === 'entity' && !empty($associations['manyToMany'])) {
foreach ($associations['manyToMany'] as $alias => $options) {
$scaffoldFields[sprintf('%s._ids', $options['entities'])] = [
$cols[sprintf('%s._ids', $options['entities'])] = [
'multiple' => true
];
}
}

$scaffoldFields = array_merge($cols, $scaffoldFields);
}

// Check for blacklisted fields
Expand Down Expand Up @@ -378,7 +386,10 @@ protected function _getControllerActions()
protected function _getAllowedActions()
{
$actions = $this->_action()->config('scaffold.actions');
$actions = ($actions === null) ? $this->_crud()->config('actions') : [];
if ($actions === null) {
$actions = $this->_crud()->config('actions');
}

$extraActions = $this->_action()->config('scaffold.extra_actions') ?: [];

$allActions = array_merge(
Expand Down
6 changes: 3 additions & 3 deletions src/Template/Element/form.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
echo $this->Form->button(__d('crud', 'Save'), ['class' => 'btn btn-primary', 'name' => '_save']);
if (empty($disableExtraButtons)) {
if (!in_array('save_and_continue', $extraButtonsBlacklist)) {
echo $this->Form->button(__d('crud', 'Save & continue editing'), ['class' => 'btn btn-success btn-save-continue', 'name' => '_edit']);
echo $this->Form->button(__d('crud', 'Save & continue editing'), ['class' => 'btn btn-success btn-save-continue', 'name' => '_edit', 'value' => true]);
}
if (!in_array('save_and_create', $extraButtonsBlacklist)) {
echo $this->Form->button(__d('crud', 'Save & create new'), ['class' => 'btn btn-success', 'name' => '_add']);
echo $this->Form->button(__d('crud', 'Save & create new'), ['class' => 'btn btn-success', 'name' => '_add', 'value' => true]);
}
if (!in_array('back', $extraButtonsBlacklist)) {
echo $this->Html->link(__d('crud', 'Back'), ['action' => 'index'], ['class' => 'btn btn-default', 'role' => 'button']);
echo $this->Html->link(__d('crud', 'Back'), ['action' => 'index'], ['class' => 'btn btn-default', 'role' => 'button', 'value' => true]);
}
}
?>
Expand Down
25 changes: 20 additions & 5 deletions src/View/Widget/DateTimeWidget.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace CrudView\View\Widget;

use Cake\Core\Configure;
use Cake\I18n\I18n;
use Cake\I18n\Time;
use Cake\View\Form\ContextInterface;
Expand All @@ -24,20 +25,27 @@ public function render(array $data, ContextInterface $context)
$val = $data['val'];
$type = $data['type'];
$required = $data['required'] ? 'required' : '';
$role = isset($data['role']) ? $data['role'] : 'datetime-picker';
$format = null;
$locale = I18n::locale();

$timezoneAware = Configure::read('CrudView.timezoneAwareDateTimeWidget');

$timestamp = null;
$locale = locale_get_primary_language(I18n::locale());
$timezoneOffset = null;

if (isset($data['data-format'])) {
$format = $this->_convertPHPToMomentFormat($data['data-format']);
}

if (!$val instanceof DateTime && !empty($val)) {
if (!($val instanceof DateTime) && !empty($val)) {
$val = $type === 'date' ? Time::parseDate($val) : Time::parseDateTime($val);
}

if ($val) {
$timestamp = $val->format('U');
$dateTimeZone = new \DateTimeZone(date_default_timezone_get());
$timezoneOffset = ($dateTimeZone->getOffset($val) / 60);
$val = $val->format($type === 'date' ? 'Y-m-d' : 'Y-m-d H:i:s');
}

Expand All @@ -53,15 +61,22 @@ class="form-control"
name="$name"
value="$val"
id="$id"
role="datetime-picker"
role="$role"
data-locale="$locale"
data-format="$format"
html;
if ($timezoneAware && isset($timestamp, $timezoneOffset)) {
$widget .= <<<html
data-timestamp="$timestamp"
data-timezone-offset="$timezoneOffset"
html;
}
$widget .= <<<html
$required
/>
<span class="input-group-addon">
<label for="$id" class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</label>
</div>
html;
return $widget;
Expand Down
24 changes: 18 additions & 6 deletions webroot/js/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,26 @@ $(document).on('ready', function() {
}

$('[role=datetime-picker]').each(function() {
var date = new Date($(this).data('timestamp') * 1000);
if ($(this).data('timestamp') === '') {
date = '' ;
};
$(this).datetimepicker({

var picker = $(this);
var date = null;

if (picker.data('timestamp') && picker.data('timezone-offset')) {
var timezoneOffset = picker.data('timezone-offset');
date = new Date(picker.data('timestamp') * 1000);

picker.parents('form').on('submit', function () {
var timezoneDiff = timezoneOffset + date.getTimezoneOffset();
var currentDate = picker.data('DateTimePicker').date();
var convertedDate = currentDate.add(timezoneDiff, 'minutes');
picker.data('DateTimePicker').date(convertedDate);
});
}

picker.datetimepicker({
locale: $(this).data('locale'),
format: $(this).data('format'),
date: date
date: date ? date : picker.val()
});
});

Expand Down

0 comments on commit 8719787

Please sign in to comment.