src/Products/NotificationsBundle/Controller/Dashboard/ProfileController.php line 114

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Controller\Dashboard;
  3. use App\Component\ViewLayer\Views\AbstractHtmlView;
  4. use App\Component\ViewLayer\Views\AjaxHtmlView;
  5. use App\Component\ViewLayer\Views\JsonView;
  6. use App\Controller\PaginationTrait;
  7. use Cms\FrontendBundle\Service\Resolvers\SchoolResolver;
  8. use Doctrine\ORM\Tools\Pagination\Paginator;
  9. use Products\NotificationsBundle\Controller\AbstractDashboardController;
  10. use Products\NotificationsBundle\Entity\AbstractContactAttempt;
  11. use Products\NotificationsBundle\Entity\Profile;
  12. use Products\NotificationsBundle\Entity\ProfileContact;
  13. use Products\NotificationsBundle\Form\Forms\Profiles\ProfileSearchForm;
  14. use Products\NotificationsBundle\Model\Searching\ProfileSearch;
  15. use Products\NotificationsBundle\Service\PortalService;
  16. use Products\NotificationsBundle\Util\Preferences;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. /**
  23.  * @Route(
  24.  *     "/profiles",
  25.  * )
  26.  */
  27. final class ProfileController extends AbstractDashboardController
  28. {
  29.     use PaginationTrait;
  30.     const ROUTES__MAIN 'app.notifications.dashboard.profiles.main';
  31.     const ROUTES__VIEW 'app.notifications.dashboard.profiles.view';
  32.     const ROUTES__ACTIVITY 'app.notifications.dashboard.profiles.activity';
  33.     const ROUTES__DETAILS 'app.notifications.dashboard.profiles.details';
  34.     const ROUTES__TOGGLE_ENABLED 'app.notifications.dashboard.profiles.toggle_enabled';
  35.     const ROUTES__TOGGLE_PRIMARY_PREFERENCES 'app.notifications.dashboard.profiles.toggle_primary_preferences';
  36.     const ROUTES__TOGGLE_SECONDARY_PREFERENCES 'app.notifications.dashboard.profiles.toggle_secondary_preferences';
  37.     protected SchoolResolver $schoolResolver;
  38.     /**
  39.      * @param SchoolResolver $schoolResolver
  40.      */
  41.     public function __construct(SchoolResolver $schoolResolver)
  42.     {
  43.         $this->schoolResolver $schoolResolver;
  44.     }
  45.     /**
  46.      * @param int $pagination
  47.      * @return AbstractHtmlView|Response
  48.      *
  49.      * @Route(
  50.      *     "/list/{pagination}",
  51.      *     name = self::ROUTES__MAIN,
  52.      *     requirements = {
  53.      *         "pagination" = "[1-9]\d*",
  54.      *     },
  55.      *     defaults = {
  56.      *         "pagination" = 0,
  57.      *     },
  58.      * )
  59.      */
  60.     public function mainAction(int $pagination 0)
  61.     {
  62.         // AUDIT
  63.         $this->denyAccessUnlessGranted(['app.notifications.contacts.admin''app.notifications.contacts.view']);
  64.         $result $this->doSearch(
  65.             Profile::class,
  66.             'profiles',
  67.             ProfileSearch::class,
  68.             ProfileSearchForm::class,
  69.             $pagination,
  70.         );
  71.         if ($result instanceof Response) {
  72.             return $result;
  73.         }
  74.         $result['isAjax'] = $this->isAjax();
  75.         /** @var Paginator $profiles */
  76.         $profiles $result['profiles'];
  77.         if ($result['isAjax'] === true && $profiles->count() === 1) {
  78.             /** @var Profile $profile */
  79.             $profile $profiles->getIterator()[0];
  80.             return $this->redirectToRoute(self::ROUTES__VIEW, ['profile' => $profile->getId()]);
  81.         }
  82.         return $this->html($result);
  83.     }
  84.     /**
  85.      * @param Profile $profile
  86.      * @return AbstractHtmlView
  87.      *
  88.      * @Route(
  89.      *     "/{profile}",
  90.      *     name = self::ROUTES__VIEW,
  91.      *     requirements = {
  92.      *         "profile" = "[1-9]\d*",
  93.      *     },
  94.      * )
  95.      * @ParamConverter(
  96.      *     "profile",
  97.      *     class = Profile::class,
  98.      * )
  99.      */
  100.     public function viewAction(Profile $profile): AbstractHtmlView
  101.     {
  102.         // AUDIT
  103.         $this->denyAccessUnlessGranted(['app.notifications.contacts.admin''app.notifications.contacts.view']);
  104.         return $this->ajax([
  105.             'profile' => $profile,
  106.             'contacts' => $this->getEntityManager()->getRepository(ProfileContact::class)->findByProfile(
  107.                 $profile,
  108.             ),
  109.             'family' => [],
  110. //            'family' => $this->getEntityManager()->getRepository(Profile::class)->findByFamily(
  111. //                $profile,
  112. //            ),
  113.             'schools' => $this->schoolResolver->resolveSchoolsByStudents(
  114.                 $profile->getStudents()->toArray()
  115.             ),
  116.         ])->unwrap();
  117.     }
  118.     /**
  119.      * @param Profile $profile
  120.      * @param int $pagination
  121.      * @return AjaxHtmlView|RedirectResponse
  122.      *
  123.      * @Route(
  124.      *     "/{profile}/activty/{pagination}",
  125.      *     name = self::ROUTES__ACTIVITY,
  126.      *     requirements = {
  127.      *         "profile" = "[1-9]\d*",
  128.      *         "pagination" = "[1-9]\d*",
  129.      *     },
  130.      *     defaults = {
  131.      *         "pagination" = 0,
  132.      *     },
  133.      * )
  134.      * @ParamConverter(
  135.      *     "profile",
  136.      *     class = Profile::class,
  137.      * )
  138.      */
  139.     public function activityAction(Profile $profileint $pagination 0)
  140.     {
  141.         // AUDIT
  142.         $this->denyAccessUnlessGranted(['app.notifications.contacts.admin''app.notifications.contacts.view']);
  143.         // load up all the attempts for this person
  144.         $attempts $this->getEntityManager()->getRepository(AbstractContactAttempt::class)->findByProfile(
  145.             $profile,
  146.             $this->getPageSize(),
  147.             $this->getPageOffset($pagination),
  148.         );
  149.         // determine if we are out of bounds on the pagination
  150.         if ($this->isPageOutOfBounds($attempts$pagination)) {
  151.             return $this->handlePageOutOfBounds($attempts);
  152.         }
  153.         return $this->ajax([
  154.             'profile' => $profile,
  155.             'attempts' => $attempts,
  156.             'schools' => $this->schoolResolver->resolveSchoolsByStudents($profile->getStudents()->toArray()),
  157.             'pagination' => $this->generatePagination(
  158.                 $attempts,
  159.                 $pagination,
  160.             ),
  161.         ])->unwrap();
  162.     }
  163.     /**
  164.      * @param Profile $profile
  165.      * @return AbstractHtmlView
  166.      *
  167.      * @Route(
  168.      *     "/{profile}/details",
  169.      *     name = self::ROUTES__DETAILS,
  170.      *     requirements = {
  171.      *         "profile" = "[1-9]\d*",
  172.      *     },
  173.      * )
  174.      * @ParamConverter(
  175.      *     "profile",
  176.      *     class = Profile::class,
  177.      * )
  178.      */
  179.     public function detailsAction(Profile $profile): AbstractHtmlView
  180.     {
  181.         // AUDIT
  182.         $this->denyAccessUnlessGranted(['app.notifications.contacts.admin''app.notifications.contacts.view']);
  183.         return $this->ajax([
  184.             'profile' => $profile,
  185.             'schools' => $this->schoolResolver->resolveSchoolsByStudents(
  186.                 $profile->getStudents()->toArray(),
  187.             ),
  188.         ])->unwrap();
  189.     }
  190.     /**
  191.      * @param Request $request
  192.      * @param ProfileContact $contact
  193.      * @return JsonView
  194.      *
  195.      * @Route(
  196.      *     "/_toggle_enabled/{contact}",
  197.      *     name = self::ROUTES__TOGGLE_ENABLED,
  198.      *     methods = {"POST"},
  199.      *     requirements = {
  200.      *         "contact" = "[1-9]\d*",
  201.      *     },
  202.      * )
  203.      * @ParamConverter(
  204.      *     "contact",
  205.      *     class = ProfileContact::class,
  206.      * )
  207.      */
  208.     public function toggleEnabledAction(
  209.         Request $request,
  210.         ProfileContact $contact
  211.     ): JsonView
  212.     {
  213.         // AUDIT
  214.         $this->denyAccessUnlessGranted('app.notifications.contacts.admin');
  215.         $this->getPortalService()->toggleEnabled(
  216.             $contact,
  217.             $request->request->getBoolean('value'),
  218.         );
  219.         $this->getLoggingService()->createLog($contact);
  220.         return $this->jsonView([
  221.             'profile' => $contact->getProfile()->getId(),
  222.             'contact' => $contact->getId(),
  223.             'value' => $contact->isEnabled(),
  224.         ]);
  225.     }
  226.     /**
  227.      * @param Request $request
  228.      * @param ProfileContact $contact
  229.      * @return JsonView
  230.      *
  231.      * @Route(
  232.      *     "/_toggle_primary_preferences/{contact}",
  233.      *     name = self::ROUTES__TOGGLE_PRIMARY_PREFERENCES,
  234.      *     methods = {"POST"},
  235.      *     requirements = {
  236.      *         "contact" = "[1-9]\d*",
  237.      *     },
  238.      * )
  239.      * @ParamConverter(
  240.      *     "contact",
  241.      *     class = ProfileContact::class,
  242.      * )
  243.      */
  244.     public function togglePrimaryPreferencesAction(
  245.         Request $request,
  246.         ProfileContact $contact
  247.     ): JsonView
  248.     {
  249.         // AUDIT
  250.         $this->denyAccessUnlessGranted('app.notifications.contacts.admin');
  251.         $this->getPortalService()->togglePrimaryPreference(
  252.             $contact,
  253.             $preference $request->request->getAlpha('preference'),
  254.             $request->request->getBoolean('value'),
  255.         );
  256.         $this->getLoggingService()->createLog($contact);
  257.         return $this->jsonView([
  258.             'profile' => $contact->getProfile()->getId(),
  259.             'contact' => $contact->getId(),
  260.             'preference' => Preferences::identity($preference),
  261.             'value' => $contact->hasPrimaryPreference($preference),
  262.         ]);
  263.     }
  264.     /**
  265.      * @param Request $request
  266.      * @param ProfileContact $contact
  267.      * @return JsonView
  268.      *
  269.      * @Route(
  270.      *     "/_toggle_secondary_preferences/{contact}",
  271.      *     name = self::ROUTES__TOGGLE_SECONDARY_PREFERENCES,
  272.      *     methods = {"POST"},
  273.      *     requirements = {
  274.      *         "contact" = "[1-9]\d*",
  275.      *     },
  276.      * )
  277.      * @ParamConverter(
  278.      *     "contact",
  279.      *     class = ProfileContact::class,
  280.      * )
  281.      */
  282.     public function toggleSecondaryPreferencesAction(
  283.         Request $request,
  284.         ProfileContact $contact
  285.     ): JsonView
  286.     {
  287.         // AUDIT
  288.         $this->denyAccessUnlessGranted('app.notifications.contacts.admin');
  289.         $this->getPortalService()->toggleSecondaryPreference(
  290.             $contact,
  291.             $preference $request->request->getAlpha('preference'),
  292.             $request->request->getBoolean('value'),
  293.         );
  294.         $this->getLoggingService()->createLog($contact);
  295.         return $this->jsonView([
  296.             'profile' => $contact->getProfile()->getId(),
  297.             'contact' => $contact->getId(),
  298.             'preference' => Preferences::identity($preference),
  299.             'value' => $contact->hasSecondaryPreference($preference),
  300.         ]);
  301.     }
  302.     /**
  303.      * @return PortalService|object
  304.      */
  305.     private function getPortalService(): PortalService
  306.     {
  307.         return $this->get(__METHOD__);
  308.     }
  309. }