/**
  * Returns an array of moduleId and the their states (enabled, disabled, force enabled)
  * for given user  id. If space id is 0 or empty, the default states will be returned.
  * 
  * @param int $userId 
  * @return array State of Module Ids
  */
 public static function getStates($userId = 0)
 {
     if (isset(self::$_states[$userId])) {
         return self::$_states[$userId];
     }
     $states = Yii::app()->cache->get(self::STATES_CACHE_ID_PREFIX . $userId);
     if ($states === false) {
         $states = array();
         foreach (UserApplicationModule::model()->findAllByAttributes(array('user_id' => $userId)) as $userModule) {
             $states[$userModule->module_id] = $userModule->state;
         }
         Yii::app()->cache->set(self::STATES_CACHE_ID_PREFIX . $userId, $states);
     }
     self::$_states[$userId] = $states;
     return self::$_states[$userId];
 }
Exemple #2
0
 /**
  * Disables a module
  *
  * Which means delete all (user-) data created by the module.
  *
  */
 public function disable()
 {
     if (!$this->isEnabled() || $this->isCoreModule) {
         return false;
     }
     // Check this module is a SpaceModule
     if ($this->isSpaceModule()) {
         foreach ($this->getSpaceModuleSpaces() as $space) {
             $space->disableModule($this->getId());
         }
     }
     // Check this module is a UserModule
     if ($this->isUserModule()) {
         foreach ($this->getUserModuleUsers() as $user) {
             $user->disableModule($this->getId());
         }
     }
     // Disable module in database
     $moduleEnabled = ModuleEnabled::model()->findByPk($this->getId());
     if ($moduleEnabled != null) {
         $moduleEnabled->delete();
     }
     HSetting::model()->deleteAllByAttributes(array('module_id' => $this->getId()));
     SpaceSetting::model()->deleteAllByAttributes(array('module_id' => $this->getId()));
     UserSetting::model()->deleteAllByAttributes(array('module_id' => $this->getId()));
     // Delete also records with disabled state from SpaceApplicationModule Table
     foreach (SpaceApplicationModule::model()->findAllByAttributes(array('module_id' => $this->getId())) as $sam) {
         $sam->delete();
     }
     // Delete also records with disabled state from UserApplicationModule Table
     foreach (UserApplicationModule::model()->findAllByAttributes(array('module_id' => $this->getId())) as $uam) {
         $uam->delete();
     }
     ModuleManager::flushCache();
     return true;
 }
 /**
  * Sets default enabled/disabled on User or/and Space Modules
  *
  * @throws CHttpException
  */
 public function actionSetAsDefault()
 {
     $moduleId = Yii::app()->request->getQuery('moduleId');
     $module = Yii::app()->moduleManager->getModule($moduleId);
     if ($module == null) {
         throw new CHttpException(500, Yii::t('AdminModule.controllers_ModuleController', 'Could not find requested module!'));
     }
     $model = new ModuleSetAsDefaultForm();
     $spaceDefaultModule = null;
     if ($module->isSpaceModule()) {
         $spaceDefaultModule = SpaceApplicationModule::model()->findByAttributes(array('space_id' => 0, 'module_id' => $moduleId));
         if ($spaceDefaultModule === null) {
             $spaceDefaultModule = new SpaceApplicationModule();
             $spaceDefaultModule->module_id = $moduleId;
             $spaceDefaultModule->space_id = 0;
             $spaceDefaultModule->state = SpaceApplicationModule::STATE_DISABLED;
         }
         $model->spaceDefaultState = $spaceDefaultModule->state;
     }
     $userDefaultModule = null;
     if ($module->isUserModule()) {
         $userDefaultModule = UserApplicationModule::model()->findByAttributes(array('user_id' => 0, 'module_id' => $moduleId));
         if ($userDefaultModule === null) {
             $userDefaultModule = new UserApplicationModule();
             $userDefaultModule->module_id = $moduleId;
             $userDefaultModule->user_id = 0;
             $userDefaultModule->state = UserApplicationModule::STATE_DISABLED;
         }
         $model->userDefaultState = $userDefaultModule->state;
     }
     if (isset($_POST['ModuleSetAsDefaultForm'])) {
         $_POST['ModuleSetAsDefaultForm'] = Yii::app()->input->stripClean($_POST['ModuleSetAsDefaultForm']);
         $model->attributes = $_POST['ModuleSetAsDefaultForm'];
         if ($model->validate()) {
             if ($module->isSpaceModule()) {
                 $spaceDefaultModule->state = $model->spaceDefaultState;
                 $spaceDefaultModule->save();
             }
             if ($module->isUserModule()) {
                 $userDefaultModule->state = $model->userDefaultState;
                 $userDefaultModule->save();
             }
             // close modal
             $this->renderModalClose();
         }
     }
     $this->renderPartial('setAsDefault', array('module' => $module, 'model' => $model), false, true);
 }
 /**
  * Disables a Module
  */
 public function disableModule($moduleId)
 {
     // Not enabled globally
     if (!array_key_exists($moduleId, $this->getAvailableModules())) {
         return false;
     }
     // Already enabled module
     if (!$this->isModuleEnabled($moduleId)) {
         Yii::log("User->disableModule(" . $moduleId . ") module is not enabled");
         return false;
     }
     // New Way: Handle it directly in module class
     $module = Yii::app()->moduleManager->getModule($moduleId);
     $module->disableUserModule($this->getOwner());
     $userModule = UserApplicationModule::model()->findByAttributes(array('user_id' => $this->getOwner()->id, 'module_id' => $moduleId));
     if ($userModule == null) {
         $userModule = new UserApplicationModule();
         $userModule->user_id = $this->getOwner()->id;
         $userModule->module_id = $moduleId;
     }
     $userModule->state = UserApplicationModule::STATE_DISABLED;
     $userModule->save();
     return true;
 }