src/OceanExpertBundle/Controller/GroupController.php line 247

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.             /*
  642.             $allExpertData = $this->getDoctrine()
  643.                 ->getRepository('OceanExpertBundle:Indiv')
  644.                 ->createQueryBuilder('i')
  645.                 ->select('i.idInd, i.fname, i.mname, i.sname, i.email1, i.email2, mg.idGroup, c.country')
  646.                 ->leftJoin('OceanExpertBundle:MemberGroups', 'mg', 'WITH', 'mg.idInd = i.idInd')
  647.                 ->leftJoin('OceanExpertBundle:Countries', 'c', 'WITH', 'c.idCountry = i.idNationality')
  648.                 ->where('i.status = 1')
  649.                 ->andwhere('i.retired = 0')
  650.                 ->andwhere('i.deceased = 0')
  651.                 ->andWhere('mg.idGroup = :groupId')
  652.                 ->setParameter('groupId', $groupId)
  653.                 ->getQuery()
  654.                 ->getResult();
  655.             */
  656.             //get the data of the members of the group
  657.             //use the SSP class to get the data, so we have the same data as in the datatable
  658.             //we need this data
  659.             $columns = array(
  660.                 array('db' => 'email1''dt' => 0),
  661.                 array('db' => 'email2''dt' => 1),
  662.                 array('db' => 'fname''dt' => 2),
  663.                 array('db' => 'mname''dt' => 3),
  664.                 array('db' => 'sname''dt' => 4),
  665.                 array('db' => 'country''dt' => 5)
  666.             );
  667.             $ssp = new SSP();
  668.             $allExpertData $ssp->simple(
  669.                 array(
  670.                     'group' => $groupId
  671.                 ),
  672.                 $this->getDoctrine()->getManager(),
  673.                 'email1',
  674.                 $columns
  675.             );
  676.             $allExpertData $allExpertData['data'];
  677.             //get the data in the correct format
  678.             //if there is a second address, use that one
  679.             foreach ($allExpertData as $expertData) {
  680.                 if (isset($expertData[1])
  681.                     && $expertData[1] != ''
  682.                 ) {
  683.                     $email $expertData[1];
  684.                 } else {
  685.                     $email $expertData[0];
  686.                 }
  687.                 $addresses[$email] = array(
  688.                     'email' => $email,
  689.                     'fname' => $expertData[2],
  690.                     'mname' => $expertData[3],
  691.                     'sname' => $expertData[4],
  692.                     'country' => htmlspecialchars_decode($expertData[5], ENT_QUOTES)
  693.                 );
  694.             }
  695.         }
  696.         //create the csv file
  697.         $response $this->render(
  698.             'Group/showAddresses.html.twig',
  699.             array(
  700.                 'data' => array(
  701.                     'addresses' => $addresses
  702.                 )
  703.             )
  704.         );
  705.         $response->headers->set(
  706.             'Content-Type',
  707.             'text/csv'
  708.         );
  709.         $response->headers->set(
  710.             'Content-Disposition',
  711.             'attachment; filename="OceanExpertGroup' $groupId 'Addresses_' date('Ymd') . '.csv"');
  712.         return $response;
  713.     }
  714.     /**
  715.      * @param Request $request
  716.      * @param int $id
  717.      * @param int $groupId
  718.      * @param int $i
  719.      *
  720.      * @return array
  721.      */
  722.     function getExpertDetailsById(Request $request$id$groupId null$i 1): array
  723.     {
  724.         $member $this->getDoctrine()
  725.             ->getRepository('OceanExpertBundle:Indiv')
  726.             ->createQueryBuilder('i')
  727.             ->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')
  728.             ->leftJoin('OceanExpertBundle:IndivInstitution''ii''WITH''ii.idInd = i.idInd')
  729.             ->leftJoin('OceanExpertBundle:Institutions''ins''WITH''ii.idInst = ins.idInst')
  730.             ->leftJoin('OceanExpertBundle:Countries''c''WITH''i.countryCode = c.idCountry')
  731.             ->leftJoin('OceanExpertBundle:Countries''ci''WITH''ins.countryCode = ci.idCountry')
  732.             ->where('i.idInd = :id')
  733.             ->andWhere('i.status = 1')
  734.             ->setParameter('id',$id)
  735.             ->getQuery()
  736.             ->getResult();
  737.         $grouprole'';
  738.         $groupOrder'';
  739.         $primaryGroup'';
  740.         if ($groupId) {
  741.             $memberGroup $this->getDoctrine()
  742.                 ->getRepository('OceanExpertBundle:MemberGroups')
  743.                 ->findOneBy(
  744.                     array(
  745.                         'idGroup' => $groupId,
  746.                         'idInd' => $id
  747.                     )
  748.                 );
  749.             $groupOrder $i;
  750.             if ($memberGroup) {
  751.                 $primaryGroup $groupId;
  752.                 $grouprole $memberGroup->getRole();
  753.                 $leader $memberGroup->getIsLeader();
  754.                 $groupOrder $i;
  755.                 if ( $memberGroup->getMemberOrder() != null) {
  756.                     $groupOrder $memberGroup->getMemberOrder();
  757.                 }
  758.             } else {
  759.                 $primaryGroup '';
  760.                 $grouprole '';
  761.                 $leader '';
  762.             }
  763.         }
  764.         $memberdata = array();
  765.         if ($member) {
  766.             $memberdata['idInd'] = $member[0]['idInd'];
  767.             $memberdata['fname'] = $member[0]['fname'];
  768.             $memberdata['sname'] = $member[0]['sname'];
  769.             if (file_exists('uploads/profile/profile_' $member[0]['idInd'].'.png')) {
  770.                 $memberdata['image'] = $request->getBasePath() . '/uploads/profile/profile_' $member[0]['idInd'].'.png';
  771.             } else {
  772.                 $memberdata['image'] =  $request->getBasePath() . '/assets/uploads/default.png';
  773.             }
  774.             $memberdata['name'] = $member[0]['fname'].' '.$member[0]['sname'];
  775.             $memberdata['jobtitle'] = $member[0]['jobtitle'];
  776.             $memberdata['idInst'] = $member[0]['idInst'];
  777.             $memberdata['instName'] = $member[0]['instName'];
  778.             $memberdata['instNameEng'] = $member[0]['instNameEng'];
  779.             $memberdata['useInstAddr'] = $member[0]['useInstAddr'];
  780.             if ($member[0]['useInstAddr'] === 1) {
  781.                 $memberdata['country'] = $member[0]['insCountry'];
  782.                 $memberdata['countryCode'] = $member[0]['insCountryCode'];
  783.             } else {
  784.                 $memberdata['country'] = $member[0]['country'];
  785.                 $memberdata['countryCode'] = $member[0]['countryCode'];
  786.             }
  787.             $memberdata['insCountry'] = $member[0]['insCountry'];
  788.             $memberdata['groupRole'] = $grouprole;
  789.             $memberdata['groupOrder'] = $groupOrder;
  790.             $memberdata['primaryGroup'] = $primaryGroup;
  791.             $memberdata['leader'] = isset($leader)?$leader:'';
  792.         }
  793.         return $memberdata;
  794.     }
  795.     /**
  796.      * @param         $id
  797.      * @param Request $request
  798.      *
  799.      * @return Response
  800.      */
  801.     public function getGroupDataAction($idRequest $request)
  802.     {
  803.         $isGroupAdmin $this->hasEditPermission($id);
  804.         $file $request->getBasePath() . 'groups/' $id '.json';
  805.         $members file_get_contents($file);
  806.         $memberData json_decode($memberstrue);
  807.         if ($request->request->get('action') == 'remove') {
  808.             foreach ($request->request->get('data') as $index => $item) {
  809.                 $this->removeMemberFromGroup($item['idInd'], $id$request);
  810.             }
  811.         }
  812.         if ($request->request->get('action') == 'edit') {
  813.             foreach ($request->request->get('data') as $index => $item) {
  814.                 $this->editMemberGroupRole($item['idInd'], $id$item['groupRole'], $request);
  815.             }
  816.         }
  817.         $i 1;
  818.         foreach ($memberData['data'] as $key => $member) {
  819.             if ($member['groupOrder'] == '') {
  820.                 $member['groupOrder'] = $i;
  821.                 $i++;
  822.             }
  823.         }
  824.         return new JsonResponse($members);
  825.     }
  826.     /**
  827.      * @param int     $groupId id of the group
  828.      * @param Request $request
  829.      *
  830.      * @todo do we still need this???
  831.      *
  832.      * @return Response
  833.      */
  834.     public function rebuildGroupsAction(int $groupIdRequest $request): Response
  835.     {
  836.         set_time_limit(0);
  837.         if ($this->container->has('profiler'))
  838.         {
  839.             $this->container->get('profiler')->disable();
  840.         }
  841.         $em $this->getDoctrine()->getManager();
  842.         $query $em->createQuery(
  843.             'SELECT gs.idGroup 
  844.             FROM OceanExpertBundle\Entity\Groups gs 
  845.             WHERE gs.idGroup > 30 ');
  846.         $result $query->getResult();
  847.         if (count($result) == 0) {
  848.             echo 'Finished';
  849.             exit();
  850.         }
  851.         $ids array_map('current'$result);
  852.         if ($request->query->get('count')) {
  853.             $count $request->query->get('count');
  854.         } else {
  855.             $count 0;
  856.         }
  857.         if (!isset($ids[$count])) {
  858.             echo 'Array finished';
  859.             exit();
  860.         }
  861.         $groupId $ids[$count];
  862.         $allGroups self::allGroups($this->getDoctrine());
  863.         $doctrine $this->getDoctrine();
  864.         $em $doctrine->getManager();
  865.         if ($groupId == 99999) {
  866.             $groups self::descendantsOf($groupId,$allGroups);
  867.         } else {
  868.             $groups = array(
  869.                 array(
  870.                     'idGroup' => $groupId
  871.                 )
  872.             );
  873.         }
  874.         $subgroups self::descendantsOf($groups[0]['idGroup'],$allGroups);
  875.         $subgroups[] = $this->getGroupArrayById($groupId$doctrine);
  876.         $i 0;
  877.         $allmembers = array();
  878.         foreach ($subgroups as $group) {
  879.             $members $this->getGroupExpertsById($group['idGroup']);
  880.             if ($members) {
  881.                 foreach ($members as $member ) {
  882.                     if ($groupId == $member['idGroup'] ) {
  883.                         $allmembers[$i]['id'] = '';
  884.                         $allmembers[$i]['idInd'] = $member['idInd'];
  885.                         $allmembers[$i]['idGroup'] = $groupId;
  886.                         $allmembers[$i]['mainGroup'] = $group['idGroup'];
  887.                         $allmembers[$i]['isLeader'] = $member['isLeader'];
  888.                         $allmembers[$i]['role'] = $member['role'];
  889.                         $allmembers[$i]['lname'] = '';
  890.                     } else {
  891.                         $allmembers[$i]['id'] = '';
  892.                         $allmembers[$i]['idInd'] = $member['idInd'];
  893.                         $allmembers[$i]['idGroup'] = $groupId;
  894.                         $allmembers[$i]['mainGroup'] = $group['idGroup'];
  895.                         $allmembers[$i]['isLeader'] = '';
  896.                         $allmembers[$i]['role'] = '';
  897.                         $allmembers[$i]['lname'] = '';
  898.                     }
  899.                     $i++;
  900.                 }
  901.             }
  902.         }
  903.         $allmembers $this->array_msort(
  904.             $allmembers,
  905.             array(
  906.                 'role' => SORT_DESC,
  907.                 'isLeader' => SORT_DESC
  908.             )
  909.         );
  910.         $uniquemembers $this->unique_multidim_array(
  911.             $allmembers,
  912.             'idInd'
  913.         );
  914.         $file 'groups/' $groupId '.csv';
  915.         $fp fopen($file'w');
  916.         foreach ($uniquemembers as $member) {
  917.             fputcsv($fp$member);
  918.         }
  919.         fclose($fp);
  920.         /*
  921.         * Upload CSV Files in database
  922.         */
  923.         $dbhost $this->container->getParameter('database_host');
  924.         $dbuser $this->container->getParameter('database_user');
  925.         $dbpass $this->container->getParameter('database_password');
  926.         $connParams $em->getConnection()->getParams();
  927.         //@todo what the hell is this doing here???? remove this Arno 06/04/2022
  928.         /*
  929.         $pdoConn = new \PDO(
  930.             'mysql:host=' . $dbhost . ';dbname=' . $connParams['dbname'],
  931.             $dbuser,
  932.             $dbpass,
  933.             array(
  934.                 \PDO::MYSQL_ATTR_LOCAL_INFILE => true
  935.             )
  936.         );
  937.         $sql = "DELETE FROM group_members_ids
  938.             WHERE id_group = '$groupId'";
  939.         $stmt = $pdoConn->prepare($sql);
  940.         $stmt->execute();
  941.         $sql = "SET FOREIGN_KEY_CHECKS=0";
  942.         $stmt = $pdoConn->prepare($sql);
  943.         $stmt->execute();
  944.         $sql = "LOAD DATA LOCAL INFILE '$file' IGNORE
  945.             INTO TABLE group_members_ids
  946.             FIELDS TERMINATED BY ','
  947.             ENCLOSED BY '\"'
  948.             ESCAPED BY '\"'";
  949.         $stmt = $pdoConn->prepare($sql);
  950.         $stmt->execute();
  951.         */
  952.         unlink($file);
  953.         $count ++;
  954.         return $this->render(
  955.             'Group/rebuildGroup.html.twig',
  956.             array(
  957.                 'count' => $count
  958.             )
  959.         );
  960.     }
  961.     /**
  962.      * @todo do we still need this????
  963.      *       if so remove code for group_members table!!!
  964.      *
  965.      * @param Request $request
  966.      *
  967.      * @return Response
  968.      */
  969.     public function updateMemberIdsAction(Request $request): Response
  970.     {
  971.         if ($request->query->get('count')) {
  972.             $count $request->query->get('count');
  973.         } else {
  974.             $count 0;
  975.         }
  976.         $message = array();
  977.         $em $this->getDoctrine()->getManager();
  978.         $limit =100;
  979.         $offset $count $limit;
  980.         $members $em->createQueryBuilder()
  981.             ->select('i.idInd')
  982.             ->from('OceanExpertBundle:GroupMembersIds''i')
  983.             ->where('i.idInd is NOT NULL')
  984.             ->andWhere('i.lname is NOT NULL')
  985.             ->setMaxResults($limit)
  986.             ->setFirstResult($offset)
  987.             ->getQuery()->getResult();
  988.         if (count($members) ==0) {
  989.             echo 'All data has been populated.';
  990.             exit();
  991.         }
  992.         foreach ($members as $member) {
  993.             $memberdetail $this->getDoctrine()
  994.                 ->getRepository('OceanExpertBundle:Indiv')
  995.                 ->findOneByIdInd($member['idInd']);
  996.             if ($memberdetail) {
  997.                 $memberId $this->getDoctrine()
  998.                     ->getRepository('OceanExpertBundle:GroupMembersIds')
  999.                     ->findByIdInd($memberdetail->getIdInd());
  1000.                 if ($memberId) {
  1001.                     foreach ($memberId as $item) {
  1002.                         $item->setLname($memberdetail->getSname());
  1003.                         $em->persist($item);
  1004.                         $em->flush();
  1005.                     }
  1006.                 }
  1007.             }
  1008.         }
  1009.         $count $count+1;
  1010.         return $this->render(
  1011.             'OceanExpertBundle:Group:rebuild.group.htm.twig',
  1012.             array(
  1013.                 'count' => $count
  1014.             )
  1015.         );
  1016.     }
  1017.     /**
  1018.      * @todo this should be deleted, uses the wrong tables Arno 06/04/2022
  1019.      *
  1020.      * @param Request $request
  1021.      *
  1022.      * @return Response
  1023.      */
  1024.     public function updateGroupMembersAction(Request $request): Response
  1025.     {
  1026.         $message = array();
  1027.         $smember $this->getDoctrine()
  1028.             ->getRepository('OceanExpertBundle:GroupMembersIds')
  1029.             ->findOneBy(
  1030.                 array(),
  1031.                 array(
  1032.                     'isLeader' => 'DESC',
  1033.                     'role' => 'DESC',
  1034.                     'lname' => 'ASC'
  1035.                 )
  1036.             );
  1037.         if (!$smember) {
  1038.             echo 'All data has been populated.';
  1039.             exit();
  1040.         }
  1041.         $members $this->getDoctrine()
  1042.             ->getRepository('OceanExpertBundle:GroupMembersIds')
  1043.             ->findBy(
  1044.                 array(
  1045.                     'idGroup' => $smember->getIdGroup()
  1046.                 ),
  1047.                 array(
  1048.                     'isLeader' => 'DESC',
  1049.                     'role' => 'DESC',
  1050.                     'lname' => 'ASC'
  1051.                 ),
  1052.                 100
  1053.             );
  1054.         $em $this->getDoctrine()->getManager();
  1055.         foreach ($members as $member) {
  1056.             $memberDetails $this->getExpertDetailsById(
  1057.                 $request,
  1058.                 $member->getIdInd(),
  1059.                 $member->getIdGroup()
  1060.             );
  1061.             $addMember $this->getDoctrine()
  1062.                 ->getRepository('OceanExpertBundle:GroupMembers')
  1063.                 ->findBy(
  1064.                     array(
  1065.                         'idInd' => $member->getIdInd(),
  1066.                         'idGroup' => $member->getIdGroup()
  1067.                     )
  1068.                 );
  1069.             $em->remove($member);
  1070.             $em->flush();
  1071.         }
  1072.         return $this->render(
  1073.             'OceanExpertBundle:Group:rebuild.group.htm.twig'
  1074.         );
  1075.     }
  1076.     /**
  1077.      * @param int $id      id of the expert to be removed
  1078.      * @param int $groupId id of the group from where the expert should be removed
  1079.      *
  1080.      * @return void
  1081.      */
  1082.     function removeMemberFromGroup(int $idint $groupIdRequest $request)
  1083.     {
  1084.         $em $this->getDoctrine()->getManager();
  1085.         $member $this->getDoctrine()
  1086.             ->getRepository('OceanExpertBundle:MemberGroups')
  1087.             ->findOneBy(
  1088.                 array(
  1089.                     'idInd' => $id,
  1090.                     'idGroup' => $groupId
  1091.                 )
  1092.             );
  1093.         $em->remove($member);
  1094.         $em->flush();
  1095.         $this->rebuildGroupsAction($groupId$request);
  1096.     }
  1097.     /**
  1098.      * @param int     $id
  1099.      * @param int     $groupId
  1100.      * @param string  $role
  1101.      * @param Request $request
  1102.      *
  1103.      * @return void
  1104.      */
  1105.     function editMemberGroupRole(int $idint $groupIdstring $roleRequest $request)
  1106.     {
  1107.         $em $this->getDoctrine()->getManager();
  1108.         $member $this->getDoctrine()
  1109.             ->getRepository('OceanExpertBundle:MemberGroups')
  1110.             ->findOneBy(
  1111.                 array(
  1112.                     'idInd' => $id,
  1113.                     'idGroup' => $groupId
  1114.                 )
  1115.             );
  1116.         $member->setRole($role);
  1117.         $em->persist($member);
  1118.         $em->flush();
  1119.         $this->rebuildGroupsAction($groupId$request);
  1120.     }
  1121.     /**
  1122.      * membership to a group gives some people more power
  1123.      * @todo : this is not a maintainable way of managing roles!!!!!!
  1124.      *
  1125.      * @param int $groupId id of the group to check
  1126.      *
  1127.      * @return bool
  1128.      */
  1129.     function hasEditPermission(int $groupId): bool
  1130.     {
  1131.         $loggedUser $this->get('security.token_storage')->getToken()->getUser();
  1132.         $allAdminGroups = array(
  1133.             //31 => array(161),
  1134.             //315 => array(57),
  1135.             //47 => array(57),
  1136.             //138 => array(249),
  1137.             //127 => array(34,35,307),
  1138.             //32 => array(34,35,307),
  1139.             //46 => array(57),
  1140.             //269 => array(57),
  1141.             //291 => array(292),
  1142.             //78 => array(79),
  1143.             //169 => array(239),
  1144.             //234 => array(57),
  1145.             //236 => array(247),
  1146.             537 => array(564//members of 'OBON Group Admins' have edit permission for the 'OBON' group
  1147.         );
  1148.         if (array_key_exists($groupId$allAdminGroups)) {
  1149.             $adminGroup $allAdminGroups[$groupId];
  1150.             $userGroups $this->getDoctrine()
  1151.                 ->getRepository('OceanExpertBundle:MemberGroups')
  1152.                 ->findBy(
  1153.                     array(
  1154.                         'idInd' => $loggedUser->getId()
  1155.                     )
  1156.                 );
  1157.             foreach ($userGroups as $groups) {
  1158.                 if (in_array($groups->getIdGroup(), $adminGroup)) {
  1159.                     return true;
  1160.                 }
  1161.             }
  1162.         }
  1163.         return false;
  1164.     }
  1165.     /**
  1166.      * @return JsonResponse
  1167.      */
  1168.     function getAllGroupJsonAction(): JsonResponse
  1169.     {
  1170.         $groups $this->getDoctrine()
  1171.             ->getRepository('OceanExpertBundle:Groups')
  1172.             ->createQueryBuilder('e')
  1173.             ->select('e.idGroup, e.groupname, e.idParentgroup, e.groupOrder')
  1174.             ->addOrderBy('e.groupname''ASC')
  1175.             ->getQuery()
  1176.             ->getResult(AbstractQuery::HYDRATE_ARRAY);
  1177.         return new JsonResponse($groups);
  1178.     }
  1179.     /**
  1180.      * @param Request $request
  1181.      * @param int $id id of the expert to be added to the group, defaults to 0
  1182.      * @param int $groupId id of the group where we want to add the expert to, defaults to 0
  1183.      *
  1184.      * @return JsonResponse
  1185.      */
  1186.     function addGroupMemberAction(Request $request$id 0$groupId 0): JsonResponse
  1187.     {
  1188.         if (!$id
  1189.             && !$groupId
  1190.         ) {
  1191.             $groupId $request->request->get('groupid');
  1192.             $id $request->request->get('userid');
  1193.             $groupRole $request->request->get('grouprole');
  1194.         } else {
  1195.             $groupRole '';
  1196.         }
  1197.         $em $this->getDoctrine()->getManager();
  1198.         $member $this->getDoctrine()
  1199.             ->getRepository('OceanExpertBundle:MemberGroups')
  1200.             ->findOneBy(
  1201.                 array(
  1202.                     'idInd' => $id,
  1203.                     'idGroup' => $groupId
  1204.                 )
  1205.             );
  1206.         if (!$member) {
  1207.             $member = new MemberGroups();
  1208.             $member->setIdInd($id);
  1209.             $member->setIdGroup($groupId);
  1210.             $member->setRole($groupRole);
  1211.             $member->setMemberOrder(0);
  1212.             $member->setIsLeader(0);
  1213.             $em->persist($member);
  1214.             $em->flush();
  1215.         }
  1216.         //check if we are in all the parent groups also
  1217.         //if not add this expert to the parent groups
  1218.         //@todo is that a good idea???
  1219.         /*
  1220.         foreach ($this->getAncestors($groupId) as $childGroup) {
  1221.             $memberGroup = $this->getDoctrine()
  1222.                 ->getRepository('OceanExpertBundle:MemberGroups')
  1223.                 ->findOneBy(
  1224.                     array(
  1225.                         'idInd'=>$id,
  1226.                         'idGroup' => $childGroup['idGroup']
  1227.                     )
  1228.                 );
  1229.             if (!$memberGroup) {
  1230.                 $this->addGroupMemberAction($id, $childGroup['idGroup'], $request);
  1231.             }
  1232.             /*
  1233.             if (!$memberGroup) {
  1234.                 $maxorder = $em->createQueryBuilder()
  1235.                     ->select('MAX(e.groupOrder)')
  1236.                     ->from('OceanExpertBundle:GroupMembers', 'e')
  1237.                     ->where('e.idGroup = :groupid')
  1238.                     ->setParameter('groupid',$childGroup['idGroup'])
  1239.                     ->getQuery()
  1240.                     ->getSingleScalarResult();
  1241.                 $order = $maxorder+1;
  1242.                 $memberdata = $this->getExpertDetailsById($id);
  1243.                 $memberGroup = new GroupMembers();
  1244.                 $memberGroup->setIdInd($id);
  1245.                 $memberGroup->setIdGroup($childGroup['idGroup']);
  1246.                 $memberGroup->setFname($memberdata['fname']);
  1247.                 $memberGroup->setLname($memberdata['sname']);
  1248.                 $memberGroup->setImage($memberdata['image']);
  1249.                 if ($childGroup['idGroup']==$groupId) {
  1250.                     $memberGroup->setGroupRole($groupRole);
  1251.                 } else {
  1252.                     $memberGroup->setGroupRole('');
  1253.                 }
  1254.                 $memberGroup->setIdInst($memberdata['idInst']);
  1255.                 $memberGroup->setInstName($memberdata['instName']);
  1256.                 $memberGroup->setInstNameEng($memberdata['instNameEng']);
  1257.                 $memberGroup->setUseInstAddr($memberdata['useInstAddr']);
  1258.                 $memberGroup->setInsCountry($memberdata['insCountry']);
  1259.                 $memberGroup->setCountry($memberdata['country']);
  1260.                 $memberGroup->setGroupOrder($order);
  1261.                 $memberGroup->setMainGroup($groupId);
  1262.                 $em->persist($memberGroup);
  1263.                 $em->flush();
  1264.             }
  1265.         }
  1266.         //end inner comment
  1267. */
  1268.         return new JsonResponse(
  1269.             array(
  1270.                 'status' => 1,
  1271.                 'message' => 'Member added to group'
  1272.             )
  1273.         );
  1274.     }
  1275.     /**
  1276.      * @todo remove code for group_members
  1277.      *
  1278.      * @param Request $request
  1279.      * @param array $data
  1280.      * @param int $groupId id of the group where to add the experts, defaults to 0
  1281.      *
  1282.      * @return JsonResponse
  1283.      */
  1284.     function addBulkGroupMemberAction(Request $request$data = array(), $groupId 0): JsonResponse
  1285.     {
  1286.         $groupId $request->request->get('groupid');
  1287.         $memberdata $request->request->get('memberdata');
  1288.         $memberdataArr json_decode($memberdata);
  1289.         foreach ($memberdataArr as $addmemberdata) {
  1290.             $em $this->getDoctrine()->getManager();
  1291.             $member $this->getDoctrine()
  1292.                 ->getRepository('OceanExpertBundle:MemberGroups')
  1293.                 ->findOneBy(
  1294.                     array(
  1295.                         'idInd' => $addmemberdata->id,
  1296.                         'idGroup' => $groupId
  1297.                     )
  1298.                 );
  1299.             if (!$member) {
  1300.                 $member = new MemberGroups();
  1301.                 $member->setIdInd($addmemberdata->id);
  1302.                 $member->setIdGroup($groupId);
  1303.                 $member->setRole($addmemberdata->role);
  1304.                 $member->setMemberOrder(0);
  1305.                 $member->setIsLeader(0);
  1306.                 $em->persist($member);
  1307.                 $em->flush();
  1308.             }
  1309.             /*
  1310.             foreach ($this->getAncestors($groupId) as $childGroup) {
  1311.                 $groupMember = $this->getDoctrine()
  1312.                     ->getRepository('OceanExpertBundle:GroupMembers')
  1313.                     ->findOneBy(
  1314.                         array(
  1315.                             'idInd' => $addmemberdata->id,
  1316.                             'idGroup' => $childGroup['idGroup']
  1317.                         )
  1318.                     );
  1319.                 if (!$groupMember) {
  1320.                     $maxorder = $em->createQueryBuilder()
  1321.                         ->select('MAX(e.groupOrder)')
  1322.                         ->from('OceanExpertBundle:GroupMembers', 'e')
  1323.                         ->where('e.idGroup = :groupid')
  1324.                         ->setParameter('groupid',$childGroup['idGroup'])
  1325.                         ->getQuery()
  1326.                         ->getSingleScalarResult();
  1327.                     $order = $maxorder+1;
  1328.                     $memberdata = $this->getExpertDetailsById($addmemberdata->id);
  1329.                     $groupMember = new GroupMembers();
  1330.                     $groupMember->setIdInd($addmemberdata->id);
  1331.                     $groupMember->setIdGroup($childGroup['idGroup']);
  1332.                     $groupMember->setFname($memberdata['fname']);
  1333.                     $groupMember->setLname($memberdata['sname']);
  1334.                     $groupMember->setImage($memberdata['image']);
  1335.                     if ($childGroup['idGroup']==$groupId) {
  1336.                         $groupMember->setGroupRole($addmemberdata->role);
  1337.                     } else {
  1338.                         $groupMember->setGroupRole('');
  1339.                     }
  1340.                     $groupMember->setIdInst($memberdata['idInst']);
  1341.                     $groupMember->setInstName($memberdata['instName']);
  1342.                     $groupMember->setInstNameEng($memberdata['instNameEng']);
  1343.                     $groupMember->setUseInstAddr($memberdata['useInstAddr']);
  1344.                     $groupMember->setInsCountry($memberdata['insCountry']);
  1345.                     $groupMember->setCountry($memberdata['country']);
  1346.                     $groupMember->setGroupOrder($order);
  1347.                     $groupMember->setMainGroup($groupId);
  1348.                     $em->persist($groupMember);
  1349.                     $em->flush();
  1350.                 }
  1351.             }
  1352.             */
  1353.         }
  1354.         return new JsonResponse(
  1355.             array(
  1356.                 'status' => 1,
  1357.                 'message' => 'Member added to group'
  1358.             )
  1359.         );
  1360.     }
  1361.     /**
  1362.      * @return Response
  1363.      */
  1364.     public function setGroupRoleAction(Request $request)
  1365.     {
  1366.          $memberID $request->request->get('memberid');
  1367.          $groupID $request->request->get('groupid');
  1368.          $role $request->request->get('role');
  1369.          $member $this->getDoctrine()
  1370.              ->getRepository('OceanExpertBundle:MemberGroups')
  1371.              ->findOneBy(
  1372.                  array(
  1373.                      'idInd' => $memberID,
  1374.                      'idGroup' => $groupID
  1375.                  )
  1376.              );
  1377.          if ($member) {
  1378.              $em $this->getDoctrine()->getManager();
  1379.              $member->setRole($role);
  1380.              $em->persist($member);
  1381.              $em->flush();
  1382.          } else {
  1383.              return new JsonResponse(
  1384.                  array(
  1385.                      'status' => 0,
  1386.                      'message' => "Expert '$memberID' not found in group '$groupID'"
  1387.                  )
  1388.              );
  1389.          }
  1390.          return new JsonResponse(
  1391.              array(
  1392.                  'status' => 1,
  1393.                  'message' => 'Role updated successfully.'
  1394.              )
  1395.          );
  1396.      }
  1397.     /**
  1398.      * @return Response
  1399.      */
  1400.      public function deleteGroupMemberAction(Request $request)
  1401.      {
  1402.          $memberids $request->request->get('memberids');
  1403.          $groupId $request->request->get('groupid');
  1404.          foreach ($memberids as $idInd) {
  1405.              $member $this->getDoctrine()
  1406.                  ->getRepository('OceanExpertBundle:MemberGroups')
  1407.                  ->findOneBy(
  1408.                      array(
  1409.                          'idInd' => $idInd,
  1410.                          'idGroup' => $groupId
  1411.                      )
  1412.                  );
  1413.              if ($member) {
  1414.                  $em $this->getDoctrine()->getManager();
  1415.                  $em->remove($member);
  1416.                  $em->flush();
  1417.              }
  1418.          }
  1419.          return new JsonResponse(
  1420.              array(
  1421.                  'status' => 1,
  1422.                  'message' => 'Members deleted successfully.'
  1423.              )
  1424.          );
  1425.      }
  1426.     /**
  1427.      * add expert to group(s)
  1428.      *
  1429.      * @param Request $request
  1430.      *
  1431.      * @return Response
  1432.      */
  1433.     public function setGroupsAction(Request $request): Response
  1434.     {
  1435.         $id $request->request->get('userId');
  1436.         $groups $request->request->get('groups');
  1437.         $message '';
  1438.         $security_context $this->get('security.authorization_checker');
  1439.         if ($security_context->isGranted('ROLE_GLOBAL_EDITOR')) {
  1440.             $author $this->get('security.token_storage')->getToken()->getUser()->getId();
  1441.             $updateRecord $this->getDoctrine()
  1442.                 ->getRepository('OceanExpertBundle:Indiv')
  1443.                 ->findOneByIdInd($id);
  1444.             if ($updateRecord) {
  1445.                 $updateRecord->setLDateUpd(new \DateTime('now'));
  1446.                 $updateRecord->setLastEditBy($author);
  1447.                 $em $this->getDoctrine()->getManager();
  1448.                 $em->persist($updateRecord); //marks object to be saved in the next transaction.
  1449.                 $em->flush(); //performs all saves and transactions.
  1450.             }
  1451.             if (is_array($groups)) {
  1452.                 $em $this->getDoctrine()->getManager();
  1453.                 $results $em->createQueryBuilder()
  1454.                     ->add('select''m')
  1455.                     ->add('from''OceanExpertBundle:MemberGroups m')
  1456.                     ->where('m.idGroup not in (:groups)')
  1457.                     ->andWhere('m.idInd =:idInd')
  1458.                     ->setParameters(array('groups' => $groups'idInd' => $id))
  1459.                     ->getQuery()->getResult();
  1460.                 foreach ($results as $result) {
  1461.                     $em->remove($result);
  1462.                 }
  1463.                 $em->flush();
  1464.                 foreach ($groups as $value) {
  1465.                     $updateGroup $this->getDoctrine()
  1466.                         ->getRepository('OceanExpertBundle:MemberGroups')
  1467.                         ->find(
  1468.                             array(
  1469.                                 'idInd' => $id,
  1470.                                 'idGroup' => $value
  1471.                             )
  1472.                     );
  1473.                     if (!$updateGroup) {
  1474.                         $this->addGroupMemberAction($request$id$value);
  1475.                     }
  1476.                 }
  1477.             } else {
  1478.                 $updateGroup $this->getDoctrine()
  1479.                     ->getRepository('OceanExpertBundle:MemberGroups')
  1480.                     ->findBy(
  1481.                         array(
  1482.                             'idInd' => $id
  1483.                         )
  1484.                     );
  1485.                 $em $this->getDoctrine()->getEntityManager();
  1486.                 foreach ($updateGroup as $groups) {
  1487.                     $em->remove($groups);
  1488.                 }
  1489.                 $em->flush();
  1490.             }
  1491.         } else {
  1492.             $message json_encode(array(
  1493.                 'status' => 2,
  1494.                 'error' => 'you do not have the correct rights to do this'
  1495.             ));
  1496.         }
  1497.         return new Response($message);
  1498.     }
  1499.     /**
  1500.      * @param $array
  1501.      * @param $cols
  1502.      *
  1503.      * @return array
  1504.      */
  1505.     function array_msort($array$cols): array
  1506.     {
  1507.         $colarr = array();
  1508.         foreach ($cols as $col => $order) {
  1509.             $colarr[$col] = array();
  1510.             foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
  1511.         }
  1512.         $eval 'array_multisort(';
  1513.         foreach ($cols as $col => $order) {
  1514.             $eval .= '$colarr[\''.$col.'\'],'.$order.',';
  1515.         }
  1516.         $eval substr($eval,0,-1).');';
  1517.         eval($eval);
  1518.         $ret = array();
  1519.         foreach ($colarr as $col => $arr) {
  1520.             foreach ($arr as $k => $v) {
  1521.                 $k substr($k,1);
  1522.                 if (!isset($ret[$k])) $ret[$k] = $array[$k];
  1523.                 $ret[$k][$col] = $array[$k][$col];
  1524.             }
  1525.         }
  1526.         return $ret;
  1527.     }
  1528.     /**
  1529.      * @param $array
  1530.      * @param $key
  1531.      *
  1532.      * @return array
  1533.      */
  1534.     function unique_multidim_array($array$key): array
  1535.     {
  1536.         $temp_array = array();
  1537.         $i 0;
  1538.         $key_array = array();
  1539.         foreach($array as $val) {
  1540.             if (!in_array($val[$key], $key_array)) {
  1541.                 $key_array[$i] = $val[$key];
  1542.                 $temp_array[$i] = $val;
  1543.             }
  1544.             $i++;
  1545.         }
  1546.         return $temp_array;
  1547.     }
  1548.     /**
  1549.      * @todo : check what is going on here and if this is used Arno 5/07/22
  1550.      *
  1551.      * @return JsonResponse
  1552.      */
  1553.     public function sendGroupEmailAction(): JsonResponse
  1554.     {
  1555.         $message = array();
  1556.         $process = new Process(array('php ../app/console oceanexpert:create-users'));
  1557.         $process->start();
  1558.         while ($process->isRunning()) {
  1559.             // waiting for process to finish
  1560.         }
  1561.         echo $process->getOutput();
  1562.         return new JsonResponse(
  1563.             array(
  1564.                 'status' => 1,
  1565.                 'message' => $message
  1566.             )
  1567.         );
  1568.     }
  1569.     /**
  1570.      * @param EntityManagerInterface $entityManager
  1571.      *
  1572.      * @return JsonResponse
  1573.      */
  1574.     public function dataTablesServerProcessingAction(
  1575.         $groupId,
  1576.         EntityManagerInterface $entityManager
  1577.     ): JsonResponse {
  1578.         //dump($request);
  1579.         //die();
  1580.         // Table's primary key
  1581.         $primaryKey self::getPrimaryKey();
  1582.         // Array of database columns which should be read and sent back to DataTables.
  1583.         // The `db` parameter represents the column name in the database, while the `dt`
  1584.         // parameter represents the DataTables column identifier. In this case simple
  1585.         // indexes
  1586.         $columns self::getColumns();
  1587.         //check if we got some info from the request itself
  1588.         //pass this to the method
  1589.         //this used to be done by passing $_GET which is not the correct way
  1590.         //not useable for the API calls
  1591.         $request = array();
  1592.         //we are looking for these parameters
  1593.         $params = array(
  1594.             'order',
  1595.             'columns',
  1596.             'search',
  1597.             'group',
  1598.             'start',
  1599.             'length',
  1600.             'draw',
  1601.             '_'
  1602.         );
  1603.         foreach($params as $param) {
  1604.             $request[$param] = '';
  1605.             if (isset($_GET[$param])) {
  1606.                 $request[$param] = $_GET[$param];
  1607.             }
  1608.         }
  1609.         $ssp = new SSP();
  1610.         $data $ssp->simple(
  1611.             $request,
  1612.             $entityManager,
  1613.             $primaryKey,
  1614.             $columns
  1615.         );
  1616.         return new JsonResponse($data);
  1617.     }
  1618.     /**
  1619.      * give back the list of colums we need for the datatables
  1620.      * this is done by a get method as this is used by the API also
  1621.      *
  1622.      * @return array
  1623.      */
  1624.     public static function getColumns(): array
  1625.     {
  1626.         return array(
  1627.             array( 'db' => 'id''dt' => 'id' ),
  1628.             array( 'db' => 'memberOrder''dt' => 'groupOrder' ),
  1629.             array( 'db' => 'idInd',  'dt' => 'DT_RowId' ),
  1630.             array( 'db' => 'idInd',  'dt' => 'idInd' ),
  1631.             array( 'db' => 'fname',  'dt' => 'fname' ),
  1632.             array( 'db' => 'sname',  'dt' => 'sname' ),
  1633.             array( 'db' => 'image',  'dt' => 'image' ),
  1634.             array( 'db' => 'idInst',  'dt' => 'idInst' ),
  1635.             array( 'db' => 'instName',  'dt' => 'instName' ),
  1636.             array( 'db' => 'fullname',  'dt' => 'name' ),
  1637.             array( 'db' => 'status',  'dt' => 'status' ),
  1638.             array( 'db' => 'deceased',  'dt' => 'deceased' ),
  1639.             array( 'db' => 'retired',  'dt' => 'retired' ),
  1640.             array( 'db' => 'doNotInvite',  'dt' => 'doNotInvite' ),
  1641.             array( 'db' => 'role',  'dt' => 'groupRole' ),
  1642.             array( 'db' => 'country',  'dt' => 'country' )
  1643.         );
  1644.     }
  1645.     /**
  1646.      * give back the primary key we need for the datatables
  1647.      * this is done by a get method as this is used by the API also
  1648.      *
  1649.      * @return string
  1650.      */
  1651.     public static function getPrimaryKey(): string
  1652.     {
  1653.         return 'idInd';
  1654.     }
  1655. }