src/OceanExpertBundle/Controller/SearchController.php line 2600

Open in your IDE?
  1. <?php
  2. namespace OceanExpertBundle\Controller;
  3. use Doctrine\Common\Persistence\ManagerRegistry;
  4. use Doctrine\ORM\AbstractQuery;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class SearchController extends AbstractController
  10. {
  11.     public function generalSearchAction(Request $request): Response
  12.     {
  13.         $searchParams['searchQuery'] = $request->query->get('q');
  14.         $searchParams['searchQuery'] = self::cleaned($searchParams['searchQuery']);
  15.         if (null !== ($request->query->get('limit'))) {
  16.             $limit $request->query->get('limit');
  17.         } else {
  18.             $limit 10;
  19.         }
  20.         $em self::getDoctrine()->getManager();
  21.         $connection $em->getConnection();
  22.         $statement $connection->prepare("
  23.             SELECT 
  24.                 * 
  25.             FROM 
  26.                 indiv 
  27.             WHERE 
  28.                 ( 
  29.                      MATCH (fname,mname,sname) AGAINST (:searchQuery IN BOOLEAN MODE) 
  30.                      OR fname LIKE :percSearchQuery 
  31.                      OR fname LIKE :searchQueryPerc  
  32.                      OR mname LIKE :percSearchQuery 
  33.                      OR mname LIKE :searchQueryPerc 
  34.                      OR sname LIKE :percSearchQuery 
  35.                      OR sname LIKE :searchQueryPerc
  36.                 ) 
  37.                 AND status = 1;
  38.             ");
  39.         $statement->bindValue('searchQuery'$searchParams['searchQuery']);
  40.         $statement->bindValue('percSearchQuery''%' $searchParams['searchQuery']);
  41.         $statement->bindValue('searchQueryPerc'$searchParams['searchQuery'] . '%');
  42.         $statement->execute();
  43.         $query $statement->fetchAll();
  44.         $paginator $this->get('knp_paginator');
  45.         $pagination $paginator->paginate(
  46.             $query,
  47.             $request->query->getInt('experts'1),
  48.             $limit,
  49.             array('pageParameterName' => 'experts''sortDirectionParameterName' => 'dir')
  50.         );
  51.         $ids = array();
  52.         foreach ($pagination->getItems() as $value) {
  53.             $ids[] = $value['id_ind'];
  54.         }
  55.         $indiv $em->getRepository('OceanExpertBundle:Indiv');
  56.         $experts $indiv->createQueryBuilder('i')
  57.             ->select('i,ix.instName,g.groupname,m.idGroup,m.role,c.country')
  58.             ->leftJoin('OceanExpertBundle:IndivInstitution''ii''WITH''ii.idInd = i.idInd')
  59.             ->leftJoin('OceanExpertBundle:Institutions''ix''WITH''ix.idInst = ii.idInst')
  60.             ->leftJoin('OceanExpertBundle:MemberGroups''m''WITH''i.idInd = m.idInd')
  61.             ->leftJoin('OceanExpertBundle:Groups''g''WITH''g.idGroup = m.idGroup')
  62.             ->leftJoin('OceanExpertBundle:Countries''c''WITH''c.idCountry = i.countryCode')
  63.             ->where('i.idInd IN (:group)')
  64.             ->andWhere('i.status = 1')
  65.             ->setParameter('group'$ids)
  66.             ->addOrderBy('ix.instName''DESC')
  67.             ->addOrderBy('i.sname''ASC')
  68.             ->getQuery()
  69.             ->getResult();
  70.         $em self::getDoctrine()->getManager();
  71.         $connection $em->getConnection();
  72.         $statement $connection->prepare("
  73.             SELECT 
  74.                 * 
  75.             FROM 
  76.                 institutions 
  77.             WHERE 
  78.                 (
  79.                     MATCH (inst_name,inst_name_eng,acronym) AGAINST (:searchQuery IN BOOLEAN MODE)
  80.                     OR inst_name LIKE :percSearchQuery 
  81.                     OR inst_name LIKE :searchQueryPerc 
  82.                     OR inst_name_eng LIKE :percSearchQuery 
  83.                     OR inst_name_eng LIKE :searchQueryPerc 
  84.                     OR acronym LIKE :percSearchQuery 
  85.                     OR acronym LIKE :searchQueryPerc
  86.                     OR activities LIKE :percSearchQueryPerc
  87.                 )
  88.                 AND activated = 1;
  89.         ");
  90.         $statement->bindValue('searchQuery'$searchParams['searchQuery']);
  91.         $statement->bindValue('percSearchQuery''%' $searchParams['searchQuery']);
  92.         $statement->bindValue('searchQueryPerc'$searchParams['searchQuery'] . '%');
  93.         $statement->bindValue('percSearchQueryPerc''%' $searchParams['searchQuery'] . '%');
  94.         $statement->execute();
  95.         $institutions $statement->fetchAll();
  96.         $instPage $paginator->paginate(
  97.             $institutions,
  98.             $request->query->getInt('insts'1),
  99.             12,
  100.             array('pageParameterName' => 'insts''sortDirectionParameterName' => 'dir')
  101.         );
  102.         $evnts $em->getRepository('OceanExpertBundle:Events');
  103.         $events $evnts->createQueryBuilder('e')
  104.             ->select('e')
  105.             ->where('e.title LIKE :searchQuery')
  106.             ->where('e.shorttitle LIKE :searchQuery')
  107.             ->where('e.summary LIKE :searchQuery')
  108.             ->setParameter('searchQuery''%' $searchParams['searchQuery'] . '%')
  109.             ->addOrderBy('e.title''ASC')
  110.             ->getQuery()
  111.             ->getResult();
  112.         $eventspage $paginator->paginate(
  113.             $events,
  114.             $request->query->getInt('events'1),
  115.             10,
  116.             array('pageParameterName' => 'events''sortDirectionParameterName' => 'dir')
  117.         );
  118.         return $this->render(
  119.             'Search/generalSearch.html.twig',
  120.             array(
  121.                 'pagination' => $pagination,
  122.                 'experts' => $experts,
  123.                 'institutions' => $instPage,
  124.                 'events' => $eventspage,
  125.                 'items' => array(),
  126.                 'qry' => $searchParams['searchQuery']
  127.             )
  128.         );
  129.     }
  130.     /**
  131.      * extract the possible filters from the results
  132.      * these can be used to filter down the results even move (especially in the webinterface)
  133.      *
  134.      * @param array $dataResult the results from a search
  135.      *
  136.      * @return array
  137.      */
  138.     static function getFilters(array $dataResult): array
  139.     {
  140.         $filters = array();
  141.         foreach(['type''country'] as $type) {
  142.             $filters[$type] = SearchController::getUnique(
  143.                 $dataResult,
  144.               $type
  145.             );
  146.         }
  147.         return $filters;
  148.     }
  149.     /**
  150.      * extract all the params we need to do the search
  151.      * these params should be reflected in the documentation of the API
  152.      * as these are all the possible params that can be given and have effect on the result
  153.      * they are shown here in alfabetic order to be sure there are no doubles in the variables
  154.      *
  155.      * @param Request $request
  156.      *
  157.      * @return array all the params in an array
  158.      */
  159.     static function extractParams(Request $request): array
  160.     {
  161.         $searchParams['queryString']    = $request->query->all();
  162.         $searchParams['action']         = $request->query->get('action') ?? 'advSearch';
  163.         $searchParams['countryName']    = $request->query->get('countryName') ?? '';
  164.         $searchParams['endDate']        = $request->query->get('endDate');
  165.         $searchParams['expertLname']    = $request->query->get('expertLname') ?? '';
  166.         $searchParams['expertSubject']  = $request->query->get('expertSubject') ?? '';
  167.         $searchParams['expertRegion']   = $request->query->get('expertRegion') ?? '';
  168.         $searchParams['instCountry']    = $request->query->get('instCountry') ?? '';
  169.         $searchParams['instName']       = $request->query->get('instName') ?? '';
  170.         $searchParams['instRegion']     = $request->query->get('instRegion') ?? '';
  171.         $searchParams['instType']       = $request->query->get('instType') ?? '';
  172.         $searchParams['searchKeywords'] = $request->query->get('keywords', array());
  173.         $searchParams['limit']          = $request->query->get('limit') ?? 10;
  174.         $searchParams['sortOrder']      = $request->query->get('order') ?? 'ASC';
  175.         $searchParams['searchQuery']    = $request->query->get('query') ?? '';
  176.         $searchParams['sortby']         = $request->query->get('sortby') ?? '';
  177.         $searchParams['startDate']      = $request->query->get('startDate');
  178.         $searchParams['searchToggle']   = $request->query->get('toggle');
  179.         //what kind of items are we looking for
  180.         $searchParams['searchType']     = $request->query->get('type', array());
  181.         //default value for the type when using browse should be 'all'
  182.         if (null == $request->query->get('type')
  183.             || $request->query->get('type') == ''
  184.             || (is_array($searchParams['searchType'])
  185.                 && !count($searchParams['searchType']))
  186.         ) {
  187.             $searchParams['searchType'] = array('all');
  188.         }
  189.         //search types shouls always be an array, even with one element
  190.         if (!is_array($searchParams['searchType'])) {
  191.             $searchParams['searchType'] = array($searchParams['searchType']);
  192.         }
  193.         //get rid of those empty types that get added somewhere by javascript
  194.         foreach ($searchParams['searchType'] as $key => $val) {
  195.             if ($val === '') {
  196.                 unset($searchParams['searchType'][$key]);
  197.             }
  198.         }
  199.         //independent of the searchType, what type should we use to filter the results
  200.         //typically afte a search with searchType == all
  201.         $searchParams['searchFilterType']= $request->query->get('filterType');
  202.         //cleanup the filters
  203.         $searchParams['searchFilterType'] = array_unique(
  204.             explode(
  205.                 ',',
  206.                 $searchParams['searchFilterType']
  207.             )
  208.         );
  209.         if ($searchParams['searchFilterType'][0] == '') {
  210.             unset($searchParams['searchFilterType']);
  211.         }
  212.         //special case of FilterType
  213.         $searchParams['filterCountry']  = $request->query->get('filterCountry');
  214.         //not sure if this is used everywhere/somewhere and what it should do
  215.         $searchParams['searchFilter']   = $request->query->get('filter');
  216.         //clenaup the query
  217.         $searchParams['searchQuery'] = self::cleaned($searchParams['searchQuery']);
  218.         //remember the search conditions, so we can recreate the search in the interface
  219.         $conditions = array();
  220.         foreach ($searchParams['searchType'] as $i => $value) {
  221.             $conditions[$i]['type'] = $value;
  222.             if (!isset($searchParams['searchFilter'][$i])) {
  223.                 $searchParams['searchFilter'][$i] = '';
  224.             }
  225.             $conditions[$i]['filter'] = $searchParams['searchFilter'][$i];
  226.             $conditions[$i]['keywords'] = $searchParams['searchKeywords'][$i] ?? '';
  227.             if (!isset($searchParams['searchToggle'][$i])) {
  228.                 $searchParams['searchToggle'][$i] = ' OR ';
  229.             }
  230.             $conditions[$i]['toggle'] = $searchParams['searchToggle'][$i];
  231.             if (!isset($searchParams['startDate'][$i])) {
  232.                 $searchParams['startDate'][$i] = '';
  233.             }
  234.             $conditions[$i]['startDate'] = $searchParams['startDate'][$i];
  235.             if (!isset($searchParams['endDate'][$i])) {
  236.                 $searchParams['endDate'][$i] = '';
  237.             }
  238.             $conditions[$i]['endDate'] = $searchParams['endDate'][$i];
  239.         }
  240.         $searchParams['conditions'] = $conditions;
  241.         //what to do with the sortby if there is none defined (yet)
  242.         $sortFields = array(
  243.             'relevance' => 'Relevance'
  244.         );
  245.         if ($searchParams['sortby'] == '') {
  246.             //for all, experts and institutions use 'relevance' as sortby in case we have a search for 'name contains'
  247.             //in other cases use inst_name for institutions
  248.             // or sname for experts
  249.             if (!empty(array_intersect($searchParams['searchType'], array('all''experts''institutions')))
  250.                 && !empty(array_intersect(['Name contains'], $searchParams['searchFilter']))
  251.             ) {
  252.                 $searchParams['sortby'] = 'relevance';
  253.                 $sortFields['relevance'] = 'Relevance';
  254.             } elseif (!empty(array_intersect($searchParams['searchType'], array('experts')))) {
  255.                 $searchParams['sortby'] = 'sname';
  256.                 $sortFields['sname'] = 'Last Name';
  257.             } elseif (!empty(array_intersect($searchParams['searchType'], array('documents')))) {
  258.                 $searchParams['sortby'] = 'title';
  259.                 $sortFields['country'] = 'Country';
  260.             } elseif (!empty(array_intersect($searchParams['searchType'], array('institutions')))) {
  261.                 $searchParams['sortby'] = 'inst_name';
  262.                 $sortFields['inst_name'] = 'Institute Name';
  263.             }
  264.         }
  265.         //if we still have no sortby, default to sname for both browse and advSearch
  266.         if ($searchParams['sortby'] == ''
  267.             && ($searchParams['action'] == 'browse'
  268.                 || $searchParams['action'] == 'advSearch')
  269.         ) {
  270.             $searchParams['sortby'] = 'sname';
  271.             unset($sortFields['relevance']);
  272.             $sortFields['sname'] = 'Last Name';
  273.         }
  274.         if (count($searchParams['searchType'])) {
  275.             if (in_array('institutions'$searchParams['searchType'])) {
  276.                 unset($sortFields['relevance']);
  277.                 $sortFields['instType'] = 'Institute Type';
  278.                 $sortFields['name'] = 'Institute Name';
  279.             }
  280.             if (in_array('experts'$searchParams['searchType'])) {
  281.                 $sortFields['jobtitle'] = 'Position';
  282.                 $sortFields['sname'] = 'Last Name';
  283.             }
  284.             if (in_array('events'$searchParams['searchType'])) {
  285.                 $sortFields['eventtype'] = 'Event Type';
  286.             }
  287.             $sortFields['country'] = 'Country';
  288.         }
  289.         $searchParams['sortFields'] = $sortFields;
  290.         return $searchParams;
  291.     }
  292.     /**
  293.      * get the results for the search when action=browse
  294.      *
  295.      * @param array  $searchParams all the search parameters that have been passed in this request
  296.      * @param object $doctrine
  297.      *
  298.      * @return array with the results
  299.      */
  300.     public static function getBrowseSearchResults(array $searchParamsobject $doctrine): array
  301.     {
  302.         $connection $doctrine->getManager()->getConnection();
  303.         $searchResultType 'Browse Results';
  304.         $dataResult = array();
  305.         $sortFields $searchParams['sortFields'];
  306.         $message '';
  307.         //status=0 means everything is ok
  308.         $status 0;
  309.         if (is_array($searchParams['searchType'])
  310.             && count($searchParams['searchType']) > 1
  311.         ) {
  312.             //impossible for action=browse, this should be a simple query in the end
  313.             $message 'You can only have one (filter)type when you use action=browse.';
  314.             $message .= ' Provided (filter)types: ' implode(', '$searchParams['searchType']);
  315.             return array(
  316.                 'status' => 1,
  317.                 'message' => $message
  318.             );
  319.         }
  320.         //for the searchtype 'browse' we do not need an array of search types
  321.         if (isset($searchParams['searchType'])
  322.             && is_array($searchParams['searchType'])
  323.             && count($searchParams['searchType'])
  324.         ) {
  325.             $searchParams['searchType'] = $searchParams['searchType'][0];
  326.         }
  327.         if ($searchParams['searchQuery'] != '') {
  328.             $indiv = array();
  329.             $indivNames = array();
  330.             $indivGroups = array();
  331.             if (($searchParams['searchType'] == 'all'
  332.                     || $searchParams['searchType'] == 'experts'
  333.                 )
  334.                 && !(isset($searchParams['queryString']['searchType'])
  335.                     && $searchParams['queryString']['searchType'] === 'group')
  336.             ) {
  337.                 $newString self::fulltext($searchParams['searchQuery']);
  338.                 $stmtInd $connection->prepare("
  339.                         SELECT 
  340.                             'experts' AS type, 
  341.                             i.status, 
  342.                             i.id_ind, 
  343.                             i.title,   
  344.                             concat(i.fname,' ',i.sname) AS name,
  345.                             i.fname,
  346.                             i.mname,
  347.                             i.sname,
  348.                             i.jobtitle,
  349.                             i.addr_1,
  350.                             i.addr_2,
  351.                             i.city,
  352.                             i.state,
  353.                             i.postcode,
  354.                             i.use_inst_addr, 
  355.                             i.deceased, 
  356.                             i.retired, 
  357.                             i.quality_checked, 
  358.                             inst.inst_name,
  359.                             inst.inst_address,
  360.                             inst.addr_2 AS instAddr2,
  361.                             inst.city AS instCity,
  362.                             inst.state AS instState,
  363.                             inst.postcode AS instPostCode, 
  364.                             (IF(i.use_inst_addr = 1, ic.country, c.country)) AS country, 
  365.                             MATCH (i.fname,i.sname) AGAINST (:newString IN BOOLEAN MODE) AS relevance, 
  366.                             'zz' AS instType, 
  367.                             'zz' AS eventtype, 
  368.                             '' AS inst_logo, 
  369.                             'zz' AS name
  370.                         FROM 
  371.                             indiv i
  372.                             LEFT JOIN countries c ON c.id_country = i.country_code
  373.                             LEFT JOIN indiv_institution ii ON ii.id_ind = i.id_ind
  374.                             LEFT JOIN institutions inst ON inst.id_inst = ii.id_inst
  375.                             LEFT JOIN countries ic ON ic.id_country = inst.country_code
  376.                         WHERE 
  377.                             ( 
  378.                                 MATCH (fname, mname, sname) AGAINST (:newString IN BOOLEAN MODE) 
  379.                                 OR i.fname LIKE :percSearchQuery
  380.                                 OR i.fname LIKE :searchQueryPerc 
  381.                                 OR i.mname LIKE :percSearchQuery
  382.                                 OR i.mname LIKE :searchQueryPerc 
  383.                                 OR i.sname LIKE :percSearchQuery
  384.                                 OR i.sname LIKE :searchQueryPerc
  385.                                 OR i.private_address LIKE :percSearchQueryPerc
  386.                                 OR i.addr_1 LIKE :percSearchQueryPerc
  387.                                 OR i.addr_2 LIKE :percSearchQueryPerc
  388.                                 OR i.city LIKE :percSearchQueryPerc
  389.                                 OR i.state LIKE :percSearchQueryPerc
  390.                             ) 
  391.                             " $searchParams['indivStatus'] . '
  392.                         ORDER BY relevance DESC, sname ASC;
  393.                     ');
  394.                 $stmtInd->bindValue('newString'$newString);
  395.                 $stmtInd->bindValue('percSearchQuery''%' $searchParams['searchQuery']);
  396.                 $stmtInd->bindValue('searchQueryPerc'$searchParams['searchQuery'] . '%');
  397.                 $stmtInd->bindValue('percSearchQueryPerc''%' $searchParams['searchQuery'] . '%');
  398.                 $stmtInd->execute();
  399.                 $indiv $stmtInd->fetchAll();
  400.                 $sortFields['jobtitle'] = 'Position';
  401.             }
  402.             if ($searchParams['searchType'] == 'name') {
  403.                 $newString self::fulltext($searchParams['searchQuery']);
  404.                 $stmtInd $connection->prepare("
  405.                         SELECT 
  406.                             'experts' AS type, 
  407.                             i.status, 
  408.                             i.id_ind, 
  409.                             i.title,   
  410.                             concat(i.fname,' ',i.sname) AS name,
  411.                             i.fname,
  412.                             i.mname,
  413.                             i.sname,
  414.                             i.jobtitle,
  415.                             i.addr_1,
  416.                             i.addr_2,
  417.                             i.city,
  418.                             i.state,
  419.                             i.postcode,
  420.                             i.use_inst_addr, 
  421.                             i.deceased, 
  422.                             i.retired, 
  423.                             i.quality_checked, 
  424.                             inst.inst_name,
  425.                             inst.inst_address,
  426.                             inst.addr_2 AS instAddr2,
  427.                             inst.city AS instCity,
  428.                             inst.state AS instState,
  429.                             inst.postcode AS instPostCode, 
  430.                             (IF(i.use_inst_addr = 1, ic.country, c.country)) AS country, 
  431.                             MATCH (i.fname,i.sname) AGAINST (:newString IN BOOLEAN MODE) AS relevance, 
  432.                             'zz' AS instType, 
  433.                             'zz' AS eventtype, 
  434.                             '' AS inst_logo, 
  435.                             'zz' AS name
  436.                         FROM 
  437.                             indiv i
  438.                             LEFT JOIN countries c ON c.id_country = i.country_code
  439.                             LEFT JOIN indiv_institution ii ON ii.id_ind = i.id_ind
  440.                             LEFT JOIN institutions inst ON inst.id_inst = ii.id_inst
  441.                             LEFT JOIN countries ic ON ic.id_country = inst.country_code
  442.                         WHERE 
  443.                             ( 
  444.                                 MATCH (fname, mname, sname) AGAINST (:newString IN BOOLEAN MODE) 
  445.                                 OR i.fname LIKE :percSearchQuery
  446.                                 OR i.fname LIKE :searchQueryPerc 
  447.                                 OR i.mname LIKE :percSearchQuery
  448.                                 OR i.mname LIKE :searchQueryPerc 
  449.                                 OR i.sname LIKE :percSearchQuery
  450.                                 OR i.sname LIKE :searchQueryPerc
  451.                             ) 
  452.                             " $searchParams['indivStatus'] . '
  453.                         ORDER BY relevance DESC, sname ASC;
  454.                     ');
  455.                 $stmtInd->bindValue('newString'$newString);
  456.                 $stmtInd->bindValue('percSearchQuery''%' $searchParams['searchQuery']);
  457.                 $stmtInd->bindValue('searchQueryPerc'$searchParams['searchQuery'] . '%');
  458.                 $stmtInd->execute();
  459.                 $indivNames $stmtInd->fetchAll();
  460.                 $sortFields['jobtitle'] = 'Position';
  461.             }
  462.             if ($searchParams['searchType'] == 'all'
  463.                 || (isset($searchParams['queryString']['searchType'])
  464.                 && $searchParams['queryString']['searchType'] == 'group'
  465.                 && $searchParams['searchType'] == 'experts')
  466.                 || $searchParams['searchType'] == 'group'
  467.             ) {
  468.                 //looking for experts that are in a group that matches the searchTerm
  469.                 $newString self::fulltext($searchParams['searchQuery']);
  470.                 $stmtInd $connection->prepare("
  471.                         SELECT 
  472.                             'experts' AS type, 
  473.                             i.status, 
  474.                             i.id_ind, 
  475.                             i.title,   
  476.                             concat(i.fname,' ',i.sname) AS name,
  477.                             i.fname,
  478.                             i.mname,
  479.                             i.sname,
  480.                             i.jobtitle,
  481.                             i.addr_1,
  482.                             i.addr_2,
  483.                             i.city,
  484.                             i.state,
  485.                             i.postcode,
  486.                             i.use_inst_addr, 
  487.                             i.deceased, 
  488.                             i.retired, 
  489.                             i.quality_checked, 
  490.                             inst.inst_name,
  491.                             inst.inst_address,
  492.                             inst.addr_2 AS instAddr2,
  493.                             inst.city AS instCity,
  494.                             inst.state AS instState,
  495.                             inst.postcode AS instPostCode, 
  496.                             g.groupname AS groupName,
  497.                             g.id_group AS groupId,
  498.                             '' AS relevance,   
  499.                             (IF(i.use_inst_addr = 1, ic.country, c.country)) AS country
  500.                         FROM 
  501.                             indiv i
  502.                             LEFT JOIN countries c ON c.id_country = i.country_code
  503.                             LEFT JOIN indiv_institution ii ON ii.id_ind = i.id_ind
  504.                             LEFT JOIN institutions inst ON inst.id_inst = ii.id_inst
  505.                             LEFT JOIN countries ic ON ic.id_country = inst.country_code
  506.                             LEFT JOIN member_groups mg ON mg.id_ind = i.id_ind
  507.                             LEFT JOIN groups g ON mg.id_group = g.id_group
  508.                         WHERE 
  509.                             ( 
  510.                                 g.groupname LIKE :percSearchQueryPerc
  511.                             ) 
  512.                             " $searchParams['indivStatus'] . '
  513.                         ORDER BY sname ASC;
  514.                     ');
  515.                 $stmtInd->bindValue('percSearchQueryPerc''%' $searchParams['searchQuery'] . '%');
  516.                 $stmtInd->execute();
  517.                 $indivGroups $stmtInd->fetchAll();
  518.                 $sortFields['jobtitle'] = 'Position';
  519.             }
  520.             $institutions = array();
  521.             if ($searchParams['searchType'] == 'all'
  522.                 || $searchParams['searchType'] == 'institutions'
  523.             ) {
  524.                 $newString self::fulltext($searchParams['searchQuery']);
  525.                 $stmtInst $connection->prepare("
  526.                         SELECT 
  527.                             'institutions' AS type, 
  528.                             i.activated, 
  529.                             i.id_inst, 
  530.                             insttype_name AS instType, 
  531.                             inst_name,
  532.                             inst_name_eng,
  533.                             inst_address,
  534.                             addr_2,
  535.                             city AS instCity,
  536.                             state AS instState,
  537.                             postcode AS instPostCode,
  538.                             country,
  539.                             inst_logo,
  540.                             activities, 
  541.                             MATCH (inst_name,inst_name_eng,acronym) AGAINST (:newString) AS relevance, 
  542.                             'zz' AS jobtitle, 
  543.                             'zz' AS eventtype,
  544.                             'zz' AS fname,  
  545.                             'zz' AS sname, 
  546.                             'zz' AS name
  547.                         FROM 
  548.                             institutions i
  549.                             LEFT JOIN countries c ON c.id_country =  i.country_code
  550.                             LEFT JOIN insttypes it ON it.id_insttype =  i.inst_type_id
  551.                         WHERE 
  552.                             (
  553.                                 MATCH (inst_name,inst_name_eng,acronym) AGAINST (:newString IN BOOLEAN MODE)
  554.                                 OR inst_name LIKE :percSearchQueryPerc 
  555.                                 OR inst_name_eng LIKE :percSearchQueryPerc 
  556.                                 OR inst_address LIKE :percSearchQueryPerc 
  557.                                 OR addr_2 LIKE :percSearchQueryPerc 
  558.                                 OR city LIKE :percSearchQueryPerc 
  559.                                 OR state LIKE :percSearchQueryPerc 
  560.                                 OR acronym LIKE :percSearchQueryPerc 
  561.                                 OR activities LIKE :percSearchQueryPerc
  562.                             )
  563.                             " $searchParams['instStatus'] . '
  564.                         ORDER BY relevance DESC, i.inst_name ASC;
  565.                     ');
  566.                 $stmtInst->bindValue('newString'$newString);
  567.                 $stmtInst->bindValue('percSearchQueryPerc''%'.$searchParams['searchQuery'].'%');
  568.                 $stmtInst->execute();
  569.                 $institutions $stmtInst->fetchAll();
  570.                 $sortFields['instType'] = 'Institution Type';
  571.             }
  572.             $events = array();
  573.             if ($searchParams['searchType'] == 'all'
  574.                 || $searchParams['searchType'] == 'events'
  575.             ) {
  576.                 $sortFields['name'] = 'Event Title';
  577.                 if ($searchParams['sortby'] == '') {
  578.                     $searchParams['sortby'] = 'name';
  579.                 }
  580.                 $query "
  581.                         SELECT 
  582.                             'events' AS type, 
  583.                             et.eventtype_name AS eventtype, 
  584.                             e.status AS activated,
  585.                             e.id_event, 
  586.                             e.title AS name, 
  587.                             e.start_on,
  588.                             e.end_on, 
  589.                             e.address AS addr_1,
  590.                             e.city, 
  591.                             e.state, 
  592.                             c.country AS country,
  593.                             'zz' AS jobtitle, 
  594.                             'zz' AS instType,
  595.                             'zz' AS fname,
  596.                             'zz' AS sname,
  597.                             'zz' AS inst_name
  598.                         FROM 
  599.                             events e
  600.                             LEFT JOIN countries c on c.id_country =  e.id_country
  601.                             LEFT JOIN eventtypes et on et.id_eventtype =  e.id_eventtype
  602.                         WHERE 
  603.                             (
  604.                                 e.title LIKE :percSearchQueryPerc 
  605.                                 OR e.summary LIKE :percSearchQueryPerc
  606.                                 OR e.shorttitle LIKE :percSearchQueryPerc
  607.                                 OR e.keywords LIKE :percSearchQueryPerc
  608.                                 OR e.website LIKE :percSearchQueryPerc
  609.                                 OR e.city LIKE :percSearchQueryPerc
  610.                                 OR e.address LIKE :percSearchQueryPerc
  611.                             )
  612.                             " $searchParams['eventStatus'] . '
  613.                          ORDER BY e.title ASC;
  614.                      ';
  615.                 $stmtEvent $connection->prepare($query);
  616.                 $stmtEvent->bindValue('searchQuery'$searchParams['searchQuery']);
  617.                 $stmtEvent->bindValue('percSearchQueryPerc''%' $searchParams['searchQuery'] . '%');
  618.                 $stmtEvent->execute();
  619.                 $events $stmtEvent->fetchAll();
  620.                 $sortFields['eventtype'] = 'Event Type';
  621.             }
  622.             $documents = array();
  623.             if ($searchParams['searchType'] == 'all'
  624.                 || $searchParams['searchType'] == 'documents'
  625.             ) {
  626.                 $sortFields['title'] = 'Document Title';
  627.                 if ($searchParams['sortby'] == '') {
  628.                     $searchParams['sortby'] = 'title';
  629.                 }
  630.                 $query "
  631.                         SELECT 
  632.                             'documents' AS type, 
  633.                             d.id_doc,
  634.                             dt.doctypename AS documenttype, 
  635.                             d.title, 
  636.                             d.created_at,
  637.                             'undefined' AS country
  638.                         FROM 
  639.                              documents d, 
  640.                              doctypes dt 
  641.                         WHERE 
  642.                             dt.id_doctype = d.id_doctype
  643.                             AND d.title LIKE :percSearchQueryPerc
  644.                         ORDER BY d.title ASC
  645.                     ";
  646.                 $stmtDocument $connection->prepare($query);
  647.                 $stmtDocument->bindValue('percSearchQueryPerc''%' $searchParams['searchQuery'] . '%');
  648.                 $stmtDocument->execute();
  649.                 $documents $stmtDocument->fetchAll();
  650.                 $sortFields['documenttype'] = 'Document Type';
  651.             }
  652.             $dataResult array_unique(
  653.                 array_merge(
  654.                     $indiv,
  655.                     $indivNames,
  656.                     $indivGroups,
  657.                     $institutions,
  658.                     $events,
  659.                     $documents
  660.                 ),
  661.                 SORT_REGULAR
  662.             );
  663.             if ($searchParams['searchType'] == 'all') {
  664.                 if (count($dataResult) == 0) {
  665.                     $message 'We could not find any results matching "<strong>' self::cleaned($searchParams['searchQuery']) . ' </strong>". ';
  666.                     $message.= 'Please try again with different keyword(s).';
  667.                     return array(
  668.                         'status' => 3,
  669.                         'message' => $message,
  670.                         'dataResult' => array(),
  671.                         'searchResultType' => '',
  672.                         'sortFields' => array()
  673.                     );
  674.                 }
  675.                 $message 'Your search for "<strong>' self::cleaned($searchParams['searchQuery']) . '</strong>" ';
  676.                 $message.= 'returned <strong>' count($dataResult) . '</strong> result(s).';
  677.             } elseif ($searchParams['searchType'] == 'experts') {
  678.                 if (count($dataResult) == 0) {
  679.                     $message 'We could not find any results matching "<strong>' self::cleaned($searchParams['searchQuery']) . '</strong>" in Experts. ';
  680.                     $message.= 'Please try again with different keyword(s).';
  681.                     return array(
  682.                         'status' => 3,
  683.                         'message' => $message,
  684.                         'dataResult' => array(),
  685.                         'searchResultType' => '',
  686.                         'sortFields' => array()
  687.                     );
  688.                 }
  689.                 $message 'Your search for "<strong>' self::cleaned($searchParams['searchQuery']) . '</strong>" in "<strong>Experts</strong>" ';
  690.                 $message.= 'returned <strong>' count($dataResult) . '</strong> result(s).';
  691.             } elseif ($searchParams['searchType'] == 'institutions') {
  692.                 if (count($dataResult) == 0) {
  693.                     $message 'We could not find any results matching "<strong>' self::cleaned($searchParams['searchQuery']) . '</strong>" in Institutions. ';
  694.                     $message.= 'Please try again with different keyword(s).';
  695.                     return array(
  696.                         'status' => 3,
  697.                         'message' => $message,
  698.                         'dataResult' => array(),
  699.                         'searchResultType' => '',
  700.                         'sortFields' => array()
  701.                     );
  702.                 }
  703.                 $message 'Your search for "<strong>' self::cleaned($searchParams['searchQuery']) . '</strong>" in "<strong>Institutions</strong>" ';
  704.                 $message.= 'returned <strong>' count($dataResult) . '</strong> result(s).';
  705.             } elseif ($searchParams['searchType'] == 'documents') {
  706.                 $searchParams['countryName'] = '';
  707.                 $searchParams['expertLname'] = '';
  708.                 if (count($dataResult) == 0) {
  709.                     $message 'We could not find any results matching "<strong>' self::cleaned($searchParams['searchQuery']) . '</strong>" in the title of Documents. ';
  710.                     $message .= 'Please try again with different word(s).';
  711.                     return array(
  712.                         'status' => 3,
  713.                         'message' => $message,
  714.                         'dataResult' => array(),
  715.                         'searchResultType' => '',
  716.                         'sortFields' => array()
  717.                     );
  718.                 }
  719.                 $message "Your search for \"<strong>" self::cleaned($searchParams['searchQuery']) . "</strong>\" ";
  720.                 $message .= "in \"<strong>Documents</strong>\" ";
  721.                 $message .= "returned <strong>" count($dataResult) . "</strong> result(s).";
  722.             }
  723.         }
  724.         if ($searchParams['expertLname'] != '') {
  725.             $stmtInd $connection->prepare("
  726.                     SELECT 
  727.                         'experts' AS type, 
  728.                         i.status, 
  729.                         i.id_ind, 
  730.                         concat(i.fname, ' ', i.sname) AS name,
  731.                         i.fname,
  732.                         i.mname,
  733.                         i.sname,
  734.                         inst_name,
  735.                         jobtitle,
  736.                         i.addr_1,
  737.                         i.addr_2,
  738.                         i.city,
  739.                         i.state,
  740.                         i.postcode,
  741.                         inst.inst_address,
  742.                         inst.addr_2 AS instAddr2,
  743.                         inst.city AS instCity,
  744.                         inst.state AS instState,
  745.                         inst.postcode AS instPostCode, 
  746.                         use_inst_addr, 
  747.                         deceased, 
  748.                         retired, 
  749.                         quality_checked, 
  750.                         (IF(use_inst_addr = 1, ic.country, c.country)) AS country, 
  751.                         'zz' AS instType, 
  752.                         'zz' AS eventtype, 
  753.                         '' AS inst_logo
  754.                     FROM 
  755.                         indiv i
  756.                         LEFT JOIN countries c on c.id_country =  i.country_code
  757.                         LEFT JOIN indiv_institution ii on ii.id_ind = i.id_ind
  758.                         LEFT JOIN institutions inst on inst.id_inst = ii.id_inst
  759.                         LEFT JOIN countries ic on ic.id_country =  inst.country_code
  760.                     WHERE 
  761.                         (sname LIKE :expertLNamePerc)
  762.                         " $searchParams['indivStatus'] . 
  763.                     ORDER BY sname, fname ASC
  764.                 ');
  765.             $stmtInd->bindValue('expertLNamePerc'$searchParams['expertLname'] . '%');
  766.             $stmtInd->execute();
  767.             $dataResult $stmtInd->fetchAll();
  768.             $sortFields['jobtitle'] = 'Position';
  769.             $message 'There are <strong>' count($dataResult) . '</strong> experts ';
  770.             $message.= 'with their <strong>last name</strong> starting with "<strong>' $searchParams['expertLname'] . '</strong>".';
  771.         }
  772.         if ($searchParams['countryName'] != '') {
  773.             if ($searchParams['searchType'] == 'experts') {
  774.                 $query "
  775.                         SELECT 
  776.                             'experts' AS type, 
  777.                             i.status, 
  778.                             i.id_ind, 
  779.                             concat(i.fname,' ',i.sname) AS name,
  780.                             i.fname,
  781.                             i.mname,
  782.                             i.sname,
  783.                             inst_name,
  784.                             jobtitle,
  785.                             i.addr_1,
  786.                             i.addr_2,
  787.                             i.city,
  788.                             i.state,
  789.                             i.postcode,
  790.                             inst.inst_address,
  791.                             inst.addr_2 AS instAddr2,
  792.                             inst.city AS instCity,
  793.                             inst.state AS instState,
  794.                             inst.postcode AS instPostCode, 
  795.                             use_inst_addr, 
  796.                             deceased, 
  797.                             retired, 
  798.                             quality_checked, 
  799.                             (IF(use_inst_addr = 1, ic.country, c.country)) AS country, 
  800.                             'zz' AS instType, 
  801.                             'zz' AS eventtype, 
  802.                             '' AS inst_logo
  803.                         FROM    
  804.                             indiv i
  805.                             LEFT JOIN countries c on c.id_country =  i.country_code
  806.                             LEFT JOIN indiv_institution ii on ii.id_ind = i.id_ind
  807.                             LEFT JOIN institutions inst on inst.id_inst = ii.id_inst
  808.                             LEFT JOIN countries ic on ic.id_country =  inst.country_code
  809.                         WHERE 
  810.                             ( CASE WHEN use_inst_addr = 1 THEN ";
  811.                 if (strlen($searchParams['countryName']) === 1) {
  812.                     $query .= 'ic.country LIKE :countryNamePerc ELSE c.country LIKE :countryNamePerc END) ';
  813.                 } else {
  814.                     $query .= 'ic.country LIKE :countryName ELSE c.country LIKE :countryName END) ';
  815.                 }
  816.                 $query .=  $searchParams['indivStatus'] . ' ORDER BY sname ASC ';
  817.                 $stmtInd $connection->prepare($query);
  818.                 $stmtInd->bindValue('countryName'$searchParams['countryName']);
  819.                 $stmtInd->bindValue('countryNamePerc'$searchParams['countryName'] . '%');
  820.                 $stmtInd->execute();
  821.                 $dataResult $stmtInd->fetchAll();
  822.                 $sortFields['jobtitle'] = 'Position';
  823.                 $message 'There are <strong>' count($dataResult) . '</strong> experts ';
  824.                 $message.= 'located in <strong>countries with names</strong> starting with "<strong>' $searchParams['countryName'] . '</strong>".';
  825.                 if (isset($searchParams['referrer'])
  826.                     && ($searchParams['referrer'] == 'map')
  827.                 ) {
  828.                     $message 'There are <strong>' count($dataResult) . '</strong> experts ';
  829.                     $message.= 'located in  "<strong>' $searchParams['countryName'] . '</strong>".';
  830.                 }
  831.             } elseif ($searchParams['searchType'] == 'institution') {
  832.                 $stmtInd $connection->prepare("
  833.                         SELECT 
  834.                             'institutions' AS type, 
  835.                             insttype_name AS jobtitle, 
  836.                             i.activated, 
  837.                             i.id_inst, 
  838.                             insttype_name AS instType, 
  839.                             inst_name,
  840.                             inst_name_eng,
  841.                             inst_address,
  842.                             addr_2,
  843.                             city AS instCity,
  844.                             state AS instState,
  845.                             postcode AS instPostCode,
  846.                             country,
  847.                             activities, 
  848.                             inst_logo, 
  849.                             'zz' AS jobtitle,
  850.                             'zz' AS fname, 
  851.                             'zz' AS sname
  852.                         FROM 
  853.                             institutions i
  854.                             LEFT JOIN countries c on c.id_country =  i.country_code
  855.                             LEFT JOIN insttypes it on it.id_insttype =  i.inst_type_id
  856.                         WHERE 
  857.                             ( c.country LIKE '" $searchParams['countryName'] . "' )
  858.                             " $searchParams['instStatus'] . 
  859.                         ORDER BY inst_name ASC
  860.                     ');
  861.                 $stmtInd->execute();
  862.                 $dataResult $stmtInd->fetchAll();
  863.                 $sortFields['instType'] = 'Institution Type';
  864.                 $message 'There are <strong>' count($dataResult) . '</strong> experts ';
  865.                 $message.= 'located in <strong>countries with names</strong> starting with "<strong>' $searchParams['countryName'] . '</strong>".';
  866.                 if (isset($searchParams['referrer'])
  867.                     && ($searchParams['referrer'] == 'map')
  868.                 ) {
  869.                     $message 'There are <strong>' count($dataResult) . '</strong> experts ';
  870.                     $message.= 'located in  "<strong>' $searchParams['countryName'] . '</strong>".';
  871.                 }
  872.             }
  873.         }
  874.         if ($searchParams['expertSubject'] != '') {
  875.             $stmtInd $connection->prepare("
  876.                     SELECT 
  877.                         'experts' AS type, 
  878.                         i.status, 
  879.                         i.id_ind, 
  880.                         concat(i.fname,' ',i.sname) AS name,
  881.                         i.fname,
  882.                         i.mname,
  883.                         i.sname,
  884.                         inst_name,
  885.                         jobtitle,
  886.                         i.addr_1,
  887.                         i.addr_2,
  888.                         i.city,
  889.                         i.state,
  890.                         i.postcode,
  891.                         inst.inst_address,
  892.                         inst.addr_2 AS instAddr2,
  893.                         inst.city AS instCity,
  894.                         inst.state AS instState,
  895.                         inst.postcode AS instPostCode, 
  896.                         use_inst_addr, 
  897.                         deceased, 
  898.                         retired, 
  899.                         quality_checked, 
  900.                         (IF(use_inst_addr = 1, ic.country, c.country)) AS country, 
  901.                         'zz' AS instType, 
  902.                         'zz' AS eventtype, 
  903.                         '' AS inst_logo
  904.                     FROM 
  905.                         indiv i
  906.                         LEFT JOIN countries c on c.id_country =  i.country_code
  907.                         LEFT JOIN indiv_institution ii on ii.id_ind = i.id_ind
  908.                         LEFT JOIN institutions inst on inst.id_inst = ii.id_inst
  909.                         LEFT JOIN indiv_subjects ins on ins.id_ind = i.id_ind
  910.                         LEFT JOIN countries ic on ic.id_country =  inst.country_code
  911.                     WHERE 
  912.                         (ins.id_sub = :expertSubject)
  913.                         " $searchParams['indivStatus'] . 
  914.                     ORDER BY sname ASC
  915.                 ');
  916.             $stmtInd->bindValue('expertSubject'$searchParams['expertSubject']);
  917.             $stmtInd->execute();
  918.             $dataResult $stmtInd->fetchAll();
  919.             $sortFields['jobtitle'] = 'Position';
  920.             $message 'There are <strong>' count($dataResult) . '</strong> experts ';
  921.             $message.= "working in \"<strong>" self::getSubjectAreaById($searchParams['expertSubject'], $doctrine) . "</strong>\".";
  922.         }
  923.         if ($searchParams['expertRegion'] != '') {
  924.             $stmtInd $connection->prepare("
  925.                     SELECT 
  926.                         'experts' AS type, 
  927.                         i.status, 
  928.                         i.id_ind, 
  929.                         concat(i.fname,' ',i.sname) AS name,
  930.                         i.fname,
  931.                         i.mname,
  932.                         i.sname,
  933.                         inst_name,
  934.                         jobtitle,
  935.                         i.addr_1,
  936.                         i.addr_2,
  937.                         i.city,
  938.                         i.state,
  939.                         i.postcode,
  940.                         inst.inst_address,
  941.                         inst.addr_2 AS instAddr2,
  942.                         inst.city AS instCity,
  943.                         inst.state AS instState,
  944.                         inst.postcode AS instPostCode, 
  945.                         use_inst_addr, 
  946.                         deceased, 
  947.                         retired, 
  948.                         quality_checked, 
  949.                         (IF(use_inst_addr = 1, ic.country, c.country)) AS country, 
  950.                         'zz' AS instType, 
  951.                         'zz' AS eventtype, 
  952.                         '' AS inst_logo
  953.                     FROM 
  954.                         indiv i
  955.                         LEFT JOIN countries c on c.id_country =  i.country_code
  956.                         LEFT JOIN indiv_institution ii on ii.id_ind = i.id_ind
  957.                         LEFT JOIN institutions inst on inst.id_inst = ii.id_inst
  958.                         LEFT JOIN countries ic on ic.id_country =  inst.country_code
  959.                     WHERE 
  960.                         FIND_IN_SET (
  961.                             :expertRegion, 
  962.                             i.studyregion
  963.                         )
  964.                         " $searchParams['indivStatus'] . 
  965.                     ORDER BY sname ASC
  966.                 ');
  967.             $stmtInd->bindValue('expertRegion'$searchParams['expertRegion']);
  968.             $stmtInd->execute();
  969.             $dataResult $stmtInd->fetchAll();
  970.             $sortFields['jobtitle'] = 'Position';
  971.             $message 'There are <strong>' count($dataResult) . '</strong> experts ';
  972.             $message.= "working in the \"<strong>" self::getSeaRegionById($searchParams['expertRegion'], $doctrine) . "</strong>\".";
  973.         }
  974.         if ($searchParams['instName'] != '') {
  975.             $stmtInst $connection->prepare("
  976.                     SELECT 
  977.                         'institutions' AS type, 
  978.                         i.activated, 
  979.                         i.id_inst, 
  980.                         insttype_name AS instType, 
  981.                         inst_name,
  982.                         inst_name_eng,
  983.                         inst_address,
  984.                         addr_2,
  985.                         city AS instCity,
  986.                         state AS instState,
  987.                         postcode AS instPostCode,
  988.                         country,
  989.                         activities, 
  990.                         inst_logo, 
  991.                         'zz' AS jobtitle, 
  992.                         'zz' AS eventtype,
  993.                         'zz' AS fname, 
  994.                         'zz' AS sname
  995.                     FROM 
  996.                         institutions i
  997.                         LEFT JOIN countries c on c.id_country =  i.country_code
  998.                         LEFT JOIN insttypes it on it.id_insttype =  i.inst_type_id
  999.                     WHERE 
  1000.                         ( 
  1001.                             inst_name LIKE :instNamePerc
  1002.                             OR inst_name_eng LIKE :instNamePerc 
  1003.                             OR acronym LIKE :instNamePerc
  1004.                         )
  1005.                         " $searchParams['instStatus'] . 
  1006.                     ORDER BY inst_name ASC 
  1007.                 ");
  1008.             $stmtInst->bindValue('instNamePerc'$searchParams['instName'] . '%');
  1009.             $stmtInst->execute();
  1010.             $dataResult $stmtInst->fetchAll();
  1011.             $sortFields['instType'] = 'Institution Type';
  1012.             $sortFields['inst_name'] = 'Institution Name';
  1013.             $searchParams['sortby'] = 'inst_name';
  1014.             $message 'There are <strong>' count($dataResult) . '</strong> institutions ';
  1015.             $message.= 'with their name starting with "<strong>' $searchParams['instName'] . '</strong>".';
  1016.         }
  1017.         if ($searchParams['instCountry'] != '') {
  1018.             $stmtInst $connection->prepare("
  1019.                     SELECT 
  1020.                         'institutions' AS type, 
  1021.                         i.activated, 
  1022.                         i.id_inst, 
  1023.                         insttype_name AS instType, 
  1024.                         inst_name,
  1025.                         inst_name_eng,
  1026.                         inst_address,
  1027.                         addr_2,
  1028.                         city AS instCity,
  1029.                         state AS instState,
  1030.                         postcode AS instPostCode,
  1031.                         country,
  1032.                         activities, 
  1033.                         inst_logo, 
  1034.                         'zz' AS jobtitle, 
  1035.                         'zz' AS eventtype,
  1036.                         'zz' AS fname, 
  1037.                         'zz' AS sname
  1038.                     FROM 
  1039.                         institutions i
  1040.                         LEFT JOIN countries c on c.id_country =  i.country_code
  1041.                         LEFT JOIN insttypes it on it.id_insttype =  i.inst_type_id
  1042.                     WHERE 
  1043.                         ( c.country LIKE :instCountryPerc )
  1044.                         " $searchParams['instStatus'] . 
  1045.                     ORDER BY inst_name ASC 
  1046.                 ');
  1047.             $stmtInst->bindValue('instCountryPerc'$searchParams['instCountry'] . '%');
  1048.             $stmtInst->execute();
  1049.             $dataResult $stmtInst->fetchAll();
  1050.             $sortFields['instType'] = 'Institution Type';
  1051.             $sortFields['inst_name'] = 'Institution Name';
  1052.             $searchParams['sortby'] = 'inst_name';
  1053.             $message 'There are <strong>' count($dataResult) . '</strong> institutions ';
  1054.             $message.= 'located in countries with names starting with "<strong>' $searchParams['instCountry'] . '</strong>".';
  1055.         }
  1056.         if ($searchParams['instRegion'] != '') {
  1057.             $stmtInst $connection->prepare("
  1058.                     SELECT 
  1059.                         'institutions' AS type, 
  1060.                         i.activated, 
  1061.                         i.id_inst, 
  1062.                         insttype_name AS instType, 
  1063.                         inst_name,
  1064.                         inst_name_eng,
  1065.                         inst_address,
  1066.                         addr_2,
  1067.                         city AS instCity,
  1068.                         state AS instState,
  1069.                         postcode AS instPostCode,
  1070.                         country,
  1071.                         activities,
  1072.                         inst_logo, 
  1073.                         'zz' AS jobtitle, 
  1074.                         'zz' AS eventtype,
  1075.                         'zz' AS fname, 
  1076.                         'zz' AS sname
  1077.                     FROM 
  1078.                         institutions i
  1079.                         LEFT JOIN countries c on c.id_country =  i.country_code
  1080.                         LEFT JOIN insttypes it on it.id_insttype =  i.inst_type_id
  1081.                     WHERE 
  1082.                         FIND_IN_SET (
  1083.                             :instRegion, 
  1084.                             i.inst_region
  1085.                         )
  1086.                         " $searchParams['instStatus'] . 
  1087.                     ORDER BY inst_name ASC 
  1088.                 ');
  1089.             $stmtInst->bindValue('instRegion'$searchParams['instRegion']);
  1090.             $stmtInst->execute();
  1091.             $dataResult $stmtInst->fetchAll();
  1092.             $sortFields['instType'] = 'Institution Type';
  1093.             $sortFields['inst_name'] = 'Institution Name';
  1094.             $searchParams['sortby'] = 'inst_name';
  1095.             $message 'There are <strong>' count($dataResult) . '</strong> institutions ';
  1096.             $message.= 'working in the "<strong>' self::getSeaRegionById($searchParams['instRegion'], $doctrine) . '</strong>".';
  1097.         }
  1098.         if ($searchParams['instType'] != '') {
  1099.             $stmtInst $connection->prepare("
  1100.                     SELECT 
  1101.                         'institutions' AS type, 
  1102.                         i.activated, 
  1103.                         i.id_inst, 
  1104.                         insttype_name AS instType, 
  1105.                         inst_name,
  1106.                         inst_name_eng,
  1107.                         inst_address,
  1108.                         addr_2,
  1109.                         city AS instCity,
  1110.                         state AS instState,
  1111.                         postcode AS instPostCode,
  1112.                         country,
  1113.                         activities,
  1114.                         inst_logo, 
  1115.                         'zz' AS jobtitle, 
  1116.                         'zz' AS eventtype,
  1117.                         'zz' AS fname, 
  1118.                         'zz' AS sname
  1119.                     FROM 
  1120.                         institutions i
  1121.                         LEFT JOIN countries c on c.id_country =  i.country_code
  1122.                         LEFT JOIN insttypes it on it.id_insttype =  i.inst_type_id
  1123.                     WHERE 
  1124.                         ( i.inst_type_id = :instType) 
  1125.                         " $searchParams['instStatus'] . 
  1126.                     ORDER BY inst_name ASC 
  1127.                 ');
  1128.             $stmtInst->bindValue('instType'$searchParams['instType']);
  1129.             $stmtInst->execute();
  1130.             $dataResult $stmtInst->fetchAll();
  1131.             $sortFields['instType'] = 'Institution Type';
  1132.             $sortFields['inst_name'] = 'Institution Name';
  1133.             $searchParams['sortby'] = 'inst_name';
  1134.             $message 'There are <strong>' count($dataResult) . '</strong> institutions ';
  1135.             $message.= 'with type "<strong>' SearchController::getInstituteTypeById($searchParams['instType'], $doctrine) . '</strong>".';
  1136.         }
  1137.         //status=0 means everything is ok
  1138.         return array(
  1139.             'status' => $status,
  1140.             'dataResult' => $dataResult,
  1141.             'sortFields' => $sortFields,
  1142.             'sortby' => $searchParams['sortby'],
  1143.             'message' => $message,
  1144.             'searchResultType' => $searchResultType
  1145.         );
  1146.     }
  1147.     /**
  1148.      * get the results for the search when action=advSearch
  1149.      *
  1150.      * @param array  $searchParams all the search parameters that have been passed in this request
  1151.      * @param object $doctrine
  1152.      *
  1153.      * @return array with the results
  1154.      */
  1155.     public static function getAdvSearchResults(array $searchParamsobject $doctrine): array
  1156.     {
  1157.         $connection $doctrine->getManager()->getConnection();
  1158.         $searchResultType 'Advanced Search Results';
  1159.         $dataResult = array();
  1160.         $sortBy $searchParams['sortby'];
  1161.         //$searchParams['sortby'] = 'sname';
  1162.         $messageStart 'Your search for ';
  1163.         $messagePart '';
  1164.         $sortFields $searchParams['sortFields'];
  1165.         if (isset($searchParams['searchType'])
  1166.             && isset($searchParams['searchType'][0])
  1167.             && $searchParams['searchType'][0] === 'all'
  1168.             && !count($searchParams['queryString'])
  1169.         ) {
  1170.             //we don't want a search for all types with specific search params
  1171.             return array(
  1172.                 'status' => 0,
  1173.                 'dataResult' => array(),
  1174.                 'sortFields' => $sortFields,
  1175.                 'sortby' => 'relevance',
  1176.                 'message' => 'we cannot show the results, too many results!!!!',
  1177.                 'searchResultType' => $searchResultType
  1178.             );
  1179.         } elseif (isset($searchParams['searchType'])
  1180.             && count($searchParams['searchType']) > 0
  1181.         ) {
  1182.             //where to store the results for the different types
  1183.             $dataResultInd = array();
  1184.             $dataResultInst = array();
  1185.             $dataResultEvnt = array();
  1186.             $dataResultDocument = array();
  1187.             $instCondCnt 1;
  1188.             $endQueryInst false;
  1189.             $endQueryInd false;
  1190.             $endQueryEvent false;
  1191.             $endQueryDocument false;
  1192.             $whereInd ' WHERE ( 1=1 ';
  1193.             $whereInst ' WHERE ( 1=1 ';
  1194.             $whereEvent ' WHERE ( 1=1 ';
  1195.             $whereDocument '';
  1196.             $joinsInd '';
  1197.             $searchParams['searchToggle'][-1] = ' AND ';
  1198.             $operationInd ' AND ';
  1199.             $operationInst ' AND ';
  1200.             $operationEvnt ' AND ';
  1201.             $operation '';
  1202.             foreach ($searchParams['searchType'] as $key => $type) {
  1203.                 if (isset($searchParams['searchKeywords'])
  1204.                     && isset($searchParams['searchKeywords'][$key])
  1205.                 ) {
  1206.                     if (is_array($searchParams['searchKeywords'][$key])) {
  1207.                         foreach($searchParams['searchKeywords'][$key] as $value) {
  1208.                             $string[] = addcslashes($value,'\'');
  1209.                         }
  1210.                     } else {
  1211.                         $string addcslashes($searchParams['searchKeywords'][$key], '\'');
  1212.                     }
  1213.                 } else {
  1214.                     $string '';
  1215.                 }
  1216.                 //what kind of filter are we using
  1217.                 //there should be one at least, except for type=all
  1218.                 if (isset($searchParams['searchFilter'][$key])) {
  1219.                     $value $searchParams['searchFilter'][$key] ?? '';
  1220.                 } else {
  1221.                     $value $searchParams['searchFilter'][array_key_last($searchParams['searchFilter'])];
  1222.                 }
  1223.                 if ($type != 'all'
  1224.                     && $value == ''
  1225.                 ) {
  1226.                     continue;
  1227.                 }
  1228.                 if ($type == 'experts') {
  1229.                     if ($sortBy == '') {
  1230.                         $sortBy 'sname';
  1231.                     }
  1232.                     $bindValues[$type][$key] = array();
  1233.                     $selectQryInd "
  1234.                             SELECT 
  1235.                                 'experts' AS type, 
  1236.                                 i.status, 
  1237.                                 i.id_ind, 
  1238.                                 concat(i.fname,' ',i.sname) AS name,
  1239.                                 i.fname,
  1240.                                 i.mname,
  1241.                                 i.sname,
  1242.                                 inst.inst_name,
  1243.                                 i.jobtitle,
  1244.                                 i.addr_1,
  1245.                                 i.addr_2,
  1246.                                 i.city,
  1247.                                 i.state,
  1248.                                 i.postcode,
  1249.                                 inst.inst_address,
  1250.                                 inst.addr_2 AS instAddr2,
  1251.                                 inst.city AS instCity,
  1252.                                 inst.state AS instState,
  1253.                                 inst.postcode AS instPostCode, 
  1254.                                 i.use_inst_addr, 
  1255.                                 deceased, 
  1256.                                 retired, 
  1257.                                 quality_checked, 
  1258.                                 (CASE WHEN i.use_inst_addr = 1 THEN ic.country ELSE c.country END) AS country, 
  1259.                                 'zz' AS instType, 
  1260.                                 'zz' AS eventtype, 
  1261.                                 '' AS inst_logo ";
  1262.                     $commonJoins ' LEFT JOIN countries c on c.id_country =  i.country_code
  1263.                                 LEFT JOIN indiv_institution ii on ii.id_ind = i.id_ind
  1264.                                 LEFT JOIN institutions inst on inst.id_inst = ii.id_inst
  1265.                                 LEFT JOIN countries ic on ic.id_country =  inst.country_code ';
  1266.                     $matchOrder '';
  1267.                     switch ($value) {
  1268.                         case 'Name contains':
  1269.                             $newString self::fulltext($string);
  1270.                             $selectQryInd .= " , MATCH (fname, mname, sname) AGAINST ('$newString' IN BOOLEAN MODE) AS relevance ";
  1271.                             $whereInd .= ' ' $operationInd " ( i.fname LIKE  :percString$key
  1272.                                                     OR i.fname LIKE  :stringPerc$key
  1273.                                                     OR i.mname LIKE  :percString$key
  1274.                                                     OR i.mname LIKE  :stringPerc$key
  1275.                                                     OR i.sname LIKE  :percString$key
  1276.                                                     OR i.sname LIKE  :stringPerc$key
  1277.                                                     OR MATCH (fname, mname, sname) AGAINST ('$newString' IN BOOLEAN MODE) ) ";
  1278.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1279.                             $messagePart .= "in $type ";
  1280.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1281.                             $matchOrder ' ORDER BY relevance DESC';
  1282.                             //remember what value has to be bound to what placeholder
  1283.                             $bindValues[$type][$key] = array(
  1284.                                 'percString' $key => '%' $string,
  1285.                                 'stringPerc' $key => $string '%'
  1286.                             );
  1287.                             break;
  1288.                         case 'First name contains':
  1289.                             $whereInd .= ' ' $operationInd " ( 
  1290.                                                        i.fname LIKE  :percString$key
  1291.                                                     OR i.fname LIKE  :stringPerc$key ) ";
  1292.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1293.                             $messagePart .= "in $type ";
  1294.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1295.                             //remember what value has to be bound to what placeholder
  1296.                             $bindValues[$type][$key] = array(
  1297.                                 'percString' $key => '%' $string,
  1298.                                 'stringPerc' $key => $string '%'
  1299.                             );
  1300.                             break;
  1301.                         case 'Last name contains':
  1302.                             $whereInd .= ' ' $operationInd "( 
  1303.                                                        i.sname LIKE  :percString$key
  1304.                                                     OR i.sname LIKE  :stringPerc$key ) ";
  1305.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1306.                             $messagePart .= "in $type ";
  1307.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1308.                             //remember what value has to be bound to what placeholder
  1309.                             $bindValues[$type][$key] = array(
  1310.                                 'percString' $key => '%' $string,
  1311.                                 'stringPerc' $key => $string '%'
  1312.                             );
  1313.                             break;
  1314.                         case 'Current address contains':
  1315.                             $whereInd .= ' ' $operationInd 
  1316.                                         ( CASE 
  1317.                                             WHEN i.use_inst_addr = 1 THEN 
  1318.                                                 inst.inst_address LIKE  :percStringPerc$key
  1319.                                                     OR inst.addr_2 LIKE  :percStringPerc$key
  1320.                                                     OR inst.city LIKE  :percStringPerc$key
  1321.                                                     OR inst.state LIKE  :percStringPerc$key
  1322.                                             ELSE 
  1323.                                                 i.addr_1 LIKE  :percStringPerc$key
  1324.                                                     OR i.addr_2 LIKE  :percStringPerc$key
  1325.                                                     OR i.city LIKE  :percStringPerc$key
  1326.                                                     OR i.state LIKE  :percStringPerc$key
  1327.                                                     OR i.private_address LIKE  :percStringPerc$key
  1328.                                             END
  1329.                                         ) ";
  1330.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1331.                             $messagePart .= "in $type ";
  1332.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1333.                             //remember what value has to be bound to what placeholder
  1334.                             $bindValues[$type][$key] = array(
  1335.                                 'percStringPerc' $key => '%' $string '%'
  1336.                             );
  1337.                             break;
  1338.                         /*
  1339.                          * should not be used
  1340.                          * #680
  1341.                         case 'Current/Previous addresses contain':
  1342.                             $joinsInd .= ' LEFT JOIN previous_addresses pa on pa.id_record = i.id_ind ';
  1343.                             $whereInd .= ' ' . $operationInd . "
  1344.                                             ( CASE
  1345.                                                 WHEN i.use_inst_addr = 1 THEN
  1346.                                                     inst.inst_address LIKE  :percStringPerc$key
  1347.                                                         OR inst.addr_2 LIKE  :percStringPerc$key
  1348.                                                         OR inst.city LIKE  :percStringPerc$key
  1349.                                                         OR inst.state LIKE  :percStringPerc$key
  1350.                                                 ELSE
  1351.                                                     i.addr_1 LIKE  :percStringPerc$key
  1352.                                                         OR i.addr_2 LIKE  :percStringPerc$key
  1353.                                                         OR i.city LIKE  :percStringPerc$key
  1354.                                                         OR i.state LIKE  :percStringPerc$key
  1355.                                                         OR i.private_address LIKE  :percStringPerc$key
  1356.                                                         OR pa.city LIKE  :percStringPerc$key
  1357.                                                         OR pa.state LIKE  :percStringPerc$key
  1358.                                                         OR pa.address LIKE  :percStringPerc$key
  1359.                                                 END
  1360.                                             ) ";
  1361.                             $messagePart .= "$operation \"<strong>" . self::cleaned($string) . "</strong>\" ";
  1362.                             $messagePart .= "in $type ";
  1363.                             $messagePart .= "using filter \"<strong>" . self::readableFilter($value) . "</strong>\" ";
  1364.                             //remember what value has to be bound to what placeholder
  1365.                             $bindValues[$type][$key] = array(
  1366.                                 'percStringPerc' . $key => '%' . $string . '%'
  1367.                             );
  1368.                             break;
  1369.                         */
  1370.                         case 'Phone/Fax contains':
  1371.                             $whereInd .= ' ' $operationInd " (
  1372.                                                        i.tel LIKE  :percStringPerc$key
  1373.                                                     OR i.fax LIKE  :percStringPerc$key
  1374.                                                     )";
  1375.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1376.                             $messagePart .= "in $type ";
  1377.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1378.                             //remember what value has to be bound to what placeholder
  1379.                             $bindValues[$type][$key] = array(
  1380.                                 'percStringPerc' $key => '%' $string '%'
  1381.                             );
  1382.                             break;
  1383.                         case 'Email contains':
  1384.                             $whereInd .= ' ' $operationInd " (
  1385.                                                        i.email1 LIKE  :percStringPerc$key
  1386.                                                     OR i.email2 LIKE  :percStringPerc$key
  1387.                                                     OR i.email3 LIKE  :percStringPerc$key
  1388.                                                     ) ";
  1389.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1390.                             $messagePart .= "in $type ";
  1391.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1392.                             //remember what value has to be bound to what placeholder
  1393.                             $bindValues[$type][$key] = array(
  1394.                                 'percStringPerc' $key => '%' $string '%'
  1395.                             );
  1396.                             break;
  1397.                         case 'Website URL contains':
  1398.                             $whereInd .= ' ' $operationInd " (
  1399.                                                        i.url1 LIKE  :percStringPerc$key
  1400.                                                     OR i.url2 LIKE  :percStringPerc$key
  1401.                                                     OR i.url3 LIKE  :percStringPerc$key
  1402.                                                     ) ";
  1403.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1404.                             $messagePart .= "in $type ";
  1405.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1406.                             //remember what value has to be bound to what placeholder
  1407.                             $bindValues[$type][$key] = array(
  1408.                                 'percStringPerc' $key => '%' $string '%'
  1409.                             );
  1410.                             break;
  1411.                         case 'Country is':
  1412.                             $whereInd .= ' ' $operationInd 
  1413.                                             ( CASE 
  1414.                                                 WHEN i.use_inst_addr = 1 THEN
  1415.                                                        inst.country_code = $string
  1416.                                                 ELSE
  1417.                                                        i.country_code = $string
  1418.                                                 END
  1419.                                             ) ";
  1420.                             $messagePart .= "$operation $type from  \"<strong>" self::getCountryById($string$doctrine) . "</strong>\" ";
  1421.                             break;
  1422.                         case 'In multiple countries':
  1423.                             $string implode(','$string);
  1424.                             $whereInd .= ' ' $operationInd 
  1425.                                             ( CASE 
  1426.                                                 WHEN i.use_inst_addr = 1 THEN
  1427.                                                     inst.country_code in ($string)
  1428.                                                 ELSE
  1429.                                                     i.country_code in ($string)
  1430.                                                 END
  1431.                                             ) ";
  1432.                             $messagePart .= "$operation $type from  \"<strong>" implode(', 'self::getMultipleCountryById($string$doctrine)) . "</strong>\" ";
  1433.                             break;
  1434.                         case 'Sea regions of study is':
  1435.                             $whereInd .= ' ' $operationInd " (
  1436.                                                        FIND_IN_SET ('$string', i.studyregion)
  1437.                                                     ) ";
  1438.                             $messagePart .= "$operation \"<strong>" self::getRegionById($string$doctrine) . "</strong>\" ";
  1439.                             $messagePart .= "in $type ";
  1440.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1441.                             break;
  1442.                         case 'Member of group or sub-group':
  1443.                             $groups implode(','self::getChildGroup($string$doctrine));
  1444.                             $joinsInd .= ' LEFT JOIN member_groups mg ON mg.id_ind = i.id_ind ';
  1445.                             $whereInd .= ' ' $operationInd " (
  1446.                                                         mg.id_group IN ($groups)
  1447.                                                     ) ";
  1448.                             $messagePart .= "$operation \"<strong>" self::getGroupById($string$doctrine) . "</strong>\" ";
  1449.                             $messagePart .= "in $type ";
  1450.                             $messagePart .= "using filter \"<strong>Member of group OR sub-group</strong>\" ";
  1451.                             break;
  1452.                         case 'Job type is':
  1453.                             $joinsInd .= " LEFT JOIN indiv_jobtype ij ON ij.id_ind = i.id_ind ";
  1454.                             $whereInd .= ' ' $operationInd " (
  1455.                                                        id_job = $string
  1456.                                                     ) ";
  1457.                             $messagePart .= "$operation \"<strong>" self::getJobTypesById($string$doctrine) . "</strong>\" ";
  1458.                             $messagePart .= "in $type ";
  1459.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1460.                             break;
  1461.                         case 'Job title contains':
  1462.                             $whereInd .= ' ' $operationInd " (
  1463.                                                        i.jobtitle LIKE  :percString$key
  1464.                                                     OR i.jobtitle LIKE  :stringPerc$key
  1465.                                                     ) ";
  1466.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1467.                             $messagePart .= "in $type ";
  1468.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1469.                             //remember what value has to be bound to what placeholder
  1470.                             $bindValues[$type][$key] = array(
  1471.                                 'percString' $key => '%' $string,
  1472.                                 'stringPerc' $key => $string '%'
  1473.                             );
  1474.                             break;
  1475.                         case 'Department contains':
  1476.                             $whereInd .= ' ' $operationInd " (
  1477.                                                        i.dept LIKE  :percString$key
  1478.                                                     OR i.dept LIKE  :stringPerc$key
  1479.                                                     ) ";
  1480.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1481.                             $messagePart .= "in $type ";
  1482.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1483.                             //remember what value has to be bound to what placeholder
  1484.                             $bindValues[$type][$key] = array(
  1485.                                 'percString' $key => '%' $string,
  1486.                                 'stringPerc' $key => $string '%'
  1487.                             );
  1488.                             break;
  1489.                         case 'Institution name contains':
  1490.                             $whereInd .= ' ' $operationInd " (
  1491.                                                        inst.inst_name LIKE  :percStringPerc$key
  1492.                                                     OR inst.inst_name_eng LIKE  :percStringPerc$key
  1493.                                                     OR inst.acronym LIKE  :percStringPerc$key
  1494.                                                     ) ";
  1495.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1496.                             $messagePart .= "in $type ";
  1497.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1498.                             //remember what value has to be bound to what placeholder
  1499.                             $bindValues[$type][$key] = array(
  1500.                                 'percStringPerc' $key => '%' $string '%'
  1501.                             );
  1502.                             break;
  1503.                         case 'Subject Area is':
  1504.                             $joinsInd .= " LEFT JOIN indiv_meta im ON im.indiv_id = i.id_ind ";
  1505.                             $whereInd .= ' ' $operationInd " (
  1506.                                                        im.meta_option = 'researcharea'
  1507.                                                     AND im.meta_value LIKE  :percStringPerc$key
  1508.                                                     ) ";
  1509.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1510.                             $messagePart .= "in $type ";
  1511.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1512.                             //remember what value has to be bound to what placeholder
  1513.                             $bindValues[$type][$key] = array(
  1514.                                 'percStringPerc' $key => '%' $string '%'
  1515.                             );
  1516.                             break;
  1517.                         case 'Activities include':
  1518.                             $whereInd .= ' ' $operationInd " (
  1519.                                                        i.activeng LIKE  :percStringPerc$key
  1520.                                                     ) ";
  1521.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1522.                             $messagePart .= "in $type ";
  1523.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1524.                             //remember what value has to be bound to what placeholder
  1525.                             $bindValues[$type][$key] = array(
  1526.                                 'percStringPerc' $key => '%' $string '%'
  1527.                             );
  1528.                             break;
  1529.                         case 'Citations include':
  1530.                             $joinsInd .= " LEFT JOIN indiv_meta im ON im.indiv_id = i.id_ind ";
  1531.                             $whereInd .= ' ' $operationInd " (
  1532.                                                         (
  1533.                                                             im.meta_option = 'researcharea'
  1534.                                                             AND im.meta_value LIKE  :percStringPerc$key
  1535.                                                         )
  1536.                                                         OR (
  1537.                                                             im.meta_option = 'other-citation'
  1538.                                                             AND im.meta_value LIKE  :percStringPerc$key
  1539.                                                         )
  1540.                                                         OR (
  1541.                                                             im.meta_option = 'researchgate'
  1542.                                                             AND im.meta_value LIKE  :percStringPerc$key
  1543.                                                         )
  1544.                                                     ) ";
  1545.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1546.                             $messagePart .= "in $type ";
  1547.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1548.                             //remember what value has to be bound to what placeholder
  1549.                             $bindValues[$type][$key] = array(
  1550.                                 'percStringPerc' $key => '%' $string '%'
  1551.                             );
  1552.                             break;
  1553.                         case 'Working languages includes':
  1554.                             $whereInd .= ' ' $operationInd " (
  1555.                                                        FIND_IN_SET ('$string', i.languages)
  1556.                                                     ) ";
  1557.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1558.                             $messagePart .= "in $type ";
  1559.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1560.                             break;
  1561.                         case 'Degree contains':
  1562.                             $whereInd .= ' ' $operationInd " (
  1563.                                                        i.degree LIKE  :percStringPerc$key
  1564.                                                     ) ";
  1565.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1566.                             $messagePart .= "in $type ";
  1567.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1568.                             //remember what value has to be bound to what placeholder
  1569.                             $bindValues[$type][$key] = array(
  1570.                                 'percStringPerc' $key => '%' $string '%'
  1571.                             );
  1572.                             break;
  1573.                         case 'Is retired':
  1574.                             $whereInd .= ' ' $operationInd " (
  1575.                                                        i.retired = $string
  1576.                                                     ) ";
  1577.                             $messagePart .= "$operation experts who are \"<strong>Retired</strong>\" ";
  1578.                             break;
  1579.                         case 'Is deceased':
  1580.                             $whereInd .= ' ' $operationInd " (
  1581.                                                        i.deceased = $string
  1582.                                                     ) ";
  1583.                             $messagePart .= "$operation  experts who are \"<strong>Deceased</strong>\" ";
  1584.                             break;
  1585.                         case 'Is quality checked':
  1586.                             $whereInd .= ' ' $operationInd " (
  1587.                                                        i.quality_checked = $string
  1588.                                                     ) ";
  1589.                             $messagePart .= "$operation experts who are \"<strong>Quality checked</strong>\" ";
  1590.                             break;
  1591.                         case 'Comment contains':
  1592.                             $whereInd .= ' ' $operationInd " (
  1593.                                                        i.comments LIKE  :percStringPerc$key
  1594.                                                     ) ";
  1595.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1596.                             $messagePart .= "in $type ";
  1597.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1598.                             //remember what value has to be bound to what placeholder
  1599.                             $bindValues[$type][$key] = array(
  1600.                                 'percStringPerc' $key => '%' $string '%'
  1601.                             );
  1602.                             break;
  1603.                         case 'Is Active':
  1604.                             $whereInd .= ' ' $operationInd " (
  1605.                                                         i.status = $string
  1606.                                                 ) ";
  1607.                             if ($string == '1') {
  1608.                                 $messagePart .= "$operation expert is <strong>Active</strong> ";
  1609.                             } else {
  1610.                                 $messagePart .= "$operation expert is <strong>not active</strong> ";
  1611.                             }
  1612.                             break;
  1613.                         case 'Gender is':
  1614.                             $whereInd .= ' ' $operationInd " ( 
  1615.                                                         i.gender = '$string'
  1616.                                                 ) ";
  1617.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\"  \"<strong>$string</strong>\" ";
  1618.                             break;
  1619.                         case 'Do Not Invite':
  1620.                             $whereInd .= ' ' $operationInd " (
  1621.                                                         i.do_not_invite = 1
  1622.                                                 ) ";
  1623.                             $messagePart .= "$operation $type using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1624.                             break;
  1625.                         case 'Unique Identifier':
  1626.                             $whereInd .= ' ' $operationInd " (
  1627.                                                 i.id_ind = '$string'
  1628.                                                 ) ";
  1629.                             $messagePart .= "$operation $type having unique identifier \"<strong>$string</strong>\" ";
  1630.                             break;
  1631.                         case 'Updated':
  1632.                             $startDate = isset($searchParams['startDate'][$key]) ? "l_date_upd >= '" self::cleanDate($searchParams['startDate'][$key]) . "' " "1=1 ";
  1633.                             $endDate = isset($searchParams['endDate'][$key]) ? "and l_date_upd <= '" self::cleanDate($searchParams['endDate'][$key]) . "' " " AND 1=1 ";
  1634.                             $whereInd .= ' ' $operationInd ' ' $startDate ' ' $endDate;
  1635.                             $messagePart .= "$operation $type updated ";
  1636.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  1637.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  1638.                             break;
  1639.                         case 'Created':
  1640.                             $startDate = isset($searchParams['startDate'][$key]) ? "f_date_ent >= '" self::cleanDate($searchParams['startDate'][$key]) . "' " "1=1 ";
  1641.                             $endDate = isset($searchParams['endDate'][$key]) ? "and f_date_ent <= '" self::cleanDate($searchParams['endDate'][$key]) . "' " " AND 1=1 ";
  1642.                             $whereInd .= ' ' $operationInd ' ' $startDate ' ' $endDate;
  1643.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1644.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  1645.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  1646.                             break;
  1647.                         case 'Quality last checked':
  1648.                             $startDate = isset($searchParams['startDate'][$key]) ? "quality_checked_date > '" self::cleanDate($searchParams['startDate'][$key]) . "' " "1=1 ";
  1649.                             $endDate = isset($searchParams['endDate'][$key]) ? "and quality_checked_date < '" self::cleanDate($searchParams['endDate'][$key]) . "' " " AND 1=1 ";
  1650.                             $whereInd .= ' ' $operationInd ' ' $startDate ' ' $endDate;
  1651.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1652.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  1653.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  1654.                             break;
  1655.                         default:
  1656.                             break;
  1657.                     }
  1658.                     $operationInd $searchParams['searchToggle'][$key] ?? " OR ";
  1659.                     $operation $searchParams['searchToggle'][$key] ?? " OR ";
  1660.                 } elseif ($type == 'institutions') {
  1661.                     if ($sortBy == '') {
  1662.                         $sortBy 'name';
  1663.                     }
  1664.                     $bindValues[$type][$key] = array();
  1665.                     $selectQryInst "
  1666.                             SELECT 
  1667.                                 'institutions' AS type, 
  1668.                                 i.activated, 
  1669.                                 i.id_inst, 
  1670.                                 it.insttype_name AS instType, 
  1671.                                 i.inst_name,
  1672.                                 i.inst_name_eng,
  1673.                                 i.inst_address,
  1674.                                 i.addr_2,
  1675.                                 i.city AS instCity,
  1676.                                 i.state AS instState,
  1677.                                 i.postcode AS instPostCode,
  1678.                                 c.country,
  1679.                                 i.activities,
  1680.                                 i.inst_logo,
  1681.                                 i.parent_id,   
  1682.                                 '' AS jobtitle, 
  1683.                                 '' AS eventtype,
  1684.                                 '' AS fname, 
  1685.                                 '' AS sname
  1686.                             FROM 
  1687.                                 institutions i
  1688.                         ";
  1689.                     $joinsInst ' LEFT JOIN countries c ON c.id_country =  i.country_code
  1690.                                         LEFT JOIN insttypes it ON it.id_insttype =  i.inst_type_id ';
  1691.                     switch ($value) {
  1692.                         case 'Name contains':
  1693.                             $whereInst .= ' ' $operationInst " ( 
  1694.                                                    i.inst_name LIKE  :percStringPerc$key
  1695.                                                 OR i.inst_name_eng LIKE  :percStringPerc$key
  1696.                                                 OR i.acronym LIKE  :percStringPerc$key
  1697.                                                         ) ";
  1698.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1699.                             $messagePart .= "in $type ";
  1700.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1701.                             //remember what value has to be bound to what placeholder
  1702.                             $bindValues[$type][$key] = array(
  1703.                                 'percStringPerc' $key => '%' $string '%'
  1704.                             );
  1705.                             break;
  1706.                         case 'Current address contains':
  1707.                             $whereInst .= ' ' $operationInst " ( 
  1708.                                                           i.inst_address LIKE  :percStringPerc$key
  1709.                                                     OR i.addr_2 LIKE  :percStringPerc$key
  1710.                                                     OR i.city LIKE  :percStringPerc$key
  1711.                                                     OR i.state LIKE  :percStringPerc$key
  1712.                                                        ) ";
  1713.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1714.                             $messagePart .= "in $type ";
  1715.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1716.                             //remember what value has to be bound to what placeholder
  1717.                             $bindValues[$type][$key] = array(
  1718.                                 'percStringPerc' $key => '%' $string '%'
  1719.                             );
  1720.                             break;
  1721.                         /*
  1722.                          * #680
  1723.                          *
  1724.                         case 'Current/Previous addresses contain':
  1725.                             $whereInst .= ' ' . $operationInst . " (
  1726.                                                           i.inst_address LIKE  :percString$key
  1727.                                                     OR i.addr_2 LIKE  :percStringPerc$key
  1728.                                                     OR i.city LIKE  :percStringPerc$key
  1729.                                                     OR i.state LIKE  :percStringPerc$key
  1730.                                                        ) ";
  1731.                             $messagePart .= "$operation \"<strong>" . self::cleaned($string) . "</strong>\" ";
  1732.                             $messagePart .= "in $type ";
  1733.                             $messagePart .= "using filter \"<strong>" . self::readableFilter($value) . "</strong>\" ";
  1734.                             //remember what value has to be bound to what placeholder
  1735.                             $bindValues[$type][$key] = array(
  1736.                                 'percString' . $key => '%' . $string,
  1737.                                 'percStringPerc' . $key => '%' . $string . '%'
  1738.                             );
  1739.                             break;
  1740.                         */
  1741.                         case 'Country is':
  1742.                             $whereInst .= ' ' $operationInst " ( 
  1743.                                                           i.country_code = $string
  1744.                                                        ) ";
  1745.                             $messagePart .= "$operation $type ";
  1746.                             $messagePart .= "from  \"<strong>" self::getCountryById($string$doctrine) . "</strong>\" ";
  1747.                             break;
  1748.                         case 'Sea regions of study is':
  1749.                             $whereInst .= ' ' $operationInst " ( 
  1750.                                                           FIND_IN_SET ('$string', i.inst_region)
  1751.                                                        ) ";
  1752.                             $messagePart .= "$operation $type ";
  1753.                             $messagePart .= "having $value \"<strong>" self::getRegionById($string$doctrine) . "</strong>\" ";
  1754.                             break;
  1755.                         case 'Type is':
  1756.                             $whereInst .= ' ' $operationInst " ( 
  1757.                                                           i.inst_type_id = $string
  1758.                                                        ) ";
  1759.                             $messagePart .= "$operation $type having " self::readableFilter($value) . " \"<strong>";
  1760.                             $messagePart .= SearchController::getInstituteTypeById($string$doctrine) . "</strong>\"";
  1761.                             break;
  1762.                         case 'Activities contains':
  1763.                             $whereInst .= ' ' $operationInst " (
  1764.                                                     i.activities LIKE  :percStringPerc$key
  1765.                                                     ) ";
  1766.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1767.                             $messagePart .= "in $type ";
  1768.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1769.                             //remember what value has to be bound to what placeholder
  1770.                             $bindValues[$type][$key] = array(
  1771.                                 'percStringPerc' $key => '%' $string '%'
  1772.                             );
  1773.                             break;
  1774.                         case 'Tel/Fax contains':
  1775.                             $whereInst .= ' ' $operationInst " ( 
  1776.                                                           i.inst_tel LIKE  :percStringPerc$key
  1777.                                                     OR i.inst_fax LIKE  :percStringPerc$key
  1778.                                                        ) ";
  1779.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1780.                             $messagePart .= "in $type ";
  1781.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1782.                             //remember what value has to be bound to what placeholder
  1783.                             $bindValues[$type][$key] = array(
  1784.                                 'percStringPerc' $key => '%' $string '%'
  1785.                             );
  1786.                             break;
  1787.                         case 'Website URL contains':
  1788.                             $whereInst .= ' ' $operationInst " ( 
  1789.                                                           i.inst_url LIKE  :percStringPerc$key
  1790.                                                        ) ";
  1791.                             $messagePart .= "$operation \"<strong" self::cleaned($string) . "</strong>\" ";
  1792.                             $messagePart .= "in $type ";
  1793.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1794.                             //remember what value has to be bound to what placeholder
  1795.                             $bindValues[$type][$key] = array(
  1796.                                 'percStringPerc' $key => '%' $string '%'
  1797.                             );
  1798.                             break;
  1799.                         case 'EDMO Code is':
  1800.                             $whereInst .= ' ' $operationInst " ( 
  1801.                                                           i.edmo_code = '$string'
  1802.                                                           AND edmo_code != 0
  1803.                                                           ) ";
  1804.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  1805.                             $messagePart .= "in $type ";
  1806.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1807.                             break;
  1808.                         case 'Updated':
  1809.                             $startDate = isset($searchParams['startDate'][$key]) ? "l_date_updated >= '" self::cleanDate($searchParams['startDate'][$key]) . "' " "1=1 ";
  1810.                             $endDate = isset($searchParams['endDate'][$key]) ? "AND l_date_updated <= '" self::cleanDate($searchParams['endDate'][$key]) . "' " " AND 1=1 ";
  1811.                             $whereInst .= ' ' $operationInst ' ' $startDate ' ' $endDate;
  1812.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1813.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  1814.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  1815.                             break;
  1816.                         case 'Created':
  1817.                             $startDate = isset($searchParams['startDate'][$key]) ? "f_date_entered >= '" self::cleanDate($searchParams['startDate'][$key]) . "' " "1=1 ";
  1818.                             $endDate = isset($searchParams['endDate'][$key]) ? "AND f_date_entered <= '" self::cleanDate($searchParams['endDate'][$key]) . "' " " AND 1=1 ";
  1819.                             $whereInst .= ' ' $operationInst ' ' $startDate ' ' $endDate;
  1820.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  1821.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  1822.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  1823.                             break;
  1824.                     }
  1825.                     $instCondCnt++;
  1826.                     $operationInst $searchParams['searchToggle'][$key] ?? " OR ";
  1827.                     $operation $searchParams['searchToggle'][$key] ?? " OR ";
  1828.                 } elseif ($type == 'all') {
  1829.                     $newString self::fulltext($string);
  1830.                     $stmtInd $connection->prepare("
  1831.                             SELECT 
  1832.                                 'experts' AS type, 
  1833.                                 i.status, 
  1834.                                 i.id_ind, 
  1835.                                 concat(i.fname,' ',i.sname) AS name, 
  1836.                                 i.fname,
  1837.                                 i.mname,
  1838.                                 i.sname, 
  1839.                                 inst_name,
  1840.                                 jobtitle,
  1841.                                 i.addr_1,
  1842.                                 i.addr_2,
  1843.                                 i.city,
  1844.                                 i.state,
  1845.                                 i.postcode,
  1846.                                 inst.inst_address,
  1847.                                 inst.addr_2 AS instAddr2,
  1848.                                 inst.city AS instCity,
  1849.                                 inst.state AS instState,
  1850.                                 inst.postcode AS instPostCode, 
  1851.                                 use_inst_addr, 
  1852.                                 deceased, 
  1853.                                 retired, 
  1854.                                 quality_checked, 
  1855.                                 (IF(use_inst_addr = 1, ic.country, c.country)) AS country, 
  1856.                                 MATCH (fname, sname) AGAINST (:newString IN BOOLEAN MODE) AS relevance, 
  1857.                                 'zz' AS instType, 
  1858.                                 'zz' AS eventtype, 
  1859.                                 '' AS inst_logo
  1860.                             FROM 
  1861.                                 indiv i
  1862.                                 LEFT JOIN countries c ON c.id_country =  i.country_code
  1863.                                 LEFT JOIN indiv_institution ii ON ii.id_ind = i.id_ind
  1864.                                 LEFT JOIN institutions inst ON inst.id_inst = ii.id_inst
  1865.                                 LEFT JOIN countries ic ON ic.id_country =  inst.country_code
  1866.                             WHERE 
  1867.                                 ( 
  1868.                                     MATCH (fname, mname, sname) AGAINST (:newString IN BOOLEAN MODE)
  1869.                                     OR fname LIKE :string
  1870.                                     OR fname LIKE :stringPerc
  1871.                                     OR fname LIKE :percString
  1872.                                     OR mname LIKE :stringPerc
  1873.                                     OR mname LIKE :percString
  1874.                                     OR sname LIKE :stringPerc
  1875.                                     OR sname LIKE :percString
  1876.                                 )
  1877.                                 " $searchParams['indivStatus'] . '
  1878.                             ORDER BY relevance DESC, i.sname ASC;
  1879.                         ');
  1880.                     $stmtInd->bindValue('newString'$newString);
  1881.                     $stmtInd->bindValue('string'$string);
  1882.                     $stmtInd->bindValue('percString''%' $string);
  1883.                     $stmtInd->bindValue('stringPerc'$string '%');
  1884.                     $stmtInd->bindValue('percStringPerc''%' $string '%');
  1885.                     $stmtInd->execute();
  1886.                     $dataResultInd $stmtInd->fetchAll();
  1887.                     $sortFields['jobtitle'] = 'Position';
  1888.                     $stmtInst $connection->prepare("
  1889.                             SELECT 
  1890.                                 'institutions' AS type, 
  1891.                                 i.activated, 
  1892.                                 i.id_inst, 
  1893.                                 insttype_name AS instType, 
  1894.                                 inst_name,
  1895.                                 inst_name_eng,
  1896.                                 inst_address,
  1897.                                 addr_2,
  1898.                                 city AS instCity,
  1899.                                 state AS instState,
  1900.                                 postcode AS instPostCode,
  1901.                                 country,
  1902.                                 activities,
  1903.                                 inst_logo,
  1904.                                 'zz' AS jobtitle, 
  1905.                                 MATCH (inst_name,inst_name_eng,acronym) AGAINST (:newString) AS relevance, 
  1906.                                 'zz' AS eventtype, 
  1907.                                 'zz' AS fname, 
  1908.                                 'zz' AS sname
  1909.                             FROM 
  1910.                                 institutions i
  1911.                                 LEFT JOIN countries c ON c.id_country =  i.country_code
  1912.                                 LEFT JOIN insttypes it ON it.id_insttype =  i.inst_type_id
  1913.                             WHERE 
  1914.                                 (
  1915.                                     MATCH (inst_name, inst_name_eng, acronym) AGAINST (:newString IN BOOLEAN MODE)
  1916.                                     OR inst_name LIKE :percStringPerc
  1917.                                     OR inst_name_eng LIKE :percStringPerc
  1918.                                     OR acronym LIKE :percStringPerc
  1919.                                     OR activities LIKE :percStringPerc
  1920.                                 )
  1921.                                  " $searchParams['instStatus'] . '
  1922.                             ORDER BY relevance DESC, i.inst_name ASC;
  1923.                         ');
  1924.                     $stmtInst->bindValue('newString'$newString);
  1925.                     $stmtInst->bindValue('percStringPerc''%' $string '%');
  1926.                     $stmtInst->execute();
  1927.                     $dataResultInst $stmtInst->fetchAll();
  1928.                     $sortFields['instType'] = 'Institution Type';
  1929.                     /**
  1930.                      *    Will be taken care when PPC
  1931.                      */
  1932.                     $stmtEvnt $connection->prepare("
  1933.                             SELECT 
  1934.                                 'events' AS type, 
  1935.                                 et.eventtype_name AS eventtype, 
  1936.                                 e.status AS activated, 
  1937.                                 e.id_event, 
  1938.                                 e.title AS name, 
  1939.                                 e.start_on, 
  1940.                                 e.end_on, 
  1941.                                 e.address AS addr_1, 
  1942.                                 e.city, 
  1943.                                 e.state, 
  1944.                                 c.country AS country, 
  1945.                                 'zz' AS jobtitle, 
  1946.                                 'zz' AS instType, 
  1947.                                 'zz' AS fname, 
  1948.                                 'zz' AS sname, 
  1949.                                 'zz' AS inst_name
  1950.                             FROM 
  1951.                                 events e
  1952.                                 LEFT JOIN countries c on c.id_country =  e.id_country
  1953.                                 LEFT JOIN eventtypes et on et.id_eventtype =  e.id_eventtype
  1954.                             WHERE 
  1955.                                 (
  1956.                                     title LIKE  :percStringPerc
  1957.                                     OR shorttitle LIKE  :percStringPerc
  1958.                                     OR summary LIKE  :percStringPerc
  1959.                                     OR keywords LIKE  :percStringPerc
  1960.                                 )
  1961.                                 " $searchParams['eventStatus'] . '
  1962.                             ORDER BY start_on desc
  1963.                         ');
  1964.                     $stmtEvnt->bindValue('percStringPerc''%' $string '%');
  1965.                     $stmtEvnt->execute();
  1966.                     $dataResultEvnt $stmtEvnt->fetchAll();
  1967.                     $messagePart .= ' "<strong>' self::cleaned($string) . '</strong>" ';
  1968.                 } elseif ($type == 'events') {
  1969.                     $sortFields['name'] = 'Event Title';
  1970.                     if ($sortBy == '') {
  1971.                         $sortBy 'name';
  1972.                     }
  1973.                     $bindValues[$type][$key] = array();
  1974.                     $selectQryEvnt "
  1975.                             SELECT 
  1976.                                 'events' AS type, 
  1977.                                 et.eventtype_name AS eventtype, 
  1978.                                 e.status AS activated, 
  1979.                                 e.id_event, 
  1980.                                 e.title AS name, 
  1981.                                 e.start_on, 
  1982.                                 e.end_on, 
  1983.                                 e.address AS addr_1, 
  1984.                                 e.city, 
  1985.                                 e.state, 
  1986.                                 c.country AS country, 
  1987.                                 'zz' AS jobtitle, 
  1988.                                 'zz' AS instType,
  1989.                                 'zz' AS fname, 
  1990.                                 'zz' AS sname
  1991.                             FROM 
  1992.                                  events e ";
  1993.                     $joinsEvnt ' LEFT JOIN countries c ON c.id_country = e.id_country
  1994.                                        LEFT JOIN eventtypes et ON et.id_eventtype = e.id_eventtype ';
  1995.                     switch ($value) {
  1996.                         case 'Title contains':
  1997.                             $whereEvent .= ' ' $operationEvnt " (
  1998.                                                   e.title LIKE  :percStringPerc$key
  1999.                                                        ) ";
  2000.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  2001.                             $messagePart .= "in $type ";
  2002.                             $messagePart .= "using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2003.                             //remember what value has to be bound to what placeholder
  2004.                             $bindValues[$type][$key] = array(
  2005.                                 'percStringPerc' $key => '%' $string '%'
  2006.                             );
  2007.                             break;
  2008.                         case 'eType is':
  2009.                             $whereEvent .= ' ' $operationEvnt " (
  2010.                                                   e.id_eventtype = $string
  2011.                                                        ) ";
  2012.                             $messagePart .= "$operation $type ";
  2013.                             $messagePart .= "having Event Type \"<strong>" SearchController::getInstituteTypeById($string$doctrine) . "</strong>\" ";
  2014.                             break;
  2015.                         case 'Summary contains':
  2016.                             $whereEvent .= ' ' $operationEvnt " (
  2017.                                                   e.summary LIKE  :percStringPerc$key
  2018.                                                        ) ";
  2019.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  2020.                             $messagePart .= "in $type using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2021.                             //remember what value has to be bound to what placeholder
  2022.                             $bindValues[$type][$key] = array(
  2023.                                 'percStringPerc' $key => '%' $string '%'
  2024.                             );
  2025.                             break;
  2026.                         case 'Keywords contain':
  2027.                             $whereEvent .= ' ' $operationEvnt " (
  2028.                                                e.keywords LIKE  :percStringPerc$key
  2029.                                               ) ";
  2030.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  2031.                             $messagePart .= "in $type using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2032.                             //remember what value has to be bound to what placeholder
  2033.                             $bindValues[$type][$key] = array(
  2034.                                 'percStringPerc' $key => '%' $string '%'
  2035.                             );
  2036.                             break;
  2037.                         case 'Address contains':
  2038.                             $whereEvent .= ' ' $operationEvnt " (
  2039.                                                e.address LIKE  :percStringPerc$key
  2040.                                                OR e.city LIKE  :percStringPerc$key
  2041.                                                OR e.state LIKE  :percStringPerc$key
  2042.                                               ) ";
  2043.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  2044.                             $messagePart .= "in $type using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2045.                             //remember what value has to be bound to what placeholder
  2046.                             $bindValues[$type][$key] = array(
  2047.                                 'percStringPerc' $key => '%' $string '%'
  2048.                             );
  2049.                             break;
  2050.                         case 'Country is':
  2051.                             $whereEvent .= ' ' $operationEvnt " (
  2052.                                                e.id_country = $string
  2053.                                               ) ";
  2054.                             $messagePart .= "$operation $type ";
  2055.                             $messagePart .= "from  \"<strong>" self::getCountryById($string$doctrine) . "</strong>\" ";
  2056.                             break;
  2057.                         case 'Date between':
  2058.                             $date '';
  2059.                             $messagePart .= "$operation $type with parameters '<strong>" self::readableFilter($value) . "</strong>': ";
  2060.                             if (isset($searchParams['startDate'][$key])
  2061.                                 && $searchParams['startDate'][$key] != ''
  2062.                                 && isset($searchParams['endDate'][$key])
  2063.                                 && $searchParams['endDate'][$key] != ''
  2064.                             ) {
  2065.                                 $startDate self::cleanDate($searchParams['startDate'][$key]);
  2066.                                 $endDate self::cleanDate($searchParams['endDate'][$key]);
  2067.                                 $date " start_on >= '$startDate
  2068.                                         AND end_on <= '$endDate' ";
  2069.                                 $messagePart .= "between '<strong>$startDate</strong>' ";
  2070.                                 $messagePart .= "and '<strong>$endDate</strong>' ";
  2071.                             } elseif (isset($searchParams['startDate'][$key])
  2072.                                 && (!$searchParams['endDate'][$key]
  2073.                                     || $searchParams['endDate'][$key] == '')
  2074.                             ) {
  2075.                                 $startDate self::cleanDate($searchParams['startDate'][$key]);
  2076.                                 $date " start_on >= '$startDate' ";
  2077.                                 $messagePart .= " -> after '<strong>$startDate</strong>' ";
  2078.                             } elseif (isset($searchParams['endDate'][$key])
  2079.                                 && (!$searchParams['startDate'][$key]
  2080.                                     || $searchParams['startDate'][$key] == '')
  2081.                             ) {
  2082.                                 $endDate self::cleanDate($searchParams['endDate'][$key]);
  2083.                                 $date " end_on <= '$endDate' ";
  2084.                                 $messagePart .= " -> before '<strong>$endDate</strong>' ";
  2085.                             } else {
  2086.                                 $whereEvent .= '';
  2087.                             }
  2088.                             $whereEvent .= ' ' $operationInst ' ' $date;
  2089.                             break;
  2090.                         case 'Updated':
  2091.                             $endDate = isset($searchParams['endDate'][$key]) ? "AND updated_at <= '" self::cleanDate($searchParams['endDate'][$key]) . "' " " AND 1=1 ";
  2092.                             $whereEvent .= ' ' $operationInst ' ' $searchParams['startDate'][$key] . ' ' $endDate;
  2093.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2094.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  2095.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  2096.                             break;
  2097.                         case 'Created':
  2098.                             $startDate = isset($searchParams['startDate'][$key]) ? "created_at >= '" self::cleanDate($searchParams['startDate'][$key]) . "' " "1=1 ";
  2099.                             $endDate = isset($searchParams['endDate'][$key]) ? "AND created_at <= '" self::cleanDate($searchParams['endDate'][$key]) . "' " " AND 1=1 ";
  2100.                             $whereEvent .= ' ' $operationInst ' ' $startDate ' ' $endDate;
  2101.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2102.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  2103.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  2104.                             break;
  2105.                     }
  2106.                     $operationEvnt $searchParams['searchToggle'][$key] ?? " OR ";
  2107.                 } elseif ($type == 'documents') {
  2108.                     $sortFields['name'] = 'Document Title';
  2109.                     if ($sortBy == '') {
  2110.                         $sortBy 'title';
  2111.                     }
  2112.                     $bindValues[$type][$key] = array();
  2113.                     $selectQryDocument "
  2114.                             SELECT 
  2115.                                 'documents' AS type, 
  2116.                                 d.id_doc,
  2117.                                 dt.id_doctype AS id_doctype, 
  2118.                                 dt.doctypename AS documenttype, 
  2119.                                 d.title, 
  2120.                                 d.created_at,
  2121.                                 d.doc_code,
  2122.                                 'undefined' AS country
  2123.                             FROM 
  2124.                                  documents d, 
  2125.                                  doctypes dt 
  2126.                             WHERE 
  2127.                                   dt.id_doctype = d.id_doctype ";
  2128.                     switch ($value) {
  2129.                         case 'Title contains':
  2130.                             $whereDocument .= ' ' $operationEvnt " (
  2131.                                                   d.title LIKE  :percStringPerc$key
  2132.                                                        ) ";
  2133.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  2134.                             $messagePart .= "in $type using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2135.                             //remember what value has to be bound to what placeholder
  2136.                             $bindValues[$type][$key] = array(
  2137.                                 'percStringPerc' $key => '%' $string '%'
  2138.                             );
  2139.                             break;
  2140.                         case 'Document type is':
  2141.                             $whereDocument .= ' ' $operationEvnt " (
  2142.                                                   dt.id_doctype = :idDoctype
  2143.                                                        ) ";
  2144.                             $messagePart .= "$operation $type having Document Type ";
  2145.                             $messagePart .= "\"<strong> " self::readableFilter($value) . "</strong>\" ";
  2146.                             //remember what value has to be bound to what placeholder
  2147.                             $bindValues[$type][$key] = array(
  2148.                                 'idDoctype' => $string
  2149.                             );
  2150.                             break;
  2151.                         case 'Document code is':
  2152.                             $whereDocument .= ' ' $operationEvnt " (
  2153.                                                   d.doc_code LIKE  :percStringPerc$key
  2154.                                                        ) ";
  2155.                             $messagePart .= "$operation $type having Document Code ";
  2156.                             $messagePart .= "\"<strong> " self::readableFilter($value) . "</strong>\" ";
  2157.                             //remember what value has to be bound to what placeholder
  2158.                             $bindValues[$type][$key] = array(
  2159.                                 'percStringPerc' $key => '%' $string '%'
  2160.                             );
  2161.                             break;
  2162.                         case 'Summary contains':
  2163.                             $whereDocument .= ' ' $operationEvnt " (
  2164.                                                   d.summary LIKE  :percStringPerc$key
  2165.                                                        ) ";
  2166.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  2167.                             $messagePart .= "in $type using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2168.                             //remember what value has to be bound to what placeholder
  2169.                             $bindValues[$type][$key] = array(
  2170.                                 'percStringPerc' $key => '%' $string '%'
  2171.                             );
  2172.                             break;
  2173.                         case 'Keywords contain':
  2174.                             $whereDocument .= ' ' $operationEvnt " (
  2175.                                                d.keywords LIKE  :percStringPerc$key
  2176.                                               ) ";
  2177.                             $messagePart .= "$operation \"<strong>" self::cleaned($string) . "</strong>\" ";
  2178.                             $messagePart .= "in $type using filter \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2179.                             //remember what value has to be bound to what placeholder
  2180.                             $bindValues[$type][$key] = array(
  2181.                                 'percStringPerc' $key => '%' $string '%'
  2182.                             );
  2183.                             break;
  2184.                         case 'Updated':
  2185.                             if (isset($searchParams['endDate'][$key])) {
  2186.                                 $endDate self::cleanDate($searchParams['endDate'][$key]);
  2187.                                 $endDate "AND updated_at <= '$endDate' ";
  2188.                             } else {
  2189.                                 $endDate ' AND 1=1 ';
  2190.                             }
  2191.                             $whereDocument .= ' ' $operationInst ' ' $searchParams['startDate'][$key] . ' ' $endDate;
  2192.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2193.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  2194.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  2195.                             break;
  2196.                         case 'Created':
  2197.                             if (isset($searchParams['startDate'][$key])) {
  2198.                                 $startDate self::cleanDate($searchParams['startDate'][$key]);
  2199.                                 $startDate "created_at >= '$startDate' ";
  2200.                             } else {
  2201.                                 $startDate '1=1 ';
  2202.                             }
  2203.                             if (isset($searchParams['endDate'][$key])) {
  2204.                                 $endDate self::cleanDate($searchParams['endDate'][$key]);
  2205.                                 $endDate "AND created_at <= '$endDate' ";
  2206.                             } else {
  2207.                                 $endDate ' AND 1=1 ';
  2208.                             }
  2209.                             $whereDocument .= ' ' $operationInst ' ' $startDate ' ' $endDate;
  2210.                             $messagePart .= "$operation $type \"<strong>" self::readableFilter($value) . "</strong>\" ";
  2211.                             $messagePart .= "between \"<strong>" $searchParams['startDate'][$key] . "</strong>\" ";
  2212.                             $messagePart .= "and \"<strong>" $searchParams['endDate'][$key] . "</strong>\" ";
  2213.                             break;
  2214.                     }
  2215.                     $operationEvnt $searchParams['searchToggle'][$key] ?? " OR ";
  2216.                 }
  2217.             }
  2218.             if (isset($selectQryInd)) {
  2219.                 $selectQryInd .= " FROM indiv i ";
  2220.                 if ($endQueryInd == false) {
  2221.                     $whereInd .= ' ) ' $searchParams['indivStatus'] . 'GROUP BY i.id_ind';
  2222.                 }
  2223.                 $queryInd $selectQryInd ' ' $commonJoins ' ' $joinsInd ' ' $whereInd ' ' $matchOrder;
  2224.                 $stmtInd $connection->prepare($queryInd);
  2225.                 foreach($bindValues['experts'] as $bindValue) {
  2226.                     foreach($bindValue as $placeholder => $value) {
  2227.                         $stmtInd->bindValue($placeholder$value);
  2228.                     }
  2229.                 }
  2230.                 $stmtInd->execute();
  2231.                 $dataResultInd $stmtInd->fetchAll();
  2232.                 $sortFields['jobtitle'] = 'Position';
  2233.             }
  2234.             if (isset($selectQryInst)) {
  2235.                 if ($endQueryInst == false) {
  2236.                     $whereInst .= " ) " $searchParams['instStatus'] . " GROUP BY i.id_inst ";
  2237.                 }
  2238.                 $queryInst $selectQryInst ' ' $joinsInst ' ' $whereInst "  ORDER BY i.inst_name ASC";
  2239.                 $stmtInst $connection->prepare($queryInst);
  2240.                 foreach($bindValues['institutions'] as $bindValue) {
  2241.                     foreach($bindValue as $placeholder => $value) {
  2242.                         $stmtInst->bindValue($placeholder$value);
  2243.                     }
  2244.                 }
  2245.                 $stmtInst->execute();
  2246.                 $dataResultInst $stmtInst->fetchAll();
  2247.                 $sortFields["instType"] = "Institution Type";
  2248.             }
  2249.             if (isset($selectQryEvnt)) {
  2250.                 if ($endQueryEvent == false) {
  2251.                     $whereEvent .= ' ) ' $searchParams['eventStatus'];
  2252.                     $whereEvent.= ' GROUP BY e.id_event ORDER BY start_on DESC ';
  2253.                 }
  2254.                 $queryEvnt $selectQryEvnt ' ' $joinsEvnt ' ' $whereEvent;
  2255.                 $stmtEvnt $connection->prepare($queryEvnt);
  2256.                 foreach($bindValues['events'] as $bindValue) {
  2257.                     foreach($bindValue as $placeholder => $value) {
  2258.                         $stmtEvnt->bindValue($placeholder$value);
  2259.                     }
  2260.                 }
  2261.                 $stmtEvnt->execute();
  2262.                 $dataResultEvnt $stmtEvnt->fetchAll();
  2263.             }
  2264.             if (isset($selectQryDocument)) {
  2265.                 if ($endQueryDocument == false) {
  2266.                     $whereDocument .= ' ORDER BY created_at DESC';
  2267.                 }
  2268.                 $queryDocument $selectQryDocument ' ' $whereDocument;
  2269.                 $stmtDocument $connection->prepare($queryDocument);
  2270.                 foreach($bindValues['documents'] as $bindValue) {
  2271.                     foreach($bindValue as $placeholder => $value) {
  2272.                         $stmtDocument->bindValue($placeholder$value);
  2273.                     }
  2274.                 }
  2275.                 $stmtDocument->execute();
  2276.                 $dataResultDocument $stmtDocument->fetchAll();
  2277.             }
  2278.             $dataResult array_merge(
  2279.                 $dataResultInd,
  2280.                 $dataResultInst,
  2281.                 $dataResultEvnt,
  2282.                 $dataResultDocument
  2283.             );
  2284.         }
  2285.         if ($messagePart == '') {
  2286.             $message 'no data found at all';
  2287.         } else {
  2288.             $messageEnd 'returned <strong>' count($dataResult) . '</strong> result(s).';
  2289.             $message $messageStart $messagePart $messageEnd;
  2290.         }
  2291.         //status 0 means there are no errors to be mentioned
  2292.         $return = array(
  2293.             'status' => 0,
  2294.             'dataResult' => $dataResult,
  2295.             'sortFields' => $sortFields,
  2296.             'sortby' => $sortBy,
  2297.             'message' => $message,
  2298.             'searchResultType' => $searchResultType
  2299.         );
  2300.         return $return;
  2301.     }
  2302.     /**
  2303.      * show the results of the advanced search
  2304.      *
  2305.      * @param Request $request
  2306.      *
  2307.      * @return Response
  2308.      */
  2309.     public function advancedSearchAction(Request $request): Response
  2310.     {
  2311.         //if we do a 'browse' search (from the homepage)
  2312.         //AND we try to filter the results (this can be the case after searching for a word in 'all')
  2313.         //we should redirect this to a more complex = advanced search type
  2314.         $searchParams $this::extractParams($request);
  2315.         //we want to see inactive/unapproved/... results as a global editor
  2316.         //normal users should not see that
  2317.         if ($this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')) {
  2318.             $indivStatus ' ';
  2319.             $instStatus ' ';
  2320.             $eventStatus ' ';
  2321.         } else {
  2322.             $indivStatus ' AND status = 1 ';
  2323.             $instStatus ' AND activated = 1 ';
  2324.             $eventStatus ' AND status = 1 ';
  2325.         }
  2326.         $searchParams['indivStatus'] = $indivStatus;
  2327.         $searchParams['instStatus'] = $instStatus;
  2328.         $searchParams['eventStatus'] = $eventStatus;
  2329.         //remember the filters that we have applied
  2330.         //used to add action to the filter labels shown above the results
  2331.         $appliedFilters = array();
  2332.         $appliedCountryFilters = array();
  2333.         $doctrine $this->get('doctrine');
  2334.         //get the results depending on the defined action
  2335.         if ($searchParams['action'] == 'browse') {
  2336.             $results self::getBrowseSearchResults($searchParams$doctrine);
  2337.         } elseif ($searchParams['action'] == 'advSearch') {
  2338.             $results self::getAdvSearchResults($searchParams$doctrine);
  2339.         } else {
  2340.             $results = array(
  2341.                 'status' => 2,
  2342.                 'message' => 'we need at least the parameter "action" (browse or advSearch)'
  2343.             );
  2344.         }
  2345.         //this means we have an error somewhere, or we have no results (status == 4)
  2346.         if ($results['status']
  2347.             && $results['status'] != 3
  2348.         ) {
  2349.             return $this->render(
  2350.                 'Exception/error.html.twig',
  2351.                 array(
  2352.                     'message' => $results['message']
  2353.                 )
  2354.             );
  2355.         }
  2356.         $dataResult $results['dataResult'];
  2357.         $searchResultType $results['searchResultType'];
  2358.         //only keep the results that match one of the filtertypes (experts, institutions,....) we are asking
  2359.         if (isset($searchParams['searchFilterType'])
  2360.             && is_array($searchParams['searchFilterType'])
  2361.         ) {
  2362.             $filterData = array();
  2363.             foreach (array_unique($searchParams['searchFilterType']) as $type) {
  2364.                 $filterData array_merge(
  2365.                     $filterData,
  2366.                     $this->addFilter(
  2367.                       $type,
  2368.                         'type',
  2369.                         $dataResult
  2370.                     )
  2371.                 );
  2372.                 $appliedFilters[] = $type;
  2373.             }
  2374.             $dataResult $filterData;
  2375.         }
  2376.         //only show the results for the country we are filtering
  2377.         if (isset($searchParams['filterCountry'])
  2378.             && trim($searchParams['filterCountry']) != ''
  2379.         ) {
  2380.             $filterData = array();
  2381.             foreach (explode(','$searchParams['filterCountry']) as $countryValue) {
  2382.                 if (trim($countryValue) != '') {
  2383.                     $filterData array_merge(
  2384.                         $filterData,
  2385.                         $this->addFilter(
  2386.                             $countryValue,
  2387.                             'country',
  2388.                             $dataResult
  2389.                         )
  2390.                     );
  2391.                     $appliedCountryFilters[] = $countryValue;
  2392.                 }
  2393.             }
  2394.             $dataResult $filterData;
  2395.         }
  2396.         //if sortby is defined other than relevance, then manually sort by name;
  2397.         if (isset($results['sortby'])
  2398.             && $results['sortby'] != 'relevance'
  2399.         ) {
  2400.             $this->sortBy(
  2401.                 $results['sortby'],
  2402.                 $dataResult,
  2403.                 $searchParams['sortOrder']
  2404.             );
  2405.         } else {
  2406.             $results['sortby'] = 'relevance';
  2407.         }
  2408.         $data $this->get('knp_paginator')
  2409.             ->paginate(
  2410.                 $dataResult,
  2411.                 $request->query->getInt('page'1),
  2412.                 $searchParams['limit'],
  2413.                 array(
  2414.                     'pageParameterName' => 'page',
  2415.                     'sortDirectionParameterName' => 'dir'
  2416.                 )
  2417.             );
  2418.         //gather all the email address already
  2419.         //could be done at the receiving end also...
  2420.         //or are we the receiving end already?
  2421.         $emailusers = array();
  2422.         foreach ($dataResult as $expert) {
  2423.             if ($expert['type'] == 'experts') {
  2424.                 $emailusers[] = $expert['id_ind'];
  2425.             }
  2426.         }
  2427.         //who is making this query
  2428.         //this is used for searches where we want to send emails to the resulting experts
  2429.         $user self::getDoctrine()
  2430.             ->getRepository('OceanExpertBundle:Indiv')
  2431.             ->findOneBy(
  2432.                 array(
  2433.                     'idInd' => $this->get('security.token_storage')->getToken()->getUser()
  2434.                 )
  2435.             );
  2436.         //show what we have
  2437.         return $this->render(
  2438.             'Search/advancedSearch.html.twig',
  2439.             array(
  2440.                 'action' => $searchParams['action'],
  2441.                 'data' => $data,
  2442.                 'filters' => self::getFilters($dataResult),
  2443.                 'searchResultText' => $results['message'],
  2444.                 'appliedFilters' => $appliedFilters,
  2445.                 'appliedCountryFilters' => $appliedCountryFilters,
  2446.                 'searchResultType' => $searchResultType,
  2447.                 'searchConditions' => $searchParams['conditions'],
  2448.                 'sortfields' => $results['sortFields'],
  2449.                 'user' => $user,
  2450.                 'emailUsers' => $emailusers,
  2451.                 'sortby' => $results['sortby']
  2452.             )
  2453.         );
  2454.     }
  2455.     public static function sortBy($field, &$array$direction 'ASC'): bool
  2456.     {
  2457.         usort($array, function($a$b) use ($field$direction) {
  2458.             $a $a[$field] ?? '';
  2459.             $b $b[$field] ?? '';
  2460.             if ($a == $b) {
  2461.                 return 0;
  2462.             }
  2463.             if (strtolower($direction) == 'desc') {
  2464.                 return $a $b ? -1;
  2465.             } else {
  2466.                 return $a $b ? -1;
  2467.             }
  2468.         });
  2469.         return true;
  2470.     }
  2471.     /**
  2472.      * @param $dataArr
  2473.      * @param $type
  2474.      * @return array
  2475.      *
  2476.      */
  2477.     public static function getUnique($dataArr$type): array
  2478.     {
  2479.         SearchController::sortBy($type$dataArr'ASC');
  2480.         $fields = array();
  2481.         foreach ($dataArr as $value) {
  2482.             if (isset($value[$type])
  2483.                 && trim($value[$type]) != ''
  2484.             ) {
  2485.                 if (array_key_exists($value[$type], $fields)) {
  2486.                     $fields[$value[$type]] += 1;
  2487.                 } else {
  2488.                     $fields[$value[$type]] = 1;
  2489.                 }
  2490.             }
  2491.         }
  2492.         ksort($fields);
  2493.         return $fields;
  2494.     }
  2495.     public static function addFilter($filter$filterKey$dataArr): array
  2496.     {
  2497.         $filterData = array();
  2498.         foreach ($dataArr as $key => $value) {
  2499.             if ($filter == 'all'
  2500.                 || (isset($value[$filterKey])
  2501.                 && $value[$filterKey] == $filter)
  2502.             ) {
  2503.                 $filterData[$key] = $value;
  2504.             }
  2505.         }
  2506.         return $filterData;
  2507.     }
  2508.     /**
  2509.      * get the subject area name by its id
  2510.      *
  2511.      * @param int    $id id of the subject area
  2512.      * @param object $doctrine
  2513.      *
  2514.      * @return string
  2515.      */
  2516.     public static function getSubjectAreaById(int $idobject $doctrine): string
  2517.     {
  2518.         $subjectArea $doctrine
  2519.             ->getRepository('OceanExpertBundle:Subjects')
  2520.             ->findOneBy(array('idSub' => $id));
  2521.         return $subjectArea->getSubname();
  2522.     }
  2523.     /**
  2524.      * get the name of the sea region by its id
  2525.      *
  2526.      * @param int    $id       id of the sea area
  2527.      * @param object $doctrine doctrine
  2528.      *
  2529.      * @return string
  2530.      */
  2531.     public static function getSeaRegionById(int $idobject $doctrine): string
  2532.     {
  2533.         $seaRegion $doctrine
  2534.             ->getRepository('OceanExpertBundle:Regions')
  2535.             ->findOneBy(array('idRegion' => $id));
  2536.         return $seaRegion->getName();
  2537.     }
  2538.     /**
  2539.      * get the name of the institution type
  2540.      * replaced by getInstituteTypeNameById()
  2541.      *
  2542.      * @param int    $id       the id of the institution type
  2543.      * @param object $doctrine doctrine
  2544.      *
  2545.      * @return string
  2546.      */
  2547.     public static function getInstituteTypeById(int $idobject $doctrine): string
  2548.     {
  2549.         return self::getInstituteTypeNameById($id$doctrine);
  2550.     }
  2551.     /**
  2552.      * get the name of the institution type
  2553.      * used to be getInstituteTypeById()
  2554.      *
  2555.      * @param int    $id       the id of the institution type
  2556.      * @param object $doctrine doctrine
  2557.      *
  2558.      * @return string
  2559.      */
  2560.     public function getInstituteTypeNameById(int $idobject $doctrine): string
  2561.     {
  2562.         $instType $doctrine
  2563.             ->getRepository('OceanExpertBundle:Insttypes')
  2564.             ->findOneBy(
  2565.                 array(
  2566.                     'idInsttype' => $id
  2567.                 )
  2568.             );
  2569.         if ($instType) {
  2570.             return $instType->getInsttypeName();
  2571.         } else {
  2572.             return 'no institution found';
  2573.         }
  2574.     }
  2575.     public static function getJobTypesById($id$doctrine)
  2576.     {
  2577.         $jobType $doctrine
  2578.             ->getRepository('OceanExpertBundle:Jobtypes')
  2579.             ->findOneBy(array('idJob' => $id));
  2580.         return $jobType->getJobName();
  2581.     }
  2582.     public static function getCountryById($idManagerRegistry $doctrine null): bool
  2583.     {
  2584.         if ($id != 0) {
  2585.             $country $doctrine
  2586.                 ->getRepository('OceanExpertBundle:Countries')
  2587.                 ->findOneBy(array('idCountry' => $id));
  2588.             return $country->getCountry();
  2589.         } else {
  2590.             return false;
  2591.         }
  2592.     }
  2593.     public static function getMultipleCountryById($id$doctrine): array
  2594.     {
  2595.         $id explode(','$id);
  2596.         $countries $doctrine
  2597.             ->getRepository('OceanExpertBundle:Countries')
  2598.             ->createQueryBuilder('c')
  2599.             ->select('c.country')
  2600.             ->where('c.idCountry in (:idCountries)')
  2601.             ->setParameter('idCountries'$id)
  2602.             ->orderBy('c.country')
  2603.             ->getQuery()
  2604.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2605.         return array_column($countries'country');
  2606.     }
  2607.     public static function getRegionById($id$doctrine)
  2608.     {
  2609.         $region $doctrine
  2610.             ->getRepository('OceanExpertBundle:Regions')
  2611.             ->findOneBy(array('idRegion' => $id));
  2612.         return $region->getName();
  2613.     }
  2614.     public static function getGroupById($id$doctrine)
  2615.     {
  2616.         $group $doctrine
  2617.             ->getRepository('OceanExpertBundle:Groups')
  2618.             ->findOneBy(array('idGroup' => $id));
  2619.         return $group->getGroupName();
  2620.     }
  2621.     public static function readableFilter($filter): string
  2622.     {
  2623.         return ucfirst(preg_replace('/_/'' '$filter));
  2624.     }
  2625.     public function getSeaRegionsAction(): Response
  2626.     {
  2627.         $regions self::getDoctrine()
  2628.             ->getRepository('OceanExpertBundle:Regions')
  2629.             ->seaRegions();
  2630.         return new JsonResponse($regions);
  2631.     }
  2632.     public function getMemberGroupsAction(): Response
  2633.     {
  2634.         $groups self::getDoctrine()
  2635.             ->getRepository('OceanExpertBundle:Groups')
  2636.             ->createQueryBuilder('e')
  2637.             ->select('e.idGroup AS id,e.groupname AS name')
  2638.             ->orderBy('e.groupname')
  2639.             ->getQuery()
  2640.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2641.         return new JsonResponse($groups);
  2642.     }
  2643.     public static function getChildGroup($group$doctrine): array
  2644.     {
  2645.         $groupids = array();
  2646.         $allgroups = array($group);
  2647.         $groups $doctrine
  2648.             ->getRepository('OceanExpertBundle:Groups')
  2649.             ->createQueryBuilder('e')
  2650.             ->select('e.idGroup,e.groupname')
  2651.             ->orderBy('e.groupname')
  2652.             ->where('e.idParentgroup = :group')
  2653.             ->setParameter('group'$group)
  2654.             ->getQuery()
  2655.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2656.         foreach ($groups as $value) {
  2657.             $groupids[] = $value['idGroup'];
  2658.             $allgroups[] = $value['idGroup'];
  2659.         }
  2660.         if (count($groupids) > 0) {
  2661.             do {
  2662.                 $group2 $doctrine
  2663.                     ->getRepository('OceanExpertBundle:Groups')
  2664.                     ->createQueryBuilder('e')
  2665.                     ->select('e.idGroup AS id,e.groupname AS name')
  2666.                     ->orderBy('e.groupname')
  2667.                     ->where('e.idParentgroup in(:groupids)')
  2668.                     ->setParameter('groupids'$groupids)
  2669.                     ->getQuery()
  2670.                     ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2671.                 $groupids = array();
  2672.                 foreach ($group2 as $value) {
  2673.                     $groupids[] = $value['id'];
  2674.                     $allgroups[] = $value['id'];
  2675.                 }
  2676.             } while (count($group2) > 0);
  2677.         }
  2678.         return $allgroups;
  2679.     }
  2680.     public function getJobTypesAction(): Response
  2681.     {
  2682.         $jobTypes self::getDoctrine()
  2683.             ->getRepository('OceanExpertBundle:Jobtypes')
  2684.             ->createQueryBuilder('e')
  2685.             ->select('e.idJob AS id,e.jobname AS name')
  2686.             ->orderBy('e.jobname')
  2687.             ->getQuery()
  2688.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2689.         return new JsonResponse($jobTypes);
  2690.     }
  2691.     public function getCountriesAction(): Response
  2692.     {
  2693.         $countries self::getDoctrine()
  2694.             ->getRepository('OceanExpertBundle:Countries')
  2695.             ->createQueryBuilder('e')
  2696.             ->select('e.idCountry AS id,e.country AS name')
  2697.             ->orderBy('e.country')
  2698.             ->getQuery()
  2699.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2700.         return new JsonResponse($countries);
  2701.     }
  2702.     public function getSubjectAreaAction(): Response
  2703.     {
  2704.         $subjectArea self::getDoctrine()
  2705.             ->getRepository('OceanExpertBundle:Subjects')
  2706.             ->createQueryBuilder('e')
  2707.             ->select('e.subname AS id,e.subname AS name')
  2708.             ->orderBy('e.subname')
  2709.             ->getQuery()
  2710.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2711.         return new JsonResponse($subjectArea);
  2712.     }
  2713.     public function getEventTypesAction(): Response
  2714.     {
  2715.         $eventTypes self::getDoctrine()
  2716.             ->getRepository('OceanExpertBundle:Eventtypes')
  2717.             ->createQueryBuilder('e')
  2718.             ->select('e.idEventtype AS id,e.eventtypeName AS name')
  2719.             ->orderBy('e.eventtypeName')
  2720.             ->getQuery()
  2721.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2722.         return new JsonResponse($eventTypes);
  2723.     }
  2724.     public function getInstitutionTypesAction(): Response
  2725.     {
  2726.         $instTypes self::getDoctrine()
  2727.             ->getRepository('OceanExpertBundle:Insttypes')
  2728.             ->createQueryBuilder('i')
  2729.             ->select('i.idInsttype AS id,i.insttypeName AS name')
  2730.             ->orderBy('i.insttypeName')
  2731.             ->getQuery()
  2732.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2733.         return new JsonResponse($instTypes);
  2734.     }
  2735.     /**
  2736.      * get all possible document types
  2737.      * can be used by the search to provide a dropdown menu
  2738.      * @see #609
  2739.      *
  2740.      * @return Response
  2741.      */
  2742.     public function getDocumentTypesAction(): Response
  2743.     {
  2744.         $documentTypes self::getDoctrine()
  2745.             ->getRepository('OceanExpertBundle:Doctypes')
  2746.             ->createQueryBuilder('i')
  2747.             ->select('i.idDoctype AS id,i.doctypename AS name')
  2748.             ->orderBy('i.doctypename')
  2749.             ->getQuery()
  2750.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  2751.         return new JsonResponse($documentTypes);
  2752.     }
  2753.     public function getMembersAction(Request $request): Response
  2754.     {
  2755.         $doctrine self::getDoctrine();
  2756.         $members = array();
  2757.         if (null !== $request->request->get('searchterm')) {
  2758.             $searchTerm $request->request->get('searchterm');
  2759.             $type $request->request->get('searchparam');
  2760.         } elseif (null !== $request->query->get('searchterm')) {
  2761.             $searchTerm $request->query->get('searchterm');
  2762.             $type $request->query->get('searchparam');
  2763.         } else {
  2764.             return new JsonResponse(
  2765.                 array(
  2766.                     'status' => 1,
  2767.                     'error' => 'please send "searchparam" and "searchterm"'
  2768.                 )
  2769.             );
  2770.         }
  2771.         $searchParams $this::extractParams($request);
  2772.         $searchParams['searchQuery'] = $searchTerm;
  2773.         $searchParams['searchType'] = $type;
  2774.         $searchParams['searchFilterType'] = ['experts'];
  2775.         $connection $doctrine->getManager()->getConnection();
  2776.         if ($type == 'name'
  2777.             || $type == 'all'
  2778.         ) {
  2779.             /*
  2780.             $statement = $connection->prepare("
  2781.                 SELECT
  2782.                     i.id_ind AS DT_RowId,
  2783.                     i.id_ind,
  2784.                     i.title,
  2785.                     i.fname,
  2786.                     i.mname,
  2787.                     i.sname,
  2788.                     inst.inst_name,
  2789.                     c.country,
  2790.                     i.status,
  2791.                     MATCH (i.fname,i.sname) AGAINST (:searchQuery IN BOOLEAN MODE) AS relevance
  2792.                 FROM
  2793.                     indiv i
  2794.                     LEFT JOIN countries c ON c.id_country =  i.country_code
  2795.                     LEFT JOIN indiv_institution ii ON ii.id_ind = i.id_ind
  2796.                     LEFT JOIN institutions inst ON inst.id_inst = ii.id_inst
  2797.                 WHERE
  2798.                     (
  2799.                          MATCH (i.fname,i.mname,i.sname) AGAINST (:searchQuery IN BOOLEAN MODE)
  2800.                         OR i.fname LIKE :percSearchQuery
  2801.                         OR i.fname LIKE :searchQueryPerc
  2802.                         OR i.mname LIKE :percSearchQuery
  2803.                         OR i.mname LIKE :searchQueryPerc
  2804.                         OR i.sname LIKE :percSearchQuery
  2805.                         OR i.sname LIKE :searchQueryPerc
  2806.                     )
  2807.                 ORDER BY relevance DESC, sname ASC;
  2808.             ");
  2809.             $statement->bindValue('searchQuery', $searchParams['searchQuery']);
  2810.             $statement->bindValue('searchQueryPerc', $searchParams['searchQuery'] . '%');
  2811.             $statement->bindValue('percSearchQuery', '%' . $searchParams['searchQuery']);
  2812.             $statement->execute();
  2813.             $members = array_merge($members, $statement->fetchAll());
  2814.             */
  2815.             //we only need the active ones
  2816.             $indivStatus ' AND status = 1 ';
  2817.             $instStatus ' AND activated = 1 ';
  2818.             $eventStatus ' AND status = 1 ';
  2819.             $searchParams['indivStatus'] = $indivStatus;
  2820.             $searchParams['instStatus'] = $instStatus;
  2821.             $searchParams['eventStatus'] = $eventStatus;
  2822.             $membersTmp self::getBrowseSearchResults($searchParams$doctrine);
  2823.             //recreate the array we expected here (see old code)
  2824.             foreach($membersTmp['dataResult'] as $dataResult) {
  2825.                 if (!isset($dataResult['id_ind'])) {
  2826.                     //this is not an expert, skip
  2827.                     continue;
  2828.                 }
  2829.                 $members[] = array(
  2830.                     "DT_RowId" => $dataResult['id_ind'],
  2831.                     "id_ind" => $dataResult['id_ind'],
  2832.                     "title" => $dataResult['title'],
  2833.                     "fname" => $dataResult['fname'],
  2834.                     "mname" => $dataResult['mname'],
  2835.                     "sname" => $dataResult['sname'],
  2836.                     "inst_name" => $dataResult['inst_name'],
  2837.                     "country" => $dataResult['country'],
  2838.                     "status" => $dataResult['status'],
  2839.                     "relevance" => $dataResult['relevance'],
  2840.                 );
  2841.             }
  2842.         } elseif ($type == 'email') {
  2843.             $statement $connection->prepare("
  2844.                 SELECT 
  2845.                     i.id_ind AS DT_RowId,
  2846.                     i.id_ind, 
  2847.                     i.title, 
  2848.                     i.fname, 
  2849.                     i.mname, 
  2850.                     i.sname,
  2851.                     inst.inst_name,
  2852.                     c.country, 
  2853.                     i.status, 
  2854.                     MATCH (i.email1) AGAINST (:searchQuery IN BOOLEAN MODE) AS relevance 
  2855.                 FROM 
  2856.                     indiv i
  2857.                     LEFT JOIN countries c ON c.id_country =  i.country_code
  2858.                     LEFT JOIN indiv_institution ii ON ii.id_ind = i.id_ind
  2859.                     LEFT JOIN institutions inst ON inst.id_inst = ii.id_inst
  2860.                 WHERE 
  2861.                     email1 = :searchQuery
  2862.                     OR email2 = :searchQuery
  2863.                     OR email3 = :searchQuery
  2864.                 ORDER BY relevance DESC;
  2865.             ");
  2866.             $statement->bindValue(':searchQuery'$searchParams['searchQuery']);
  2867.             $statement->execute();
  2868.             $members array_merge(
  2869.                 $members,
  2870.                 $statement->fetchAll()
  2871.             );
  2872.         } elseif ($type == 'group') {
  2873.             //we only need the active ones
  2874.             $indivStatus ' AND status = 1 ';
  2875.             $instStatus ' AND activated = 1 ';
  2876.             $eventStatus ' AND status = 1 ';
  2877.             $searchParams['indivStatus'] = $indivStatus;
  2878.             $searchParams['instStatus'] = $instStatus;
  2879.             $searchParams['eventStatus'] = $eventStatus;
  2880.             $membersTmp self::getBrowseSearchResults($searchParams$doctrine);
  2881.             //recreate the array we expected here (see old code)
  2882.             foreach($membersTmp['dataResult'] as $dataResult) {
  2883.                 if (!isset($dataResult['id_ind'])) {
  2884.                     //this is not an experts, skip
  2885.                     continue;
  2886.                 }
  2887.                 $members[] = array(
  2888.                     "DT_RowId" => $dataResult['id_ind'],
  2889.                     "id_ind" => $dataResult['id_ind'],
  2890.                     "title" => $dataResult['title'],
  2891.                     "fname" => $dataResult['fname'],
  2892.                     "mname" => $dataResult['mname'],
  2893.                     "sname" => $dataResult['sname'],
  2894.                     "inst_name" => $dataResult['inst_name'],
  2895.                     "country" => $dataResult['country'],
  2896.                     "status" => $dataResult['status'],
  2897.                     "relevance" => $dataResult['relevance'],
  2898.                     "groupName" => $dataResult['groupName'],
  2899.                     "groupId" => $dataResult['groupId']
  2900.                 );
  2901.             }
  2902.             /*
  2903.             $statement = $connection->prepare('
  2904.                 SELECT
  2905.                     i.id_ind AS DT_RowId,
  2906.                     i.id_ind,
  2907.                     i.title,
  2908.                     i.fname,
  2909.                     i.mname,
  2910.                     i.sname,
  2911.                     inst.inst_name,
  2912.                     c.country,
  2913.                     i.status,
  2914.                     1 AS relevance
  2915.                 FROM
  2916.                     indiv i
  2917.                     LEFT JOIN countries c ON c.id_country =  i.country_code
  2918.                     LEFT JOIN indiv_institution ii ON ii.id_ind = i.id_ind
  2919.                     LEFT JOIN institutions inst ON inst.id_inst = ii.id_inst
  2920.                     LEFT JOIN member_groups m ON i.id_ind = m.id_ind
  2921.                 WHERE
  2922.                     m.id_group like "%t%"
  2923.                 ORDER BY relevance DESC;
  2924.             ');
  2925.             $statement->bindValue('searchQuery', $searchParams['searchQuery']);
  2926.             $statement->execute();
  2927.             $members = array_merge($members, $statement->fetchAll());
  2928.             */
  2929.         }
  2930.         return new JsonResponse(
  2931.             array(
  2932.                 'data' => $members
  2933.             )
  2934.         );
  2935.     }
  2936.     /**
  2937.      * @param int $expert expert id
  2938.      *
  2939.      * @return int
  2940.      *
  2941.      * @todo is this used somewhere??? Arno 15/04/2022
  2942.      */
  2943.     function getExpertEvents(int $expert): int
  2944.     {
  2945.         $em self::getDoctrine()->getManager();
  2946.         $qb $em->createQueryBuilder();
  2947.         $qb->select('e.idEvent');
  2948.         $qb->from('OceanExpertBundle:EventParticipants''e');
  2949.         $qb->where('e.idInd = :idInd');
  2950.         $qb->setParameter('idInd'$expert);
  2951.         $participants $qb->getQuery()->getResult();
  2952.         $participants array_map('current'$participants);
  2953.         $qb $em->createQueryBuilder();
  2954.         $qb->select('e.idEvent');
  2955.         $qb->from('OceanExpertBundle:EventStaff''e');
  2956.         $qb->where('e.idInd = :idInd');
  2957.         $qb->setParameter('idInd'$expert);
  2958.         $staff $qb->getQuery()->getResult();
  2959.         $staff array_map('current'$staff);
  2960.         $qb $em->createQueryBuilder();
  2961.         $qb->select('e.idEvent');
  2962.         $qb->from('OceanExpertBundle:EventContacts''e');
  2963.         $qb->where('e.idInd = :idInd');
  2964.         $qb->setParameter('idInd'$expert);
  2965.         $contacts $qb->getQuery()->getResult();
  2966.         $contacts array_map('current'$contacts);
  2967.         $result array_merge($participants$staff$contacts);
  2968.         $results array_unique($resultSORT_REGULAR);
  2969.         return count($results);
  2970.     }
  2971.     /**
  2972.      * clean input strings to prevent XSS and other problems
  2973.      *
  2974.      * @param string $string input string
  2975.      *
  2976.      * @return string
  2977.      */
  2978.     public static function cleaned(string $string): string
  2979.     {
  2980.         $string nl2br(stripslashes($string));
  2981.         return preg_replace(
  2982.             '/[<>]/',
  2983.             '--*--',
  2984.             $string
  2985.         );
  2986.     }
  2987.     /**
  2988.      * in case we get the wrong format for the date
  2989.      * cleanup and return the correct format
  2990.      * we want a string like 'yyyy-mm-dd'
  2991.      *
  2992.      * @param string $string the date to be checked
  2993.      *
  2994.      * @return string
  2995.      */
  2996.     public static function cleanDate(string $string): string
  2997.     {
  2998.         if (strpos($string'/')) {
  2999.             $date explode('/'$string);
  3000.             if (strlen($date[0]) == 4) {
  3001.                 // this was something like yyyy/mm/dd I guess
  3002.                 return $date[0] . '-' $date[1] . '-' $date[2];
  3003.             } else {
  3004.                 // this was something like dd/mm/yyyy I guess
  3005.                 return $date[2] . '-' $date[1] . '-' $date[0];
  3006.             }
  3007.         } elseif (strpos($string'-')) {
  3008.             $date explode('-'$string);
  3009.             if (strlen($date[0]) == 4) {
  3010.                 // this was something like yyyy-mm-dd I guess
  3011.                 //that is the correct format already
  3012.                 return $string;
  3013.             } else {
  3014.                 // this was something like dd-mm-yyyy I guess
  3015.                 return $date[2] . '-' $date[1] . '-' $date[0];
  3016.             }
  3017.         } else {
  3018.             //no idea what to clean up
  3019.             return $string;
  3020.         }
  3021.     }
  3022.     public static function fulltext($string): string
  3023.     {
  3024.         $newString '';
  3025.         if (trim($string) != '') {
  3026.             $arrStr explode(' '$string);
  3027.             foreach ($arrStr as &$value) {
  3028.                 $value '+' $value;
  3029.             }
  3030.             $newString implode(' '$arrStr);
  3031.         }
  3032.         return $newString;
  3033.     }
  3034. }