Skip to content

Instantly share code, notes, and snippets.

@teresko

teresko/view.php Secret

Last active September 2, 2016 20:31
Show Gist options
  • Save teresko/c13a801dadf595d2b515e1b30d1f3b49 to your computer and use it in GitHub Desktop.
Save teresko/c13a801dadf595d2b515e1b30d1f3b49 to your computer and use it in GitHub Desktop.
<?php
namespace Aggregator\Views;
use Symfony\Component\HttpFoundation\Response;
use Components\View;
use Model\Services\Configuration;
use Model\Services\Parser;
use Model\Services\Warehouse;
class Main extends View
{
private $configuration;
private $parser;
private $warehouse;
public function __construct(Configuration $configuration, Parser $parser, Warehouse $warehouse)
{
$this->configuration = $configuration;
$this->parser = $parser;
$this->warehouse = $warehouse;
}
public function getDefault(Response $response)
{
$columns = $this->configuration->getColumns('leads');
$template = $this->engine->loadTemplate('listing.html.twig');
$response->setContent($template->render(array(
'section' => 'leads',
'results' => $this->warehouse->retrieveEntries(),
'columns' => $columns,
'visible' => array_reduce($columns, function ($carry = array(), $item) {
$carry[$item['key']] = $item['visible'];
return $carry;
}),
)));
return $response;
}
public function postDefault(Response $response)
{
$entries = $this->warehouse->retrieveEntries();
$template = $this->engine->loadTemplate('rows.html.twig');
$data = array(
'total' => $entries->getTotal(),
'html' => $template->render(array(
'results' => $entries,
'columns' => $this->configuration->getColumns('leads'),
)),
);
$response->setContent(json_encode($data));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment