$(window).bind('statechange', function () {
var State = History.getState();
var url = State.url;
var data = State.data;
if (ajaxRequest) {
ajaxRequest.abort();
} else {
$resultContainer.html('<div class="ajax-loader" style="position:relative;"><img src="http://i.imgur.com/6RMhx.gif" /></div>');
}
ajaxRequest = $.ajax({
url: url,
data: data,
dataType: 'json',
success: function (data, textStatus, jqXHR) {
$resultContainer.html(data.content);
if (data.form) {
$filterForm.html(data.form);
filterFormInitCallback();
}
ajaxRequest = null;
},
error: function (jqXHR, textStatus, errorThrown) {
if ('abort' != textStatus) {
document.location.href = url;
}
return false;
}
});
});
$filterForm.submit(function () {
var query = $(this).formSerialize();
// decodeURIComponent т.к. formSerialize выдает закодированный урл, а histoty.js пытается закодировать его еще раз
History.pushState(null, document.title, '?' + window.decodeURIComponent(query));
return false;
});
defaults: { _controller: TestTestBundle:Test:index }
defaults: { _controller: test:indexAction }
<?php
$name = 'name';
$title = 'title';
$text = 'text';
$body = <<<BODY
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>$title</title>
</head>
<body>
<h1>$title<h1>
<p>$text</p>
</body>
</html>
BODY;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . sprintf('%s.html', $name));
header('Content-Length: ' . sizeof($body));
echo $body;
interface VotableInterface
{
public function setRating($rating);
}
class Object implements VotableInterface
{
/**
* @ORM\Column(type="integer")
*/
private $rating;
public function setRating($rating)
{
$this->rating = $rating;
}
public function getRating()
{
return $this->rating;
}
}
class VoteListener
{
public function postPersist(LifecycleEventArgs $event)
{
$vote = $event->getEntity();
if (!$vote instanceof Vote) {
return;
}
/**
* @var Object $entity
*/
$entity = $vote->getEntityObject();
if (!$entity instanceof VotableInterface) {
// Exception
}
$entity->setRating($entity->getRating() + $vote->getValue());
// Update $entity
}
}
$(document).ready(function(){
$('.list-h-item').addClass("hidden").viewportChecker({
callbackFunction: function(elem, action) {
$('.list-h-item').addClass('visible');
$('.list-h-item').each(function(i) {
$(this).delay((i++) * 500).fadeTo(1000, 2); });
});
}
});
});
$dateFormatter = new \IntlDateFormatter(
'ru',
\IntlDateFormatter::FULL,
\IntlDateFormatter::FULL,
new \DateTimeZone('UTC'),
IntlDateFormatter::GREGORIAN,
'dd MMM yyyy HH:mm:ss'
);
$string = '06 окт. 2014 21:45:17';
var_dump($string);
$timestamp = $dateFormatter->parse($string);
$test = new \DateTime(sprintf('@%s UTC', $timestamp));
var_dump($test);
{{ object_path(entity) }}
$compiledRoute = $route->compile();
$variables = $compiledRoute->getVariables();
$parameters = array();
if (!empty($variables)) {
foreach ($variables as $variable) {
$parameters[$variable] = $this->getObjectProperty($object, $variable);
}
}
return $this->router->generate($name, $parameters);
{{ object_path('route_name', entity) }}
Все необходимые параметры возьмутся из модели. Удобно для CRUD getRequest()
и getDoctrine()
$this->getDoctrine()->getManager()->getRepository('...')
сам пока не решил. class LogListener implements EventSubscriber
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getSubscribedEvents()
{
return array(Events::postUpdate, Events::postPersist, Events::preRemove);
}
public function postUpdate(LifecycleEventArgs $eventArgs)
{
$this->log($eventArgs, Log::ACTION_UPDATE);
}
public function postPersist(LifecycleEventArgs $eventArgs)
{
$this->log($eventArgs, Log::ACTION_PERSIST);
}
public function preRemove(LifecycleEventArgs $eventArgs)
{
$this->log($eventArgs, Log::ACTION_REMOVE);
}
private function log(LifecycleEventArgs $eventArgs, $action)
{
$token = $this->container->get('security.context')->getToken();
if (!$token) {
return;
}
$user = $token->getUser();
if (!($user instanceof User)) {
return;
}
$entity = $eventArgs->getEntity();
if ($entity instanceof Log) {
return;
}
$request = $this->container->get('request');
$log = new Log();
$log->setAction($action);
$log->setUser($user);
$log->setIp($request->server->get('REMOTE_ADDR'));
$log->setEntityClass(get_class($entity));
$log->setEntityId($entity->getId());
$em = $this->container->get('doctrine')->getEntityManager();
$meta = $em->getClassMetadata(get_class($log));
$conn = $em->getConnection();
foreach ($meta->getReflectionProperties() as $fieldName => $reflProp) {
if (!$meta->isIdentifier($fieldName)) {
$columns[] = $fieldName;
$values[] = ':' . $fieldName;
}
}
$insertSql = 'INSERT INTO ' . $meta->getQuotedTableName($conn->getDatabasePlatform())
. ' (' . implode(', ', $columns) . ') '
. 'VALUES (' . implode(', ', $values) . ')';
$stmt = $conn->prepare($insertSql);
$fields = $meta->getFieldNames();
$association = $meta->getAssociationNames();
foreach ($meta->getReflectionProperties() as $fieldName => $reflProp) {
if (!$meta->isIdentifier($fieldName)) {
$value = $reflProp->getValue($log);
if (in_array($fieldName, $fields)) {
$mapping = $meta->getFieldMapping($fieldName);
$stmt->bindValue($meta->getColumnName($fieldName), $value, $mapping['type']);
} else if (in_array($fieldName, $association)) {
$classMeta = $em->getClassMetadata($meta->getAssociationTargetClass($fieldName));
list($classId) = $classMeta->getIdentifier();
$mapping = $classMeta->getFieldMapping($classId);
$associationIdProperty = $classMeta->getReflectionProperty($classId);
$associationValue = $associationIdProperty->getValue($value);
$stmt->bindValue($meta->getColumnName($fieldName), $associationValue, $mapping['type']);
} else {
throw new \Exception('Exception in log listener');
}
}
}
$stmt->execute();
}
}