From 728bb364b5eb638a17aaf333c3d438efecfa3330 Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Mon, 27 Nov 2017 17:39:49 +0100
Subject: [PATCH] dev #65267 add upload trailer

---
 FEATURES/65267                                | 10 +++++
 VERSIONS_WIP/65267                            |  1 +
 .../admin/controllers/RecordsController.php   | 19 ++++++++++
 .../admin/views/scripts/records/trailer.phtml |  5 +++
 .../opac/controllers/NoticeajaxController.php | 19 +++++++++-
 library/Class/WebService/AllServices.php      | 18 +++++++++
 library/ZendAfi/Form/RecordTrailer.php        | 38 +++++++++++++++++++
 .../controllers/NoticeAjaxControllerTest.php  |  2 +
 8 files changed, 110 insertions(+), 2 deletions(-)
 create mode 100644 FEATURES/65267
 create mode 100644 VERSIONS_WIP/65267
 create mode 100644 application/modules/admin/views/scripts/records/trailer.phtml
 create mode 100644 library/ZendAfi/Form/RecordTrailer.php

diff --git a/FEATURES/65267 b/FEATURES/65267
new file mode 100644
index 00000000000..082a91a4add
--- /dev/null
+++ b/FEATURES/65267
@@ -0,0 +1,10 @@
+        '65267' =>
+            ['Label' => $this->_('[Bonus] Modération des vidéos'),
+             'Desc' => '',
+             'Image' => '',
+             'Video' => '',
+             'Category' => '',
+             'Right' => function($feature_description, $user) {return true;},
+             'Wiki' => '',
+             'Test' => '',
+             'Date' => '2017-11-27'],
\ No newline at end of file
diff --git a/VERSIONS_WIP/65267 b/VERSIONS_WIP/65267
new file mode 100644
index 00000000000..923ecb1bfe3
--- /dev/null
+++ b/VERSIONS_WIP/65267
@@ -0,0 +1 @@
+ - ticket #65267 : [Bonus] Modération des vidéos
\ No newline at end of file
diff --git a/application/modules/admin/controllers/RecordsController.php b/application/modules/admin/controllers/RecordsController.php
index d7493bf81de..3fa0e102c2b 100644
--- a/application/modules/admin/controllers/RecordsController.php
+++ b/application/modules/admin/controllers/RecordsController.php
@@ -77,4 +77,23 @@ class Admin_RecordsController extends ZendAfi_Controller_Action {
 
     $this->view->form = $form;
   }
+
+
+  public function trailerAction() {
+    $this->view->titre = $this->_('Modifier la bande-annonce');
+
+    $form = new ZendAfi_Form_RecordTrailer(['action' => $this->view->url()]);
+
+    if ($this->_request->isPost()
+        && $form->isValid($this->_request->getPost())) {
+      if (!$this->view->error = Class_WebService_AllServices::uploadTrailerForRecord($this->_getParam('trailer_url'),
+                                                                                     $this->_getParam('id'))) {
+        $this->_helper->notify($this->_('La bande-annonce a bien été mise à jour'));
+        $this->_redirectClose($this->_getReferer());
+        return;
+      }
+    }
+
+    $this->view->form = $form;
+  }
 }
diff --git a/application/modules/admin/views/scripts/records/trailer.phtml b/application/modules/admin/views/scripts/records/trailer.phtml
new file mode 100644
index 00000000000..1ceea3e2444
--- /dev/null
+++ b/application/modules/admin/views/scripts/records/trailer.phtml
@@ -0,0 +1,5 @@
+<?php
+if ($this->error)
+  echo $this->tag('p',  $this->error, ['class' => 'error']);
+
+echo $this->renderForm($this->form);
diff --git a/application/modules/opac/controllers/NoticeajaxController.php b/application/modules/opac/controllers/NoticeajaxController.php
index 0279a3446a9..dc24a8ac67a 100644
--- a/application/modules/opac/controllers/NoticeajaxController.php
+++ b/application/modules/opac/controllers/NoticeajaxController.php
@@ -227,6 +227,8 @@ class NoticeAjaxController extends Zend_Controller_Action {
 
       $args= ['titre' => $this->notice->getRecordTitle(),
               'auteur' => $this->notice->getAuteurPrincipal(),
+              'clef_oeuvre' => $this->notice->getClefOeuvre(),
+              'type_doc' => $this->notice->getFamilleId(),
               'width' => $width,
               'height' => $height];
 
@@ -235,9 +237,22 @@ class NoticeAjaxController extends Zend_Controller_Action {
       $bo=$data["player"];
       $html=$this->notice_html->getBandeAnnonce($source,$bo);
     }
-    else
-      $html= $html=$this->notice_html->getNonTrouve($this->view->_("Service non disponible"),true);
+    else {
+      $html = $this->notice_html->getNonTrouve($this->view->_("Service non disponible"),true);
+    }
+
+    if (Class_Users::isCurrentUserCanAccesBackend()) {
+      $url = $this->view->url(['module' => 'admin',
+                               'controller' => 'records',
+                               'action' => 'trailer',
+                               'id' => $this->notice->getId()]);
+
+      $html = $html . $this->view->tag('a', $this->_('Modifier la bande-annonce'),
+                                       ['href' => $url, 'data-popup' => 'true']);
+    }
+
     $this->_sendResponseWithScripts($html);
+
   }
 
 
diff --git a/library/Class/WebService/AllServices.php b/library/Class/WebService/AllServices.php
index 92e62e910d3..13bd7d46b41 100644
--- a/library/Class/WebService/AllServices.php
+++ b/library/Class/WebService/AllServices.php
@@ -97,6 +97,11 @@ class Class_WebService_AllServices {
   }
 
 
+  public static function runServiceAfiUploadTrailer($args) {
+    return self::runServiceAfi(14, $args);
+  }
+
+
   public static function runServiceGetUrlVignette($args) {
     return self::runServiceAfi(10, $args);
   }
@@ -138,6 +143,19 @@ class Class_WebService_AllServices {
   }
 
 
+
+  public static function uploadTrailerForRecord($url, $id) {
+    $notice = Class_Notice::find($id);
+    $result = static::runServiceAfiUploadTrailer(array_filter(['trailer_url' => $url,
+                                                               'clef_oeuvre' => $notice->getClefOeuvre(),
+                                                               'type_doc' => $notice->getFamilleId()]));
+
+    if (self::RETOUR_SERVICE_OK != $result['statut_recherche'])
+      return $result['message'];
+  }
+
+
+
   public static function httpGet($url, $args) {
     if (!isset(self::$_http_client))
       self::$_http_client = new Class_WebService_SimpleWebClient();
diff --git a/library/ZendAfi/Form/RecordTrailer.php b/library/ZendAfi/Form/RecordTrailer.php
new file mode 100644
index 00000000000..193930f7475
--- /dev/null
+++ b/library/ZendAfi/Form/RecordTrailer.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+class ZendAfi_Form_RecordTrailer extends ZendAfi_Form {
+  public function init() {
+    parent::init();
+    $this
+      ->addElement('url',
+                   'trailer_url',
+                   ['label' => $this->_('URL de la bande-annonce'),
+                    'size' => 80,
+                    'required' => true,
+                    'placeholder' => 'ex: https://youtu.be/AUI12X'])
+      ->addElement('submit',
+                   'envoyer',
+                   ['label' => 'Envoyer']);
+  }
+}
+
+?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php
index 7b14ee991a9..5d08c45e34d 100644
--- a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php
+++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php
@@ -467,6 +467,8 @@ class NoticeAjaxControllerBandeAnnonceTest extends AbstractControllerTestCase {
                                                 Storm_Test_ObjectWrapper::mock()
                                                 ->whenCalled('open_url')
                                                 ->answers(json_encode(['source' => 'Testing', 'player' => 'toto'])));
+        $admin = Class_Users::newInstanceWithId(15,['login'=>'admin'])->beAdminPortail();
+    Zend_Auth::getInstance()->logUser($admin);
     $this->dispatch('noticeajax/bandeannonce', true);
     $this->assertXPathContentContains('//h3', 'Source : Testing', $this->_response->getBody());
   }
-- 
GitLab