ReportWidgetFactory.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Report; |
| 10 | |
| 11 | use Piwik\Plugin\Report; |
| 12 | use Piwik\Widget\WidgetContainerConfig; |
| 13 | /** |
| 14 | * Report widget factory. This factory allows you to create widgets for a given report without having to re-specify |
| 15 | * redundant information like module, action, category, subcategory, order, ... When creating a widget from a report |
| 16 | * these values will be automatically specified so that ideally `$factory->createWidget()` is all one has to do in |
| 17 | * order to create a new widget. |
| 18 | * |
| 19 | * @api since Piwik 3.0.0 |
| 20 | */ |
| 21 | class ReportWidgetFactory |
| 22 | { |
| 23 | /** |
| 24 | * @var Report |
| 25 | */ |
| 26 | private $report = null; |
| 27 | /** |
| 28 | * Generates a new report widget factory. |
| 29 | * @param Report $report A report instance, widgets will be created based on the data provided by this report. |
| 30 | */ |
| 31 | public function __construct(Report $report) |
| 32 | { |
| 33 | $this->report = $report; |
| 34 | } |
| 35 | /** |
| 36 | * Creates a widget based on the specified report in {@link construct()}. |
| 37 | * |
| 38 | * It will automatically use the report's name, categoryId, subcategoryId (if specified), |
| 39 | * defaultViewDataTable, module, action, order and parameters in order to create the widget. |
| 40 | * |
| 41 | * @return ReportWidgetConfig |
| 42 | */ |
| 43 | public function createWidget() |
| 44 | { |
| 45 | $widget = new \Piwik\Report\ReportWidgetConfig(); |
| 46 | $widget->setName($this->report->getName()); |
| 47 | $widget->setCategoryId($this->report->getCategoryId()); |
| 48 | if ($this->report->getDefaultTypeViewDataTable()) { |
| 49 | $widget->setDefaultViewDataTable($this->report->getDefaultTypeViewDataTable()); |
| 50 | } |
| 51 | if ($this->report->getSubcategoryId()) { |
| 52 | $widget->setSubcategoryId($this->report->getSubcategoryId()); |
| 53 | } |
| 54 | $widget->setModule($this->report->getModule()); |
| 55 | $widget->setAction($this->report->getAction()); |
| 56 | $orderThatListsReportsAtTheEndOfEachCategory = 100 + $this->report->getOrder(); |
| 57 | $widget->setOrder($orderThatListsReportsAtTheEndOfEachCategory); |
| 58 | $parameters = $this->report->getParameters(); |
| 59 | if (!empty($parameters)) { |
| 60 | $widget->setParameters($parameters); |
| 61 | } |
| 62 | return $widget; |
| 63 | } |
| 64 | /** |
| 65 | * Creates a new container widget based on the specified report in {@link construct()}. |
| 66 | * |
| 67 | * It will automatically use the report's categoryId, subcategoryId (if specified) and order in order to |
| 68 | * create the container. |
| 69 | * |
| 70 | * @param string $containerId eg 'Products' or 'Contents' see {Piwik\Widget\WidgetContainerConfig::setId()}. |
| 71 | * Other reports or widgets will be able to add more widgets to this container. |
| 72 | * This is useful when you want to show for example multiple related widgets |
| 73 | * together. |
| 74 | * @return WidgetContainerConfig |
| 75 | */ |
| 76 | public function createContainerWidget($containerId) |
| 77 | { |
| 78 | $widget = new WidgetContainerConfig(); |
| 79 | $widget->setCategoryId($this->report->getCategoryId()); |
| 80 | $widget->setId($containerId); |
| 81 | if ($this->report->getSubcategoryId()) { |
| 82 | $widget->setSubcategoryId($this->report->getSubcategoryId()); |
| 83 | } |
| 84 | $orderThatListsReportsAtTheEndOfEachCategory = 100 + $this->report->getOrder(); |
| 85 | $widget->setOrder($orderThatListsReportsAtTheEndOfEachCategory); |
| 86 | return $widget; |
| 87 | } |
| 88 | /** |
| 89 | * Creates a custom widget that doesn't use a viewDataTable to render the report but instead a custom |
| 90 | * controller action. Make sure the specified `$action` exists in the plugin's controller. Otherwise |
| 91 | * behaves as {@link createWidget()}. |
| 92 | * |
| 93 | * @param string $action eg 'conversionReports' (requires a method `public function conversionReports()` in |
| 94 | * the plugin's controller). |
| 95 | * @return ReportWidgetConfig |
| 96 | */ |
| 97 | public function createCustomWidget($action) |
| 98 | { |
| 99 | $widget = $this->createWidget(); |
| 100 | $widget->setDefaultViewDataTable(null); |
| 101 | $widget->setAction($action); |
| 102 | return $widget; |
| 103 | } |
| 104 | } |
| 105 |