Skip to content

Instantly share code, notes, and snippets.

@awartoft
Created July 20, 2012 09:13
Show Gist options
  • Save awartoft/3149767 to your computer and use it in GitHub Desktop.
Save awartoft/3149767 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Antoine Hedgecock <[email protected]>
*/
/**
* @namespace
*/
namespace Company\Controller;
use Zend\View\Model\ViewModel,
Zend\Mvc\Controller\AbstractActionController,
Zend\Http\Response as HttpResponse,
Company\Service\Company\Category as CategoryService,
Company\Service\Company as CompanyService;
class IndexController extends AbstractActionController
{
/**
* @return \Company\Service\Company
*/
protected function getService()
{
return $this->getServiceLocator()
->get('company_service');
}
/**
* @return \Company\Form\SearchBasic
*/
protected function getSearchForm()
{
// get the search form
$form = $this->getServiceLocator()
->get('company_form_search_basic');
return $form;
}
public function indexAction()
{
$form = $this->getSearchForm();
// query options for the branches
$options = array(
'sort' => array(
CategoryService::SORT_NAME => 'ASC'
),
'queryOptions' => array(
CategoryService::QUERY_OPT_WITHOUT_EMPTY_CATEGORIES => true,
CategoryService::QUERY_OPT_WITHOUT_UNSPECIFIED_CATEGORY => true
)
);
// retrieve from the database
$categories = $this->getServiceLocator()
->get('company_service_category')
->fetchAll($options);
return array(
'form' => $form,
'categories' => $categories
);
}
public function searchAction()
{
// Check if there are any parameters related to this request
$parameters = $this->searchStorage('company/search', 'company');
// Get the page from the request
$page = $this->params('page', 1);
// Options to query the search engine with
$options = new \ArrayObject(
array(
'limit' => 6,
'offset' => ($page - 1) * 6,
'relations' => array(
'category'
)
)
);
// get the search form
$form = $this->getSearchForm();
$form->bind($options);
// Apply the parameters to the form
$form->setData($parameters);
// Validate the form
if (! $form->isValid()) {
// todo: dispatch error
}
$companies = $this->getService()
->search($options->getArrayCopy());
return array(
'form' => $form,
'companies' => $companies
);
}
public function latestAction()
{
$vm = new ViewModel();
$vm->setTemplate('company/index/view');
$options = array(
'limit' => 1,
'sort' => array(
CompanyService::SORT_CREATED => 'DESC'
),
'queryOptions' => array(
CompanyService::QUERY_OPT_WITHOUT_DELETED => true
)
);
$company = $this->getService()
->fetchAll($options)[0];
return $vm->setVariable('company', $company);
}
public function contactAction()
{
$company = $this->getService()
->getByOrganisationNumber($this->params('organisation_number'), array('category'));
// check that the company exists
if (! $company) {
// Set status code
$this->getResponse()
->setStatusCode(404);
// not returning anything renders the 404 template
return;
}
return array(
'company' => $company
);
}
public function viewAction()
{
$relations = array(
'category',
'articles' => array(
'joinAlias' => 'article'
),
'article.images' => array(
'joinAlias' => 'article_images',
)
);
$company = $this->getService()
->getByOrganisationNumber($this->params('organisation_number'), $relations);
// check that the company exists
if (! $company) {
// Set status code
$this->getResponse()
->setStatusCode(404);
// not returning anything renders the 404 template
return;
}
// Add a new view to the company
$this->getServiceLocator()
->get('mcn.service.view')
->addViewTo($company);
// Render
return array(
'company' => $company
);
}
public function mostViewedAction()
{
$vm = new ViewModel();
$vm->setTemplate('company/index/most-viewed');
$options = array(
'sort' => array(
CompanyService::SORT_HIDDEN_VIEW_COUNT => 'DESC'
),
'queryOptions' => array(
CompanyService::QUERY_OPT_WITHOUT_DELETED => true,
CompanyService::QUERY_OPT_WITH_HIDDEN_VIEW_COUNT => true
),
'limit' => 10
);
$companies = $this->getService()
->fetchAll($options);
return $vm->setVariable('companies', $companies);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment