<?php
namespace Products\NotificationsBundle\Controller\Dashboard;
use App\Component\ViewLayer\Views\AbstractHtmlView;
use App\Component\ViewLayer\Views\AjaxHtmlView;
use App\Component\ViewLayer\Views\JsonView;
use App\Controller\PaginationTrait;
use Cms\FrontendBundle\Service\Resolvers\SchoolResolver;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Products\NotificationsBundle\Controller\AbstractDashboardController;
use Products\NotificationsBundle\Entity\AbstractContactAttempt;
use Products\NotificationsBundle\Entity\Profile;
use Products\NotificationsBundle\Entity\ProfileContact;
use Products\NotificationsBundle\Form\Forms\Profiles\ProfileSearchForm;
use Products\NotificationsBundle\Model\Searching\ProfileSearch;
use Products\NotificationsBundle\Service\PortalService;
use Products\NotificationsBundle\Util\Preferences;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(
* "/profiles",
* )
*/
final class ProfileController extends AbstractDashboardController
{
use PaginationTrait;
const ROUTES__MAIN = 'app.notifications.dashboard.profiles.main';
const ROUTES__VIEW = 'app.notifications.dashboard.profiles.view';
const ROUTES__ACTIVITY = 'app.notifications.dashboard.profiles.activity';
const ROUTES__DETAILS = 'app.notifications.dashboard.profiles.details';
const ROUTES__TOGGLE_ENABLED = 'app.notifications.dashboard.profiles.toggle_enabled';
const ROUTES__TOGGLE_PRIMARY_PREFERENCES = 'app.notifications.dashboard.profiles.toggle_primary_preferences';
const ROUTES__TOGGLE_SECONDARY_PREFERENCES = 'app.notifications.dashboard.profiles.toggle_secondary_preferences';
protected SchoolResolver $schoolResolver;
/**
* @param SchoolResolver $schoolResolver
*/
public function __construct(SchoolResolver $schoolResolver)
{
$this->schoolResolver = $schoolResolver;
}
/**
* @param int $pagination
* @return AbstractHtmlView|Response
*
* @Route(
* "/list/{pagination}",
* name = self::ROUTES__MAIN,
* requirements = {
* "pagination" = "[1-9]\d*",
* },
* defaults = {
* "pagination" = 0,
* },
* )
*/
public function mainAction(int $pagination = 0)
{
// AUDIT
$this->denyAccessUnlessGranted(['app.notifications.contacts.admin', 'app.notifications.contacts.view']);
$result = $this->doSearch(
Profile::class,
'profiles',
ProfileSearch::class,
ProfileSearchForm::class,
$pagination,
);
if ($result instanceof Response) {
return $result;
}
$result['isAjax'] = $this->isAjax();
/** @var Paginator $profiles */
$profiles = $result['profiles'];
if ($result['isAjax'] === true && $profiles->count() === 1) {
/** @var Profile $profile */
$profile = $profiles->getIterator()[0];
return $this->redirectToRoute(self::ROUTES__VIEW, ['profile' => $profile->getId()]);
}
return $this->html($result);
}
/**
* @param Profile $profile
* @return AbstractHtmlView
*
* @Route(
* "/{profile}",
* name = self::ROUTES__VIEW,
* requirements = {
* "profile" = "[1-9]\d*",
* },
* )
* @ParamConverter(
* "profile",
* class = Profile::class,
* )
*/
public function viewAction(Profile $profile): AbstractHtmlView
{
// AUDIT
$this->denyAccessUnlessGranted(['app.notifications.contacts.admin', 'app.notifications.contacts.view']);
return $this->ajax([
'profile' => $profile,
'contacts' => $this->getEntityManager()->getRepository(ProfileContact::class)->findByProfile(
$profile,
),
'family' => [],
// 'family' => $this->getEntityManager()->getRepository(Profile::class)->findByFamily(
// $profile,
// ),
'schools' => $this->schoolResolver->resolveSchoolsByStudents(
$profile->getStudents()->toArray()
),
])->unwrap();
}
/**
* @param Profile $profile
* @param int $pagination
* @return AjaxHtmlView|RedirectResponse
*
* @Route(
* "/{profile}/activty/{pagination}",
* name = self::ROUTES__ACTIVITY,
* requirements = {
* "profile" = "[1-9]\d*",
* "pagination" = "[1-9]\d*",
* },
* defaults = {
* "pagination" = 0,
* },
* )
* @ParamConverter(
* "profile",
* class = Profile::class,
* )
*/
public function activityAction(Profile $profile, int $pagination = 0)
{
// AUDIT
$this->denyAccessUnlessGranted(['app.notifications.contacts.admin', 'app.notifications.contacts.view']);
// load up all the attempts for this person
$attempts = $this->getEntityManager()->getRepository(AbstractContactAttempt::class)->findByProfile(
$profile,
$this->getPageSize(),
$this->getPageOffset($pagination),
);
// determine if we are out of bounds on the pagination
if ($this->isPageOutOfBounds($attempts, $pagination)) {
return $this->handlePageOutOfBounds($attempts);
}
return $this->ajax([
'profile' => $profile,
'attempts' => $attempts,
'schools' => $this->schoolResolver->resolveSchoolsByStudents($profile->getStudents()->toArray()),
'pagination' => $this->generatePagination(
$attempts,
$pagination,
),
])->unwrap();
}
/**
* @param Profile $profile
* @return AbstractHtmlView
*
* @Route(
* "/{profile}/details",
* name = self::ROUTES__DETAILS,
* requirements = {
* "profile" = "[1-9]\d*",
* },
* )
* @ParamConverter(
* "profile",
* class = Profile::class,
* )
*/
public function detailsAction(Profile $profile): AbstractHtmlView
{
// AUDIT
$this->denyAccessUnlessGranted(['app.notifications.contacts.admin', 'app.notifications.contacts.view']);
return $this->ajax([
'profile' => $profile,
'schools' => $this->schoolResolver->resolveSchoolsByStudents(
$profile->getStudents()->toArray(),
),
])->unwrap();
}
/**
* @param Request $request
* @param ProfileContact $contact
* @return JsonView
*
* @Route(
* "/_toggle_enabled/{contact}",
* name = self::ROUTES__TOGGLE_ENABLED,
* methods = {"POST"},
* requirements = {
* "contact" = "[1-9]\d*",
* },
* )
* @ParamConverter(
* "contact",
* class = ProfileContact::class,
* )
*/
public function toggleEnabledAction(
Request $request,
ProfileContact $contact
): JsonView
{
// AUDIT
$this->denyAccessUnlessGranted('app.notifications.contacts.admin');
$this->getPortalService()->toggleEnabled(
$contact,
$request->request->getBoolean('value'),
);
$this->getLoggingService()->createLog($contact);
return $this->jsonView([
'profile' => $contact->getProfile()->getId(),
'contact' => $contact->getId(),
'value' => $contact->isEnabled(),
]);
}
/**
* @param Request $request
* @param ProfileContact $contact
* @return JsonView
*
* @Route(
* "/_toggle_primary_preferences/{contact}",
* name = self::ROUTES__TOGGLE_PRIMARY_PREFERENCES,
* methods = {"POST"},
* requirements = {
* "contact" = "[1-9]\d*",
* },
* )
* @ParamConverter(
* "contact",
* class = ProfileContact::class,
* )
*/
public function togglePrimaryPreferencesAction(
Request $request,
ProfileContact $contact
): JsonView
{
// AUDIT
$this->denyAccessUnlessGranted('app.notifications.contacts.admin');
$this->getPortalService()->togglePrimaryPreference(
$contact,
$preference = $request->request->getAlpha('preference'),
$request->request->getBoolean('value'),
);
$this->getLoggingService()->createLog($contact);
return $this->jsonView([
'profile' => $contact->getProfile()->getId(),
'contact' => $contact->getId(),
'preference' => Preferences::identity($preference),
'value' => $contact->hasPrimaryPreference($preference),
]);
}
/**
* @param Request $request
* @param ProfileContact $contact
* @return JsonView
*
* @Route(
* "/_toggle_secondary_preferences/{contact}",
* name = self::ROUTES__TOGGLE_SECONDARY_PREFERENCES,
* methods = {"POST"},
* requirements = {
* "contact" = "[1-9]\d*",
* },
* )
* @ParamConverter(
* "contact",
* class = ProfileContact::class,
* )
*/
public function toggleSecondaryPreferencesAction(
Request $request,
ProfileContact $contact
): JsonView
{
// AUDIT
$this->denyAccessUnlessGranted('app.notifications.contacts.admin');
$this->getPortalService()->toggleSecondaryPreference(
$contact,
$preference = $request->request->getAlpha('preference'),
$request->request->getBoolean('value'),
);
$this->getLoggingService()->createLog($contact);
return $this->jsonView([
'profile' => $contact->getProfile()->getId(),
'contact' => $contact->getId(),
'preference' => Preferences::identity($preference),
'value' => $contact->hasSecondaryPreference($preference),
]);
}
/**
* @return PortalService|object
*/
private function getPortalService(): PortalService
{
return $this->get(__METHOD__);
}
}