<?php
namespace Hona\ApiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Hona\ApiBundle\Library\Utils;
class AddressController extends EntityController
{
protected $required = array("street", "residential");
protected function _validateAfterGET(Request $request, $entity, $uuid, &$data, $clean){
if($entity == "access_history" || $entity == "invitation"){
$this->_changeIdForEntity("user", $data, $clean);
if(!empty($data["image_code_path"])){
$data["image_code_path"] = Utils::addMediaDomainToPath($data["image_code_path"]);
}
}else if($entity == "event"){
$this->_changeIdForEntity("user", $data, $clean);
}else if($entity == "address"){
$this->_changeIdForEntity("residential", $data, $clean);
}
}
protected function _validateAfterPUT(Request $request, $entity, $uuid, &$data, $clean){
$this->_changeIdForEntity("residential", $data, $clean);
}
protected function _validateAfterPOST(Request $request, $entity, $uuid, &$data, $clean){
$this->_changeIdForEntity("residential", $data, $clean);
}
protected function _validateBeforePOST(Request $request, $entity, &$data){
$this->_changeUuidForId("residential", $data);
if(!isset($data["status"])){
$data["status"] = Utils::ACTIVE;
}
}
protected function _validateBeforePUT(Request $request, $entity, $uuid, &$data){
$this->_changeUuidForId("residential", $data);
}
/**
* @Route("/address/{uuid}/news/{size}/{page}", name="address_get_uuid_news", methods={"GET"})
* @param $request
*/
public function addressNewsGETAction(Request $request, $uuid, $size = 10, $page = 0)
{
$user = $this->getUser();
$userHasAddress = Utils::checkUserAddressExists($this->getDoctrine()->getManager(), $uuid, $user);
if($userHasAddress){
$address = $this->_entityGET($request, "address", $uuid, null, true);
if(is_array($address)){
return $this->forward('HonaApiBundle:Residential:residentialNewsGET', [
'request' => $request,
'uuid' => $address["residential"]["uuid"],
'type' => 1,
'size' => $size,
'page' => $page
]);
}
return $address;
}else{
$data = [
'message' => "user with no address",
'code' => 20
];
return new JsonResponse($data, 400);
}
}
/**
* @Route("/address/{uuid}/user/{user_uuid}/history/{size}/{page}", name="address_get_uuid_user_uuid_history", methods={"GET"})
* @param $request
*/
public function addressUserHistoryGETAction(Request $request, $uuid, $user_uuid, $size = 10, $page = 0)
{
$user = $this->getUser();
$userHasAddress = Utils::checkUserAddressExists($this->getDoctrine()->getManager(), $uuid, $user);
if($userHasAddress){
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
$user = $this->_entityGET($request, "user", $user_uuid, null, true, false);
if(is_array($user)){
return $this->_listEntitiesByFunction($request, "access_history", "address_id_user_id_and_status", array(
"address_id" => $address["id"],
"user_id" => $user["id"],
"status" => Utils::ACTIVE
), array("date_entry" => "DESC"), $size, $page);
}else{
return $address;
}
}else{
return $address;
}
}else{
$data = [
'message' => "user with no address",
'code' => 20
];
return new JsonResponse($data, 400);
}
}
/**
* @Route("/address/{uuid}/user/{user_uuid}/invitation/{size}/{page}", name="address_get_uuid_user_uuid_invitation", methods={"GET"})
* @param $request
*/
public function addressUserInvitationGETAction(Request $request, $uuid, $user_uuid, $size = 10, $page = 0)
{
$user = $this->getUser();
$userHasAddress = Utils::checkUserAddressExists($this->getDoctrine()->getManager(), $uuid, $user);
if($userHasAddress){
$order = $request->query->get('order');
if(isset($order)){
$order = strtoupper($order);
}else{
$order = "ASC";
}
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
$user = $this->_entityGET($request, "user", $user_uuid, null, true, false);
if(is_array($user)){
return $this->_listEntitiesByFunction($request, "invitation", "address_id_user_id_and_status", array(
"address_id" => $address["id"],
"user_id" => $user["id"],
"status" => Utils::ACTIVE
), array("date_invitation" => $order), $size, $page);
}else{
return $user;
}
}else{
return $address;
}
}else{
$data = [
'message' => "user with no address",
'code' => 20
];
return new JsonResponse($data, 400);
}
}
/**
* @Route("/address/{uuid}/user/{user_uuid}/event/history/{size}/{page}", name="address_get_uuid_user_uuid_event_history", methods={"GET"})
* @param $request
*/
public function addressUserEventHistoryGETAction(Request $request, $uuid, $user_uuid, $size = 10, $page = 0)
{
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
$user = $this->_entityGET($request, "user", $user_uuid, null, true, false);
if(is_array($user)){
return $this->_listEntitiesByFunction($request, "event", "history_address_id_user_id_and_status", array(
"address_id" => $address["id"],
"user_id" => $user["id"],
"status" => Utils::ACTIVE
), array("date_event" => "DESC"), $size, $page);
}else{
return $address;
}
}else{
return $address;
}
}
/**
* @Route("/address/{uuid}/user/{user_uuid}/event/{size}/{page}", name="address_get_uuid_user_uuid_event", methods={"GET"})
* @param $request
*/
public function addressUserEventGETAction(Request $request, $uuid, $user_uuid, $size = 10, $page = 0)
{
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
$user = $this->_entityGET($request, "user", $user_uuid, null, true, false);
if(is_array($user)){
return $this->_listEntitiesByFunction($request, "event", "address_id_user_id_and_status", array(
"address_id" => $address["id"],
"user_id" => $user["id"],
"status" => Utils::ACTIVE
), array("date_event" => "ASC"), $size, $page);
}else{
return $user;
}
}else{
return $address;
}
}
/**
* @Route("/address/{uuid}/user/{size}/{page}", name="address_get_uuid_user", methods={"GET"})
* @param $request
*/
public function addressUserGETAction(Request $request, $uuid, $size = 10, $page = 0)
{
$user = $this->getUser();
$userHasAddress = Utils::checkUserAddressExists($this->getDoctrine()->getManager(), $uuid, $user);
if($userHasAddress){
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
return $this->_listEntitiesByFunction($request, "user", "address_id_and_status", array(
"address_id" => $address["id"],
"status" => Utils::ACTIVE
), array(), $size, $page);
}else{
return $address;
}
}else{
$data = [
'message' => "user with no address",
'code' => 20
];
return new JsonResponse($data, 400);
}
}
/**
* @Route("/address/{uuid}/history/{size}/{page}", name="address_get_uuid_history", methods={"GET"})
* @param $request
*/
public function addressHistoryGETAction(Request $request, $uuid, $size = 10, $page = 0)
{
$user = $this->getUser();
$userHasAddress = Utils::checkUserAddressExists($this->getDoctrine()->getManager(), $uuid, $user);
if($userHasAddress){
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
return $this->_listEntitiesByFunction($request, "access_history", "address_id_and_status", array(
"address_id" => $address["id"],
"status" => Utils::ACTIVE
), array("date_entry" => "DESC"), $size, $page);
}else{
return $address;
}
}else{
$data = [
'message' => "user with no address",
'code' => 20
];
return new JsonResponse($data, 400);
}
}
/**
* @Route("/address/{uuid}/invitation/{size}/{page}", name="address_get_uuid_invitation", methods={"GET"})
* @param $request
*/
public function addressInvitationGETAction(Request $request, $uuid, $size = 10, $page = 0)
{
$user = $this->getUser();
$userHasAddress = Utils::checkUserAddressExists($this->getDoctrine()->getManager(), $uuid, $user);
if($userHasAddress){
$order = $request->query->get('order');
if(isset($order)){
$order = strtoupper($order);
}else{
$order = "ASC";
}
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
return $this->_listEntitiesByFunction($request, "invitation", "address_id_and_status", array(
"address_id" => $address["id"],
"status" => Utils::ACTIVE
), array("date_invitation" => $order), $size, $page);
}else{
return $address;
}
}else{
$data = [
'message' => "user with no address",
'code' => 20
];
return new JsonResponse($data, 400);
}
}
/**
* @Route("/address/{uuid}/event/history/{size}/{page}", name="address_get_uuid_historic_event", methods={"GET"})
* @param $request
*/
public function addressEventHistoryGETAction(Request $request, $uuid, $size = 10, $page = 0)
{
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
return $this->_listEntitiesByFunction($request, "event", "history_address_id_and_status", array(
"address_id" => $address["id"],
"status" => Utils::ACTIVE
), array("date_event" => "ASC"), $size, $page);
}else{
return $address;
}
}
/**
* @Route("/address/{uuid}/event/{size}/{page}", name="address_get_uuid_event", methods={"GET"})
* @param $request
*/
public function addressEventGETAction(Request $request, $uuid, $size = 10, $page = 0)
{
$address = $this->_entityGET($request, "address", $uuid, null, true, false);
if(is_array($address)){
return $this->_listEntitiesByFunction($request, "event", "address_id_and_status", array(
"address_id" => $address["id"],
"status" => Utils::ACTIVE
), array("date_event" => "ASC"), $size, $page);
}else{
return $address;
}
}
/**
* @Route("/address/{uuid}", name="address_get_uuid", methods={"GET"})
* @param $request
*/
public function addressGETAction(Request $request, $uuid)
{
return $this->_entityGET($request, "address", $uuid);
}
/**
* @Route("/address", name="address_post_uuid", methods={"POST"})
* @param $request
*/
public function addressPOSTAction(Request $request)
{
return $this->_entityPOST($request, "address");
}
/**
* @Route("/address/{uuid}", name="address_put_uuid", methods={"PUT"})
* @param $request
*/
public function addressPUTAction(Request $request, $uuid)
{
return $this->_entityPUT($request, "address", $uuid);
}
/**
* @Route("/address/{uuid}", name="address_delete_uuid", methods={"DELETE"})
* @param $request
*/
public function addressDELETEAction(Request $request, $uuid)
{
return $this->_entityDELETE($request, "address", $uuid);
}
}