Piwik\Plugins\Dashboard\Model::replaceDashboardWidgets PHP Method

replaceDashboardWidgets() public static method

It uses the given widget definitions to find the old and to create the new widgets Each widget is defined with an array containing the following information array ( 'module' => string 'action' => string 'params' => array() ) if $newWidget does not contain a widget definition at the current position, the old widget will simply be removed
public static replaceDashboardWidgets ( $dashboardLayout, array $oldWidgets, array $newWidgets )
$oldWidgets array array containing widget definitions
$newWidgets array array containing widget definitions
    public static function replaceDashboardWidgets($dashboardLayout, $oldWidgets, $newWidgets)
    {
        if (empty($dashboardLayout) || !isset($dashboardLayout->columns)) {
            return $dashboardLayout;
        }
        $newColumns = array();
        foreach ($dashboardLayout->columns as $id => $column) {
            $newColumn = array();
            foreach ($column as $widget) {
                foreach ($oldWidgets as $pos => $oldWidgetData) {
                    $oldWidgetId = WidgetsList::getWidgetUniqueId($oldWidgetData['module'], $oldWidgetData['action'], $oldWidgetData['params']);
                    if (empty($newWidgets[$pos])) {
                        continue 2;
                    }
                    $newWidget = $newWidgets[$pos];
                    if ($widget->uniqueId == $oldWidgetId) {
                        if (!empty($newWidget['uniqueId'])) {
                            $newWidgetId = $newWidget['uniqueId'];
                        } else {
                            $newWidgetId = WidgetsList::getWidgetUniqueId($newWidget['module'], $newWidget['action'], $newWidget['params']);
                        }
                        // is new widget already is on dashboard just remove the old one
                        if (self::layoutContainsWidget($dashboardLayout, $newWidgetId)) {
                            continue 2;
                        }
                        $widget->uniqueId = $newWidgetId;
                        $widget->parameters->module = $newWidget['module'];
                        $widget->parameters->action = $newWidget['action'];
                        foreach ($newWidget['params'] as $key => $value) {
                            $widget->parameters->{$key} = $value;
                        }
                    }
                }
                $newColumn[] = $widget;
            }
            $newColumns[] = $newColumn;
        }
        $dashboardLayout->columns = $newColumns;
        return $dashboardLayout;
    }

Usage Example

Example #1
0
 public function getMigrations(Updater $updater)
 {
     $migrations = array('# ATTENTION: This update script will execute some more SQL queries than that below as it is necessary to rebuilt some archives #' => false);
     // update scheduled reports to use new plugin
     $reportsToReplace = array('UserSettings_getBrowserVersion' => 'DevicesDetection_getBrowserVersions', 'UserSettings_getBrowser' => 'DevicesDetection_getBrowsers', 'UserSettings_getOSFamily' => 'DevicesDetection_getOsFamilies', 'UserSettings_getOS' => 'DevicesDetection_getOsVersions', 'UserSettings_getMobileVsDesktop' => 'DevicesDetection_getType', 'UserSettings_getBrowserType' => 'DevicesDetection_getBrowserEngines', 'UserSettings_getWideScreen' => 'UserSettings_getScreenType');
     $reportTable = Common::prefixTable('report');
     foreach ($reportsToReplace as $old => $new) {
         $migrations[] = $this->migration->db->sql("UPDATE {$reportTable} SET reports = REPLACE(reports, '" . $old . "', '" . $new . "')");
     }
     // update dashboard to use new widgets
     $oldWidgets = array(array('module' => 'UserSettings', 'action' => 'getBrowserVersion', 'params' => array()), array('module' => 'UserSettings', 'action' => 'getBrowser', 'params' => array()), array('module' => 'UserSettings', 'action' => 'getOSFamily', 'params' => array()), array('module' => 'UserSettings', 'action' => 'getOS', 'params' => array()), array('module' => 'UserSettings', 'action' => 'getMobileVsDesktop', 'params' => array()), array('module' => 'UserSettings', 'action' => 'getBrowserType', 'params' => array()), array('module' => 'UserSettings', 'action' => 'getWideScreen', 'params' => array()));
     $newWidgets = array(array('module' => 'DevicesDetection', 'action' => 'getBrowserVersions', 'params' => array()), array('module' => 'DevicesDetection', 'action' => 'getBrowsers', 'params' => array()), array('module' => 'DevicesDetection', 'action' => 'getOsFamilies', 'params' => array()), array('module' => 'DevicesDetection', 'action' => 'getOsVersions', 'params' => array()), array('module' => 'DevicesDetection', 'action' => 'getType', 'params' => array()), array('module' => 'DevicesDetection', 'action' => 'getBrowserEngines', 'params' => array()), array('module' => 'UserSettings', 'action' => 'getScreenType', 'params' => array()));
     $allDashboards = Db::get()->fetchAll(sprintf("SELECT * FROM %s", Common::prefixTable('user_dashboard')));
     $dashboardTable = Common::prefixTable('user_dashboard');
     $dashboardQuery = "UPDATE {$dashboardTable} SET layout = ? WHERE iddashboard = ?";
     foreach ($allDashboards as $dashboard) {
         $dashboardLayout = json_decode($dashboard['layout']);
         $dashboardLayout = DashboardModel::replaceDashboardWidgets($dashboardLayout, $oldWidgets, $newWidgets);
         $newLayout = json_encode($dashboardLayout);
         if ($newLayout != $dashboard['layout']) {
             $migrations[] = $this->migration->db->boundSql($dashboardQuery, array($newLayout, $dashboard['iddashboard']));
         }
     }
     return $migrations;
 }
All Usage Examples Of Piwik\Plugins\Dashboard\Model::replaceDashboardWidgets