<?php
/**
 * Copyright (c) 2012-2019, 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 Intonation_View_RenderTrailers extends ZendAfi_View_Helper_BaseHelper {
  public function renderTrailers($trailers) {
    if ($trailers->isEmpty())
      return '';

    $html = [];

    foreach ($trailers as $trailer) {
      $html [] = $this->_renderEdit($trailer)
        . $this->_toggleTrailer($trailer)
        . $this->_renderTrailer($trailer);
    }

    return $this->_renderTrailers($html);
  }


  protected function _renderTrailer($trailer) {
    return $trailer->isDisabled()
      ? ''
      : $this->view->renderEmbed($trailer->getSource(),
                                 $trailer->getUrl());
  }


  protected function _renderTrailers($html) {
    return implode($html);
  }


  protected function _renderEdit($trailer) {
    if (!Class_Users::isCurrentUserCanAccesBackend())
      return '';

    if ($trailer->isDisabled())
      return '';

    $url = $this->view->url(['module' => 'admin',
                             'controller' => 'records',
                             'action' => 'trailer',
                             'id' => $trailer->getModel()->getId()]);

    return $this->view
      ->Admin_Button((new Class_Button)
                     ->setText($this->_('Modifier la bande-annonce'))
                     ->setUrl($url)
                     ->setAttribs(['data-popup' => 'true'])
                     ->setImage(Class_Admin_Skin::current()
                                ->renderButtonIconOn('configuration', $this->view)));
  }


  protected function _toggleTrailer($trailer) {
    if (!Class_Users::isCurrentUserCanAccesBackend())
      return '';

    return $trailer->isDisabled()
      ? $this->_renderEnableTrailer($trailer)
      : $this->_renderDisableTrailer($trailer);
  }


  protected function _renderDisableTrailer($trailer) {
    $url = $this->view->url(['module' => 'admin',
                             'controller' => 'records',
                             'action' => 'disable-trailer',
                             'id' => $trailer->getModel()->getId()]);

    return $this->view
      ->Admin_Button((new Class_Button)
                     ->setText($this->_('Désactiver la bande-annonce'))
                     ->setUrl($url)
                     ->setAttribs(['data-popup' => 'true'])
                     ->setImage(Class_Admin_Skin::current()
                                ->renderButtonIconOn('inactive', $this->view)));
  }


  protected function _renderEnableTrailer($trailer) {
    $url = $this->view->url(['module' => 'admin',
                             'controller' => 'records',
                             'action' => 'enable-trailer',
                             'id' => $trailer->getModel()->getId()]);

    return $this->view
      ->Admin_Button((new Class_Button)
                     ->setText($this->_('Activer la bande-annonce'))
                     ->setUrl($url)
                     ->setAttribs(['data-popup' => 'true'])
                     ->setImage(Class_Admin_Skin::current()
                                ->renderButtonIconOn('active', $this->view)));
  }
}