From 915749c2241918fe969945a950a4456b02f819dd Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 21 Mar 2013 16:06:06 +0100 Subject: [PATCH 1/3] Fix sub views substitution --- lib/Alchemy/Phrasea/Controller/Prod/Tools.php | 4 +- .../web/prod/actions/Tools/index.html.twig | 122 +++++++++--------- .../Phrasea/Controller/Prod/ToolsTest.php | 59 +++++++++ 3 files changed, 124 insertions(+), 61 deletions(-) create mode 100644 tests/Alchemy/Phrasea/Controller/Prod/ToolsTest.php diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Tools.php b/lib/Alchemy/Phrasea/Controller/Prod/Tools.php index 66cd544cbd..c0f5ef05ae 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Tools.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Tools.php @@ -205,13 +205,13 @@ class Tools implements ControllerProviderInterface , $request->get('record_id') ); - $media = $app['Core']['mediavorus']->guess($file); + $media = $app['Core']['mediavorus']->guess(new \SplFileInfo($tmpFile)); $record->substitute_subdef('thumbnail', $media); $success = true; } catch (\Exception $e) { - $errorMessage = $e->getMessage(); + $errorMessage = _('An error occured'); } } else { $errorMessage = _('file is not valid'); diff --git a/templates/web/prod/actions/Tools/index.html.twig b/templates/web/prod/actions/Tools/index.html.twig index be20523459..7fc649b510 100644 --- a/templates/web/prod/actions/Tools/index.html.twig +++ b/templates/web/prod/actions/Tools/index.html.twig @@ -57,7 +57,7 @@ {% endif %} - {% if selectionLength == 1 and registry.get('GV_seeOngChgDoc') %} + {% if selectionLength == 1 and (registry.get('GV_seeOngChgDoc') or registry.get('GV_seeNewThumb')) %}
  • {% trans "substitution" %} @@ -73,7 +73,7 @@ {% endif %} - {# subef section #} + {# subdef section #}
    @@ -257,70 +257,74 @@ {% endif %} {# hd sub section #} - {% if selectionLength == 1 %} + {% if selectionLength == 1 and (registry.get('GV_seeOngChgDoc') or registry.get('GV_seeNewThumb')) %} {% for record in selection %}
    - {% if registry.get('GV_seeOngChgDoc') %} -
    - + {% if "unknown" == record.get_type() %} + {% trans "Substitution is not possible for this kind of record" %} + {% else %} + {% if registry.get('GV_seeOngChgDoc') %} +
    + -
    -  {% trans "substitution HD" %}  -
    - - -
    - - - - -
    -
    -
    -
    - - +
    +  {% trans "substitution HD" %}  +
    + + +
    + + + + +
    +
    +
    +
    + + +
    + +
    - -
    -
    - {% endif %} + {% endif %} - {% if registry.get('GV_seeNewThumb') %} -
    -
    -
    -  {% trans "substitution SD" %}  -
    - - - - -
    -
    -
    -
    - - + {% if registry.get('GV_seeNewThumb') %} +
    + +
    +  {% trans "substitution SD" %}  +
    + + + + +
    +
    +
    +
    + + +
    + +
    - -
    -
    - {% endif %} + {% endif %} + {% endif %}
    {% endfor %} {% endif %} diff --git a/tests/Alchemy/Phrasea/Controller/Prod/ToolsTest.php b/tests/Alchemy/Phrasea/Controller/Prod/ToolsTest.php new file mode 100644 index 0000000000..62e15dd5a2 --- /dev/null +++ b/tests/Alchemy/Phrasea/Controller/Prod/ToolsTest.php @@ -0,0 +1,59 @@ +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 testRouteChangeThumb() + { + $record = static::$records['record_1']; + + $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(); + + $content = $response->getContent(); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertFalse(strpos(_('An error occured'), $content)); + $this->assertFalse(strpos(_('file is not valid'), $content)); + } +} From 3bcf24b611bbd23e07dfd4c3a355a6df26b6cc83 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 21 Mar 2013 17:10:46 +0100 Subject: [PATCH 2/3] Subsitution cleanup && test enhancement --- lib/Alchemy/Phrasea/Controller/Prod/Tools.php | 101 ++++++++---------- .../prod/actions/Tools/iframeUpload.html.twig | 7 +- .../web/prod/actions/Tools/index.html.twig | 2 - .../Phrasea/Controller/Prod/ToolsTest.php | 28 +++-- 4 files changed, 70 insertions(+), 68 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Tools.php b/lib/Alchemy/Phrasea/Controller/Prod/Tools.php index c0f5ef05ae..01fee5f25a 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Tools.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Tools.php @@ -127,30 +127,29 @@ class Tools implements ControllerProviderInterface $controllers->post('/hddoc/', function(Application $app, Request $request) { $success = false; - $errorMessage = ""; - $fileName = null; + $message = _('An error occured'); if ($file = $request->files->get('newHD')) { + $fileName = $file->getClientOriginalName(); - if ($file->isValid()) { - - $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 { - $record = new \record_adapter( - $request->get('sbas_id') - , $request->get('record_id') + $tmpFile = sprintf( + '%stmp/%s', + $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); @@ -159,50 +158,44 @@ class Tools implements ControllerProviderInterface } $success = true; + $message = _('Record document has been successfully substitued'); + unlink($tmpFile); } catch (\Exception $e) { - $errorMessage = $e->getMessage(); - } - unlink($tempoFile); - rmdir(dirname($tempoFile)); - unlink($file->getPathname()); + } } else { - $errorMessage = _('file is not valid'); + $message = _('file is not valid'); } } - $template = 'prod/actions/Tools/iframeUpload.html.twig'; - $var = array( - 'success' => $success - , 'fileName' => $fileName - , 'errorMessage' => $errorMessage - ); - - return new Response($app['Core']->getTwig()->render($template, $var)); - - /** - * - */ + return new Response($app['Core']->getTwig()->render('prod/actions/Tools/iframeUpload.html.twig', array( + 'success' => $success, + 'message' => $message, + ))); }); $controllers->post('/chgthumb/', function(Application $app, Request $request) { $success = false; - $errorMessage = ""; + $message = _('An error occured'); if ($file = $request->files->get('newThumb')) { - - $size = $file->getClientSize(); $fileName = $file->getClientOriginalName(); - if ($size && $fileName && $file->isValid()) { + if ($file->getClientSize() && $fileName && $file->isValid()) { try { - $rootPath = $app['Core']->getRegistry()->get('GV_RootPath'); - $tmpFile = $rootPath . 'tmp/' . $fileName; - rename($file->getPathname(), $tmpFile); + $tmpFile = sprintf( + '%stmp/%s', + $app['Core']->getRegistry()->get('GV_RootPath'), + $fileName + ); + + if (false === rename($file->getPathname(), $tmpFile)) { + throw new \Exception(); + } $record = new \record_adapter( - $request->get('sbas_id') - , $request->get('record_id') + $request->get('sbas_id'), + $request->get('record_id') ); $media = $app['Core']['mediavorus']->guess(new \SplFileInfo($tmpFile)); @@ -210,21 +203,19 @@ class Tools implements ControllerProviderInterface $record->substitute_subdef('thumbnail', $media); $success = true; + $message = _('Record thumbnail has been successfully substitued'); + unlink($tmpFile); } catch (\Exception $e) { - $errorMessage = _('An error occured'); + } } else { - $errorMessage = _('file is not valid'); + $message = _('file is not valid'); } - $template = 'prod/actions/Tools/iframeUpload.html.twig'; - $var = array( - 'success' => $success - , 'fileName' => $fileName - , 'errorMessage' => $errorMessage - ); - - return new Response($app['Core']->getTwig()->render($template, $var)); + return new Response($app['Core']->getTwig()->render('prod/actions/Tools/iframeUpload.html.twig', array( + 'success' => $success, + 'message' => $message, + ))); } }); diff --git a/templates/web/prod/actions/Tools/iframeUpload.html.twig b/templates/web/prod/actions/Tools/iframeUpload.html.twig index a060dbff78..d6e852a871 100644 --- a/templates/web/prod/actions/Tools/iframeUpload.html.twig +++ b/templates/web/prod/actions/Tools/iframeUpload.html.twig @@ -1,7 +1,4 @@ -
    - {% if not success %} - {% trans 'an error occured' %} - {{errorMessage}} - {% endif %} +
    + {{ message }}
    diff --git a/templates/web/prod/actions/Tools/index.html.twig b/templates/web/prod/actions/Tools/index.html.twig index 7fc649b510..017b1b95ac 100644 --- a/templates/web/prod/actions/Tools/index.html.twig +++ b/templates/web/prod/actions/Tools/index.html.twig @@ -276,7 +276,6 @@
     {% trans "substitution HD" %} 
    -