src/OceanExpertBundle/Controller/GroupController.php line 318

Open in your IDE?
  1. <?php
  2. namespace OceanExpertBundle\Controller;
  3. use Doctrine\ORM\AbstractQuery;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use OceanExpertBundle\Entity\Groups;
  7. use OceanExpertBundle\Entity\MemberGroups;
  8. use OceanExpertBundle\Utils\SSP;
  9. use Symfony\Component\Process\Process;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. class GroupController extends AbstractController
  14. {
  15.     /**
  16.      * @todo add function description
  17.      *
  18.      * @return Response
  19.      */
  20.     public function indexAction(): Response
  21.     {
  22.         return new Response('Group Index');
  23.     }
  24.     /**
  25.      * return all groups
  26.      *
  27.      * @param int $id id of the group for wich the permission will be checked
  28.      *
  29.      * @return Response
  30.      */
  31.     public function allGroupsAction(int $id): Response
  32.     {
  33.         $twigTemplate 'Group/groups.html.twig';
  34.         return $this->viewAllGroups($id$twigTemplate);
  35.     }
  36.     /**
  37.      * do the real work to show all the groups
  38.      * will be used by both GroupsController and AdminController to show the list of groups
  39.      *
  40.      * @param int       $id             id of the group for wich the permission will be checked
  41.      * @param string    $twigTemplate   template to be used
  42.      *
  43.      * @return Response
  44.      */
  45.     public function viewAllGroups(int $idstring $twigTemplate): Response
  46.     {
  47.         $isGroupAdmin 0;
  48.         if (true === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
  49.             $isGroupAdmin $this->hasEditPermission($id);
  50.         }
  51.         /*
  52.         //let's check if the logged-in user has a 'real' profile
  53.         //the mandatory profile fields are all filled and the expert is active
  54.         $em = $this->getDoctrine()->getManager();
  55.         $userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
  56.         if (!SecurityController::checkUserProfile($em, $userId)) {
  57.             return $this->redirect(
  58.                 $this->generateUrl(
  59.                     'user_profile_edit'
  60.                 )
  61.             );
  62.         }
  63.         */
  64.         $groupTree $this->getDoctrine()
  65.             ->getRepository('OceanExpertBundle:Groups')
  66.             ->createQueryBuilder('e')
  67.             ->select('e.idGroup, e.groupname, e.idParentgroup, e.groupOrder')
  68.             ->where('e.idGroup = :groupId')
  69.             ->setParameter(':groupId',$id)
  70.             ->getQuery()
  71.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  72.         $allgroups self::allGroups($this->getDoctrine());
  73.         $groups self::descendantsOf($id$allgroups);
  74.         if ($groups == '') {
  75.             //@todo what happens here??
  76.             $groupTree[0]['children'] = array();
  77.         } else {
  78.             $groupTree[0]['children'] = $this->buildTree($groups,$id);
  79.         }
  80.         //@todo should be a dynamic list
  81.         $filtergroups = array(
  82.             31 => 'IOC',
  83.             315 => 'MPR',
  84.             47 => 'Capacity Development',
  85.             138 => 'HAB',
  86.             127 => 'OOS',
  87.             32 => 'GOOS',
  88.             46 => 'IODE',
  89.             269 => 'OBIS',
  90.             291 => 'ICAN',
  91.             78 => 'JCOMM',
  92.             169 => 'TSUNAMI',
  93.             234 => 'ITIC',
  94.             236 => 'Integrated Coastal Research',
  95.         );
  96.         return $this->render(
  97.             $twigTemplate,
  98.             array(
  99.                 'data' => array(
  100.                     'isGroupAdmin' => $isGroupAdmin,
  101.                     'groups' => json_encode(
  102.                         array($groupTree)
  103.                     ),
  104.                     'filtergroups' => $filtergroups,
  105.                     'treeGroup' => $id
  106.                 )
  107.             )
  108.         );
  109.     }
  110.     /**
  111.      * @return Response
  112.      */
  113.     public function addGroupsAction(Request $request)
  114.     {
  115.         $parentGroup $request->request->get('parentId');
  116.         $groupName $request->request->get('groupname');
  117.         $groupName preg_replace('/[^a-zA-Z0-9\s\-\(\)]/'''$groupName);
  118.         $group = new Groups();
  119.         $group->setGroupname($groupName);
  120.         $group->setIdParentgroup($parentGroup);
  121.         $group->setGroupOrder(0);
  122.         $group->setHasSite(0);
  123.         $group->setOfficialBody(0);
  124.         $group->setIdMstemplate(1);
  125.         $em $this->getDoctrine()->getManager();
  126.         $em->persist($group);
  127.         $em->flush();
  128.         $groupId $group->getIdGroup();
  129.         //why would be do a query for that??
  130.         //$groupName = $group->getGroupname();
  131.         return new JsonResponse(
  132.             array(
  133.                 'status' => 1,
  134.                 'childGroup' => $groupId,
  135.                 'groupName' => $groupName
  136.             )
  137.         );
  138.     }
  139.     /**
  140.      * @return Response
  141.      */
  142.     public function saveGroupOrderAction(Request $request)
  143.     {
  144.         $order $request->request->get('order');
  145.         $groupOrders json_decode($order);
  146.         foreach($groupOrders->groups as $groupData) {
  147.            $group $this->getDoctrine()->getRepository('OceanExpertBundle:Groups')->findOneBy(array('idGroup' => $groupData->groupId));
  148.            $em $this->getDoctrine()->getManager();
  149.            if ($group) {
  150.                $group->setIdParentgroup($groupData->parentId);
  151.                $group->setGroupOrder($groupData->order);
  152.                $em->persist($group);
  153.                $em->flush();
  154.            }
  155.         }
  156.         return new JsonResponse(array('status'=>1));
  157.     }
  158.     /**
  159.      * @return Response
  160.      */
  161.     public function deleteGroupAction(Request $request)
  162.     {
  163.         $groupId $request->request->get('groupId');
  164.         $em $this->getDoctrine()->getManager();
  165.         $group $this->getDoctrine()
  166.             ->getRepository('OceanExpertBundle:Groups')
  167.             ->findOneBy(
  168.                 array(
  169.                     'idGroup' => $groupId
  170.                 )
  171.             );
  172.         $em->remove($group);
  173.         $em->flush();
  174.         $updateGroup $this->getDoctrine()
  175.             ->getRepository('OceanExpertBundle:MemberGroups')
  176.             ->findBy(
  177.                 array(
  178.                     'idGroup' => $groupId
  179.                 )
  180.             );
  181.         if ($updateGroup) {
  182.             foreach ($updateGroup as $groups) {
  183.                 $em->remove($groups);
  184.             }
  185.             $em->flush();
  186.         }
  187.         return new JsonResponse(array('status'=>1));
  188.     }
  189.     /**
  190.      * @param $ar
  191.      * @param null $pid
  192.      *
  193.      * @return array
  194.      */
  195.     public  function buildTree$ar$pid null )
  196.     {
  197.         $op = array();
  198.         $i=1;
  199.         foreach( $ar as $item ) {
  200.             if ( $item['idParentgroup'] == $pid ) {
  201.                 $op[$i] = array(
  202.                     'groupname' => preg_replace('/[^a-zA-Z0-9\s\-\(\)]/'''$item['groupname']),
  203.                     'idParentgroup' => $item['idParentgroup'],
  204.                     'idGroup' => $item['idGroup'],
  205.                     'groupOrder' => $item['groupOrder']
  206.                 );
  207.                 // using recursion
  208.                 $children =  $this->buildTree$ar$item['idGroup'] );
  209.                 if ( $children ) {
  210.                     $op[$i]['children'] = $children;
  211.                 }
  212.             }
  213.             $i++;
  214.         }
  215.         return $op;
  216.     }
  217.     /**
  218.      * show the members of the group with the given group id
  219.      *
  220.      * @param int $groupId id of the group
  221.      *
  222.      * @return Response
  223.      */
  224.     public function viewGroupAction(int $groupId): Response
  225.     {
  226.         $twigTemplate 'Group/editGroup.html.twig';
  227.         return $this->viewGroup($groupId$twigTemplate);
  228.     }
  229.     /**
  230.      * get the data of the group
  231.      * call the correct template
  232.      * this is used by this controller and by the AdminController
  233.      *
  234.      * @param int    $groupId
  235.      * @param string $twigTemplate
  236.      *
  237.      * @return Response
  238.      */
  239.     public function viewGroup(int $groupIdstring $twigTemplate): Response
  240.     {
  241.         if (true === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
  242.             $isGroupAdmin $this->hasEditPermission($groupId);
  243.         } else {
  244.             $isGroupAdmin 0;
  245.         }
  246.         /*
  247.         //let's check if the logged-in user has a 'real' profile
  248.         //the mandatory profile fields are all filled and the expert is active
  249.         $em = $this->getDoctrine()->getManager();
  250.         $userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
  251.         if (!SecurityController::checkUserProfile($em, $userId)) {
  252.             return $this->redirect(
  253.                 $this->generateUrl(
  254.                     'user_profile_edit'
  255.                 )
  256.             );
  257.         }
  258.         */
  259.         //collect all the info about the group, the ancestors and subgroups
  260.         $allGroups self::allGroups($this->getDoctrine());
  261.         $ancestors array_reverse($this->getAncestors($groupId$this->getDoctrine()));
  262.         $subgroups self::descendantsOf($groupId$allGroups);
  263.         if ($group $this->getGroupDetailsById($groupId$this->getDoctrine())) {
  264.             $user $this->getDoctrine()
  265.                 ->getRepository('OceanExpertBundle:Indiv')
  266.                 ->findOneBy(
  267.                     array(
  268.                         'idInd' => $this
  269.                             ->get('security.token_storage')
  270.                             ->getToken()
  271.                             ->getUser()
  272.                     )
  273.                 );
  274.             $templates = array(
  275.                 '1' => 'like normal search results',
  276.                 '2' => 'with photos (if available)',
  277.                 '3' => 'grouped by country'
  278.             );
  279.             $html $this->renderView(
  280.                 $twigTemplate,
  281.                 array(
  282.                     'data' => array(
  283.                         'user' => $user,
  284.                         'isGroupAdmin' => $isGroupAdmin,
  285.                         'group' => $group,
  286.                         'templates' => $templates,
  287.                         'ancestors' => $ancestors,
  288.                         'subgroups' => $subgroups
  289.                     )
  290.                 )
  291.             );
  292.             return new Response(
  293.                 $html,
  294.                 200,
  295.                 array(
  296.                     'Cache-Control' => 'no-cache, must-revalidate, post-check=0, pre-check=0'
  297.                 )
  298.             );
  299.         } else {
  300.             return $this->render('Exception/error404.html.twig');
  301.         }
  302.     }
  303.     /**
  304.      * @param $id
  305.      *
  306.      * @return Response
  307.      */
  308.     public function editGroupAction($id)
  309.     {
  310.         $ancestors array_reverse($this->getAncestors($id$this->getDoctrine()));
  311.         return $this->render(
  312.             'Group/editGroup.html.twig',
  313.             array(
  314.                 'data' => array(
  315.                     'ancestors' => $ancestors
  316.                 )
  317.             )
  318.         );
  319.     }
  320.     /**
  321.      * save the new group settings
  322.      *
  323.      * @param Request $request
  324.      *
  325.      * @return JsonResponse
  326.      */
  327.     public function saveGroupAction(Request $request): JsonResponse
  328.     {
  329.         $groupId $request->request->get('groupid');
  330.         $groupName $request->request->get('groupname');
  331.         $groupName preg_replace('/[^a-zA-Z0-9\s\-\(\)]/'''$groupName);
  332.         $description =  $request->request->get('description');
  333.         $officialBody =  $request->request->get('officialbody');
  334.         $template $request->request->get('template');
  335.         $group $this->getDoctrine()
  336.             ->getRepository('OceanExpertBundle:Groups')
  337.             ->findOneBy(
  338.                 array(
  339.                     'idGroup' => $groupId
  340.                 )
  341.             );
  342.         if ($group) {
  343.             $em $this->getDoctrine()->getManager();
  344.             $group->setGroupname($groupName);
  345.             $group->setDescription($description);
  346.             $group->setOfficialBody($officialBody);
  347.             $group->setIdMstemplate($template);
  348.             $em->persist($group);
  349.             $em->flush();
  350.             return new JsonResponse(
  351.                 array(
  352.                     'status' => 1,
  353.                     'message' => 'Group updated'
  354.                 )
  355.             );
  356.         } else {
  357.             return new JsonResponse(
  358.                 array(
  359.                     'status' => 0,
  360.                     'message' => 'cannot find group with id ' $groupId
  361.                 )
  362.             );
  363.         }
  364.     }
  365.     /**
  366.      * save the new order of the members of a group
  367.      *
  368.      * @param Request $request
  369.      *
  370.      * @return JsonResponse
  371.      */
  372.     public function saveGroupMemberOrderAction(Request $request): JsonResponse
  373.     {
  374.         $groupId $request->request->get('groupid');
  375.         $memberOrder json_decode($request->request->get('order'));
  376.         $em $this->getDoctrine()->getManager();
  377.         foreach ($memberOrder as $order) {
  378.             $group $this->getDoctrine()
  379.                 ->getRepository('OceanExpertBundle:MemberGroups')
  380.                 ->findOneBy(
  381.                     array(
  382.                         'idGroup' => $groupId,
  383.                         'idInd' =>$order->idInd
  384.                     )
  385.                 );
  386.             if ($group) {
  387.                 $group->setMemberOrder($order->newData);
  388.                 $em->persist($group);
  389.                 $em->flush();
  390.             } else {
  391.                 return new JsonResponse(
  392.                     array(
  393.                         'status' => 0,
  394.                         'message' => "cannot find group with id '$groupId' for expert '" $order->idInd "'"
  395.                     )
  396.                 );
  397.             }
  398.         }
  399.         return new JsonResponse(
  400.             array(
  401.                 'status' => 1,
  402.                 'message' => 'Group order saved'
  403.             )
  404.         );
  405.     }
  406.     /**
  407.      * @return mixed
  408.      */
  409.     public static function allGroups($doctrine) {
  410.         //$groups = $this->getDoctrine()
  411.         return $doctrine
  412.             ->getRepository('OceanExpertBundle:Groups')
  413.             ->createQueryBuilder('e')
  414.             ->select('e.idGroup, e.groupname, e.idParentgroup, e.groupOrder')
  415.             ->addOrderBy('e.idParentgroup''ASC')
  416.             ->addOrderBy('e.groupOrder''ASC')
  417.             ->getQuery()
  418.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  419.     }
  420.     /**
  421.      * @param $parentGroupId
  422.      * @param $groups
  423.      *
  424.      * @return array
  425.      */
  426.     public static function childgroupsof($parentGroupId$groups): array
  427.     {
  428.         $i=0;
  429.         $children = array();
  430.         foreach ($groups as $childGroup) {
  431.             if ($childGroup['idParentgroup'] == $parentGroupId) {
  432.                 $children[$i]['idGroup'] = $childGroup['idGroup'];
  433.                 $children[$i]['groupname'] = $childGroup['groupname'];
  434.                 $children[$i]['idParentgroup'] = $childGroup['idParentgroup'];
  435.                 $children[$i]['groupOrder'] = $childGroup['groupOrder'];
  436.                 $i++;
  437.             }
  438.         }
  439.         return $children;
  440.     }
  441.     /**
  442.      * get all the groups that are a descendant of this group
  443.      *
  444.      * @param int   $parentGroupId
  445.      * @param array $groups all the groups to check
  446.      *
  447.      * @return array
  448.      */
  449.     public static function descendantsOf(int $parentGroupId, array $groups): array
  450.     {
  451.         $result = array();
  452.         $children GroupController::childgroupsof($parentGroupId$groups);
  453.         if ($children) {
  454.             $result $children;
  455.             for ($i 0$i count($children); $i++) {
  456.                 $desc GroupController::descendantsOf($children[$i]['idGroup'], $groups);
  457.                 if ($desc) {
  458.                     $result GroupController::insertIntoAt($desc$result$children[$i]);
  459.                 }
  460.             }
  461.         }
  462.         return $result;
  463.     }
  464.     /**
  465.      * @param $subarray
  466.      * @param $array
  467.      * @param $element
  468.      *
  469.      * @return mixed
  470.      */
  471.     private static function insertIntoAt($subarray$array$element)
  472.     {
  473.         for($i 0$i count($array); $i++) {
  474.             if ($array[$i] == $element) {
  475.                 array_splice($array$i+10$subarray);
  476.                 return $array;
  477.             }
  478.         }
  479.         return $array;
  480.     }
  481.     /**
  482.      * get the list of groups where this group is a member of
  483.      *
  484.      * @param int $groupId the (sub)group to start with
  485.      *
  486.      * @return array
  487.      */
  488.     public static function getAncestors(int $groupId$doctrine): array
  489.     {
  490.         //this is useless and overwritten
  491.         //$group = $this->getGroupArrayById($groupId);
  492.         $i 0;
  493.         $ancestors = array();
  494.         do {
  495.             $group self::getGroupArrayById($groupId$doctrine);
  496.             if (!$group
  497.                 || !is_array($group)
  498.                 || !count($group)
  499.             ) {
  500.                 //this will result in an endless loop
  501.                 break;
  502.             }
  503.             $groupId $group['idParentgroup'];
  504.             $ancestors[] = $group;
  505.             $i++;
  506.         } while($group['idParentgroup'] != 0);
  507.         return $ancestors;
  508.     }
  509.     /**
  510.      * get the information about a group as an associative array
  511.      *
  512.      * @param int $groupId
  513.      *
  514.      * @return array
  515.      */
  516.     static function getGroupArrayById($groupId$doctrine)
  517.     {
  518.         //$group = $this->getDoctrine()
  519.         $group $doctrine
  520.             ->getRepository('OceanExpertBundle:Groups')
  521.             ->createQueryBuilder('e')
  522.             ->select('e.idGroup, e.groupname, e.idParentgroup, e.groupOrder')
  523.             ->where('e.idGroup = :groupid')
  524.             ->setParameter('groupid',$groupId)
  525.             ->getQuery()
  526.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  527.         if (is_array($group)
  528.             && isset($group['0'])
  529.         ) {
  530.             return $group['0'];
  531.         } else {
  532.             return array();
  533.         }
  534.     }
  535.     /**
  536.      * get the details of a group with given group id
  537.      *
  538.      * @param int $groupId id of the group
  539.      *
  540.      * @return mixed
  541.      */
  542.     static function getGroupDetailsById(int $groupId$doctrine)
  543.     {
  544.         //$group = $this->getDoctrine()
  545.         $group $doctrine
  546.             ->getRepository('OceanExpertBundle:Groups')
  547.             ->createQueryBuilder('e')
  548.             ->select('e')
  549.             ->where('e.idGroup = :groupid')
  550.             ->setParameter('groupid'$groupId)
  551.             ->getQuery()
  552.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  553.         if ($group
  554.             && is_array($group)
  555.             && isset($group['0'])
  556.         ) {
  557.             $group $group['0'];
  558.             if (!isset($group['description'])
  559.                 || $group['description'] == ''
  560.             ) {
  561.                 $group['description'] = 'no description for this group';
  562.             }
  563.             if (!isset($group['officialBody'])
  564.                 || $group['officialBody'] == ''
  565.             ) {
  566.                 $group['officialBody'] = 'no Official Body info for this group';
  567.             }
  568.             return $group;
  569.         } else {
  570.             return false;
  571.         }
  572.     }
  573.     /**
  574.      * @param $groupId
  575.      * @return mixed
  576.      */
  577.     function getGroupExpertsById($groupId)
  578.     {
  579.         $memberId $this->getDoctrine()
  580.             ->getRepository('OceanExpertBundle:MemberGroups')
  581.             ->createQueryBuilder('g')
  582.             ->select('g.idInd,g.idGroup,g.role,g.isLeader')
  583.             ->where('g.idGroup = :groupid')
  584.             ->setParameter('groupid',$groupId)
  585.             ->getQuery()
  586.             ->getResult();
  587.         return $memberId;
  588.     }
  589.     /**
  590.      * get an array of all the subgroups under a given group
  591.      * this is a recursive function that will keep on digging till nothing is found anymore
  592.      *
  593.      * @param int $groupId id of the parent group
  594.      *
  595.      * @return array|boolean
  596.      **/
  597.     function getAllSubGroups(int $groupId)
  598.     {
  599.         $subGroupIds $this->getDoctrine()
  600.             ->getRepository('OceanExpertBundle:Groups')
  601.             ->createQueryBuilder('g')
  602.             ->select('g.idGroup')
  603.             ->where('g.idParentgroup = :groupId')
  604.             ->setParameter('groupId'$groupId)
  605.             ->getQuery()
  606.             ->getResult();
  607.         if (is_array($subGroupIds)
  608.             && count($subGroupIds)
  609.         ) {
  610.             foreach ($subGroupIds as $subGroupId) {
  611.                 $allGroupIds[] = $subGroupId['idGroup'];
  612.                 $subSubGroups $this->getAllSubGroups($subGroupId['idGroup']);
  613.                 if ($subSubGroups != false) {
  614.                     $allGroupIds array_merge($allGroupIds$subSubGroups);
  615.                 }
  616.             }
  617.             return $allGroupIds;
  618.         } else {
  619.             return false;
  620.         }
  621.     }
  622.     /**
  623.      * get a list of all email addresses of the members of a group
  624.      * AND all the subgroups
  625.      *
  626.      * @param Request $request
  627.      *
  628.      * @return Response
  629.      */
  630.     function getGroupExpertsAddressesAction(Request $request): Response
  631.     {
  632.         $addresses = array();
  633.         $groupId = (int) $request->get('groupId');
  634.         $allGroupIds $this->getAllSubGroups($groupId);
  635.         if (is_array($allGroupIds)) {
  636.             $allGroupIds[] = $groupId;
  637.         } else {
  638.             $allGroupIds = array($groupId);
  639.         }
  640.         foreach ($allGroupIds as $groupId) {
  641.             //get the data of the members of the group
  642.             //use the SSP class to get the data, so we have the same data as in the datatable
  643.             //we need this data
  644.             // #852 add idNationality to the export
  645.             $columns = array(
  646.                 array('db' => 'email1''dt' => 0),
  647.                 array('db' => 'email2''dt' => 1),
  648.                 array('db' => 'fname''dt' => 2),
  649.                 array('db' => 'mname''dt' => 3),
  650.                 array('db' => 'sname''dt' => 4),
  651.                 array('db' => 'country''dt' => 5),
  652.                 array('db' => 'idNationality''dt' => 6),
  653.                 array('db' => 'idInd''dt' => 7)
  654.             );
  655.             $ssp = new SSP();
  656.             $allExpertData $ssp->simple(
  657.                 array(
  658.                     'group' => $groupId
  659.                 ),
  660.                 $this->getDoctrine()->getManager(),
  661.                 'email1',
  662.                 $columns
  663.             );
  664.             //dump($allExpertData);
  665.             $allExpertData $allExpertData['data'];
  666.             //dump($allExpertData);
  667.             //die();
  668.             //get the data in the correct format
  669.             //if there is a second address, use that one
  670.             foreach ($allExpertData as $expertData) {
  671.                 //should never happen...
  672.                 // #852 add idNationality to the export
  673.                 if (!isset($expertData[6])) {
  674.                     $expertData[6] = 'unknown';
  675.                 }
  676.                 //decide what email address to use
  677.                 if (isset($expertData[1])
  678.                     && $expertData[1] != ''
  679.                 ) {
  680.                     $email $expertData[1];
  681.                 } else {
  682.                     $email $expertData[0];
  683.                 }
  684.                 //get the name of the institute #881
  685.                 $indivInst $this->getDoctrine()
  686.                     ->getRepository('OceanExpertBundle:IndivInstitution')
  687.                     ->findOneByIdInd($expertData[7]);
  688.                 if ($indivInst) {
  689.                     $idInst $indivInst->getIdInst();
  690.                     $inst $this->getDoctrine()
  691.                         ->getRepository('OceanExpertBundle:Institutions')
  692.                         ->findOneByIdInst($idInst);
  693.                     if ($inst) {
  694.                         $instName $inst->getInstName();
  695.                     } else {
  696.                         $instName 'unknown institute';
  697.                     }
  698.                 } else {
  699.                     $instName 'no affiliation';
  700.                 }
  701.                 // #852 add nationality to the export
  702.                 $addresses[$email] = array(
  703.                     'email' => $email,
  704.                     'fname' => $expertData[2],
  705.                     'mname' => $expertData[3],
  706.                     'sname' => $expertData[4],
  707.                     'country' => htmlspecialchars_decode($expertData[5], ENT_QUOTES),
  708.                     'nationality' => $expertData[6],
  709.                     'institute' => $instName
  710.                 );
  711.             }
  712.         }
  713.         //create the csv file
  714.         $response $this->render(
  715.             'Group/showAddresses.html.twig',
  716.             array(
  717.                 'data' => array(
  718.                     'addresses' => $addresses
  719.                 )
  720.             )
  721.         );
  722.         $response->headers->set(
  723.             'Content-Type',
  724.             'text/csv'
  725.         );
  726.         $response->headers->set(
  727.             'Content-Disposition',
  728.             'attachment; filename="OceanExpertGroup' $groupId 'Addresses_' date('Ymd') . '.csv"');
  729.         return $response;
  730.     }
  731.     /**
  732.      * @param Request $request
  733.      * @param int $id
  734.      * @param int $groupId
  735.      * @param int $i
  736.      *
  737.      * @return array
  738.      */
  739.     function getExpertDetailsById(Request $request$id$groupId null$i 1): array
  740.     {
  741.         $member $this->getDoctrine()
  742.             ->getRepository('OceanExpertBundle:Indiv')
  743.             ->createQueryBuilder('i')
  744.             ->select('i.idInd,i.fname,i.sname,i.email1,i.jobtitle,ins.idInst,ins.instName,ins.instNameEng,i.useInstAddr,ci.country as insCountry,c.country')
  745.             ->leftJoin('OceanExpertBundle:IndivInstitution''ii''WITH''ii.idInd = i.idInd')
  746.             ->leftJoin('OceanExpertBundle:Institutions''ins''WITH''ii.idInst = ins.idInst')
  747.             ->leftJoin('OceanExpertBundle:Countries''c''WITH''i.countryCode = c.idCountry')
  748.             ->leftJoin('OceanExpertBundle:Countries''ci''WITH''ins.countryCode = ci.idCountry')
  749.             ->where('i.idInd = :id')
  750.             ->andWhere('i.status = 1')
  751.             ->setParameter('id',$id)
  752.             ->getQuery()
  753.             ->getResult();
  754.         $grouprole'';
  755.         $groupOrder'';
  756.         $primaryGroup'';
  757.         if ($groupId) {
  758.             $memberGroup $this->getDoctrine()
  759.                 ->getRepository('OceanExpertBundle:MemberGroups')
  760.                 ->findOneBy(
  761.                     array(
  762.                         'idGroup' => $groupId,
  763.                         'idInd' => $id
  764.                     )
  765.                 );
  766.             $groupOrder $i;
  767.             if ($memberGroup) {
  768.                 $primaryGroup $groupId;
  769.                 $grouprole $memberGroup->getRole();
  770.                 $leader $memberGroup->getIsLeader();
  771.                 $groupOrder $i;
  772.                 if ( $memberGroup->getMemberOrder() != null) {
  773.                     $groupOrder $memberGroup->getMemberOrder();
  774.                 }
  775.             } else {
  776.                 $primaryGroup '';
  777.                 $grouprole '';
  778.                 $leader '';
  779.             }
  780.         }
  781.         $memberdata = array();
  782.         if ($member) {
  783.             $memberdata['idInd'] = $member[0]['idInd'];
  784.             $memberdata['fname'] = $member[0]['fname'];
  785.             $memberdata['sname'] = $member[0]['sname'];
  786.             if (file_exists('uploads/profile/profile_' $member[0]['idInd'].'.png')) {
  787.                 $memberdata['image'] = $request->getBasePath() . '/uploads/profile/profile_' $member[0]['idInd'].'.png';
  788.             } else {
  789.                 $memberdata['image'] =  $request->getBasePath() . '/images/default.png';
  790.             }
  791.             $memberdata['name'] = $member[0]['fname'].' '.$member[0]['sname'];
  792.             $memberdata['jobtitle'] = $member[0]['jobtitle'];
  793.             $memberdata['idInst'] = $member[0]['idInst'];
  794.             $memberdata['instName'] = $member[0]['instName'];
  795.             $memberdata['instNameEng'] = $member[0]['instNameEng'];
  796.             $memberdata['useInstAddr'] = $member[0]['useInstAddr'];
  797.             if ($member[0]['useInstAddr'] === 1) {
  798.                 $memberdata['country'] = $member[0]['insCountry'];
  799.                 $memberdata['countryCode'] = $member[0]['insCountryCode'];
  800.             } else {
  801.                 $memberdata['country'] = $member[0]['country'];
  802.                 $memberdata['countryCode'] = $member[0]['countryCode'];
  803.             }
  804.             $memberdata['insCountry'] = $member[0]['insCountry'];
  805.             $memberdata['groupRole'] = $grouprole;
  806.             $memberdata['groupOrder'] = $groupOrder;
  807.             $memberdata['primaryGroup'] = $primaryGroup;
  808.             $memberdata['leader'] = isset($leader)?$leader:'';
  809.         }
  810.         return $memberdata;
  811.     }
  812.     /**
  813.      * @param         $id
  814.      * @param Request $request
  815.      *
  816.      * @return Response
  817.      */
  818.     public function getGroupDataAction($idRequest $request)
  819.     {
  820.         $isGroupAdmin $this->hasEditPermission($id);
  821.         $file $request->getBasePath() . 'groups/' $id '.json';
  822.         $members file_get_contents($file);
  823.         $memberData json_decode($memberstrue);
  824.         if ($request->request->get('action') == 'remove') {
  825.             foreach ($request->request->get('data') as $index => $item) {
  826.                 $this->removeMemberFromGroup($item['idInd'], $id$request);
  827.             }
  828.         }
  829.         if ($request->request->get('action') == 'edit') {
  830.             foreach ($request->request->get('data') as $index => $item) {
  831.                 $this->editMemberGroupRole($item['idInd'], $id$item['groupRole'], $request);
  832.             }
  833.         }
  834.         $i 1;
  835.         foreach ($memberData['data'] as $key => $member) {
  836.             if ($member['groupOrder'] == '') {
  837.                 $member['groupOrder'] = $i;
  838.                 $i++;
  839.             }
  840.         }
  841.         return new JsonResponse($members);
  842.     }
  843.     /**
  844.      * @param int     $groupId id of the group
  845.      * @param Request $request
  846.      *
  847.      * @todo do we still need this???
  848.      *
  849.      * @return Response
  850.      */
  851.     public function rebuildGroupsAction(int $groupIdRequest $request): Response
  852.     {
  853.         set_time_limit(0);
  854.         if ($this->container->has('profiler'))
  855.         {
  856.             $this->container->get('profiler')->disable();
  857.         }
  858.         $em $this->getDoctrine()->getManager();
  859.         $query $em->createQuery(
  860.             'SELECT gs.idGroup 
  861.             FROM OceanExpertBundle\Entity\Groups gs 
  862.             WHERE gs.idGroup > 30 ');
  863.         $result $query->getResult();
  864.         if (count($result) == 0) {
  865.             echo 'Finished';
  866.             exit();
  867.         }
  868.         $ids array_map('current'$result);
  869.         if ($request->query->get('count')) {
  870.             $count $request->query->get('count');
  871.         } else {
  872.             $count 0;
  873.         }
  874.         if (!isset($ids[$count])) {
  875.             echo 'Array finished';
  876.             exit();
  877.         }
  878.         $groupId $ids[$count];
  879.         $allGroups self::allGroups($this->getDoctrine());
  880.         $doctrine $this->getDoctrine();
  881.         $em $doctrine->getManager();
  882.         if ($groupId == 99999) {
  883.             $groups self::descendantsOf($groupId,$allGroups);
  884.         } else {
  885.             $groups = array(
  886.                 array(
  887.                     'idGroup' => $groupId
  888.                 )
  889.             );
  890.         }
  891.         $subgroups self::descendantsOf($groups[0]['idGroup'],$allGroups);
  892.         $subgroups[] = $this->getGroupArrayById($groupId$doctrine);
  893.         $i 0;
  894.         $allmembers = array();
  895.         foreach ($subgroups as $group) {
  896.             $members $this->getGroupExpertsById($group['idGroup']);
  897.             if ($members) {
  898.                 foreach ($members as $member ) {
  899.                     if ($groupId == $member['idGroup'] ) {
  900.                         $allmembers[$i]['id'] = '';
  901.                         $allmembers[$i]['idInd'] = $member['idInd'];
  902.                         $allmembers[$i]['idGroup'] = $groupId;
  903.                         $allmembers[$i]['mainGroup'] = $group['idGroup'];
  904.                         $allmembers[$i]['isLeader'] = $member['isLeader'];
  905.                         $allmembers[$i]['role'] = $member['role'];
  906.                         $allmembers[$i]['lname'] = '';
  907.                     } else {
  908.                         $allmembers[$i]['id'] = '';
  909.                         $allmembers[$i]['idInd'] = $member['idInd'];
  910.                         $allmembers[$i]['idGroup'] = $groupId;
  911.                         $allmembers[$i]['mainGroup'] = $group['idGroup'];
  912.                         $allmembers[$i]['isLeader'] = '';
  913.                         $allmembers[$i]['role'] = '';
  914.                         $allmembers[$i]['lname'] = '';
  915.                     }
  916.                     $i++;
  917.                 }
  918.             }
  919.         }
  920.         $allmembers $this->array_msort(
  921.             $allmembers,
  922.             array(
  923.                 'role' => SORT_DESC,
  924.                 'isLeader' => SORT_DESC
  925.             )
  926.         );
  927.         $uniquemembers $this->unique_multidim_array(
  928.             $allmembers,
  929.             'idInd'
  930.         );
  931.         $file 'groups/' $groupId '.csv';
  932.         $fp fopen($file'w');
  933.         foreach ($uniquemembers as $member) {
  934.             fputcsv($fp$member);
  935.         }
  936.         fclose($fp);
  937.         /*
  938.         * Upload CSV Files in database
  939.         */
  940.         $dbhost $this->container->getParameter('database_host');
  941.         $dbuser $this->container->getParameter('database_user');
  942.         $dbpass $this->container->getParameter('database_password');
  943.         $connParams $em->getConnection()->getParams();
  944.         //@todo what the hell is this doing here???? remove this Arno 06/04/2022
  945.         /*
  946.         $pdoConn = new \PDO(
  947.             'mysql:host=' . $dbhost . ';dbname=' . $connParams['dbname'],
  948.             $dbuser,
  949.             $dbpass,
  950.             array(
  951.                 \PDO::MYSQL_ATTR_LOCAL_INFILE => true
  952.             )
  953.         );
  954.         $sql = "DELETE FROM group_members_ids
  955.             WHERE id_group = '$groupId'";
  956.         $stmt = $pdoConn->prepare($sql);
  957.         $stmt->execute();
  958.         $sql = "SET FOREIGN_KEY_CHECKS=0";
  959.         $stmt = $pdoConn->prepare($sql);
  960.         $stmt->execute();
  961.         $sql = "LOAD DATA LOCAL INFILE '$file' IGNORE
  962.             INTO TABLE group_members_ids
  963.             FIELDS TERMINATED BY ','
  964.             ENCLOSED BY '\"'
  965.             ESCAPED BY '\"'";
  966.         $stmt = $pdoConn->prepare($sql);
  967.         $stmt->execute();
  968.         */
  969.         unlink($file);
  970.         $count ++;
  971.         return $this->render(
  972.             'Group/rebuildGroup.html.twig',
  973.             array(
  974.                 'count' => $count
  975.             )
  976.         );
  977.     }
  978.     /**
  979.      * @todo do we still need this????
  980.      *       if so remove code for group_members table!!!
  981.      *
  982.      * @param Request $request
  983.      *
  984.      * @return Response
  985.      */
  986.     public function updateMemberIdsAction(Request $request): Response
  987.     {
  988.         if ($request->query->get('count')) {
  989.             $count $request->query->get('count');
  990.         } else {
  991.             $count 0;
  992.         }
  993.         $message = array();
  994.         $em $this->getDoctrine()->getManager();
  995.         $limit =100;
  996.         $offset $count $limit;
  997.         $members $em->createQueryBuilder()
  998.             ->select('i.idInd')
  999.             ->from('OceanExpertBundle:GroupMembersIds''i')
  1000.             ->where('i.idInd is NOT NULL')
  1001.             ->andWhere('i.lname is NOT NULL')
  1002.             ->setMaxResults($limit)
  1003.             ->setFirstResult($offset)
  1004.             ->getQuery()->getResult();
  1005.         if (count($members) ==0) {
  1006.             echo 'All data has been populated.';
  1007.             exit();
  1008.         }
  1009.         foreach ($members as $member) {
  1010.             $memberdetail $this->getDoctrine()
  1011.                 ->getRepository('OceanExpertBundle:Indiv')
  1012.                 ->findOneByIdInd($member['idInd']);
  1013.             if ($memberdetail) {
  1014.                 $memberId $this->getDoctrine()
  1015.                     ->getRepository('OceanExpertBundle:GroupMembersIds')
  1016.                     ->findByIdInd($memberdetail->getIdInd());
  1017.                 if ($memberId) {
  1018.                     foreach ($memberId as $item) {
  1019.                         $item->setLname($memberdetail->getSname());
  1020.                         $em->persist($item);
  1021.                         $em->flush();
  1022.                     }
  1023.                 }
  1024.             }
  1025.         }
  1026.         $count $count+1;
  1027.         return $this->render(
  1028.             'OceanExpertBundle:Group:rebuild.group.htm.twig',
  1029.             array(
  1030.                 'count' => $count
  1031.             )
  1032.         );
  1033.     }
  1034.     /**
  1035.      * @todo this should be deleted, uses the wrong tables Arno 06/04/2022
  1036.      *
  1037.      * @param Request $request
  1038.      *
  1039.      * @return Response
  1040.      */
  1041.     public function updateGroupMembersAction(Request $request): Response
  1042.     {
  1043.         $message = array();
  1044.         $smember $this->getDoctrine()
  1045.             ->getRepository('OceanExpertBundle:GroupMembersIds')
  1046.             ->findOneBy(
  1047.                 array(),
  1048.                 array(
  1049.                     'isLeader' => 'DESC',
  1050.                     'role' => 'DESC',
  1051.                     'lname' => 'ASC'
  1052.                 )
  1053.             );
  1054.         if (!$smember) {
  1055.             echo 'All data has been populated.';
  1056.             exit();
  1057.         }
  1058.         $members $this->getDoctrine()
  1059.             ->getRepository('OceanExpertBundle:GroupMembersIds')
  1060.             ->findBy(
  1061.                 array(
  1062.                     'idGroup' => $smember->getIdGroup()
  1063.                 ),
  1064.                 array(
  1065.                     'isLeader' => 'DESC',
  1066.                     'role' => 'DESC',
  1067.                     'lname' => 'ASC'
  1068.                 ),
  1069.                 100
  1070.             );
  1071.         $em $this->getDoctrine()->getManager();
  1072.         foreach ($members as $member) {
  1073.             $memberDetails $this->getExpertDetailsById(
  1074.                 $request,
  1075.                 $member->getIdInd(),
  1076.                 $member->getIdGroup()
  1077.             );
  1078.             $addMember $this->getDoctrine()
  1079.                 ->getRepository('OceanExpertBundle:GroupMembers')
  1080.                 ->findBy(
  1081.                     array(
  1082.                         'idInd' => $member->getIdInd(),
  1083.                         'idGroup' => $member->getIdGroup()
  1084.                     )
  1085.                 );
  1086.             $em->remove($member);
  1087.             $em->flush();
  1088.         }
  1089.         return $this->render(
  1090.             'OceanExpertBundle:Group:rebuild.group.htm.twig'
  1091.         );
  1092.     }
  1093.     /**
  1094.      * @param int $id      id of the expert to be removed
  1095.      * @param int $groupId id of the group from where the expert should be removed
  1096.      *
  1097.      * @return void
  1098.      */
  1099.     function removeMemberFromGroup(int $idint $groupIdRequest $request)
  1100.     {
  1101.         $em $this->getDoctrine()->getManager();
  1102.         $member $this->getDoctrine()
  1103.             ->getRepository('OceanExpertBundle:MemberGroups')
  1104.             ->findOneBy(
  1105.                 array(
  1106.                     'idInd' => $id,
  1107.                     'idGroup' => $groupId
  1108.                 )
  1109.             );
  1110.         $em->remove($member);
  1111.         $em->flush();
  1112.         $this->rebuildGroupsAction($groupId$request);
  1113.     }
  1114.     /**
  1115.      * @param int     $id
  1116.      * @param int     $groupId
  1117.      * @param string  $role
  1118.      * @param Request $request
  1119.      *
  1120.      * @return void
  1121.      */
  1122.     function editMemberGroupRole(int $idint $groupIdstring $roleRequest $request)
  1123.     {
  1124.         $em $this->getDoctrine()->getManager();
  1125.         $member $this->getDoctrine()
  1126.             ->getRepository('OceanExpertBundle:MemberGroups')
  1127.             ->findOneBy(
  1128.                 array(
  1129.                     'idInd' => $id,
  1130.                     'idGroup' => $groupId
  1131.                 )
  1132.             );
  1133.         $member->setRole($role);
  1134.         $em->persist($member);
  1135.         $em->flush();
  1136.         $this->rebuildGroupsAction($groupId$request);
  1137.     }
  1138.     /**
  1139.      * membership to a group gives some people more power
  1140.      * @todo : this is not a maintainable way of managing roles!!!!!!
  1141.      *
  1142.      * @param int $groupId id of the group to check
  1143.      *
  1144.      * @return bool
  1145.      */
  1146.     function hasEditPermission(int $groupId): bool
  1147.     {
  1148.         $loggedUser $this->get('security.token_storage')->getToken()->getUser();
  1149.         $allAdminGroups = array(
  1150.             //31 => array(161),
  1151.             //315 => array(57),
  1152.             //47 => array(57),
  1153.             //138 => array(249),
  1154.             //127 => array(34,35,307),
  1155.             //32 => array(34,35,307),
  1156.             //46 => array(57),
  1157.             //269 => array(57),
  1158.             //291 => array(292),
  1159.             //78 => array(79),
  1160.             //169 => array(239),
  1161.             //234 => array(57),
  1162.             //236 => array(247),
  1163.             537 => array(564//members of 'OBON Group Admins' have edit permission for the 'OBON' group
  1164.         );
  1165.         if (array_key_exists($groupId$allAdminGroups)) {
  1166.             $adminGroup $allAdminGroups[$groupId];
  1167.             $userGroups $this->getDoctrine()
  1168.                 ->getRepository('OceanExpertBundle:MemberGroups')
  1169.                 ->findBy(
  1170.                     array(
  1171.                         'idInd' => $loggedUser->getId()
  1172.                     )
  1173.                 );
  1174.             foreach ($userGroups as $groups) {
  1175.                 if (in_array($groups->getIdGroup(), $adminGroup)) {
  1176.                     return true;
  1177.                 }
  1178.             }
  1179.         }
  1180.         return false;
  1181.     }
  1182.     /**
  1183.      * @return JsonResponse
  1184.      */
  1185.     function getAllGroupJsonAction(): JsonResponse
  1186.     {
  1187.         $groups $this->getDoctrine()
  1188.             ->getRepository('OceanExpertBundle:Groups')
  1189.             ->createQueryBuilder('e')
  1190.             ->select('e.idGroup, e.groupname, e.idParentgroup, e.groupOrder')
  1191.             ->addOrderBy('e.groupname''ASC')
  1192.             ->getQuery()
  1193.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  1194.         return new JsonResponse($groups);
  1195.     }
  1196.     /**
  1197.      * @param Request $request
  1198.      * @param int $id id of the expert to be added to the group, defaults to 0
  1199.      * @param int $groupId id of the group where we want to add the expert to, defaults to 0
  1200.      *
  1201.      * @return JsonResponse
  1202.      */
  1203.     function addGroupMemberAction(Request $request$id 0$groupId 0): JsonResponse
  1204.     {
  1205.         if (!$id
  1206.             && !$groupId
  1207.         ) {
  1208.             $groupId $request->request->get('groupid');
  1209.             $id $request->request->get('userid');
  1210.             $groupRole $request->request->get('grouprole');
  1211.         } else {
  1212.             $groupRole '';
  1213.         }
  1214.         $em $this->getDoctrine()->getManager();
  1215.         $member $this->getDoctrine()
  1216.             ->getRepository('OceanExpertBundle:MemberGroups')
  1217.             ->findOneBy(
  1218.                 array(
  1219.                     'idInd' => $id,
  1220.                     'idGroup' => $groupId
  1221.                 )
  1222.             );
  1223.         if (!$member) {
  1224.             $member = new MemberGroups();
  1225.             $member->setIdInd($id);
  1226.             $member->setIdGroup($groupId);
  1227.             $member->setRole($groupRole);
  1228.             $member->setMemberOrder(0);
  1229.             $member->setIsLeader(0);
  1230.             $em->persist($member);
  1231.             $em->flush();
  1232.         }
  1233.         //check if we are in all the parent groups also
  1234.         //if not add this expert to the parent groups
  1235.         //@todo is that a good idea???
  1236.         /*
  1237.         foreach ($this->getAncestors($groupId) as $childGroup) {
  1238.             $memberGroup = $this->getDoctrine()
  1239.                 ->getRepository('OceanExpertBundle:MemberGroups')
  1240.                 ->findOneBy(
  1241.                     array(
  1242.                         'idInd'=>$id,
  1243.                         'idGroup' => $childGroup['idGroup']
  1244.                     )
  1245.                 );
  1246.             if (!$memberGroup) {
  1247.                 $this->addGroupMemberAction($id, $childGroup['idGroup'], $request);
  1248.             }
  1249.             /*
  1250.             if (!$memberGroup) {
  1251.                 $maxorder = $em->createQueryBuilder()
  1252.                     ->select('MAX(e.groupOrder)')
  1253.                     ->from('OceanExpertBundle:GroupMembers', 'e')
  1254.                     ->where('e.idGroup = :groupid')
  1255.                     ->setParameter('groupid',$childGroup['idGroup'])
  1256.                     ->getQuery()
  1257.                     ->getSingleScalarResult();
  1258.                 $order = $maxorder+1;
  1259.                 $memberdata = $this->getExpertDetailsById($id);
  1260.                 $memberGroup = new GroupMembers();
  1261.                 $memberGroup->setIdInd($id);
  1262.                 $memberGroup->setIdGroup($childGroup['idGroup']);
  1263.                 $memberGroup->setFname($memberdata['fname']);
  1264.                 $memberGroup->setLname($memberdata['sname']);
  1265.                 $memberGroup->setImage($memberdata['image']);
  1266.                 if ($childGroup['idGroup']==$groupId) {
  1267.                     $memberGroup->setGroupRole($groupRole);
  1268.                 } else {
  1269.                     $memberGroup->setGroupRole('');
  1270.                 }
  1271.                 $memberGroup->setIdInst($memberdata['idInst']);
  1272.                 $memberGroup->setInstName($memberdata['instName']);
  1273.                 $memberGroup->setInstNameEng($memberdata['instNameEng']);
  1274.                 $memberGroup->setUseInstAddr($memberdata['useInstAddr']);
  1275.                 $memberGroup->setInsCountry($memberdata['insCountry']);
  1276.                 $memberGroup->setCountry($memberdata['country']);
  1277.                 $memberGroup->setGroupOrder($order);
  1278.                 $memberGroup->setMainGroup($groupId);
  1279.                 $em->persist($memberGroup);
  1280.                 $em->flush();
  1281.             }
  1282.         }
  1283.         //end inner comment
  1284. */
  1285.         return new JsonResponse(
  1286.             array(
  1287.                 'status' => 1,
  1288.                 'message' => 'Member added to group'
  1289.             )
  1290.         );
  1291.     }
  1292.     /**
  1293.      * @todo remove code for group_members
  1294.      *
  1295.      * @param Request $request
  1296.      * @param array $data
  1297.      * @param int $groupId id of the group where to add the experts, defaults to 0
  1298.      *
  1299.      * @return JsonResponse
  1300.      */
  1301.     function addBulkGroupMemberAction(Request $request$data = array(), $groupId 0): JsonResponse
  1302.     {
  1303.         $groupId $request->request->get('groupid');
  1304.         $memberdata $request->request->get('memberdata');
  1305.         $memberdataArr json_decode($memberdata);
  1306.         foreach ($memberdataArr as $addmemberdata) {
  1307.             $em $this->getDoctrine()->getManager();
  1308.             $member $this->getDoctrine()
  1309.                 ->getRepository('OceanExpertBundle:MemberGroups')
  1310.                 ->findOneBy(
  1311.                     array(
  1312.                         'idInd' => $addmemberdata->id,
  1313.                         'idGroup' => $groupId
  1314.                     )
  1315.                 );
  1316.             if (!$member) {
  1317.                 $member = new MemberGroups();
  1318.                 $member->setIdInd($addmemberdata->id);
  1319.                 $member->setIdGroup($groupId);
  1320.                 $member->setRole($addmemberdata->role);
  1321.                 $member->setMemberOrder(0);
  1322.                 $member->setIsLeader(0);
  1323.                 $em->persist($member);
  1324.                 $em->flush();
  1325.             }
  1326.             /*
  1327.             foreach ($this->getAncestors($groupId) as $childGroup) {
  1328.                 $groupMember = $this->getDoctrine()
  1329.                     ->getRepository('OceanExpertBundle:GroupMembers')
  1330.                     ->findOneBy(
  1331.                         array(
  1332.                             'idInd' => $addmemberdata->id,
  1333.                             'idGroup' => $childGroup['idGroup']
  1334.                         )
  1335.                     );
  1336.                 if (!$groupMember) {
  1337.                     $maxorder = $em->createQueryBuilder()
  1338.                         ->select('MAX(e.groupOrder)')
  1339.                         ->from('OceanExpertBundle:GroupMembers', 'e')
  1340.                         ->where('e.idGroup = :groupid')
  1341.                         ->setParameter('groupid',$childGroup['idGroup'])
  1342.                         ->getQuery()
  1343.                         ->getSingleScalarResult();
  1344.                     $order = $maxorder+1;
  1345.                     $memberdata = $this->getExpertDetailsById($addmemberdata->id);
  1346.                     $groupMember = new GroupMembers();
  1347.                     $groupMember->setIdInd($addmemberdata->id);
  1348.                     $groupMember->setIdGroup($childGroup['idGroup']);
  1349.                     $groupMember->setFname($memberdata['fname']);
  1350.                     $groupMember->setLname($memberdata['sname']);
  1351.                     $groupMember->setImage($memberdata['image']);
  1352.                     if ($childGroup['idGroup']==$groupId) {
  1353.                         $groupMember->setGroupRole($addmemberdata->role);
  1354.                     } else {
  1355.                         $groupMember->setGroupRole('');
  1356.                     }
  1357.                     $groupMember->setIdInst($memberdata['idInst']);
  1358.                     $groupMember->setInstName($memberdata['instName']);
  1359.                     $groupMember->setInstNameEng($memberdata['instNameEng']);
  1360.                     $groupMember->setUseInstAddr($memberdata['useInstAddr']);
  1361.                     $groupMember->setInsCountry($memberdata['insCountry']);
  1362.                     $groupMember->setCountry($memberdata['country']);
  1363.                     $groupMember->setGroupOrder($order);
  1364.                     $groupMember->setMainGroup($groupId);
  1365.                     $em->persist($groupMember);
  1366.                     $em->flush();
  1367.                 }
  1368.             }
  1369.             */
  1370.         }
  1371.         return new JsonResponse(
  1372.             array(
  1373.                 'status' => 1,
  1374.                 'message' => 'Member added to group'
  1375.             )
  1376.         );
  1377.     }
  1378.     /**
  1379.      * @return Response
  1380.      */
  1381.     public function setGroupRoleAction(Request $request)
  1382.     {
  1383.          $memberID $request->request->get('memberid');
  1384.          $groupID $request->request->get('groupid');
  1385.          $role $request->request->get('role');
  1386.          $member $this->getDoctrine()
  1387.              ->getRepository('OceanExpertBundle:MemberGroups')
  1388.              ->findOneBy(
  1389.                  array(
  1390.                      'idInd' => $memberID,
  1391.                      'idGroup' => $groupID
  1392.                  )
  1393.              );
  1394.          if ($member) {
  1395.              $em $this->getDoctrine()->getManager();
  1396.              $member->setRole($role);
  1397.              $em->persist($member);
  1398.              $em->flush();
  1399.          } else {
  1400.              return new JsonResponse(
  1401.                  array(
  1402.                      'status' => 0,
  1403.                      'message' => "Expert '$memberID' not found in group '$groupID'"
  1404.                  )
  1405.              );
  1406.          }
  1407.          return new JsonResponse(
  1408.              array(
  1409.                  'status' => 1,
  1410.                  'message' => 'Role updated successfully.'
  1411.              )
  1412.          );
  1413.      }
  1414.     /**
  1415.      * @return Response
  1416.      */
  1417.      public function deleteGroupMemberAction(Request $request)
  1418.      {
  1419.          $memberids $request->request->get('memberids');
  1420.          $groupId $request->request->get('groupid');
  1421.          foreach ($memberids as $idInd) {
  1422.              $member $this->getDoctrine()
  1423.                  ->getRepository('OceanExpertBundle:MemberGroups')
  1424.                  ->findOneBy(
  1425.                      array(
  1426.                          'idInd' => $idInd,
  1427.                          'idGroup' => $groupId
  1428.                      )
  1429.                  );
  1430.              if ($member) {
  1431.                  $em $this->getDoctrine()->getManager();
  1432.                  $em->remove($member);
  1433.                  $em->flush();
  1434.              }
  1435.          }
  1436.          return new JsonResponse(
  1437.              array(
  1438.                  'status' => 1,
  1439.                  'message' => 'Members deleted successfully.'
  1440.              )
  1441.          );
  1442.      }
  1443.     /**
  1444.      * add expert to group(s)
  1445.      *
  1446.      * @param Request $request
  1447.      *
  1448.      * @return Response
  1449.      */
  1450.     public function setGroupsAction(Request $request): Response
  1451.     {
  1452.         $id $request->request->get('userId');
  1453.         $groups $request->request->get('groups');
  1454.         $message '';
  1455.         $security_context $this->get('security.authorization_checker');
  1456.         if ($security_context->isGranted('ROLE_GLOBAL_EDITOR')) {
  1457.             $author $this->get('security.token_storage')->getToken()->getUser()->getId();
  1458.             $updateRecord $this->getDoctrine()
  1459.                 ->getRepository('OceanExpertBundle:Indiv')
  1460.                 ->findOneByIdInd($id);
  1461.             if ($updateRecord) {
  1462.                 $updateRecord->setLDateUpd(new \DateTime('now'));
  1463.                 $updateRecord->setLastEditBy($author);
  1464.                 $em $this->getDoctrine()->getManager();
  1465.                 $em->persist($updateRecord); //marks object to be saved in the next transaction.
  1466.                 $em->flush(); //performs all saves and transactions.
  1467.             }
  1468.             if (is_array($groups)) {
  1469.                 $em $this->getDoctrine()->getManager();
  1470.                 $results $em->createQueryBuilder()
  1471.                     ->add('select''m')
  1472.                     ->add('from''OceanExpertBundle:MemberGroups m')
  1473.                     ->where('m.idGroup not in (:groups)')
  1474.                     ->andWhere('m.idInd =:idInd')
  1475.                     ->setParameters(array('groups' => $groups'idInd' => $id))
  1476.                     ->getQuery()->getResult();
  1477.                 foreach ($results as $result) {
  1478.                     $em->remove($result);
  1479.                 }
  1480.                 $em->flush();
  1481.                 foreach ($groups as $value) {
  1482.                     $updateGroup $this->getDoctrine()
  1483.                         ->getRepository('OceanExpertBundle:MemberGroups')
  1484.                         ->find(
  1485.                             array(
  1486.                                 'idInd' => $id,
  1487.                                 'idGroup' => $value
  1488.                             )
  1489.                     );
  1490.                     if (!$updateGroup) {
  1491.                         $this->addGroupMemberAction($request$id$value);
  1492.                     }
  1493.                 }
  1494.             } else {
  1495.                 $updateGroup $this->getDoctrine()
  1496.                     ->getRepository('OceanExpertBundle:MemberGroups')
  1497.                     ->findBy(
  1498.                         array(
  1499.                             'idInd' => $id
  1500.                         )
  1501.                     );
  1502.                 $em $this->getDoctrine()->getEntityManager();
  1503.                 foreach ($updateGroup as $groups) {
  1504.                     $em->remove($groups);
  1505.                 }
  1506.                 $em->flush();
  1507.             }
  1508.         } else {
  1509.             $message json_encode(array(
  1510.                 'status' => 2,
  1511.                 'error' => 'you do not have the correct rights to do this'
  1512.             ));
  1513.         }
  1514.         return new Response($message);
  1515.     }
  1516.     /**
  1517.      * @param $array
  1518.      * @param $cols
  1519.      *
  1520.      * @return array
  1521.      */
  1522.     function array_msort($array$cols): array
  1523.     {
  1524.         $colarr = array();
  1525.         foreach ($cols as $col => $order) {
  1526.             $colarr[$col] = array();
  1527.             foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
  1528.         }
  1529.         $eval 'array_multisort(';
  1530.         foreach ($cols as $col => $order) {
  1531.             $eval .= '$colarr[\''.$col.'\'],'.$order.',';
  1532.         }
  1533.         $eval substr($eval,0,-1).');';
  1534.         eval($eval);
  1535.         $ret = array();
  1536.         foreach ($colarr as $col => $arr) {
  1537.             foreach ($arr as $k => $v) {
  1538.                 $k substr($k,1);
  1539.                 if (!isset($ret[$k])) $ret[$k] = $array[$k];
  1540.                 $ret[$k][$col] = $array[$k][$col];
  1541.             }
  1542.         }
  1543.         return $ret;
  1544.     }
  1545.     /**
  1546.      * @param $array
  1547.      * @param $key
  1548.      *
  1549.      * @return array
  1550.      */
  1551.     function unique_multidim_array($array$key): array
  1552.     {
  1553.         $temp_array = array();
  1554.         $i 0;
  1555.         $key_array = array();
  1556.         foreach($array as $val) {
  1557.             if (!in_array($val[$key], $key_array)) {
  1558.                 $key_array[$i] = $val[$key];
  1559.                 $temp_array[$i] = $val;
  1560.             }
  1561.             $i++;
  1562.         }
  1563.         return $temp_array;
  1564.     }
  1565.     /**
  1566.      * @todo : check what is going on here and if this is used Arno 5/07/22
  1567.      *
  1568.      * @return JsonResponse
  1569.      */
  1570.     public function sendGroupEmailAction(): JsonResponse
  1571.     {
  1572.         $message = array();
  1573.         $process = new Process(array('php ../app/console oceanexpert:create-users'));
  1574.         $process->start();
  1575.         while ($process->isRunning()) {
  1576.             // waiting for process to finish
  1577.         }
  1578.         echo $process->getOutput();
  1579.         return new JsonResponse(
  1580.             array(
  1581.                 'status' => 1,
  1582.                 'message' => $message
  1583.             )
  1584.         );
  1585.     }
  1586.     /**
  1587.      * @param EntityManagerInterface $entityManager
  1588.      *
  1589.      * @return JsonResponse
  1590.      */
  1591.     public function dataTablesServerProcessingAction(
  1592.         $groupId,
  1593.         EntityManagerInterface $entityManager
  1594.     ): JsonResponse {
  1595.         //dump($request);
  1596.         //die();
  1597.         // Table's primary key
  1598.         $primaryKey self::getPrimaryKey();
  1599.         // Array of database columns which should be read and sent back to DataTables.
  1600.         // The `db` parameter represents the column name in the database, while the `dt`
  1601.         // parameter represents the DataTables column identifier. In this case simple
  1602.         // indexes
  1603.         $columns self::getColumns();
  1604.         //check if we got some info from the request itself
  1605.         //pass this to the method
  1606.         //this used to be done by passing $_GET which is not the correct way
  1607.         //not useable for the API calls
  1608.         $request = array();
  1609.         //we are looking for these parameters
  1610.         $params = array(
  1611.             'order',
  1612.             'columns',
  1613.             'search',
  1614.             'group',
  1615.             'start',
  1616.             'length',
  1617.             'draw',
  1618.             '_'
  1619.         );
  1620.         foreach($params as $param) {
  1621.             $request[$param] = '';
  1622.             if (isset($_GET[$param])) {
  1623.                 $request[$param] = $_GET[$param];
  1624.             }
  1625.         }
  1626.         $ssp = new SSP();
  1627.         $data $ssp->simple(
  1628.             $request,
  1629.             $entityManager,
  1630.             $primaryKey,
  1631.             $columns
  1632.         );
  1633.         return new JsonResponse($data);
  1634.     }
  1635.     /**
  1636.      * give back the list of colums we need for the datatables
  1637.      * this is done by a get method as this is used by the API also
  1638.      *
  1639.      * @return array
  1640.      */
  1641.     public static function getColumns(): array
  1642.     {
  1643.         return array(
  1644.             array( 'db' => 'id''dt' => 'id' ),
  1645.             array( 'db' => 'memberOrder''dt' => 'groupOrder' ),
  1646.             array( 'db' => 'idInd',  'dt' => 'DT_RowId' ),
  1647.             array( 'db' => 'idInd',  'dt' => 'idInd' ),
  1648.             array( 'db' => 'fname',  'dt' => 'fname' ),
  1649.             array( 'db' => 'sname',  'dt' => 'sname' ),
  1650.             array( 'db' => 'image',  'dt' => 'image' ),
  1651.             array( 'db' => 'idInst',  'dt' => 'idInst' ),
  1652.             array( 'db' => 'instName',  'dt' => 'instName' ),
  1653.             array( 'db' => 'fullname',  'dt' => 'name' ),
  1654.             array( 'db' => 'status',  'dt' => 'status' ),
  1655.             array( 'db' => 'deceased',  'dt' => 'deceased' ),
  1656.             array( 'db' => 'retired',  'dt' => 'retired' ),
  1657.             array( 'db' => 'doNotInvite',  'dt' => 'doNotInvite' ),
  1658.             array( 'db' => 'role',  'dt' => 'groupRole' ),
  1659.             array( 'db' => 'country',  'dt' => 'country' )
  1660.         );
  1661.     }
  1662.     /**
  1663.      * give back the primary key we need for the datatables
  1664.      * this is done by a get method as this is used by the API also
  1665.      *
  1666.      * @return string
  1667.      */
  1668.     public static function getPrimaryKey(): string
  1669.     {
  1670.         return 'idInd';
  1671.     }
  1672. }