/**
* Saves new topic. Possibility to overwrite AUTO strategy (set custom ids).
*
* @param Topic $node Topic object
* @param string|null $locale Language code
*
* @return bool
*
* @throws ResourcesConflictException When Topic already exists
*/
public function saveNewTopic(Topic $node, $locale = null)
{
$node->setTranslatableLocale($locale ?: $node->getTranslatableLocale());
$topicTranslation = $this->getTopicRepository()->createQueryBuilder('t')->join('t.translations', 'tt')->where('tt.locale = :locale')->andWhere('tt.content = :title')->andWhere("tt.field = 'title'")->setParameters(array('title' => $node->getTitle(), 'locale' => $node->getTranslatableLocale()))->getQuery()->getOneOrNullResult();
if ($topicTranslation) {
throw new ResourcesConflictException('Topic already exists', 409);
}
if (!$node->getParent()) {
$qb = $this->getTopicRepository()->createQueryBuilder('t');
$maxOrderValue = $qb->select('MAX(t.topicOrder)')->setMaxResults(1)->getQuery()->getSingleScalarResult();
$node->setOrder((int) $maxOrderValue + 1);
}
$node->addTranslation(new TopicTranslation($locale ?: $node->getTranslatableLocale(), 'title', $node->getTitle(), true));
$this->em->persist($node);
$metadata = $this->em->getClassMetaData(get_class($node));
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
$this->em->flush();
$this->dispatcher->dispatch('topic.create', new GenericEvent($this, array('title' => $node->getTitle(), 'id' => array('id' => $node->getId()), 'diff' => (array) $node)));
return true;
}