<commit_before><?php

namespace Bundle\MainBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

use Bundle\CommonBundle\Controller\BaseController;

use Bundle\MainBundle\Form\Survey\SurveyType;
use Bundle\MainBundle\Form\Post\PostType;

use Bundle\CommonBundle\Entity\Survey\Survey;
use Bundle\CommonBundle\Entity\Post\Post;
use Bundle\CommonBundle\Entity\Wall\WallRepository;
use Bundle\CommonBundle\Entity\Wall\Wall;
use Bundle\CommonBundle\Response\JsonResponse;
use Bundle\CommonBundle\Constants\PostConstant;
use Bundle\MainBundle\Controller\Traits;

/**
 * @Route("/wall")
 */
class WallController extends BaseController
{
    use Traits\SurveyControllerTrait;

    /**
     * @Route("", name="wall")
     * @Template("MainBundle:Wall/Container:content.html.twig")
     */
    public function indexAction(Request $request)
    {
        $wallId = $request->get('wallId');
        $filter = $request->get('filter', PostConstant::FILTER_ALL);
        $user = $this->getUser();

        $survey = $this->_createSurveyPrototype();
        $surveyForm = $this->_getSurveyForm($survey);

        $wall = $this->getRepository('wall')->getWallById($wallId, $filter);
        $newPostForm = $this->createForm(new PostType(), new Post($wall));

        return array(
            'newPostForm' => $newPostForm->createView(),
            'surveyForm' => $surveyForm->createView(),
            'user' => $user,
            'wall' => $wall
        );
    }

    /**
     * @Route("/type/json", name="wall_json")
     * @return \Bundle\CommonBundle\Response\JsonResponse
     */
    public function indexJsonAction(Request $request)
    {
        $wallId = $request->get('wallId');
        $filter = $request->get('filter', PostConstant::FILTER_ALL);
        $response = new JsonResponse();
        try {
            $wall = $this->getRepository('wall')->getWallById($wallId, $filter);
            $totalPosts = $wall->getTotalPosts();
            $view = $this->forward('MainBundle:Wall:index');
        } catch (\Exception $ex) {
            return $this->generateError('repository.get', $ex);
        }

        $response->setJsonContent(array(
            'view' => $view->getContent(),
            'totalPosts' => $totalPosts,
        ));

        return $response;
    }

    /**
     * @Route("/post/add", name="add_post")
     * @Method("POST")
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Bundle\CommonBundle\Response\JsonResponse
     */
    public function addPost(Request $request)
    {
        $user = $this->getUser();
        //TODO Вынести в форму
        $post = $request->get('post');
        /** @var WallRepository $wallRepository */
        $wallRepository = $this->getRepository('wall');

        if (empty($post["message"])) {
            return $this->generateError('post.empty');
        }

        if (!$post["wallId"]) {
            return $this->generateError('post.notFound');
        } else {
            /** @var Wall $wall */
            $wall = $wallRepository->find($post["wallId"]);
        }

        $newPost = new Post();
        try {
            $newPost->setMessage($post["message"]);
            $newPost->setWall($wall);
            $newPost->setFixed($post['fixed']);
            $this->getRepository('wall')->save($newPost);
            //Attachments
            $images = $request->files->get("images");
            $documents = $request->files->get("documents");
            $count = 0;

            // Сохраняем изображения
            if (is_array($images)) {
                $this->getRepository('attachment')->saveAttachments($images, $newPost, $user, $count, true);
            }

            // Сохраняем документы
            if (is_array($documents)) {
                $this->getRepository('attachment')->saveAttachments($documents, $newPost, $user, $count, false);
            }
        } catch (\Exception $ex) {
            return $this->generateError('repository.save', $ex);
        }

        $response = new JsonResponse();
        $response->setJsonContent(array(
            'success' => true
        ));

        return $response;
    }

    /**
     * Получить порцию постов на стену
     *
     * @Route("/getPortion", name="get_posts_portion")
     * @param Request $request
     * @return array
     */
    public function getPostsPortion(Request $request)
    {
        $response = new JsonResponse();
        $wallId = $request->get('wallId');
        $offset = (int) $request->get('offset');
        $filter = $request->get('filter', PostConstant::FILTER_ALL);
        $fpIds = array();
        if ($filter == PostConstant::FILTER_ALL) {
            $fixedPosts = $this->getRepository('post')->getPostsByWallId($wallId, true, 0, $filter);
            $fpIds = array_keys($this->getRepository('post')->indexEntities($fixedPosts));
        }

        $posts = $this->getRepository('post')->getPostsByWallId($wallId, false, $offset, $filter, $fpIds);

        $view = $this->renderView("MainBundle:Wall/Container/Content:posts.html.twig", array(
            "posts" => $posts
        ));

        $postOnWall = $offset + count($posts);
        $totalPostCount = $this->getRepository('post')->getPostCountByWallId($wallId, $filter, $fpIds);
        $jsonContent = array('view' => $view);
        if ($postOnWall == $totalPostCount) {
            $jsonContent['stop'] = true;
        }

        $response->setJsonContent($jsonContent);

        return $response;
    }

    /**
     * Удалить пост
     *
     * @Route("/delete", name="delete_post")
     * @Method("POST")
     * @param Request $request
     * @return array
     */
    public function deleteAction(Request $request)
    {
        $postId = (int) $request->get('postId');
        if (! $post = $this->getRepository('post')->find($postId)) {
            return $this->generateError('post.notFound');
        }
        if (! $post->isAuthorUser($this->getUser())) {<commit_msg>Исправление отображения закрепленных постов при отправке нового сообщения<commit_after><?php

namespace Bundle\MainBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

use Bundle\CommonBundle\Controller\BaseController;

use Bundle\MainBundle\Form\Survey\SurveyType;
use Bundle\MainBundle\Form\Post\PostType;

use Bundle\CommonBundle\Entity\Survey\Survey;
use Bundle\CommonBundle\Entity\Post\Post;
use Bundle\CommonBundle\Entity\Wall\WallRepository;
use Bundle\CommonBundle\Entity\Wall\Wall;
use Bundle\CommonBundle\Response\JsonResponse;
use Bundle\CommonBundle\Constants\PostConstant;
use Bundle\MainBundle\Controller\Traits;

/**
 * @Route("/wall")
 */
class WallController extends BaseController
{
    use Traits\SurveyControllerTrait;

    /**
     * @Route("", name="wall")
     * @Template("MainBundle:Wall/Container:content.html.twig")
     */
    public function indexAction(Request $request)
    {
        $wallId = $request->get('wallId');
        $filter = $request->get('filter', PostConstant::FILTER_ALL);
        $user = $this->getUser();

        $survey = $this->_createSurveyPrototype();
        $surveyForm = $this->_getSurveyForm($survey);

        $wall = $this->getRepository('wall')->getWallById($wallId, $filter);
        $newPostForm = $this->createForm(new PostType(), new Post($wall));

        return array(
            'newPostForm' => $newPostForm->createView(),
            'surveyForm' => $surveyForm->createView(),
            'user' => $user,
            'wall' => $wall
        );
    }

    /**
     * @Route("/type/json", name="wall_json")
     * @return \Bundle\CommonBundle\Response\JsonResponse
     */
    public function indexJsonAction(Request $request)
    {
        $wallId = $request->get('wallId');
        $filter = $request->get('filter', PostConstant::FILTER_ALL);
        $response = new JsonResponse();
        try {
            $wall = $this->getRepository('wall')->getWallById($wallId, $filter);
            $totalPosts = $wall->getTotalPosts();
            $view = $this->forward('MainBundle:Wall:index');
        } catch (\Exception $ex) {
            return $this->generateError('repository.get', $ex);
        }

        $response->setJsonContent(array(
            'view' => $view->getContent(),
            'totalPosts' => $totalPosts,
        ));

        return $response;
    }

    /**
     * @Route("/post/add", name="add_post")
     * @Method("POST")
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Bundle\CommonBundle\Response\JsonResponse
     */
    public function addPost(Request $request)
    {
        $user = $this->getUser();
        //TODO Вынести в форму
        $post = $request->get('post');
        /** @var WallRepository $wallRepository */
        $wallRepository = $this->getRepository('wall');

        if (empty($post["message"])) {
            return $this->generateError('post.empty');
        }

        if (!$post["wallId"]) {
            return $this->generateError('post.notFound');
        } else {
            /** @var Wall $wall */
            $wall = $wallRepository->find($post["wallId"]);
        }

        $newPost = new Post();
        try {
            $newPost->setMessage($post["message"]);
            $newPost->setWall($wall);
            $newPost->setFixed($post['fixed']);
            $this->getRepository('wall')->save($newPost);
            //Attachments
            $images = $request->files->get("images");
            $documents = $request->files->get("documents");
            $count = 0;

            // Сохраняем изображения
            if (is_array($images)) {
                $this->getRepository('attachment')->saveAttachments($images, $newPost, $user, $count, true);
            }

            // Сохраняем документы
            if (is_array($documents)) {
                $this->getRepository('attachment')->saveAttachments($documents, $newPost, $user, $count, false);
            }
        } catch (\Exception $ex) {
            return $this->generateError('repository.save', $ex);
        }

        $response = new JsonResponse();
        $response->setJsonContent(array(
            'success' => true
        ));

        return $response;
    }

    /**
     * Получить порцию постов на стену
     *
     * @Route("/getPortion", name="get_posts_portion")
     * @param Request $request
     * @return array
     */
    public function getPostsPortion(Request $request)
    {
        $response = new JsonResponse();
        $wallId = $request->get('wallId');
        $offset = (int) $request->get('offset');
        $filter = $request->get('filter', PostConstant::FILTER_ALL);
        $fpIds = array();

        $view = '';

        if ($filter == PostConstant::FILTER_ALL) {
            $fixedPosts = $this->getRepository('post')->getPostsByWallId($wallId, true, 0, $filter);
            $fpIds = array_keys($this->getRepository('post')->indexEntities($fixedPosts));

            if ($offset == 0) {
                $view = $this->renderView("MainBundle:Wall/Container/Content:posts.html.twig", array(
                    "posts" => $fixedPosts
                ));
            }
        }

        $posts = $this->getRepository('post')->getPostsByWallId($wallId, false, $offset, $filter, $fpIds);


        $view .= $this->renderView("MainBundle:Wall/Container/Content:posts.html.twig", array(
            "posts" => $posts
        ));

        $postOnWall = $offset + count($posts);
        $totalPostCount = $this->getRepository('post')->getPostCountByWallId($wallId, $filter, $fpIds);
        $jsonContent = array('view' => $view);
        if ($postOnWall == $totalPostCount) {
            $jsonContent['stop'] = true;
        }

        $response->setJsonContent($jsonContent);

        return $response;
    }

    /**
     * Удалить пост
     *
     * @Route("/delete", name="delete_post")
     * @Method("POST")
     * @param Request $request
     * @return array
     */
    public function deleteAction(Request $request)
    {
        $postId = (int) $request->get('postId');
        if (! $post = $this->getRepository('post')->find($postId)) {
            return $this->generateError('post.notFound');
        }
        if (! $post->isAuthorUser($this->getUser())) {
{'fraction_non_alphanumeric': 0.1609899729428617, 'fraction_numerical': 0.00039789909279006844, 'mean_word_length': 2.5231286795626575, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7481852', 'n_tokens_mistral': 3676, 'n_tokens_neox': 3605, 'n_words': 594}