Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/dev/1.5' into 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrebenchuk committed Feb 12, 2015
2 parents f87c18f + 77e01d0 commit 5904df7
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class BufferedQueryResultIterator implements \Iterator, \Countable
*/
private $useCountWalker;

/**
* Walk through results in reverse order
* Useful when selected records are being updated in between page load
*
* @var bool
*/
private $reverse = false;

/**
* Constructor
*
Expand Down Expand Up @@ -164,6 +172,20 @@ public function setHydrationMode($hydrationMode)
return $this;
}

/**
* Sets iteration order
*
* @param bool $reverse Determines the iteration order
* @return BufferedQueryResultIterator
*/
public function setReverse($reverse)
{
$this->assertQueryWasNotExecuted('reverse mode');
$this->reverse = $reverse;

return $this;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -311,13 +333,25 @@ protected function loadNextPage()
$query = $this->getQuery();

$totalPages = ceil($this->count() / $query->getMaxResults());
if (!$totalPages || $totalPages <= $this->page + 1) {
unset($this->rows);
if ($this->reverse) {
if ($this->page == -1) {
$this->page = $totalPages;
}
if ($this->page < 1) {
unset($this->rows);

return false;
return false;
}
$this->page--;
} else {
if (!$totalPages || $totalPages <= $this->page + 1) {
unset($this->rows);

return false;
}
$this->page++;
}

$this->page++;
$this->offset = 0;

$this->prepareQueryToExecute($query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,63 @@ function ($sql) use (&$statements, &$statementCounter, &$actualSqls) {
$actualSqls[1]
);
}

public function testIteratorInReverseDirection()
{
$records = [
['a0' => '1'],
['a0' => '2'],
['a0' => '3'],
];
$actualSqls = [];
$statementCounter = 0;
$statements = [
$this->createFetchStatementMock([['sclr0' => count($records)]]),
$this->createFetchStatementMock([$records[0], $records[1]]),
$this->createFetchStatementMock([$records[2]])
];

$this->getDriverConnectionMock($this->em)->expects($this->any())
->method('query')
->will(
$this->returnCallback(
function ($sql) use (&$statements, &$statementCounter, &$actualSqls) {
$actualSqls[$statementCounter] = $sql;
$statement = $statements[$statementCounter];
$statementCounter++;
return $statement;
}
)
);

$source = $this->em->createQueryBuilder()
->select('o')
->from('Stub:Entity', 'o');

$iterator = new BufferedQueryResultIterator($source);
$iterator->setReverse(true);
$iterator->setBufferSize(2);

$this->assertEquals(count($records), $iterator->count());
$count = 0;
foreach ($iterator as $record) {
$this->assertInstanceOf('Oro\Bundle\BatchBundle\Tests\Unit\ORM\Query\Stub\Entity', $record);
$this->assertEquals($records[$count]['a0'], $record->a);
$count++;
}
$this->assertEquals(count($records), $count);
$this->assertCount(3, $actualSqls);
$this->assertEquals(
'SELECT count(e0_.a) AS sclr0 FROM Entity e0_',
$actualSqls[0]
);
$this->assertEquals(
'SELECT e0_.a AS a0, e0_.b AS b1 FROM Entity e0_ LIMIT 2 OFFSET 2',
$actualSqls[1]
);
$this->assertEquals(
'SELECT e0_.a AS a0, e0_.b AS b1 FROM Entity e0_ LIMIT 2 OFFSET 0',
$actualSqls[2]
);
}
}
78 changes: 78 additions & 0 deletions src/Oro/Bundle/FormBundle/Form/Type/DownloadLinksType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Oro\Bundle\FormBundle\Form\Type;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Templating\Helper\CoreAssetsHelper;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DownloadLinksType extends AbstractType
{
/** @var CoreAssetsHelper */
protected $assetHelper;

/**
* @param CoreAssetsHelper $assetHelper
*/
public function __construct(CoreAssetsHelper $assetHelper)
{
$this->assetHelper = $assetHelper;
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setRequired(['source'])
->setOptional(['class'])
->setDefaults(['class' => ''])
->setAllowedTypes(['source' => 'array']);
}

/**
* {@inheritdoc}
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
$view->vars['files'] = $this->getFiles($options['source']);
$view->vars['class'] = $options['class'];
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'oro_download_links_type';
}

/**
* Get files from a specified source data
*
* @param array $source
* @return array
*/
public function getFiles($source)
{
$resources = [];
if (isset($source['path'], $source['url'])) {
$finder = new Finder();
$pathParts = explode('/', $source['path']);
$fileNamePattern = array_pop($pathParts);
$files = $finder->name($fileNamePattern)->in(implode(DIRECTORY_SEPARATOR, $pathParts));
/** @var \SplFileInfo[] $files */
foreach ($files as $file) {
$resources[$file->getFilename()] = $this->assetHelper->getUrl(
rtrim($source['url'], '/') . '/' . $file->getFilename()
);
}
}

return $resources;
}
}
9 changes: 9 additions & 0 deletions src/Oro/Bundle/FormBundle/Resources/config/form_type.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parameters:
oro_form.type.simple_color_choice.class: Oro\Bundle\FormBundle\Form\Type\OroSimpleColorChoiceType
oro_form.type.color_table.class: Oro\Bundle\FormBundle\Form\Type\OroColorTableType
oro_form.type.link.class: Oro\Bundle\FormBundle\Form\Type\LinkType
oro_form.type.download_links.class: Oro\Bundle\FormBundle\Form\Type\DownloadLinksType
oro_form.type.api.class: Oro\Bundle\FormBundle\Form\Type\AbstractApiType

oro_form.extension.data_block.class: Oro\Bundle\FormBundle\Form\Extension\DataBlockExtension
Expand Down Expand Up @@ -192,6 +193,14 @@ services:
tags:
- { name: form.type, alias: oro_link_type }

oro_form.type.download_links:
class: %oro_form.type.download_links.class%
arguments:
- @templating.helper.assets
scope: request
tags:
- { name: form.type, alias: oro_download_links_type }

oro_form.type.api:
class: %oro_form.type.api.class%
tags:
Expand Down
25 changes: 13 additions & 12 deletions src/Oro/Bundle/FormBundle/Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -679,21 +679,22 @@

{% block oro_link_type_widget %}
{% if isPath or resource_granted(acl) %} {# @todo after BAP-4696 implementation remove isPath from condition #}
{# @todo after BAP-4696 implementation remove link class #}
{% if value is defined and value is iterable%}
{% for fileName, route in value %}
<a href="{{ route }}" class="{{ class is defined and class ? class : '' }}" style="display: block; margin-top: 5px;">
{{ fileName }}
</a>
{% endfor %}
{% else %}
<a href="{{ isPath ? route : path(route, routeParameters) }}" class="{{ class is defined and class ? class : '' }}" style="display: block; margin-top: 5px;">
{{ title|trans }}
</a>
{% endif %}
<a href="{{ isPath ? route : path(route, routeParameters) }}" class="{{ class is defined and class ? class : '' }}" style="display: block; margin-top: 5px;">
{{ title|trans }}
</a>
{% endif %}
{% endblock %}

{% block oro_download_links_type_widget %}
{% spaceless %}
{% for fileName, route in files %}
<a href="{{ route }}" class="{{ class is defined and class ? class : '' }}" style="display: block; margin-top: 5px;">
{{ fileName }}
</a>
{% endfor %}
{% endspaceless %}
{% endblock %}

{% block oro_simple_color_picker_row %}
{{ block('form_row') }}
{% endblock %}
Expand Down
Loading

0 comments on commit 5904df7

Please sign in to comment.