1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreConsole\Commands;
use Piwik\Columns\Dimension;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugin\ReportsProvider;
class GenerateReport extends GeneratePluginBase
{
protected function configure()
{
$this->setName('generate:report')
->setDescription('Adds a new report to an existing plugin')
->addRequiredValueOption('pluginname', null, 'The name of an existing plugin which does not have a menu defined yet')
->addRequiredValueOption('reportname', null, 'The name of the report you want to create')
->addRequiredValueOption('category', null, 'The name of the category the report belongs to')
->addOptionalValueOption('dimension', null, 'The name of the dimension in case your report has a dimension')
->addRequiredValueOption('documentation', null, 'A documentation that explains what your report is about');
}
protected function doExecute(): int
{
$pluginName = $this->getPluginName();
$this->checkAndUpdateRequiredPiwikVersion($pluginName);
$reportName = $this->getReportName();
$category = $this->getCategory($pluginName);
$documentation = $this->getDocumentation();
[$dimension, $dimensionClass] = $this->getDimension($pluginName);
$order = $this->getOrder($category);
$apiName = $this->getApiName($reportName);
$exampleFolder = Manager::getPluginDirectory('ExampleReport');
$replace = array('GetExampleReport' => ucfirst($apiName),
'getExampleReport' => lcfirst($apiName),
'getApiReport' => lcfirst($apiName),
'ExampleCategory' => $category,
'ExampleReportName' => $this->makeTranslationIfPossible($pluginName, $reportName),
'ExampleReportDocumentation' => $documentation,
'999' => $order,
'new ExitPageUrl()' => $dimension,
'use Piwik\Plugins\Actions\Columns\ExitPageUrl;' => $dimensionClass,
'ExampleReport' => $pluginName,
);
$whitelistFiles = array('/Reports', '/Reports/Base.php', '/Reports/GetExampleReport.php');
if (file_exists($this->getPluginPath($pluginName) . '/API.php')) {
$this->copyTemplateMethodToExisitingClass('Piwik\Plugins\ExampleReport\API', 'getExampleReport', $replace);
} else {
$whitelistFiles[] = '/API.php';
}
$this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);
$this->writeSuccessMessage(array(
sprintf('plugins/%s/Reports/%s.php for %s generated.', $pluginName, ucfirst($apiName), $pluginName),
'You should now implement the method called <comment>"' . lcfirst($apiName) . '()"</comment> in API.php',
// 'Read more about this here: link to developer guide',
'Enjoy!',
));
return self::SUCCESS;
}
private function getOrder($category)
{
$order = 1;
$reports = new ReportsProvider();
foreach ($reports->getAllReports() as $report) {
if ($report->getCategoryId() === $category) {
if ($report->getOrder() > $order) {
$order = $report->getOrder() + 1;
}
}
}
return $order;
}
private function getApiName($reportName)
{
$reportName = trim($reportName);
$reportName = str_replace(' ', '', $reportName);
$reportName = preg_replace("/[^A-Za-z0-9]/", '', $reportName);
$apiName = 'get' . ucfirst($reportName);
return $apiName;
}
/**
* @return string
* @throws \RuntimeException
*/
protected function getReportName()
{
$input = $this->getInput();
$validate = function ($reportName) {
if (empty($reportName)) {
throw new \InvalidArgumentException('Please enter the name of your report');
}
if (preg_match("/[^A-Za-z0-9 ]/", $reportName)) {
throw new \InvalidArgumentException('Only alpha numerical characters and whitespaces are allowed');
}
return $reportName;
};
$reportName = $input->getOption('reportname');
if (empty($reportName)) {
$reportName = $this->askAndValidate(
'Enter the name of your report, for instance "Browser Families": ',
$validate
);
} else {
$validate($reportName);
}
$reportName = ucfirst($reportName);
return $reportName;
}
/**
* @return string
* @throws \RuntimeException
*/
protected function getDocumentation()
{
$input = $this->getInput();
$validate = function ($documentation) {
if (empty($documentation)) {
return '';
}
return $documentation;
};
$documentation = $input->getOption('documentation');
if (empty($documentation)) {
$documentation = $this->askAndValidate(
'Enter a documentation that describes the data of your report (you can leave it empty and define it later): ',
$validate
);
} else {
$validate($documentation);
}
$documentation = ucfirst($documentation);
return $documentation;
}
/**
* @param string $pluginName
* @return string
* @throws \RuntimeException
*/
protected function getCategory($pluginName)
{
$input = $this->getInput();
$path = $this->getPluginPath($pluginName) . '/Reports/Base.php';
if (file_exists($path)) {
// category is already defined in base.php
return '';
}
$validate = function ($category) {
if (empty($category)) {
throw new \InvalidArgumentException('Please enter the name of the category your report belongs to');
}
return $category;
};
$category = $input->getOption('category');
$reports = new ReportsProvider();
$categories = array();
foreach ($reports->getAllReports() as $report) {
if ($report->getCategoryId()) {
$categories[] = Piwik::translate($report->getCategoryId());
}
}
$categories = array_values(array_unique($categories));
if (empty($category)) {
$category = $this->askAndValidate(
'Enter the report category, for instance "Visitor" (you can reuse any existing category or define a new one): ',
$validate,
false,
$categories
);
} else {
$validate($category);
}
$translationKey = StaticContainer::get('Piwik\Translation\Translator')->findTranslationKeyForTranslation($category);
if (!empty($translationKey)) {
return $translationKey;
}
$category = ucfirst($category);
return $category;
}
/**
* @param string $pluginName
* @return array
* @throws \RuntimeException
*/
protected function getDimension($pluginName)
{
$input = $this->getInput();
$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) {
$actualDimension = $this->askAndValidate(
'Enter the report dimension, for instance "Browser" (you can leave it either empty or use an existing one): ',
$validate,
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 . ';');
}
/**
* @return string
* @throws \RuntimeException
*/
protected function getPluginName()
{
$pluginNames = $this->getPluginNames();
$invalidName = 'You have to enter a name of an existing plugin.';
return $this->askPluginNameAndValidate($pluginNames, $invalidName);
}
}
|