/** * This method is a simplification of loadPreferences() * It is created mostly because of the performance reasons. * The method loads given preferences in given account only. * * It doesn't take into account any preferences cascading so * before using this method be sure that the performance you are * looking for can't cascade (this is often the case for the preferences * which are available only in given accounts). * * This method uses by default static in-memory caching to improve * the speed of reading in the preferences - this is helpful in maintenance * where admin preferences can be read in many places and many times. * */ function loadPreferencesByNameAndAccount($accountId, $aPreferencesNames, $accountType, $useCache = true) { $aPrefs = OA_Preferences::cachePreferences($accountId, $aPreferencesNames); if (count($aPrefs) == count($aPreferencesNames)) { return $aPrefs; } $aPrefsIds = OA_Preferences::getCachedPreferencesIds($aPreferencesNames, $accountType); $doAccount_preference_assoc = OA_Dal::factoryDO('account_preference_assoc'); $doAccount_preference_assoc->account_id = $accountId; $doAccount_preference_assoc->whereInAdd('preference_id', $aPrefsIds); $doAccount_preference_assoc->find(); $aPrefs = array(); $prefsIdsFlip = array_flip($aPrefsIds); while ($doAccount_preference_assoc->fetch()) { $aPrefs[$prefsIdsFlip[$doAccount_preference_assoc->preference_id]] = $doAccount_preference_assoc->value; } OA_Preferences::cachePreferences($accountId, $aPrefs); return $aPrefs; }
function testCacheGetPreferenceIds() { $expectedPrefs = array('pref1' => 1, 'pref2' => 2); $this->_createPreferences($expectedPrefs); $prefs = OA_Preferences::getPreferenceIds(array(''), 'ADMIN'); $this->assertEqual($prefs, array()); $prefs = OA_Preferences::getCachedPreferencesIds(array(''), 'ADMIN'); $this->assertEqual($prefs, array()); $expectedPref1 = array('pref1' => 1); $prefs = OA_Preferences::getPreferenceIds(array('pref1'), 'ADMIN'); $this->assertEqual($prefs, $expectedPref1); $prefs = OA_Preferences::getCachedPreferencesIds(array('pref1'), 'ADMIN'); $this->assertEqual($prefs, $expectedPref1); $prefs = OA_Preferences::getPreferenceIds(array('pref1', 'pref2'), 'ADMIN'); $this->assertEqual($prefs, $expectedPrefs); $prefs = OA_Preferences::getCachedPreferencesIds(array('pref1', 'pref2'), 'ADMIN'); $this->assertEqual($prefs, $expectedPrefs); // change one of the cached preferences and check that getCachedPreferencesIds() still returns // old values $doPreferences = OA_Dal::factoryDO('preferences'); $doPreferences->preference_id = 1; $doPreferences->preference_name = 'test'; $doPreferences->update(); $prefs = OA_Preferences::getCachedPreferencesIds(array('pref1', 'pref2'), 'ADMIN'); $this->assertEqual($prefs, $expectedPrefs); $prefs = OA_Preferences::getCachedPreferencesIds(array('test'), 'ADMIN'); $this->assertEqual($prefs, array('test' => 1)); }