src/OceanExpertBundle/Controller/InstitutionController.php line 655

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 Exception;
  12. use OceanExpertBundle\Controller\Api\ApiReportsController;
  13. use OceanExpertBundle\Entity\Institutions;
  14. use Psr\Container\ContainerExceptionInterface;
  15. use Psr\Container\NotFoundExceptionInterface;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. class InstitutionController extends AbstractController
  22. {
  23.     /**
  24.      * get the list of all institutes for a given countryCode/Id
  25.      *
  26.      * @param Request $request
  27.      *
  28.      * @return JsonResponse
  29.      */
  30.     public function getInstitutionByCountryAction(Request $request): JsonResponse
  31.     {
  32.         $instituteList = array();
  33.         if ($request->query->get('countryCode') == NULL) {
  34.             $instituteList = array(
  35.                 'institution' => 'Select Country First.'
  36.             );
  37.         } else {
  38.             $countryCode $request->query->get('countryCode');
  39.             $repo $this->getDoctrine()
  40.                 ->getRepository('OceanExpertBundle:Institutions');
  41.             $query $repo->createQueryBuilder('a')
  42.                 ->select('distinct a.instName, a.idInst, a.instAddress, a.city, a.state, a.postcode, a.idInst, a.activated')
  43.                 ->where('a.countryCode = :code')
  44.                 ->andWhere('a.activated in (0,1)')
  45.                 ->orderBy('a.instName''ASC')
  46.                 ->setParameter('code'$countryCode);
  47.             if ($request->query->get('search') !== '') {
  48.                 $query $query->andWhere('a.instName like :search')
  49.                     ->setParameter('search''%' $request->query->get('search') . '%');
  50.             }
  51.             $query $query->getQuery();
  52.             $instituteList $query->getResult();
  53.         }
  54.         return new JsonResponse($instituteList);
  55.     }
  56.     /**
  57.      * get the information about an institute with a given id
  58.      *
  59.      * @param Request $request
  60.      *
  61.      * @return JsonResponse
  62.      */
  63.     public function getInstitutionAddressAction(Request $request): Response
  64.     {
  65.         $instituteDetails = array();
  66.         if ($request->query->get('instName') != NULL) {
  67.             $idInst $request->query->get('instName');
  68.             $repo $this->getDoctrine()
  69.                 ->getRepository('OceanExpertBundle:Institutions');
  70.             $query $repo->createQueryBuilder('a')
  71.                 ->select('
  72.                     a.instName, 
  73.                     a.instAddress, 
  74.                     a.addr2,
  75.                     a.postcode, 
  76.                     a.city, 
  77.                     a.state, 
  78.                     a.countryCode,
  79.                     a.instTel
  80.                     ')
  81.                 ->where('a.idInst = :idInst')
  82.                 // ->setParameter('title',$title)
  83.                 ->setParameter('idInst'$idInst)
  84.                 ->getQuery();
  85.             $instituteDetails $query->getResult();
  86.             return new JsonResponse($instituteDetails);
  87.         } else {
  88.             return new JsonResponse(
  89.                 array(
  90.                     'status' => 0,
  91.                     'message' => 'need an id of the institute'
  92.                 )
  93.             );
  94.         }
  95.     }
  96.     /**
  97.      * add a new institute
  98.      *
  99.      * @deprecated
  100.      *
  101.      * @param Request $request
  102.      *
  103.      * @return Response
  104.      */
  105.     public function addInstitutionAction(Request $request): Response
  106.     {
  107.         return $this->addInstituteAction($request);
  108.     }
  109.     /**
  110.      * add a new institute
  111.      *
  112.      * @param Request $request
  113.      *
  114.      * @return Response
  115.      */
  116.     public function addInstituteAction(Request $request): Response
  117.     {
  118.         $data $request->request->all();
  119.         $securityContext $this->get('security.authorization_checker');
  120.         if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  121.             $em $this->getDoctrine()->getManager();
  122.             $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  123.             //let's check if the logged-in user has a 'real' profile
  124.             //the mandatory profile fields are all filled and the expert is active
  125.             // Arno 21/06/23 : we will not check this for the time being see also #521
  126.             //if people cannot create their institute while registering they will not make / add it later
  127.             /*
  128.             if (!SecurityController::checkUserProfile($em, $userId)) {
  129.                 return $this->redirect(
  130.                     $this->generateUrl(
  131.                         'user_profile_edit'
  132.                     )
  133.                 );
  134.             }
  135.             */
  136.             //this is here to prevent breaking inst creation by something else that 'edit/create profile'
  137.             if (null !== $request->request->get('instCountryCode')) {
  138.                 //this is correct and used by 'edit/create profile'
  139.                 $countryCode $request->request->get('instCountryCode');
  140.             } else {
  141.                 //this may be incorrect as we need the country code of the inst
  142.                 //and on some page this is mixed with country code of the expert
  143.                 $countryCode $request->request->get('countryCode');
  144.             }
  145.             if (trim($request->request->get("iedmo")) == ''
  146.                 || !is_numeric($request->request->get("iedmo"))
  147.             ) {
  148.                 $edmo_code 0;
  149.             } else {
  150.                 $edmo_code $request->request->get("iedmo");
  151.             }
  152.             $instituteDetails = array();
  153.             $repo $this->getDoctrine()
  154.                 ->getRepository('OceanExpertBundle:Institutions');
  155.             $query $repo->createQueryBuilder('a')
  156.                 ->select('a.idInst, a.instName, a.instAddress, a.city, a.state, a.postcode, a.instTel')
  157.                 ->where('a.instName like :name')
  158.                 ->andWhere('a.countryCode = :countryCode')
  159.                 ->setParameter('name'$request->request->get('instName'))
  160.                 ->setParameter('countryCode'$countryCode)
  161.                 ->getQuery();
  162.             $instituteDetails $query->getResult();
  163.             if (count($instituteDetails) == 0) {
  164.                 try {
  165.                     $institute = new Institutions();
  166.                     $institute->setCountryCode($countryCode);
  167.                     $institute->setInstName($request->request->get('instName'));
  168.                     $institute->setInstNameEng($request->request->get('instNameEng'));
  169.                     $institute->setInstTypeID($request->request->get('instType'));
  170.                     $institute->setParentId($request->request->get('parentinstitute'));
  171.                     if (null == $institute->getParentId()) {
  172.                         $institute->setParentId(0);
  173.                     }
  174.                     $institute->setInstAddress($request->request->get('iaddress'));
  175.                     $institute->setAcronym($request->request->get('iacronym'));
  176.                     $institute->setAddr2($request->request->get('iaddress2'));
  177.                     $institute->setPostcode($request->request->get('ipostcode'));
  178.                     $institute->setCity($request->request->get('icity'));
  179.                     $institute->setState($request->request->get('istate'));
  180.                     $institute->setInstTel($request->request->get('icontact'));
  181.                     $institute->setInstFax($request->request->get('ifax'));
  182.                     $institute->setInstEmail($request->request->get('iemail'));
  183.                     $institute->setInstURL($request->request->get('iwebsite'));
  184.                     $institute->setEdmoCode($edmo_code);
  185.                     $institute->setActivities($request->request->get('iactivity'));
  186.                     if (null !== $request->request->get('isearegion')
  187.                         && is_array($request->request->get('isearegion'))
  188.                         && count($request->request->get('isearegion')) > 1
  189.                     ) {
  190.                         $iseaRegion implode(','$request->request->get('isearegion'));
  191.                     } else {
  192.                         $iseaRegion '';
  193.                     }
  194.                     $institute->setInstRegion($iseaRegion);
  195.                     $institute->setPopularity(0);
  196.                     if ($this->get('security.authorization_checker')->isGranted('ROLE_LME')) {
  197.                         $institute->setActivated(1);
  198.                     } else {
  199.                         $institute->setActivated(0);
  200.                     }
  201.                     $institute->setFDateEntered(new DateTime('now'));
  202.                     $institute->setLDateUpdated(new DateTime('now'));
  203.                     $institute->setCreatedBy($userId);
  204.                     $em->persist($institute); //marks object to be saved in the next transaction.
  205.                     $em->flush(); //performs all saves and transactions.
  206.                     $logoFilename 'instituteLogo.jpg';
  207.                     $logoDirectory 'uploads/institutes/' $institute->getIdInst() . '/';
  208.                     if (null != $request->files->get('instLogo')) {
  209.                         $file $request->files->get('instLogo');
  210.                         $file->move($logoDirectory$logoFilename);
  211.                         $institute->setInstLogo($logoFilename);
  212.                     } else {
  213.                         if (isset($data['textLogo']) && $data['textLogo'] == '') {
  214.                             $institute->setInstLogo('');
  215.                             $filename $logoDirectory $logoFilename;
  216.                             if (file_exists($filename)) {
  217.                                 unlink($filename);
  218.                             }
  219.                         }
  220.                     }
  221.                     $em->persist($institute);
  222.                     $em->flush();
  223.                     return new JsonResponse(
  224.                         array(
  225.                             'status' => 1,
  226.                             'id' => $institute->getIdInst(),
  227.                             'message' => '<br /><br /><h3 class="text-success">institute created</h3>
  228.                                 Your new institute has been created successfully<br />
  229.                                 and should be already selected in the list of institutes.<br />
  230.                                 If this is not the case, please select your newly created institute in the list.
  231.                                 <br /><br />'
  232.                         )
  233.                     );
  234.                 } catch (Exception $e) {
  235.                     return new JsonResponse(
  236.                         array(
  237.                             'status' => 0,
  238.                             'message' => 'Error in adding Institute. Please try again later.'
  239.                         )
  240.                     );
  241.                 }
  242.             } else {
  243.                 foreach ($instituteDetails as $instituteDetail) {
  244.                     $existingInstitutes[] = '<a 
  245.                             href="/institute/' $instituteDetail['idInst'] . '" 
  246.                             target="_new">' .
  247.                         $instituteDetail['instName'] .
  248.                     '</a>';
  249.                 }
  250.                 $existingInstitutes implode(','$existingInstitutes);
  251.                 return new JsonResponse(
  252.                     array(
  253.                         'status' => 2,
  254.                         'message' => "Institute name/acronym already exists. ($existingInstitutes)"
  255.                     )
  256.                 );
  257.             }
  258.         } else {
  259.             return new JsonResponse(
  260.                 array(
  261.                     'status' => 2,
  262.                     'message' => 'you do not have the correct rights to do this'
  263.                 )
  264.             );
  265.         }
  266.     }
  267.     /**
  268.      * get all existing institutes
  269.      *
  270.      * @return Response
  271.      */
  272.     public function getAllInstitutions(): Response
  273.     {
  274.         $institutesQuery $this->getAllInstitutionsQuery();
  275.         $institutesTmp $institutesQuery->execute();
  276.         $institutes = array();
  277.         foreach($institutesTmp as $institute) {
  278.             $institutes[$institute['idInst']] = $institute;
  279.         }
  280.         return new Response(json_encode($institutes));
  281.     }
  282.     /**
  283.      * get the query to get all existing institutes
  284.      *
  285.      * @param string $query
  286.      *
  287.      */
  288.     private function getAllInstitutionsQuery(
  289.         $query ''
  290.     ) {
  291.         //get the institutes
  292.         $repo $this->getDoctrine()
  293.             ->getRepository('OceanExpertBundle:Institutions');
  294.         $institutes $repo->createQueryBuilder('i');
  295.         $institutes->select('
  296.             i.idInst, 
  297.             i.parentId,
  298.             i.instTypeId,
  299.             i.instName,
  300.             i.instNameEng,
  301.             i.countryCode,
  302.             c.country,
  303.             it.insttypeName'
  304.         );
  305.         $institutes->leftJoin(
  306.             'OceanExpertBundle:Countries',
  307.             'c',
  308.             'WITH',
  309.             'i.countryCode = c.idCountry');
  310.         $institutes->leftJoin(
  311.             'OceanExpertBundle:Insttypes',
  312.             'it',
  313.             'WITH',
  314.             'i.instTypeId = it.idInsttype');
  315.         //non-admin users only get the active ones
  316.         if (!$this->get('security.authorization_checker')->isGranted('ROLE_SUPERADMIN')
  317.             && !$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')
  318.         ) {
  319.             $institutes->where('i.activated = 1');
  320.         }
  321.         if (trim($query) != '') {
  322.             $institutes->andwhere('i.instName LIKE :query OR i.instNameEng LIKE :query');
  323.             $institutes->setParameter(
  324.                 'query',
  325.                 '%' trim($query) . '%'
  326.             );
  327.         }
  328.         //store all the found institutes to be able to find the name of the parents
  329.         $institutesQuery $institutes->getQuery();
  330.         return $institutesQuery;
  331.     }
  332.     /**
  333.      * show all existing institutes
  334.      *
  335.      * @param Request $request
  336.      *
  337.      * @return Response|void
  338.      */
  339.     function viewInstitutesActionRequest $request ): Response
  340.     {
  341.         $limits = array(
  342.             10 => 10,
  343.             25 => 25,
  344.             50 => 50,
  345.             100 => 100,
  346.             500 => 500
  347.         );
  348.         $limit $request->query->get('limit'10);
  349.         $query $request->query->get('search''');
  350.         $orderby $request->query->get('orderby''order');
  351.         $dir $request->query->get('dir''asc');
  352.         //store all the found institutes to be able to find the name of the parents
  353.         $institutesQuery $this->getAllInstitutionsQuery($query);
  354.         $institutesTmp $institutesQuery->execute();
  355.         foreach($institutesTmp as $institute) {
  356.             $parentInstitutes[$institute['idInst']] = $institute['instName'];
  357.         }
  358.         $paginator $this->get('knp_paginator');
  359.         $data $paginator->paginate(
  360.             $institutesQuery,
  361.             $request->query->getInt('page'1),
  362.             $limit,
  363.             array(
  364.                 'pageParameterName' => 'page',
  365.                 'sortDirectionParameterName' => 'dir',
  366.                 'defaultSortDirection' => 'desc'
  367.             )
  368.         );
  369.         $data->setCustomParameters([
  370.             'limits' => $limits,
  371.             'title' => 'Institutes'
  372.         ]);
  373.         return $this->render(
  374.             'Institute/viewInstitutes.html.twig',
  375.             array(
  376.                 'institutesData' => $data,
  377.                 'parentInstitutes' => $parentInstitutes
  378.             )
  379.         );
  380.     }
  381.     /**
  382.      * this is here for backwards compatibility
  383.      *
  384.      * @deprecated
  385.      *
  386.      * @param int $instId
  387.      * @param Request $request
  388.      *
  389.      * @return RedirectResponse|Response
  390.      * @throws ContainerExceptionInterface
  391.      * @throws NotFoundExceptionInterface
  392.      */
  393.     public function viewInstitutionAction(int $instIdRequest $request)
  394.     {
  395.         return $this->viewInstituteAction($instId$request);
  396.     }
  397.     /**
  398.      * view the details of an institute
  399.      * This is used for both viewing and editing an institute.
  400.      *
  401.      * @param int $instId
  402.      * @param Request $request
  403.      *
  404.      * @return RedirectResponse|Response
  405.      * @throws ContainerExceptionInterface
  406.      * @throws NotFoundExceptionInterface
  407.      */
  408.     public function viewInstituteAction(int $instIdRequest $request)
  409.     {
  410.         if (!is_numeric($instId)) {
  411.             return $this->render(
  412.                 'Exception/error.html.twig',
  413.                 array(
  414.                     'message' => 'The institute id should be a numberic value, "' $instId '" given instead.'
  415.                 )
  416.             );
  417.         }
  418.         /*
  419.          * container to hold to OIH/ODIS JSON-LD info
  420.          * see also https://book.oceaninfohub.org/thematics/expinst/README.html
  421.          * in the end we will need something like
  422.          * {
  423.                 "@context": {
  424.                     "@vocab": "https://schema.org/"
  425.                 },
  426.                 "@id": "https://example.org/id/org/x",
  427.                 "@type": "Organization",
  428.                 "address": {
  429.                     "@type": "PostalAddress",
  430.                     "addressLocality": "Paris, France",
  431.                     "postalCode": "F-75002",
  432.                     "streetAddress": "38 avenue de l'Opera"
  433.                 },
  434.                 "email": "secretariat(at)example.org",
  435.                 "name": "Organization X",
  436.                 "description": "Description of org ...",
  437.                 "telephone": "( 33 1) 42 68 53 00",
  438.                 "member": [
  439.                     {
  440.                         "@type": "Organization",
  441.                         "name": "Organization A",
  442.                         "description": "Org A is a potential parent organization of Org X"
  443.                     }
  444.                 ],
  445.                 "identifier": {
  446.                     "@id": "https://grid.ac/institutes/grid.475727.4",
  447.                     "@type": "PropertyValue",
  448.                     "description": "UN Department of Economic and Social Affairs Sustainable Development",
  449.                     "propertyID": "https://registry.identifiers.org/registry/grid",
  450.                     "url": "https://grid.ac/institutes/grid.475727.4"
  451.                 }
  452.             }
  453.          */
  454.         $OIHData = array(
  455.             '@context' => array(
  456.                 '@vocab' => 'https://schema.org/'
  457.             ),
  458.             '@id' => "https://oceanexpert.org/institute/$instId",
  459.             '@type' => 'Organization'
  460.         );
  461.         if ($instId !== ) {
  462.             $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  463.             $qb->add(
  464.                 'select',
  465.                 'ins.idInst,
  466.                 ins.instTypeId, 
  467.                 ins.instName, 
  468.                 ins.instNameEng, 
  469.                 ins.instAddress, 
  470.                 ins.addr2, 
  471.                 ins.city, 
  472.                 ins.state, 
  473.                 ins.postcode, 
  474.                 ins.countryCode, 
  475.                 ins.acronym, 
  476.                 ins.instLogo, 
  477.                 ins.instTel, 
  478.                 ins.instFax, 
  479.                 ins.instEmail, 
  480.                 ins.instUrl, 
  481.                 ins.edmoCode,
  482.                 ins.instRegion, 
  483.                 ins.activities, 
  484.                 ins.popularity, 
  485.                 ins.activated, 
  486.                 ins.fDateEntered, 
  487.                 ins.lDateUpdated, 
  488.                 ins.createdBy, 
  489.                 ins.lastEditBy,
  490.                 it.insttypeName,
  491.                 c.country, 
  492.                 c.countryCode as instCountryCode,
  493.                 ins.parentId as parentIdInst, 
  494.                 p.instName as parentInstName, 
  495.                 p.instNameEng as parentInstNameEng, 
  496.                 p.acronym as parentAcronym, 
  497.                 p.instLogo as parentInstLogo')
  498.                 ->add(
  499.                     'from',
  500.                     'OceanExpertBundle:Institutions ins')
  501.                 ->leftJoin(
  502.                     'OceanExpertBundle:Insttypes',
  503.                     'it',
  504.                     'WITH',
  505.                     'it.idInsttype = ins.instTypeId')
  506.                 ->leftJoin(
  507.                     'OceanExpertBundle:Countries',
  508.                     'c',
  509.                     'WITH',
  510.                     'c.idCountry = ins.countryCode')
  511.                 ->leftJoin(
  512.                     'OceanExpertBundle:Institutions',
  513.                     'p',
  514.                     'WITH',
  515.                     'p.idInst = ins.parentId AND p.activated = 1')
  516.                 ->where('ins.idInst = :instId')
  517.                 ->setParameter(
  518.                     'instId',
  519.                     $instId
  520.                 );
  521.             //get some info about the user asking this info
  522.             $userId '-1';
  523.             $securityContext $this->get('security.authorization_checker');
  524.             if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  525.                 $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  526.             }
  527.             if (!$securityContext->isGranted('ROLE_GLOBAL_EDITOR')) {
  528.                 $qb->andWhere('ins.activated = 1');
  529.             }
  530.             $institute $qb->getQuery()->getResult();
  531.         }
  532.         //get the list of all known institute types
  533.         $instituteTypes = [];
  534.         $instTypes $this->getDoctrine()
  535.             ->getRepository('OceanExpertBundle:Insttypes')
  536.             ->findAll();
  537.         foreach ($instTypes as $instType) {
  538.             $instituteTypes[] = array(
  539.                 'id' => $instType->getIdInsttype(),
  540.                 'name' => $instType->getInsttypeName()
  541.             );
  542.         }
  543.         //get the list of all countries
  544.         $countries $this->getDoctrine()
  545.             ->getRepository('OceanExpertBundle:Countries')
  546.             ->findBy(
  547.                 [],
  548.                 ['country' => 'ASC']
  549.             );
  550.         if ((!isset($institute)
  551.             || !$institute)
  552.             && $instId !== 0
  553.         ) {
  554.             return $this->render(
  555.                 'Exception/error.html.twig',
  556.                 array(
  557.                     'message' => 'the institute with id "' $instId '" does not exist or has not been approved by an OceanExpert editor (yet)'
  558.                 )
  559.             );
  560.         } elseif ($instId == 0) {
  561.             $institute = array(
  562.                 'idInst' => 0,
  563.                 'instName' => '',
  564.                 'instNameEng' => '',
  565.                 'instAddress' => '',
  566.                 'addr2' => '',
  567.                 'city' => '',
  568.                 'state' => '',
  569.                 'postcode' => '',
  570.                 'countryCode' => '',
  571.                 'acronym' => '',
  572.                 'instLogo' => '',
  573.                 'instTel' => '',
  574.                 'instFax' => '',
  575.                 'instEmail' => '',
  576.                 'instUrl' => '',
  577.                 'edmoCode' => 0,
  578.                 'instRegion' => '',
  579.                 'activities' => '',
  580.                 'country' => '',
  581.                 'instTypeId' => '',
  582.                 'availableSeaRegions' => '',
  583.                 'parentIdInst' => '',
  584.                 'parentInstName' => '',
  585.             );
  586.             $securityContext $this->get('security.authorization_checker');
  587.             if (($request->request->get('instName') === ''
  588.                 || null === $request->request->get('instName'))
  589.                 && $securityContext->isGranted('IS_AUTHENTICATED_FULLY')
  590.             ) {
  591.                 return $this->render(
  592.                     'Institute/editInstitute.html.twig',
  593.                     array(
  594.                         'institute' => $institute,
  595.                         'instituteTypes' => $instituteTypes,
  596.                         'countries' => $countries,
  597.                         'countryInstitutes' => array(),
  598.                     )
  599.                 );
  600.             } elseif ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  601.                 //create a new institute
  602.                 $institute = new Institutions();
  603.                 $institute->setInstName($request->request->get('instName'));
  604.                 $institute->setInstNameEng($request->request->get('instNameEng'));
  605.                 $institute->setInstAddress($request->request->get('iaddress'));
  606.                 $institute->setAddr2($request->request->get('iaddressline2'));
  607.                 $institute->setCity($request->request->get('icity'));
  608.                 $institute->setState($request->request->get('istate'));
  609.                 $institute->setPostcode($request->request->get('ipostcode'));
  610.                 $institute->setCountryCode($request->request->get('instCountryCode'));
  611.                 $institute->setInstTel($request->request->get('icontact'));
  612.                 $institute->setInstFax($request->request->get('ifax'));
  613.                 $institute->setInstEmail($request->request->get('iemail'));
  614.                 $institute->setInstUrl($request->request->get('iwebsite'));
  615.                 $institute->setEdmoCode($request->request->get('iedmo'));
  616.                 $institute->setInstRegion($request->request->get('isearegion'));
  617.                 $institute->setInstTypeId($request->request->get('instType'));
  618.                 $institute->setParentId($request->request->get('parentinstitute'));
  619.                 $institute->setActivities($request->request->get('iactivity'));
  620.                 if (null == $institute->getParentId()) {
  621.                     $institute->setParentId(0);
  622.                 }
  623.                 $institute->setPopularity(0);
  624.                 $institute->setActivated(0);
  625.                 $institute->setFDateEntered(new DateTime('now'));
  626.                 $institute->setLDateUpdated(new DateTime('now'));
  627.                 $institute->setCreatedBy($this->get('security.token_storage')->getToken()->getUser()->getId());
  628.                 $institute->setLastEditBy($this->get('security.token_storage')->getToken()->getUser()->getId());
  629.                 $institute->setInstLogo($request->request->get('instLogo'));
  630.                 $institute->setInstTypeId($request->request->get('instType'));
  631.                 $em $this->getDoctrine()->getManager();
  632.                 $em->persist($institute);
  633.                 $em->flush();
  634.                 $url $this->generateUrl(
  635.                     'view_institute',
  636.                     array(
  637.                         'instId' => $institute->getIdInst()
  638.                     )
  639.                 );
  640.                 return new RedirectResponse($url);
  641.             } else {
  642.                 return $this->render(
  643.                     'Exception/error.html.twig',
  644.                     array(
  645.                         'message' => 'You need to be logged in to create a new institute.'
  646.                     )
  647.                 );
  648.             }
  649.         } else {
  650.             $institute $institute[0];
  651.             $OIHData['name'] = $institute['instName'];
  652.             if (isset($institute['instUrl'])
  653.                 && $institute['instUrl'] != ''
  654.             ) {
  655.                 $OIHData['url'] = $institute['instUrl'];
  656.             } else {
  657.                 $OIHData['url'] = "https://oceanexpert.org/institute/$instId";
  658.             }
  659.             if (isset($institute['instTel'])
  660.                 && $institute['instTel'] != ''
  661.             ) {
  662.                 $OIHData['telephone'] = $institute['instTel'];
  663.             }
  664.             if (isset($institute['parentIdInst'])
  665.                 && $institute['parentIdInst'] !== ''
  666.                 && $institute['parentIdInst'] !== 0
  667.             ) {
  668.                 $OIHData['memberOf'] = array(
  669.                     '@type' => 'Organization',
  670.                     '@id' => 'https://oceanexpert.org/institute/' $institute['parentIdInst'],
  671.                     'url' => 'https://oceanexpert.org/institute/' $institute['parentIdInst'],
  672.                     'name' => $institute['parentInstName']
  673.                 );
  674.             }
  675.         }
  676.         //get the members of the institute
  677.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  678.         $qb->add(
  679.             'select',
  680.             'i.idInd,
  681.             i.fname,
  682.             i.mname,
  683.             i.sname,
  684.             i.jobtitle,
  685.             i.deceased,
  686.             i.retired, 
  687.             i.qualityChecked')
  688.             ->add(
  689.                 'from',
  690.                 'OceanExpertBundle:Indiv i')
  691.             ->leftJoin(
  692.                 'OceanExpertBundle:IndivInstitution',
  693.                 'iins',
  694.                 'WITH',
  695.                 'iins.idInd = i.idInd')
  696.             ->leftJoin(
  697.                 'OceanExpertBundle:Institutions',
  698.                 'ins',
  699.                 'WITH',
  700.                 'ins.idInst = iins.idInst')
  701.             ->where('ins.idInst = :instId')
  702.             ->andWhere('i.status = 1')
  703.             ->orderBy(
  704.                 'i.sname',
  705.                 'ASC')
  706.             ->setParameter(
  707.                 'instId',
  708.                 $instId
  709.             );
  710.         $instituteMembers $qb->getQuery()->getResult();
  711.         if (is_array($instituteMembers)) {
  712.             foreach ($instituteMembers as $member) {
  713.                 $OIHData['member'][] = array(
  714.                     'member' => array(
  715.                         '@type' => 'Person',
  716.                         '@id' => 'https://oceanexpert.org/expert/' $member['idInd'],
  717.                         'url' => 'https://oceanexpert.org/expert/' $member['idInd'],
  718.                         'name' => $member['fname'] . ' ' $member['mname'] . ' ' $member['sname']
  719.                     )
  720.                 );
  721.             }
  722.         }
  723.         //what page with members are we showing
  724.         $paginator $this->get('knp_paginator');
  725.         $memberLimit $request->query->getInt('mlimit'5);
  726.         $memberPage $request->query->getInt('members'1);
  727.         if (!in_array($memberLimit, array(510"All"))) {
  728.             $memberLimit 5;
  729.             $memberPage 1;
  730.         }
  731.         if ($memberLimit == "All") {
  732.             $memberLimit 9999;
  733.             $memberPage 1;
  734.         }
  735.         $members $paginator->paginate(
  736.             $instituteMembers,
  737.             $memberPage,
  738.             $memberLimit,
  739.             array(
  740.                 'pageParameterName' => 'members',
  741.                 'sortDirectionParameterName' => 'dir'
  742.             )
  743.         );
  744.         //get the members of the child institutes
  745.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  746.         $qb->add(
  747.             'select',
  748.             'i.idInd,
  749.             i.fname,
  750.             i.mname,
  751.             i.sname,
  752.             i.jobtitle,
  753.             i.deceased,
  754.             i.retired, 
  755.             i.qualityChecked, 
  756.             ins.idInst,
  757.             ins.instName')
  758.             ->add(
  759.                 'from',
  760.                 'OceanExpertBundle:Indiv i')
  761.             ->leftJoin(
  762.                 'OceanExpertBundle:IndivInstitution',
  763.                 'iins',
  764.                 'WITH',
  765.                 'iins.idInd = i.idInd')
  766.             ->leftJoin(
  767.                 'OceanExpertBundle:Institutions',
  768.                 'ins',
  769.                 'WITH',
  770.                 'ins.idInst = iins.idInst')
  771.             ->where('ins.parentId = :instId')
  772.             ->andWhere('i.status = 1')
  773.             ->andWhere('ins.activated = 1')
  774.             ->orderBy(
  775.                 'i.sname',
  776.                 'ASC')
  777.             ->setParameter(
  778.                 'instId',
  779.                 $instId
  780.             );
  781.         $childInstituteMembers $qb->getQuery()->getResult();
  782.         if (is_array($childInstituteMembers)) {
  783.             foreach ($childInstituteMembers as $member) {
  784.                 $instUrl 'https://oceanexpert.org/institute/' $member['idInst'];
  785.                 $instName $member['instName'];
  786.                 $OIHData['member'][] = array(
  787.                     'member' => array(
  788.                         '@type' => 'Person',
  789.                         '@id' => 'https://oceanexpert.org/expert/' $member['idInd'],
  790.                         'url' => 'https://oceanexpert.org/expert/' $member['idInd'],
  791.                         'name' => $member['fname'] . ' ' $member['mname'] . ' ' $member['sname'],
  792.                         'description' => "is member of '$instName' ($instUrl), a child of this organization"
  793.                     )
  794.                 );
  795.             }
  796.         }
  797.         //what page with child members are we showing
  798.         $childMemberLimit $request->query->get('cmlimit');
  799.         if (!in_array($childMemberLimit, array(510"All"))) {
  800.             $childMemberLimit 5;
  801.         }
  802.         if ($childMemberLimit == "All") {
  803.             $childMemberLimit 9999;
  804.         }
  805.         $childMembers $paginator->paginate(
  806.             $childInstituteMembers,
  807.             $request->query->getInt('childmembers'1),
  808.             $childMemberLimit,
  809.             array(
  810.                 'pageParameterName' => 'childmembers',
  811.                 'sortDirectionParameterName' => 'dir'
  812.             )
  813.         );
  814.         //get the child institutes
  815.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  816.         $qb->add(
  817.             'select',
  818.             'ins.idInst,
  819.             ins.instTypeId, 
  820.             ins.instName, 
  821.             ins.instNameEng, 
  822.             ins.instAddress, 
  823.             ins.addr2, 
  824.             ins.city, 
  825.             ins.state, 
  826.             ins.postcode, 
  827.             ins.countryCode, 
  828.             ins.acronym, 
  829.             ins.instLogo, 
  830.             ins.instTel, 
  831.             ins.instFax, 
  832.             ins.instEmail, 
  833.             ins.instUrl, 
  834.             ins.edmoCode,
  835.             ins.instRegion, 
  836.             ins.activities, 
  837.             ins.popularity, 
  838.             ins.activated, 
  839.             ins.fDateEntered, 
  840.             ins.lDateUpdated, 
  841.             ins.createdBy, 
  842.             ins.lastEditBy,
  843.             it.insttypeName,
  844.             c.country, 
  845.             c.countryCode as instCountryCode')
  846.             ->add(
  847.                 'from',
  848.                 'OceanExpertBundle:Institutions ins')
  849.             ->leftJoin(
  850.                 'OceanExpertBundle:Insttypes',
  851.                 'it',
  852.                 'WITH',
  853.                 'it.idInsttype = ins.instTypeId')
  854.             ->leftJoin(
  855.                 'OceanExpertBundle:Countries',
  856.                 'c',
  857.                 'WITH',
  858.                 'c.idCountry = ins.countryCode')
  859.             ->where('ins.parentId = :instId')
  860.             ->andWhere('ins.activated = 1')
  861.             ->orderBy(
  862.                 'ins.instName',
  863.                 'ASC')
  864.             ->setParameter(
  865.                 'instId',
  866.                 $instId);
  867.         $childInstitutes $qb->getQuery()->getResult();
  868.         if (is_array($childInstitutes)) {
  869.             foreach ($childInstitutes as $member) {
  870.                 $OIHData['member'][] = array(
  871.                     'member' => array(
  872.                         '@type' => 'Organization',
  873.                         '@id' => 'https://oceanexpert.org/institute/' $member['idInst'],
  874.                         'url' => 'https://oceanexpert.org/institute/' $member['idInst'],
  875.                         'name' => $member['instName']
  876.                     )
  877.                 );
  878.             }
  879.         }
  880.         //what page with child institutes are we showing
  881.         $childInstitutes = array();
  882.         $childInstituteLimit $request->query->get('cilimit');
  883.         if (!in_array($childInstituteLimit, array(510"All"))) {
  884.             $childInstituteLimit 5;
  885.         }
  886.         if ($childInstituteLimit == "All") {
  887.             $childInstituteLimit 9999;
  888.         }
  889.         $childInstitutes $paginator->paginate(
  890.             $childInstitutes,
  891.             $request->query->getInt('childinstitutes'1),
  892.             $childInstituteLimit,
  893.             array(
  894.                 'pageParameterName' => 'childinstitutes',
  895.                 'sortDirectionParameterName' => 'dir'
  896.             )
  897.         );
  898.         //get the sea regions of the institute
  899.         $repository $this->getDoctrine()
  900.             ->getRepository('OceanExpertBundle:Searegions');
  901.         $institute['seaRegions'] = $repository->createQueryBuilder('r')
  902.             ->select(
  903.                 'r.idSearegion, 
  904.                 r.name')
  905.             ->where('r.idSearegion IN (:seaRegions)')
  906.             ->setParameter(
  907.                 'seaRegions',
  908.                 array_values(
  909.                     explode(
  910.                         ',',
  911.                         $institute['instRegion']
  912.                     )
  913.                 )
  914.             )
  915.             ->getQuery()->getResult();
  916.         //get the list of all available sea regions
  917.         $institute['availableSeaRegions'] = $this->getDoctrine()
  918.             ->getRepository('OceanExpertBundle:Searegions')
  919.             ->createQueryBuilder('e')
  920.             ->select('e.idSearegion as id, e.name')
  921.             ->orderBy('e.name')
  922.             ->getQuery()
  923.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  924.         //what are we trying to do here - edit or view
  925.         $routeName $request->get('_route');
  926.         if ($routeName == 'edit_institute'
  927.             || $routeName == 'edit_institution'
  928.         ) {
  929.             $securityContext $this->container->get('security.authorization_checker');
  930.             if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  931.                 $user $this->container->get('security.token_storage')->getToken()->getUser();
  932.                 //let's check if the logged-in user has a 'real' profile
  933.                 //the mandatory profile fields are all filled and the expert is active
  934.                 $em $this->getDoctrine()->getManager();
  935.                 $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  936.                 if (!SecurityController::checkUserProfile($em$userId)) {
  937.                     return $this->redirect(
  938.                         $this->generateUrl(
  939.                             'user_profile_edit'
  940.                         )
  941.                     );
  942.                 }
  943.                 if ($user->getId() == $institute['createdBy']
  944.                     || $this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')
  945.                 ) {
  946.                     if (!empty($data $request->request->all())) {
  947.                         $em $this->getDoctrine()->getManager();
  948.                         if ($instId !== 0) {
  949.                             $inst $em
  950.                                 ->getRepository('OceanExpertBundle:Institutions')
  951.                                 ->findOneBy(
  952.                                     array(
  953.                                         'idInst' => $instId
  954.                                     )
  955.                                 );
  956.                         } else {
  957.                             $inst = new Institutions();
  958.                         }
  959.                         if ($inst) {
  960.                             if (trim($data['iedmo']) == ''
  961.                                 || !is_numeric($data['iedmo'])
  962.                             ) {
  963.                                 $edmo_code 0;
  964.                             } else {
  965.                                 $edmo_code $data['iedmo'];
  966.                             }
  967.                             $inst->setInstTypeId($data['instType']);
  968.                             $inst->setInstName($data['instName']);
  969.                             $inst->setInstNameEng($data['instNameEng']);
  970.                             $inst->setInstAddress($data['iaddress']);
  971.                             $inst->setAddr2($data['iaddressline2']);
  972.                             $inst->setCity($data['icity']);
  973.                             $inst->setState($data['istate']);
  974.                             $inst->setPostcode($data['ipostcode']);
  975.                             $inst->setCountryCode($data['instCountryCode']);
  976.                             $inst->setAcronym($data['iacronym']);
  977.                             $inst->setInstTel($data['icontact']);
  978.                             $inst->setInstFax($data['ifax']);
  979.                             $inst->setInstEmail($data['iemail']);
  980.                             $inst->setInstUrl($data['iwebsite']);
  981.                             $inst->setEdmoCode($edmo_code);
  982.                             $inst->setActivities($data['iactivity']);
  983.                             if ($data['parentinstitute'] === '' ) {
  984.                                 $data['parentinstitute'] = 0;
  985.                             }
  986.                             $inst->setParentId($data['parentinstitute']);
  987.                             if (isset($data['studyregion'])
  988.                                 && count($data['studyregion']) > 0
  989.                             ) {
  990.                                 $inst->setInstRegion(implode(','$data['studyregion']));
  991.                             } else {
  992.                                 $inst->setInstRegion('');
  993.                             }
  994.                             $inst->setLDateUpdated(new DateTime("now"));
  995.                             $inst->setLastEditBy($user->getId());
  996.                             if (null != $request->files->get('instLogo')) {
  997.                                 $file $request->files->get('instLogo');
  998.                                 $filename 'instituteLogo.jpg';
  999.                                 $directory 'uploads/institutes/' $instId '/';
  1000.                                 $file->move($directory$filename);
  1001.                                 $inst->setInstLogo($filename);
  1002.                             } else {
  1003.                                 if (isset($data['textLogo'])
  1004.                                     && $data['textLogo'] === ''
  1005.                                 ) {
  1006.                                     $inst->setInstLogo('');
  1007.                                     $filename 'uploads/institutes/' $inst->getIdInst() . '/instituteLogo.jpg';
  1008.                                     if (file_exists($filename)) {
  1009.                                         unlink($filename);
  1010.                                     }
  1011.                                 }
  1012.                             }
  1013.                             $em->persist($inst);
  1014.                             $em->flush();
  1015.                             return $this->redirect(
  1016.                                 $this->generateUrl(
  1017.                                     'edit_institute_success',
  1018.                                     array(
  1019.                                         'idInst' => $instId
  1020.                                     )
  1021.                                 )
  1022.                             );
  1023.                         } else {
  1024.                             return $this->render(
  1025.                                 'Exception/error.html.twig',
  1026.                                 array(
  1027.                                     'message' => 'you are trying to edit an institute with id "' $instId '" that does not exist'
  1028.                                 )
  1029.                             );
  1030.                         }
  1031.                     }
  1032.                     $countryInstitutes $this->getDoctrine()
  1033.                         ->getRepository('OceanExpertBundle:Institutions')
  1034.                         ->findBy(
  1035.                             array(
  1036.                                 'countryCode' => $institute['countryCode']
  1037.                             ),
  1038.                             array(
  1039.                                 'instName' => 'ASC'
  1040.                             )
  1041.                         );
  1042.                     return $this->render(
  1043.                         'Institute/editInstitute.html.twig',
  1044.                         array(
  1045.                             'institute' => $institute,
  1046.                             'instituteTypes' => $instituteTypes,
  1047.                             'countries' => $countries,
  1048.                             'countryInstitutes' => $countryInstitutes,
  1049.                         )
  1050.                     );
  1051.                 } else {
  1052.                     return $this->render(
  1053.                         'Exception/error.html.twig',
  1054.                         array(
  1055.                             'message' => 'you do not have sufficient rights to edit the institute with id "' $instId '"'
  1056.                         )
  1057.                     );
  1058.                 }
  1059.             } else {
  1060.                 return $this->render(
  1061.                     'Exception/error.html.twig',
  1062.                     array(
  1063.                         'message' => 'you need to log in to edit the institute with id "' $instId '"'
  1064.                     )
  1065.                 );
  1066.             }
  1067.         }
  1068.         //show the institute info
  1069.         $addressFormatRepository = new AddressFormatRepository();
  1070.         $countryRepository = new CountryRepository();
  1071.         $subdivisionRepository = new SubdivisionRepository();
  1072.         $formatter = new DefaultFormatter($addressFormatRepository$countryRepository$subdivisionRepository);
  1073.         $address = new Address();
  1074.         $address $address
  1075.             ->withCountryCode($institute['instCountryCode'])
  1076.             ->withAdministrativeArea($institute['state'])
  1077.             ->withLocality($institute['city'])
  1078.             ->withPostalCode($institute['postcode'])
  1079.             ->withAddressLine2($institute['addr2'])
  1080.             ->withAddressLine1($institute['instAddress']);
  1081.         $institute['address'] = $formatter->format($address);
  1082.         //make the address
  1083.         $streetAddress $institute['instAddress'];
  1084.         if (trim($institute['addr2']) != '') {
  1085.             $streetAddress .= ', ' trim($institute['addr2']);
  1086.         }
  1087.         $address $streetAddress ', ' .
  1088.             $institute['postcode'] . ' ' $institute['city'] . ', ' .
  1089.             $institute['country'];
  1090.         $OIHData['location'] = array(
  1091.             '@type' => 'Place',
  1092.             'address' => trim($address)
  1093.         );
  1094.         $OIHData['address'] = array(
  1095.             '@type' => 'PostalAddress',
  1096.             'addressLocality' => $institute['city'] . ', ' $institute['country'],
  1097.             'postalCode' => $institute['postcode'],
  1098.             'streetAddress' => $streetAddress
  1099.         );
  1100.         /*
  1101.         $OIHData['location'] = array(
  1102.             '@type' => 'Place',
  1103.             'address' => array(
  1104.                 '@type' => 'PostalAddress',
  1105.                 'addressLocality' => $institute['city'] . ', ' . $institute['country'],
  1106.                 'postalCode' => $institute['postcode'],
  1107.                 'streetAddress' => $streetAddress
  1108.             )
  1109.         );
  1110.         */
  1111.         if ($institute['createdBy']) {
  1112.             $institute['createdId'] = $institute['createdBy'];
  1113.             $institute['createdBy'] = $this->getUserById($institute['createdBy']);
  1114.         }
  1115.         if ($institute['lastEditBy']) {
  1116.             $institute['lastEditId'] = $institute['lastEditBy'];
  1117.             $institute['lastEditBy'] = $this->getUserById($institute['lastEditBy']);
  1118.         }
  1119.         $limits = array(
  1120.             'memberlimit' => array(
  1121.                 'options' => array(
  1122.                     '5' => 5,
  1123.                     '10' => 10,
  1124.                     '9999' => 'All'
  1125.                 ),
  1126.                 'selected' => $memberLimit
  1127.             ),
  1128.             'childmemberlimit' => array(
  1129.                 'options' => array(
  1130.                     '5' => 5,
  1131.                     '10' => 10,
  1132.                     '9999' => 'All'
  1133.                 ),
  1134.                 'selected' => $childMemberLimit
  1135.             ),
  1136.             'childinstitutelimit' => array(
  1137.                 'options' => array(
  1138.                     '5' => 5,
  1139.                     '10' => 10,
  1140.                     '9999' => 'All'
  1141.                 ),
  1142.                 'selected' => $childInstituteLimit
  1143.             ),
  1144.         );
  1145.         $OIHData json_encode(
  1146.             $OIHData,
  1147.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES JSON_NUMERIC_CHECK
  1148.         );
  1149.         return $this->render(
  1150.             'Institute/viewInstitute.html.twig',
  1151.             array(
  1152.                 'OIHData' => $OIHData,
  1153.                 'institute' => $institute,
  1154.                 'members' => $members,
  1155.                 'limits' => $limits,
  1156.                 'childmembers' => $childMembers,
  1157.                 'childinstitutes' => $childInstitutes
  1158.             )
  1159.         );
  1160.     }
  1161.     public function getUserById($id '')
  1162.     {
  1163.         $updatedBy $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($id);
  1164.         if ($updatedBy) {
  1165.             $name $updatedBy->getfname() . " " $updatedBy->getsname();
  1166.         } else {
  1167.             $name false;
  1168.         }
  1169.         return $name;
  1170.     }
  1171.     public function activateInstituteAction($instituteId)
  1172.     {
  1173.         $securityContext $this->get('security.authorization_checker');
  1174.         if ($securityContext->isGranted('ROLE_GLOBAL_EDITOR')) {
  1175.             if ($instituteId) {
  1176.                 $institute $this->getDoctrine()
  1177.                     ->getRepository('OceanExpertBundle:Institutions')
  1178.                     ->findOneByIdInst($instituteId);
  1179.                 if ($institute) {
  1180.                     $institute->setActivated(1);
  1181.                     $institute->setLDateUpdated(new DateTime("now"));
  1182.                     $institute->setLastEditBy(
  1183.                         $this->get('security.token_storage')
  1184.                             ->getToken()
  1185.                             ->getUser()
  1186.                             ->getId()
  1187.                     );
  1188.                     $em $this->getDoctrine()->getManager();
  1189.                     $em->persist($institute); //marks object to be saved in the next transaction.
  1190.                     $em->flush(); //performs all saves and transactions.
  1191.                 }
  1192.                 return new JsonResponse(
  1193.                     array(
  1194.                         'status' => true,
  1195.                         'msg' => 'Record updated successfully'
  1196.                     )
  1197.                 );
  1198.             } else {
  1199.                 return new Response('Cannot update record.');
  1200.             }
  1201.         }
  1202.         return new Response('Cannot update record.');
  1203.     }
  1204.     public function deactivateInstituteAction($instituteId)
  1205.     {
  1206.         $securityContext $this->get('security.authorization_checker');
  1207.         if ($securityContext->isGranted('ROLE_GLOBAL_EDITOR')) {
  1208.             if ($instituteId) {
  1209.                 $institute $this->getDoctrine()
  1210.                     ->getRepository('OceanExpertBundle:Institutions')
  1211.                     ->findOneByIdInst($instituteId);
  1212.                 if ($institute) {
  1213.                     $institute->setActivated(0);
  1214.                     $institute->setLDateUpdated(new DateTime("now"));
  1215.                     $institute->setLastEditBy(
  1216.                         $this->get('security.token_storage')
  1217.                             ->getToken()
  1218.                             ->getUser()
  1219.                             ->getId());
  1220.                     $em $this->getDoctrine()->getManager();
  1221.                     $em->persist($institute); //marks object to be saved in the next transaction.
  1222.                     $em->flush(); //performs all saves and transactions.
  1223.                 }
  1224.                 return new JsonResponse(
  1225.                     array(
  1226.                         'status' => true,
  1227.                         'msg' => 'updated successfully'
  1228.                     )
  1229.                 );
  1230.             } else {
  1231.                 return new Response('Cannot update record.');
  1232.             }
  1233.         }
  1234.         return new Response('Cannot update record.');
  1235.     }
  1236.     /**
  1237.      * show this page when the institute has been edited successfully
  1238.      *
  1239.      * @param int $idInst
  1240.      *
  1241.      * @return Response
  1242.      */
  1243.     public function editInstituteSuccessAction($idInst)
  1244.     {
  1245.         return $this->render(
  1246.             'Institute/editInstituteSuccess.html.twig',
  1247.             array(
  1248.                 'idInst' => $idInst
  1249.             )
  1250.         );
  1251.     }
  1252.     /**
  1253.      * @param Request $request
  1254.      *
  1255.      * @return JsonResponse
  1256.      */
  1257.     public function getInstitutionsAjaxAction(Request $request)
  1258.     {
  1259.         if (!null == $request->query->get('q')) {
  1260.             $inst $request->query->get('q');
  1261.             $em $this->getDoctrine()->getManager();
  1262.             $qb $em->createQueryBuilder();
  1263.             $qb->select('i.idInst as id,i.instName');
  1264.             $qb->from('OceanExpertBundle:Institutions''i');
  1265.             $qb->where('i.instName like :name OR i.acronym like :name');
  1266.             if (!null == $request->query->get('instId')) {
  1267.                 $qb->andWhere('i.idInst != :instId');
  1268.                 $qb->setParameter('instId'$request->query->get('instId'));
  1269.             }
  1270.             $qb->andWhere('i.activated = 1');
  1271.             $qb->setParameter('name''%' $inst '%');
  1272.             $qb->setParameter('name''%' $inst '%');
  1273.             $qb->orderBy('i.instName''ASC');
  1274.             $institutes $qb->getQuery()->getResult();
  1275.             $data = array();
  1276.             if ($institutes) {
  1277.                 $data = array(
  1278.                     'incomplete_results' => false,
  1279.                     'items' => $institutes,
  1280.                     'total_count' => count($institutes),
  1281.                 );
  1282.             } else {
  1283.                 $data = array(
  1284.                     'incomplete_results' => false,
  1285.                     'items' => array(),
  1286.                     'total_count' => 0,
  1287.                 );
  1288.             }
  1289.             return new JsonResponse($data);
  1290.         }
  1291.         return new JsonResponse();
  1292.     }
  1293. }