From 1d8650dd9280797a08355db04ac0aaa6f43506dc Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Wed, 20 Jan 2016 11:57:22 +0100
Subject: [PATCH] dev #33383 sso redmine : refactoring

---
 .../admin/controllers/RedmineController.php   | 38 ++++++++-----------
 .../admin/views/scripts/redmine/index.phtml   | 10 ++++-
 library/Class/Bib.php                         |  8 +++-
 library/Class/WebService/Redmine.php          | 16 ++++++--
 library/Class/WebService/Redmine/Issue.php    | 11 +++++-
 .../WebService/Redmine/Workflow/Abstract.php  |  2 +
 .../WebService/Redmine/Workflow/Afibre.php    |  5 +++
 library/ZendAfi/Form/Redmine/Issue.php        |  9 ++++-
 .../View/Helper/Admin/MenuHorizontalAdmin.php |  1 +
 .../ZendAfi/View/Helper/Redmine/Issues.php    | 19 ++++++----
 .../ZendAfi/View/Helper/Redmine/Library.php   | 28 --------------
 .../controllers/RedmineControllerTest.php     |  6 +++
 12 files changed, 85 insertions(+), 68 deletions(-)
 delete mode 100644 library/ZendAfi/View/Helper/Redmine/Library.php

diff --git a/application/modules/admin/controllers/RedmineController.php b/application/modules/admin/controllers/RedmineController.php
index 64e04914912..d6a04c54a70 100644
--- a/application/modules/admin/controllers/RedmineController.php
+++ b/application/modules/admin/controllers/RedmineController.php
@@ -26,36 +26,30 @@ class Admin_RedmineController extends ZendAfi_Controller_Action {
     $this->view->titre = $this->_('Assistance');
     $this->view->user = $user = Class_Users::getIdentity();
 
-    if (!$libraries = $user->getRedmineLibraries())
-      return;
-
-    $this->view->libraries = $this->_librariesOptions($libraries);
 
-    if (!$id_bib = (int)$this->_getParam('id_bib', 0)) {
-      $this->view->library = $this->fetchLibraryDatas($libraries[0]);
+    $issues = Class_WebService_Redmine_Issues::forUser($user);
+    if (!$librairies = Class_Bib::byIdLabels($user->getRedmineLibraries()))
       return;
-    }
 
-    if (!$library = $this->_selectLibraryIn($id_bib, $libraries))
-      return;
+    if (!$id_bib = (int)$this->_getParam('id_bib', 0))
+      $id_bib = array_keys($librairies)[0];
 
-    $this->view->library = $this->fetchLibraryDatas($library);
-  }
+    $this->view->libraries = $librairies;
 
+    if (!isset($librairies[$id_bib])) {
+      $this->view->issues = new Class_WebService_Redmine_Issues();
+      return;
+    }
 
-  protected function _librariesOptions($libraries) {
-    $options = [];
-    foreach($libraries as $library)
-      $options[$library->getId()] = $library->getLabel();
-
-    return $options;
-  }
+    $issues_for_library = $issues->selectLibraryId($id_bib);
+    $library = Class_Bib::find($id_bib);
 
+    $this->view->library = $library;
+    $this->view->redmine_account = $issues_for_library->isEmpty()
+      ? (new Class_WebService_Redmine($library))->getUser()
+      : $issues_for_library->first()->getService()->getUser();
 
-  protected function _selectLibraryIn($id, $possibles) {
-    foreach($possibles as $possible)
-      if ($possible->getId() == $id)
-        return $possible;
+    $this->view->issues = $issues_for_library;
   }
 
 
diff --git a/application/modules/admin/views/scripts/redmine/index.phtml b/application/modules/admin/views/scripts/redmine/index.phtml
index f76fdd09948..1f5d9162260 100644
--- a/application/modules/admin/views/scripts/redmine/index.phtml
+++ b/application/modules/admin/views/scripts/redmine/index.phtml
@@ -5,5 +5,11 @@ if (!$this->libraries) {
   return;
 }
 
-if ($this->library)
-  echo $this->redmine_Library($this->library, $this->user, $this->libraries);
+if ($this->library) {
+  echo $this->redmine_Header($this->user,
+                             $this->library,
+                             $this->redmine_account,
+                             $this->libraries);
+
+  echo $this->redmine_Issues($this->library, $this->issues);
+}
diff --git a/library/Class/Bib.php b/library/Class/Bib.php
index c7054decd83..ba17122a9cf 100644
--- a/library/Class/Bib.php
+++ b/library/Class/Bib.php
@@ -140,13 +140,17 @@ class BibLoader extends Storm_Model_Loader {
 
 
   public function findAllLabels() {
+    return static::byIdLabels(Class_Bib::findAll());
+  }
+
+
+  public function byIdLabels($librairies) {
     $labels = [];
-    foreach (Class_bib::findAll() as $library) {
+    foreach ($librairies as $library) {
       $labels[$library->getId()]=$library->getLibelle();
     }
     return $labels;
   }
-
 }
 
 
diff --git a/library/Class/WebService/Redmine.php b/library/Class/WebService/Redmine.php
index fe2d641b2a4..6cbc609bad0 100644
--- a/library/Class/WebService/Redmine.php
+++ b/library/Class/WebService/Redmine.php
@@ -140,10 +140,18 @@ class Class_WebService_Redmine extends Class_WebService_Abstract {
     $params['project_id'] = Class_AdminVar::get('REDMINE_PROJECT_ID');
 
     $data = $this->getIssueApi()->all($params);
+    if (!isset($data['issues']))
+      return [];
 
-    return isset($data['issues'])
-      ? array_map(['Class_WebService_Redmine_Issue', 'newWith'], $data['issues'])
-      : [];
+    return array_map([$this, 'newIssue'],
+                     $data['issues']);
+
+  }
+
+
+  public function newIssue($datas) {
+    return Class_WebService_Redmine_Issue::newWith($datas)
+      ->setService($this);
   }
 
 
@@ -273,7 +281,7 @@ class Class_WebService_Redmine extends Class_WebService_Abstract {
   public function isIssueStatusClosed($id) {
     foreach($this->getIssueStatus() as $status)
       if ($status['id'] == $id)
-        return $status['is_closed'];
+        return isset($status['is_closed']) && $status['is_closed'];
 
     return false;
   }
diff --git a/library/Class/WebService/Redmine/Issue.php b/library/Class/WebService/Redmine/Issue.php
index e9b7c191b9e..a85a3fe26aa 100644
--- a/library/Class/WebService/Redmine/Issue.php
+++ b/library/Class/WebService/Redmine/Issue.php
@@ -40,7 +40,7 @@ class Class_WebService_Redmine_Issue extends Class_Entity {
       $custom_fields[] = ['id' => Class_WebService_Redmine::CUSTOM_QUALIFICATION,
                           'value' => $this->getDefaultQualification()];
 
-    return ['status_id' => $this->_attribs['status_id'],
+    return ['status_id' => $this->_attribs['status']['id'],
             'custom_fields' => $custom_fields,
             'notes' => $this->_attribs['notes']];
   }
@@ -75,6 +75,12 @@ class Class_WebService_Redmine_Issue extends Class_Entity {
     return null;
   }
 
+  public function getStatusId() {
+    return isset($this->_attribs['status']['id'])
+      ? $this->_attribs['status']['id']
+      : null;
+  }
+
 
   public function getPriority() {
     return $this->getCustomField(Class_WebService_Redmine::CUSTOM_PRIORITY_ID);
@@ -91,6 +97,7 @@ class Class_WebService_Redmine_Issue extends Class_Entity {
   }
 
 
+  /** @return Class_Bib */
   public function getLibrary() {
     return $this->getService()->getLibrary();
   }
@@ -112,7 +119,7 @@ class Class_WebService_Redmine_Issue extends Class_Entity {
   public function isClosed() {
     return $this->withServiceDo(
                                 function($service) {
-                                  return $service->isIssueStatusClosed($this->_attribs['status_id']);
+                                  return $service->isIssueStatusClosed($this->_attribs['status']['id']);
                                 });
   }
 
diff --git a/library/Class/WebService/Redmine/Workflow/Abstract.php b/library/Class/WebService/Redmine/Workflow/Abstract.php
index 44566503e05..5335ed95225 100644
--- a/library/Class/WebService/Redmine/Workflow/Abstract.php
+++ b/library/Class/WebService/Redmine/Workflow/Abstract.php
@@ -26,4 +26,6 @@ abstract class Class_WebService_Redmine_Workflow_Abstract {
   public function getFormSelectedStatusIdForCurrent($status_id) {
     return $status_id;
   }
+
+  abstract public function getNumberOfIssuesNotification();
 }
\ No newline at end of file
diff --git a/library/Class/WebService/Redmine/Workflow/Afibre.php b/library/Class/WebService/Redmine/Workflow/Afibre.php
index 19f58c7aa01..2c38ec9e67c 100644
--- a/library/Class/WebService/Redmine/Workflow/Afibre.php
+++ b/library/Class/WebService/Redmine/Workflow/Afibre.php
@@ -61,4 +61,9 @@ class Class_WebService_Redmine_Workflow_Afibre extends  Class_WebService_Redmine
   public function getFormSelectedStatusIdForCurrent($status_id) {
     return $status_id == self::QUESTION_CLIENT ? self::AFFECTE_HOTLINE : $status_id;
   }
+
+
+  public function getNumberOfIssuesNotification() {
+    return 1;
+  }
 }
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Redmine/Issue.php b/library/ZendAfi/Form/Redmine/Issue.php
index 928ea11dd74..af2f6e2dcb7 100644
--- a/library/ZendAfi/Form/Redmine/Issue.php
+++ b/library/ZendAfi/Form/Redmine/Issue.php
@@ -35,6 +35,13 @@ class ZendAfi_Form_Redmine_Issue extends ZendAfi_Form {
   }
 
 
+  public function getValues() {
+    $values = parent::getValues();
+    $values['status'] = ['id' => $values['status_id']];
+    return $values;
+  }
+
+
   protected function initIssue($issue) {
     $this->_issue = $issue;
     return $this;
@@ -173,7 +180,7 @@ class ZendAfi_Form_Redmine_Issue extends ZendAfi_Form {
 
 
   protected function addStatus() {
-    if (!$current = $this->_issue->getstatus()['id'])
+    if (!$current = $this->_issue->getStatusId())
       return $this;
 
     $possibles = [];
diff --git a/library/ZendAfi/View/Helper/Admin/MenuHorizontalAdmin.php b/library/ZendAfi/View/Helper/Admin/MenuHorizontalAdmin.php
index 38e03c7bff1..e67c2797e7d 100644
--- a/library/ZendAfi/View/Helper/Admin/MenuHorizontalAdmin.php
+++ b/library/ZendAfi/View/Helper/Admin/MenuHorizontalAdmin.php
@@ -27,6 +27,7 @@ class ZendAfi_View_Helper_Admin_MenuHorizontalAdmin extends ZendAfi_View_Helper_
     $redmine = Class_AdminVar::isRedmineEnabled()
       ? ['icon' => 'redmine_16.png',
          'label' => $this->_('Assistance'),
+         'count' => Class_WebService_Redmine_Workflow::current()->getNumberOfIssuesNotification(),
          'url' => $this->view->url(['module' => 'admin',
                                     'controller' => 'redmine',
                                     'action' => 'index'], null, true)]
diff --git a/library/ZendAfi/View/Helper/Redmine/Issues.php b/library/ZendAfi/View/Helper/Redmine/Issues.php
index 11c9c76b7f9..85d8bc4f5c6 100644
--- a/library/ZendAfi/View/Helper/Redmine/Issues.php
+++ b/library/ZendAfi/View/Helper/Redmine/Issues.php
@@ -21,12 +21,17 @@
 
 
 class ZendAfi_View_Helper_Redmine_Issues extends ZendAfi_View_Helper_BaseHelper {
-  public function Redmine_Issues($library) {
-    if (!Class_AdminVar::isRedmineEnabled() || !$library)
+  /**
+   * @param $library Class_Bib
+   * @param $issues Class_WebService_Redmine_Issues
+   */
+  public function Redmine_Issues($library, $issues) {
+    if (!Class_AdminVar::isRedmineEnabled() || !$issues)
       return '';
 
-    return $this->renderIssues($library, $library->getIssues(), $this->_('Demandes en cours'))
-      . $this->renderIssues($library, $library->getClosedIssues(), $this->_('Demandes fermées'));
+    return
+      $this->renderIssues($library, $issues->selectNotClosed(), $this->_('Demandes en cours'))
+      . $this->renderIssues($library, $issues->selectClosed(), $this->_('Demandes fermées'));
   }
 
 
@@ -34,11 +39,11 @@ class ZendAfi_View_Helper_Redmine_Issues extends ZendAfi_View_Helper_BaseHelper
     if (!$issues)
       return '';
 
-    $editAction = function($issue) use($library) {
+    $editAction = function($issue) {
       return $this->view->tagAnchor(['module' => 'admin',
                                      'controller' => 'redmine',
                                      'action' => 'edit-issue',
-                                     'id_lib' => $library->getLibrary()->getId(),
+                                     'id_lib' => $issue->getLibrary()->getId(),
                                      'id' => $issue->getid()],
                                     $this->view->boutonIco('type=edit'));
     };
@@ -60,7 +65,7 @@ class ZendAfi_View_Helper_Redmine_Issues extends ZendAfi_View_Helper_BaseHelper
 
                                    [$editAction],
 
-                                   'issues-' . $library->getLibrary()->getId(),
+                                   'issues-' . $library->getId(),
                                    null,
 
                                    ['status' => $status_renderer,
diff --git a/library/ZendAfi/View/Helper/Redmine/Library.php b/library/ZendAfi/View/Helper/Redmine/Library.php
deleted file mode 100644
index 7515953d320..00000000000
--- a/library/ZendAfi/View/Helper/Redmine/Library.php
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-/**
- * Copyright (c) 2012-2014, 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_View_Helper_Redmine_Library extends ZendAfi_View_Helper_BaseHelper {
-  public function redmine_Library($library, $user, $others=[]) {
-    return $this->view->redmine_Header($user, $library->getLibrary(), $library->getUser(), $others)
-      . $this->view->redmine_Issues($library);
-  }
-}
\ No newline at end of file
diff --git a/tests/application/modules/admin/controllers/RedmineControllerTest.php b/tests/application/modules/admin/controllers/RedmineControllerTest.php
index ae352b4a8b4..65840a1ba2b 100644
--- a/tests/application/modules/admin/controllers/RedmineControllerTest.php
+++ b/tests/application/modules/admin/controllers/RedmineControllerTest.php
@@ -293,6 +293,12 @@ class Admin_RedmineControllerIndexTest extends Admin_RedmineControllerWithApiTes
   public function anchorToEditCurrentUserShouldBePresent() {
     $this->assertXPath('//div[@class="modules"]//a[contains(@href, "admin/users/edit/id/")]');
   }
+
+
+  /** @test */
+  public function menuHorizontalShouldContainsOneIssueQuestionClient() {
+    $this->assertXPathContentContains('//div[contains(@class, "barre_nav")]//a[@title="Assistance"]//span[@class="menu_info"]', '1');
+  }
 }
 
 
-- 
GitLab