PHP Class piwik\plugin\Controller

Plugins that wish to add display HTML should create a Controller that either extends from this class or from {@link ControllerAdmin}. Every public method in the controller will be exposed as a controller method and can be invoked via an HTTP request. Learn more about Piwik's MVC system here. ### Examples **Defining a controller** class Controller extends \Piwik\Plugin\Controller { public function index() { $view = new View("@MyPlugin/index.twig"); ... setup view ... return $view->render(); } } **Linking to a controller action** Link
Mostrar archivo Open project: piwik/piwik Class Usage Examples

Protected Properties

Property Type Description
$date Piwik\Date | null The Date object created with ($strDate)[#strDate] or null if the requested date is a range.
$idSite integer The value of the **idSite** query parameter.
$pluginName string The plugin name, eg. 'Referrers'.
$site Piwik\Site The Site object created with {@link $idSite}.
$strDate string The value of the **date** query parameter.

Public Methods

Method Description
__construct ( ) Constructor.
getCalendarPrettyDate ( Piwik\Period $period ) : string Returns a prettified date string for use in period selector widget.
getDefaultAction ( ) : string Returns the name of the default method that will be called when visiting: index.php?module=PluginName without the action parameter.
getPrettyDate ( $date, $period ) : string Returns the pretty date representation
redirectToIndex ( string $moduleToRedirect, string $actionToRedirect, integer | null $websiteId = null, string | null $defaultPeriod = null, string | null $defaultDate = null, array $parameters = [] ) Helper method used to redirect the current HTTP request to another module/action.
setHostValidationVariablesView ( View $view ) Checks if the current host is valid and sets variables on the given view, including:
setPeriodVariablesView ( View $view ) Sets general period variables on a view, including:

Protected Methods

Method Description
addCustomLogoInfo ( $view )
checkSitePermission ( )
checkTokenInUrl ( ) Checks that the token_auth in the URL matches the currently logged-in user's token_auth.
getDateParameterInTimezone ( string $date, string $timezone ) : Date Helper method that converts "today" or "yesterday" to the specified timezone.
getEnabledPeriodsInUI ( ) : array Returns values that are enabled for the parameter &period=
getGraphParamsModified ( array $paramsToSet = [] ) : array Returns the array of new processed parameters once the parameters are applied.
getLastUnitGraph ( string $currentModuleName, string $currentControllerAction, string $apiMethod ) : ViewDataTable Returns a ViewDataTable object that will render a jqPlot evolution graph for the last30 days/weeks/etc. of the current period, relative to the current date.
getLastUnitGraphAcrossPlugins ( string $currentModuleName, string $currentControllerAction, array $columnsToDisplay = false, array $selectableColumns = [], boolean | string $reportDocumentation = false, string $apiMethod = 'API.get' ) : ViewDataTable Same as {@link getLastUnitGraph()}, but will set some properties of the ViewDataTable object based on the arguments supplied.
getNumericValue ( string $methodToCall, boolean | string $date = false ) : integer | float Returns a numeric value from the API.
getUrlSparkline ( string $action, array $customParameters = [] ) : string Returns a URL to a sparkline image for a report served by the current plugin.
init ( )
renderReport ( string | Report $apiAction, boolean $controllerAction = false ) : string | void Convenience method that creates and renders a ViewDataTable for a API method.
renderTemplate ( string $template, array $variables = [] ) : string Assigns the given variables to the template and renders it.
renderView ( Piwik\View\ViewInterface $view ) : string | void A helper method that renders a view either to the screen or to a string.
setBasicVariablesView ( View $view ) Assigns a set of generally useful variables to a {@link Piwik\View} instance.
setDate ( Date $date ) : void Sets the date to be used by all other methods in the controller.
setGeneralVariablesView ( View $view ) : void Assigns variables to {@link Piwik\View} instances that display an entire page.
setMaxDateView ( Date $maxDate, View $view ) Sets the last date available in the period selector's calendar. Usually this is just the "today" date for a site (which varies based on the timezone of a site).
setMinDateView ( Date $minDate, View $view ) Sets the first date available in the period selector's calendar.

Private Methods

Method Description
doRedirectToUrl ( $moduleToRedirect, $actionToRedirect, $websiteId, $defaultPeriod, $defaultDate, $parameters )
getEnabledPeriodsNames ( ) : array
getValidDate ( Date $date, Date $minDate, Date $maxDate )

Method Details

__construct() public method

Constructor.
public __construct ( )

addCustomLogoInfo() protected method

protected addCustomLogoInfo ( $view )

checkSitePermission() protected method

protected checkSitePermission ( )

checkTokenInUrl() protected method

This is a protection against CSRF and should be used in all controller methods that modify Piwik or any user settings. If called from JavaScript by using the ajaxHelper you have to call ajaxHelper.withTokenInUrl(); before ajaxHandler.send(); to send the token along with the request. **The token_auth should never appear in the browser's address bar.**
protected checkTokenInUrl ( )

getCalendarPrettyDate() public static method

Returns a prettified date string for use in period selector widget.
public static getCalendarPrettyDate ( Piwik\Period $period ) : string
$period Piwik\Period The period to return a pretty string for.
return string

getDateParameterInTimezone() protected method

If the date is absolute, ie. YYYY-MM-DD, it will not be converted to the timezone.
protected getDateParameterInTimezone ( string $date, string $timezone ) : Date
$date string `'today'`, `'yesterday'`, `'YYYY-MM-DD'`
$timezone string The timezone to use.
return Piwik\Date

getDefaultAction() public method

Returns the name of the default method that will be called when visiting: index.php?module=PluginName without the action parameter.
public getDefaultAction ( ) : string
return string

getEnabledPeriodsInUI() protected static method

Returns values that are enabled for the parameter &period=
protected static getEnabledPeriodsInUI ( ) : array
return array eg. array('day', 'week', 'month', 'year', 'range')

getGraphParamsModified() protected method

For example: if you set range=last30 and date=2008-03-10, the date element of the returned array will be "2008-02-10,2008-03-10" Parameters you can set: - range: last30, previous10, etc. - date: YYYY-MM-DD, today, yesterday - period: day, week, month, year
protected getGraphParamsModified ( array $paramsToSet = [] ) : array
$paramsToSet array array( 'date' => 'last50', 'viewDataTable' =>'sparkline' )
return array

getLastUnitGraph() protected method

Returns a ViewDataTable object that will render a jqPlot evolution graph for the last30 days/weeks/etc. of the current period, relative to the current date.
protected getLastUnitGraph ( string $currentModuleName, string $currentControllerAction, string $apiMethod ) : ViewDataTable
$currentModuleName string The name of the current plugin.
$currentControllerAction string The name of the action that renders the desired report.
$apiMethod string The API method that the ViewDataTable will use to get graph data.
return ViewDataTable

getLastUnitGraphAcrossPlugins() protected method

Same as {@link getLastUnitGraph()}, but will set some properties of the ViewDataTable object based on the arguments supplied.
protected getLastUnitGraphAcrossPlugins ( string $currentModuleName, string $currentControllerAction, array $columnsToDisplay = false, array $selectableColumns = [], boolean | string $reportDocumentation = false, string $apiMethod = 'API.get' ) : ViewDataTable
$currentModuleName string The name of the current plugin.
$currentControllerAction string The name of the action that renders the desired report.
$columnsToDisplay array The value to use for the ViewDataTable's columns_to_display config property.
$selectableColumns array The value to use for the ViewDataTable's selectable_columns config property.
$reportDocumentation boolean | string The value to use for the ViewDataTable's documentation config property.
$apiMethod string The API method that the ViewDataTable will use to get graph data.
return ViewDataTable

getNumericValue() protected method

Works only for API methods that originally returns numeric values (there is no cast here)
protected getNumericValue ( string $methodToCall, boolean | string $date = false ) : integer | float
$methodToCall string Name of method to call, eg. Referrers.getNumberOfDistinctSearchEngines
$date boolean | string A custom date to use when getting the value. If false, the 'date' query parameter is used.
return integer | float

getPrettyDate() public static method

Returns the pretty date representation
public static getPrettyDate ( $date, $period ) : string
$date string
$period string
return string Pretty date

getUrlSparkline() protected method

The result of this URL should be used with the sparkline() twig function. The current site ID and period will be used.
protected getUrlSparkline ( string $action, array $customParameters = [] ) : string
$action string Method name of the controller that serves the report.
$customParameters array The array of query parameter name/value pairs that should be set in result URL.
return string The generated URL.

init() protected method

protected init ( )

redirectToIndex() public method

This function will exit immediately after executing.
public redirectToIndex ( string $moduleToRedirect, string $actionToRedirect, integer | null $websiteId = null, string | null $defaultPeriod = null, string | null $defaultDate = null, array $parameters = [] )
$moduleToRedirect string The plugin to redirect to, eg. `"MultiSites"`.
$actionToRedirect string Action, eg. `"index"`.
$websiteId integer | null The new idSite query parameter, eg, `1`.
$defaultPeriod string | null The new period query parameter, eg, `'day'`.
$defaultDate string | null The new date query parameter, eg, `'today'`.
$parameters array Other query parameters to append to the URL.

renderReport() protected method

Convenience method that creates and renders a ViewDataTable for a API method.
protected renderReport ( string | Report $apiAction, boolean $controllerAction = false ) : string | void
$apiAction string | Report The name of the API action (eg, `'getResolution'`) or an instance of an report.
$controllerAction boolean The name of the Controller action name that is rendering the report. Defaults to the `$apiAction`.
return string | void See `$fetch`.

renderTemplate() protected method

Example: public function myControllerAction () { return $this->renderTemplate('index', array( 'answerToLife' => '42' )); } This will render the 'index.twig' file within the plugin templates folder and assign the view variable answerToLife to 42.
Since: 2.5.0
protected renderTemplate ( string $template, array $variables = [] ) : string
$template string The name of the template file. If only a name is given it will automatically use the template within the plugin folder. For instance 'myTemplate' will result in '@$pluginName/myTemplate.twig'. Alternatively you can include the full path: '@anyOtherFolder/otherTemplate'. The trailing '.twig' is not needed.
$variables array For instance array('myViewVar' => 'myValue'). In template you can use {{ myViewVar }}
return string

renderView() protected method

A helper method that renders a view either to the screen or to a string.
protected renderView ( Piwik\View\ViewInterface $view ) : string | void
$view Piwik\View\ViewInterface The view to render.
return string | void

setBasicVariablesView() protected method

The following variables assigned: **isSuperUser** - True if the current user is the Super User, false if otherwise. **hasSomeAdminAccess** - True if the current user has admin access to at least one site, false if otherwise. **isCustomLogo** - The value of the branding_use_custom_logo option. **logoHeader** - The header logo URL to use. **logoLarge** - The large logo URL to use. **logoSVG** - The SVG logo URL to use. **hasSVGLogo** - True if there is a SVG logo, false if otherwise. **enableFrames** - The value of the [General] enable_framed_pages INI config option. If true, {@link Piwik\View::setXFrameOptions()} is called on the view. Also calls {@link setHostValidationVariablesView()}.
protected setBasicVariablesView ( View $view )
$view Piwik\View

setDate() protected method

If the date has to be modified, this method should be called just after construction.
protected setDate ( Date $date ) : void
$date Piwik\Date The new Date.
return void

setGeneralVariablesView() protected method

The following variables assigned: **date** - The value of the **date** query parameter. **idSite** - The value of the **idSite** query parameter. **rawDate** - The value of the **date** query parameter. **prettyDate** - A pretty string description of the current period. **siteName** - The current site's name. **siteMainUrl** - The URL of the current site. **startDate** - The start date of the current period. A {@link Piwik\Date} instance. **endDate** - The end date of the current period. A {@link Piwik\Date} instance. **language** - The current language's language code. **config_action_url_category_delimiter** - The value of the [General] action_url_category_delimiter INI config option. **topMenu** - The result of MenuTop::getInstance()->getMenu(). As well as the variables set by {@link setPeriodVariablesView()}. Will exit on error.
protected setGeneralVariablesView ( View $view ) : void
$view Piwik\View
return void

setHostValidationVariablesView() public static method

- **isValidHost** - true if host is valid, false if otherwise - **invalidHostMessage** - message to display if host is invalid (only set if host is invalid) - **invalidHost** - the invalid hostname (only set if host is invalid) - **mailLinkStart** - the open tag of a link to email the Super User of this problem (only set if host is invalid)
public static setHostValidationVariablesView ( View $view )
$view Piwik\View

setMaxDateView() protected method

Sets the last date available in the period selector's calendar. Usually this is just the "today" date for a site (which varies based on the timezone of a site).
protected setMaxDateView ( Date $maxDate, View $view )
$maxDate Piwik\Date The max date.
$view Piwik\View The view that contains the period selector.

setMinDateView() protected method

Sets the first date available in the period selector's calendar.
protected setMinDateView ( Date $minDate, View $view )
$minDate Piwik\Date The min date.
$view Piwik\View The view that contains the period selector.

setPeriodVariablesView() public static method

- **displayUniqueVisitors** - Whether unique visitors should be displayed for the current period. - **period** - The value of the **period** query parameter. - **otherPeriods** - array('day', 'week', 'month', 'year', 'range') - **periodsNames** - List of available periods mapped to their singular and plural translations.
public static setPeriodVariablesView ( View $view )
$view Piwik\View

Property Details

$date protected_oe property

The Date object created with ($strDate)[#strDate] or null if the requested date is a range.
protected Date,Piwik|null $date
return Piwik\Date | null

$idSite protected_oe property

The value of the **idSite** query parameter.
protected int $idSite
return integer

$pluginName protected_oe property

The plugin name, eg. 'Referrers'.
protected string $pluginName
return string

$site protected_oe property

The Site object created with {@link $idSite}.
protected Site,Piwik $site
return Piwik\Site

$strDate protected_oe property

The value of the **date** query parameter.
protected string $strDate
return string