Merge pull request #322 from nlegoff/fix_1111

[3.7] Fix #1111 sub views substitution is broken
This commit is contained in:
Romain Neutron
2013-03-21 11:42:56 -07:00
4 changed files with 187 additions and 122 deletions

View File

@@ -127,30 +127,29 @@ class Tools implements ControllerProviderInterface
$controllers->post('/hddoc/', function(Application $app, Request $request) { $controllers->post('/hddoc/', function(Application $app, Request $request) {
$success = false; $success = false;
$errorMessage = ""; $message = _('An error occured');
$fileName = null;
if ($file = $request->files->get('newHD')) { if ($file = $request->files->get('newHD')) {
if ($file->isValid()) {
$fileName = $file->getClientOriginalName(); $fileName = $file->getClientOriginalName();
$size = $file->getClientSize();
$tempoFile = tempnam(sys_get_temp_dir(), 'substit');
unlink($tempoFile);
mkdir($tempoFile);
$tempoFile = $tempoFile . DIRECTORY_SEPARATOR . $fileName;
copy($file->getPathname(), $tempoFile);
if ($file->getClientSize() && $fileName && $file->isValid()) {
try { try {
$record = new \record_adapter( $tmpFile = sprintf(
$request->get('sbas_id') '%stmp/%s',
, $request->get('record_id') $app['Core']->getRegistry()->get('GV_RootPath'),
$fileName
); );
$media = $app['Core']['mediavorus']->guess(new \SplFileInfo($tempoFile)); if (false === rename($file->getPathname(), $tmpFile)) {
throw new \Exception();
}
$record = new \record_adapter(
$request->get('sbas_id'),
$request->get('record_id')
);
$media = $app['Core']['mediavorus']->guess(new \SplFileInfo($tmpFile));
$record->substitute_subdef('document', $media); $record->substitute_subdef('document', $media);
@@ -159,72 +158,64 @@ class Tools implements ControllerProviderInterface
} }
$success = true; $success = true;
$message = _('Document has been successfully substitued');
unlink($tmpFile);
} catch (\Exception $e) { } catch (\Exception $e) {
$errorMessage = $e->getMessage();
}
unlink($tempoFile); }
rmdir(dirname($tempoFile));
unlink($file->getPathname());
} else { } else {
$errorMessage = _('file is not valid'); $message = _('file is not valid');
} }
} }
$template = 'prod/actions/Tools/iframeUpload.html.twig'; return new Response($app['Core']->getTwig()->render('prod/actions/Tools/iframeUpload.html.twig', array(
$var = array( 'success' => $success,
'success' => $success 'message' => $message,
, 'fileName' => $fileName )));
, 'errorMessage' => $errorMessage
);
return new Response($app['Core']->getTwig()->render($template, $var));
/**
*
*/
}); });
$controllers->post('/chgthumb/', function(Application $app, Request $request) { $controllers->post('/chgthumb/', function(Application $app, Request $request) {
$success = false; $success = false;
$errorMessage = ""; $message = _('An error occured');
if ($file = $request->files->get('newThumb')) { if ($file = $request->files->get('newThumb')) {
$size = $file->getClientSize();
$fileName = $file->getClientOriginalName(); $fileName = $file->getClientOriginalName();
if ($size && $fileName && $file->isValid()) { if ($file->getClientSize() && $fileName && $file->isValid()) {
try { try {
$rootPath = $app['Core']->getRegistry()->get('GV_RootPath'); $tmpFile = sprintf(
$tmpFile = $rootPath . 'tmp/' . $fileName; '%stmp/%s',
rename($file->getPathname(), $tmpFile); $app['Core']->getRegistry()->get('GV_RootPath'),
$fileName
$record = new \record_adapter(
$request->get('sbas_id')
, $request->get('record_id')
); );
$media = $app['Core']['mediavorus']->guess($file); if (false === rename($file->getPathname(), $tmpFile)) {
throw new \Exception();
}
$record = new \record_adapter(
$request->get('sbas_id'),
$request->get('record_id')
);
$media = $app['Core']['mediavorus']->guess(new \SplFileInfo($tmpFile));
$record->substitute_subdef('thumbnail', $media); $record->substitute_subdef('thumbnail', $media);
$success = true; $success = true;
$message = _('Thumbnail has been successfully substitued');
unlink($tmpFile);
} catch (\Exception $e) { } catch (\Exception $e) {
$errorMessage = $e->getMessage();
} }
} else { } else {
$errorMessage = _('file is not valid'); $message = _('file is not valid');
} }
$template = 'prod/actions/Tools/iframeUpload.html.twig'; return new Response($app['Core']->getTwig()->render('prod/actions/Tools/iframeUpload.html.twig', array(
$var = array( 'success' => $success,
'success' => $success 'message' => $message,
, 'fileName' => $fileName )));
, 'errorMessage' => $errorMessage
);
return new Response($app['Core']->getTwig()->render($template, $var));
} }
}); });

View File

@@ -1,7 +1,4 @@
<div class='content'> <div class='content {% if not success %}text-error{% endif %}'>
{% if not success %} {{ message }}
{% trans 'an error occured' %}
{{errorMessage}}
{% endif %}
</div> </div>

View File

@@ -57,7 +57,7 @@
</a> </a>
</li> </li>
{% endif %} {% endif %}
{% if selectionLength == 1 and registry.get('GV_seeOngChgDoc') %} {% if selectionLength == 1 and (registry.get('GV_seeOngChgDoc') or registry.get('GV_seeNewThumb')) %}
<li> <li>
<a href="#substitution"> <a href="#substitution">
{% trans "substitution" %} {% trans "substitution" %}
@@ -73,7 +73,7 @@
{% endif %} {% endif %}
</ul> </ul>
</div> </div>
{# subef section #} {# subdef section #}
<div id="subdefs" class="tabBox"> <div id="subdefs" class="tabBox">
<form id="new-img-form" action="/prod/tools/image/" method="post"> <form id="new-img-form" action="/prod/tools/image/" method="post">
<fieldset style='border:1px solid #999; padding:20px;'> <fieldset style='border:1px solid #999; padding:20px;'>
@@ -269,10 +269,13 @@
{% endif %} {% endif %}
{# hd sub section #} {# hd sub section #}
{% if selectionLength == 1 %} {% if selectionLength == 1 and (registry.get('GV_seeOngChgDoc') or registry.get('GV_seeNewThumb')) %}
{% for record in selection %} {% for record in selection %}
<div id="substitution" class="tabBox"> <div id="substitution" class="tabBox">
{% if "unknown" == record.get_type() %}
<i class="icon-warning-sign icon-white"></i>{% trans "Substitution is not possible for this kind of record" %}
{% else %}
{% if registry.get('GV_seeOngChgDoc') %} {% if registry.get('GV_seeOngChgDoc') %}
<div id="substitution-hd"> <div id="substitution-hd">
<form <form
@@ -285,7 +288,6 @@
<fieldset style='border:1px solid #999;padding:20px;'> <fieldset style='border:1px solid #999;padding:20px;'>
<legend style='color:#EEE'>&nbsp;<b>{% trans "substitution HD" %}</b>&nbsp;</legend> <legend style='color:#EEE'>&nbsp;<b>{% trans "substitution HD" %}</b>&nbsp;</legend>
<div> <div>
<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
<input id='new-hd-file' name="newHD" type="file"/> <input id='new-hd-file' name="newHD" type="file"/>
<br /> <br />
<label for="CCFNALP" class="checkbox"> <label for="CCFNALP" class="checkbox">
@@ -318,7 +320,6 @@
<fieldset style='border:1px solid #999;padding:20px;'> <fieldset style='border:1px solid #999;padding:20px;'>
<legend style='color:#EEE'>&nbsp;<b>{% trans "substitution SD" %}</b>&nbsp;</legend> <legend style='color:#EEE'>&nbsp;<b>{% trans "substitution SD" %}</b>&nbsp;</legend>
<div> <div>
<input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
<input id='new-sd-file' name="newThumb" type="file" /> <input id='new-sd-file' name="newThumb" type="file" />
<input type="hidden" name="sbas_id" value="{{record.get_sbas_id()}}" /> <input type="hidden" name="sbas_id" value="{{record.get_sbas_id()}}" />
<input type="hidden" name="record_id" value="{{record.get_record_id()}}" /> <input type="hidden" name="record_id" value="{{record.get_record_id()}}" />
@@ -333,6 +334,7 @@
<div class='resultAction'></div> <div class='resultAction'></div>
</div> </div>
{% endif %} {% endif %}
{% endif %}
</div> </div>
{% endfor %} {% endfor %}
{% endif %} {% endif %}

View File

@@ -0,0 +1,75 @@
<?php
require_once __DIR__ . '/../../../../PhraseanetWebTestCaseAuthenticatedAbstract.class.inc';
use Symfony\Component\HttpFoundation\File\UploadedFile;
class ControllerToolsTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
{
protected $client;
protected $tmpFile;
public function setUp()
{
parent::setUp();
$this->client = $this->createClient();
$this->tmpFile = sys_get_temp_dir() . '/' . time() . mt_rand(1000, 9999) . '.jpg';
copy(__DIR__ . '/../../../../testfiles/cestlafete.jpg', $this->tmpFile);
}
public function tearDown()
{
if (file_exists($this->tmpFile)) {
unlink($this->tmpFile);
}
parent::tearDown();
}
public function createApplication()
{
$app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Prod.php';
$app['debug'] = true;
unset($app['exception_handler']);
return $app;
}
public function testRouteChangeDoc()
{
$record = static::$records['record_1'];
$crawler = $this->client->request('POST', '/tools/hddoc/', array(
'sbas_id' => $record->get_sbas_id(),
'record_id' => $record->get_record_id(),
), array(
'newHD' => new UploadedFile(
$this->tmpFile, 'KIKOO.JPG', 'image/jpg', 2000
)
));
$response = $this->client->getResponse();
$message = trim($crawler->filterXPath('//div')->text());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(_('Document has been successfully substitued'), $message);
}
public function testRouteChangeThumb()
{
$record = static::$records['record_1'];
$crawler = $this->client->request('POST', '/tools/chgthumb/', array(
'sbas_id' => $record->get_sbas_id(),
'record_id' => $record->get_record_id(),
), array(
'newThumb' => new UploadedFile(
$this->tmpFile, 'KIKOO.JPG', 'image/jpg', 2000
)
));
$response = $this->client->getResponse();
$message = trim($crawler->filterXPath('//div')->text());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(_('Thumbnail has been successfully substitued'), $message);
}
}