Created
August 2, 2012 17:10
-
-
Save awartoft/3238809 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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