Skip to content

Instantly share code, notes, and snippets.

@awartoft
Created August 2, 2012 17:10
Show Gist options
  • Save awartoft/3238809 to your computer and use it in GitHub Desktop.
Save awartoft/3238809 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Antoine Hedgecock <[email protected]>
*/
/**
* @namespace
*/
namespace Company\Form;
use Zend\Form\Form,
Zend\InputFilter\InputFilter,
Zend\ServiceManager\ServiceManager,
Zend\ServiceManager\ServiceManagerAwareInterface,
Company\Service\Company\Category as CategoryService,
MCNCore\Sphinx\Hydrator\QueryInfo as QueryInfoHydrator;
class SearchBasic extends Form implements ServiceManagerAwareInterface
{
public function __construct()
{
parent::__construct('company_search');
// Basic stuff
$this->setAttribute('method', 'post');
// Very basic input filter
$filter = new \Zend\InputFilter\InputFilter();
$filter->add(array('required' => true), 'category');
$filter->add(array('required' => false), 'keywords');
// Apply filter
$this->setInputFilter($filter);
// Sphinx hydrator
$this->setHydrator(
new QueryInfoHydrator(
array(
'query_fields' => array(
'keywords' => array('name', 'description')
),
'filters' => array(
'category' => function(\SphinxClient $client, $value) {
if ($value == '_') {
return null;
}
$client->setFilter('category_id', (array) $value);
}
)
)
)
);
// Keywords
$this->add(
array(
'name' => 'keywords',
'attributes' => array(
'style' => 'width: 352px;'
)
)
);
// category
$this->add(
array(
'name' => 'category',
)
);
}
public function setServiceManager(ServiceManager $sm)
{
$options = array(
'sort' => array(
CategoryService::SORT_NAME => 'ASC'
),
'queryOptions' => array(
CategoryService::QUERY_OPT_WITHOUT_EMPTY_CATEGORIES => true,
CategoryService::QUERY_OPT_WITHOUT_UNSPECIFIED_CATEGORY => true
)
);
$categories = $sm->get('company_service_category')
->fetchAll($options);
$options = array('_' => 'Alla kategorier');
foreach($categories as $category)
{
$options[$category['id']] = $category['name'];
}
$this->get('category')
->setAttribute('options', $options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment