Skip to content
Permalink
Browse files

(re-)add sortable support to image types

  • Loading branch information...
flack committed Mar 10, 2019
1 parent 54a1242 commit 693f5e15f1ef70091b4fc674cd10d5c18ea9d40c
@@ -17,11 +17,10 @@ public function transform($input)
if ($input === null) {
return [];
}
$result = ['objects' => []];
foreach ($input as $key => $value) {
if ($key === 'delete' || $key === 'description' || $key == 'title') {
if ($key === 'delete' || $key === 'description' || $key == 'title' || $key == 'score') {
$result[$key] = $value;
} else {
$result['objects'][$key] = parent::transform($value);
@@ -46,12 +45,16 @@ public function reverseTransform($array)
if (empty($array)) {
return null;
}
$result = [];
if ( !empty($array['file'])
|| !empty($array['identifier']) && substr($array['identifier'], 0, 8) === 'tmpfile-') {
$result['file'] = parent::reverseTransform($array);
} elseif (!empty($array['objects'])) {
foreach ($array['objects'] as $key => $value) {
if (array_key_exists('score', $array)) {
$value['score'] = $array['score'];
}
$result[$key] = parent::reverseTransform($value);
}
}
@@ -64,6 +67,9 @@ public function reverseTransform($array)
if (!empty($this->config['widget_config']['show_title'])) {
$result['title'] = $array['title'];
}
if (!empty($this->config['widget_config']['sortable'])) {
$result['score'] = $array['score'];
}
return $result;
}
}
@@ -34,7 +34,8 @@ public function configureOptions(OptionsResolver $resolver)
'widget_config' => [
'map_action_elements' => false,
'show_title' => true,
'show_description' => false
'show_description' => false,
'sortable' => false
],
'type_config' => [
'do_not_save_archival' => true,
@@ -68,6 +69,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ($options['widget_config']['show_description']) {
$builder->add('description', TextType::class, ['required' => false]);
}
if ($options['widget_config']['sortable']) {
$builder->add('score', HiddenType::class);
}
$builder->add('delete', CheckboxType::class, ['attr' => [
"class" => "midcom_datamanager_photo_checkbox"
], "required" => false ]);
@@ -23,7 +23,8 @@ public function configureOptions(OptionsResolver $resolver)
'widget_config' => [
'map_action_elements' => false,
'show_title' => false,
'show_description' => false
'show_description' => false,
'sortable' => false
],
'type_config' => [
'do_not_save_archival' => false,
@@ -106,15 +106,6 @@ public function save()
$attachment->delete();
}
if (!empty($this->config['widget_config']['sortable'])) {
uasort($this->map, function ($a, $b) {
if ($a->metadata->score == $b->metadata->score) {
return strnatcasecmp($a->name, $b->name);
}
return $b->metadata->score - $a->metadata->score;
});
}
return $this->save_attachment_list();
}
@@ -151,6 +142,15 @@ public function move_uploaded_files()
*/
protected function save_attachment_list()
{
if (!empty($this->config['widget_config']['sortable'])) {
uasort($this->map, function ($a, $b) {
if ($a->metadata->score == $b->metadata->score) {
return strnatcasecmp($a->name, $b->name);
}
return $b->metadata->score - $a->metadata->score;
});
}
$list = [];
foreach ($this->map as $identifier => $attachment) {
@@ -56,13 +56,25 @@ public function save()
$stat = $this->save_attachment_list();
}
if ( (array_key_exists('description', $this->value) || array_key_exists('title', $this->value))
&& $main = $this->get_main()) {
if (array_key_exists('description', $this->value)) {
$main->set_parameter('midcom.helper.datamanager2.type.blobs', 'description', $this->value['description']);
$check_fields = ['description', 'title', 'score'];
if (array_intersect_key(array_keys($check_fields), $this->value)) {
$main = $this->get_main();
$needs_update = false;
foreach ($check_fields as $field) {
if (array_key_exists($field, $this->value)) {
if ($field === 'description') {
$main->set_parameter('midcom.helper.datamanager2.type.blobs', 'description', $this->value['description']);
} elseif ($field === 'title') {
$needs_update = $needs_update || $main->title != $this->value['title'];
$main->title = $this->value['title'];
} elseif ($field === 'score') {
$needs_update = $needs_update || $main->metadata->score != $this->value['score'];
$main->metadata->score = (int) $this->value['score'];
}
}
}
if (array_key_exists('title', $this->value) && $main->title != $this->value['title']) {
$main->title = $this->value['title'];
if ($needs_update) {
$main->update();
}
}
@@ -45,15 +45,18 @@ public function load()
}
}
$map = [];
foreach ($identifiers as $item) {
list($identifier, $images_identifier, $images_name) = explode(':', $item);
if (array_key_exists($identifier, $results)) {
if (!array_key_exists($images_identifier, $grouped)) {
$grouped[$images_identifier] = [];
}
$grouped[$images_identifier][$images_name] = $results[$identifier];
$map[$identifier] = [$images_identifier, $images_name];
}
// we iterate over results since that takes sorting into account
foreach ($results as $identifier => $image) {
list($images_identifier, $images_name) = $map[$identifier];
if (!array_key_exists($images_identifier, $grouped)) {
$grouped[$images_identifier] = [];
}
$grouped[$images_identifier][$images_name] = $image;
}
return $grouped;
@@ -87,8 +90,12 @@ public function save()
$images['main']->title = $images['title'];
$images['main']->update();
}
if (!empty($this->config['widget_config']['sortable'])) {
$images['main']->update();
}
}
}
return $this->save_image_map($map) && $this->save_attachment_list();
}
@@ -486,6 +486,9 @@ public function image_widget(FormView $view, array $data)
$view->children['description']->vars['attr']['placeholder'] = $this->renderer->humanize('description');
$string .= $this->renderer->widget($view->children['description']);
}
if (array_key_exists('score', $view->children)) {
$string .= $this->renderer->widget($view->children['score']);
}
$string .= '</td></tr></table></div>';
$string .= $this->renderer->row($data['form']['identifier']);

0 comments on commit 693f5e1

Please sign in to comment.
You can’t perform that action at this time.