public function setParent(\Newscoop\Entity\Comment $p_parent = NULL) { $this->__load(); return parent::setParent($p_parent); }
/** * Method for saving a comment * * @param Comment $entity * @param array $values * * @return Comment */ public function save(Comment $entity, $values) { $values += array('recommended' => false); $em = $this->getEntityManager(); $commenterRepository = $em->getRepository('Newscoop\\Entity\\Comment\\Commenter'); $commenter = new Commenter(); $commenter = $commenterRepository->save($commenter, $values); $entity->setCommenter($commenter)->setSubject($values['subject'])->setMessage($values['message'])->setStatus($values['status'])->setIp($values['ip'])->setTimeCreated($values['time_created'])->setRecommended($values['recommended']); if (array_key_exists('source', $values)) { $entity->setSource($values['source']); } $threadLevel = 0; if (!empty($values['parent']) && 0 != $values['parent']) { $parent = $this->find($values['parent']); // set parent of the comment $entity->setParent($parent)->setLanguage($parent->getLanguage())->setForum($parent->getForum())->setThread($parent->getThread()); /** * get the maximum thread order from the current parent */ $qb = $this->createQueryBuilder('c'); $threadOrder = $qb->select('MAX(c.thread_order)')->andwhere('c.parent = :parent')->andWhere('c.thread = :thread')->andWhere('c.language = :language')->setParameter('parent', $parent)->setParameter('thread', $parent->getThread()->getId())->setParameter('language', $parent->getLanguage()->getId()); $threadOrder = $threadOrder->getQuery()->getSingleScalarResult(); // if the comment parent doesn't have children then use the parent thread order if (empty($threadOrder)) { $threadOrder = $parent->getThreadOrder(); } $threadOrder += 1; /** * update all the comment for the thread where thread order is less or equal * of the current thread_order */ $qb = $this->createQueryBuilder('c'); $qb->update()->set('c.thread_order', 'c.thread_order+1')->andwhere('c.thread_order >= :thread_order')->andWhere('c.thread = :thread')->andWhere('c.language = :language')->setParameter('language', $parent->getLanguage()->getId())->setParameter('thread', $parent->getThread()->getId())->setParameter('thread_order', $threadOrder); $qb->getQuery()->execute(); // set the thread level the thread level of the parent plus one the current level $threadLevel = $parent->getThreadLevel() + 1; } else { $languageRepository = $em->getRepository('Newscoop\\Entity\\Language'); if (is_numeric($values['language'])) { $language = $languageRepository->findOneById($values['language']); } else { $language = $languageRepository->findOneByCode($values['language']); } $articleRepository = $em->getRepository('Newscoop\\Entity\\Article'); $thread = $articleRepository->find(array('number' => $values['thread'], 'language' => $language->getId())); $query = $this->createQueryBuilder('c')->select('MAX(c.thread_order)')->where('c.thread = :thread')->andWhere('c.language = :language')->setParameter('thread', $thread->getNumber())->setParameter('language', $language->getId())->getQuery(); // increase by one of the current comment $threadOrder = $query->getSingleScalarResult() + 1; $entity->setLanguage($language)->setForum($thread->getPublication())->setThread($thread); } $entity->setThreadOrder($threadOrder)->setThreadLevel($threadLevel); $em->persist($entity); $user = $commenter->getUser(); if ($user instanceof User) { $em->getRepository('Newscoop\\Entity\\User')->setUserPoints($user); } return $entity; }