Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/7861_2' into manage_apps
Browse files Browse the repository at this point in the history
Conflicts:
	core/Plugin/Widget.php
  • Loading branch information
tsteur committed May 13, 2015
2 parents c5f7569 + 2996cd6 commit 28c1066
Show file tree
Hide file tree
Showing 100 changed files with 1,189 additions and 495 deletions.
18 changes: 13 additions & 5 deletions core/Http/ControllerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Exception;
use Piwik\Plugin\Controller;
use Piwik\Plugin\Report;
use Piwik\Plugin\Widget;
use Piwik\Plugin\Widgets;
use Piwik\Session;

Expand Down Expand Up @@ -85,15 +86,22 @@ private function createPluginController($module, $action)

private function createWidgetController($module, $action, array &$parameters)
{
$widget = Widgets::factory($module, $action);
$widget = Widget::factory($module, $action);

if (!$widget) {
return null;
$widget = Widgets::factory($module, $action);

if (!$widget) {
return null;
}

$parameters['widget'] = $widget;
$parameters['method'] = $action;
} else {
$parameters['widget'] = $widget;
$parameters['method'] = 'render';
}

$parameters['widget'] = $widget;
$parameters['method'] = $action;

return array($this->createCoreHomeController(), 'renderWidget');
}

Expand Down
67 changes: 63 additions & 4 deletions core/Plugin/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ class Report
* a widget for this report. Alternatively, this behavior can be overwritten in {@link configureWidget()}.
* @var string
* @api
* @deprecated since Piwik 2.15, use `createWidget()` instead
*/
protected $widgetTitle;

/**
* Optional widget params that will be appended to the widget URL if a {@link $widgetTitle} is set.
* @var array
* @api
* @deprecated since Piwik 2.15, use `createWidget()` instead
*/
protected $widgetParams = array();

Expand Down Expand Up @@ -212,6 +214,11 @@ class Report
*/
protected $defaultSortOrderDesc = true;

/**
* @var null|WidgetConfig
*/
private $widget = null;

/**
* @var array
* @ignore
Expand Down Expand Up @@ -354,21 +361,69 @@ public function render()
return $rendered;
}

protected function createWidget()
{
$widget = new WidgetConfig();
$widget->setOrder(9989); // this allows to detect whether the report set a custom module/action/order
// todo if we actually go with that code later we need to make a constant for 9989

// createWidget() should only create a widget and not set it, but keep it for now for simplicity
$this->widget = $widget;

return $widget;
}

/**
* By default a widget will be configured for this report if a {@link $widgetTitle} is set. If you want to customize
* the way the widget is added or modify any other behavior you can overwrite this method.
* @param WidgetsList $widget
* @api
* @deprecated since Piwik 2.15, use `createWidget()` instead
* FYI: We will most likely keep this method but we will remove @api
* @internal
*/
public function configureWidget(WidgetsList $widget)
{
if ($this->widgetTitle) {
$params = array();
if (!$this->widget && $this->widgetTitle) {
// for BC
$createdWidget = $this->createWidget();
$createdWidget->setName($this->widgetTitle);
if (!empty($this->widgetParams) && is_array($this->widgetParams)) {
$params = $this->widgetParams;
$createdWidget->setParameters($this->widgetParams);
}
$widget->add($this->category, $this->widgetTitle, $this->module, $this->action, $params);
}

if (!$this->widget) {
return;
}

if (!$this->isEnabled()) {
// todo I just notice we did not respect $this->isEnabled() before. Maybe it was on purpose?
return;
}

if (!$this->widget->getCategory()) {
$this->widget->setCategory($this->category);
}

if (!$this->widget->getName()) {
$this->widget->setName($this->name);
}

if (!$this->widget->getModule()) {
$this->widget->setModule($this->module);
}

if (!$this->widget->getAction()) {
$this->widget->setAction($this->action);
}

if (9989 === $this->widget->getOrder()) {
$orderThatListsReportsAtTheEndOfEachCategory = 100 + $this->order;
$this->widget->setOrder($orderThatListsReportsAtTheEndOfEachCategory);
}

$widget->addWidget($this->widget);
}

/**
Expand Down Expand Up @@ -647,6 +702,10 @@ public function getRelatedReports()
*/
public function getWidgetTitle()
{
if ($this->widget) {
return Piwik::translate($this->widget->getName());
}

if ($this->widgetTitle) {
return Piwik::translate($this->widgetTitle);
}
Expand Down
115 changes: 67 additions & 48 deletions core/Plugin/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,104 @@
*/
namespace Piwik\Plugin;

use Piwik\Development;
use Piwik\Container\StaticContainer;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\WidgetsList;
use Exception;

/**
* Base class of all plugin widget providers. Plugins that define their own widgets can extend this class to easily
* add new widgets or to remove widgets defined by other plugins.
* Defines a new widget. You can create a new widget using the console command `./console generate:widget`.
* The generated widget will guide you through the creation of a widget.
*
* For an example, see the {@link https://github.com/piwik/piwik/blob/master/plugins/ExamplePlugin/Widgets.php} plugin.
* For an example, see {@link https://github.com/piwik/piwik/blob/master/plugins/ExamplePlugin/Widgets/MyExampleWidget.php}
*
* @api
* @api since Piwik 2.15
*/
class Widget
{
protected $category = '';
protected $module = '';
protected $action = '';

/**
* @ignore
* @param WidgetConfig $config
* @api
*/
public function getCategory()
public static function configure(WidgetConfig $config)
{
return $this->category;
}

public function getModule()
/**
* @return string
*/
public function render()
{
if (empty($this->module)) {
$parts = $this->getClassNameParts();

$this->module = $parts[2];
}
return '';
}

return $this->module;
/**
* Allows you to configure previously added widgets.
* For instance you can remove any widgets defined by any plugin by calling the
* {@link \Piwik\WidgetsList::remove()} method.
*
* @param WidgetsList $widgetsList
* @api
*/
public static function configureWidgetsList(WidgetsList $widgetsList)
{
}

public function getAction()
/**
* @return \Piwik\Plugin\WidgetConfig[]
*/
public static function getAllWidgetConfigurations()
{
if (empty($this->action)) {
$parts = $this->getClassNameParts();
$widgetClasses = self::getAllWidgetClassNames();

if (count($parts) >= 4) {
$this->action = lcfirst(end($parts));
}
$configs = array();
foreach ($widgetClasses as $widgetClass) {
$configs[] = self::getWidgetConfigForClassName($widgetClass);
}

return $this->action;
return $configs;
}

private function getClassNameParts()
private static function getWidgetConfigForClassName($widgetClass)
{
$classname = get_class($this);
return explode('\\', $classname);
/** @var string|Widget $widgetClass */
$config = new WidgetConfig();
$config->setModule(self::getModuleFromWidgetClassName($widgetClass));
$config->setAction(self::getActionFromWidgetClassName($widgetClass));
$widgetClass::configure($config);

return $config;
}

/**
* @return \Piwik\Plugin\Widget[]
* @ignore
* @return string[]
*/
public static function getAllWidgets()
public static function getAllWidgetClassNames()
{
$widgetClasses = PluginManager::getInstance()->findMultipleComponents('Widgets', 'Piwik\\Plugin\\Widget');
return PluginManager::getInstance()->findMultipleComponents('Widgets', 'Piwik\\Plugin\\Widget');
}

$widgets = array();
foreach ($widgetClasses as $widgetClass) {
$widgets[] = new $widgetClass();
private static function getModuleFromWidgetClassName($widgetClass)
{
$parts = explode('\\', $widgetClass);

return $parts[2];
}

private static function getActionFromWidgetClassName($widgetClass)
{
$parts = explode('\\', $widgetClass);

if (count($parts) >= 4) {
return lcfirst(end($parts));
}

return $widgets;
return '';
}

/**
* @ignore
* @return Widgets|null
* @throws \Exception
*/
public static function factory($module, $action)
{
Expand All @@ -103,20 +126,16 @@ public static function factory($module, $action)
return;
}

// the widget class implements such an action, but we have to check whether it is actually exposed and whether
// it was maybe disabled by another plugin, this is only possible by checking the widgetslist, unfortunately
if (!WidgetsList::isDefined($module, $action)) {
return;
}

/** @var Widget[] $widgetContainer */
$widgets = $plugin->findMultipleComponents('Widgets', 'Piwik\\Plugin\\Widget');

foreach ($widgets as $widget) {
if ($widget->getAction() == $action) {
return $widget;
foreach ($widgets as $widgetClass) {
$config = self::getWidgetConfigForClassName($widgetClass);
if ($config->getAction() === $action) {
$config->checkIsEnabled(); // todo how can we handle this better?!? only isEnabled? or setEnabled on widget?
return StaticContainer::get($widgetClass);
}
}
}

}
}

0 comments on commit 28c1066

Please sign in to comment.