src/Controller/FrontApi/StoryApiController.php line 131

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontApi;
  3. use App\Controller\BaseAbstractApiController;
  4. use App\Entity\Blog;
  5. use App\Entity\Story;
  6. use App\Entity\StoryComment;
  7. use App\Entity\User;
  8. use App\Enum\Banlist\Target;
  9. use App\Enum\Story\Status;
  10. use App\Enum\Site\Domain;
  11. use App\Enum\Site\Lang;
  12. use App\Service\Banlist\BanlistService;
  13. use App\Service\Category\CategoryService;
  14. use App\Service\Stories\StoryService;
  15. use App\Service\User\UserService;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. use Symfony\Component\Security\Core\Security;
  21. class StoryApiController extends BaseAbstractApiController
  22. {
  23.     /**
  24.      * @var StoryService
  25.      */
  26.     private $storiesService;
  27.     /**
  28.      * @var UserService
  29.      */
  30.     private $userService;
  31.     /**
  32.      * @var BanlistService
  33.      */
  34.     private $banlistService;
  35.     public function __construct(
  36.         RequestStack           $requestStack,
  37.         Security               $security,
  38.         EntityManagerInterface $em,
  39.         StoryService           $storiesService,
  40.         UserService            $userService,
  41.         BanlistService         $banlistService
  42.     )
  43.     {
  44.         parent::__construct($requestStack$security$em);
  45.         $this->storiesService $storiesService;
  46.         $this->userService $userService;
  47.         $this->banlistService $banlistService;
  48.     }
  49.     public function add()
  50.     {
  51.         $user $this->security->getUser() ? $this->security->getUser()->getAssociatedUser() : null;
  52.         $domain Domain::isWLDomainHasOwnContent($this->dataArray['original_domain'] ?? '') ? $this->dataArray['original_domain'] : $this->dataArray['domain'];
  53.         $story = (new Story())->setDomain($domain);
  54.         if ($user) {
  55.             $story->setUser($user);
  56.         }
  57.         $data $this->dataArray['story'];
  58.         if (($data['id'] ?? 0) > 0) {
  59.             $story $this->storiesService->get($data['id'], $domain);
  60.             if (isset($data['user'])) {
  61.                 unset($data['user']);
  62.             }
  63.         }
  64.         if (isset($data['categories'])) {
  65.             foreach ($data['categories'] as $key => $categoryId) {
  66.                 $category $this->storiesService->getCategory($categoryId);
  67.                 $story->addCategory($category);
  68.                 unset($data['categories']);
  69.             }
  70.         }
  71.         if (isset($data['firstStoryCategoryId'])) {
  72.             $story->setFirstStoryCategory($data['firstStoryCategoryId']);
  73.         }
  74.         $story->fromArray($data);
  75.         $story->setDomain($domain);
  76.         $errors = [];
  77.         if (!$this->banlistService->checkText(
  78.             $story->getName(),
  79.             $story->getDomain(),
  80.             Target::TARGET_STORY,
  81.             true,
  82.             null,
  83.             false,
  84.             [],
  85.             $user $user->getId() : null,
  86.             ($data['id'] ?? 0) > $data['id'] : null
  87.         )) {
  88.             $errors['name'] = 'error_content_link_phone';
  89.         }
  90.         if (!$this->banlistService->checkText(
  91.             $story->getText(),
  92.             $story->getDomain(),
  93.             Target::TARGET_STORY,
  94.             true,
  95.             null,
  96.             false,
  97.             [],
  98.             $user $user->getId() : null,
  99.             ($data['id'] ?? 0) > $data['id'] : null
  100.         )) {
  101.             $errors['text'] = 'error_content_link_phone';
  102.         }
  103.         if (empty($errors)) {
  104.             $this->storiesService->update($story);
  105.             if (!$story->getId() || $story->getId() <= 0) {
  106.                 $errors[] = 'Error';
  107.             } else {
  108.                 $this->dataArray['story'] = $story->toArray();
  109.             }
  110.         }
  111.         return $this->json(['errors' => $errors'data' => $this->dataArray['story']]);
  112.     }
  113.     public function getCategories()
  114.     {
  115.         $lang $this->dataArray['sys_locale'];
  116.         $domain $this->dataArray['domain'];
  117.         $originalDomain $this->dataArray['original_domain'];
  118.         $data $this->storiesService->getCategories($domain$originalDomain$lang);
  119.         return $this->json($data);
  120.     }
  121.     public function getStory()
  122.     {
  123.         $domain $this->dataArray['domain'];
  124.         $slug $this->dataArray['slug'];
  125.         $lang $this->dataArray['lang'];
  126.         $page $this->dataArray['page'];
  127.         $perPage $this->dataArray['perPage'];
  128.         $data $this->storiesService->getBySlug($slug$domain$lang$page$perPage);
  129.         return $this->json($data);
  130.     }
  131.     public function getStoryById()
  132.     {
  133.         return $this->json($this->storiesService->getById($this->dataArray['id'], $this->dataArray['domain']) ?: []);
  134.     }
  135.     public function update()
  136.     {
  137.         $user $this->security->getUser() ? $this->security->getUser()->getAssociatedUser() : null;
  138.         $domain $this->dataArray['domain'];
  139.         $story = (new Story())->setDomain($domain);
  140.         if ($user) {
  141.             $story->setUser($user);
  142.         }
  143.         $data $this->dataArray['story'];
  144.         if (($data['id'] ?? 0) > 0) {
  145.             $story $this->storiesService->get($data['id'], $domain);
  146.             if ($story && isset($data['categories'])) {
  147.                 $categoryIds $this->storiesService->getCategoryIds($story->getCategories());
  148.                 foreach ($data['categories'] as $newStoryCategory) {
  149.                     if (!in_array($newStoryCategory$categoryIds)) {
  150.                         $category $this->storiesService->getCategory($newStoryCategory);
  151.                         if ($category) {
  152.                             $story->addCategory($category);
  153.                         }
  154.                     }
  155.                 }
  156.                 foreach ($categoryIds as $oldStoryCategoryId) {
  157.                     if (!in_array($oldStoryCategoryId$data['categories'])) {
  158.                         $oldCategory $this->storiesService->getCategory($oldStoryCategoryId);
  159.                         if ($oldCategory) {
  160.                             $story->removeCategory($oldCategory);
  161.                         }
  162.                     }
  163.                 }
  164.                 if (isset($data['user'])) {
  165.                     unset($data['user']);
  166.                 }
  167.             }
  168.         }
  169.         if (isset($data['firstStoryCategoryId'])) {
  170.             $story->setFirstStoryCategory($data['firstStoryCategoryId']);
  171.         }
  172.         $story->fromArray($data);
  173.         $story->setDomain($domain);
  174.         if (!isset($data['id']) || !$data['id'] || $data['id'] <= 0) {
  175.             $story->setStatus(Status::STATUS_NEW);
  176.         }
  177.         $errors = [];
  178.         if (!$this->banlistService->checkText(
  179.             $story->getName(),
  180.             $story->getDomain(),
  181.             Target::TARGET_STORY,
  182.             true,
  183.             null,
  184.             false,
  185.             [],
  186.             $user $user->getId() : null,
  187.             ($data['id'] ?? 0) > $data['id'] : null
  188.         )) {
  189.             $errors['name'] = 'error_content_link_phone';
  190.         }
  191.         if (!$this->banlistService->checkText(
  192.             $story->getText(),
  193.             $story->getDomain(),
  194.             Target::TARGET_STORY,
  195.             true,
  196.             null,
  197.             false,
  198.             [],
  199.             $user $user->getId() : null,
  200.             ($data['id'] ?? 0) > $data['id'] : null
  201.         )) {
  202.             $errors['text'] = 'error_content_link_phone';
  203.         }
  204.         if (empty($errors)) {
  205.             $this->storiesService->update($story);
  206.             if (!$story->getId() || $story->getId() <= 0) {
  207.                 $errors[] = 'Error';
  208.             } else {
  209.                 $this->dataArray['story'] = $story->toArray();
  210.                 if ($story->getUser()) {
  211.                     $this->dataArray['story']['user'] = $story->getUser()->toArray();
  212.                 }
  213.             }
  214.         }
  215.         return $this->json(['errors' => $errors'data' => $this->dataArray['story']]);
  216.     }
  217.     public function incViews()
  218.     {
  219.         if (getenv('APP_ENV') === 'dev') {
  220.             $story $this->storiesService->get($this->dataArray['id'], $this->dataArray['domain']);
  221.             if ($story) {
  222.                 $story->incViews();
  223.                 $this->storiesService->update($story);
  224.                 file_put_contents(getenv('RAMFSPATH') . '/incStory.cnt'$this->dataArray['id'] . '|'FILE_APPEND);
  225.                 return $this->json(['views' => $story->getViews()]);
  226.             }
  227.         } else {
  228.             file_put_contents(getenv('RAMFSPATH') . '/incStory.cnt'$this->dataArray['id'] . '|'FILE_APPEND);
  229.         }
  230.         return $this->json([]);
  231.     }
  232.     public function map()
  233.     {
  234.         $sql 'select slug, lang from story where status="active" and domain="'.$this->dataArray['domain'].'" order by id desc limit 0,1000';
  235.         $q $this->getDoctrine()->getConnection()->prepare($sql);
  236.         $q->execute();
  237.         $result = [];
  238.         foreach ($q->fetchAll() as $item) {
  239.             $result[] = $item;
  240.         }
  241.         return $this->json($result);
  242.     }
  243.     public function random()
  244.     {
  245.         $domain $this->dataArray['domain'];
  246.         $lang $this->dataArray['lang'] ?: Lang::LANG_LV;
  247.         $skipId $this->dataArray['skipId'] ?: 0;
  248.         $limit $this->dataArray['limit'] ?: 3;
  249.         $data $this->storiesService->getRandom($domain$lang$skipId$limit);
  250.         return $this->json($data);
  251.     }
  252.     public function addComment()
  253.     {
  254.         /** @var Story $story */
  255.         $story $this->storiesService->get($this->dataArray['storyId'], $this->dataArray['domain']);
  256.         /** @var User $user */
  257.         $user $this->userService->get($this->dataArray['userId'], $this->dataArray['domain']);
  258.         $data = [];
  259.         if ($story && $user) {
  260.             $comment = new StoryComment();
  261.             $comment->setStory($story);
  262.             $comment->setUser($user);
  263.             $comment->setName($user->getUsername() ?: $user->getName() ?: 'Guest');
  264.             $comment->setStatus(Status::STATUS_ACTIVE);
  265.             $comment->setText($this->dataArray['text']);
  266.             $comment->setIp($this->dataArray['ip']);
  267.             $comment->setDomain($this->dataArray['domain']);
  268.             $this->storiesService->update($comment);
  269.             $isValid $this->banlistService->checkText(
  270.                 $this->dataArray['text'],
  271.                 $this->dataArray['domain'],
  272.                 Target::TARGET_STORY_COMMENT,
  273.                 true,
  274.                 null,
  275.                 false,
  276.                 [],
  277.                 $user->getId(),
  278.                 $comment->getId()
  279.             );
  280.             if (!$isValid) {
  281.                 $comment->setStatus(Status::STATUS_DELETED);
  282.                 $this->storiesService->update($comment);
  283.             }
  284.             $data $comment->toArray();
  285.             $data['user'] = $comment->getUser()->toArray();
  286.             $data['storySlug'] = $comment->getStory()->getSlug();
  287.         }
  288.         return $this->json($data);
  289.     }
  290.     public function getNewSlug()
  291.     {
  292.         $id = (int)($this->dataArray['id'] ?? 0);
  293.         if ($id 0) {
  294.             switch ($this->dataArray['type'] ?? '') {
  295.                 case 'blog':
  296.                     $sql 'SELECT*FROM setting s WHERE s.name="k6blog-'.$id.'-new_id"';
  297.                     $qTmp $this->em->getConnection()->prepare($sql);
  298.                     $qTmp->execute();
  299.                     $tmp $qTmp->fetch();
  300.                     if (isset($tmp['value'])) {
  301.                         $id = (int) unserialize($tmp['value']);
  302.                         if ($id 0) {
  303.                             $sql 'select slug from blog where id='.$id;
  304.                             $qTmp $this->em->getConnection()->prepare($sql);
  305.                             $qTmp->execute();
  306.                             $tmp $qTmp->fetch();
  307.                             if (isset($tmp['slug'])) {
  308.                                 return $this->json($tmp['slug']);
  309.                             }
  310.                         }
  311.                     }
  312.                     break;
  313.                 case 'novel':
  314.                     $sql 'SELECT*FROM setting s WHERE s.name="k6story-'.$id.'-new_id"';
  315.                     $qTmp $this->em->getConnection()->prepare($sql);
  316.                     $qTmp->execute();
  317.                     $tmp $qTmp->fetch();
  318.                     if (isset($tmp['value'])) {
  319.                         $id = (int) unserialize($tmp['value']);
  320.                         if ($id 0) {
  321.                             $sql 'select slug from story where id='.$id;
  322.                             $qTmp $this->em->getConnection()->prepare($sql);
  323.                             $qTmp->execute();
  324.                             $tmp $qTmp->fetch();
  325.                             if (isset($tmp['slug'])) {
  326.                                 return $this->json($tmp['slug']);
  327.                             }
  328.                         }
  329.                     }
  330.                     break;
  331.                 default:
  332.             }
  333.         }
  334.         return $this->json('');
  335.     }
  336.     public function deleteComment()
  337.     {
  338.         /** @var StoryComment $comment */
  339.         $comment $this->storiesService->getComment($this->dataArray['commentId'], $this->dataArray['domain']);
  340.         /** @var User $user */
  341.         $user $this->userService->get($this->dataArray['userId'], $this->dataArray['domain']);
  342.         /** @var Story $story */
  343.         $story $comment->getStory();
  344.         $storyOwner $story $story->getUser() : null;
  345.         if ($comment && $user) {
  346.             $ownerStoryIsCorrect $storyOwner && $storyOwner->getId() === $user->getId();
  347.             $userIdIsCorrect $comment->getUser() && $comment->getUser()->getId() === $user->getId();
  348.             $isModer $this->userService->isModer($user->getId(), $user->getDomain());
  349.             if ($userIdIsCorrect || $isModer || $ownerStoryIsCorrect) {
  350.                 $comment->setStatus(Status::STATUS_DELETED);
  351.                 $this->storiesService->update($comment);
  352.                 return $this->json([
  353.                     'status' => 'success',
  354.                     'storySlug' => $comment->getStory() ? $comment->getStory()->getSlug() : null,
  355.                     'storyLang' => $comment->getStory() ? $comment->getStory()->getLang() : null,
  356.                 ]);
  357.             }
  358.         }
  359.         return $this->json([
  360.             'status' => 'reject'
  361.         ]);
  362.     }
  363.     public function delete()
  364.     {
  365.         $user $this->security->getUser() ? $this->security->getUser()->getAssociatedUser() : null;
  366.         $story $this->storiesService->get($this->dataArray['id']);
  367.         if (!$story || !$user || ($story->getUser()->getId() !== $user->getId() && !$this->userService->isModer($user->getId(), $user->getDomain()))) {
  368.             return $this->json([
  369.                 'status' => 'reject'
  370.             ]);
  371.         }
  372.         $story->setStatus(Status::STATUS_DELETED);
  373.         $this->storiesService->update($story);
  374.         $storyCategoryIds $this->storiesService->getCategoryIds($story->getCategories());
  375.         return $this->json([
  376.             'status' => 'success',
  377.             'id' => $story->getId(),
  378.             'categories' => $storyCategoryIds ?? null,
  379.             'slug' => $story->getSlug(),
  380.             'lang' => $story->getLang(),
  381.             'authorUsername' => $story->getUser() ? $story->getUser()->getUsername() : null,
  382.         ]);
  383.     }
  384.     public function prev()
  385.     {
  386.         $domain $this->dataArray['domain'];
  387.         $lang $this->dataArray['lang'];
  388.         $id $this->dataArray['id'];
  389.         $data $this->storiesService->getPrev($domain$lang$id);
  390.         return $this->json($data);
  391.     }
  392.     public function next()
  393.     {
  394.         $domain $this->dataArray['domain'];
  395.         $lang $this->dataArray['lang'];
  396.         $id $this->dataArray['id'];
  397.         $data $this->storiesService->getNext($domain$lang$id);
  398.         return $this->json($data);
  399.     }
  400.     public function getStories()
  401.     {
  402.         $data $this->storiesService->getStoriesList(
  403.             $this->dataArray['page'],
  404.             $this->dataArray['perPage'],
  405.             $this->dataArray['domain'],
  406.             $this->dataArray['sort'],
  407.             $this->dataArray['sys_locale'],
  408.             $this->dataArray['categoryId'],
  409.             $this->dataArray['userId']
  410.         );
  411.         return $this->json($data);
  412.     }
  413.     public function getSoftStories()
  414.     {
  415.         $data $this->storiesService->getSoftListByLang(
  416.             $this->dataArray['lang'],
  417.             $this->dataArray['domain'],
  418.             (int)$this->dataArray['cnt']
  419.         );
  420.         return $this->json($data);
  421.     }
  422.     public function getStoriesByCatsIds()
  423.     {
  424.         $domain Domain::isWLDomainHasOwnContent($this->dataArray['original_domain'] ?? '') ? $this->dataArray['original_domain'] : $this->dataArray['domain'];
  425.         $data $this->storiesService->getStoriesListBySeveralIds(
  426.             $this->dataArray['page'],
  427.             $this->dataArray['perPage'],
  428.             $domain,
  429.             $this->dataArray['sort'],
  430.             $this->dataArray['sys_locale'],
  431.             $this->dataArray['storyIds'],
  432.         );
  433.         return $this->json($data);
  434.     }
  435.     public function getStoriesSitemapData(): Response
  436.     {
  437.         $lang $this->dataArray['sys_locale'];
  438.         $domain $this->dataArray['domain'];
  439.         $storySql "select id, name, slug, first_story_category_id from story where status='active' and domain='{$domain}' and lang='{$lang}' order by id desc limit 0,1000";
  440.         $storyQ $this->getDoctrine()->getConnection()->prepare($storySql);
  441.         $storyQ->execute();
  442.         $storyResult $storyQ->fetchAll();
  443.         $data = [];
  444.         foreach ($storyResult as $key => $story) {
  445.             if ($story['first_story_category_id'] === null || $story['first_story_category_id'] === '0') {
  446.                 continue;
  447.             }
  448.             $data[] = $story;
  449.             unset($storyResult[$key]);
  450.         }
  451.         $ids array_map(function ($item) {
  452.             return $item['id'];
  453.         }, $storyResult);
  454.         if (count($ids) > 0) {
  455.             $idsAsString implode(', '$ids);
  456.             $storyCategorySql "select * from story_story_category where story_id in ({$idsAsString})";
  457.             $storyCategoryQ $this->getDoctrine()->getConnection()->prepare($storyCategorySql);
  458.             $storyCategoryQ->execute();
  459.             $storyCategoryResult $storyCategoryQ->fetchAll();
  460.         } else {
  461.             $storyCategoryResult = [];
  462.         }
  463.         foreach ($storyResult as $story) {
  464.             foreach ($storyCategoryResult as $storyCategoryItem) {
  465.                 if ($story['id'] === $storyCategoryItem['story_id']) {
  466.                     $story['first_story_category_id'] = $storyCategoryItem['story_category_id'];
  467.                     $data[] = $story;
  468.                 }
  469.             }
  470.         }
  471.         usort($data, function ($a$b) {
  472.             if ($a['id'] == $b['id']) {
  473.                 return 0;
  474.             }
  475.             return ($a['id'] > $b['id']) ? -1;
  476.         });
  477.         return $this->json($data);
  478.     }
  479. }