src/Controller/api/ApiController.php line 175
<?php
namespace App\Controller\api;
use App\Classes\ApiUtility;
use App\Classes\ASUtility;
use App\Classes\CommonUtility;
use App\Entity\AccountCodes;
use App\Entity\Configurations;
use App\Repository\ConfigurationsRepository;
use App\Repository\ActivityAssignmentsRepository;
use App\Repository\ActivityDefineCrewsRepository;
use App\Repository\ActivityDefineCrewPairsRepository;
use App\Repository\AccountCodeActivitiesRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
/**
* @Route("/api")
*/
class ApiController extends AbstractController
{
private $apiLogger;
private $apiUtility;
private $activityAssignmentsRepository;
private $activityDefineCrewsRepository;
private $activityDefineCrewPairsRepository;
private $accountCodeActivitiesRepository;
private $entityManager;
public function __construct(
ApiUtility $apiUtility,
LoggerInterface $apiLogger,
EntityManagerInterface $entityManager,
ConfigurationsRepository $configurationsRepository,
ActivityAssignmentsRepository $activityAssignmentsRepository,
ActivityDefineCrewsRepository $activityDefineCrewsRepository,
ActivityDefineCrewPairsRepository $activityDefineCrewPairsRepository,
AccountCodeActivitiesRepository $accountCodeActivitiesRepository,
) {
header('Content-type: application/json');
$this->apiUtility = $apiUtility;
$this->activityAssignmentsRepository = $activityAssignmentsRepository;
$this->activityDefineCrewsRepository = $activityDefineCrewsRepository;
$this->activityDefineCrewPairsRepository = $activityDefineCrewPairsRepository;
$this->accountCodeActivitiesRepository = $accountCodeActivitiesRepository;
$this->apiLogger = $apiLogger;
$this->entityManager = $entityManager;
$CONF_EW_API_ADMIN_USERNAME = $configurationsRepository->findByCode(Configurations::CONF_EW_API_ADMIN_USERNAME);
$CONF_EW_API_ADMIN_PASSWORD = $configurationsRepository->findByCode(Configurations::CONF_EW_API_ADMIN_PASSWORD);
// dd($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'],
//$CONF_EW_API_ADMIN_USERNAME, $CONF_EW_API_ADMIN_PASSWORD);
if (
(isset($_SERVER['PHP_AUTH_USER']) && ($_SERVER['PHP_AUTH_USER'] === $CONF_EW_API_ADMIN_USERNAME))
&& (isset($_SERVER['PHP_AUTH_PW']) && ($_SERVER['PHP_AUTH_PW'] === $CONF_EW_API_ADMIN_PASSWORD))
) {
} else {
//Send headers to cause a browser to request
//username and password from user
header("WWW-Authenticate: " . "Basic realm=\"AS-EW Protected Area\"");
header("HTTP/1.0 401 Unauthorized");
//Show failure text, which browsers usually
//show only after several failed attempts
print("invalid webservice credentials");
exit;
}
}
/**
* @Route("/hworld", name="hello_world", methods={"GET"})
* @return Response
*/
public function helloWorld(Request $request)
{
$response = 'Hello World';
return $this->json($response);
}
/**
* @Route("/checkActivityAssignWorkers", name="check_activity_assign_workers", methods={"GET"})
* @param Request $request
* @return Response
*/
public function checkActivityAssignWorkers(Request $request)
{
$conferenceId = $request->query->get('conferenceId');
$worksheetId = $request->query->get('worksheetId');
$response = array();
try {
if ($conferenceId && $worksheetId) {
$response = $this->activityAssignmentsRepository->findByFilters(array(
'assign_workers' => true,
'conference_id' => $conferenceId,
'worksheet_id' => $worksheetId
));
$response = (CommonUtility::countArray($response) > 0) ? true : false;
}
} catch (\Exception $e) {
$response = array(
"error" => array(
"message" => $e->getMessage(),
"code" => $e->getCode()
)
);
}
return $this->json($response);
}
/**
* @Route("/loadActivityAssignWorkers", name="load_activity_assign_workers", methods={"GET"})
* @param Request $request
* @return Response
*/
public function loadActivityAssignWorkers(Request $request)
{
$conferenceId = $request->query->get('conferenceId');
$schoolYear = $request->query->get('schoolYear');
$eventId = $request->query->get('eventId');
$dateId = $request->query->get('dateId');
$aglId = $request->query->get('aglId');
$response = array();
try {
if ($conferenceId && $dateId && $eventId) {
$response = $this->activityAssignmentsRepository->findByFilters(array(
'assign_workers' => true,
'conference_id' => $conferenceId,
'school_year' => $schoolYear,
'date_id' => $dateId,
'event_id' => $eventId,
));
if (CommonUtility::countArray($response) > 0) {
$response = $response[$aglId];
}
}
} catch (\Exception $e) {
$response = array(
"error" => array(
"message" => $e->getMessage(),
"code" => $e->getCode()
)
);
}
return $this->json($response);
}
/**
* @Route("/eventSync", name="event_sync", methods={"POST"})
* @param Request $request
* @return Response
*/
public function eventSync(Request $request)
{
$parameters = json_decode($request->getContent(), true);
$action = (isset($parameters["action"])) ? $parameters["action"] : '';
$eventIds = (isset($parameters["eventIds"])) ? $parameters["eventIds"] : array();
$conferenceId = (isset($parameters["conferenceId"])) ? $parameters["conferenceId"] : 0;
$details = (isset($parameters["details"])) ? $parameters["details"] : array();
$this->apiLogger->info(" --- START EVENT SYNCING ---");
$this->apiLogger->info("parameters: " . $request->getContent());
$response = array();
$objEntityManager = $this->entityManager;
try {
if (isset($action)) {
if ($action === 'edit') {
if (CommonUtility::countArray($eventIds) > 0) {
$aActivityAssignments = $this->activityAssignmentsRepository->findByFilters(array(
'conference_id' => $conferenceId,
'event_ids' => $eventIds,
'event' => true
));
if (CommonUtility::countArray($aActivityAssignments) > 0) {
$aActivityAssignmentsEventIdFound = array();
foreach ($aActivityAssignments as $assId) {
$objActivityAssignmentsRepository = $this->activityAssignmentsRepository
->find($assId['assignment_id']);
if ($objActivityAssignmentsRepository) {
if ($objActivityAssignmentsRepository->getEventId()) {
$aActivityAssignmentsEventIdFound[] =
$objActivityAssignmentsRepository->getEventId();
}
if (isset($details["date_id"])) {
$objActivityAssignmentsRepository->setDateId($details["date_id"]);
}
if (isset($details["event_id"])) {
$objActivityAssignmentsRepository->setEventId($details["event_id"]);
}
if (isset($details["season_id"])) {
$objActivityAssignmentsRepository->setSeasonId($details["season_id"]);
}
if (isset($details["season_name"])) {
$objActivityAssignmentsRepository->setSeasonName($details["season_name"]);
}
if (isset($details["activity_id"])) {
$objActivityAssignmentsRepository->setActivityId($details["activity_id"]);
}
if (isset($details["activity_name"])) {
$objActivityAssignmentsRepository->setActivityName($details["activity_name"]);
}
if (isset($details["event_date"]) || isset($details["event_updated_details"])) {
$eventDate = $details["event_updated_details"]["date"]["new"] !== ""
? $details["event_updated_details"]["date"]["new"]
: $details["event_date"];
$eventDate = ($eventDate != '')
? \DateTime::createFromFormat('Y-m-d', $eventDate) : null;
$objActivityAssignmentsRepository->setEventDate($eventDate);
}
if (isset($details["date_start"]) || isset($details["event_updated_details"])) {
$dateStart = $details["event_updated_details"]["date"]["new"] !== ""
? $details["event_updated_details"]["date"]["new"]
: $details["date_start"];
;
$dateStart = ($dateStart != '')
? \DateTime::createFromFormat('Y-m-d', $dateStart) : null;
$objActivityAssignmentsRepository->setDateStart($dateStart);
}
if (isset($details["date_end"]) || isset($details["event_updated_details"])) {
$dateEnd = $details["event_updated_details"]["date"]["new"] !== ""
? $details["event_updated_details"]["date"]["new"]
: $details["date_end"];
$dateEnd = ($dateEnd != '')
? \DateTime::createFromFormat('Y-m-d', $dateEnd) : null;
$objActivityAssignmentsRepository->setDateEnd($dateEnd);
}
if (isset($details["time_start"]) || isset($details["event_updated_details"])) {
$timeStart = $details["event_updated_details"]["time"]["new"]["start"]
!== "" ? $details["event_updated_details"]["time"]["new"]["start"]
: $details["time_start"];
$timeStart = ($timeStart != '')
? \DateTime::createFromFormat('H:i:s', $timeStart) : null;
$objActivityAssignmentsRepository->setTimeStart($timeStart);
}
if (isset($details["time_end"]) || isset($details["event_updated_details"])) {
$timeEnd = $details["event_updated_details"]["time"]["new"]["end"]
!== "" ? $details["event_updated_details"]["time"]["new"]["end"]
: $details["time_end"];
$timeEnd = ($timeEnd != '')
? \DateTime::createFromFormat('H:i:s', $timeEnd) : null;
$objActivityAssignmentsRepository->setTimeEnd($timeEnd);
}
if (isset($details["location"]) || isset($details["event_updated_details"])) {
$location = $details["event_updated_details"]["location"]["new"] !== ""
? $details[
"event_updated_details"
]["location"]["new"] : $details["location"];
$objActivityAssignmentsRepository->setLocation($location);
}
if (isset($details["home"])) {
$objActivityAssignmentsRepository->setHome($details["home"]);
}
if (isset($details["away_infos"])) {
$away = "";
$awayInfos = array();
if (CommonUtility::countArray($details["away_infos"]) > 0) {
$awayInfos = $details["away_infos"];
$away = $awayInfos[0]["name"];
}
$objActivityAssignmentsRepository->setAway($away);
$objActivityAssignmentsRepository->setAwayInfos(json_encode($awayInfos));
}
if (isset($details["is_fs_connected"])) {
$objActivityAssignmentsRepository->setIsFsConnected(
$details["is_fs_connected"]
);
}
if (isset($details["event_status_id"])) {
$objActivityAssignmentsRepository->setEventStatusId(
$details["event_status_id"]
);
}
if (isset($details["event_status_name"])) {
$objActivityAssignmentsRepository->setEventStatusName(
$details["event_status_name"]
);
}
if (isset($details["event_status_active"])) {
$objActivityAssignmentsRepository->setEventStatusActive(
intval($details["event_status_active"])
);
}
if (isset($details["event_updated_details"])) {
$objActivityAssignmentsRepository->setEventUpdatedDetails(
json_encode([$details["event_updated_details"]])
);
}
//updating activity assignment info....
$objEntityManager->persist($objActivityAssignmentsRepository);
$objEntityManager->flush();
}
}
if (CommonUtility::countArray($aActivityAssignmentsEventIdFound) > 0) {
$response = array(
"success" => array(
"event_ids" => $aActivityAssignmentsEventIdFound,
"message" => "Successfully Updated",
"code" => 200
)
);
} else {
$response = array(
"error" => array(
"event_ids" => $eventIds,
"message" => "No Event IDs found",
"code" => 404
)
);
}
} else {
$response = array(
"error" => array(
"event_ids" => $eventIds,
"message" => "No Event IDs found",
"code" => 404
)
);
}
} else {
$response = array(
"error" => array(
"message" => "DATA NOT FOUND",
"code" => 404
)
);
}
} elseif ($action === 'delete') {
if (CommonUtility::countArray($eventIds) > 0) {
$aActivityAssignments = $this->activityAssignmentsRepository->findByFilters(array(
'conference_id' => $conferenceId,
'event_ids' => $eventIds > 0 ? $eventIds : false,
'event' => true
));
if (CommonUtility::countArray($aActivityAssignments) > 0) {
$response = array(
"success" => array(
"data" => $aActivityAssignments,
"code" => 200
)
);
} else {
$response = array(
"success" => array(
"message" => "Event(s) not found!",
"code" => 200
)
);
}
}
} else {
$response = array(
"error" => array(
"message" => "Please specify action to be process",
"code" => 404
)
);
}
} else {
$response = array(
"error" => array(
"message" => "Please specify action to be process",
"code" => 404
)
);
}
} catch (\Exception $e) {
$response = array(
"error" => array(
"message" => $e->getMessage(),
"code" => $e->getCode()
)
);
}
return $this->json($response);
}
/**
* @Route("/deleteEventSync", name="delete_event_sync", methods={"POST"})
* @param Request $request
* @return Response
*/
public function deleteEventSync(Request $request)
{
$conferenceId = $request->query->get('conferenceId');
$workSheetIds = $request->query->get('workSheetIds');
$this->apiLogger->info(" --- START EVENT DELETE SYNCING ---");
$this->apiLogger->info("conferenceId: " . $conferenceId);
$this->apiLogger->info("workSheetIds: " . serialize($workSheetIds));
$workSheetIds = ($workSheetIds) ? $workSheetIds : array();
$response = false;
if ($conferenceId > 0 && CommonUtility::countArray($workSheetIds) > 0) {
$aActivityAssignments = $this->activityAssignmentsRepository->findByFilters(array(
'conference_id' => $conferenceId,
'worksheet_ids' => $workSheetIds,
'event' => true
));
if (CommonUtility::countArray($aActivityAssignments) > 0) {
$this->activityAssignmentsRepository->deleteActivityAssignments(array(
'conference_id' => $conferenceId,
"worksheet_ids" => $workSheetIds
));
$response = true;
}
}
return $this->json($response);
}
public function eventSOSync(Request $request)
{
$parameters = json_decode($request->getContent(), true);
$action = (isset($parameters["action"])) ? $parameters["action"] : '';
$eventIds = (isset($parameters["eventIds"])) ? $parameters["eventIds"] : array();
$conferenceId = (isset($parameters["conferenceId"])) ? $parameters["conferenceId"] : 0;
$details = (isset($parameters["details"])) ? $parameters["details"] : array();
$this->apiLogger->info(" --- START EVENT SO SYNCING ---");
$this->apiLogger->info("parameters: " . $request->getContent());
$response = array();
$objEntityManager = $this->entityManager;
try {
// if(count($eventIds) > 0){
// $aActivityAssignments = $this->activityAssignmentsRepository->findByFilters(array(
// 'conference_id' => $conferenceId,
// 'event_ids' => $eventIds,
// 'event' => true
// ));
// if(count($aActivityAssignments) > 0){
// $aActivityAssignmentsEventIdFound = array();
// foreach($aActivityAssignments AS $assId){
// $objActivityAssignmentsRepository = $this->activityAssignmentsRepository->find($assId);
// if($objActivityAssignmentsRepository){
// if($objActivityAssignmentsRepository->getEventId())
// $aActivityAssignmentsEventIdFound[]
// = $objActivityAssignmentsRepository->getEventId();
// if(isset($details["date_id"]))
// $objActivityAssignmentsRepository->setDateId($details["date_id"]);
// if(isset($details["event_id"]))
// $objActivityAssignmentsRepository->setEventId($details["event_id"]);
// if(isset($details["season_id"]))
// $objActivityAssignmentsRepository->setSeasonId($details["season_id"]);
// if(isset($details["season_name"]))
// $objActivityAssignmentsRepository->setSeasonName($details["season_name"]);
// if(isset($details["activity_id"]))
// $objActivityAssignmentsRepository->setActivityId($details["activity_id"]);
// if(isset($details["activity_name"]))
// $objActivityAssignmentsRepository->setActivityName($details["activity_name"]);
// if(isset($details["event_date"])){
// $eventDate = $details["event_date"];
// $eventDate = ($eventDate != '')
// ? \DateTime::createFromFormat('Y-m-d', $eventDate) : null;
// $objActivityAssignmentsRepository->setEventDate($eventDate);
// }
// if(isset($details["date_start"])){
// $dateStart = $details["date_start"];
// $dateStart = ($dateStart != '')
// ? \DateTime::createFromFormat('Y-m-d', $dateStart) : null;
// $objActivityAssignmentsRepository->setDateStart($dateStart);
// }
// if(isset($details["date_end"])){
// $dateEnd = $details["date_end"];
// $dateEnd = ($dateEnd != '')? \DateTime::createFromFormat('Y-m-d', $dateEnd) : null;
// $objActivityAssignmentsRepository->setDateEnd($dateEnd);
// }
// if(isset($details["time_start"])){
// $timeStart = $details["time_start"];
// $timeStart = ($timeStart != '')
//? \DateTime::createFromFormat('H:i:s', $timeStart) : null;
// $objActivityAssignmentsRepository->setTimeStart($timeStart);
// }
// if(isset($details["time_end"])){
// $timeEnd = $details["time_end"];
// $timeEnd = ($timeEnd != '')? \DateTime::createFromFormat('H:i:s', $timeEnd) : null;
// $objActivityAssignmentsRepository->setTimeEnd($timeEnd);
// }
// if(isset($details["location"]))
// $objActivityAssignmentsRepository->setLocation($details["location"]);
// if(isset($details["home"]))
// $objActivityAssignmentsRepository->setHome($details["home"]);
// if(isset($details["away_infos"])){
// $away = "";
// $awayInfos = array();
// if(count($details["away_infos"]) > 0){
// $awayInfos = $details["away_infos"];
// $away = $awayInfos[0]["name"];
// }
// $objActivityAssignmentsRepository->setAway($away);
// $objActivityAssignmentsRepository->setAwayInfos(json_encode($awayInfos));
// }
// if(isset($details["is_fs_connected"]))
// $objActivityAssignmentsRepository->setIsFsConnected($details["is_fs_connected"]);
// //updating activity assignment info....
// $objEntityManager->persist($objActivityAssignmentsRepository);
// $objEntityManager->flush();
// }
// }
// if(count($aActivityAssignmentsEventIdFound) > 0){
// $response = array(
// "success" => array(
// "event_ids" => $aActivityAssignmentsEventIdFound,
// "message" => "Successfully Updated",
// "code" => 200
// )
// );
// }else{
// $response = array(
// "error" => array(
// "event_ids" => $eventIds,
// "message" => "No Event IDs found",
// "code" => 404
// )
// );
// }
// } else {
// $response = array(
// "error" => array(
// "event_ids" => $eventIds,
// "message" => "No Event IDs found",
// "code" => 404
// )
// );
// }
// }else{
// $response = array(
// "error" => array(
// "message" => "DATA NOT FOUND",
// "code" => 404
// )
// );
// }
} catch (\Exception $e) {
$response = array(
"error" => array(
"message" => $e->getMessage(),
"code" => $e->getCode()
)
);
}
return $this->json($response);
}
/**
* @Route("/aglSync", name="agl_sync", methods={"POST"})
* @param Request $request
* @return Response
*/
public function activityGenderLevelSync(Request $request)
{
$parameters = json_decode($request->getContent(), true);
$action = (isset($parameters["action"])) ? $parameters["action"] : '';
$activityId = (isset($parameters["activityId"])) ? $parameters["activityId"] : '';
$activityName = (isset($parameters["activityName"])) ? $parameters["activityName"] : '';
$this->apiLogger->info(" --- START AGL SYNCING ---");
$this->apiLogger->info("action: " . $action);
$this->apiLogger->info("activityId: " . $activityId);
$this->apiLogger->info("activityName: " . $activityName);
$response = array();
$objEntityManager = $this->entityManager;
try {
if ($activityId != "" && $activityName != "") {
if ($action === "edit") {
#activity assignment...
$aActivityAssignmentsRepository = $this->activityAssignmentsRepository
->findAll(array('activity_id' => $activityId));
if (CommonUtility::countArray($aActivityAssignmentsRepository) > 0) {
foreach ($aActivityAssignmentsRepository as $objActivityAssignmentsRepository) {
$objActivityAssignmentsRepository->setActivityName($activityName);
//updating activity assignment info....
$objEntityManager->persist($objActivityAssignmentsRepository);
$objEntityManager->flush();
}
}
#activity define...
$aActivityDefineCrewsRepository = $this->activityDefineCrewsRepository
->findAll(array('activity_id' => $activityId));
if (CommonUtility::countArray($aActivityDefineCrewsRepository) > 0) {
foreach ($aActivityDefineCrewsRepository as $objActivityDefineCrewsRepository) {
$objActivityDefineCrewsRepository->setActivityName($activityName);
//updating activity define info....
$objEntityManager->persist($objActivityDefineCrewsRepository);
$objEntityManager->flush();
}
}
#activity define pair...
$aActivityDefineCrewPairsRepository = $this->activityDefineCrewPairsRepository
->findAll(array('activity_id' => $activityId));
if (CommonUtility::countArray($aActivityDefineCrewPairsRepository) > 0) {
foreach ($aActivityDefineCrewPairsRepository as $objActivityDefineCrewPairsRepository) {
$objActivityDefineCrewPairsRepository->setActivityName($activityName);
//updating activity define pair info....
$objEntityManager->persist($objActivityDefineCrewPairsRepository);
$objEntityManager->flush();
}
}
#account code activities...
$aAccountCodeActivitiesRepository = $this->accountCodeActivitiesRepository
->findAll(array('activity_id' => $activityId));
if (CommonUtility::countArray($aAccountCodeActivitiesRepository) > 0) {
foreach ($aAccountCodeActivitiesRepository as $objAccountCodeActivitiesRepository) {
$objAccountCodeActivitiesRepository->setActivityName($activityName);
//updating account code activities info....
$objEntityManager->persist($objAccountCodeActivitiesRepository);
$objEntityManager->flush();
}
}
$response = array(
"success" => array(
"message" => "Successfully Updated"
)
);
} elseif ($action === "delete") {
}
} else {
$response = array(
"error" => array(
"message" => "NOT FOUND",
"code" => 404
)
);
}
} catch (\Exception $e) {
$response = array(
"error" => array(
"message" => $e->getMessage(),
"code" => $e->getCode()
)
);
}
return $this->json($response);
}
/**
* @Route("/getSchoolOnlyActivityUsage", name="get_school_only_activity_usage", methods={"GET"})
* @param Request $request
* @return Response
*/
public function getSchoolOnlyActivityUsage(Request $request)
{
$conference_id = $request->query->get('conference_id');
$activity_id = $request->query->get('activity_id');
try {
$data = $this->activityDefineCrewsRepository->findByFilters(array(
'checkSOActivities' => true,
'conference_id' => $conference_id,
'activity_id' => $activity_id
));
if (CommonUtility::countArray($data) > 0) {
$response = $data;
} else {
$response = array(
"data" => null,
"meta" => null,
"links" => null,
"code" => 404,
"message" => "data not found",
"status" => "NOT_FOUND",
"result" => null
);
}
} catch (\Exception $e) {
$response = array(
"error" => array(
"message" => $e->getMessage(),
"code" => $e->getCode()
)
);
}
return $this->json($response);
}
/**
* @Route("/get-worker-info-by-event", name="get_worker_info_by_event", methods={"POST"})
* @param Request $request
* @return Response
*/
public function getWorkerInfoByEvent(Request $request)
{
$parameters = json_decode($request->getContent(), true);
$eventIds = (isset($parameters["eventIds"])) ? $parameters["eventIds"] : array();
$conference_id = (isset($parameters["conference_id"])) ? $parameters["conference_id"] : '';
if (count($eventIds) > 0 && isset($conference_id)) {
$workerInfos = $this->activityAssignmentsRepository->getWorkerInfoPerEvent(array(
'event_ids' => $eventIds,
'conference_id' => $conference_id
));
if (count($workerInfos) > 0) {
$response = array(
"success" => array(
"data" => $workerInfos,
"message" => "Worker Info Successfully Fetched!",
"code" => 200
)
);
} else {
$response = array(
"error" => array(
"event_ids" => $eventIds,
"message" => "No Event IDs found",
"code" => 404
)
);
}
} else {
$response = array(
"error" => array(
"message" => "DATA NOT FOUND",
"payload_required" => 'eventIds, conference_id',
"code" => 404
)
);
}
return $this->json($response);
}
}