/**
  *
  * @param $uid
  * @param $table
  * @return string
  */
 public static function getVisibleFlagsForElement($uid, $table)
 {
     $cacheKey = $uid . ':' . $table;
     $cacheManager = tx_languagevisibility_cacheManager::getInstance();
     $isCacheEnabled = $cacheManager->isCacheEnabled();
     $cacheData = $cacheManager->get('visibleFlagsCache');
     if (!$isCacheEnabled || !isset($cacheData[$cacheKey])) {
         $dao = t3lib_div::makeInstance('tx_languagevisibility_daocommon');
         if (version_compare(TYPO3_version, '4.3.0', '<')) {
             $elementfactoryName = t3lib_div::makeInstanceClassName('tx_languagevisibility_elementFactory');
             $elementfactory = new $elementfactoryName($dao);
         } else {
             $elementfactory = t3lib_div::makeInstance('tx_languagevisibility_elementFactory', $dao);
         }
         try {
             $element = $elementfactory->getElementForTable($table, $uid);
         } catch (Exception $e) {
             return '-';
         }
         $languageRep = t3lib_div::makeInstance('tx_languagevisibility_languagerepository');
         $languageList = $languageRep->getLanguages();
         $visibility = t3lib_div::makeInstance('tx_languagevisibility_visibilityService');
         $visibleFlags = array();
         foreach ($languageList as $language) {
             if ($visibility->isVisible($language, $element)) {
                 $visibleFlags[] = $language->getFlagImg(0);
             }
         }
         $cacheData[$cacheKey] = implode('', $visibleFlags);
         $cacheManager->set('visibleFlagsCache', $cacheData);
     }
     return $cacheData[$cacheKey];
 }
 /**
  * @return void
  */
 function setUp()
 {
     /*
      * save the current environment  this should allways be done first because
      * database tests may get skipped because no testdatabase exists
      */
     $this->environmentSaver = new tx_languagevisibility_tests_helper_environmentHelper();
     $this->environmentSaver->save();
     $this->createDatabase();
     $db = $this->useTestDatabase();
     $this->importStdDB();
     $GLOBALS["TYPO3_CONF_VARS"]["FE"]["addRootLineFields"] = "tx_languagevisibility_inheritanceflag_original,tx_languagevisibility_inheritanceflag_overlayed";
     $GLOBALS["TYPO3_CONF_VARS"]["FE"]["pageOverlayFields"] = "uid,title,subtitle,nav_title,media,keywords,description,abstract,author,author_email,sys_language_uid,tx_languagevisibility_inheritanceflag_overlayed";
     unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['languagevisibility']);
     // order of extension-loading is important !!!!
     if (version_compare(TYPO3_version, '4.5.0', '<')) {
         $this->importExtensions(array('cms', 'languagevisibility'));
     } else {
         $this->importExtensions(array('cms', 'extbase', 'fluid', 'version', 'workspaces', 'languagevisibility'));
     }
     tx_languagevisibility_cacheManager::getInstance()->flushAllCaches();
 }
 /**
  * This method is used to initialize new Elements with the default
  *
  * @param unknown_type $status
  * @param unknown_type $table
  * @param unknown_type $id
  * @param unknown_type $fieldArray
  * @param unknown_type $reference
  */
 public function processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, &$reference)
 {
     if (in_array($table, tx_languagevisibility_visibilityService::getSupportedTables())) {
         /**
          * Now we set the default visibility for elements which did not get a defaultvisibility array.
          * This can happen, if a user creates a new element AND the user has no access for the languagevisibility_field
          */
         if ($status == 'new') {
             $row['uid'] = $reference->substNEWwithIDs[$id];
             if ($fieldArray['pid'] == '-1') {
                 $row = t3lib_BEfunc::getWorkspaceVersionOfRecord($fieldArray['t3ver_wsid'], $table, $row['uid'], $fields = '*');
             }
             require_once t3lib_extMgm::extPath("languagevisibility") . 'class.tx_languagevisibility_beservices.php';
             $newdata = array('tx_languagevisibility_visibility' => serialize(tx_languagevisibility_beservices::getDefaultVisibilityArray()));
             $where = "tx_languagevisibility_visibility = '' AND uid=" . $row['uid'];
             $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, $where, $newdata);
         }
         tx_languagevisibility_cacheManager::getInstance()->flushAllCaches();
     }
 }
 /**
  * Returns an instance for a language by the id.
  * Note: since the language is an value object all languages can be cached
  *
  * @param $id
  * @return tx_languagevisibility_language
  */
 public function getLanguageById($id)
 {
     $cacheManager = tx_languagevisibility_cacheManager::getInstance();
     $cacheData = $cacheManager->get('languagesCache');
     $isCacheEnabled = $cacheManager->isCacheEnabled();
     if (!$isCacheEnabled || !isset($cacheData[$id])) {
         if ($id == 0) {
             $cacheData[$id] = $this->getDefaultLanguage();
         } else {
             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_language', 'uid=' . intval($id), '', '', '');
             $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
             $language = t3lib_div::makeInstance('tx_languagevisibility_language');
             $language->setData($row);
             $cacheData[$id] = $language;
             $GLOBALS['TYPO3_DB']->sql_free_result($res);
             $cacheManager->set('languagesCache', $cacheData);
         }
     }
     return $cacheData[$id];
 }
 /**
  * This method is used to test if a disabled cache returns cache hits.
  *
  * @test
  * @return void
  */
 public function disabledCacheReturnsNoData()
 {
     $cache = tx_languagevisibility_cacheManager::getInstance();
     $cache->enableCache();
     $cache->flushAllCaches();
     $cache->set('aaaa', 12);
     $this->assertEquals(12, $cache->get('aaaa'));
     $cache->disableCache();
     $this->assertEquals(array(), $cache->get('aaaa'));
     $cache->enableCache();
     $this->assertEquals(12, $cache->get('aaaa'));
 }
 /**
  * Returns an instance of the cacheManager singleton.
  *
  * @return  tx_languagevisibility_cacheManager
  */
 public static function getInstance()
 {
     if (!self::$instance instanceof tx_languagevisibility_cacheManager) {
         self::$instance = new tx_languagevisibility_cacheManager();
     }
     return self::$instance;
 }
 /**
  * This method is used to retrieve an overlay record of a given record.
  *
  * @param $languageId
  * @param $onlyUid
  * @return array
  */
 public function getOverLayRecordForCertainLanguage($languageId, $onlyUid = false)
 {
     //get caching hints
     $table = $this->getTable();
     $uid = $this->getUid();
     $workspace = intval($GLOBALS['BE_USER']->workspace);
     $cacheManager = tx_languagevisibility_cacheManager::getInstance();
     $cacheData = $cacheManager->get('overlayRecordCache');
     $isCacheEnabled = $cacheManager->isCacheEnabled();
     if (!$isCacheEnabled || !isset($cacheData[$table][$uid][$languageId][$workspace])) {
         $cacheData[$table][$uid][$languageId][$workspace] = $this->getOverLayRecordForCertainLanguageImplementation($languageId);
         $cacheManager->set('overlayRecordCache', $cacheData);
     }
     return $cacheData[$table][$uid][$languageId][$workspace];
 }
 /**
  * Returns a record by table and uid.
  *
  * @param $uid
  * @param $table
  * @return array
  */
 public static function getRecords($table, $where)
 {
     $cacheManager = tx_languagevisibility_cacheManager::getInstance();
     $isCacheEnabled = $cacheManager->isCacheEnabled();
     self::$recordCache = $cacheManager->get('daoRecordCache');
     if ($isCacheEnabled) {
         if (!isset(self::$recordCache[$table][$where])) {
             //!TODO we're still running two queries - this can be reduced to one with a tricky search criteria
             $rows = self::getRequestedRecords($table, $where);
             if ($rows) {
                 self::$recordCache[$table][$where] = $rows;
             }
         }
         $cacheManager->set('daoRecordCache', self::$recordCache);
         $result = self::$recordCache[$table][$where];
     } else {
         $result = self::getRequestedRecords($table, $where);
     }
     return $result;
 }
 /**
  * @return void
  */
 public function setUp()
 {
     $this->environmentSaver = new tx_languagevisibility_tests_helper_environmentHelper();
     $this->environmentSaver->save();
     tx_languagevisibility_cacheManager::getInstance()->flushAllCaches();
 }
 /**
  * We have the same situation as before but in this
  * testcase we test the visibility of page c and change
  * the pid afterward. Because the visibility is forced
  * to no by inheritance, it should normaly be visible,
  * but the result of the visibility is chached in
  * that situation and the visibility will only change afterwards
  * when the cache was flushed.
  *
  * @param void
  * @return void
  * @author Timo Schmidt <*****@*****.**>
  * @test
  */
 public function changeOfPidDoesNotInfluenceCachedResult()
 {
     $cacheManager = tx_languagevisibility_cacheManager::getInstance();
     $isCacheEnabled = $cacheManager->isCacheEnabled();
     $this->assertTrue($isCacheEnabled, 'Cache needs to be enabled to perform this test');
     $this->importDataSet(dirname(__FILE__) . '/fixtures/canDetermineInheritedVisibility.xml');
     $fixtureLanguageRow = array('uid' => 1, 'tx_languagevisibility_defaultvisibility' => 't');
     $fixtureLanguage = new tx_languagevisibility_language();
     $fixtureLanguage->setData($fixtureLanguageRow);
     $dao = new tx_languagevisibility_daocommon();
     $elementFactory = new tx_languagevisibility_elementFactory($dao);
     $fixtureElement = $elementFactory->getElementForTable('pages', 3);
     $visibilityService = new tx_languagevisibility_visibilityService();
     $visibilityResult = true;
     $visibilityResult = $visibilityService->isVisible($fixtureLanguage, $fixtureElement);
     $this->assertFalse($visibilityResult, 'The element should not be visibile because of the inherited force to no setting');
     $db = $GLOBALS['TYPO3_DB'];
     /* @var  $db t3lib_db */
     $db->exec_UPDATEquery('pages', 'pid=2', array('pid' => 0));
     $visibilityResult = $visibilityService->isVisible($fixtureLanguage, $fixtureElement);
     $this->assertFalse($visibilityResult, 'The element should not still not be visible because the visibility result is cached');
     tx_languagevisibility_cacheManager::getInstance()->flushAllCaches();
     $visibilityResult = $visibilityService->isVisible($fixtureLanguage, $fixtureElement);
     $this->assertTrue($visibilityResult, 'Now the element should be visible because the cache was flushed');
 }
 /**
  * Determines the dataStructure from a given sourcePointer.
  *
  * @param $srcPointer
  * @return array
  */
 protected function _getTVDS($srcPointer)
 {
     $cacheManager = tx_languagevisibility_cacheManager::getInstance();
     $cacheData = $cacheManager->get('dataStructureCache');
     $isCacheEnabled = $cacheManager->isCacheEnabled();
     if (!$isCacheEnabled || !isset($cacheData[$srcPointer])) {
         $DS = array();
         if (t3lib_div::testInt($srcPointer)) {
             // If integer, then its a record we will look up:
             $sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
             $DSrec = $sys_page->getRawRecord('tx_templavoila_datastructure', $srcPointer, 'dataprot');
             $DS = t3lib_div::xml2array($DSrec['dataprot']);
         } else {
             // Otherwise expect it to be a file:
             $file = t3lib_div::getFileAbsFileName($srcPointer);
             if ($file && @is_file($file)) {
                 $DS = t3lib_div::xml2array(t3lib_div::getUrl($file));
             }
         }
         $cacheData[$srcPointer] = $DS;
         $cacheManager->set('dataStructureCache', $cacheData);
     }
     return $cacheData[$srcPointer];
 }
 /**
  * return the accumulated visibility setting: reads default for language then reads local for element and merges them.
  * if local is default, then the global is used or it is forced to be "yes" if the language was set to all.
  * if the element itself is a translated original record the element is only visible in the specific language
  * If nothing is set the hardcoded default "t" (translated) is returned
  *
  * @param tx_languagevisibility_language $language
  * @param tx_languagevisibility_element $element
  * @param boolean
  * @return string
  */
 public function getVisibilitySetting(tx_languagevisibility_language $language, tx_languagevisibility_element $element, $omitLocal = false)
 {
     $cacheManager = tx_languagevisibility_cacheManager::getInstance();
     $cacheData = $cacheManager->get('visibilitySettingCache');
     $isCacheEnabled = $cacheManager->isCacheEnabled();
     $elementTable = $element->getTable();
     $elementUid = $element->getUid();
     $languageUid = $language->getUid();
     $cacheKey = $languageUid . '_' . $elementUid . '_' . $elementTable . '_' . $omitLocal;
     if (!$isCacheEnabled || !isset($cacheData[$cacheKey])) {
         $cacheData[$cacheKey] = $this->getVisibility($language, $element, $omitLocal)->getVisibilityString();
         $cacheManager->set('visibilitySettingCache', $cacheData);
     }
     return $cacheData[$cacheKey];
 }