Scalr_Account_User::setDashboard PHP Method

setDashboard() public method

Set user dashboard
public setDashboard ( integer $envId, array $value )
$envId integer
$value array
    public function setDashboard($envId, $value)
    {
        // check consistency
        $usedWidgets = array();
        if (is_array($value) && isset($value['configuration']) && is_array($value['configuration']) && isset($value['flags']) && is_array($value['flags'])) {
            $configuration = array();
            foreach ($value['configuration'] as $col) {
                if (is_array($col)) {
                    $column = array();
                    foreach ($col as $wid) {
                        if (is_array($wid) && isset($wid['name'])) {
                            // deprecated widgets
                            if (in_array($wid['name'], ['dashboard.usagelaststat', 'dashboard.uservoice'])) {
                                continue;
                            }
                            $usedWidgets[] = $wid['name'];
                            array_push($column, $wid);
                        }
                    }
                    array_push($configuration, $column);
                }
            }
            $value['configuration'] = $configuration;
            $value['widgets'] = array_values(array_unique($usedWidgets));
        } else {
            throw new Scalr_Exception_Core('Invalid configuration for dashboard');
        }
        $srlvalue = serialize($value);
        if (!$envId) {
            // if envId is NULL, foreign key doesn't work, remove possible record (todo: refactor)
            $this->db->Execute('DELETE FROM account_user_dashboard WHERE user_id = ? AND env_id IS NULL', [$this->id]);
        }
        $this->db->Execute("\n            INSERT account_user_dashboard\n            SET `user_id` = ?, `env_id` = ?, `value` = ?\n            ON DUPLICATE KEY UPDATE\n                `value` = ?\n        ", [$this->id, $envId, $srlvalue, $srlvalue]);
    }

Usage Example

 /**
  * Performs upgrade literally for the stage ONE.
  *
  * Implementation of this method performs update steps needs to be taken
  * to accomplish upgrade successfully.
  *
  * If there are any error during an execution of this scenario it must
  * throw an exception.
  *
  * @param   int  $stage  optional The stage number
  * @throws  \Exception
  */
 protected function run1($stage)
 {
     $dashboards = $this->db->Execute('SELECT user_id, env_id FROM account_user_dashboard');
     foreach ($dashboards as $keys) {
         try {
             $user = new \Scalr_Account_User();
             $user->loadById($keys['user_id']);
             $dash = $user->getDashboard($keys['env_id']);
             if (!(is_array($dash) && isset($dash['configuration']) && is_array($dash['configuration']) && isset($dash['flags']) && is_array($dash['flags']))) {
                 // old configuration, remove it
                 $this->db->Execute('DELETE FROM account_user_dashboard WHERE user_id = ? AND env_id = ?', array($keys['user_id'], $keys['env_id']));
                 continue;
             }
             foreach ($dash['configuration'] as &$column) {
                 foreach ($column as &$widget) {
                     if ($widget['name'] == 'dashboard.monitoring') {
                         $metrics = array('CPUSNMP' => 'cpu', 'LASNMP' => 'la', 'NETSNMP' => 'net', 'ServersNum' => 'snum', 'MEMSNMP' => 'mem');
                         $params = array('farmId' => $widget['params']['farmid'], 'period' => $widget['params']['graph_type'], 'metrics' => $metrics[$widget['params']['watchername']], 'title' => $widget['params']['title'], 'hash' => $this->db->GetOne('SELECT hash FROM farms WHERE id = ?', array($widget['params']['farmid'])));
                         if (stristr($widget['params']['role'], "INSTANCE_")) {
                             $ar = explode('_', $widget['params']['role']);
                             $params['farmRoleId'] = $ar[1];
                             $params['index'] = $ar[2];
                         } else {
                             if ($widget['params']['role'] != 'FARM' && $widget['params']['role'] != 'role') {
                                 $params['farmRoleId'] = $widget['params']['role'];
                             }
                         }
                         $widget['params'] = $params;
                     }
                 }
             }
             $user->setDashboard($keys['env_id'], $dash);
         } catch (\Exception $e) {
             $this->console->warning($e->getMessage());
         }
     }
 }