Newscoop\Service\Implementation\ThemeManagementServiceLocal::assignOutputSetting PHP Метод

assignOutputSetting() публичный Метод

public assignOutputSetting ( OutputSettings $outputSettings, Theme $theme )
$outputSettings Newscoop\Entity\OutputSettings
$theme Newscoop\Entity\Theme
    public function assignOutputSetting(OutputSettings $outputSettings, Theme $theme)
    {
        Validation::notEmpty($outputSettings, 'outputSettings');
        Validation::notEmpty($theme, 'theme');
        // We update the XML config file with the new output setting.
        $xml = $this->loadXML($this->toFullPath($theme, $this->themeConfigFileName));
        if ($xml == NULL) {
            throw new \Exception("Unknown theme path '.{$theme->gePath}().' to assign to.");
        }
        $outNode = NULL;
        $nodes = $this->getNodes($xml, self::TAG_OUTPUT, self::ATTR_OUTPUT_NAME, $outputSettings->getOutput()->getName());
        if (count($nodes) == 0) {
            // The ouput node does not exist, we need to add it.
            $node = $xml->addChild(self::TAG_OUTPUT);
            $node[self::ATTR_OUTPUT_NAME] = $outputSettings->getOutput()->getName();
        } else {
            // The ouput node exists so we need to update it.
            $node = $nodes[0];
            /* @var $node \SimpleXMLElement */
            // We remove all the childens node that contain the template pages.
            $toRemove = array();
            foreach ($node->children() as $kid) {
                $toRemove[] = $kid->getName();
            }
            foreach ($toRemove as $name) {
                unset($node->{$name});
            }
        }
        $front = $node->addChild(self::TAG_PAGE_FRONT);
        $front[self::ATTR_PAGE_SRC] = $this->getRelativePath($outputSettings->getFrontPage(), $theme->getPath());
        $section = $node->addChild(self::TAG_PAGE_SECTION);
        $section[self::ATTR_PAGE_SRC] = $this->getRelativePath($outputSettings->getSectionPage(), $theme->getPath());
        $article = $node->addChild(self::TAG_PAGE_ARTICLE);
        $article[self::ATTR_PAGE_SRC] = $this->getRelativePath($outputSettings->getArticlePage(), $theme->getPath());
        $error = $node->addChild(self::TAG_PAGE_ERROR);
        $error[self::ATTR_PAGE_SRC] = $this->getRelativePath($outputSettings->getErrorPage(), $theme->getPath());
        $xml->asXML($this->toFullPath($theme, $this->themeConfigFileName));
        // We have to update also the output theme settings in the database if there is one.
        $em = $this->getManager();
        $q = $em->createQueryBuilder();
        $q->select('ost')->from(OutputSettingsTheme::NAME, 'ost');
        $q->leftJoin('ost.themePath', 'rsc');
        $q->andWhere('rsc.path = ?1');
        $q->setParameter(1, $theme->getPath());
        $result = $q->getQuery()->getResult();
        // If there are results than it means that the theme belongs to a publication
        if (count($result) > 0) {
            $updated = FALSE;
            foreach ($result as $outTh) {
                /* @var $outTh Newscoop\Entity\Output\OutputSettingsTheme */
                if ($outTh->getOutput() == $outputSettings->getOutput()) {
                    $this->syncOutputSettings($outTh, $outputSettings);
                    $em->persist($outTh);
                    $em->flush();
                    $updated = TRUE;
                    break;
                }
            }
            if (!$updated) {
                $pathRsc = new Resource();
                $pathRsc->setName(self::THEME_PATH_RSC_NAME);
                $pathRsc->setPath($themeFolder);
                $pathRsc = $this->getSyncResourceService()->getSynchronized($pathRsc);
                $outTh = new OutputSettingsTheme();
                $outTh->setPublication($result[0]->getPublication());
                $outTh->setThemePath($pathRsc);
                $outTh->setOutput($outputSettings->getOutput());
                $this->syncOutputSettings($outTh, $outputSettings);
                $em->persist($outTh);
                $em->flush();
            }
        }
    }