/** * @Route( * "/project/{project}/collection/{collection}/{module}/taxo_query", * defaults={"query"="null"}, * name="front_module_taxo_query_path" * ) * @Route( * "/project/{project}/collection/{collection}/{module}/taxo_query/{query}", * name="front_module_taxo_query" * ) * @Template() */ public function module_taxo_queryAction($project, $collection, $module, $query) { ControllerHelp::check_enable_project($project, $this->get_prefix(), $this); if ($query == 'null') { $response = new Response(json_encode(array())); $response->headers->set('Content-Type', 'application/json'); return $response; exit; } $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.'); } $taxons = $dm->createQueryBuilder('PlantnetDataBundle:Taxon')->hydrate(false)->select('name', 'identifier', 'label', 'issynonym')->field('module')->references($module)->field('name')->in(array(new \MongoRegex('/.*' . StringHelp::accentToRegex($query) . '.*/i')))->sort('name', 'asc')->limit(10)->getQuery()->execute(); $results = array(); foreach ($taxons as $tax) { $results[] = array('identifier' => $tax['identifier'], 'name' => $tax['name'], 'label' => $tax['label'], 'issynonym' => $tax['issynonym']); } $response = new Response(json_encode($results)); $response->headers->set('Content-Type', 'application/json'); return $response; exit; }
/** * Edits an existing Collection entity. * * @Route("/{id}/update", name="collection_update") * @Method("post") * @Template() */ public function collection_updateAction($id) { $user = $this->container->get('security.context')->getToken()->getUser(); $dm = $this->get('doctrine.odm.mongodb.document_manager'); $dm->getConfiguration()->setDefaultDB($this->getDataBase($user, $dm)); $collection = $dm->getRepository('PlantnetDataBundle:Collection')->findOneBy(array('id' => $id)); if (!$collection) { throw $this->createNotFoundException('Unable to find Collection entity.'); } $editForm = $this->createForm(new CollectionType(), $collection); $deleteForm = $this->createDeleteForm($id); $request = $this->getRequest(); if ('POST' === $request->getMethod()) { $editForm->bind($request); $url = $collection->getUrl(); if (StringHelp::isGoodForUrl($url)) { if ($editForm->isValid()) { $dm->persist($collection); $dm->flush(); $this->get('session')->getFlashBag()->add('msg_success', 'Collection updated'); return $this->redirect($this->generateUrl('collection_edit', array('id' => $id))); } } else { $form->get('url')->addError(new FormError('Illegal characters (allowed \'a-z\', \'0-9\', \'-\', \'_\').')); } } return $this->render('PlantnetDataBundle:Backend\\Collection:collection_edit.html.twig', array('entity' => $collection, 'edit_form' => $editForm->createView(), 'delete_form' => $deleteForm->createView())); }
/** * @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; }