/**
  * @Route(
  *      "/project/{project}/collection/{collection}/{module}/taxo_view_map/{taxon}",
  *      name="front_module_taxo_view_map"
  *  )
  * @Template()
  */
 public function module_taxo_view_mapAction($project, $collection, $module, $taxon)
 {
     ControllerHelp::check_enable_project($project, $this->get_prefix(), $this);
     $translations = ControllerHelp::make_translations($project, 'front_module_taxo', array('project' => $project, 'collection' => $collection, 'module' => $module), $this, $this->container->getParameter('mdb_base'));
     //
     $dm = $this->get('doctrine.odm.mongodb.document_manager');
     $dm->getConfiguration()->setDefaultDB($this->get_prefix() . $project);
     $collection = $dm->getRepository('PlantnetDataBundle:Collection')->findOneBy(array('url' => $collection));
     if (!$collection) {
         throw $this->createNotFoundException('Unable to find Collection entity.');
     }
     $module = $dm->getRepository('PlantnetDataBundle:Module')->findOneBy(array('url' => $module, 'collection.id' => $collection->getId()));
     if (!$module || $module->getType() != 'text' || $module->getWsonly() == true) {
         throw $this->createNotFoundException('Unable to find Module entity.');
     }
     $taxon = $dm->createQueryBuilder('PlantnetDataBundle:Taxon')->field('module')->references($module)->field('identifier')->equals($taxon)->getQuery()->getSingleResult();
     if (!$taxon) {
         throw $this->createNotFoundException('Unable to find Taxon entity.');
     }
     $display = array();
     $field = $module->getProperties();
     foreach ($field as $row) {
         if ($row->getMain() == true) {
             $display[] = $row->getId();
         }
     }
     $tab_ref = array($taxon->getId() => $taxon);
     $syns = $taxon->getSynonyms();
     if (count($syns)) {
         foreach ($syns as $syn) {
             $tab_ref[$syn->getId()] = $syn;
         }
     }
     /*
     $children=$taxon->getChildren();
     while(count($children)){
         $children_new=array();
         foreach($children as $child){
             $tab_ref[$child->getId()]=$child;
             $syns=$child->getSynonyms();
             if(count($syns)){
                 foreach($syns as $syn){
                     $tab_ref[$syn->getId()]=$syn;
                 }
             }
             $tmp_children=$child->getChildren();
             if(count($tmp_children)){
                 foreach($tmp_children as $new_child){
                     $children_new[$new_child->getId()]=$new_child;
                 }
             }
         }
         $children=$children_new;
     }
     */
     $plantunits = $dm->createQueryBuilder('PlantnetDataBundle:Plantunit');
     $plantunits->field('module')->references($module);
     $plantunits->hydrate(false);
     $plantunits->select('_id');
     if (count($tab_ref) > 1) {
         foreach ($tab_ref as $ref) {
             $plantunits->addOr($plantunits->expr()->field('taxonsrefs')->references($ref));
         }
     } else {
         $plantunits->field('taxonsrefs')->references($tab_ref[key($tab_ref)]);
     }
     $plantunits = $plantunits->getQuery()->execute();
     $pu_ids = array();
     foreach ($plantunits as $id) {
         $pu_ids[] = $id['_id'];
     }
     $locations = $dm->createQueryBuilder('PlantnetDataBundle:Location');
     $locations->field('plantunit.$id')->in($pu_ids);
     /*
     $locations=$dm->createQueryBuilder('PlantnetDataBundle:Location');
     if(count($tab_ref)>1){
         foreach($tab_ref as $ref){
             $locations->addOr($locations->expr()->field('taxonsrefs')->references($ref));
         }
     }
     else{
         $locations->field('taxonsrefs')->references($tab_ref[key($tab_ref)]);
     }
     */
     $locations = $locations->getQuery()->execute();
     //count to display
     $nb_images = $taxon->getHasimages() ? 1 : 0;
     $nb_locations = 1;
     $dir = $this->get('kernel')->getBundle('PlantnetDataBundle')->getPath() . '/Resources/config/';
     $layers = new \SimpleXMLElement($dir . 'layers.xml', 0, true);
     $config = ControllerHelp::get_config($project, $dm, $this);
     $tpl = $config->getTemplate();
     return $this->render('PlantnetDataBundle:' . ($tpl ? $tpl : 'Frontend') . '\\Module:taxo_view.html.twig', array('config' => $config, 'project' => $project, 'collection' => $collection, 'module_parent' => $module, 'module' => $module, 'taxon' => $taxon, 'layers' => $layers, 'locations' => $locations, 'nbResults' => count($locations), 'nb_images' => $nb_images, 'nb_locations' => $nb_locations, 'display' => $display, 'translations' => $translations, 'current' => 'collection', 'current_display' => 'locations', 'selected' => 'taxo' . $collection->getId() . $module->getId()));
 }
 /**
  * @ApiDoc(
  *  section="Publish v2 - GeoData. Sub-module entity [type = locality]",
  *  description="Returns geo data from a 'locality' sub-module entity [GeoJson]",
  *  statusCodes={
  *      200="Returned when: Successful",
  *      401="Returned when: Unauthorized client",
  *      404="Returned when: Resource not found"
  *  },
  *  filters={
  *      {"name"="project", "dataType"="String", "required"=true, "description"="Project url"},
  *      {"name"="collection", "dataType"="String", "required"=true, "description"="Collection url"},
  *      {"name"="module", "dataType"="String", "required"=true, "description"="Module url"},
  *      {"name"="submodule", "dataType"="String", "required"=true, "description"="'Location' Sub-Module url"}
  *  }
  * )
  *
  * @Route(
  *      "/{project}/{collection}/{module}/{submodule}/geodata",
  *      name="api_submodule_geodata"
  * )
  * @Method("get")
  */
 public function api_submodule_geodataAction($project, $collection, $module, $submodule)
 {
     ini_set('memory_limit', '-1');
     //check project
     try {
         ControllerHelp::check_enable_project($project, $this->get_prefix(), $this);
     } catch (\Exception $e) {
         $this->return_404_not_found($e->getMessage());
         exit;
     }
     //init
     $dm = $this->get('doctrine.odm.mongodb.document_manager');
     $dm->getConfiguration()->setDefaultDB($this->get_prefix() . $project);
     $result = array();
     //get language config
     $config = ControllerHelp::get_config($project, $dm, $this);
     $this->check_authorized_client($config);
     //data1
     $collection = $dm->getRepository('PlantnetDataBundle:Collection')->findOneBy(array('url' => $collection));
     if (!$collection || $collection->getDeleting() == true) {
         $this->return_404_not_found('Unable to find Collection entity.');
         exit;
     }
     $module = $dm->getRepository('PlantnetDataBundle:Module')->findOneBy(array('url' => $module, 'collection.id' => $collection->getId()));
     if (!$module || $module->getType() != 'text' || $module->getDeleting() == true) {
         $this->return_404_not_found('Unable to find Module entity.');
         exit;
     }
     $submodule = $dm->getRepository('PlantnetDataBundle:Module')->findOneBy(array('url' => $submodule, 'parent.id' => $module->getId(), 'collection.id' => $collection->getId()));
     if (!$submodule || $submodule->getType() == 'text' || $submodule->getDeleting() == true) {
         $this->return_404_not_found('Unable to find Sub-module entity.');
         exit;
     }
     if ($submodule->getType() != 'locality') {
         $this->return_404_not_found('Unable to find geo data for this Sub-module entity.');
         exit;
     }
     //data2
     $display = array();
     $field = $module->getProperties();
     foreach ($field as $row) {
         if ($row->getMain() == true) {
             $display[] = $row->getId();
         }
     }
     $field_sub = $submodule->getProperties();
     $field_sub_tab = array();
     foreach ($field_sub as $f) {
         if ($f->getDetails() == true) {
             $field_sub_tab[$f->getId()] = StringHelp::cleanToKey($f->getName());
         }
     }
     \MongoCursor::$timeout = -1;
     $locations = $dm->createQueryBuilder('PlantnetDataBundle:Location')->hydrate(false)->select('title1')->select('title2')->select('title3')->select('property')->select('latitude')->select('longitude')->select('idparent')->field('module')->references($submodule)->getQuery()->execute()->toArray();
     array_walk($locations, function (&$item, $key, $field_sub_tab) {
         $l = array('type' => 'Feature', 'geometry' => array('type' => 'Point', 'coordinates' => array($item['longitude'], $item['latitude'])), 'properties' => array('title1' => $item['title1'], 'title2' => $item['title2'], 'title3' => $item['title3'], 'parent_identifier' => $item['idparent']));
         foreach ($item['property'] as $key => $value) {
             if (array_key_exists($key, $field_sub_tab)) {
                 $l['properties'][$field_sub_tab[$key]] = $value;
             }
         }
         $item = $l;
     }, $field_sub_tab);
     $locations = array_values($locations);
     //response
     $response = new Response(json_encode(array('type' => 'FeatureCollection', 'features' => $locations)));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
 /**
  * @Route("/project/{project}/search", name="front_search")
  * @Method("get")
  * @Template()
  */
 public function searchAction($project, Request $request)
 {
     ControllerHelp::check_enable_project($project, $this->get_prefix(), $this);
     $translations = ControllerHelp::make_translations($project, $this->container->get('request')->get('_route'), array('project' => $project), $this, $this->container->getParameter('mdb_base'));
     //
     $dm = $this->get('doctrine.odm.mongodb.document_manager');
     $dm->getConfiguration()->setDefaultDB($this->get_prefix() . $project);
     $punits = array();
     $query = '';
     $paginator = null;
     if ($request->isMethod('GET') && $request->query->get('q')) {
         $query = $request->query->get('q');
         $string = new \MongoRegex('/.*' . StringHelp::accentToRegex($query) . '.*/i');
         $collections = $dm->getRepository('PlantnetDataBundle:Collection')->findAll();
         //
         $punit_filters = array('identifier' => 1, 'title1' => 1, 'title2' => 1, 'title3' => 1);
         $img_filters = array();
         $loc_filters = array();
         $other_filters = array();
         //
         /*
         foreach($collections as $collection){
             $modules=$collection->getModules();
             foreach($modules as $module){
                 $fields=$module->getProperties();
                 foreach($fields as $field){
                     if($field->getSearch()==true){
                         $punit_filters['attributes.'.$field->getId()]=1;
                     }
                 }
                 $submodules=$module->getChildren();
                 foreach($submodules as $submodule){
                     switch($submodule->getType()){
                         case 'image':
                             break;
                         case 'locality':
                             break;
                         case 'other':
                             $subfields=$submodule->getProperties();
                             foreach($subfields as $subfield){
                                 if($subfield->getSearch()==true){
                                     $other_filters['property.'.$subfield->getId()]=1;
                                 }
                             }
                             break;
                     }
                 }
             }
         }
         */
         //
         $tmp_ids = array();
         if (count($img_filters)) {
             $ids = $dm->createQueryBuilder('PlantnetDataBundle:Image')->hydrate(false)->select('plantunit');
             foreach ($img_filters as $filter => $active) {
                 $ids->addOr($ids->expr()->field($filter)->in(array($string)));
             }
             $ids = $ids->getQuery()->execute();
             foreach ($ids as $id) {
                 $tmp_ids[] = $id['plantunit']['$id'] . '';
             }
         }
         if (count($loc_filters)) {
             $ids = $dm->createQueryBuilder('PlantnetDataBundle:Location')->hydrate(false)->select('plantunit');
             foreach ($loc_filters as $filter => $active) {
                 $ids->addOr($ids->expr()->field($filter)->in(array($string)));
             }
             $ids = $ids->getQuery()->execute();
             foreach ($ids as $id) {
                 $tmp_ids[] = $id['plantunit']['$id'] . '';
             }
         }
         if (count($other_filters)) {
             $ids = $dm->createQueryBuilder('PlantnetDataBundle:Other')->hydrate(false)->select('plantunit');
             foreach ($other_filters as $filter => $active) {
                 $ids->addOr($ids->expr()->field($filter)->in(array($string)));
             }
             $ids = $ids->getQuery()->execute();
             foreach ($ids as $id) {
                 $tmp_ids[] = $id['plantunit']['$id'] . '';
             }
         }
         $tmp_ids = array_unique($tmp_ids);
         //
         $punits = $dm->createQueryBuilder('PlantnetDataBundle:Plantunit');
         foreach ($punit_filters as $filter => $active) {
             $punits->addOr($punits->expr()->field($filter)->in(array($string)));
         }
         if (count($tmp_ids)) {
             $punits->addOr($punits->expr()->field('_id')->in($tmp_ids));
         }
         $punits = $punits->sort('title1', 'asc')->sort('title2', 'asc')->sort('title3', 'asc');
         $paginator = new Pagerfanta(new DoctrineODMMongoDBAdapter($punits));
         try {
             $paginator->setMaxPerPage(50);
             $paginator->setCurrentPage($this->get('request')->query->get('page', 1));
         } catch (\Pagerfanta\Exception\NotValidCurrentPageException $e) {
             throw $this->createNotFoundException('Page not found.');
         }
         if ($paginator->getNbResults() == 1) {
             foreach ($paginator as $punit) {
                 if ($punit->getModule()->getWsonly() != true) {
                     return $this->redirect($this->generateUrl('front_details', array('project' => $project, 'collection' => $punit->getModule()->getCollection()->getUrl(), 'module' => $punit->getModule()->getUrl(), 'id' => $punit->getIdentifier())));
                 }
             }
         }
     }
     $config = ControllerHelp::get_config($project, $dm, $this);
     $tpl = $config->getTemplate();
     return $this->render('PlantnetDataBundle:' . ($tpl ? $tpl : 'Frontend') . ':search.html.twig', array('config' => $config, 'project' => $project, 'query' => $query, 'paginator' => $paginator, 'translations' => $translations, 'current' => 'contacts'));
 }
 /**
  * @Route(
  *      "/project/{project}/collection/{collection}/{module}/result",
  *      defaults={"mode"="grid"},
  *      name="front_module_result"
  *  )
  * @Route(
  *      "/project/{project}/collection/{collection}/{module}/result/{mode}",
  *      requirements={"mode"="\w+"},
  *      name="front_module_result_mode"
  *  )
  * @Method("get")
  * @Template()
  */
 public function module_resultAction($project, $collection, $module, $mode, Request $request)
 {
     ControllerHelp::check_enable_project($project, $this->get_prefix(), $this);
     $translations = ControllerHelp::make_translations($project, 'front_module_search', array('project' => $project, 'collection' => $collection, 'module' => $module), $this, $this->container->getParameter('mdb_base'));
     //
     // $this->get_config($project);
     $dm = $this->get('doctrine.odm.mongodb.document_manager');
     $dm->getConfiguration()->setDefaultDB($this->get_prefix() . $project);
     ControllerHelp::get_config($project, $dm, $this);
     $configuration = $dm->getConnection()->getConfiguration();
     // $configuration->setLoggerCallable(null);
     $collection = $dm->getRepository('PlantnetDataBundle:Collection')->findOneBy(array('url' => $collection));
     if (!$collection) {
         throw $this->createNotFoundException('Unable to find Collection entity.');
     }
     $module = $dm->getRepository('PlantnetDataBundle:Module')->findOneBy(array('url' => $module, 'collection.id' => $collection->getId()));
     if (!$module || $module->getWsonly() == true) {
         throw $this->createNotFoundException('Unable to find Module entity.');
     }
     if ($module->getParent()) {
         $module = $dm->getRepository('PlantnetDataBundle:Module')->find($module->getParent()->getId());
         if (!$module) {
             throw $this->createNotFoundException('Unable to find Module entity.');
         }
     }
     $fields = array();
     $display = array();
     $field = $module->getProperties();
     foreach ($field as $row) {
         if ($row->getSearch() == true) {
             $fields[] = $row->getId();
         }
         if ($row->getMain() == true) {
             $display[] = $row->getId();
         }
     }
     $sub_fields = array();
     $children = $module->getChildren();
     if (count($children)) {
         foreach ($children as $child) {
             if ($child->getType() == 'other') {
                 $sub_field = $child->getProperties();
                 foreach ($sub_field as $row) {
                     if ($row->getSearch() == true) {
                         $sub_fields[$child->getId() . '#' . $row->getId()] = $child;
                     }
                 }
             }
         }
     }
     $form = $this->createModuleSearchForm($fields, $module, $sub_fields);
     if ($request->isMethod('GET')) {
         $form->bind($request);
         $data = $form->getData();
         $dm = $this->get('doctrine.odm.mongodb.document_manager');
         $dm->getConfiguration()->setDefaultDB($this->get_prefix() . $project);
         // Location Filters
         $ids_punit = array();
         if (isset($data['x_lng_1_bottom_left']) && !empty($data['x_lng_1_bottom_left'])) {
             $locations = $dm->createQueryBuilder('PlantnetDataBundle:Location')->field('coordinates')->withinBox(floatval($data['x_lng_1_bottom_left']), floatval($data['y_lat_1_bottom_left']), floatval($data['x_lng_2_top_right']), floatval($data['y_lat_2_top_right']))->hydrate(false)->getQuery()->execute();
             foreach ($locations as $location) {
                 $ids_punit[] = $location['plantunit']['$id']->{'$id'};
             }
             $locations = null;
             unset($locations);
         }
         $ids_punit = array_unique($ids_punit);
         // Field (and sub-field) Filters
         $fields = array();
         $sub_fields = array();
         foreach ($data as $key => $val) {
             if (substr_count($key, 'name_field_') && $val) {
                 if (isset($data[str_replace('name_', '', $key) . '_string']) && !empty($data[str_replace('name_', '', $key) . '_string'])) {
                     if (substr_count($val, '#') === 0) {
                         $fields[$val] = explode('~|~', $data[str_replace('name_', '', $key) . '_string']);
                     } else {
                         $sub_fields[$val] = explode('~|~', $data[str_replace('name_', '', $key) . '_string']);
                     }
                 } elseif (isset($data[str_replace('name_', '', $key)]) && !empty($data[str_replace('name_', '', $key)])) {
                     if (substr_count($val, '#') === 0) {
                         $fields[$val] = $data[str_replace('name_', '', $key)];
                     } else {
                         $sub_fields[$val] = $data[str_replace('name_', '', $key)];
                     }
                 }
             }
         }
         // Sub-field Filters
         $ids_other = array();
         if (count($sub_fields)) {
             foreach ($sub_fields as $sub_ids => $value) {
                 $sub_module_id = substr($sub_ids, 0, strpos($sub_ids, '#'));
                 $sub_attribute_id = substr($sub_ids, strpos($sub_ids, '#') + 1);
                 $sub_module = $dm->getRepository('PlantnetDataBundle:Module')->findOneBy(array('id' => $sub_module_id, 'parent.id' => $module->getId()));
                 if (!$sub_module) {
                     throw $this->createNotFoundException('Unable to find Module entity.');
                 }
                 $others = $dm->createQueryBuilder('PlantnetDataBundle:Other')->hydrate(false)->field('module')->references($sub_module);
                 if (is_array($value)) {
                     for ($i = 0; $i < count($value); $i++) {
                         $value[$i] = new \MongoRegex('/.*' . StringHelp::accentToRegex($value[$i]) . '.*/i');
                     }
                     $others->field('property.' . $sub_attribute_id)->in($value);
                 } else {
                     $others->field('property.' . $sub_attribute_id)->in(array(new \MongoRegex('/.*' . StringHelp::accentToRegex($value) . '.*/i')));
                 }
                 $others = $others->getQuery()->execute();
                 $tmp_ids_other = array();
                 foreach ($others as $other) {
                     $tmp_ids_other[] = $other['plantunit']['$id']->{'$id'};
                 }
                 $others = null;
                 unset($others);
                 $ids_other = array_merge($ids_other, $tmp_ids_other);
                 $tmp_ids_other = null;
                 unset($tmp_ids_other);
             }
         }
         $ids_other = array_unique($ids_other);
         // $ids_punit x $ids_other
         if (!count($ids_punit)) {
             $ids_punit = $ids_other;
         } elseif (count($ids_other)) {
             $ids_punit = array_intersect($ids_punit, $ids_other);
         }
         // Filters to URL
         $url = '';
         $data_url = $form->getData();
         foreach ($data_url as $key => $val) {
             if ($url != '') {
                 $url .= '&';
             }
             if (substr_count($val, '#') === 0) {
                 $url .= $form->getName() . '[' . $key . ']=' . $val;
             } else {
                 $url .= $form->getName() . '[' . $key . ']=' . urlencode($val);
             }
         }
         // Search
         switch ($mode) {
             case 'grid':
                 $paginator = null;
                 $nbResults = 0;
                 $nb_images = 0;
                 $nb_locations = 0;
                 $sortby = $this->get('request')->query->get('sort', 'null');
                 $sortorder = $this->get('request')->query->get('order', 'null');
                 if (!in_array($sortorder, array('null', 'asc', 'desc'))) {
                     $sortorder = 'null';
                 }
                 if (count($ids_punit) || count($fields)) {
                     $plantunits = $dm->createQueryBuilder('PlantnetDataBundle:Plantunit')->field('module.id')->equals($module->getId());
                     if (count($ids_punit)) {
                         $plantunits->field('_id')->in($ids_punit);
                     }
                     if (count($fields)) {
                         foreach ($fields as $key => $value) {
                             if (is_array($value)) {
                                 $value_query_tab = array();
                                 for ($i = 0; $i < count($value); $i++) {
                                     //check for int or float value
                                     if (is_numeric($value[$i])) {
                                         $tmp_value = intval($value[$i]);
                                         if ($value[$i] == $tmp_value) {
                                             $value_query_tab[] = $tmp_value;
                                         } else {
                                             $tmp_value = floatval($value[$i]);
                                             if ($value[$i] == $tmp_value) {
                                                 $value_query_tab[] = $tmp_value;
                                             }
                                         }
                                     }
                                     //
                                     $value_query_tab[] = new \MongoRegex('/.*' . StringHelp::accentToRegex($value[$i]) . '.*/i');
                                 }
                                 $plantunits->field('attributes.' . $key)->in($value_query_tab);
                             } else {
                                 $value_query_tab = array(new \MongoRegex('/.*' . StringHelp::accentToRegex($value) . '.*/i'));
                                 //check for int or float value
                                 if (is_numeric($value)) {
                                     $tmp_value = intval($value);
                                     if ($value == $tmp_value) {
                                         $value_query_tab[] = $tmp_value;
                                     } else {
                                         $tmp_value = floatval($value);
                                         if ($value == $tmp_value) {
                                             $value_query_tab[] = $tmp_value;
                                         }
                                     }
                                 }
                                 //
                                 $plantunits->field('attributes.' . $key)->in($value_query_tab);
                             }
                         }
                     }
                     $order = array();
                     $field = $module->getProperties();
                     foreach ($field as $row) {
                         if ($row->getSortorder()) {
                             $order[$row->getSortorder()] = $row->getId();
                         }
                     }
                     ksort($order);
                     if ($sortby != 'null' && $sortorder != 'null') {
                         if (in_array($sortby, $order)) {
                             unset($order[array_search($sortby, $order)]);
                         }
                         $plantunits->sort('attributes.' . $sortby, $sortorder);
                     }
                     if (count($order)) {
                         foreach ($order as $num => $prop) {
                             $plantunits->sort('attributes.' . $prop, 'asc');
                         }
                     }
                     $paginator = new Pagerfanta(new DoctrineODMMongoDBAdapter($plantunits));
                     try {
                         $paginator->setMaxPerPage(50);
                         $paginator->setCurrentPage($this->get('request')->query->get('page', 1));
                     } catch (\Pagerfanta\Exception\NotValidCurrentPageException $e) {
                         throw $this->createNotFoundException('Page not found.');
                     }
                     $nbResults = $paginator->getNbResults();
                     //count to display
                     $clone_plantunits = clone $plantunits;
                     $ids_c = $clone_plantunits->hydrate(false)->select('_id')->getQuery()->execute();
                     $ids_tab = array();
                     foreach ($ids_c as $id) {
                         // $ids_tab[$id['_id']->{'$id'}]=$id['_id']->{'$id'};
                         $ids_tab[$id['_id']->{'$id'}] = $id['_id'];
                     }
                     $clone_plantunits = null;
                     unset($clone_plantunits);
                     $ids_c = null;
                     unset($ids_c);
                     /*
                     $nb_images=$dm->createQueryBuilder('PlantnetDataBundle:Image')
                         ->hydrate(false)
                         ->select('_id')
                         ->field('plantunit.id')->in(array_values($ids_tab))
                         ->getQuery()
                         ->execute()
                         ->count();
                     $nb_locations=$dm->createQueryBuilder('PlantnetDataBundle:Location')
                         ->hydrate(false)
                         ->select('_id')
                         ->field('plantunit.id')->in(array_values($ids_tab))
                         ->getQuery()
                         ->execute()
                         ->count();
                     */
                     $connection = new \MongoClient();
                     $db = $this->get_prefix() . $project;
                     $db = $connection->{$db};
                     $nb_images = $db->Image->find(array('plantunit.$id' => array('$in' => array_values($ids_tab))))->count();
                     $nb_locations = $db->Location->find(array('plantunit.$id' => array('$in' => array_values($ids_tab))))->count();
                     $ids_tab = null;
                     unset($ids_tab);
                 }
                 $config = ControllerHelp::get_config($project, $dm, $this);
                 $tpl = $config->getTemplate();
                 return $this->render('PlantnetDataBundle:' . ($tpl ? $tpl : 'Frontend') . '\\Module:module_result.html.twig', array('config' => $config, 'project' => $project, 'collection' => $collection, 'module' => $module, 'paginator' => $paginator, 'display' => $display, 'sortby' => $sortby, 'sortorder' => $sortorder, 'nbResults' => $nbResults, 'nb_images' => $nb_images, 'nb_locations' => $nb_locations, 'url' => $url, 'translations' => $translations, 'current' => 'collection', 'current_display' => 'grid', 'selected' => 'searchform' . $collection->getId() . $module->getId()));
                 break;
             case 'images':
                 $paginator = null;
                 $nbResults = 0;
                 $nb_images = 0;
                 $nb_locations = 0;
                 if (count($ids_punit) || count($fields)) {
                     $plantunits = $dm->createQueryBuilder('PlantnetDataBundle:Plantunit')->hydrate(false)->select('_id')->field('module.id')->equals($module->getId());
                     if (count($ids_punit)) {
                         $plantunits->field('_id')->in(array_values($ids_punit));
                     }
                     if (count($fields)) {
                         foreach ($fields as $key => $value) {
                             if (is_array($value)) {
                                 $value_query_tab = array();
                                 for ($i = 0; $i < count($value); $i++) {
                                     //check for int or float value
                                     if (is_numeric($value[$i])) {
                                         $tmp_value = intval($value[$i]);
                                         if ($value[$i] == $tmp_value) {
                                             $value_query_tab[] = $tmp_value;
                                         } else {
                                             $tmp_value = floatval($value[$i]);
                                             if ($value[$i] == $tmp_value) {
                                                 $value_query_tab[] = $tmp_value;
                                             }
                                         }
                                     }
                                     //
                                     $value_query_tab[] = new \MongoRegex('/.*' . StringHelp::accentToRegex($value[$i]) . '.*/i');
                                 }
                                 $plantunits->field('attributes.' . $key)->in($value_query_tab);
                             } else {
                                 $value_query_tab = array(new \MongoRegex('/.*' . StringHelp::accentToRegex($value) . '.*/i'));
                                 //check for int or float value
                                 if (is_numeric($value)) {
                                     $tmp_value = intval($value);
                                     if ($value == $tmp_value) {
                                         $value_query_tab[] = $tmp_value;
                                     } else {
                                         $tmp_value = floatval($value);
                                         if ($value == $tmp_value) {
                                             $value_query_tab[] = $tmp_value;
                                         }
                                     }
                                 }
                                 //
                                 $plantunits->field('attributes.' . $key)->in($value_query_tab);
                             }
                         }
                     }
                     $ids_c = $plantunits->getQuery()->execute();
                     $ids_tab = array();
                     foreach ($ids_c as $id) {
                         // $ids_tab[$id['_id']->{'$id'}]=$id['_id']->{'$id'};
                         $ids_tab[$id['_id']->{'$id'}] = $id['_id'];
                     }
                     $ids_c = null;
                     unset($ids_c);
                     $images = $dm->createQueryBuilder('PlantnetDataBundle:Image')->field('plantunit.id')->in(array_values($ids_tab))->sort('title1', 'asc')->sort('title2', 'asc');
                     $paginator = new Pagerfanta(new DoctrineODMMongoDBAdapter($images));
                     try {
                         $paginator->setMaxPerPage(15);
                         $paginator->setCurrentPage($this->get('request')->query->get('page', 1));
                     } catch (\Pagerfanta\Exception\NotValidCurrentPageException $e) {
                         throw $this->createNotFoundException('Page not found.');
                     }
                     $nbResults = $paginator->getNbResults();
                     //count to display
                     $nb_images = $nbResults;
                     /*
                     $nb_locations=$dm->createQueryBuilder('PlantnetDataBundle:Location')
                         ->hydrate(false)
                         ->select('_id')
                         ->field('plantunit.id')->in(array_values($ids_tab))
                         ->getQuery()
                         ->execute()
                         ->count();
                     */
                     $connection = new \MongoClient();
                     $db = $this->get_prefix() . $project;
                     $db = $connection->{$db};
                     $nb_locations = $db->Location->find(array('plantunit.$id' => array('$in' => array_values($ids_tab))))->count();
                     $ids_tab = null;
                     unset($ids_tab);
                 }
                 $config = ControllerHelp::get_config($project, $dm, $this);
                 $tpl = $config->getTemplate();
                 return $this->render('PlantnetDataBundle:' . ($tpl ? $tpl : 'Frontend') . '\\Module:module_result.html.twig', array('config' => $config, 'project' => $project, 'collection' => $collection, 'module_parent' => $module, 'module' => $module, 'paginator' => $paginator, 'display' => $display, 'nbResults' => $nbResults, 'nb_images' => $nb_images, 'nb_locations' => $nb_locations, 'url' => $url, 'translations' => $translations, 'current' => 'collection', 'current_display' => 'images', 'selected' => 'searchform' . $collection->getId() . $module->getId()));
                 break;
             case 'locations':
                 $locations = null;
                 $nbResults = 0;
                 if (count($ids_punit) || count($fields)) {
                     $plantunits = $dm->createQueryBuilder('PlantnetDataBundle:Plantunit')->hydrate(false)->select('_id')->field('module.id')->equals($module->getId());
                     if (count($ids_punit)) {
                         $plantunits->field('_id')->in(array_values($ids_punit));
                     }
                     if (count($fields)) {
                         foreach ($fields as $key => $value) {
                             if (is_array($value)) {
                                 $value_query_tab = array();
                                 for ($i = 0; $i < count($value); $i++) {
                                     //check for int or float value
                                     if (is_numeric($value[$i])) {
                                         $tmp_value = intval($value[$i]);
                                         if ($value[$i] == $tmp_value) {
                                             $value_query_tab[] = $tmp_value;
                                         } else {
                                             $tmp_value = floatval($value[$i]);
                                             if ($value[$i] == $tmp_value) {
                                                 $value_query_tab[] = $tmp_value;
                                             }
                                         }
                                     }
                                     //
                                     $value_query_tab[] = new \MongoRegex('/.*' . StringHelp::accentToRegex($value[$i]) . '.*/i');
                                 }
                                 $plantunits->field('attributes.' . $key)->in($value_query_tab);
                             } else {
                                 $value_query_tab = array(new \MongoRegex('/.*' . StringHelp::accentToRegex($value) . '.*/i'));
                                 //check for int or float value
                                 if (is_numeric($value)) {
                                     $tmp_value = intval($value);
                                     if ($value == $tmp_value) {
                                         $value_query_tab[] = $tmp_value;
                                     } else {
                                         $tmp_value = floatval($value);
                                         if ($value == $tmp_value) {
                                             $value_query_tab[] = $tmp_value;
                                         }
                                     }
                                 }
                                 //
                                 $plantunits->field('attributes.' . $key)->in($value_query_tab);
                             }
                         }
                     }
                     $ids_c = $plantunits->getQuery()->execute();
                     $ids_tab = array();
                     foreach ($ids_c as $id) {
                         // $ids_tab[$id['_id']->{'$id'}]=$id['_id']->{'$id'};
                         $ids_tab[$id['_id']->{'$id'}] = $id['_id'];
                     }
                     $ids_c = null;
                     unset($ids_c);
                     $locations = $dm->createQueryBuilder('PlantnetDataBundle:Location')->field('plantunit.id')->in(array_values($ids_tab))->getQuery()->execute();
                     $nbResults = count($locations);
                     //count to display
                     $nb_locations = $nbResults;
                     /*
                     $nb_images=$dm->createQueryBuilder('PlantnetDataBundle:Image')
                         ->hydrate(false)
                         ->select('_id')
                         ->field('plantunit.id')->in(array_values($ids_tab))
                         ->getQuery()
                         ->execute()
                         ->count();
                     */
                     $connection = new \MongoClient();
                     $db = $this->get_prefix() . $project;
                     $db = $connection->{$db};
                     $nb_images = $db->Image->find(array('plantunit.$id' => array('$in' => array_values($ids_tab))))->count();
                     $ids_tab = null;
                     unset($ids_tab);
                 }
                 $dir = $this->get('kernel')->getBundle('PlantnetDataBundle')->getPath() . '/Resources/config/';
                 $layers = new \SimpleXMLElement($dir . 'layers.xml', 0, true);
                 $config = ControllerHelp::get_config($project, $dm, $this);
                 $tpl = $config->getTemplate();
                 return $this->render('PlantnetDataBundle:' . ($tpl ? $tpl : 'Frontend') . '\\Module:module_result.html.twig', array('config' => $config, 'project' => $project, 'collection' => $collection, 'module_parent' => $module, 'module' => $module, 'display' => $display, 'layers' => $layers, 'locations' => $locations, 'nbResults' => $nbResults, 'nb_images' => $nb_images, 'nb_locations' => $nb_locations, 'url' => $url, 'translations' => $translations, 'current' => 'collection', 'current_display' => 'locations', 'selected' => 'searchform' . $collection->getId() . $module->getId()));
                 break;
         }
     } else {
         return $this->redirect($this->generateUrl('front_module_search', array('project' => $project, 'collection' => $collection, 'module' => $module)));
     }
     $config = ControllerHelp::get_config($project, $dm, $this);
     $tpl = $config->getTemplate();
     return $this->render('PlantnetDataBundle:' . ($tpl ? $tpl : 'Frontend') . '\\Module:module_result.html.twig', array('config' => $config, 'project' => $project, 'collection' => $collection, 'module' => $module, 'translations' => $translations, 'nbResults' => 0, 'current' => 'collection', 'selected' => 'searchform' . $collection->getId() . $module->getId()));
 }