<?php
namespace App\Services\Api;
use App\Entity\Customer;
use App\Entity\Exercise;
use App\Entity\ExerciseCalendar;
use App\Entity\ExerciseQualification;
use App\Entity\ExerciseTranslation;
use App\Entity\ExerciseUser;
use App\Form\Type\LocaleType;
use DateTime;
use Exception;
use Symfony\Component\Filesystem\Filesystem;
use neidercode\translatorphp\Traits\TranslateTrait;
class ExerciseManager extends BaseApiManager
{
public function add($data, $request)
{
/** @var Customer $customer */
$customer = $this->security->getUser();
$season = $customer->getSeasonActive();
if (empty($season))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.season_dont_active'),
"code" => 403
];
/** @var Exercise $exercise */
$exercise = $this->em->getRepository(Exercise::class)->find($data['exercise']);
if (empty($exercise))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.exercise_dont_exist'),
"code" => 404
];
$date = \DateTime::createFromFormat("d-m-Y", $data['date']);
$filter = [
"season" => $season->getId(),
"exercise" => $exercise->getId(),
"date" => $date
];
/** @var ExerciseCalendar $exerciseCalendar */
$exerciseCalendar = $this->em->getRepository(ExerciseCalendar::class)->findOneBy($filter);
if (empty($exerciseCalendar)) {
$exerciseCalendar = new ExerciseCalendar();
$exerciseCalendar->setSeason($season)
->setExercise($exercise)
->setTypeExercise($data['type'])
->setDate($date);
$this->em->persist($exerciseCalendar);
$this->em->flush();
}
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_add_success'),
"code" => 200
];
}
use TranslateTrait;
public function list(Customer $customer, String $urlImages)
{
$season = $customer->getSeasonActive();
$exercises = [];
if (!empty($season)) {
$exercises_user = $this->filterNotRemove(Exercise::class, ['customer_id' => $customer->getId()]);
/** @var Exercise $exercise */
foreach ($exercises_user as $exercise) {
$exercises[] = $exercise->__toArray($urlImages);
}
}
return $exercises;
}
public function addCalendar(array $data)
{
/** @var Customer $customer */
$customer = $this->security->getUser();
$season = $customer->getSeasonActive();
if (empty($season))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.season_dont_active'),
"code" => 403
];
/** @var Exercise $exercise */
$exercise = $this->em->getRepository(Exercise::class)->find($data['exercise']);
if (empty($exercise))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.exercise_dont_exist'),
"code" => 404
];
$date = \DateTime::createFromFormat("d-m-Y", $data['date']);
$filter = [
"season" => $season->getId(),
"exercise" => $exercise->getId(),
"date" => $date
];
/** @var ExerciseCalendar $exerciseCalendar */
$exerciseCalendar = $this->em->getRepository(ExerciseCalendar::class)->findOneBy($filter);
if (empty($exerciseCalendar)) {
$exerciseCalendar = new ExerciseCalendar();
$exerciseCalendar->setSeason($season)
->setExercise($exercise)
->setTypeExercise($data['type'])
->setDate($date);
$this->em->persist($exerciseCalendar);
$this->em->flush();
}
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_add_success'),
"code" => 200
];
}
public function removeCalendar(array $data)
{
/** @var Customer $customer */
$customer = $this->security->getUser();
$season = $customer->getSeasonActive();
if (empty($season))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.season_dont_active'),
"code" => 403
];
/** @var Exercise $exercise */
$exercise = $this->em->getRepository(Exercise::class)->find($data['exercise']);
if (empty($exercise))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.exercise_dont_exist'),
"code" => 404
];
$date = \DateTime::createFromFormat("d-m-Y", $data['date']);
$filter = [
"season" => $season->getId(),
"exercise" => $exercise->getId(),
"date" => $date
];
/** @var ExerciseCalendar $exerciseCalendar */
$exerciseCalendar = $this->em->getRepository(ExerciseCalendar::class)->findOneBy($filter);
if (!empty($exerciseCalendar)) {
$this->em->remove($exerciseCalendar);
$this->em->flush();
}
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_delete_success'),
"code" => 200
];
}
public function edit(Exercise $exercise, $data)
{
/** @var Customer $customer */
$customer = $this->security->getUser();
$season = $customer->getSeasonActive();
if (empty($season))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.season_dont_active'),
"code" => 403
];
if (empty($exercise))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.exercise_dont_exist'),
"code" => 404
];
$exercise->setDuration($data['duration']);
$this->em->persist($exercise);
$this->em->flush();
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_edit_success'),
"code" => 200
];
}
protected function validExercise($data, $is_update = false)
{
$code = 200;
$validates = [];
if (empty($data["title"])) {
$code = 422;
$validates["title"] = [
"type" => "error",
"msg" => "El tÃtulo es requerido",
"icon" => "glyphicon-warning-sign",
];
}
return [
"code" => $code,
"messageType" => "warning",
"message" => $this->translator->trans('msg.filed_requiered'),
"validates" => $validates
];
}
public function addExercise($data, $request)
{
$r = $this->validExercise($data);
if ($r['code'] != 200)
return $r;
/** @var Customer $customer */
$customer = $this->security->getUser();
$season = $customer->getSeasonActive();
if (empty($season))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.season_dont_active'),
"code" => 403
];
$exercise = new Exercise();
$exercise = $this->setValues($exercise, $data);
$this->em->persist($exercise);
$this->em->flush();
//get latest exercise
$res = $this->em->getRepository(Exercise::class)->findOneBy(['customer_id' => $data['customerId']], ['id' => 'DESC']);
//update exercise
$exercise = $this->em->getRepository(Exercise::class)->find($res->getId());
$exercise->setPoster($res->getImage());
$this->em->persist($exercise);
$title_t = $request->get('title');
$description_t = $request->get('description');
$exerciseTranslation = new ExerciseTranslation();
$exerciseTranslation->setLocale($request->getLocale());
$exerciseTranslation->setName($title_t);
$exerciseTranslation->setDescription($description_t);
$exerciseTranslation->setTranslatable($res);
$this->em->persist($exerciseTranslation);
$this->em->flush();
if ($request->get('date') && $request->get('type')) {
$data = [
'date' => $request->get('date'),
'exercise' => $exercise->getId(),
'type' => $request->get('type'),
];
$this->add($data, $request);
}
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_add_success'),
"exercise" => $exercise,
"code" => 200
];
}
public function updateExercise($id, $data, $request)
{
/** @var Customer $customer */
$customer = $this->security->getUser();
$season = $customer->getSeasonActive();
if (empty($season))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.season_dont_active'),
"code" => 403
];
/** @var Exercise $exercise */
$exercise = $this->em->getRepository(Exercise::class)->findOneBy([
'id' => $id,
'customer_id' => $customer->getId(),
]);
if (empty($exercise))
return [
"messageType" => "danger",
"message" => $this->translator->trans('msg.exercise_dont_exist'),
"code" => 404
];
$r = $this->validExercise($data, true);
if ($r['code'] != 200)
return $r;
if (empty($data['image']))
unset($data['image']);
/**
* before entering the new materials delete all associations
*/
$exercise->removeMaterials();
$exerciseTranslationManager = $this->em->getRepository(ExerciseTranslation::class);
$locale_request = $request->getLocale();
$locales = LocaleType::getItems();
if ($exercise->getIsShared()) {
foreach ($locales as $key => $value) {
$exerciseTranslation = $exerciseTranslationManager->findOneBy([
'translatable_id' => $exercise->getId(),
'locale' => $value
]);
if ($exerciseTranslation) {
$title = $this->translate($data['title'], $value, $locale_request);
$description = $this->translate($data['description'], $value, $locale_request);
$exerciseTranslation->setName($title);
$exerciseTranslation->setDescription($description);
$this->em->persist($exerciseTranslation);
$this->em->flush();
} else {
$title = $this->translate($data['title'], $value, $locale_request);
$description = $this->translate($data['description'], $value, $locale_request);
$exerciseTranslation = new ExerciseTranslation();
$exerciseTranslation->setLocale($value);
$exerciseTranslation->setName($title);
$exerciseTranslation->setDescription($description);
$exerciseTranslation->setTranslatable($exercise);
$this->em->persist($exerciseTranslation);
$this->em->flush();
}
}
}
if (!$exercise->getIsShared()) {
$exerciseTranslation = $exerciseTranslationManager->findOneBy([
'translatable_id' => $exercise->getId(),
'locale' => $locale_request
]);
if ($exerciseTranslation) {
$exerciseTranslation->setLocale($locale_request);
$exerciseTranslation->setName($data['title']);
$exerciseTranslation->setDescription($data['description']);
$exerciseTranslation->setTranslatable($exercise);
$this->em->persist($exerciseTranslation);
} else {
$exerciseTranslation = new ExerciseTranslation();
$exerciseTranslation->setLocale($locale_request);
$exerciseTranslation->setName($data['title']);
$exerciseTranslation->setDescription($data['description']);
$exerciseTranslation->setTranslatable($exercise);
$this->em->persist($exerciseTranslation);
}
}
$exercise = $this->setValues($exercise, $data);
$this->em->persist($exercise);
$this->em->flush();
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_edit_success'),
"code" => 200
];
}
public function deleteExercise(Exercise $exercise, $prefiFile)
{
if (empty($exercise)){
return [
"messageType" => "warning",
"message" => $this->translator->trans('msg.exercise_dont_exist'),
"code" => 404
];
}
foreach ($this->em->getRepository(ExerciseQualification::class)->findBy(['exercise' => $exercise]) as $key => $exerciseQualification) {
$this->em->remove($exerciseQualification);
$this->em->flush();
}
if (!count($exercise->getExerciseCalendars())) {
$fileSystem = new Filesystem();
if ( ! empty($exercise->getImage())) {
$urlImage = '.' . $prefiFile . '/' . $exercise->getImage();
if (file_exists($urlImage) === true) {
$fileSystem->remove($urlImage);
}
}
if ( ! empty($exercise->getPoster())) {
$urlPoster = '.' . $prefiFile . '/' . $exercise->getPoster();
if (file_exists($urlPoster) === true) {
$fileSystem->remove($urlPoster);
}
}
$this->em->remove($exercise);
$this->em->flush();
}
if (count($exercise->getExerciseCalendars())) {
$exercise->setDeletedAt(new DateTime());
$this->em->flush();
}
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_removed_success'),
"code" => 200
];
}
public function shared($request, $id, $isShared)
{
$exercise = $this->em->getRepository(Exercise::class)->find($id);
if (empty($exercise))
return [
"messageType" => "warning",
"message" => $this->translator->trans('msg.exercise_dont_exist'),
"code" => 404
];
$exercise->setIsShared($isShared);
$exerciseTranslationManager = $this->em->getRepository(ExerciseTranslation::class);
$locale_request = $request->getLocale();
$locales = LocaleType::getItems();
foreach ($locales as $key => $value) {
$exerciseTranslation = $exerciseTranslationManager->findOneBy([
'translatable_id' => $exercise->getId(),
'locale' => $value
]);
if ($exerciseTranslation) {
$title = $this->translate($exercise->getName(), $value, $locale_request);
$description = $this->translate($exercise->getDescription(), $value, $locale_request);
$exerciseTranslation->setName($title);
$exerciseTranslation->setDescription($description);
$this->em->persist($exerciseTranslation);
$this->em->flush();
} else {
$title = $this->translate($exercise->getName(), $value, $locale_request);
$description = $this->translate($exercise->getDescription(), $value, $locale_request);
$exerciseTranslation = new ExerciseTranslation();
$exerciseTranslation->setLocale($value);
$exerciseTranslation->setName($title);
$exerciseTranslation->setDescription($description);
$exerciseTranslation->setTranslatable($exercise);
$this->em->persist($exerciseTranslation);
$this->em->flush();
}
}
$this->em->flush();
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_removed_success'),
"code" => 200
];
}
public function addQualification($id, $quantity)
{
$exercise = $this->em->getRepository(Exercise::class)->find($id);
/** @var Customer $customer */
$customer = $this->security->getUser();
$exerciseQualification = $this->em->getRepository(ExerciseQualification::class)->findOneBy([
'exercise' => $exercise,
'customer' => $customer
]);
if ($exerciseQualification) {
$exerciseQualification->setQuantity($quantity);
$this->em->flush();
}
if (!$exerciseQualification) {
$exerciseQualification = new ExerciseQualification();
$exerciseQualification->setCustomer($customer);
$exerciseQualification->setExercise($exercise);
$exerciseQualification->setQuantity($quantity);
$this->em->persist($exerciseQualification);
$this->em->flush();
}
return [
"messageType" => "success",
"message" => $this->translator->trans('msg.exercise_QUALIFICATION_success'),
"code" => 200
];
}
public function listShared(Customer $customer, string $urlImages)
{
$exercises = [];
$season = $customer->getSeasonActive();
if (!empty($season)) {
$exercises_user = $this->filterNotRemove(Exercise::class, [
'customer_id' => $customer->getId(),
'is_shared' => 1,
]);
/** @var Exercise $exercise */
foreach ($exercises_user as $exercise) {
$stars = $this->em->getRepository(ExerciseQualification::class)->findBy([
'exercise' => $exercise
]);
$result = $exercise->__toArray($urlImages);
$result['qualification'] = 0;
$result['quantityQualifications'] = count($stars);
$result['starsQualifications'] = 0;
foreach ($stars as $key => $value) {
if ($value->getCustomer()->getId()) {
$result['qualification'] = $value->getQuantity();
}
$result['starsQualifications'] = $result['starsQualifications'] + $value->getQuantity();
}
$exercises[] = $result;
}
}
return $exercises;
}
public function listSharedAll(string $urlImages)
{
$exercises = [];
$exercises_user = $this->filterNotRemove(Exercise::class, [
'is_shared' => 1,
]);
/** @var Exercise $exercise */
foreach ($exercises_user as $exercise) {
$stars = $this->em->getRepository(ExerciseQualification::class)->findBy([
'exercise' => $exercise
]);
$result = $exercise->__toArray($urlImages);
$result['qualification'] = 0;
$result['quantityQualifications'] = count($stars);
$result['starsQualifications'] = 0;
foreach ($stars as $key => $value) {
if ($value->getCustomer()->getId()) {
$result['qualification'] = $value->getQuantity();
}
$result['starsQualifications'] = $result['starsQualifications'] + $value->getQuantity();
}
$exercises[] = $result;
}
return $exercises;
}
}