Skip to content

Commit

Permalink
Lisem Fix & Features (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
FanchTheSystem committed Jul 18, 2017
1 parent 284d4a3 commit 53d563c
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 5 deletions.
70 changes: 69 additions & 1 deletion src/Admin/CoreAdmin.php
Expand Up @@ -28,8 +28,10 @@
use Blast\CoreBundle\Admin\Traits\ManyToManyManager;
use Blast\CoreBundle\Admin\Traits\Actions;
use Blast\CoreBundle\Admin\Traits\ListActions;
use Blast\CoreBundle\CodeGenerator\CodeGeneratorRegistry;
use Symfony\Component\PropertyAccess\PropertyAccess;

abstract class CoreAdmin extends SonataAdmin
abstract class CoreAdmin extends SonataAdmin implements \JsonSerializable
{
use CollectionsManager,
ManyToManyManager,
Expand Down Expand Up @@ -457,4 +459,70 @@ public function removeAllFieldsFromFormGroup($groupName, $mapper)
}
}
}

public function jsonSerialize()
{
$propertiesToShow = [
'baseRouteName',
'baseRoutePattern',
'extraTemplates',
'listFieldDescriptions',
'showFieldDescriptions',
'formFieldDescriptions',
'filterFieldDescriptions',
'maxPerPage',
'maxPageLinks',
'classnameLabel',
'translationDomain',
'formOptions',
'datagridValues',
'perPageOptions',
'pagerType',
'code',
'label',
'routes',
'subject',
'children',
'parent',
'baseCodeRoute',
'uniqid',
'extensions',
'class',
'subClasses',
'list',
'show',
'form',
'filter',
'formGroups',
'formTabs',
'showGroups',
'showTabs',
'managedCollections',
'helperLinks',
'titles',
];

$properties = [];
foreach ($this as $key => $value) {
if (in_array($key, $propertiesToShow)) {
$properties[$key] = $value;
}
}

return $properties;
}

/**
* {@inheritdoc}
*/
public function prePersist($object)
{
$hasCodeGenerator = CodeGeneratorRegistry::hasGeneratorForClass(get_class($object));
if ($hasCodeGenerator) {
$accessor = PropertyAccess::createPropertyAccessor();
foreach (CodeGeneratorRegistry::getCodeGenerators(get_class($object)) as $name => $generator) {
$accessor->setValue($object, $name, $generator->generate($object));
}
}
}
}
63 changes: 61 additions & 2 deletions src/Admin/Traits/Mapper.php
Expand Up @@ -18,6 +18,8 @@
use Sonata\AdminBundle\Mapper\BaseMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Doctrine\ORM\QueryBuilder;

trait Mapper
{
Expand Down Expand Up @@ -290,8 +292,7 @@ protected function addContent(BaseMapper $mapper, $group)
$endgroup = $endtab = false;

// tab
if (!empty($tabcontent['_options']['hideTitle']) ||
$mapper instanceof ShowMapper && !$this->forceTabs) {
if (!empty($tabcontent['_options']['hideTitle']) || $mapper instanceof ShowMapper && !$this->forceTabs) {
// display tabs as groups
$tabs = $this->{$fcts['tabs']['getter']}();
$groups = $this->{$fcts['groups']['getter']}();
Expand All @@ -311,6 +312,18 @@ protected function addContent(BaseMapper $mapper, $group)
$mapper->tab($tab, isset($tabcontent['_options']) ? $tabcontent['_options'] : []);
$endtab = true;
}

// adding count of collections items in tab
if (isset($tabcontent['_options']['countChildItems']) && is_array($tabcontent['_options']['countChildItems'])) {
$tabs = $this->{$fcts['tabs']['getter']}();
$tabs[$tab]['class'] .= ' countable-tab';
foreach ($tabcontent['_options']['countChildItems'] as $fieldToCount) {
$tabs[$tab]['class'] .= ' count-' . $fieldToCount;
}
$this->{$fcts['tabs']['setter']}($tabs);
}

// clearing tabcontent options
if (isset($tabcontent['_options'])) {
unset($tabcontent['_options']);
}
Expand Down Expand Up @@ -466,6 +479,14 @@ protected function addField(BaseMapper $mapper, $name, $options = [], $fieldDesc
$options['constraints'] = [new NotBlank()];
}

if (isset($options['query'])) {
$this->manageQueryCallback($mapper, $options);
}

if (isset($options['choicesCallback'])) {
$this->manageChoicesCallback($mapper, $options);
}

// save-and-remove CoreBundle-specific options
$extras = [];
foreach ([
Expand Down Expand Up @@ -769,4 +790,42 @@ protected function getBaseRouteMapping()

return $baseRoute;
}

protected function manageQueryCallback($mapper, &$options)
{
$query = $options['query'];
$entityClass = $options['class'] ? $options['class'] : $this->getClass();

if (!$query instanceof QueryBuilder) {
if (!is_array($query)) {
throw new Exception('« query » option must be an array : ["FQDN"=>"static method name"]');
}

list($className, $methodName) = $query;

$queryFunction = call_user_func($className . '::' . $methodName, $this->getModelManager(), $entityClass);

$options['query'] = $queryFunction;
}
}

protected function manageChoicesCallback($mapper, &$options)
{
$callback = $options['choicesCallback'];
$entityClass = $options['class'] ? $options['class'] : $this->getClass();

if (!is_array($callback)) {
throw new Exception('« choicesCallback » option must be an array : ["FQDN"=>"static method name"]');
}

list($className, $methodName) = $callback;

$choicesFunction = call_user_func($className . '::' . $methodName, $this->getModelManager(), $entityClass);

$options['choices'] = $choicesFunction;
$options['choice_loader'] = new CallbackChoiceLoader(function () use ($options) {
return $options['choices'];
});
unset($options['choicesCallback']);
}
}
16 changes: 16 additions & 0 deletions src/CodeGenerator/CodeGeneratorRegistry.php
Expand Up @@ -54,6 +54,22 @@ public static function getCodeGenerator($entityClass, $entityField = 'code')
return self::$generators[$entityClass][$entityField];
}

/**
* Returns registred code generators for specifyed entity class.
*
* @param string $entityClass
*
* @return array
*/
public static function getCodeGenerators($entityClass)
{
if (!isset(self::$generators[$entityClass])) {
throw new \Exception("There is no registered entity code generator for class $entityClass");
}

return self::$generators[$entityClass];
}

/**
* @param string $entityClass
*
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/public/css/main.css
@@ -1,4 +1,5 @@

.sidebar-form div{ margin: 0 0 3px 0; }
.js-i18n, .js-data { display: none; }
.blast-table-label { margin: 6px 0 8px 0; }
.blast-table-label { margin: 6px 0 8px 0; }
.countable-tab span.counter {position: absolute;top:0;right:0;}
21 changes: 21 additions & 0 deletions src/Resources/public/js/deleteCheckboxCollection.js
@@ -0,0 +1,21 @@
$(document).on('ifChecked','.sonata-ba-field-inline-natural input[type="checkbox"], .sonata-ba-field-inline-table input[type="checkbox"]',function(e) {
var input = $(this);
var name = input.attr('name');

if(name.match(/\[[0-9]*\]\[_delete\]/)) {

var formRow = input.closest('div.sonata-ba-tabs>div');

if(formRow.length == 0) {
formRow = input.closest('tr');
}

var r = confirm(Translator.trans('librinfo.confirm.delete_collection_item', {}, 'messages'));
if (r == true) {
formRow.remove();
} else {
// throw exception to avoid checkbox checking
e.dummy();
}
}
});
44 changes: 44 additions & 0 deletions src/Resources/public/js/tabNavigation.js
Expand Up @@ -3,4 +3,48 @@ $(document).ready(function() {
var tabName = $(this).attr('data-gototab');
$('li[data-tab-name="' + tabName + '"] a').trigger('click');
});

var countCollectionsInTabs = function() {
var countableTabs = $('.countable-tab');

countableTabs.each(function(index) {
var tab = $(this);
tab.data('currentCount', 0);

var countableClasses = $.grep(tab.attr("class").split(' '), function(v) {
return v.match(/count-.*/);
});

$.each(countableClasses, function(i, cls) {
var fieldName = cls.replace('count-', '');
var fieldId = 'field_widget_' + Admin.currentAdmin.uniqid + '_' + fieldName;

var collectionAsForm = $('#' + fieldId + ' > div.sonata-ba-tabs > div');
var collectionAsTable = $('#' + fieldId + ' > table > tbody > tr');

tab.data('currentCount', parseInt(tab.data('currentCount'), 10) + (
parseInt(collectionAsForm.length, 10) +
parseInt(collectionAsTable.length, 10)
));
});

var counterItem = tab.find('a > span.counter');

if (counterItem.length == 0) {
counterItem = tab.find('a').append(
$('<span/>').attr({
'class': 'counter'
})
).find('.counter');
}

counterItem.html(tab.data('currentCount')).attr('data-count',tab.data('currentCount'));
});
};

countCollectionsInTabs();

$(document).on('sonata.add_element',function() {
countCollectionsInTabs();
});
});
2 changes: 2 additions & 0 deletions src/Resources/translations/messages.fr.yml
Expand Up @@ -4,6 +4,8 @@ librinfo:
_action: 'Action'
_list_action: 'Action groupée'
generate_code: 'Générer le code'
confirm:
delete_collection_item: 'Voulez-vous vraiment supprimer cet élément ?'
main_tab.main_group: 'Général'
main_tab: 'Général'
'main_group': 'Général'
Expand Down
6 changes: 5 additions & 1 deletion src/Resources/views/CRUD/base_edit.html.twig
Expand Up @@ -73,7 +73,7 @@
<div class="nav-tabs-custom">
<ul class="nav nav-tabs" role="tablist">
{% for name, form_tab in admin.formtabs %}
<li data-tab-name="{{form_tab.name}}" {% if loop.index == 1 %} class="active"{% endif %}><a href="#tab_{{ admin.uniqid }}_{{ loop.index }}" data-toggle="tab"><i class="fa fa-exclamation-circle has-errors hide" aria-hidden="true"></i> {{ name|trans({}, form_tab.translation_domain ?: admin.translationDomain) }}</a></li>
<li data-tab-name="{{form_tab.name}}" class="{% if loop.index == 1 %}active {% endif %}{{form_tab.class}}"><a href="#tab_{{ admin.uniqid }}_{{ loop.index }}" data-toggle="tab"><i class="fa fa-exclamation-circle has-errors hide" aria-hidden="true"></i> {{ name|trans({}, form_tab.translation_domain ?: admin.translationDomain) }}</a></li>
{% endfor %}
</ul>
<div class="tab-content">
Expand Down Expand Up @@ -149,6 +149,10 @@
</div>
{% endblock formactions %}
</form>

<script>
Admin.currentAdmin = {{admin|json_encode|raw}};
</script>
{% endif%}

{{ sonata_block_render_event('sonata.admin.edit.form.bottom', { 'admin': admin, 'object': object }) }}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/views/standard_layout.html.twig
Expand Up @@ -76,4 +76,5 @@ file that was distributed with this source code.

<script src="{{ asset('bundles/blastcore/js/globalSearch.js') }}"></script>
<script src="{{ asset('bundles/blastcore/js/tabNavigation.js') }}"></script>
<script src="{{ asset('bundles/blastcore/js/deleteCheckboxCollection.js') }}"></script>
{% endblock javascripts %}

0 comments on commit 53d563c

Please sign in to comment.