private static function filterRows($rows, $onlyPrioritized)
 {
     if (!$onlyPrioritized) {
         return $rows;
     }
     $idMap = array();
     foreach ($rows as $row) {
         if (!isset($idMap[$row['id']])) {
             $idMap[$row['id']] = array();
         }
         $idMap[$row['id']][] = $row;
     }
     $rows = array();
     foreach ($idMap as $id => $langRows) {
         $rows[] = eZURLAliasML::choosePrioritizedRow($langRows);
     }
     return $rows;
 }
 public function testChoosePrioritizedRow()
 {
     // Make sure we can see all languages
     ezpINIHelper::setINISetting('site.ini', 'RegionalSettings', 'ShowUntranslatedObjects', 'enabled');
     $action = "eznode:" . mt_rand();
     $name = __FUNCTION__ . mt_rand();
     $engGB = eZContentLanguage::fetchByLocale('eng-GB');
     $norNO = eZContentLanguage::fetchByLocale('nor-NO');
     // Create an english entry
     $url1 = eZURLAliasML::create($name . " en", $action, 0, $engGB->attribute('id'));
     $url1->store();
     // Create a norwegian entry
     $url2 = eZURLAliasML::create($name . " no", $action, 0, $norNO->attribute('id'));
     $url2->store();
     // Fetch the created entries. choosePrioritizedRow() wants rows from the
     // database so our eZURLAliasML objects wont work.
     $db = eZDB::instance();
     $rows = $db->arrayQuery("SELECT * FROM ezurlalias_ml where action = '{$action}'");
     // -------------------------------------------------------------------
     // TEST PART 1 - NORMAL PRIORITIZATION -------------------------------
     // The order of the language array also determines the prioritization.
     // In this case 'eng-GB' should be prioritized before 'nor-NO'.
     $languageList = array("eng-GB", "nor-NO");
     ezpINIHelper::setINISetting('site.ini', 'RegionalSettings', 'SiteLanguageList', $languageList);
     eZContentLanguage::clearPrioritizedLanguages();
     $row = eZURLAliasML::choosePrioritizedRow($rows);
     // The prioritzed language should be 'eng-GB'
     self::assertEquals($engGB->attribute('id'), $row["lang_mask"]);
     // -------------------------------------------------------------------
     // TEST PART 2 - REVERSED PRIORITIZATION -----------------------------
     // Reverse the order of the specified languages, this will also
     // reverse the priority.
     $languageList = array_reverse($languageList);
     ezpINIHelper::setINISetting('site.ini', 'RegionalSettings', 'SiteLanguageList', $languageList);
     eZContentLanguage::clearPrioritizedLanguages();
     $row = eZURLAliasML::choosePrioritizedRow($rows);
     // The prioritzed language should be 'nor-NO'
     self::assertEquals($norNO->attribute('id'), $row["lang_mask"]);
     // -------------------------------------------------------------------
     // TEST TEAR DOWN ----------------------------------------------------
     ezpINIHelper::restoreINISettings();
     // -------------------------------------------------------------------
 }
 function getPathArray()
 {
     if ($this->PathArray !== null) {
         return $this->PathArray;
     }
     // Fetch path 'text' elements of correct parent path
     $path = array($this);
     $id = (int) $this->Parent;
     $db = eZDB::instance();
     while ($id != 0) {
         $query = "SELECT * FROM ezurlalias_ml WHERE id={$id}";
         $rows = $db->arrayQuery($query);
         if (count($rows) == 0) {
             break;
         }
         $result = eZURLAliasML::choosePrioritizedRow($rows);
         if (!$result) {
             $result = $rows[0];
         }
         $id = (int) $result['parent'];
         array_unshift($path, new eZPathElement($result));
     }
     $this->PathArray = $path;
     return $this->PathArray;
 }
 public function testChoosePrioritizedRow()
 {
     // TEST SETUP --------------------------------------------------------
     $ini = eZINI::instance();
     // Make sure to preserve ini settings in case other tests depend on them
     $orgShowUntranslatedObjects = $ini->variable('RegionalSettings', 'ShowUntranslatedObjects');
     $orgSiteLanguageList = $ini->variable('RegionalSettings', 'SiteLanguageList');
     // Make sure we can see all languages
     $ini->setVariable('RegionalSettings', 'ShowUntranslatedObjects', 'enabled');
     $action = "eznode:" . mt_rand();
     $name = __FUNCTION__ . mt_rand();
     // Create an english entry
     $url1 = eZURLAliasML::create($name . " en", $action, 0, 2);
     $url1->store();
     // Create a norwegian entry
     $url2 = eZURLAliasML::create($name . " no", $action, 0, 4);
     $url2->store();
     // Fetch the created entries. choosePrioritizedRow() wants rows from the
     // database so our eZURLAliasML objects wont work.
     $db = eZDB::instance();
     $rows = $db->arrayQuery("SELECT * FROM ezurlalias_ml where action = '{$action}'");
     // -------------------------------------------------------------------
     // TEST PART 1 - NORMAL PRIORITIZATION -------------------------------
     // The order of the language array also determines the prioritization.
     // In this case 'eng-GB' should be prioritized before 'nor-NO'.
     $languageList = array("eng-GB", "nor-NO");
     $ini->setVariable('RegionalSettings', 'SiteLanguageList', $languageList);
     eZContentLanguage::clearPrioritizedLanguages();
     $row = eZURLAliasML::choosePrioritizedRow($rows);
     // The prioritzed language should be 'eng-GB' (lang_mask = 2)
     self::assertEquals(2, $row["lang_mask"]);
     // -------------------------------------------------------------------
     // TEST PART 2 - REVERSED PRIORITIZATION -----------------------------
     // Reverse the order of the specified languages, this will also
     // reverse the priority.
     $languageList = array_reverse($languageList);
     $ini->setVariable('RegionalSettings', 'SiteLanguageList', $languageList);
     eZContentLanguage::clearPrioritizedLanguages();
     $row = eZURLAliasML::choosePrioritizedRow($rows);
     // The prioritzed language should be 'nor-NO' (lang_mask = 4)
     self::assertEquals(4, $row["lang_mask"]);
     // -------------------------------------------------------------------
     // TEST TEAR DOWN ----------------------------------------------------
     $ini->setVariable('RegionalSettings', 'ShowUntranslatedObjects', $orgShowUntranslatedObjects);
     $ini->setVariable('RegionalSettings', 'SiteLanguageList', $orgSiteLanguageList);
     // -------------------------------------------------------------------
 }