|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\PageBuilderAnalytics\Model; |
| 8 | + |
| 9 | + |
| 10 | +use Magento\Cms\Model\ResourceModel\Page\CollectionFactory; |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides PageBuilder content type report. |
| 14 | + */ |
| 15 | +class PageBuilderProvider |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var \Magento\Cms\Model\ResourceModel\Page\CollectionFactory |
| 19 | + */ |
| 20 | + private $collectionFactory; |
| 21 | + |
| 22 | + /** |
| 23 | + * PageBuilderProvider constructor. |
| 24 | + * |
| 25 | + * @param CollectionFactory $collectionFactory |
| 26 | + */ |
| 27 | + public function __construct(CollectionFactory $collectionFactory) |
| 28 | + { |
| 29 | + $this->collectionFactory = $collectionFactory; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns report data for CMS pages |
| 34 | + * |
| 35 | + * @return \IteratorIterator |
| 36 | + */ |
| 37 | + public function getReport() |
| 38 | + { |
| 39 | + // move content types elsewhere and load |
| 40 | + $contentTypes = ['row', 'column', 'tabs', 'tab-item', 'text', 'heading', 'buttons', 'button-item', 'divider', |
| 41 | + 'html', 'image', 'video', 'banner', 'slider', 'slide', 'map', 'block', 'dynamic_block', 'products']; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Magento\Cms\Model\ResourceModel\Page\Collection |
| 45 | + */ |
| 46 | + $collection = $this->collectionFactory->create(); |
| 47 | + // build select by iterating content types |
| 48 | + foreach ($contentTypes as $contentType) { |
| 49 | + $expr = 'sum((char_length(content) - char_length(replace(content, \'data-role=\"' . $contentType . |
| 50 | + '\", \'\'))) / ' . strlen($contentType) . ')'; |
| 51 | + $collection->addExpressionFieldToSelect($contentType, $expr, 'content'); |
| 52 | + } |
| 53 | + $contentTypeReport = $collection->getData(); |
| 54 | + return new \IteratorIterator(new \ArrayIterator($contentTypeReport ?: [])); |
| 55 | + } |
| 56 | +} |
0 commit comments