예제 #1
0
 /**
  * Init dashboard check early.
  */
 public function init()
 {
     // Don't run when in console
     if (!craft()->isConsole()) {
         // Get current user
         $currentUser = craft()->userSession->getUser();
         // Make sure we're logged in onto CP
         if ($currentUser && $currentUser->can('accessCp') && craft()->request->isCpRequest()) {
             // Check if this user already has widgets
             $userWidgetRecords = WidgetRecord::model()->ordered()->findAllByAttributes(array('userId' => $currentUser->id));
             if (!$userWidgetRecords) {
                 // Get enabled admin widgets
                 $adminWidgetRecords = WidgetRecord::model()->ordered()->findAllByAttributes(array('userId' => 1, 'enabled' => 1));
                 // Populate widget models
                 $adminWidgets = WidgetModel::populateModels($adminWidgetRecords);
                 // Loop through widget models
                 foreach ($adminWidgets as $widget) {
                     // Clear widget id
                     $widget->id = null;
                     // Save on user
                     $result = craft()->dashboard->saveUserWidget($widget);
                     // If that worked then we should have an id on the model
                     // and can go ahead and update the colspan
                     if ($result && $widget->id) {
                         craft()->dashboard->changeWidgetColspan($widget->id, $widget->colspan);
                     }
                 }
             }
         }
     }
 }
 /**
  * Returns the dashboard widgets for the current user.
  *
  * @param string|null $indexBy
  *
  * @return array
  */
 public function getUserWidgets($indexBy = null)
 {
     $widgetRecords = $this->_getUserWidgetRecords();
     // If there are no widget records, this is the first time they've hit the dashboard.
     if (!$widgetRecords) {
         // Add the defaults and try again
         $this->_addDefaultUserWidgets();
         $widgetRecords = $this->_getUserWidgetRecords();
     } else {
         // Get only the enabled widgets.
         foreach ($widgetRecords as $key => $widgetRecord) {
             if (!$widgetRecord->enabled) {
                 unset($widgetRecords[$key]);
             }
         }
     }
     if (count($widgetRecords) > 0) {
         return WidgetModel::populateModels($widgetRecords, $indexBy);
     }
     return array();
 }