Skip to content

Commit

Permalink
Refactor for less duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
flack committed Apr 6, 2015
1 parent 213f253 commit b6ea2f2
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 147 deletions.
5 changes: 2 additions & 3 deletions src/midcom/datamanager/extension/schemaextension.php
Expand Up @@ -19,15 +19,14 @@ protected function loadTypes()
{
return array
(
new type\attachment,
new type\autocomplete,
new type\blobs,
new type\codemirror,
new type\downloads,
new type\image,
new type\images,
new type\jsdate,
new type\photo,
new type\radiocheckselect,
new type\subform,
new type\select,
new type\tinymce,
new type\toolbar,
Expand Down
Expand Up @@ -7,12 +7,11 @@

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use midcom\datamanager\extension\listener\attachment as listener;

/**
* Experimental attachment type
*/
class attachment extends AbstractType
class blobs extends AbstractType
{
/**
* {@inheritdoc}
Expand All @@ -26,6 +25,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)

public function getName()
{
return 'attachment';
return 'blobs';
}
}
63 changes: 0 additions & 63 deletions src/midcom/datamanager/extension/type/image.php

This file was deleted.

74 changes: 11 additions & 63 deletions src/midcom/datamanager/extension/type/images.php
Expand Up @@ -14,61 +14,28 @@
use DateTime;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use midcom\datamanager\extension\transformer\blobs as transformer;
use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Validator\Constraints\Count;

/**
* Experimental images type
* Experimental image type
*/
class images extends CollectionType
class images extends AbstractType
{
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setDefaults(array
(
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__name__',
'type' => 'image',
'options' => array('required' => false), //@todo no idea why this is necessary
'delete_empty' => true,
'error_bubbling' => false
));
$resolver->setNormalizers(array
(
'type_config' => function (Options $options, $value)
'widget_config' => function (Options $options, $value)
{
$widget_defaults = array
(
'sortable' => false,
'max_count' => 0
'map_action_elements' => false,
'show_title' => true
);
return helper::resolve_options($widget_defaults, $value);
},
'constraints' => function (Options $options, $value)
{
$validation = array();
if ($options['type_config']['max_count'] > 0)
{
$validation['max'] = $options['type_config']['max_count'];
}
if ($options['required'])
{
$validation['min'] = 1;
}
if (!empty($validation))
{
return array(new Count($validation));
}
return $validation;
}
));
}

Expand All @@ -77,23 +44,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);

$builder->addViewTransformer(new transformer($options));
$builder->addEventSubscriber(new ResizeFormListener('image'));

$head = midcom::get()->head;
$head->enable_jquery();
$head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.datamanager/subform.js');
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);
$view->vars['max_count'] = $options['type_config']['max_count'];
$builder->add('file', 'file');
if ($options['widget_config']['show_title'])
{
$builder->add('title', 'text');
}
$builder->add('identifier', 'hidden', array('data' => 'file'));
}

/**
Expand All @@ -103,12 +59,4 @@ public function getName()
{
return 'images';
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'form';
}
}
Expand Up @@ -20,9 +20,9 @@
use Symfony\Component\Validator\Constraints\Count;

/**
* Experimental downloads type
* Experimental images type
*/
class downloads extends CollectionType
class subform extends CollectionType
{
/**
* {@inheritdoc}
Expand All @@ -36,13 +36,16 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__name__',
'type' => 'attachment',
'options' => array('required' => false), //@todo no idea why this is necessary
'delete_empty' => true,
'error_bubbling' => false
));
$resolver->setNormalizers(array
(
'type' => function (Options $options, $value)
{
return $options['dm2_type'];
},
'type_config' => function (Options $options, $value)
{
$widget_defaults = array
Expand Down Expand Up @@ -80,7 +83,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
parent::buildForm($builder, $options);

$builder->addViewTransformer(new transformer($options));
$builder->addEventSubscriber(new ResizeFormListener('attachment'));
$builder->addEventSubscriber(new ResizeFormListener($options['type']));

$head = midcom::get()->head;
$head->enable_jquery();
Expand All @@ -101,7 +104,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
*/
public function getName()
{
return 'downloads';
return 'subform';
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/midcom/datamanager/schema.php
Expand Up @@ -115,6 +115,16 @@ private function resolve_field_options(array $config, $name)
'end_fieldset' => null
));

$normalize_widget = function (Options $options, $value) use ($name)
{
if ( $value == 'images'
|| $value == 'downloads')
{
return 'subform';
}
return $value;
};

$normalize_storage = function (Options $options, $value) use ($name)
{
$default = array
Expand Down Expand Up @@ -160,7 +170,8 @@ private function resolve_field_options(array $config, $name)

$resolver->setNormalizers(array
(
'storage' => $normalize_storage
'storage' => $normalize_storage,
'widget' => $normalize_widget
));

return $resolver->resolve($config);
Expand Down
13 changes: 4 additions & 9 deletions src/midcom/datamanager/template/form.php
Expand Up @@ -117,7 +117,7 @@ public function toolbar_row(FormView $view, array $data)
return $string . '</div>';
}

public function image_row(FormView $view, array $data)
public function images_row(FormView $view, array $data)
{
$string = '<fieldset' . $this->renderer->block($view, 'widget_container_attributes') . '>';
$string .= '<legend>';
Expand All @@ -127,7 +127,7 @@ public function image_row(FormView $view, array $data)
return $string . '</fieldset>';
}

public function attachment_row(FormView $view, array $data)
public function blobs_row(FormView $view, array $data)
{
$string = '<fieldset' . $this->renderer->block($view, 'widget_container_attributes') . '>';
$string .= '<legend>';
Expand Down Expand Up @@ -330,7 +330,7 @@ public function jsdate_widget(FormView $view, array $data)
return $string . '</fieldset>';
}

public function image_widget(FormView $view, array $data)
public function images_widget(FormView $view, array $data)
{
$string = '<div' . $this->renderer->block($view, 'widget_container_attributes') . '>';
$string .= '<table><tr><td>';
Expand Down Expand Up @@ -403,12 +403,7 @@ public function photo_widget(FormView $view, array $data)
return $string . '</td></tr></table></div>';
}

public function images_widget(FormView $view, array $data)
{
return $this->downloads_widget($view, $data);
}

public function downloads_widget(FormView $view, array $data)
public function subform_widget(FormView $view, array $data)
{
$view->vars['attr']['data-prototype'] = $this->escape($this->renderer->row($view->vars['prototype']));
$view->vars['attr']['data-max-count'] = $view->vars['max_count'];
Expand Down

0 comments on commit b6ea2f2

Please sign in to comment.