src/OceanExpertBundle/Controller/InstitutionController.php line 317

Open in your IDE?
  1. <?php
  2. namespace OceanExpertBundle\Controller;
  3. use CommerceGuys\Addressing\Formatter\DefaultFormatter;
  4. use CommerceGuys\Addressing\Model\Address;
  5. use CommerceGuys\Addressing\Repository\AddressFormatRepository;
  6. use CommerceGuys\Addressing\Repository\CountryRepository;
  7. use CommerceGuys\Addressing\Repository\SubdivisionRepository;
  8. use DateTime;
  9. use Doctrine\ORM\AbstractQuery;
  10. use Doctrine\ORM\Query;
  11. use OceanExpertBundle\Controller\Api\ApiReportsController;
  12. use OceanExpertBundle\Entity\Institutions;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class InstitutionController extends AbstractController
  18. {
  19.     public function getInstitutionByCountryAction(Request $request): JsonResponse
  20.     {
  21.         $institutionList = array();
  22.         if ($request->query->get('countryCode') == NULL) {
  23.             $institutionList = array(
  24.                 'institution' => 'Select Country First.'
  25.             );
  26.         } else {
  27.             $countryCode $request->query->get('countryCode');
  28.             $keyword $request->query->get('keyword');
  29.             $repo $this->getDoctrine()
  30.                 ->getRepository('OceanExpertBundle:Institutions');
  31.             $query $repo->createQueryBuilder('a')
  32.                 ->select('distinct a.instName, a.idInst, a.instAddress, a.city, a.state, a.postcode, a.idInst, a.activated')
  33.                 // ->where('a.instName LIKE :title')
  34.                 ->where('a.countryCode = :code')
  35.                 ->andWhere('a.activated in (0,1)')
  36.                 ->orderBy('a.instName''ASC')
  37.                 // ->setParameter('title', '%'.$keyword.'%')
  38.                 ->setParameter('code'$countryCode)
  39.                 ->getQuery();
  40.             $institutionList $query->getResult();
  41.             // $institutionList = array_column($institutionList, 'instName');
  42.         }
  43.         return new JsonResponse($institutionList);
  44.     }
  45.     /**
  46.      * get the informationa about a given institution
  47.      *
  48.      * @param Request $request
  49.      *
  50.      * @return JsonResponse
  51.      */
  52.     public function getInstitutionAddressAction(Request $request): Response
  53.     {
  54.         $institutionDetails = array();
  55.         if ($request->query->get('instName') != NULL) {
  56.             $title $request->query->get('instName');
  57.             $repo $this->getDoctrine()
  58.                 ->getRepository('OceanExpertBundle:Institutions');
  59.             $query $repo->createQueryBuilder('a')
  60.                 ->select('a.instName, a.instAddress, a.city, a.state, a.postcode, a.instTel,a.addr2')
  61.                 ->where('a.idInst = :idInst')
  62.                 // ->setParameter('title',$title)
  63.                 ->setParameter('idInst'$title)
  64.                 ->getQuery();
  65.             $institutionDetails $query->getResult();
  66.             return new JsonResponse($institutionDetails);
  67.         } else {
  68.             return new JsonResponse(
  69.                 array(
  70.                     'status' => 0,
  71.                     'message' => 'need a name of the institution'
  72.                 )
  73.             );
  74.         }
  75.     }
  76.     public function addInstitutionAction(Request $request): Response
  77.     {
  78.         $data $request->request->all();
  79.         $security_context $this->get('security.authorization_checker');
  80.         if ($security_context->isGranted('IS_AUTHENTICATED_FULLY')) {
  81.             $em $this->getDoctrine()->getManager();
  82.             $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  83.             //let's check if the logged-in user has a 'real' profile
  84.             //meaning there is at least a firstname, lastname and language
  85.             // Arno 21/06/23 : we will not check this for the time being see alse #521
  86.             //if people cannot create their institute while registering they will not make / add it later
  87.             /*
  88.             if (!SecurityController::checkUserProfile($em, $userId)) {
  89.                 return $this->redirect(
  90.                     $this->generateUrl(
  91.                         'user_profile_edit'
  92.                     )
  93.                 );
  94.             }
  95.             */
  96.             if (trim($request->request->get("iedmo")) == ''
  97.                 || !is_numeric($request->request->get("iedmo"))
  98.             ) {
  99.                 $edmo_code 0;
  100.             } else {
  101.                 $edmo_code $request->request->get("iedmo");
  102.             }
  103.             $institutionDetails = array();
  104.             $repo $this->getDoctrine()
  105.                 ->getRepository('OceanExpertBundle:Institutions');
  106.             $query $repo->createQueryBuilder('a')
  107.                 ->select('a.instName, a.instAddress, a.city, a.state, a.postcode, a.instTel')
  108.                 ->Where('a.instName like :name')
  109.                 ->setParameter('name'$request->request->get("instName"))
  110.                 ->getQuery();
  111.             $institutionDetails $query->getResult();
  112.             if (count($institutionDetails) == 0) {
  113.                 try {
  114.                     $institution = new Institutions();
  115.                     $institution->setCountryCode($request->request->get('countryCode'));
  116.                     $institution->setInstName($request->request->get('instName'));
  117.                     $institution->setInstNameEng($request->request->get('instNameEng'));
  118.                     $institution->setInstTypeID($request->request->get('instType'));
  119.                     $institution->setParentId($request->request->get('parentinstitute'));
  120.                     if (null == $institution->getParentId()) {
  121.                         $institution->setParentId(0);
  122.                     }
  123.                     $institution->setInstAddress($request->request->get('iaddress'));
  124.                     $institution->setAcronym($request->request->get('iacronym'));
  125.                     $institution->setAddr2($request->request->get('iaddress2'));
  126.                     $institution->setPostcode($request->request->get('ipostcode'));
  127.                     $institution->setCity($request->request->get('icity'));
  128.                     $institution->setState($request->request->get('istate'));
  129.                     $institution->setInstTel($request->request->get('icontact'));
  130.                     $institution->setInstFax($request->request->get('ifax'));
  131.                     $institution->setInstEmail($request->request->get('iemail'));
  132.                     $institution->setInstURL($request->request->get('iwebsite'));
  133.                     $institution->setEdmoCode($edmo_code);
  134.                     $institution->setActivities($request->request->get('iactivity'));
  135.                     if (null !== $request->request->get('isearegion')
  136.                         && is_array($request->request->get('isearegion'))
  137.                         && count($request->request->get('isearegion')) > 1
  138.                     ) {
  139.                         $iseaRegion implode(','$request->request->get('isearegion'));
  140.                     } else {
  141.                         $iseaRegion '';
  142.                     }
  143.                     $institution->setInstRegion($iseaRegion);
  144.                     $institution->setPopularity(0);
  145.                     if ($this->get('security.authorization_checker')->isGranted('ROLE_LME')) {
  146.                         $institution->setActivated(1);
  147.                     } else {
  148.                         $institution->setActivated(0);
  149.                     }
  150.                     $institution->setFDateEntered(new DateTime('now'));
  151.                     $institution->setLDateUpdated(new DateTime('now'));
  152.                     $institution->setCreatedBy($userId);
  153.                     $em $this->getDoctrine()->getManager();
  154.                     $em->persist($institution); //marks object to be saved in the next transaction.
  155.                     $em->flush(); //performs all saves and transactions.
  156.                     if (null != $request->files->get('instLogo')) {
  157.                         $file $request->files->get('instLogo');
  158.                         $filetype $file->guessClientExtension();
  159.                         $filename 'instituteLogo.jpg';
  160.                         $directory 'uploads/institutes/' $institution->getIdInst() . '/';
  161.                         $uploadfile $file->move($directory$filename);
  162.                         $institution->setInstLogo($filename);
  163.                     } else {
  164.                         if (isset($data['textLogo']) && $data['textLogo'] == '') {
  165.                             $institution->setInstLogo('');
  166.                             $filename 'uploads/institutes/' $institution->getIdInst() . '/instituteLogo.jpg';
  167.                             if (file_exists($filename)) {
  168.                                 unlink($filename);
  169.                             }
  170.                         }
  171.                     }
  172.                     $em->persist($institution);
  173.                     $em->flush();
  174.                     return new JsonResponse(
  175.                         array(
  176.                             'status' => 1,
  177.                             'id' => $institution->getIdInst(),
  178.                             'message' => '<br /><br /><h3 class="text-success">institution created</h3>
  179.                                 Your new institution has been created successfully.<br /> 
  180.                                 Please select your newly created institution in the list.
  181.                                 <br /><br />'
  182.                         )
  183.                     );
  184.                 } catch (Exception $e) {
  185.                     return new JsonResponse(
  186.                         array(
  187.                             'status' => 0,
  188.                             'message' => 'Error in adding Institution. Please try again later.'
  189.                         )
  190.                     );
  191.                 }
  192.             } else {
  193.                 return new JsonResponse(
  194.                     array(
  195.                         'status' => 2,
  196.                         'message' => 'Institute name/acronym already exist.'
  197.                     )
  198.                 );
  199.             }
  200.         } else {
  201.             return new JsonResponse(
  202.                 array(
  203.                     'status' => 2,
  204.                     'message' => 'you do not have the correct rights to do this'
  205.                 )
  206.             );
  207.         }
  208.     }
  209.     /**
  210.      * show all existing institutes
  211.      *
  212.      * @param Request $request
  213.      *
  214.      * @return Response|void
  215.      */
  216.     function viewInstitutesActionRequest $request ): Response
  217.     {
  218.         $em $this->get('doctrine')->getManager();
  219.         $connection $em->getConnection();
  220.         $limits = array(
  221.             10 => 10,
  222.             25 => 25,
  223.             50 => 50,
  224.             100 => 100,
  225.             500 => 500
  226.         );
  227.         $limit $request->query->get('limit'10);
  228.         $query $request->query->get('search''');
  229.         $orderby $request->query->get('orderby''order');
  230.         $dir $request->query->get('dir''asc');
  231.         if (!in_array($orderby, array('id''title'))) {
  232.             $orderby 'title';
  233.         }
  234.         if (!in_array($dir, array('asc''desc'))) {
  235.             $dir 'asc';
  236.         }
  237.         //get the activated institutes
  238.         $repo $this->getDoctrine()
  239.             ->getRepository('OceanExpertBundle:Institutions');
  240.         $institutes $repo->createQueryBuilder('i');
  241.         $institutes->select('
  242.             i.idInst, 
  243.             i.parentId,
  244.             i.instTypeId,
  245.             i.instName,
  246.             i.instNameEng,
  247.             i.countryCode,
  248.             c.country,
  249.             it.insttypeName'
  250.         );
  251.         $institutes->leftJoin(
  252.             'OceanExpertBundle:Countries',
  253.             'c',
  254.             'WITH',
  255.             'i.countryCode = c.idCountry');
  256.         $institutes->leftJoin(
  257.             'OceanExpertBundle:Insttypes',
  258.             'it',
  259.             'WITH',
  260.             'i.instTypeId = it.idInsttype');
  261.         if (!$this->get('security.authorization_checker')->isGranted('ROLE_SUPERADMIN')
  262.             && !$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')
  263.         ) {
  264.             $institutes->where('i.activated = 1');
  265.         }
  266.         //store all the found institutes to be able to find the name of the parents
  267.         $institutesQuery $institutes->getQuery();
  268.         $institutesTmp $institutesQuery->execute();
  269.         foreach($institutesTmp as $institute) {
  270.             $parentInstitutes[$institute['idInst']] = $institute['instName'];
  271.         }
  272.         if (trim($query) != '') {
  273.             $institutes->andwhere('i.instName LIKE :query OR i.instNameEng LIKE :query');
  274.             $institutes->setParameter(
  275.                 'query',
  276.                 '%' trim($query) . '%'
  277.             );
  278.         }
  279.         $institutes->getQuery();
  280.         $paginator $this->get('knp_paginator');
  281.         $data $paginator->paginate(
  282.             $institutes,
  283.             $request->query->getInt('page'1),
  284.             $limit,
  285.             array(
  286.                 'pageParameterName' => 'page',
  287.                 'sortDirectionParameterName' => 'dir',
  288.                 'defaultSortFieldName' => 'i.idInst',
  289.                 'defaultSortDirection' => 'desc'
  290.             )
  291.         );
  292.         $data->setCustomParameters([
  293.             'limits' => $limits,
  294.             'title' => 'Institutes'
  295.         ]);
  296.         return $this->render(
  297.             'Institution/viewInstitutes.html.twig',
  298.             array(
  299.                 'institutesData' => $data,
  300.                 'parentInstitutes' => $parentInstitutes
  301.             )
  302.         );
  303.     }
  304.     public function viewInstitutionAction($instIdRequest $request)
  305.     {
  306.         /*
  307.          * container to hold to OIH/ODIS JSON-LD info
  308.          * see also https://book.oceaninfohub.org/thematics/expinst/README.html
  309.          * in the end we will need something like
  310.          * {
  311.                 "@context": {
  312.                     "@vocab": "https://schema.org/"
  313.                 },
  314.                 "@id": "https://example.org/id/org/x",
  315.                 "@type": "Organization",
  316.                 "address": {
  317.                     "@type": "PostalAddress",
  318.                     "addressLocality": "Paris, France",
  319.                     "postalCode": "F-75002",
  320.                     "streetAddress": "38 avenue de l'Opera"
  321.                 },
  322.                 "email": "secretariat(at)example.org",
  323.                 "name": "Organization X",
  324.                 "description": "Description of org ...",
  325.                 "telephone": "( 33 1) 42 68 53 00",
  326.                 "member": [
  327.                     {
  328.                         "@type": "Organization",
  329.                         "name": "Organization A",
  330.                         "description": "Org A is a potential parent organization of Org X"
  331.                     }
  332.                 ],
  333.                 "identifier": {
  334.                     "@id": "https://grid.ac/institutes/grid.475727.4",
  335.                     "@type": "PropertyValue",
  336.                     "description": "UN Department of Economic and Social Affairs Sustainable Development",
  337.                     "propertyID": "https://registry.identifiers.org/registry/grid",
  338.                     "url": "https://grid.ac/institutes/grid.475727.4"
  339.                 }
  340.             }
  341.          */
  342.         $OIHData = array(
  343.             '@context' => array(
  344.                 '@vocab' => 'https://schema.org/'
  345.             ),
  346.             '@id' => "https://oceanexpert.org/institution/$instId",
  347.             '@type' => 'Organization'
  348.         );
  349.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  350.         $qb->add(
  351.             'select'
  352.             'ins.idInst,
  353.             ins.instTypeId, 
  354.             ins.instName, 
  355.             ins.instNameEng, 
  356.             ins.instAddress, 
  357.             ins.addr2, 
  358.             ins.city, 
  359.             ins.state, 
  360.             ins.postcode, 
  361.             ins.countryCode, 
  362.             ins.acronym, 
  363.             ins.instLogo, 
  364.             ins.instTel, 
  365.             ins.instFax, 
  366.             ins.instEmail, 
  367.             ins.instUrl, 
  368.             ins.edmoCode,
  369.             ins.instRegion, 
  370.             ins.activities, 
  371.             ins.popularity, 
  372.             ins.activated, 
  373.             ins.fDateEntered, 
  374.             ins.lDateUpdated, 
  375.             ins.createdBy, 
  376.             ins.lastEditBy,
  377.             it.insttypeName,
  378.             c.country, 
  379.             c.countryCode as instCtryCode,
  380.             ins.parentId as parentIdInst, 
  381.             p.instName as parentInstName, 
  382.             p.instNameEng as parentInstNameEng, 
  383.             p.acronym as parentAcronym, 
  384.             p.instLogo as parentInstLogo')
  385.             ->add(
  386.                 'from'
  387.                 'OceanExpertBundle:Institutions ins')
  388.             ->leftJoin(
  389.                 'OceanExpertBundle:Insttypes'
  390.                 'it'
  391.                 'WITH'
  392.                 'it.idInsttype = ins.instTypeId')
  393.             ->leftJoin(
  394.                 'OceanExpertBundle:Countries'
  395.                 'c'
  396.                 'WITH'
  397.                 'c.idCountry = ins.countryCode')
  398.             ->leftJoin(
  399.                 'OceanExpertBundle:Institutions'
  400.                 'p'
  401.                 'WITH'
  402.                 'p.idInst = ins.parentId AND p.activated = 1')
  403.             ->where('ins.idInst = :instId')
  404.             ->setParameter(
  405.                 'instId'
  406.                 $instId
  407.             );
  408.         $institution $this->getDoctrine()
  409.             ->getRepository('OceanExpertBundle:Institutions')
  410.             ->findOneByIdInst($instId);
  411.         $userId '-1';
  412.         $security_context $this->get('security.authorization_checker');
  413.         if ($security_context->isGranted('IS_AUTHENTICATED_FULLY')) {
  414.             $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  415.         }
  416.         if (isset($institution
  417.             && (($security_context->isGranted('ROLE_GLOBAL_EDITOR')) 
  418.                 or ($userId == $institution->getCreatedBy()) 
  419.                 or ($userId == $institution->getLastEditBy())
  420.             )
  421.         ) {
  422.             //@todo what happens here??? Arno 23/06/2021
  423.         } else {
  424.             $qb->andWhere('ins.activated = 1');
  425.         }
  426.         $routeName $request->get('_route');
  427.         $institute $qb->getQuery()->getResult();
  428.         if (!$institute) {
  429.             return $this->render(
  430.                 'Default/error.html.twig',
  431.                 array(
  432.                     'message' => 'Institution does not exists or the institution has not been approved by OceanExpert editor.'
  433.                 )
  434.             );
  435.         }
  436.         $OIHData['name'] = $institute[0]['instName'];
  437.         if (isset($institute[0]['instUrl'])
  438.             && $institute[0]['instUrl'] != ''
  439.         ) {
  440.             $OIHData['url'] = $institute[0]['instUrl'];
  441.         } else {
  442.             $OIHData['url'] = "https://oceanexpert.org/institution/$instId";
  443.         }
  444.         if (isset($institute[0]['instTel'])
  445.             && $institute[0]['instTel'] != ''
  446.         ) {
  447.             $OIHData['telephone'] = $institute[0]['instTel'];
  448.         }
  449.         if (isset($institute[0]['parentIdInst'])
  450.             && $institute[0]['parentIdInst'] != ''
  451.             && $institute[0]['parentIdInst'] != 0
  452.         ) {
  453.             $OIHData['memberOf'] = array(
  454.                 '@type' => 'Organization',
  455.                 '@id' => 'https://oceanexpert.org/institution/' $institute[0]['parentIdInst'],
  456.                 'url' => 'https://oceanexpert.org/institution/' $institute[0]['parentIdInst'],
  457.                 'name' => $institute[0]['parentInstName']
  458.             );
  459.         }
  460.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  461.         $qb->add(
  462.             'select'
  463.             'i.idInd,
  464.             i.fname,
  465.             i.mname,
  466.             i.sname,
  467.             i.jobtitle,
  468.             i.deceased,
  469.             i.retired, 
  470.             i.qualityChecked')
  471.             ->add(
  472.                 'from'
  473.                 'OceanExpertBundle:Indiv i')
  474.             ->leftJoin(
  475.                 'OceanExpertBundle:IndivInstitution'
  476.                 'iins'
  477.                 'WITH'
  478.                 'iins.idInd = i.idInd')
  479.             ->leftJoin(
  480.                 'OceanExpertBundle:Institutions'
  481.                 'ins'
  482.                 'WITH'
  483.                 'ins.idInst = iins.idInst')
  484.             ->where('ins.idInst = :instId')
  485.             ->andWhere('i.status = 1')
  486.             ->orderBy(
  487.                 'i.sname'
  488.                 'ASC')
  489.             ->setParameter(
  490.                 'instId'
  491.                 $instId
  492.             );
  493.         $resultData $qb->getQuery()->getResult();
  494.         if (is_array($resultData)) {
  495.             foreach ($resultData as $member) {
  496.                 $OIHData['member'][] = array(
  497.                     'member' => array(
  498.                         '@type' => 'Person',
  499.                         '@id' => 'https://oceanexpert.org/expert/' $member['idInd'],
  500.                         'url' => 'https://oceanexpert.org/expert/' $member['idInd'],
  501.                         'name' => $member['fname'] . ' ' $member['mname'] . ' ' $member['sname']
  502.                     )
  503.                 );
  504.             }
  505.         }
  506.         $paginator $this->get('knp_paginator');
  507.         $memberlimit $request->query->getInt('mlimit'5);
  508.         $memberpage $request->query->getInt('members'1);
  509.         if (!in_array($memberlimit, array(510"All"))) {
  510.             $memberlimit 5;
  511.             $memberpage 1;
  512.         }
  513.         
  514.         if ($memberlimit == "All") {
  515.             $memberlimit 9999;
  516.             $memberpage 1;
  517.         }
  518.         
  519.         $members $paginator->paginate(
  520.             $resultData,
  521.             $memberpage,
  522.             $memberlimit,
  523.             array(
  524.                 'pageParameterName' => 'members'
  525.                 'sortDirectionParameterName' => 'dir'
  526.             )
  527.         );
  528.         // child members
  529.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  530.         $qb->add(
  531.             'select'
  532.             'i.idInd,
  533.             i.fname,
  534.             i.mname,
  535.             i.sname,
  536.             i.jobtitle,
  537.             i.deceased,
  538.             i.retired, 
  539.             i.qualityChecked, 
  540.             ins.idInst,
  541.             ins.instName')
  542.             ->add(
  543.                 'from'
  544.                 'OceanExpertBundle:Indiv i')
  545.             ->leftJoin(
  546.                 'OceanExpertBundle:IndivInstitution'
  547.                 'iins'
  548.                 'WITH'
  549.                 'iins.idInd = i.idInd')
  550.             ->leftJoin(
  551.                 'OceanExpertBundle:Institutions'
  552.                 'ins'
  553.                 'WITH'
  554.                 'ins.idInst = iins.idInst')
  555.             ->where('ins.parentId = :instId')
  556.             ->andWhere('i.status = 1')
  557.             ->andWhere('ins.activated = 1')
  558.             ->orderBy(
  559.                 'i.sname'
  560.                 'ASC')
  561.             ->setParameter(
  562.                 'instId'
  563.                 $instId
  564.             );
  565.         $resultData $qb->getQuery()->getResult();
  566.         if (is_array($resultData)) {
  567.             foreach ($resultData as $member) {
  568.                 $instUrl 'https://oceanexpert.org/institution/' $member['idInst'];
  569.                 $instName $member['instName'];
  570.                 $OIHData['member'][] = array(
  571.                     'member' => array(
  572.                         '@type' => 'Person',
  573.                         '@id' => 'https://oceanexpert.org/expert/' $member['idInd'],
  574.                         'url' => 'https://oceanexpert.org/expert/' $member['idInd'],
  575.                         'name' => $member['fname'] . ' ' $member['mname'] . ' ' $member['sname'],
  576.                         'description' => "is member of '$instName' ($instUrl), a child of this organization"
  577.                     )
  578.                 );
  579.             }
  580.         }
  581.         $childMembers = array();
  582.         $childMemberLimit $request->query->get('cmlimit');
  583.         if (!in_array($childMemberLimit, array(510"All"))) {
  584.             $childMemberLimit 5;
  585.         }
  586.         if ($childMemberLimit == "All") {
  587.             $childMemberLimit 9999;
  588.         }
  589.         $childMembers $paginator->paginate(
  590.             $resultData,
  591.             $request->query->getInt('childmembers'1),
  592.             $childMemberLimit,
  593.             array(
  594.                 'pageParameterName' => 'childmembers'
  595.                 'sortDirectionParameterName' => 'dir'
  596.             )
  597.         );
  598.         // child institutes
  599.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  600.         $qb->add(
  601.             'select'
  602.             'ins.idInst,
  603.             ins.instTypeId, 
  604.             ins.instName, 
  605.             ins.instNameEng, 
  606.             ins.instAddress, 
  607.             ins.addr2, 
  608.             ins.city, 
  609.             ins.state, 
  610.             ins.postcode, 
  611.             ins.countryCode, 
  612.             ins.acronym, 
  613.             ins.instLogo, 
  614.             ins.instTel, 
  615.             ins.instFax, 
  616.             ins.instEmail, 
  617.             ins.instUrl, 
  618.             ins.edmoCode,
  619.             ins.instRegion, 
  620.             ins.activities, 
  621.             ins.popularity, 
  622.             ins.activated, 
  623.             ins.fDateEntered, 
  624.             ins.lDateUpdated, 
  625.             ins.createdBy, 
  626.             ins.lastEditBy,
  627.             it.insttypeName,
  628.             c.country, 
  629.             c.countryCode as instCtryCode')
  630.             ->add(
  631.                 'from'
  632.                 'OceanExpertBundle:Institutions ins')
  633.             ->leftJoin(
  634.                 'OceanExpertBundle:Insttypes'
  635.                 'it'
  636.                 'WITH'
  637.                 'it.idInsttype = ins.instTypeId')
  638.             ->leftJoin(
  639.                 'OceanExpertBundle:Countries'
  640.                 'c'
  641.                 'WITH'
  642.                 'c.idCountry = ins.countryCode')
  643.             ->where('ins.parentId = :instId')
  644.             ->andWhere('ins.activated = 1')
  645.             ->orderBy(
  646.                 'ins.instName'
  647.                 'ASC')
  648.             ->setParameter(
  649.                 'instId'
  650.                 $instId);
  651.         $resultData $qb->getQuery()->getResult();
  652.         if (is_array($resultData)) {
  653.             foreach ($resultData as $member) {
  654.                 $OIHData['member'][] = array(
  655.                     'member' => array(
  656.                         '@type' => 'Organization',
  657.                         '@id' => 'https://oceanexpert.org/institution/' $member['idInst'],
  658.                         'url' => 'https://oceanexpert.org/institution/' $member['idInst'],
  659.                         'name' => $member['instName']
  660.                     )
  661.                 );
  662.             }
  663.         }
  664.         $childInstitutes = array();
  665.         $childInstituteLimit $request->query->get('cilimit');
  666.         if (!in_array($childInstituteLimit, array(510"All"))) {
  667.             $childInstituteLimit 5;
  668.         }
  669.         if ($childInstituteLimit == "All") {
  670.             $childInstituteLimit 9999;
  671.         }
  672.         $childInstitutes $paginator->paginate(
  673.             $resultData,
  674.             $request->query->getInt('childinstitutes'1),
  675.             $childInstituteLimit,
  676.             array(
  677.                 'pageParameterName' => 'childinstitutes',
  678.                 'sortDirectionParameterName' => 'dir'
  679.             )
  680.         );
  681.         $repository $this->getDoctrine()
  682.             ->getRepository('OceanExpertBundle:Regions');
  683.         $institute[0]['seaRegions'] = $repository->createQueryBuilder('r')
  684.             ->select(
  685.                 'r.idRegion, 
  686.                 r.name')
  687.             ->where('r.idRegion IN(:seaRegions) ')
  688.             ->setParameter(
  689.                 'seaRegions',
  690.                 array_values(
  691.                     explode(
  692.                         ',',
  693.                         $institute[0]['instRegion']
  694.                     )
  695.                 )
  696.             )
  697.             ->getQuery()->getResult();
  698.         $institute[0]['availableSeaRegions'] = $this->getDoctrine()
  699.             ->getRepository('OceanExpertBundle:Regions')
  700.             ->createQueryBuilder('e')
  701.             ->select('e.idRegion as id, e.name')
  702.             ->orderBy('e.name')
  703.             ->getQuery()
  704.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  705.         if ($routeName == 'edit_institution') {
  706.             $securityContext $this->container->get('security.authorization_checker');
  707.             if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  708.                 $user $this->container->get('security.token_storage')->getToken()->getUser();
  709.                 //let's check if the logged-in user has a 'real' profile
  710.                 //meaning there is at least a firstname, lastname and language
  711.                 $em $this->getDoctrine()->getManager();
  712.                 $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  713.                 if (!SecurityController::checkUserProfile($em$userId)) {
  714.                     return $this->redirect(
  715.                         $this->generateUrl(
  716.                             'user_profile_edit'
  717.                         )
  718.                     );
  719.                 }
  720.                 
  721.                 if ($user->getId() == $institute[0]['createdBy'
  722.                     || $this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')
  723.                 ) {
  724.                     if (!empty($data $request->request->all())) {
  725.                         $em $this->getDoctrine()->getManager();
  726.                         $inst $em
  727.                             ->getRepository('OceanExpertBundle:Institutions')
  728.                             ->findOneBy(
  729.                                 array(
  730.                                     'idInst' => $data['instId']
  731.                                 )
  732.                             );
  733.                         if ($inst) {
  734.                             if (trim($data['iedmo']) == '' 
  735.                                 || !is_numeric($data['iedmo'])
  736.                             ) {
  737.                                 $edmo_code 0;
  738.                             } else {
  739.                                 $edmo_code $data['iedmo'];
  740.                             }
  741.                             $inst->setInstTypeId($data['instType']);
  742.                             $inst->setInstName($data['instName']);
  743.                             $inst->setInstNameEng($data['instNameEng']);
  744.                             $inst->setInstAddress($data['iaddress']);
  745.                             $inst->setAddr2($data['iaddressline2']);
  746.                             $inst->setCity($data['icity']);
  747.                             $inst->setState($data['istate']);
  748.                             $inst->setPostcode($data['ipostcode']);
  749.                             $inst->setCountryCode($data['icountryCode']);
  750.                             $inst->setAcronym($data['iacronym']);
  751.                             $inst->setInstTel($data['icontact']);
  752.                             $inst->setInstFax($data['ifax']);
  753.                             $inst->setInstEmail($data['iemail']);
  754.                             $inst->setInstUrl($data['iwebsite']);
  755.                             $inst->setEdmoCode($edmo_code);
  756.                             $inst->setActivities($data['iactivity']);
  757.                             $inst->setParentId($data['parentinstitute']);
  758.                             if (null == $institution->getParentId()) {
  759.                                 $institution->setParentId(0);
  760.                             }
  761.                             if (isset($data['studyregion']) 
  762.                                 && count($data['studyregion']) > 0
  763.                             ) {
  764.                                 $inst->setInstRegion(implode(','$data['studyregion']));
  765.                             } else {
  766.                                 $inst->setInstRegion('');
  767.                             }
  768.                             $inst->setLDateUpdated(new DateTime("now"));
  769.                             $inst->setLastEditBy($user->getId());
  770.                             $em->persist($inst);
  771.                             $em->flush();
  772.                             if (null != $request->files->get('instLogo')) {
  773.                                 $file $request->files->get('instLogo');
  774.                                 $filetype $file->guessClientExtension();
  775.                                 $filename 'instituteLogo.jpg';
  776.                                 $directory 'uploads/institutes/' $inst->getIdInst() . '/';
  777.                                 $uploadfile $file->move($directory$filename);
  778.                                 $inst->setInstLogo($filename);
  779.                             } else {
  780.                                 if (isset($data['textLogo']) 
  781.                                     && $data['textLogo'] == ''
  782.                                 ) {
  783.                                     $inst->setInstLogo('');
  784.                                     $filename 'uploads/institutes/' $inst->getIdInst() . '/instituteLogo.jpg';
  785.                                     if (file_exists($filename)) {
  786.                                         unlink($filename);
  787.                                     }
  788.                                 }
  789.                             }
  790.                             $em->persist($inst);
  791.                             $em->flush();
  792.                             if ($inst) {
  793.                                 return $this->redirect(
  794.                                     $this->generateUrl(
  795.                                         'edit_institute_success'
  796.                                         array(
  797.                                             'instId' => $data['instId']
  798.                                         )
  799.                                     )
  800.                                 );
  801.                             }
  802.                         }
  803.                     }
  804.                     $countries $this->getDoctrine()
  805.                         ->getRepository('OceanExpertBundle:Countries')
  806.                         ->findBy(
  807.                             [],
  808.                             ['country' => 'ASC']
  809.                         );
  810.                     return $this->render(
  811.                         'Institution/editinstitution.html.twig',
  812.                         array(
  813.                             'institute' => $institute[0],
  814.                             'countries' => $countries
  815.                         )
  816.                     );
  817.                 } else {
  818.                     return $this->redirect(
  819.                         $this->generateUrl(
  820.                             'frontend_homepage'
  821.                         )
  822.                     );
  823.                 }
  824.             } else {
  825.                 return $this->redirect(
  826.                     $this->generateUrl(
  827.                         'frontend_homepage'
  828.                     )
  829.                 );
  830.             }
  831.         }
  832.         $addressFormatRepository = new AddressFormatRepository();
  833.         $countryRepository = new CountryRepository();
  834.         $subdivisionRepository = new SubdivisionRepository();
  835.         $formatter = new DefaultFormatter($addressFormatRepository$countryRepository$subdivisionRepository);
  836.         $address = new Address();
  837.         $address $address
  838.             ->withCountryCode($institute[0]['instCtryCode'])
  839.             ->withAdministrativeArea($institute[0]['state'])
  840.             ->withLocality($institute[0]['city'])
  841.             ->withPostalCode($institute[0]['postcode'])
  842.             ->withAddressLine2($institute[0]['addr2'])
  843.             ->withAddressLine1($institute[0]['instAddress']);
  844.         $institute[0]['address'] = $formatter->format($address);
  845.         //make the address
  846.         $streetAddress $institute[0]['instAddress'];
  847.         if (trim($institute[0]['addr2']) != '') {
  848.             $streetAddress .= ', ' trim($institute[0]['addr2']);
  849.         }
  850.         $address $streetAddress ', ' .
  851.             $institute[0]['postcode'] . ' ' $institute[0]['city'] . ', ' .
  852.             $institute[0]['country'];
  853.         $OIHData['location'] = array(
  854.             '@type' => 'Place',
  855.             'address' => trim($address)
  856.         );
  857.         $OIHData['address'] = array(
  858.             '@type' => 'PostalAddress',
  859.             'addressLocality' => $institute[0]['city'] . ', ' $institute[0]['country'],
  860.             'postalCode' => $institute[0]['postcode'],
  861.             'streetAddress' => $streetAddress
  862.         );
  863.         /*
  864.         $OIHData['location'] = array(
  865.             '@type' => 'Place',
  866.             'address' => array(
  867.                 '@type' => 'PostalAddress',
  868.                 'addressLocality' => $institute[0]['city'] . ', ' . $institute[0]['country'],
  869.                 'postalCode' => $institute[0]['postcode'],
  870.                 'streetAddress' => $streetAddress
  871.             )
  872.         );
  873.         */
  874.         if ($institute[0]['createdBy']) {
  875.             $institute[0]['createdId'] = $institute[0]['createdBy'];
  876.             $institute[0]['createdBy'] = $this->getUserById($institute[0]['createdBy']);
  877.         }
  878.         if ($institute[0]['lastEditBy']) {
  879.             $institute[0]['lastEditId'] = $institute[0]['lastEditBy'];
  880.             $institute[0]['lastEditBy'] = $this->getUserById($institute[0]['lastEditBy']);
  881.         }
  882.         $limits = array(
  883.             'memberlimit' => array(
  884.                 'options' => array(
  885.                     '5' => 5,
  886.                     '10' => 10,
  887.                     '9999' => 'All'
  888.                 ),
  889.                 'selected' => $memberlimit
  890.             ),
  891.             'childmemberlimit' => array(
  892.                 'options' => array(
  893.                     '5' => 5,
  894.                     '10' => 10,
  895.                     '9999' => 'All'
  896.                 ),
  897.                 'selected' => $childMemberLimit
  898.             ),
  899.             'childinstitutelimit' => array(
  900.                 'options' => array(
  901.                     '5' => 5,
  902.                     '10' => 10,
  903.                     '9999' => 'All'
  904.                 ),
  905.                 'selected' => $childInstituteLimit
  906.             ),
  907.         );
  908.         $OIHData json_encode(
  909.             $OIHData,
  910.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES JSON_NUMERIC_CHECK
  911.         );
  912.         return $this->render(
  913.             'Institution/viewinstitution.html.twig',
  914.             array(
  915.                 'OIHData' => $OIHData,
  916.                 'institute' => $institute[0],
  917.                 'members' => $members,
  918.                 'limits' => $limits,
  919.                 'childmembers' => $childMembers,
  920.                 'childinstitutes' => $childInstitutes
  921.             )
  922.         );
  923.     }
  924.     public function getUserById($id '')
  925.     {
  926.         $updatedBy $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($id);
  927.         if ($updatedBy) {
  928.             $name $updatedBy->getfname() . " " $updatedBy->getsname();
  929.         } else {
  930.             $name false;
  931.         }
  932.         return $name;
  933.     }
  934.     public function activateInstituteAction($instituteId)
  935.     {
  936.         $security_context $this->get('security.authorization_checker');
  937.         if ($security_context->isGranted('ROLE_GLOBAL_EDITOR')) {
  938.             if ($instituteId) {
  939.                 $institute $this->getDoctrine()
  940.                     ->getRepository('OceanExpertBundle:Institutions')
  941.                     ->findOneByIdInst($instituteId);
  942.                 if ($institute) {
  943.                     $institute->setActivated(1);
  944.                     $institute->setLDateUpdated(new DateTime("now"));
  945.                     $institute->setLastEditBy(
  946.                         $this->get('security.token_storage')
  947.                             ->getToken()
  948.                             ->getUser()
  949.                             ->getId()
  950.                     );
  951.                     $em $this->getDoctrine()->getManager();
  952.                     $em->persist($institute); //marks object to be saved in the next transaction.
  953.                     $em->flush(); //performs all saves and transactions.
  954.                 }
  955.                 return new JsonResponse(
  956.                     array(
  957.                         'status' => true,
  958.                         'msg' => 'Record updated successfully'
  959.                     )
  960.                 );
  961.             } else {
  962.                 return new Response('Cannot update record.');
  963.             }
  964.         }
  965.         return new Response('Cannot update record.');
  966.     }
  967.     public function deactivateInstituteAction($instituteId)
  968.     {
  969.         $security_context $this->get('security.authorization_checker');
  970.         if ($security_context->isGranted('ROLE_GLOBAL_EDITOR')) {
  971.             if ($instituteId) {
  972.                 $institute $this->getDoctrine()
  973.                     ->getRepository('OceanExpertBundle:Institutions')
  974.                     ->findOneByIdInst($instituteId);
  975.                 if ($institute) {
  976.                     $institute->setActivated(0);
  977.                     $institute->setLDateUpdated(new DateTime("now"));
  978.                     $institute->setLastEditBy(
  979.                         $this->get('security.token_storage')
  980.                             ->getToken()
  981.                             ->getUser()
  982.                             ->getId());
  983.                     $em $this->getDoctrine()->getManager();
  984.                     $em->persist($institute); //marks object to be saved in the next transaction.
  985.                     $em->flush(); //performs all saves and transactions.
  986.                 }
  987.                 return new JsonResponse(
  988.                     array(
  989.                         'status' => true,
  990.                         'msg' => 'updated successfully'
  991.                     )
  992.                 );
  993.             } else {
  994.                 return new Response('Cannot update record.');
  995.             }
  996.         }
  997.         return new Response('Cannot update record.');
  998.     }
  999.     public function editInstituteSuccessAction($instId)
  1000.     {
  1001.         return $this->render(
  1002.             'Institution/editInstitutionSuccess.html.twig',
  1003.             array(
  1004.                 'institute' => $instId
  1005.             )
  1006.         );
  1007.     }
  1008.     /**
  1009.      * @param Request $request
  1010.      *
  1011.      * @return Response
  1012.      */
  1013.     public function getInstitutionsAjaxAction(Request $request)
  1014.     {
  1015.         if (!null == $request->query->get('q')) {
  1016.             $inst $request->query->get('q');
  1017.             $em $this->getDoctrine()->getManager();
  1018.             $qb $em->createQueryBuilder();
  1019.             $qb->select('i.idInst as id,i.instName');
  1020.             $qb->from('OceanExpertBundle:Institutions''i');
  1021.             $qb->where('i.instName like :name OR i.acronym like :name');
  1022.             if (!null == $request->query->get('instId')) {
  1023.                 $qb->andWhere('i.idInst != :instId');
  1024.                 $qb->setParameter('instId'$request->query->get('instId'));
  1025.             }
  1026.             $qb->andWhere('i.activated = 1');
  1027.             $qb->setParameter('name''%' $inst '%');
  1028.             $qb->setParameter('name''%' $inst '%');
  1029.             $qb->orderBy('i.instName''ASC');
  1030.             $institutes $qb->getQuery()->getResult();
  1031.             $data = array();
  1032.             if ($institutes) {
  1033.                 $data = array(
  1034.                     'incomplete_results' => false,
  1035.                     'items' => $institutes,
  1036.                     'total_count' => count($institutes),
  1037.                 );
  1038.             } else {
  1039.                 $data = array(
  1040.                     'incomplete_results' => false,
  1041.                     'items' => array(),
  1042.                     'total_count' => 0,
  1043.                 );
  1044.             }
  1045.             return new JsonResponse($data);
  1046.         }
  1047.         return new JsonResponse();
  1048.     }
  1049. }