src/OceanExpertBundle/Controller/InstitutionController.php line 956

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