public function widgetsAction()
 {
     $contact_id = $this->getUserId();
     $widget_model = new waWidgetModel();
     $rows = $widget_model->getByContact($contact_id);
     $data = array();
     foreach ($rows as $row) {
         $data[$row['block']][] = $row;
     }
     $w = $b = 0;
     $real_block = 0;
     foreach ($data as $block => $block_data) {
         if ($real_block != $block) {
             $b++;
             $widget_model->updateByField(array('contact_id' => $contact_id, 'dashboard_id' => null, 'block' => $block), array('block' => $real_block));
         }
         foreach ($block_data as $sort => $row) {
             if ($row['sort'] != $sort) {
                 $widget_model->updateById($row['id'], array('sort' => $sort));
                 $w++;
             }
         }
         $real_block++;
     }
     echo 'OK';
     if ($b) {
         echo "\t" . $b . ' block(s) has been fixed.' . PHP_EOL;
     }
     if ($w) {
         echo "\t" . $w . ' widgets(s) has been fixed.' . PHP_EOL;
     }
 }
 public function editPublicAction()
 {
     if (!wa()->getUser()->isAdmin('webasyst')) {
         throw new waException('Access denied', 403);
     }
     $dashboard_id = waRequest::request('dashboard_id', 0, 'int');
     $dashboard_model = new waDashboardModel();
     $dashboard = $dashboard_model->getById($dashboard_id);
     if (!$dashboard) {
         throw new waException(_w('Not found'), 404);
     }
     // fetch widgets
     $widgets = array();
     $widget_model = new waWidgetModel();
     $rows = $widget_model->getByDashboard($dashboard_id);
     foreach ($rows as $row) {
         $app_widgets = wa($row['app_id'])->getConfig()->getWidgets();
         if (isset($app_widgets[$row['widget']])) {
             $row['size'] = explode('x', $row['size']);
             $row = $row + $app_widgets[$row['widget']];
             $row['href'] = wa()->getAppUrl($row['app_id']) . "?widget={$row['widget']}&id={$row['id']}";
             foreach ($row['sizes'] as $s) {
                 if ($s == array(1, 1)) {
                     $row['has_sizes']['small'] = true;
                 } elseif ($s == array(2, 1)) {
                     $row['has_sizes']['medium'] = true;
                 } elseif ($s == array(2, 2)) {
                     $row['has_sizes']['big'] = true;
                 }
             }
             $widgets[$row['block']][] = $row;
         }
     }
     $dashboard_url = wa()->getConfig()->getRootUrl(true) . wa()->getConfig()->getBackendUrl(false);
     $dashboard_url .= "/dashboard/{$dashboard['hash']}/";
     $this->display(array('dashboard' => $dashboard, 'dashboard_url' => $dashboard_url, 'header_date' => _ws(waDateTime::date('l')) . ', ' . trim(str_replace(date('Y'), '', waDateTime::format('humandate')), ' ,/'), 'widgets' => $widgets));
 }
 /**
  * @param $widget_id
  * @return waWidget
  * @throws waException
  */
 public function getWidget($widget_id)
 {
     $widget_model = new waWidgetModel();
     $widget = $widget_model->getById($widget_id);
     if ($widget) {
         if ($this->getConfig()->getApplication() != $widget['app_id']) {
             $path = self::getInstance($widget['app_id'])->getConfig()->getWidgetPath($widget['widget']);
         } else {
             $path = $this->getConfig()->getWidgetPath($widget['widget']);
         }
         $widget_path = $path . '/lib/config/widget.php';
         if (file_exists($widget_path)) {
             if ($widget['app_id'] == 'webasyst') {
                 $class_filename = $path . '/lib/' . $widget['widget'] . '.widget.php';
                 if (file_exists($class_filename)) {
                     require_once $class_filename;
                 } else {
                     throw new waException('Widget ' . $widget['widget'] . ' not found', 404);
                 }
                 $class = $widget['widget'] . 'Widget';
             } else {
                 $class = $widget['app_id'] . ucfirst($widget['widget']) . 'Widget';
             }
             if (!class_exists($class)) {
                 throw new waException('Widget class ' . $class . ' ' . $widget['widget'] . ' not found', 404);
             }
             $widget_config = (include $widget_path);
             $widget = $widget + $widget_config;
             if (isset($widget['img'])) {
                 $widget['img'] = 'wa-apps/' . $widget['app_id'] . '/widgets/' . $widget['widget'] . '/' . $widget['img'];
             }
             return new $class($widget);
         } else {
             throw new waException('Widget ' . $widget['widget'] . ' not found', 404);
         }
     } else {
         throw new waException('Widget ' . $widget_id . ' not found', 404);
     }
 }
 public function dashboardAction()
 {
     $widget_model = new waWidgetModel();
     $locale = wa()->getUser()->getLocale();
     // Create dashboard widgets on first login
     if (!wa()->getUser()->getSettings('webasyst', 'dashboard')) {
         $apps = wa()->getApps(true);
         $widgets = array();
         foreach ($apps as $app_id => $app) {
             if ($app_id == 'webasyst' || $this->getUser()->getRights($app_id, 'backend')) {
                 foreach (wa($app_id)->getConfig()->getWidgets() as $w_id => $w) {
                     if (!empty($w['locale']) && $w['locale'] != $locale) {
                         continue;
                     }
                     $widgets[] = $w;
                 }
             }
         }
         $block = 0;
         $contact_id = wa()->getUser()->getId();
         foreach ($widgets as $w) {
             $max_size = $w['sizes'][0];
             foreach ($w['sizes'] as $s) {
                 if ($s[0] + $s[1] > $max_size[0] + $max_size[1]) {
                     $max_size = $s;
                 }
             }
             $row = array('app_id' => $w['app_id'], 'widget' => $w['widget'], 'name' => $w['name'], 'block' => $block++, 'sort' => 0, 'size' => $max_size[0] . 'x' . $max_size[1], 'contact_id' => $contact_id, 'create_datetime' => date('Y-m-d H:i:s'));
             $widget_model->insert($row);
         }
         wa()->getUser()->setSettings('webasyst', 'dashboard', 1);
     }
     // fetch widgets
     $rows = $widget_model->getByContact($this->getUserId());
     $widgets = array();
     foreach ($rows as $row) {
         if ($row['app_id'] == 'webasyst' || $this->getUser()->getRights($row['app_id'], 'backend')) {
             $app_widgets = wa($row['app_id'])->getConfig()->getWidgets();
             if (isset($app_widgets[$row['widget']])) {
                 $row['size'] = explode('x', $row['size']);
                 $row = $row + $app_widgets[$row['widget']];
                 foreach ($row['sizes'] as $s) {
                     if ($s == array(1, 1)) {
                         $row['has_sizes']['small'] = true;
                     } elseif ($s == array(2, 1)) {
                         $row['has_sizes']['medium'] = true;
                     } elseif ($s == array(2, 2)) {
                         $row['has_sizes']['big'] = true;
                     }
                 }
                 $widgets[$row['block']][] = $row;
             }
         }
     }
     $this->view->assign('widgets', $widgets);
     // announcement
     $user = wa()->getUser();
     $announcement_model = new waAnnouncementModel();
     $apps = $user->getApps();
     $data = $announcement_model->getByApps($user->getId(), array_keys($apps), $user['create_datetime']);
     $announcements = array();
     $announcements_apps = array();
     foreach ($data as $row) {
         // show no more than 1 message per application
         if (!empty($announcements_apps[$row['app_id']])) {
             continue;
         }
         $announcements_apps[$row['app_id']] = true;
         $announcements[] = $row;
     }
     $this->view->assign('notifications', $announcements);
     // activity stream
     $activity_action = new webasystDashboardActivityAction();
     $this->view->assign('apps', wa()->getUser()->getApps());
     $user_filters = wa()->getUser()->getSettings('webasyst', 'dashboard_activity');
     if ($user_filters) {
         $user_filters = explode(',', $user_filters);
     } else {
         $user_filters = array();
     }
     $this->view->assign('user_filters', $user_filters);
     $this->view->assign('activity', $activity_action->getLogs(array(), $count));
     if ($count == 50) {
         $this->view->assign('activity_load_more', true);
     }
     // Whether to show tutorial
     $this->view->assign('show_tutorial', !wa()->getUser()->getSettings('webasyst', 'widget_tutorial_closed'));
 }
 protected function setParams($params)
 {
     $model = new waWidgetModel();
     $info = $this->getInfo();
     $model->setParams($info['id'], $params);
     $this->params = $params;
 }