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