public function attachTopicAction(Request $request)
{
$translator = $this->get('translator');
$em = $this->get('em');
$userService = $this->get('user');
$user = $userService->getCurrentUser();
$cacheService = $this->get('newscoop.cache');
$topicService = $this->get('newscoop_newscoop.topic_service');
if (!$user->hasPermission('AttachTopicToArticle')) {
return new JsonResponse(array('status' => false, 'message' => $translator->trans('You do not have the right to attach topics to articles.', array(), 'article_topics')), 403);
}
$articleNumber = $request->get('_articleNumber');
$languageCode = $request->get('_languageCode');
$qb = $em->getRepository('Newscoop\\Entity\\Article')->createQueryBuilder('a')->join('a.language', 'l')->where('a.number = :number')->andWhere('l.code = :code')->setParameters(array('number' => $articleNumber, 'code' => $languageCode));
$articleObj = $qb->getQuery()->getOneOrNullResult();
if (!$articleObj) {
return new JsonResponse(array('status' => false, 'message' => $translator->trans('Article does not exist.')), 404);
}
$ids = $request->get('ids');
$topicsIds = $this->getArticleTopicsIds($articleNumber);
$idsDiff = array_merge(array_diff($ids, $topicsIds), array_diff($topicsIds, $ids));
foreach ($idsDiff as $key => $topicId) {
$topicObj = $em->getReference("Newscoop\\NewscoopBundle\\Entity\\Topic", $topicId);
if (in_array($topicId, $topicsIds)) {
$topicService->removeTopicFromArticle($topicObj, $articleObj);
} else {
$topicService->addTopicToArticle($topicObj, $articleObj);
}
}
$cacheService->clearNamespace('topic');
return new JsonResponse(array('status' => true, 'message' => $translator->trans('topics.alerts.saved', array(), 'topics')));
}