/**
  * @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}/search/{attribute}/{query}",
  *      defaults={"attribute"="null", "query"="null"},
  *      name="front_module_search_query_path"
  *  )
  * @Route(
  *      "/project/{project}/collection/{collection}/{module}/search/{attribute}/{query}",
  *      name="front_module_search_query"
  *  )
  * @Template()
  */
 public function module_search_queryAction($project, $collection, $module, $attribute, $query)
 {
     ControllerHelp::check_enable_project($project, $this->get_prefix(), $this);
     $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->getWsonly() == true) {
         throw $this->createNotFoundException('Unable to find Module entity.');
     }
     $results = array();
     $query_tab = array(new \MongoRegex('/.*' . StringHelp::accentToRegex($query) . '.*/i'));
     //check for int or float value
     if (is_numeric($query)) {
         $tmp_query = intval($query);
         if ($query == $tmp_query) {
             $query_tab[] = $tmp_query;
         } else {
             $tmp_query = floatval($query);
             if ($query == $tmp_query) {
                 $query_tab[] = $tmp_query;
             }
         }
     }
     //
     if (substr_count($attribute, '#') == 1) {
         $sub_module_id = substr($attribute, 0, strpos($attribute, '#'));
         $sub_attribute_id = substr($attribute, strpos($attribute, '#') + 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)->distinct('property.' . $sub_attribute_id)->select('property.' . $sub_attribute_id)->field('module')->references($sub_module)->field('property.' . $sub_attribute_id)->in($query_tab)->sort('property.' . $sub_attribute_id, 'asc')->limit(10)->getQuery()->execute();
         foreach ($others as $other) {
             $results[] = $other;
         }
     } else {
         $plantunits = $dm->createQueryBuilder('PlantnetDataBundle:Plantunit')->hydrate(false)->distinct('attributes.' . $attribute)->select('attributes.' . $attribute)->field('module')->references($module)->field('attributes.' . $attribute)->in($query_tab)->sort('attributes.' . $attribute, 'asc')->limit(10)->getQuery()->execute();
         foreach ($plantunits as $punit) {
             $results[] = $punit;
         }
     }
     $response = new Response(json_encode($results));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
     exit;
 }