src/OceanExpertBundle/Controller/InstitutionController.php line 1247

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.         //get the list of all available sea regions
  551.         $availableSeaRegions $this->getDoctrine()
  552.             ->getRepository('OceanExpertBundle:Searegions')
  553.             ->createQueryBuilder('e')
  554.             ->select('e.idSearegion as id, e.name')
  555.             ->orderBy('e.name')
  556.             ->getQuery()
  557.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  558.         if ((!isset($institute)
  559.             || !$institute)
  560.             && $instId !== 0
  561.         ) {
  562.             return $this->render(
  563.                 'Exception/error.html.twig',
  564.                 array(
  565.                     'message' => 'the institute with id "' $instId '" does not exist or has not been approved by an OceanExpert editor (yet)'
  566.                 )
  567.             );
  568.         } elseif ($instId == 0) {
  569.             //this is a new institute
  570.             $institute = array(
  571.                 'idInst' => 0,
  572.                 'instName' => '',
  573.                 'instNameEng' => '',
  574.                 'instAddress' => '',
  575.                 'addr2' => '',
  576.                 'city' => '',
  577.                 'state' => '',
  578.                 'postcode' => '',
  579.                 'countryCode' => '',
  580.                 'acronym' => '',
  581.                 'instLogo' => '',
  582.                 'instTel' => '',
  583.                 'instFax' => '',
  584.                 'instEmail' => '',
  585.                 'instUrl' => '',
  586.                 'edmoCode' => 0,
  587.                 'instRegion' => '',
  588.                 'activities' => '',
  589.                 'country' => '',
  590.                 'instTypeId' => '',
  591.                 'parentIdInst' => '',
  592.                 'parentInstName' => '',
  593.             );
  594.             $securityContext $this->get('security.authorization_checker');
  595.             if (($request->request->get('instName') === ''
  596.                 || null === $request->request->get('instName'))
  597.                 && $securityContext->isGranted('IS_AUTHENTICATED_FULLY')
  598.             ) {
  599.                 return $this->render(
  600.                     'Institute/editInstitute.html.twig',
  601.                     array(
  602.                         'institute' => $institute,
  603.                         'instituteTypes' => $instituteTypes,
  604.                         'countries' => $countries,
  605.                         'countryInstitutes' => array(),
  606.                         'availableSeaRegions' => $availableSeaRegions
  607.                     )
  608.                 );
  609.             } elseif ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  610.                 //create a new institute
  611.                 $institute = new Institutions();
  612.                 $institute->setInstName($request->request->get('instName'));
  613.                 $institute->setInstNameEng($request->request->get('instNameEng'));
  614.                 $institute->setInstAddress($request->request->get('iaddress'));
  615.                 $institute->setAddr2($request->request->get('iaddressline2'));
  616.                 $institute->setCity($request->request->get('icity'));
  617.                 $institute->setState($request->request->get('istate'));
  618.                 $institute->setPostcode($request->request->get('ipostcode'));
  619.                 $institute->setCountryCode($request->request->get('instCountryCode'));
  620.                 $institute->setInstTel($request->request->get('icontact'));
  621.                 $institute->setInstFax($request->request->get('ifax'));
  622.                 $institute->setInstEmail($request->request->get('iemail'));
  623.                 $institute->setInstUrl($request->request->get('iwebsite'));
  624.                 $institute->setEdmoCode($request->request->get('iedmo'));
  625.                 $institute->setInstRegion($request->request->get('isearegion'));
  626.                 $institute->setInstTypeId($request->request->get('instType'));
  627.                 $institute->setParentId($request->request->get('parentinstitute'));
  628.                 $institute->setActivities($request->request->get('iactivity'));
  629.                 if (null == $institute->getParentId()) {
  630.                     $institute->setParentId(0);
  631.                 }
  632.                 $institute->setPopularity(0);
  633.                 $institute->setActivated(0);
  634.                 $institute->setFDateEntered(new DateTime('now'));
  635.                 $institute->setLDateUpdated(new DateTime('now'));
  636.                 $institute->setCreatedBy($this->get('security.token_storage')->getToken()->getUser()->getId());
  637.                 $institute->setLastEditBy($this->get('security.token_storage')->getToken()->getUser()->getId());
  638.                 $institute->setInstLogo($request->request->get('instLogo'));
  639.                 $institute->setInstTypeId($request->request->get('instType'));
  640.                 $em $this->getDoctrine()->getManager();
  641.                 $em->persist($institute);
  642.                 $em->flush();
  643.                 $url $this->generateUrl(
  644.                     'view_institute',
  645.                     array(
  646.                         'instId' => $institute->getIdInst()
  647.                     )
  648.                 );
  649.                 return new RedirectResponse($url);
  650.             } else {
  651.                 return $this->render(
  652.                     'Exception/error.html.twig',
  653.                     array(
  654.                         'message' => 'You need to be logged in to create a new institute.'
  655.                     )
  656.                 );
  657.             }
  658.         } else {
  659.             $institute $institute[0];
  660.             $OIHData['name'] = $institute['instName'];
  661.             if (isset($institute['instUrl'])
  662.                 && $institute['instUrl'] != ''
  663.             ) {
  664.                 $OIHData['url'] = $institute['instUrl'];
  665.             } else {
  666.                 $OIHData['url'] = "https://oceanexpert.org/institute/$instId";
  667.             }
  668.             if (isset($institute['instTel'])
  669.                 && $institute['instTel'] != ''
  670.             ) {
  671.                 $OIHData['telephone'] = $institute['instTel'];
  672.             }
  673.             if (isset($institute['parentIdInst'])
  674.                 && $institute['parentIdInst'] !== ''
  675.                 && $institute['parentIdInst'] !== 0
  676.             ) {
  677.                 $OIHData['memberOf'] = array(
  678.                     '@type' => 'Organization',
  679.                     '@id' => 'https://oceanexpert.org/institute/' $institute['parentIdInst'],
  680.                     'url' => 'https://oceanexpert.org/institute/' $institute['parentIdInst'],
  681.                     'name' => $institute['parentInstName']
  682.                 );
  683.             }
  684.         }
  685.         //get the members of the institute
  686.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  687.         $qb->add(
  688.             'select',
  689.             'i.idInd,
  690.             i.fname,
  691.             i.mname,
  692.             i.sname,
  693.             i.jobtitle,
  694.             i.deceased,
  695.             i.retired, 
  696.             i.qualityChecked')
  697.             ->add(
  698.                 'from',
  699.                 'OceanExpertBundle:Indiv i')
  700.             ->leftJoin(
  701.                 'OceanExpertBundle:IndivInstitution',
  702.                 'iins',
  703.                 'WITH',
  704.                 'iins.idInd = i.idInd')
  705.             ->leftJoin(
  706.                 'OceanExpertBundle:Institutions',
  707.                 'ins',
  708.                 'WITH',
  709.                 'ins.idInst = iins.idInst')
  710.             ->where('ins.idInst = :instId')
  711.             ->andWhere('i.status = 1')
  712.             ->orderBy(
  713.                 'i.sname',
  714.                 'ASC')
  715.             ->setParameter(
  716.                 'instId',
  717.                 $instId
  718.             );
  719.         $instituteMembers $qb->getQuery()->getResult();
  720.         if (is_array($instituteMembers)) {
  721.             foreach ($instituteMembers as $member) {
  722.                 $OIHData['member'][] = array(
  723.                     'member' => array(
  724.                         '@type' => 'Person',
  725.                         '@id' => 'https://oceanexpert.org/expert/' $member['idInd'],
  726.                         'url' => 'https://oceanexpert.org/expert/' $member['idInd'],
  727.                         'name' => $member['fname'] . ' ' $member['mname'] . ' ' $member['sname']
  728.                     )
  729.                 );
  730.             }
  731.         }
  732.         //what page with members are we showing
  733.         $paginator $this->get('knp_paginator');
  734.         $memberLimit $request->query->getInt('mlimit'5);
  735.         $memberPage $request->query->getInt('members'1);
  736.         if (!in_array($memberLimit, array(510"All"))) {
  737.             $memberLimit 5;
  738.             $memberPage 1;
  739.         }
  740.         if ($memberLimit == "All") {
  741.             $memberLimit 9999;
  742.             $memberPage 1;
  743.         }
  744.         $members $paginator->paginate(
  745.             $instituteMembers,
  746.             $memberPage,
  747.             $memberLimit,
  748.             array(
  749.                 'pageParameterName' => 'members',
  750.                 'sortDirectionParameterName' => 'dir'
  751.             )
  752.         );
  753.         //get the members of the child institutes
  754.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  755.         $qb->add(
  756.             'select',
  757.             'i.idInd,
  758.             i.fname,
  759.             i.mname,
  760.             i.sname,
  761.             i.jobtitle,
  762.             i.deceased,
  763.             i.retired, 
  764.             i.qualityChecked, 
  765.             ins.idInst,
  766.             ins.instName')
  767.             ->add(
  768.                 'from',
  769.                 'OceanExpertBundle:Indiv i')
  770.             ->leftJoin(
  771.                 'OceanExpertBundle:IndivInstitution',
  772.                 'iins',
  773.                 'WITH',
  774.                 'iins.idInd = i.idInd')
  775.             ->leftJoin(
  776.                 'OceanExpertBundle:Institutions',
  777.                 'ins',
  778.                 'WITH',
  779.                 'ins.idInst = iins.idInst')
  780.             ->where('ins.parentId = :instId')
  781.             ->andWhere('i.status = 1')
  782.             ->andWhere('ins.activated = 1')
  783.             ->orderBy(
  784.                 'i.sname',
  785.                 'ASC')
  786.             ->setParameter(
  787.                 'instId',
  788.                 $instId
  789.             );
  790.         $childInstituteMembers $qb->getQuery()->getResult();
  791.         if (is_array($childInstituteMembers)) {
  792.             foreach ($childInstituteMembers as $member) {
  793.                 $instUrl 'https://oceanexpert.org/institute/' $member['idInst'];
  794.                 $instName $member['instName'];
  795.                 $OIHData['member'][] = array(
  796.                     'member' => array(
  797.                         '@type' => 'Person',
  798.                         '@id' => 'https://oceanexpert.org/expert/' $member['idInd'],
  799.                         'url' => 'https://oceanexpert.org/expert/' $member['idInd'],
  800.                         'name' => $member['fname'] . ' ' $member['mname'] . ' ' $member['sname'],
  801.                         'description' => "is member of '$instName' ($instUrl), a child of this organization"
  802.                     )
  803.                 );
  804.             }
  805.         }
  806.         //what page with child members are we showing
  807.         $childMemberLimit $request->query->get('cmlimit');
  808.         if (!in_array($childMemberLimit, array(510"All"))) {
  809.             $childMemberLimit 5;
  810.         }
  811.         if ($childMemberLimit == "All") {
  812.             $childMemberLimit 9999;
  813.         }
  814.         $childMembers $paginator->paginate(
  815.             $childInstituteMembers,
  816.             $request->query->getInt('childmembers'1),
  817.             $childMemberLimit,
  818.             array(
  819.                 'pageParameterName' => 'childmembers',
  820.                 'sortDirectionParameterName' => 'dir'
  821.             )
  822.         );
  823.         //get the child institutes
  824.         $qb $this->getDoctrine()->getManager()->createQueryBuilder();
  825.         $qb->add(
  826.             'select',
  827.             'ins.idInst,
  828.             ins.instTypeId, 
  829.             ins.instName, 
  830.             ins.instNameEng, 
  831.             ins.instAddress, 
  832.             ins.addr2, 
  833.             ins.city, 
  834.             ins.state, 
  835.             ins.postcode, 
  836.             ins.countryCode, 
  837.             ins.acronym, 
  838.             ins.instLogo, 
  839.             ins.instTel, 
  840.             ins.instFax, 
  841.             ins.instEmail, 
  842.             ins.instUrl, 
  843.             ins.edmoCode,
  844.             ins.instRegion, 
  845.             ins.activities, 
  846.             ins.popularity, 
  847.             ins.activated, 
  848.             ins.fDateEntered, 
  849.             ins.lDateUpdated, 
  850.             ins.createdBy, 
  851.             ins.lastEditBy,
  852.             it.insttypeName,
  853.             c.country, 
  854.             c.countryCode as instCountryCode')
  855.             ->add(
  856.                 'from',
  857.                 'OceanExpertBundle:Institutions ins')
  858.             ->leftJoin(
  859.                 'OceanExpertBundle:Insttypes',
  860.                 'it',
  861.                 'WITH',
  862.                 'it.idInsttype = ins.instTypeId')
  863.             ->leftJoin(
  864.                 'OceanExpertBundle:Countries',
  865.                 'c',
  866.                 'WITH',
  867.                 'c.idCountry = ins.countryCode')
  868.             ->where('ins.parentId = :instId')
  869.             ->andWhere('ins.activated = 1')
  870.             ->orderBy(
  871.                 'ins.instName',
  872.                 'ASC')
  873.             ->setParameter(
  874.                 'instId',
  875.                 $instId);
  876.         $childInstitutes $qb->getQuery()->getResult();
  877.         if (is_array($childInstitutes)) {
  878.             foreach ($childInstitutes as $member) {
  879.                 $OIHData['member'][] = array(
  880.                     'member' => array(
  881.                         '@type' => 'Organization',
  882.                         '@id' => 'https://oceanexpert.org/institute/' $member['idInst'],
  883.                         'url' => 'https://oceanexpert.org/institute/' $member['idInst'],
  884.                         'name' => $member['instName']
  885.                     )
  886.                 );
  887.             }
  888.         }
  889.         //what page with child institutes are we showing
  890.         $childInstitutes = array();
  891.         $childInstituteLimit $request->query->get('cilimit');
  892.         if (!in_array($childInstituteLimit, array(510"All"))) {
  893.             $childInstituteLimit 5;
  894.         }
  895.         if ($childInstituteLimit == "All") {
  896.             $childInstituteLimit 9999;
  897.         }
  898.         $childInstitutes $paginator->paginate(
  899.             $childInstitutes,
  900.             $request->query->getInt('childinstitutes'1),
  901.             $childInstituteLimit,
  902.             array(
  903.                 'pageParameterName' => 'childinstitutes',
  904.                 'sortDirectionParameterName' => 'dir'
  905.             )
  906.         );
  907.         //get the sea regions of the institute
  908.         $repository $this->getDoctrine()
  909.             ->getRepository('OceanExpertBundle:Searegions');
  910.         $institute['seaRegions'] = $repository->createQueryBuilder('r')
  911.             ->select(
  912.                 'r.idSearegion, 
  913.                 r.name')
  914.             ->where('r.idSearegion IN (:seaRegions)')
  915.             ->setParameter(
  916.                 'seaRegions',
  917.                 array_values(
  918.                     explode(
  919.                         ',',
  920.                         $institute['instRegion']
  921.                     )
  922.                 )
  923.             )
  924.             ->getQuery()->getResult();
  925.         //what are we trying to do here - edit or view
  926.         $routeName $request->get('_route');
  927.         if ($routeName == 'edit_institute'
  928.             || $routeName == 'edit_institution'
  929.         ) {
  930.             $securityContext $this->container->get('security.authorization_checker');
  931.             if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
  932.                 $user $this->container->get('security.token_storage')->getToken()->getUser();
  933.                 //let's check if the logged-in user has a 'real' profile
  934.                 //the mandatory profile fields are all filled and the expert is active
  935.                 $em $this->getDoctrine()->getManager();
  936.                 $userId $this->get('security.token_storage')->getToken()->getUser()->getId();
  937.                 if (!SecurityController::checkUserProfile($em$userId)) {
  938.                     return $this->redirect(
  939.                         $this->generateUrl(
  940.                             'user_profile_edit'
  941.                         )
  942.                     );
  943.                 }
  944.                 if ($user->getId() == $institute['createdBy']
  945.                     || $this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')
  946.                 ) {
  947.                     if (!empty($data $request->request->all())) {
  948.                         $em $this->getDoctrine()->getManager();
  949.                         if ($instId !== 0) {
  950.                             $inst $em
  951.                                 ->getRepository('OceanExpertBundle:Institutions')
  952.                                 ->findOneBy(
  953.                                     array(
  954.                                         'idInst' => $instId
  955.                                     )
  956.                                 );
  957.                         } else {
  958.                             $inst = new Institutions();
  959.                         }
  960.                         if ($inst) {
  961.                             if (trim($data['iedmo']) == ''
  962.                                 || !is_numeric($data['iedmo'])
  963.                             ) {
  964.                                 $edmo_code 0;
  965.                             } else {
  966.                                 $edmo_code $data['iedmo'];
  967.                             }
  968.                             $inst->setInstTypeId($data['instType']);
  969.                             $inst->setInstName($data['instName']);
  970.                             $inst->setInstNameEng($data['instNameEng']);
  971.                             $inst->setInstAddress($data['iaddress']);
  972.                             $inst->setAddr2($data['iaddressline2']);
  973.                             $inst->setCity($data['icity']);
  974.                             $inst->setState($data['istate']);
  975.                             $inst->setPostcode($data['ipostcode']);
  976.                             $inst->setCountryCode($data['instCountryCode']);
  977.                             $inst->setAcronym($data['iacronym']);
  978.                             $inst->setInstTel($data['icontact']);
  979.                             $inst->setInstFax($data['ifax']);
  980.                             $inst->setInstEmail($data['iemail']);
  981.                             $inst->setInstUrl($data['iwebsite']);
  982.                             $inst->setEdmoCode($edmo_code);
  983.                             $inst->setActivities($data['iactivity']);
  984.                             if ($data['parentinstitute'] === '' ) {
  985.                                 $data['parentinstitute'] = 0;
  986.                             }
  987.                             $inst->setParentId($data['parentinstitute']);
  988.                             if (isset($data['studyregion'])
  989.                                 && count($data['studyregion']) > 0
  990.                             ) {
  991.                                 $inst->setInstRegion(implode(','$data['studyregion']));
  992.                             } else {
  993.                                 $inst->setInstRegion('');
  994.                             }
  995.                             $inst->setLDateUpdated(new DateTime("now"));
  996.                             $inst->setLastEditBy($user->getId());
  997.                             if (null != $request->files->get('instLogo')) {
  998.                                 $file $request->files->get('instLogo');
  999.                                 $filename 'instituteLogo.jpg';
  1000.                                 $directory 'uploads/institutes/' $instId '/';
  1001.                                 $file->move($directory$filename);
  1002.                                 $inst->setInstLogo($filename);
  1003.                             } else {
  1004.                                 if (isset($data['textLogo'])
  1005.                                     && $data['textLogo'] === ''
  1006.                                 ) {
  1007.                                     $inst->setInstLogo('');
  1008.                                     $filename 'uploads/institutes/' $inst->getIdInst() . '/instituteLogo.jpg';
  1009.                                     if (file_exists($filename)) {
  1010.                                         unlink($filename);
  1011.                                     }
  1012.                                 }
  1013.                             }
  1014.                             $em->persist($inst);
  1015.                             $em->flush();
  1016.                             return $this->redirect(
  1017.                                 $this->generateUrl(
  1018.                                     'edit_institute_success',
  1019.                                     array(
  1020.                                         'idInst' => $instId
  1021.                                     )
  1022.                                 )
  1023.                             );
  1024.                         } else {
  1025.                             return $this->render(
  1026.                                 'Exception/error.html.twig',
  1027.                                 array(
  1028.                                     'message' => 'you are trying to edit an institute with id "' $instId '" that does not exist'
  1029.                                 )
  1030.                             );
  1031.                         }
  1032.                     }
  1033.                     $countryInstitutes $this->getDoctrine()
  1034.                         ->getRepository('OceanExpertBundle:Institutions')
  1035.                         ->findBy(
  1036.                             array(
  1037.                                 'countryCode' => $institute['countryCode']
  1038.                             ),
  1039.                             array(
  1040.                                 'instName' => 'ASC'
  1041.                             )
  1042.                         );
  1043.                     return $this->render(
  1044.                         'Institute/editInstitute.html.twig',
  1045.                         array(
  1046.                             'institute' => $institute,
  1047.                             'instituteTypes' => $instituteTypes,
  1048.                             'countries' => $countries,
  1049.                             'countryInstitutes' => $countryInstitutes,
  1050.                             'availableSeaRegions' => $availableSeaRegions
  1051.                         )
  1052.                     );
  1053.                 } else {
  1054.                     return $this->render(
  1055.                         'Exception/error.html.twig',
  1056.                         array(
  1057.                             'message' => 'you do not have sufficient rights to edit the institute with id "' $instId '"'
  1058.                         )
  1059.                     );
  1060.                 }
  1061.             } else {
  1062.                 return $this->render(
  1063.                     'Exception/error.html.twig',
  1064.                     array(
  1065.                         'message' => 'you need to log in to edit the institute with id "' $instId '"'
  1066.                     )
  1067.                 );
  1068.             }
  1069.         }
  1070.         //show the institute info
  1071.         $addressFormatRepository = new AddressFormatRepository();
  1072.         $countryRepository = new CountryRepository();
  1073.         $subdivisionRepository = new SubdivisionRepository();
  1074.         $formatter = new DefaultFormatter($addressFormatRepository$countryRepository$subdivisionRepository);
  1075.         $address = new Address();
  1076.         $address $address
  1077.             ->withCountryCode($institute['instCountryCode'])
  1078.             ->withAdministrativeArea($institute['state'])
  1079.             ->withLocality($institute['city'])
  1080.             ->withPostalCode($institute['postcode'])
  1081.             ->withAddressLine2($institute['addr2'])
  1082.             ->withAddressLine1($institute['instAddress']);
  1083.         $institute['address'] = $formatter->format($address);
  1084.         //make the address
  1085.         $streetAddress $institute['instAddress'];
  1086.         if (trim($institute['addr2']) != '') {
  1087.             $streetAddress .= ', ' trim($institute['addr2']);
  1088.         }
  1089.         $address $streetAddress ', ' .
  1090.             $institute['postcode'] . ' ' $institute['city'] . ', ' .
  1091.             $institute['country'];
  1092.         $OIHData['location'] = array(
  1093.             '@type' => 'Place',
  1094.             'address' => trim($address)
  1095.         );
  1096.         $OIHData['address'] = array(
  1097.             '@type' => 'PostalAddress',
  1098.             'addressLocality' => $institute['city'] . ', ' $institute['country'],
  1099.             'postalCode' => $institute['postcode'],
  1100.             'streetAddress' => $streetAddress
  1101.         );
  1102.         /*
  1103.         $OIHData['location'] = array(
  1104.             '@type' => 'Place',
  1105.             'address' => array(
  1106.                 '@type' => 'PostalAddress',
  1107.                 'addressLocality' => $institute['city'] . ', ' . $institute['country'],
  1108.                 'postalCode' => $institute['postcode'],
  1109.                 'streetAddress' => $streetAddress
  1110.             )
  1111.         );
  1112.         */
  1113.         if ($institute['createdBy']) {
  1114.             $institute['createdId'] = $institute['createdBy'];
  1115.             $institute['createdBy'] = $this->getUserById($institute['createdBy']);
  1116.         }
  1117.         if ($institute['lastEditBy']) {
  1118.             $institute['lastEditId'] = $institute['lastEditBy'];
  1119.             $institute['lastEditBy'] = $this->getUserById($institute['lastEditBy']);
  1120.         }
  1121.         $limits = array(
  1122.             'memberlimit' => array(
  1123.                 'options' => array(
  1124.                     '5' => 5,
  1125.                     '10' => 10,
  1126.                     '9999' => 'All'
  1127.                 ),
  1128.                 'selected' => $memberLimit
  1129.             ),
  1130.             'childmemberlimit' => array(
  1131.                 'options' => array(
  1132.                     '5' => 5,
  1133.                     '10' => 10,
  1134.                     '9999' => 'All'
  1135.                 ),
  1136.                 'selected' => $childMemberLimit
  1137.             ),
  1138.             'childinstitutelimit' => array(
  1139.                 'options' => array(
  1140.                     '5' => 5,
  1141.                     '10' => 10,
  1142.                     '9999' => 'All'
  1143.                 ),
  1144.                 'selected' => $childInstituteLimit
  1145.             ),
  1146.         );
  1147.         $OIHData json_encode(
  1148.             $OIHData,
  1149.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES JSON_NUMERIC_CHECK
  1150.         );
  1151.         return $this->render(
  1152.             'Institute/viewInstitute.html.twig',
  1153.             array(
  1154.                 'OIHData' => $OIHData,
  1155.                 'institute' => $institute,
  1156.                 'members' => $members,
  1157.                 'limits' => $limits,
  1158.                 'childmembers' => $childMembers,
  1159.                 'childinstitutes' => $childInstitutes
  1160.             )
  1161.         );
  1162.     }
  1163.     public function getUserById($id '')
  1164.     {
  1165.         $updatedBy $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($id);
  1166.         if ($updatedBy) {
  1167.             $name $updatedBy->getfname() . " " $updatedBy->getsname();
  1168.         } else {
  1169.             $name false;
  1170.         }
  1171.         return $name;
  1172.     }
  1173.     public function activateInstituteAction($instituteId)
  1174.     {
  1175.         $securityContext $this->get('security.authorization_checker');
  1176.         if ($securityContext->isGranted('ROLE_GLOBAL_EDITOR')) {
  1177.             if ($instituteId) {
  1178.                 $institute $this->getDoctrine()
  1179.                     ->getRepository('OceanExpertBundle:Institutions')
  1180.                     ->findOneByIdInst($instituteId);
  1181.                 if ($institute) {
  1182.                     $institute->setActivated(1);
  1183.                     $institute->setLDateUpdated(new DateTime("now"));
  1184.                     $institute->setLastEditBy(
  1185.                         $this->get('security.token_storage')
  1186.                             ->getToken()
  1187.                             ->getUser()
  1188.                             ->getId()
  1189.                     );
  1190.                     $em $this->getDoctrine()->getManager();
  1191.                     $em->persist($institute); //marks object to be saved in the next transaction.
  1192.                     $em->flush(); //performs all saves and transactions.
  1193.                 }
  1194.                 return new JsonResponse(
  1195.                     array(
  1196.                         'status' => true,
  1197.                         'msg' => 'Record updated successfully'
  1198.                     )
  1199.                 );
  1200.             } else {
  1201.                 return new Response('Cannot update record.');
  1202.             }
  1203.         }
  1204.         return new Response('Cannot update record.');
  1205.     }
  1206.     public function deactivateInstituteAction($instituteId)
  1207.     {
  1208.         $securityContext $this->get('security.authorization_checker');
  1209.         if ($securityContext->isGranted('ROLE_GLOBAL_EDITOR')) {
  1210.             if ($instituteId) {
  1211.                 $institute $this->getDoctrine()
  1212.                     ->getRepository('OceanExpertBundle:Institutions')
  1213.                     ->findOneByIdInst($instituteId);
  1214.                 if ($institute) {
  1215.                     $institute->setActivated(0);
  1216.                     $institute->setLDateUpdated(new DateTime("now"));
  1217.                     $institute->setLastEditBy(
  1218.                         $this->get('security.token_storage')
  1219.                             ->getToken()
  1220.                             ->getUser()
  1221.                             ->getId());
  1222.                     $em $this->getDoctrine()->getManager();
  1223.                     $em->persist($institute); //marks object to be saved in the next transaction.
  1224.                     $em->flush(); //performs all saves and transactions.
  1225.                 }
  1226.                 return new JsonResponse(
  1227.                     array(
  1228.                         'status' => true,
  1229.                         'msg' => 'updated successfully'
  1230.                     )
  1231.                 );
  1232.             } else {
  1233.                 return new Response('Cannot update record.');
  1234.             }
  1235.         }
  1236.         return new Response('Cannot update record.');
  1237.     }
  1238.     /**
  1239.      * show this page when the institute has been edited successfully
  1240.      *
  1241.      * @param int $idInst
  1242.      *
  1243.      * @return Response
  1244.      */
  1245.     public function editInstituteSuccessAction($idInst)
  1246.     {
  1247.         return $this->render(
  1248.             'Institute/editInstituteSuccess.html.twig',
  1249.             array(
  1250.                 'idInst' => $idInst
  1251.             )
  1252.         );
  1253.     }
  1254.     /**
  1255.      * @param Request $request
  1256.      *
  1257.      * @return JsonResponse
  1258.      */
  1259.     public function getInstitutionsAjaxAction(Request $request)
  1260.     {
  1261.         if (!null == $request->query->get('q')) {
  1262.             $inst $request->query->get('q');
  1263.             $em $this->getDoctrine()->getManager();
  1264.             $qb $em->createQueryBuilder();
  1265.             $qb->select('i.idInst as id,i.instName');
  1266.             $qb->from('OceanExpertBundle:Institutions''i');
  1267.             $qb->where('i.instName like :name OR i.acronym like :name');
  1268.             if (!null == $request->query->get('instId')) {
  1269.                 $qb->andWhere('i.idInst != :instId');
  1270.                 $qb->setParameter('instId'$request->query->get('instId'));
  1271.             }
  1272.             $qb->andWhere('i.activated = 1');
  1273.             $qb->setParameter('name''%' $inst '%');
  1274.             $qb->setParameter('name''%' $inst '%');
  1275.             $qb->orderBy('i.instName''ASC');
  1276.             $institutes $qb->getQuery()->getResult();
  1277.             $data = array();
  1278.             if ($institutes) {
  1279.                 $data = array(
  1280.                     'incomplete_results' => false,
  1281.                     'items' => $institutes,
  1282.                     'total_count' => count($institutes),
  1283.                 );
  1284.             } else {
  1285.                 $data = array(
  1286.                     'incomplete_results' => false,
  1287.                     'items' => array(),
  1288.                     'total_count' => 0,
  1289.                 );
  1290.             }
  1291.             return new JsonResponse($data);
  1292.         }
  1293.         return new JsonResponse();
  1294.     }
  1295. }