Skip to content

Instantly share code, notes, and snippets.

@macnibblet
Created June 27, 2012 14:20
Show Gist options
  • Save macnibblet/3004348 to your computer and use it in GitHub Desktop.
Save macnibblet/3004348 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Antoine Hedgecock <antoine@pmg.se>
*/
/**
* @namespace
*/
namespace Company\Form\Company;
use Zend\Form\Form,
Company\Service\Company\Branch as BranchService,
Zend\ServiceManager\ServiceManager,
Zend\ServiceManager\ServiceManagerAwareInterface;
class Search extends Form implements ServiceManagerAwareInterface
{
public function setServiceManager(ServiceManager $serviceManager)
{
$options = array(
'sort' => array(
'name' => 'ASC'
),
'queryOptions' => array(
BranchService::QUERY_OPT_WITHOUT_EMPTY_BRANCHES => true,
BranchService::QUERY_OPT_WITHOUT_UNSPECIFIED_BRANCH => true
)
);
$branches = $serviceManager->get('company_service_branch')
->fetchAll($options);
$values = array(
'Alla kategorier' => '_'
);
foreach($branches as $branch) {
$values[$branch['name']] = $branch['id'];
}
$this->get('category')
->setAttribute('options', $values);
}
public function __construct($name = null)
{
parent::__construct($name);
$this->add(
array(
'name' => 'category',
'attributes' => array(
'label' => 'Välj kategori',
)
)
);
$this->add(
array(
'name' => 'keywords',
'attributes' => array(
'label' => 'Nyckelord'
)
)
);
}
public function getSphinxSearchParameters()
{
$parameters = array(
'query' => array(),
'methods' => array()
);
if (isSet($this->data['keywords']) && !empty($this->data['keywords'])) {
$parameters['query'][] = array(
'field' => array('name', 'description'),
'value' => $this->data['keywords']
);
}
if (isSet($this->data['category']) && $this->data['category'] != '_') {
$parameters['methods'][] = array(
'method' => 'filter',
'parameters' => array('category', (array) $this->data['category'])
);
}
return $parameters;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment