Skip to content

Instantly share code, notes, and snippets.

@awartoft
Created August 2, 2012 17:11
Show Gist options
  • Save awartoft/3238815 to your computer and use it in GitHub Desktop.
Save awartoft/3238815 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Antoine Hedgecock <[email protected]>
*/
/**
* @namespace
*/
namespace Article\Form;
use Zend\Form\Form,
Zend\ServiceManager\ServiceManager,
Zend\ServiceManager\ServiceManagerAwareInterface,
Article\Service\Article\Category as CategoryService,
Zend\InputFilter\Factory as InputFilterFactory,
MCNCore\Sphinx\Hydrator\QueryInfo as QueryInfoHydrator;
class Search extends Form implements ServiceManagerAwareInterface
{
public function __construct()
{
parent::__construct('article_search');
$this->setAttribute('method', 'post')
->setInputFilter(new InputFilter\Search)
->setHydrator(
new QueryInfoHydrator(
array(
'query_fields' => array(
'topic' => array('topic'),
'content' => array('topic', 'introduction', 'body', 'fact')
),
'filters' => array(
'category' => function($client, $value) {
if($value == '_') {
return null;
}
$client->setFilter('category_id', (array) $value);
},
'posted_within' => function($client, $value) {
if($value == '_') {
return null;
}
$date = new \DateTime();
$date->sub(new \DateInterval('P' . (int) $value . 'M'));
$client->setFilterRange('posted_within', $date->getTimestamp(), time());
}
)
)
)
);
$this->add(
array(
'name' => 'topic',
'options' => array(
'label' => 'Ord i rubrik'
)
)
);
$this->add(
array(
'name' => 'content',
'options' => array(
'label' => 'Ord i text'
),
)
);
$this->add(
array(
'name' => 'category',
'options' => array(
'label' => 'Kategori',
),
)
);
$this->add(
array(
'name' => 'posted_within',
'options' => array(
'label' => 'Tidsintervall',
),
'attributes' => array(
'options' => array(
'_' => 'Alla',
1 => 'Senaste månaden',
3 => 'Senaste 3 månader',
6 => 'Senaste 6 månader',
12 => 'Senaste 12 månader',
18 => 'Senaste 18 månader',
36 => 'Senaste 36 månader'
)
)
)
);
}
public function setServiceManager(ServiceManager $sm)
{
$options = array(
'sort' => array(
CategoryService::SORT_NAME => 'ASC'
),
'queryOptions' => array(
CategoryService::QUERY_OPT_WITHOUT_EMPTY_CATEGORIES => true,
)
);
$categories = $sm->get('article_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