Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Created May 27, 2013 06:29
Show Gist options
  • Select an option

  • Save joshuaadickerson/5655472 to your computer and use it in GitHub Desktop.

Select an option

Save joshuaadickerson/5655472 to your computer and use it in GitHub Desktop.
<?php
namespace Groundup\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class Search extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$options['facets'] = 'bla';
$this->buildFacetForm($builder, $options);
// What they are looking for
$builder->add('q', "text", array(
"attr" => array("placeholder" => "What"),
'label' => false,
));
// The location
$builder->add('l', "text", array(
"attr" => array(
"placeholder" => "Where",
'value' => $options['location']['town'] . ', ' . $options['location']['state_code'],
),
'label' => false,
'required' => false,
// 'empty_value' => 'some place',
));
// A hidden id for the location
$builder->add('idl', "hidden", array(
"attr" => array("placeholder" => "Where"),
'label' => false,
'required' => false,
));
}
protected function buildFacetForm(FormBuilderInterface $builder, array $options)
{
if (empty($options['facets']))
return;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
));
}
public function getName() {
return "";
}
}
<?php
namespace Groundup\Controller;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Form\Form;
use Groundup\Model\Manager\IPlaceManager;
use Groundup\Model\Manager\IUserManager;
use Groundup\Model\Entity\Place;
use Groundup\Form\SearchForm;
use Groundup\Model\Manager\PlaceManager;
use Symfony\Component\HttpFoundation\Request;
class SearchController implements ControllerProviderInterface
{
// Default distance
public $distance = 10;
public $start = 0;
public $limit = 10;
public $sort = 'score';
/**
* @var IPlaceManager
*/
protected $searchManager;
protected $userManager;
function __construct(Application $app)
{
$this->app = $app;
}
public function connect(Application $app)
{
// créer un nouveau controller basé sur la route par défaut
$searchController = $app['controllers_factory'];
$searchController->match("/", array($this, "index"))->bind("search.index");
$searchController->match("/loc", array($this, "location"))->bind("search.location");
$searchController->match("/results", array($this, "results"))->bind("search.results");
$searchController->get("/tag/{tag}", array($this, "getByTag"))->bind("search.getbytag")->convert('tag', function($tag) {
return urldecode($tag);
});
//get by username
$searchController->match('/user/{username}', array($this, "getByUsername"))->bind("search.getbyusername");
return $searchController;
}
/**
* Show search page
* @param \Silex\Application $app
* @return mixed
*/
function index(Application $app)
{
$this->app = $app;
$location = $this->getLocation();
$searchForm = $this->getSearchForm(array(
'location' => $location,
));
return $app["twig"]->render("search/index.twig", array("location" => $location, "searchForm" => $searchForm->createView()));
}
public function results(Request $request, Application $app)
{
$this->app = $app;
$distance = $this->getDistance();
$start = $this->getStart();
$limit = $this->getLimit();
$sort = $this->getSort();
$location = $this->getLocation($distance);
if(!empty($_REQUEST['q']))
$places = $this->getPlaces($location, $_REQUEST['q']);
else
$places = array();
$form = $this->getSearchForm(array(
'location' => $location,
));
$form->bind($request);
return $app["twig"]->render("search/index.twig", array(
"location" => $location,
'places' => $places,
"searchForm" => $form->createView(),
));
}
public function location(Application $app)
{
$this->app = $app;
return json_encode($this->getLocation()->toArray());
}
public function getSearchForm($options)
{
$searchForm = $this->app['form.factory']->create(new \Groundup\Form\Search(), array(
'location' => $options['location'],
'facets' => array(),
));
return $searchForm;
}
public function getPlaces($location)
{
}
public function getLocation()
{
// If they have already set the location (probably via autosuggest)
if (!empty($_REQUEST['idl']))
{
$id_location = $_REQUEST['idl'];
$location = $this->app['location_manager']->getById($id_location);
}
// Try it with the user's GeoIP
if (empty($location))
{
if (!empty($_SESSION['ip_location']))
$ip_location = $_SESSION['ip_location'];
else
{
$ip_location = $_SESSION['ip_location'] = $this->app['location_manager']->getByGeoIP($this->app['request']->getClientIp());
}
}
$ip_location = !empty($ip_location) ? $ip_location : array();
// Maybe they set the location
if (empty($location) && !empty($_REQUEST['l']))
{
if (!empty($ip_location['latitude']) && !empty($ip_location['longitude']))
$location = $this->app['location_manager']->getByString($_REQUEST['l'], $ip_location['latitude'], $ip_location['longitude'], 1);
else
$location = $this->app['location_manager']->getByString($_REQUEST['l'], null, null, 1);
}
if (empty($location))
$location = $ip_location;
// Can't let it get this far
if (empty($location))
throw new \Exception('No location set');
return $location;
}
public function getIntSearchParameter($param)
{
if (isset($_REQUEST[$param]))
$return = (int) $_REQUEST[$param];
elseif (isset($_SESSION['search'][$param]))
$return = (int) $_SESSION['search'][$param];
else
$return = $this->$param;
$this->$param = $return;
return $return;
}
public function getDistance()
{
return $this->getIntSearchParameter('distance');
}
public function getLimit()
{
return $this->getIntSearchParameter('limit');
}
public function getStart()
{
return $this->getIntSearchParameter('start');
}
public function getSort()
{
}
public function getTokens()
{
$string = $_REQUEST[$param];
return \Groundup\Controller\Helper\Search::getTokens($string);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment