<commit_before><?php

namespace Honeybee\Ui\Renderer\Html\Honeybee;

use Honeybee\Ui\Renderer\Renderer;
use Honeybee\Ui\Renderer\EntityRenderer;
use Honeybee\Common\Error\RuntimeError;
use Honeybee\Common\Util\ArrayToolkit;
use Honeybee\EntityInterface;
use Trellis\Runtime\Attribute\AttributePath;
use Trellis\Runtime\Attribute\AttributeValuePath;
use Trellis\Runtime\Attribute\AttributePathInterface;
use Trellis\Runtime\Attribute\Text\TextAttribute;
use Trellis\Runtime\Attribute\Textarea\TextareaAttribute;
use Trellis\Runtime\Attribute\HandlesFileListInterface;
use Trellis\Runtime\Attribute\HandlesFileInterface;

class HtmlEntityGlanceRenderer extends EntityRenderer
{
    protected function getDefaultTemplateIdentifier()
    {
        return $this->hasOption('view_template_name')
            ? 'html/entity_glance/as_fields_with_viewtemplate_without_tabs.twig'    // view_template
            : 'html/entity_glance/entity-glance.twig';  // default
    }

    protected function getTemplateParameters()
    {
        $params = parent::getDefaultTemplateParameters();

        $resource = $this->getPayload('subject');
        $parent_attribute = $resource->getType()->getParentAttribute();
        $group_parts = (array)$this->getOption('group_parts', []);

        $params['grouped_base_path'] = ArrayToolkit::flattenToArrayPath($group_parts);
        $params['is_embed_template'] = $this->getOption('is_embed_template', false);
        $params['has_parent_attribute'] = $parent_attribute !== null;
        $params['html_attributes'] = $this->getOption('html_attributes', []);
        $params['resource'] = $resource->toNative();
        $params['is_new'] = !$resource->hasValue('identifier');
        $params['css'] = $this->getOption('css', '');

        if ($this->hasOption('view_template_name')) {
            // use view_template
            $params = array_replace_recursive($this->lookupViewTemplate(), $params);

            $params['rendered_fields'] = $this->getRenderedFields($resource, $params['view_template']);
            $params['css'] .= $params['is_new'] ? ' hb-glance--empty' : null;
        } else {
            // get default values
            $image = $this->getGlanceImage($resource);

            $params['image_width'] = $this->getOption('image_width', $image['width']);
            $params['image_height'] = $this->getOption('image_height', $image['height']);
            $params['image_url'] = $image['location'];
            $params['title'] = $this->getGlanceTitle($resource);
            $params['description'] = $this->getGlanceDescription($resource);
        }

        return $params;
    }

    protected function getGlanceImage(EntityInterface $resource)
    {
        $image_default_attributes = [
            'location' => '',
            'width' => 100,
            'height' => 100
        ];

        // @todo 'image_url' should allow to restore the auto-retriving without specifying 'image_value_path',
        //       in the case a value was already set for it in a less specific config.
        if ($this->hasOption('image_url')) {
            $image_url = $this->getOption('image_url');
            if (!empty($image_url) || !$this->hasOption('image_value_path')) {
                // empty value would reset eventual global options and allow to use the value_path option
                return array_replace($image_default_attributes, [ 'location' => $image_url ]);
            }
        }

        $converjon_enabled = (bool)$this->getOption('use_converjon', false);

        $image_activity = null;
        if ($converjon_enabled) {
            $image_activity = $this->activity_service->getActivity(
                $this->getOption('image_activity_scope', 'converjon'),
                $this->getOption('image_activity_name', 'thumbnail')
            );

            $image_activity_url = $image_activity->getUrl();
            $image_default_attributes['width'] = $image_activity_url->getParameter('width');
            $image_default_attributes['height'] = $image_activity_url->getParameter('height');
        }

        // get value from configured value path
        if ($this->hasOption('image_value_path')) {
            $path = $this->getOption('image_value_path');
            $image_attribute = AttributePath::getAttributeByPath($resource->getType(), $path);
            if ($image_attribute instanceof HandlesFileListInterface
                && $image_attribute->getFiletypeName() === HandlesFileInterface::FILETYPE_IMAGE
            ) {
                $image_value = AttributeValuePath::getAttributeValueByPath($resource, $path);
                $index = $this->getOption('image_value_path_index', 0);
                if (array_key_exists($index, $image_value)) {
                    $image = $image_value[$index]->toNative();
                    $location = $image[$image_attribute->getFileLocationPropertyName()];

                    if ($converjon_enabled) {
                        $image_url = $this->url_generator->generateUrl(
                            $image_activity,
                            [ 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    } else {
                        $image_url = $this->url_generator->generateUrl(
                            'module.files.download',
                            [ 'resource' => $resource, 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    }
                }
            } else {
                throw new RuntimeError('Not supported at the moment. Please implement me.');
            }
        }

        // figure out a fallback value if there are attributes containing images
        // @todo Support class intefaces as getAttributes parameter
        $image_attributes = $resource->getType()->getAttributes();
        if (!empty($image_attributes)) {
            foreach ($image_attributes as $image_attribute) {
                if (!$image_attribute instanceof HandlesFileInterface) {
                    continue;
                }
                if ($image_attribute->getFiletypeName() !== HandlesFileInterface::FILETYPE_IMAGE) {
                    continue;
                }
                $image_value = $resource->getValue($image_attribute->getName());
                if ($image_attribute instanceof HandlesFileListInterface) {
                    if (array_key_exists(0, $image_value)) {
                        $image = $image_value[0]->toNative();
                        $location = $image[$image_attribute->getFileLocationPropertyName()];

                        if ($converjon_enabled) {
                            $image_url = $this->url_generator->generateUrl(
                                $image_activity,
                                [ 'file' => $location ]
                            );
                            return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                        } else {
                            $image_url = $this->url_generator->generateUrl(
                                'module.files.download',
                                [ 'resource' => $resource, 'file' => $location ]
                            );
                            return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                        }
                    }
                } elseif (!empty($image_value)) {
                    $image = $image_value->toNative();
                    $location = $image[$image_attribute->getFileLocationPropertyName()];

                    if ($converjon_enabled) {
                        $image_url = $this->url_generator->generateUrl(
                            $image_activity,
                            [ 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    } else {
                        $image_url = $this->url_generator->generateUrl(
                            'module.files.download',
                            [ 'resource' => $resource, 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    }
                }
            }
        }

        return $image_default_attributes;
    }

    protected function getGlanceTitle(EntityInterface $resource)
    {
        if ($this->hasOption('title')) {
            $title = $this->getOption('title');
            // empty value would reset eventual global options and allow to use the value_path option
            if ($this->hasOption('title_value_path') && empty($title)) {
                return AttributeValuePath::getAttributeValueByPath($resource, $this->getOption('title_value_path'));
            } else {
                return $title;
            }
        }

        // otherwise get first text attribute value
        $text_attributes = $resource->getType()->getAttributes([], [ TextAttribute::class ])->toArray();
        if (!empty($text_attributes)) {
            $target_text_attribute = array_keys($text_attributes)[0];
            return $resource->getValue($target_text_attribute);
        }

        return '';
    }

    protected function getGlanceDescription(EntityInterface $resource)
    {
        if ($this->hasOption('description')) {
            $description = $this->getOption('description');
            // empty value would reset eventual global options and allow to use the value_path option
            if ($this->hasOption('description_value_path') && empty($description)) {
                return AttributeValuePath::getAttributeValueByPath(
                    $resource,
                    $this->getOption('description_value_path')
                );
            } else {
                return $description;
            }
        }

        // otherwise get first textarea attribute value
        $textarea_attributes = $resource->getType()->getAttributes([], [ TextareaAttribute::class ])->toArray();
        if (!empty($textarea_attributes)) {
            $target_textarea_attribute = array_keys($textarea_attributes)[0];
            return $resource->getValue($target_textarea_attribute);
        }

        return '';
    }
}
<commit_msg>Support 'image_disabled' option in the Glance renderer<commit_after><?php

namespace Honeybee\Ui\Renderer\Html\Honeybee;

use Honeybee\Ui\Renderer\Renderer;
use Honeybee\Ui\Renderer\EntityRenderer;
use Honeybee\Common\Error\RuntimeError;
use Honeybee\Common\Util\ArrayToolkit;
use Honeybee\EntityInterface;
use Trellis\Runtime\Attribute\AttributePath;
use Trellis\Runtime\Attribute\AttributeValuePath;
use Trellis\Runtime\Attribute\AttributePathInterface;
use Trellis\Runtime\Attribute\Text\TextAttribute;
use Trellis\Runtime\Attribute\Textarea\TextareaAttribute;
use Trellis\Runtime\Attribute\HandlesFileListInterface;
use Trellis\Runtime\Attribute\HandlesFileInterface;

class HtmlEntityGlanceRenderer extends EntityRenderer
{
    protected function getDefaultTemplateIdentifier()
    {
        return $this->hasOption('view_template_name')
            ? 'html/entity_glance/as_fields_with_viewtemplate_without_tabs.twig'    // view_template
            : 'html/entity_glance/entity-glance.twig';  // default
    }

    protected function getTemplateParameters()
    {
        $params = parent::getDefaultTemplateParameters();

        $resource = $this->getPayload('subject');
        $parent_attribute = $resource->getType()->getParentAttribute();
        $group_parts = (array)$this->getOption('group_parts', []);

        $params['grouped_base_path'] = ArrayToolkit::flattenToArrayPath($group_parts);
        $params['is_embed_template'] = $this->getOption('is_embed_template', false);
        $params['has_parent_attribute'] = $parent_attribute !== null;
        $params['html_attributes'] = $this->getOption('html_attributes', []);
        $params['image_disabled'] = $this->getOption('image_disabled', false);
        $params['resource'] = $resource->toNative();
        $params['is_new'] = !$resource->hasValue('identifier');
        $params['css'] = $this->getOption('css', '');

        if ($this->hasOption('view_template_name')) {
            // use view_template
            $params = array_replace_recursive($this->lookupViewTemplate(), $params);

            $params['rendered_fields'] = $this->getRenderedFields($resource, $params['view_template']);
            $params['css'] .= $params['is_new'] ? ' hb-glance--empty' : null;
        } else {
            // get default values
            if (!$params['image_disabled']) {
                $image = $this->getGlanceImage($resource);

                $params['image_width'] = $this->getOption('image_width', $image['width']);
                $params['image_height'] = $this->getOption('image_height', $image['height']);
                $params['image_url'] = $image['location'];
            }
            $params['title'] = $this->getGlanceTitle($resource);
            $params['description'] = $this->getGlanceDescription($resource);
        }

        return $params;
    }

    protected function getGlanceImage(EntityInterface $resource)
    {
        $image_default_attributes = [
            'location' => '',
            'width' => 100,
            'height' => 100
        ];

        // @todo 'image_url' should allow to restore the auto-retriving without specifying 'image_value_path',
        //       in the case a value was already set for it in a less specific config.
        if ($this->hasOption('image_url')) {
            $image_url = $this->getOption('image_url');
            if (!empty($image_url) || !$this->hasOption('image_value_path')) {
                // empty value would reset eventual global options and allow to use the value_path option
                return array_replace($image_default_attributes, [ 'location' => $image_url ]);
            }
        }

        $converjon_enabled = (bool)$this->getOption('use_converjon', false);

        $image_activity = null;
        if ($converjon_enabled) {
            $image_activity = $this->activity_service->getActivity(
                $this->getOption('image_activity_scope', 'converjon'),
                $this->getOption('image_activity_name', 'thumbnail')
            );

            $image_activity_url = $image_activity->getUrl();
            $image_default_attributes['width'] = $image_activity_url->getParameter('width');
            $image_default_attributes['height'] = $image_activity_url->getParameter('height');
        }

        // get value from configured value path
        if ($this->hasOption('image_value_path')) {
            $path = $this->getOption('image_value_path');
            $image_attribute = AttributePath::getAttributeByPath($resource->getType(), $path);
            if ($image_attribute instanceof HandlesFileListInterface
                && $image_attribute->getFiletypeName() === HandlesFileInterface::FILETYPE_IMAGE
            ) {
                $image_value = AttributeValuePath::getAttributeValueByPath($resource, $path);
                $index = $this->getOption('image_value_path_index', 0);
                if (array_key_exists($index, $image_value)) {
                    $image = $image_value[$index]->toNative();
                    $location = $image[$image_attribute->getFileLocationPropertyName()];

                    if ($converjon_enabled) {
                        $image_url = $this->url_generator->generateUrl(
                            $image_activity,
                            [ 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    } else {
                        $image_url = $this->url_generator->generateUrl(
                            'module.files.download',
                            [ 'resource' => $resource, 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    }
                }
            } else {
                throw new RuntimeError('Not supported at the moment. Please implement me.');
            }
        }

        // figure out a fallback value if there are attributes containing images
        // @todo Support class intefaces as getAttributes parameter
        $image_attributes = $resource->getType()->getAttributes();
        if (!empty($image_attributes)) {
            foreach ($image_attributes as $image_attribute) {
                if (!$image_attribute instanceof HandlesFileInterface) {
                    continue;
                }
                if ($image_attribute->getFiletypeName() !== HandlesFileInterface::FILETYPE_IMAGE) {
                    continue;
                }
                $image_value = $resource->getValue($image_attribute->getName());
                if ($image_attribute instanceof HandlesFileListInterface) {
                    if (array_key_exists(0, $image_value)) {
                        $image = $image_value[0]->toNative();
                        $location = $image[$image_attribute->getFileLocationPropertyName()];

                        if ($converjon_enabled) {
                            $image_url = $this->url_generator->generateUrl(
                                $image_activity,
                                [ 'file' => $location ]
                            );
                            return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                        } else {
                            $image_url = $this->url_generator->generateUrl(
                                'module.files.download',
                                [ 'resource' => $resource, 'file' => $location ]
                            );
                            return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                        }
                    }
                } elseif (!empty($image_value)) {
                    $image = $image_value->toNative();
                    $location = $image[$image_attribute->getFileLocationPropertyName()];

                    if ($converjon_enabled) {
                        $image_url = $this->url_generator->generateUrl(
                            $image_activity,
                            [ 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    } else {
                        $image_url = $this->url_generator->generateUrl(
                            'module.files.download',
                            [ 'resource' => $resource, 'file' => $location ]
                        );
                        return array_replace($image_default_attributes, [ 'location' => $image_url ]);
                    }
                }
            }
        }

        return $image_default_attributes;
    }

    protected function getGlanceTitle(EntityInterface $resource)
    {
        if ($this->hasOption('title')) {
            $title = $this->getOption('title');
            // empty value would reset eventual global options and allow to use the value_path option
            if ($this->hasOption('title_value_path') && empty($title)) {
                return AttributeValuePath::getAttributeValueByPath($resource, $this->getOption('title_value_path'));
            } else {
                return $title;
            }
        }

        // otherwise get first text attribute value
        $text_attributes = $resource->getType()->getAttributes([], [ TextAttribute::class ])->toArray();
        if (!empty($text_attributes)) {
            $target_text_attribute = array_keys($text_attributes)[0];
            return $resource->getValue($target_text_attribute);
        }

        return '';
    }

    protected function getGlanceDescription(EntityInterface $resource)
    {
        if ($this->hasOption('description')) {
            $description = $this->getOption('description');
            // empty value would reset eventual global options and allow to use the value_path option
            if ($this->hasOption('description_value_path') && empty($description)) {
                return AttributeValuePath::getAttributeValueByPath(
                    $resource,
                    $this->getOption('description_value_path')
                );
            } else {
                return $description;
            }
        }

        // otherwise get first textarea attribute value
        $textarea_attributes = $resource->getType()->getAttributes([], [ TextareaAttribute::class ])->toArray();
        if (!empty($textarea_attributes)) {
            $target_textarea_attribute = array_keys($textarea_attributes)[0];
            return $resource->getValue($target_textarea_attribute);
        }

        return '';
    }
}

{'fraction_non_alphanumeric': 0.12171984141967152, 'fraction_numerical': 0.0010383235793845573, 'mean_word_length': 2.017516377100541, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 188, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '371449', 'n_tokens_mistral': 5329, 'n_tokens_neox': 5141, 'n_words': 903}