src/Services/Api/ExerciseManager.php line 238

Open in your IDE?
  1. <?php
  2. namespace App\Services\Api;
  3. use App\Entity\Customer;
  4. use App\Entity\Exercise;
  5. use App\Entity\ExerciseCalendar;
  6. use App\Entity\ExerciseQualification;
  7. use App\Entity\ExerciseTranslation;
  8. use App\Entity\ExerciseUser;
  9. use App\Form\Type\LocaleType;
  10. use DateTime;
  11. use Exception;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. use neidercode\translatorphp\Traits\TranslateTrait;
  14. class ExerciseManager extends BaseApiManager
  15. {
  16.   public function add($data$request)
  17.   {
  18.     /** @var Customer $customer */
  19.     $customer $this->security->getUser();
  20.     $season $customer->getSeasonActive();
  21.     if (empty($season))
  22.       return [
  23.         "messageType" => "danger",
  24.         "message" => $this->translator->trans('msg.season_dont_active'),
  25.         "code" => 403
  26.       ];
  27.     /** @var Exercise $exercise */
  28.     $exercise $this->em->getRepository(Exercise::class)->find($data['exercise']);
  29.     if (empty($exercise))
  30.       return [
  31.         "messageType" => "danger",
  32.         "message" => $this->translator->trans('msg.exercise_dont_exist'),
  33.         "code" => 404
  34.       ];
  35.     $date = \DateTime::createFromFormat("d-m-Y"$data['date']);
  36.     $filter = [
  37.       "season" => $season->getId(),
  38.       "exercise" => $exercise->getId(),
  39.       "date" => $date
  40.     ];
  41.     /** @var ExerciseCalendar $exerciseCalendar */
  42.     $exerciseCalendar $this->em->getRepository(ExerciseCalendar::class)->findOneBy($filter);
  43.     if (empty($exerciseCalendar)) {
  44.       $exerciseCalendar = new ExerciseCalendar();
  45.       $exerciseCalendar->setSeason($season)
  46.         ->setExercise($exercise)
  47.         ->setTypeExercise($data['type'])
  48.         ->setDate($date);
  49.       $this->em->persist($exerciseCalendar);
  50.       $this->em->flush();
  51.     }
  52.     return [
  53.       "messageType" => "success",
  54.       "message" => $this->translator->trans('msg.exercise_add_success'),
  55.       "code" => 200
  56.     ];
  57.   }
  58.   use TranslateTrait;
  59.   public function list(Customer $customerString $urlImages)
  60.   {
  61.     $season $customer->getSeasonActive();
  62.     $exercises = [];
  63.     if (!empty($season)) {
  64.       $exercises_user $this->filterNotRemove(Exercise::class, ['customer_id' => $customer->getId()]);
  65.       /** @var Exercise $exercise */
  66.       foreach ($exercises_user as $exercise) {
  67.         $exercises[] = $exercise->__toArray($urlImages);
  68.       }
  69.     }
  70.     return $exercises;
  71.   }
  72.   public function addCalendar(array $data)
  73.   {
  74.     /** @var Customer $customer */
  75.     $customer $this->security->getUser();
  76.     $season $customer->getSeasonActive();
  77.     if (empty($season))
  78.       return [
  79.         "messageType" => "danger",
  80.         "message" => $this->translator->trans('msg.season_dont_active'),
  81.         "code" => 403
  82.       ];
  83.     /** @var Exercise $exercise */
  84.     $exercise $this->em->getRepository(Exercise::class)->find($data['exercise']);
  85.     if (empty($exercise))
  86.       return [
  87.         "messageType" => "danger",
  88.         "message" => $this->translator->trans('msg.exercise_dont_exist'),
  89.         "code" => 404
  90.       ];
  91.     $date = \DateTime::createFromFormat("d-m-Y"$data['date']);
  92.     $filter = [
  93.       "season" => $season->getId(),
  94.       "exercise" => $exercise->getId(),
  95.       "date" => $date
  96.     ];
  97.     /** @var ExerciseCalendar $exerciseCalendar */
  98.     $exerciseCalendar $this->em->getRepository(ExerciseCalendar::class)->findOneBy($filter);
  99.     if (empty($exerciseCalendar)) {
  100.       $exerciseCalendar = new ExerciseCalendar();
  101.       $exerciseCalendar->setSeason($season)
  102.         ->setExercise($exercise)
  103.         ->setTypeExercise($data['type'])
  104.         ->setDate($date);
  105.       $this->em->persist($exerciseCalendar);
  106.       $this->em->flush();
  107.     }
  108.     return [
  109.       "messageType" => "success",
  110.       "message" => $this->translator->trans('msg.exercise_add_success'),
  111.       "code" => 200
  112.     ];
  113.   }
  114.   public function removeCalendar(array $data)
  115.   {
  116.     /** @var Customer $customer */
  117.     $customer $this->security->getUser();
  118.     $season $customer->getSeasonActive();
  119.     if (empty($season))
  120.       return [
  121.         "messageType" => "danger",
  122.         "message" => $this->translator->trans('msg.season_dont_active'),
  123.         "code" => 403
  124.       ];
  125.     /** @var Exercise $exercise */
  126.     $exercise $this->em->getRepository(Exercise::class)->find($data['exercise']);
  127.     if (empty($exercise))
  128.       return [
  129.         "messageType" => "danger",
  130.         "message" => $this->translator->trans('msg.exercise_dont_exist'),
  131.         "code" => 404
  132.       ];
  133.     $date = \DateTime::createFromFormat("d-m-Y"$data['date']);
  134.     $filter = [
  135.       "season" => $season->getId(),
  136.       "exercise" => $exercise->getId(),
  137.       "date" => $date
  138.     ];
  139.     /** @var ExerciseCalendar $exerciseCalendar */
  140.     $exerciseCalendar $this->em->getRepository(ExerciseCalendar::class)->findOneBy($filter);
  141.     if (!empty($exerciseCalendar)) {
  142.       $this->em->remove($exerciseCalendar);
  143.       $this->em->flush();
  144.     }
  145.     return [
  146.       "messageType" => "success",
  147.       "message" => $this->translator->trans('msg.exercise_delete_success'),
  148.       "code" => 200
  149.     ];
  150.   }
  151.   public function edit(Exercise $exercise$data)
  152.   {
  153.     /** @var Customer $customer */
  154.     $customer $this->security->getUser();
  155.     $season $customer->getSeasonActive();
  156.     if (empty($season))
  157.       return [
  158.         "messageType" => "danger",
  159.         "message" => $this->translator->trans('msg.season_dont_active'),
  160.         "code" => 403
  161.       ];
  162.     if (empty($exercise))
  163.       return [
  164.         "messageType" => "danger",
  165.         "message" => $this->translator->trans('msg.exercise_dont_exist'),
  166.         "code" => 404
  167.       ];
  168.     $exercise->setDuration($data['duration']);
  169.     $this->em->persist($exercise);
  170.     $this->em->flush();
  171.     return [
  172.       "messageType" => "success",
  173.       "message" => $this->translator->trans('msg.exercise_edit_success'),
  174.       "code" => 200
  175.     ];
  176.   }
  177.   protected function validExercise($data$is_update false)
  178.   {
  179.     $code 200;
  180.     $validates = [];
  181.     if (empty($data["title"])) {
  182.       $code 422;
  183.       $validates["title"] = [
  184.         "type" => "error",
  185.         "msg" => "El título es requerido",
  186.         "icon" => "glyphicon-warning-sign",
  187.       ];
  188.     }
  189.     return [
  190.       "code" => $code,
  191.       "messageType" => "warning",
  192.       "message" => $this->translator->trans('msg.filed_requiered'),
  193.       "validates" => $validates
  194.     ];
  195.   }
  196.   public function addExercise($data$request)
  197.   {
  198.     $r $this->validExercise($data);
  199.     if ($r['code'] != 200)
  200.       return $r;
  201.     /** @var Customer $customer */
  202.     $customer $this->security->getUser();
  203.     $season $customer->getSeasonActive();
  204.     if (empty($season))
  205.       return [
  206.         "messageType" => "danger",
  207.         "message" => $this->translator->trans('msg.season_dont_active'),
  208.         "code" => 403
  209.       ];
  210.     $exercise = new Exercise();
  211.     $exercise $this->setValues($exercise$data);
  212.     $this->em->persist($exercise);
  213.     $this->em->flush();
  214.     //get latest exercise
  215.     $res $this->em->getRepository(Exercise::class)->findOneBy(['customer_id' => $data['customerId']], ['id' => 'DESC']);
  216.     //update exercise
  217.     $exercise $this->em->getRepository(Exercise::class)->find($res->getId());
  218.     $exercise->setPoster($res->getImage());
  219.     $this->em->persist($exercise);
  220.     $title_t $request->get('title');
  221.     $description_t $request->get('description');
  222.     $exerciseTranslation = new ExerciseTranslation();
  223.     $exerciseTranslation->setLocale($request->getLocale());
  224.     $exerciseTranslation->setName($title_t);
  225.     $exerciseTranslation->setDescription($description_t);
  226.     $exerciseTranslation->setTranslatable($res);
  227.     $this->em->persist($exerciseTranslation);
  228.     $this->em->flush();
  229.     if ($request->get('date') && $request->get('type')) {
  230.       $data = [
  231.         'date' => $request->get('date'),
  232.         'exercise' => $exercise->getId(),
  233.         'type' => $request->get('type'),
  234.       ];
  235.       $this->add($data$request);
  236.     }
  237.     return [
  238.       "messageType" => "success",
  239.       "message" => $this->translator->trans('msg.exercise_add_success'),
  240.       "exercise" => $exercise,
  241.       "code" => 200
  242.     ];
  243.   }
  244.   public function updateExercise($id$data$request)
  245.   {
  246.     /** @var Customer $customer */
  247.     $customer $this->security->getUser();
  248.     $season $customer->getSeasonActive();
  249.     if (empty($season))
  250.       return [
  251.         "messageType" => "danger",
  252.         "message" => $this->translator->trans('msg.season_dont_active'),
  253.         "code" => 403
  254.       ];
  255.     /** @var Exercise $exercise */
  256.     $exercise $this->em->getRepository(Exercise::class)->findOneBy([
  257.       'id' => $id,
  258.       'customer_id' => $customer->getId(),
  259.     ]);
  260.     if (empty($exercise))
  261.       return [
  262.         "messageType" => "danger",
  263.         "message" => $this->translator->trans('msg.exercise_dont_exist'),
  264.         "code" => 404
  265.       ];
  266.     $r $this->validExercise($datatrue);
  267.     if ($r['code'] != 200)
  268.       return $r;
  269.     if (empty($data['image']))
  270.       unset($data['image']);
  271.     /**
  272.      * before entering the new materials delete all associations
  273.      */
  274.     $exercise->removeMaterials();
  275.     $exerciseTranslationManager $this->em->getRepository(ExerciseTranslation::class);
  276.     $locale_request $request->getLocale();
  277.     $locales LocaleType::getItems();
  278.     if ($exercise->getIsShared()) {
  279.       foreach ($locales as $key => $value) {
  280.         $exerciseTranslation $exerciseTranslationManager->findOneBy([
  281.           'translatable_id' => $exercise->getId(),
  282.           'locale' => $value
  283.         ]);
  284.         if ($exerciseTranslation) {
  285.           $title $this->translate($data['title'], $value$locale_request);
  286.           $description $this->translate($data['description'], $value$locale_request);
  287.           $exerciseTranslation->setName($title);
  288.           $exerciseTranslation->setDescription($description);
  289.           $this->em->persist($exerciseTranslation);
  290.           $this->em->flush();
  291.         } else {
  292.           $title $this->translate($data['title'], $value$locale_request);
  293.           $description $this->translate($data['description'], $value$locale_request);
  294.           $exerciseTranslation = new ExerciseTranslation();
  295.           $exerciseTranslation->setLocale($value);
  296.           $exerciseTranslation->setName($title);
  297.           $exerciseTranslation->setDescription($description);
  298.           $exerciseTranslation->setTranslatable($exercise);
  299.           $this->em->persist($exerciseTranslation);
  300.           $this->em->flush();
  301.         }
  302.       }
  303.     }
  304.     
  305.     if (!$exercise->getIsShared()) {
  306.       $exerciseTranslation $exerciseTranslationManager->findOneBy([
  307.         'translatable_id' => $exercise->getId(),
  308.         'locale' => $locale_request
  309.       ]);
  310.       if ($exerciseTranslation) {
  311.         $exerciseTranslation->setLocale($locale_request);
  312.         $exerciseTranslation->setName($data['title']);
  313.         $exerciseTranslation->setDescription($data['description']);
  314.         $exerciseTranslation->setTranslatable($exercise);
  315.         $this->em->persist($exerciseTranslation);
  316.       } else {
  317.         $exerciseTranslation = new ExerciseTranslation();
  318.         $exerciseTranslation->setLocale($locale_request);
  319.         $exerciseTranslation->setName($data['title']);
  320.         $exerciseTranslation->setDescription($data['description']);
  321.         $exerciseTranslation->setTranslatable($exercise);
  322.         $this->em->persist($exerciseTranslation);
  323.       }
  324.     }
  325.     $exercise $this->setValues($exercise$data);
  326.     $this->em->persist($exercise);
  327.     $this->em->flush();
  328.     return [
  329.       "messageType" => "success",
  330.       "message" => $this->translator->trans('msg.exercise_edit_success'),
  331.       "code" => 200
  332.     ];
  333.   }
  334.   public function deleteExercise(Exercise $exercise$prefiFile)
  335.   {
  336.     if (empty($exercise)){
  337.       return [
  338.         "messageType" => "warning",
  339.         "message" => $this->translator->trans('msg.exercise_dont_exist'),
  340.         "code" => 404
  341.       ];
  342.     }
  343.     foreach ($this->em->getRepository(ExerciseQualification::class)->findBy(['exercise' => $exercise]) as $key => $exerciseQualification) {
  344.       $this->em->remove($exerciseQualification);
  345.       $this->em->flush();
  346.     }
  347.     if (!count($exercise->getExerciseCalendars())) {
  348.       
  349.       $fileSystem = new Filesystem();
  350.       if ( ! empty($exercise->getImage())) {
  351.         $urlImage '.' $prefiFile '/' $exercise->getImage();
  352.         if (file_exists($urlImage) === true) {
  353.           $fileSystem->remove($urlImage);
  354.         }
  355.       }
  356.       if ( ! empty($exercise->getPoster())) {
  357.         
  358.         $urlPoster '.' $prefiFile '/' $exercise->getPoster();
  359.         if (file_exists($urlPoster) === true) {
  360.           $fileSystem->remove($urlPoster);
  361.         }
  362.       }
  363.       
  364.       $this->em->remove($exercise);
  365.       $this->em->flush();
  366.     }
  367.     if (count($exercise->getExerciseCalendars())) {
  368.       $exercise->setDeletedAt(new DateTime());
  369.       $this->em->flush();
  370.     }
  371.     return [
  372.       "messageType" => "success",
  373.       "message" => $this->translator->trans('msg.exercise_removed_success'),
  374.       "code" => 200
  375.     ];
  376.   }
  377.   public function shared($request$id$isShared)
  378.   {
  379.     $exercise $this->em->getRepository(Exercise::class)->find($id);
  380.     if (empty($exercise))
  381.       return [
  382.         "messageType" => "warning",
  383.         "message" => $this->translator->trans('msg.exercise_dont_exist'),
  384.         "code" => 404
  385.       ];
  386.     $exercise->setIsShared($isShared);
  387.     $exerciseTranslationManager $this->em->getRepository(ExerciseTranslation::class);
  388.     $locale_request $request->getLocale();
  389.     $locales LocaleType::getItems();
  390.     foreach ($locales as $key => $value) {
  391.       $exerciseTranslation $exerciseTranslationManager->findOneBy([
  392.         'translatable_id' => $exercise->getId(),
  393.         'locale' => $value
  394.       ]);
  395.       if ($exerciseTranslation) {
  396.         $title $this->translate($exercise->getName(), $value$locale_request);
  397.         $description $this->translate($exercise->getDescription(), $value$locale_request);
  398.         $exerciseTranslation->setName($title);
  399.         $exerciseTranslation->setDescription($description);
  400.         $this->em->persist($exerciseTranslation);
  401.         $this->em->flush();
  402.       } else {
  403.         $title $this->translate($exercise->getName(), $value$locale_request);
  404.         $description $this->translate($exercise->getDescription(), $value$locale_request);
  405.         $exerciseTranslation = new ExerciseTranslation();
  406.         $exerciseTranslation->setLocale($value);
  407.         $exerciseTranslation->setName($title);
  408.         $exerciseTranslation->setDescription($description);
  409.         $exerciseTranslation->setTranslatable($exercise);
  410.         $this->em->persist($exerciseTranslation);
  411.         $this->em->flush();
  412.       }
  413.     }
  414.     $this->em->flush();
  415.     return [
  416.       "messageType" => "success",
  417.       "message" => $this->translator->trans('msg.exercise_removed_success'),
  418.       "code" => 200
  419.     ];
  420.   }
  421.   public function addQualification($id$quantity)
  422.   {
  423.     $exercise $this->em->getRepository(Exercise::class)->find($id);
  424.     /** @var Customer $customer */
  425.     $customer $this->security->getUser();
  426.     $exerciseQualification $this->em->getRepository(ExerciseQualification::class)->findOneBy([
  427.       'exercise' => $exercise,
  428.       'customer' => $customer
  429.     ]);
  430.     if ($exerciseQualification) {
  431.       $exerciseQualification->setQuantity($quantity);
  432.       $this->em->flush();
  433.     }
  434.     if (!$exerciseQualification) {
  435.       $exerciseQualification = new ExerciseQualification();
  436.       $exerciseQualification->setCustomer($customer);
  437.       $exerciseQualification->setExercise($exercise);
  438.       $exerciseQualification->setQuantity($quantity);
  439.       $this->em->persist($exerciseQualification);
  440.       $this->em->flush();
  441.     }
  442.     return [
  443.       "messageType" => "success",
  444.       "message" => $this->translator->trans('msg.exercise_QUALIFICATION_success'),
  445.       "code" => 200
  446.     ];
  447.   }
  448.   public function listShared(Customer $customerstring $urlImages)
  449.   {
  450.     $exercises = [];
  451.     $season $customer->getSeasonActive();
  452.     if (!empty($season)) {
  453.       $exercises_user $this->filterNotRemove(Exercise::class, [
  454.         'customer_id' => $customer->getId(),
  455.         'is_shared' => 1,
  456.       ]);
  457.       /** @var Exercise $exercise */
  458.       foreach ($exercises_user as $exercise) {
  459.         $stars $this->em->getRepository(ExerciseQualification::class)->findBy([
  460.           'exercise' => $exercise
  461.         ]);
  462.         $result =  $exercise->__toArray($urlImages);
  463.         $result['qualification'] = 0;
  464.         $result['quantityQualifications'] = count($stars);
  465.         $result['starsQualifications'] = 0;
  466.         foreach ($stars as $key => $value) {
  467.           if ($value->getCustomer()->getId()) {
  468.             $result['qualification'] = $value->getQuantity();
  469.           }
  470.           $result['starsQualifications'] = $result['starsQualifications'] + $value->getQuantity();
  471.         }
  472.         $exercises[] = $result;
  473.       }
  474.     }
  475.     return $exercises;
  476.   }
  477.   public function listSharedAll(string $urlImages)
  478.   {
  479.     $exercises = [];
  480.     $exercises_user $this->filterNotRemove(Exercise::class, [
  481.       'is_shared' => 1,
  482.     ]);
  483.     /** @var Exercise $exercise */
  484.     foreach ($exercises_user as $exercise) {
  485.       $stars $this->em->getRepository(ExerciseQualification::class)->findBy([
  486.         'exercise' => $exercise
  487.       ]);
  488.       $result =  $exercise->__toArray($urlImages);
  489.       $result['qualification'] = 0;
  490.       $result['quantityQualifications'] = count($stars);
  491.       $result['starsQualifications'] = 0;
  492.       foreach ($stars as $key => $value) {
  493.         if ($value->getCustomer()->getId()) {
  494.           $result['qualification'] = $value->getQuantity();
  495.         }
  496.         $result['starsQualifications'] = $result['starsQualifications'] + $value->getQuantity();
  497.       }
  498.       $exercises[] = $result;
  499.     }
  500.     return $exercises;
  501.   }
  502. }