Ejemplo n.º 1
0
 /**
  * @param \Terminal42\ChangeLanguage\Event\ChangelanguageNavigationEvent $event
  * @return null
  */
 public function translateUrlParameters(\Terminal42\ChangeLanguage\Event\ChangelanguageNavigationEvent $event)
 {
     global $objPage;
     $targetRoot = $event->getNavigationItem()->getRootPage();
     $strLanguage = $targetRoot->rootLanguage ? $targetRoot->rootLanguage : $targetRoot->language;
     // The target language
     // Find your current and new alias from the current URL
     if (!\Config::get('useAutoItem')) {
         return null;
     }
     $varAlias = \Input::get('auto_item');
     if (isset($objPage->addTranslateUrl) && $objPage->addTranslateUrl == '1') {
         $this->strTable = $objPage->translateUrl;
     }
     if (!$this->strTable) {
         return null;
     }
     $table = $this->strTable;
     $tableData = $this->strTable . '_data';
     // get current item
     $objItem = $this->Database->prepare('SELECT ' . $tableData . '.*, ' . $table . '.fallback, ' . $table . '.language FROM ' . $tableData . ' LEFT OUTER JOIN ' . $table . ' ON ' . $tableData . '.pid = ' . $table . '.id WHERE ' . $tableData . '.alias = ? OR ' . $tableData . '.id = ?')->limit(1)->execute($varAlias, (int) $varAlias);
     $newAlias = '';
     if ($objItem->numRows) {
         // get all items with the same fallback item
         $fallback = !$objItem->fallback ? $objItem->mainLanguage : $objItem->id;
         // select alias
         $objTranslation = $this->Database->prepare('SELECT ' . $tableData . '.alias, ' . $tableData . '.id, ' . $tableData . '.mainLanguage, ' . $table . '.language FROM ' . $tableData . ' LEFT OUTER JOIN ' . $table . ' ON ' . $tableData . '.pid = ' . $table . '.id WHERE ' . $table . '.language = ? AND (' . $tableData . '.id = ? OR ' . $tableData . '.mainLanguage = ?)')->execute($strLanguage, $fallback, $fallback);
         if ($objTranslation->numRows) {
             $newAlias = $objTranslation->alias ? $objTranslation->alias : $objTranslation->id;
         }
     }
     // Pass the new alias to ChangeLanguage
     $event->getUrlParameterBag()->setUrlAttribute('items', $newAlias);
 }
 /**
  * Find record based on languageMain field and parent master archive
  *
  * @param ChangelanguageNavigationEvent $event
  */
 public function onChangelanguageNavigation(ChangelanguageNavigationEvent $event)
 {
     $navigationItem = $event->getNavigationItem();
     if ($navigationItem->isCurrentPage()) {
         return;
     }
     $current = $this->findCurrent();
     if (null === $current) {
         return;
     }
     // Remove the news/event/faq alias from the URL if there is no actual reader page assigned
     if (!$navigationItem->isDirectFallback()) {
         $event->getUrlParameterBag()->removeUrlAttribute($this->getUrlKey());
     }
     $t = $current::getTable();
     $parent = $current->getRelated('pid');
     if (0 === (int) $parent->master) {
         $mainId = (int) $current->id;
         $masterId = (int) $current->pid;
     } else {
         $mainId = (int) $current->languageMain;
         $masterId = (int) $parent->master;
     }
     // Abort if current record has no translated version
     if (0 === $mainId || 0 === $masterId) {
         $navigationItem->setIsDirectFallback(false);
         return;
     }
     $translated = $this->findPublishedBy(array("({$t}.id=? OR {$t}.languageMain=?)", sprintf('%s.pid=(SELECT id FROM %s WHERE (id=? OR master=?) AND jumpTo=?)', $t, $parent::getTable())), array($mainId, $mainId, $masterId, $masterId, $navigationItem->getTargetPage()->id));
     if (null === $translated) {
         $navigationItem->setIsDirectFallback(false);
         return;
     }
     $event->getUrlParameterBag()->setUrlAttribute($this->getUrlKey(), $translated->alias ?: $translated->id);
 }
 /**
  * Translate URL parameters for articles.
  *
  * @param ChangelanguageNavigationEvent $event
  */
 public function onChangelanguageNavigation(ChangelanguageNavigationEvent $event)
 {
     // Try to find matching article
     if ($event->getNavigationItem()->isCurrentPage() || !$event->getUrlParameterBag()->hasUrlAttribute('articles')) {
         return;
     }
     /** @var PageModel $objPage */
     global $objPage;
     $parameterBag = $event->getUrlParameterBag();
     $currentArticle = ArticleModel::findByIdOrAliasAndPid($parameterBag->getUrlAttribute('articles'), $objPage->id);
     if (null === $currentArticle) {
         return;
     }
     $pageFinder = new PageFinder();
     $targetRoot = $event->getNavigationItem()->getRootPage();
     $masterRoot = $pageFinder->findMasterRootForPage($targetRoot);
     $targetArticle = $this->findTargetArticle($currentArticle, $targetRoot->id, $objPage->rootId === $masterRoot->id, null !== $masterRoot && $targetRoot->id === $masterRoot->id);
     if (null === $targetArticle) {
         $parameterBag->removeUrlAttribute('articles');
     } else {
         $parameterBag->setUrlAttribute('articles', $targetArticle->alias);
     }
 }