public function assignTheme(Theme $theme, Publication $publication, $cli = false)
{
Validation::notEmpty($theme, 'theme');
Validation::notEmpty($publication, 'publication');
// We have to check if there is no other theme by the new theme name.
foreach ($this->getThemes($publication) as $th) {
/* @var $th Theme */
if (trim($th->getName()) === trim($theme->getName())) {
if ($this->getThemePublication($th) != NULL) {
if ($cli) {
return false;
}
throw new DuplicateNameException();
} else {
$thPath = $th->getPath();
$this->rrmdir($this->toFullPath($thPath));
}
}
}
$themeFolder = $this->getNewThemeFolder(self::FOLDER_PUBLICATION_PREFIX . $publication->getId() . '/');
$themeFullFolder = $this->toFullPath($themeFolder);
try {
$this->copy($this->toFullPath($theme), $themeFullFolder);
// Reset the theme configs cache so also the new theme will be avaialable
$this->cacheThemeConfigs = NULL;
// We need to persist the theme ouput setting for the new publication theme
$em = $this->getManager();
$pathRsc = $this->getSyncResourceService()->getThemePath($themeFolder);
// Persist the coresponding ouput settings theme to the database
$outSets = $this->loadOutputSettings($themeFolder);
foreach ($outSets as $outSet) {
/* @var $outSet OutputSettings */
$qb = $em->createQueryBuilder();
$qb->select('th')->from(OutputSettingsTheme::NAME, 'th');
$qb->where('th.publication = :publication');
$qb->andWhere('th.themePath = :themePath');
$qb->andWhere('th.output = :output');
$qb->setParameter('publication', $publication);
$qb->setParameter('themePath', $pathRsc);
$qb->setParameter('output', $outSet->getOutput());
$result = $qb->getQuery()->getResult();
if (count($result) > 0) {
$outTh = $result[0];
} else {
$outTh = new OutputSettingsTheme();
$outTh->setPublication($publication);
$outTh->setThemePath($pathRsc);
$outTh->setOutput($outSet->getOutput());
}
$this->syncOutputSettings($outTh, $outSet);
$issueOutSettings = $this->getOutputSettingIssueService();
foreach ($issueOutSettings->getUnthemedIssues($publication) as $issue) {
$outIssueTh = new OutputSettingsIssue();
$outIssueTh->setIssue($issue);
$outIssueTh->setThemePath($pathRsc);
$outIssueTh->setOutput($outSet->getOutput());
$em->persist($outIssueTh);
}
$em->persist($outTh);
}
$em->flush();
} catch (\Exception $e) {
$this->rrmdir($themeFullFolder);
throw $e;
}
return $this->loadThemeByPath($themeFolder);
}