Creates a comment.

@param AnCommandContext $context

@return AnDomainEntityAbstract
512643
protected function _actionAdd($context)
    {
        $data = $context->data;
        $body = $data->body;

        return $this->setItem($this->parent->addComment($body))->getItem();
    }
847912
protected function _actionPost(AnCommandContext $context)
    {
        $context->append(array(
            'data' => AnRequest::get('post', 'raw', array()),
        ));

        //backward compatiblity
        if ($context->data['action']) {
            $context->data['_action'] = $context->data['action'];
        }

        $action = 'post';

        if ($context->data['_action']) {
            $action = $context->data['_action'];
            if (in_array($action, array('browse', 'read', 'display'))) {
                throw new LibBaseControllerExceptionMethodNotAllowed('Action: '.$action.' not allowed');
            }
        }

        if ($context->request->getFormat() == 'json' || $context->request->isAjax()) {
            $this->registerCallback('after.post', array($this, 'forward'));
        } else {
            $context->response->setRedirect(AnRequest::get('server.HTTP_REFERER', 'url'));
        }

        return $this->getController()->execute($action, $context);
    }
1066667
protected function _actionAddcomment(AnCommandContext $context)
    {
        $data = $context->data;
        $comment = $this->getCommentController()->add(array('body' => $data->body));
        $context->response->status = AnHttpResponse::CREATED;
        $context->comment = $comment;

        if ($this->isDispatched()) {
            $context->response->content = $this->getCommentController()->display();

            if ($context->request->getFormat() == 'html') {
                $offset = $this->getItem()->getCommentOffset($comment->id);
                $start = (int) ($offset / $this->limit) * $this->limit;
                $context->response->setRedirect(route($comment->parent->getURL().'&start='.$start).'#scroll='.$comment->id);
            } else {
                $context->response->setRedirect(route($comment->getURL()));
            }
        }

        return $comment;
    }
21493
protected function _actionEdit(AnCommandContext $context)
    {
        $context->response->status = AnHttpResponse::RESET_CONTENT;

        return $this->getItem()->setData($context['data']);
    }
1181151
public function fromComment($comment, $context = null)
    {
        if ($context === null) {
            $context = new Context(['comment' => $comment]);
        } else {
            $context->comment = $comment;
        }
        try {
            self::$context = $context;
            if ($context->is('annotations') === false) {
                $context->annotations = [];
            }
            $comment = preg_replace_callback(
                '/^[\t ]*\*[\t ]+/m',
                function ($match) {
                    // Replace leading tabs with spaces.
                    // Workaround for http://www.doctrine-project.org/jira/browse/DCOM-255
                    return str_replace("\t", ' ', $match[0]);
                },
                $comment
            );
            $annotations = $this->docParser->parse($comment, $context);
            self::$context = null;
            return $annotations;
        } catch (Exception $e) {
            self::$context = null;
            if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote((string)$context, '/') . '\.$/', $e->getMessage(), $matches)) {
                $errorMessage = $matches[1];
                $errorPos = (int)$matches[2];
                $atPos = strpos($comment, '@');
                $context->line += substr_count($comment, "\n", 0, $atPos + $errorPos);
                $lines = explode("\n", substr($comment, $atPos, $errorPos));
                $context->character = strlen(array_pop($lines)) + 1; // position starts at 0 character starts at 1
                Logger::warning(new Exception($errorMessage . ' in ' . $context, $e->getCode(), $e));
            } else {
                Logger::warning($e);
            }
            return [];
        }
    }
1175625
private function buildCommentTree($source, $context)
    {
        $comments = $this
            ->commentRepository
            ->getAllCommentsSortedByParentAndIdAsc($source, $context);

        $commentTree = [
            0 => null,
            'children' => [],
        ];

        /**
         * @var CommentInterface $comment
         */
        foreach ($comments as $comment) {
            $parentCommentId = 0;
            $commentId = $comment->getId();

            if ($comment->getParent() instanceof CommentInterface) {
                $parentCommentId = $comment->getParent()->getId();
            }

            if ($parentCommentId && !isset($commentTree[$parentCommentId])) {
                $commentTree[$parentCommentId] = [
                    'entity' => null,
                    'children' => [],
                ];
            }

            if (!isset($commentTree[$commentId])) {
                $commentTree[$commentId] = [
                    'entity' => null,
                    'children' => [],
                ];
            }

            $commentVotePackage = $this
                ->voteManager
                ->getCommentVotes($comment);

            $commentTree[$commentId]['entity'] = $this->createCommentStructure(
                $comment,
                $commentVotePackage
            );

            $commentTree[$parentCommentId]['children'][] = &$commentTree[$commentId];
        }

        return $commentTree[0]['children']
            ?: [];
    }
1133994
public function addComment(\Haven\CmsBundle\Entity\Comment $comment)
    {
        $comment->setContent($this);
        $this->comments[] = $comment;

        return $this;
    }
377159
public function addCommentAction(
        Request $request,
        $authorToken,
        $context,
        $source
    ) {
        $requestBag = $request->request;
        $content = $requestBag->get('content');
        $authorName = $requestBag->get('author_name');
        $authorEmail = $requestBag->get('author_email');
        $parentId = $requestBag->get('parent');

        /**
         * @var CommentInterface|null $parent
         */
        $parent = $parentId > 0
            ? $this
                ->commentRepository
                ->find($parentId)
            : null;

        $comment = $this
            ->commentManager
            ->addComment(
                $source,
                $context,
                $content,
                $authorToken,
                $authorName,
                $authorEmail,
                $parent
            );

        $commentStructure = $this
            ->commentCache
            ->createCommentStructure($comment);

        return new Response(json_encode([
            'entity' => $commentStructure,
            'children' => [],
        ]));
    }
1232797
protected function _actionEditsubscription(AnCommandContext $context)
    {
        if ($subscription = $this->_subscriber->changeSubscriptionTo($this->getItem())) {
            $subscription->endDate = $context->data->endDate;
        }
    }
1050744
public static function create($comment) {
    $comment = trim($comment);
    $lines = array_map('trim', explode("\n", $comment));
    $text = "/**\n";
    foreach ($lines as $i => $line) {
      $text .= ' * ' . $line . "\n";
    }
    $text .= ' */';
    return new DocCommentNode(T_DOC_COMMENT, $text);
  }
858267
private function saveCommentTreeIntoCache(array $commentTree, $source, $context)
    {
        $this
            ->cache
            ->save(
                $this->getSpecificSourceCacheKey($source, $context),
                $this->encoder->encode($commentTree)
            );

        return $this;
    }
747486
public function setContext(Context $context)
    {
        $this->context = $context;

        if (empty($this->parameters['secret'])) {
            $this->parameters['secret'] = $context->getSecret();
        }

        return $this;
    }
632421
protected static function token(&$token, &$context) {
        $context['currentToken'] = &$token;

        if (static::rawblock($token, $context)) {
            return Token::toString($token);
        }

        if (static::delimiter($token, $context)) {
            return;
        }

        if (static::isDelimiter($context)) {
            static::spacing($token, $context);
            return;
        }

        if (static::comment($token, $context)) {
            static::spacing($token, $context);
            return;
        }

        list($raw, $vars) = Parser::parse($token, $context);

        // Handle spacing (standalone tags, partial indent)
        static::spacing($token, $context, (($token[Token::POS_OP] === '') || ($token[Token::POS_OP] === '&')) && (!$context['flags']['else'] || !isset($vars[0][0]) || ($vars[0][0] !== 'else')) || ($context['flags']['nostd'] > 0));

        $partials = static::partialBlock($context, $vars);
        $partials = static::inlinePartial($context, $vars) || $partials;

        if ($partials) {
            $context['stack'] = array_slice($context['stack'], 0, -4);
            $context['currentToken'][Token::POS_LOTHER] = '';
            $context['currentToken'][Token::POS_LSPACE] = '';
            return;
        }

        if (static::operator($token[Token::POS_OP], $context, $vars)) {
            return isset($token[Token::POS_BACKFILL]) ? null : array($raw, $vars);
        }

        if (count($vars) == 0) {
            return $context['error'][] = 'Wrong variable naming in ' . Token::toString($token);
        }

        if (!isset($vars[0])) {
            return $context['error'][] = 'Do not support name=value in ' . Token::toString($token) . ', you should use it after a custom helper.';
        }

        $context['usedFeature'][$raw ? 'raw' : 'enc']++;

        foreach ($vars as $var) {
            if (!isset($var[0]) || ($var[0] === 0)) {
                if ($context['level'] == 0) {
                    $context['usedFeature']['rootthis']++;
                }
                $context['usedFeature']['this']++;
            }
        }

        if (!isset($vars[0][0])) {
            return array($raw, $vars);
        }

        if (($vars[0][0] === 'else') && $context['flags']['else']) {
            static::doElse($context, $vars);
            return array($raw, $vars);
        }

        if (!static::helper($context, $vars)) {
            static::lookup($context, $vars);
            static::log($context, $vars);
        }

        return array($raw, $vars);
    }
157211
protected function _actionAdd($context)
    {
        $entity = parent::_actionAdd($context);

        if ($entity->owner->isSubscribable()) {
            $notification = $this->createNotification(array(
                'name' => 'topic_add',
                'object' => $entity,
                'subscribers' => $entity->owner->subscriberIds->toArray(),
            ))->setType('post', array('new_post' => true));
        }

        return $entity;
    }
18739
protected function _actionAddapp(AnCommandContext $context)
    {
        $data = $context->data;
        $this->getItem()->components->insert($data->app);
        $this->commit();
    }
706571
protected function _beforeEntityUpdate(AnCommandContext $context)
    {
        $modifications = $this->getModifiedData();

        if (isset($this->__restored)) {
            return;
        }

        if (
              $modifications->name ||
              $modifications->body ||
              $modifications->excerpt
        ) {
            $revision = $this->addNewRevision();

            foreach ($modifications as $property => $change) {
                $revision->$property = $change->old;
            }
        }
    }
737102
public function fetchRequester(AnCommandContext $context)
    {
        $data = $context->data;

        if ($this->getItem()) {
            $this->getState()
                 ->requester = $this->getItem()
                                    ->requesters
                                    ->fetch($data->requester);
        }
    }