src/Hona/ApiBundle/Controller/UserController.php line 73

Open in your IDE?
  1. <?php
  2. namespace Hona\ApiBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Hona\AdminBundle\Entity\User;
  8. use Hona\AdminBundle\Entity\UserHasAddress;
  9. use Hona\ApiBundle\Library\Utils;
  10. use Hona\ApiBundle\Exception\DuplicatedUniqueFieldsException;
  11. use Hona\ApiBundle\Exception\NotFoundEntityException;
  12. use Hona\ApiBundle\Exception\NotValidRelationshipException;
  13. class UserController extends EntityController
  14. {
  15.     protected $required = array("name""last_name");
  16.     protected $unique = array("email");
  17.     protected $relationship = array("address""control_gate""residential");
  18.     protected function _validateAfterGET(Request $request$entity$uuid, &$data$clean){
  19.         if(!empty($request->get("fields"))){
  20.             $fields explode(","$request->get("fields"));
  21.             foreach ($fields as $field) {
  22.                 if($field != "notification_setting"){
  23.                     $this->_addListEntities($field"user_has_".$field$data["id"], $data$clean);    
  24.                 }
  25.             }
  26.             
  27.             if (in_array("notification_setting"$fields)) {
  28.                 $this->_addEntity("notification_setting", array("user_id" => $data["id"]), $data);   
  29.             }
  30.         }
  31.         if(!empty($data["media_path"])){
  32.             $data["media_path"] = Utils::addMediaDomainToPath($data["media_path"]);
  33.         }
  34.         if(!empty($data["thumb_path"])){
  35.             $data["thumb_path"] = Utils::addMediaDomainToPath($data["thumb_path"]);
  36.         }
  37.     }
  38.     protected function _validateBeforePOST(Request $request$entity, &$data){
  39.         if(array_key_exists("password"$data)){
  40.             $data["password"] = hash($this->container->getParameter('encryption_hash'), $data["password"]);
  41.         }
  42.         if(!isset($data["status"])){
  43.             $data["status"] = Utils::ACTIVE;
  44.         }
  45.     }
  46.     protected function _validateAfterPOST(Request $request$entity$uuid, &$data$clean){
  47.         
  48.     }
  49.     protected function _validateBeforePUT(Request $request$entity$uuid, &$data){
  50.         if(array_key_exists("password"$data)){
  51.             $data["password"] = hash($this->container->getParameter('encryption_hash'), $data["password"]);
  52.         }
  53.     }
  54.     /**
  55.      * @Route("/user/{uuid}", name="user_get_uuid", methods={"GET"})
  56.      * @param $request
  57.      */
  58.     public function userGETAction(Request $request$uuid)
  59.     {
  60.         return $this->_entityGET($request"user"$uuid);
  61.     }
  62.     /**
  63.      * @Route("/user", name="user_post_uuid", methods={"POST"})
  64.      * @param $request
  65.      */
  66.     public function userPOSTAction(Request $request)
  67.     {
  68.         return $this->_entityPOST($request"user");
  69.     }
  70.     /**
  71.      * @Route("/user/family/member/create", name="family_post_uuid", methods={"POST"})
  72.      * @param $request
  73.      */
  74.     public function userFamiliyPOSTAction(Request $request)
  75.     {
  76.         try{
  77.             $entityManager $this->getDoctrine()->getManager();
  78.             $parentUser $this->getUser();
  79.             $dataRequest json_decode($request->getContent(), true);
  80.             
  81.             $addressUuid $dataRequest["addressUuid"];
  82.             $address $entityManager->getRepository(Utils::getClassName("Address"))->findOneByUuid($addressUuid);
  83.             $user null;
  84.             if(isset($dataRequest["email"]) && isset($dataRequest["phone"])){
  85.                 $user $entityManager->getRepository(Utils::getClassName("User"))->findOneByEmailOrPhone($dataRequest["email"], $dataRequest["phone"]);
  86.             }elseif(isset($dataRequest["email"])){
  87.                 $user $entityManager->getRepository(Utils::getClassName("User"))->findOneByEmail($dataRequest["email"]);
  88.             }
  89.             elseif(isset($dataRequest["phone"])){
  90.                 $user $entityManager->getRepository(Utils::getClassName("User"))->findOneByPhone($dataRequest["phone"]);
  91.             }
  92.             if($user == null){
  93.                 $dataUser = array();
  94.                 $dataUser["name"] = $dataRequest["name"];
  95.                 $dataUser["last_name"] = $dataRequest["lastName"];
  96.                 $dataUser["status"] = Utils::NEEDCONFIRMATION;
  97.                 $dataUser["email_verification"] = false;
  98.                 $dataUser["phone_verification"] = false;
  99.                 $sendMail false;
  100.                 if(isset($dataRequest["email"]) && !empty($dataRequest["email"])){
  101.                     $dataUser["email"] = $dataRequest["email"];
  102.                     $sendMail true;
  103.                 }
  104.                 if(isset($dataRequest["phone"]) && !empty($dataRequest["phone"])){
  105.                     $dataUser["phone"] = $dataRequest["phone"];
  106.                 }
  107.                 if(isset($dataRequest["profileImage"])){
  108.                     $imagePath Utils::base64ToImage($dataRequest["profileImage"], strstr($email'@'true), "user");
  109.                     $dataUser["media_path"] = $imagePath;
  110.                     $dataUser["thumb_path"] = $imagePath;
  111.                 }
  112.                 $familiyUser $this->_entityPOST($request"user"$dataUser);
  113.                 
  114.                 if($sendMail){
  115.                     $user $entityManager->getRepository(Utils::getClassName("User"))->findOneByEmail($dataRequest["email"]);
  116.                     $response Utils::sendConfirmationEmail($this->get('twig'), $dataUser);
  117.                 }else{
  118.                     $user $entityManager->getRepository(Utils::getClassName("User"))->findOneByPhone($dataRequest["phone"]);
  119.                 }
  120.                 
  121.             }else{
  122.                 $dataUser = array();
  123.                 $dataUser["name"] = $dataRequest["name"];
  124.                 $dataUser["last_name"] = $dataRequest["lastName"];
  125.                 if(isset($dataRequest["email"]) && !empty($dataRequest["email"])){
  126.                     $dataUser["email"] = $dataRequest["email"];
  127.                     
  128.                 }
  129.                 if(isset($dataRequest["phone"]) && !empty($dataRequest["phone"])){
  130.                     $dataUser["phone"] = $dataRequest["phone"];
  131.                 }
  132.     
  133.                 $familiyUser $this->_entityPUT($request"user"$user->getUuid(), $dataUser);
  134.             }
  135.             
  136.             $userAddress $entityManager->getRepository(Utils::getClassName("User_Has_Address"))->findOneByPrimaryKey(
  137.                 array(
  138.                     "userId" => $user->getId(),
  139.                     "addressId" => $address->getId()
  140.                 )
  141.             );
  142.             /*$parentUserAddress = $entityManager->getRepository(Utils::getClassName("User_Has_Address"))->findOneByPrimaryKey(
  143.                 array(
  144.                     "userId" => $parentUser->getId(),
  145.                     "addressId" => $address->getId()
  146.                 )
  147.             );*/
  148.             $permissionJson Utils::getPermissionJson($dataRequest);
  149.             $color $dataRequest["color"];
  150.             if($userAddress != null){
  151.                 if($userAddress->getStatus() == Utils::DELETED){
  152.                     $dataAddress = array();
  153.                     $dataAddress['parent_id'] = $parentUser->getId();
  154.                     $dataAddress['status'] = Utils::ACTIVE;
  155.                     $dataAddress['date_modified'] = new \DateTime();
  156.                     $dataAddress['permission'] = json_encode($permissionJson);
  157.                     $dataAddress['color'] = $color;
  158.                     /*if(isset($parentUserAddress)){
  159.                         $dataAddress['rent'] = $parentUserAddress->getRent();
  160.                     }else{
  161.                         $dataAddress['rent'] = -1;
  162.                     }*/
  163.                     $familiyAddress $this->forward('HonaApiBundle:UserHasAddress:userHasAddressPUT', [
  164.                         'request' => $request,
  165.                         'user_uuid' => $user->getUuid(),
  166.                         'address_uuid' => $address->getUuid(),
  167.                         'data' => $dataAddress
  168.                     ]);
  169.                     /*if($user->getStatus() == Utils::NEEDCONFIRMATION){
  170.                         $dataUser = array();
  171.                         $dataUser["name"] = $dataRequest["name"];
  172.                         $dataUser["last_name"] = $dataRequest["lastName"];
  173.                         $dataUser["email"] = $email;
  174.                         $response = Utils::sendConfirmationEmail($this->get('twig'), $dataUser);
  175.                     }*/
  176.                 }else{
  177.                     throw new DuplicatedUniqueFieldsException(array("user_id","address_id"));
  178.                 }
  179.             }else{
  180.                 $dataAddress = array();
  181.                 $dataAddress['user_id'] = $user->getId();
  182.                 $dataAddress['address_id'] = $address->getId();
  183.                 $dataAddress['parent_id'] = $parentUser->getId();
  184.                 $dataAddress['status'] = Utils::ACTIVE;
  185.                 $dataAddress['date_created'] = new \DateTime();
  186.                 $dataAddress['date_modified'] = new \DateTime();
  187.                 $dataAddress['permission'] = json_encode($permissionJson);
  188.                 $dataAddress['color'] = $color;
  189.                 $familiyAddress $this->forward('HonaApiBundle:UserHasAddress:userHasAddressPOST', [
  190.                     'request' => $request,
  191.                     'user_uuid' => $user->getUuid(),
  192.                     'address_uuid' => $address->getUuid(),
  193.                     'data' => $dataAddress
  194.                 ]);
  195.             }
  196.             $request->attributes->set('fields''address');
  197.             $userFamiliy $this->_entityGET($request"user"$user->getUuid());
  198.             return $userFamiliy;
  199.         }catch(ErrorCodeException $exception){
  200.             $data = [
  201.                 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()),
  202.                 'code' => $exception->getErrorCode()
  203.             ];
  204.             return new JsonResponse($data$exception->getStatusCode());
  205.         }catch(DuplicatedUniqueFieldsException $exception){
  206.             $data = [
  207.                 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()),
  208.                 'code' => $exception->getErrorCode()
  209.             ];
  210.             return new JsonResponse($data$exception->getStatusCode());
  211.         }catch(HttpException $exception){
  212.             $data = [
  213.                 'message' => $exception->getMessage()
  214.             ];
  215.             return new JsonResponse($data$exception->getStatusCode());
  216.         }
  217.     }
  218.      /**
  219.      * @Route("/user/family/member/{uuid}/address/{addressUuid}", name="family_put_uuid", methods={"PUT"})
  220.      * @param $request
  221.      */
  222.     public function userFamiliyPUTAction(Request $request$uuid$addressUuid)
  223.     {
  224.         try{
  225.             $entityManager $this->getDoctrine()->getManager();
  226.             $user $entityManager->getRepository(Utils::getClassName("User"))->findOneByUuid($uuid);
  227.             if($user != null){
  228.                 $dataRequest json_decode($request->getContent(), true);
  229.                 $dataUser = array();
  230.                 $update false;
  231.                 if(isset($dataRequest["name"])){
  232.                     $dataUser["name"] = $dataRequest["name"];
  233.                     $update true;
  234.                 }
  235.                 
  236.                 if(isset($dataRequest["lastName"])){
  237.                     $dataUser["last_name"] = $dataRequest["lastName"];
  238.                     $update true;
  239.                 }
  240.                 if(isset($dataRequest["phone"])){
  241.                     $dataUser["phone"] = $dataRequest["phone"];
  242.                     $update true;
  243.                 }
  244.                 if(isset($dataRequest["email"]) && !empty($dataRequest["email"])){
  245.                     if($user->getEmail() != $dataRequest["email"] && $user->getEmailVerification() == 0){
  246.                         $dataUser["email"] = $dataRequest["email"];
  247.                         $update true;
  248.                         $response Utils::sendConfirmationEmail($this->get('twig'), $dataUser);
  249.                     }
  250.                     
  251.                 }
  252.                 if(isset($dataRequest["profileImage"])){
  253.                     $imagePath Utils::base64ToImage($dataRequest["profileImage"], strstr($user->getEmail(), '@'true), "user");
  254.                     $dataUser["media_path"] = $imagePath;
  255.                     $dataUser["thumb_path"] = $imagePath;
  256.                     $update true;
  257.                 }
  258.                 if($update)
  259.                     $familiyUser $this->_entityPUT($request"user"$uuid$dataUser);
  260.                 
  261.                 $address $entityManager->getRepository(Utils::getClassName("Address"))->findOneByUuid($addressUuid);
  262.                 if($address == null)
  263.                     throw new NotFoundEntityException($addressUuid);
  264.                 $userAddress $entityManager->getRepository(Utils::getClassName("User_Has_Address"))->findOneByPrimaryKey(
  265.                     array(
  266.                         "userId" => $user->getId(),
  267.                         "addressId" => $address->getId()
  268.                     )
  269.                 );
  270.     
  271.                 if($userAddress != null){
  272.                     $updateAddress false;
  273.                     $permission Utils::getPermissionJson($dataRequest);
  274.                     $dataAddress = array();
  275.                     if(!empty($permission)){
  276.                         $dataAddress['permission'] = json_encode($permission);
  277.                         $updateAddress true;
  278.                     }
  279.                     if(isset($dataRequest["color"])){
  280.                         $dataAddress['color'] = $dataRequest["color"];
  281.                         $updateAddress true;
  282.                     }
  283.                     if($updateAddress){
  284.                         $dataAddress['date_modified'] = new \DateTime();
  285.                         $familiyAddress $this->forward('HonaApiBundle:UserHasAddress:userHasAddressPUT', [
  286.                             'request' => $request,
  287.                             'user_uuid' => $user->getUuid(),
  288.                             'address_uuid' => $address->getUuid(),
  289.                             'data' => $dataAddress
  290.                         ]);
  291.                     }
  292.                 }else{
  293.                     throw new NotValidRelationshipException("address_id");
  294.                 }
  295.     
  296.                 $request->attributes->set('fields''address');
  297.                 $userFamiliy $this->forward('HonaApiBundle:User:userGET', [
  298.                     'request' => $request,
  299.                     'uuid' => $user->getUuid()
  300.                 ]);
  301.     
  302.                 return $userFamiliy;
  303.             }else{
  304.                 throw new NotFoundEntityException($uuid);
  305.             }
  306.             
  307.         }catch(ErrorCodeException $exception){
  308.             $data = [
  309.                 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()),
  310.                 'code' => $exception->getErrorCode()
  311.             ];
  312.             return new JsonResponse($data$exception->getStatusCode());
  313.         }catch(NotFoundEntityException $exception){
  314.             $data = [
  315.                 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()),
  316.                 'code' => $exception->getErrorCode()
  317.             ];
  318.             return new JsonResponse($data$exception->getStatusCode());
  319.         }catch(NotValidRelationshipException $exception){
  320.             $data = [
  321.                 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()),
  322.                 'code' => $exception->getErrorCode()
  323.             ];
  324.             return new JsonResponse($data$exception->getStatusCode());
  325.         }catch(HttpException $exception){
  326.             $data = [
  327.                 'message' => $exception->getMessage()
  328.             ];
  329.             return new JsonResponse($data$exception->getStatusCode());
  330.         }
  331.     }
  332.     /**
  333.      * @Route("/user/{uuid}", name="user_put_uuid", methods={"PUT"})
  334.      * @param $request
  335.      */
  336.     public function userPUTAction(Request $request$uuid$data=null)
  337.     {
  338.         return $this->_entityPUT($request"user"$uuid$data);
  339.     }
  340.     /**
  341.      * @Route("/user/{uuid}", name="user_delete_uuid", methods={"DELETE"})
  342.      * @param $request
  343.      */
  344.     public function userDELETEAction(Request $request$uuid)
  345.     {
  346.         return $this->_entityDELETE($request"user"$uuid);
  347.     }
  348. }