heimrichhannot/contao-progress-bar-widget-bundle

此包为 Contao CMS 提供动态进度条小部件。

0.1.3 2021-09-16 08:25 UTC

This package is auto-updated.

Last update: 2024-09-16 14:39:05 UTC


README

此包为 Contao CMS 提供动态进度条小部件。

特性

  • 为特定数据库记录添加进度条小部件(目前仅适用于后端)
  • 支持 AJAX(自动更新)
  • 还可以显示进度数值

印象

The progress bar widget included in an example data container array

进度条小部件包含在示例数据容器数组中

安装

  1. 使用 composer 安装: composer require heimrichhannot/contao-progress-bar-widget-bundle

使用

  1. 在所需的数据容器数组(DCA)中创建字段,如同常规操作
    'importProgress'                 => [
        'inputType' => 'huhProgressBar',
        'eval' => [
            // an introduction text for the widget (mandatory)
            'description' => $GLOBALS['TL_LANG']['tl_entity_import_config']['reference']['importProgressDescription'],
            // only show the progress bar but not the numbers below (optional, default false)
            'hideNumbers' => false,
            // hide skipped items from the numbers section (optional, default false)
            'hideSkippedItems' => false
        ]
    ],
  2. 进度通过名为 LoadProgressEvent 的事件传递。在 symfony 中通常注册事件监听器(或订阅者)
    use HeimrichHannot\ProgressBarWidgetBundle\Event\LoadProgressEvent;
    use HeimrichHannot\ProgressBarWidgetBundle\Widget\ProgressBar;
    use Terminal42\ServiceAnnotationBundle\Annotation\ServiceTag;
    
    /**
     * @ServiceTag("kernel.event_listener", event="huh.progress_bar_widget.event.load_progress")
     */
    class LoadProgressListener
    {
        public function __invoke(LoadProgressEvent $event) {
            if (null === ($record = MyModel::findByPk($event->getId()))) {
                return;
            }
    
            // map the record's state field to the progress bar's expectations
            // of course you can decide yourself in which cases you'd like to pass which state
            switch ($record->state) {
                case 'success':
                    $state = ProgressBar::STATE_SUCCESS;
                    $class = 'tl_confirm';
                    break;
                case 'error':
                    $state = ProgressBar::STATE_FAILED;
                    $class = 'tl_error';
                    break;
                default:
                    $state = ProgressBar::STATE_IN_PROGRESS;
                    $class = '';
            }
    
            $data = [
                'state' => $state,
                'currentProgress' => $record->importProgressCurrent, // field name depends on your dca
                'totalCount' => $record->importProgressTotal, // field name depends on your dca
                'skippedCount' => $record->importProgressSkipped, // field name depends on your dca
            ];
    
            // add messages if some are available
            if ($record->importProgressResult) {
                $data['messages'] = [[
                    'class' => $class,
                    'text' => str_replace("\n", '<br>', $record->importProgressResult),
                ]];
            }
        }
    }

事件