Piwik\Plugin\ReportsProvider::getAllReports PHP Method

getAllReports() public method

Returns a list of all available reports. Even not enabled reports will be returned. They will be already sorted depending on the order and category of the report.
public getAllReports ( ) : Report[]
return Report[]
    public function getAllReports()
    {
        $reports = $this->getAllReportClasses();
        $cacheId = CacheId::languageAware('Reports' . md5(implode('', $reports)));
        $cache = PiwikCache::getTransientCache();
        if (!$cache->contains($cacheId)) {
            $instances = array();
            /**
             * Triggered to add new reports that cannot be picked up automatically by the platform.
             * This is useful if the plugin allows a user to create reports / dimensions dynamically. For example
             * CustomDimensions or CustomVariables. There are a variable number of dimensions in this case and it
             * wouldn't be really possible to create a report file for one of these dimensions as it is not known
             * how many Custom Dimensions will exist.
             *
             * **Example**
             *
             *     public function addReport(&$reports)
             *     {
             *         $reports[] = new MyCustomReport();
             *     }
             *
             * @param Report[] $reports An array of reports
             */
            Piwik::postEvent('Report.addReports', array(&$instances));
            foreach ($reports as $report) {
                $instances[] = new $report();
            }
            /**
             * Triggered to filter / restrict reports.
             *
             * **Example**
             *
             *     public function filterReports(&$reports)
             *     {
             *         foreach ($reports as $index => $report) {
             *              if ($report->getCategory() === 'Actions') {}
             *                  unset($reports[$index]); // remove all reports having this action
             *              }
             *         }
             *     }
             *
             * @param Report[] $reports An array of reports
             */
            Piwik::postEvent('Report.filterReports', array(&$instances));
            usort($instances, array($this, 'sort'));
            $cache->save($cacheId, $instances);
        }
        return $cache->fetch($cacheId);
    }

Usage Example

Example #1
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @param string $pluginName
  * @return array
  * @throws \RuntimeException
  */
 protected function getDimension(InputInterface $input, OutputInterface $output, $pluginName)
 {
     $dimensions = array();
     $dimensionNames = array();
     $reports = new ReportsProvider();
     foreach ($reports->getAllReports() as $report) {
         $dimension = $report->getDimension();
         if (is_object($dimension)) {
             $name = $dimension->getName();
             if (!empty($name)) {
                 $dimensions[$name] = get_class($dimension);
                 $dimensionNames[] = $name;
             }
         }
     }
     $plugin = Manager::getInstance()->loadPlugin($pluginName);
     $dimensions = Dimension::getAllDimensions();
     $dimensions = array_merge($dimensions, Dimension::getDimensions($plugin));
     foreach ($dimensions as $dimension) {
         $name = $dimension->getName();
         if (!empty($name)) {
             $dimensions[$name] = get_class($dimension);
             $dimensionNames[] = $name;
         }
     }
     $dimensionNames = array_values(array_unique($dimensionNames));
     $validate = function ($dimension) use($dimensions) {
         if (empty($dimension)) {
             return '';
         }
         if (!empty($dimension) && !array_key_exists($dimension, $dimensions)) {
             throw new \InvalidArgumentException('Leave dimension either empty or use an existing one. You can also create a new dimension by calling .console generate:dimension before generating this report.');
         }
         return $dimension;
     };
     $actualDimension = $input->getOption('dimension');
     if (null === $actualDimension) {
         $dialog = $this->getHelperSet()->get('dialog');
         $actualDimension = $dialog->askAndValidate($output, 'Enter the report dimension, for instance "Browser" (you can leave it either empty or use an existing one): ', $validate, false, null, $dimensionNames);
     } else {
         $validate($actualDimension);
     }
     if (empty($actualDimension)) {
         return array('null', '');
     }
     $className = $dimensions[$actualDimension];
     $parts = explode('\\', $className);
     $name = end($parts);
     return array('new ' . $name . '()', 'use ' . $className . ';');
 }
All Usage Examples Of Piwik\Plugin\ReportsProvider::getAllReports