<%
# encoding: utf-8
#
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2011    Vít Jonáš <vit.jonas@gmail.com>
# Copyright (C) 2012    Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2011-16 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<% html_title(l(:dmsf)) %>

<div class="contextual">
  <% if User.current.allowed_to?(:file_manipulation, @project) %>
    <% unless @file.locked_for_user? %>
      <% unless @file.locked? %>
        <%= link_to(l(:button_lock), lock_dmsf_files_path(:id => @file),
          :title => l(:title_lock_file), :class => 'icon dmsf_icon-lock') %>
      <% else %>
        <%= link_to_if(@file.unlockable?, l(:button_unlock), unlock_dmsf_files_path(:id => @file),
          :title => l(:title_unlock_file), :class => 'icon dmsf_icon-unlock') %>
      <% end %>
      <% if @file.notification %>
        <%= link_to(l(:label_notifications_off),
          notify_deactivate_dmsf_files_path(:id => @file),
          :title => l(:title_notifications_active_deactivate),
          :class => 'icon dmsf_icon-notification-on') %>
      <% else %>
        <%= link_to(l(:label_notifications_on),
          notify_activate_dmsf_files_path(:id => @file),
          :title => l(:title_notifications_not_active_activate),
          :class => 'icon dmsf_icon-notification-off') %>
      <% end %>
      <%= link_to(l(:label_link_to),
        new_dmsf_link_path(:project_id => @project.id, :dmsf_folder_id => @file.dmsf_folder ? @file.dmsf_folder.id : nil, :dmsf_file_id => @file.id, :type => 'link_to'),
        :title => l(:title_create_link),
        :class => 'icon dmsf_icon-link') %>
      <%= link_to("#{l(:button_copy)}/#{l(:button_move)}", copy_file_path(:id => @file),
        :title => l(:title_copy), :class => 'icon icon-copy') %>
      <%= delete_link @file if @file_delete_allowed %>
    <% else %>
      <% if User.current.allowed_to?(:force_file_unlock, @project) %>
        <%= link_to_if(@file.unlockable?, l(:button_unlock), unlock_dmsf_files_path(:id => @file),
          :title => l(:title_unlock_file), :class => 'icon dmsf_icon-unlock')%>
      <% end %>
    <% end %>
  <% end %>
</div>

<%= render(:partial => '/dmsf/path',
  :locals => {:folder => @file.dmsf_folder, :filename => @file.title, :title => nil}) %>

<% if User.current.allowed_to?(:file_manipulation, @file.project) && !@file.locked_for_user? %>
  <%= error_messages_for('file') %>
  <%= error_messages_for('revision') %>
  <%= render(:partial => 'file_new_revision') %>
<% end %>

<div class="dmsf_id_box tabular">
  <%= label_tag('', l(:label_document)) %>
  <%= "##{@file.id}" %>
</div>

<h3><%= l(:heading_revisions) %></h3>
<% @file.dmsf_file_revisions.visible[@revision_pages.offset, @revision_pages.per_page].each do |revision| %>
  <div class="dmsf_revision_box tabular dataTables_wrapper">
    <div class="fg-toolbar ui-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix">
      <div class="dataTables_lenght">
        <div class="dmsf_controls" style="float: right">
          <%= link_to_function image_tag('rev_downloads.png', :plugin => 'redmine_dmsf'),
            "$('#revision_access-#{revision.id}').toggle()",
            :title => l(:title_download_entries) %>
          <%= link_to image_tag('rev_download.png', :plugin => 'redmine_dmsf'),
            dmsf_file_path(@file, :download => revision),
            :title => l(:title_title_version_version_download, :title => h(revision.title), :version => revision.version) %>
          <%= link_to image_tag('rev_delete.png', :plugin => 'redmine_dmsf'),
            delete_revision_path(revision),
            :data => {:confirm => l(:text_are_you_sure)},
            :title => l(:title_delete_revision) if @file_delete_allowed && (@file.dmsf_file_revisions.visible.count > 1) %>
        </div>
        <i><%= l(:info_revision, :rev => revision.id) %></i>
        <%= (revision.source_revision.nil? ? l(:label_created) : l(:label_changed)).downcase %>
        <%= l(:info_changed_by_user, :changed => format_time(revision.updated_at)) %>
        <%= link_to(revision.user.name, user_path(revision.user)) if revision.user %>
      </div>
    </div>
    <div class="attributes dmsf_revision_inner_box">
      <div class="splitcontent">
        <div class="splitcontentleft">
          <p>
            <%= label_tag('', l(:label_title)) %>
            <%= h(revision.title) %>
          </p>
          <% if revision.description.present? %>
            <p>
              <%= label_tag('', l(:label_description)) %>
              <div class="wiki">
                <%= textilizable(revision.description) %>
              </div>
            </p>
          <% end %>
          <p>
            <%= label_tag('', l(:label_dmsf_version)) %>
            <%= revision.major_version %>.<%= revision.minor_version %>
          </p>
          <p>
            <%= label_tag('', l(:label_size)) %>
            <%= number_to_human_size(revision.size) %>
          </p>
          <% wf = DmsfWorkflow.find_by_id(revision.dmsf_workflow_id) %>
          <% if wf %>
            <p>
              <%= label_tag('', l(:link_workflow)) %>
              <%= "#{wf.name} - " %>
              <%= link_to(revision.workflow_str(false),
                log_dmsf_workflow_path(:project_id => @project.id,
                  :id => wf.id, :dmsf_file_revision_id => revision.id),
                :title => DmsfWorkflow.assignments_to_users_str(wf.next_assignments(revision.id)),
                :remote => true) %>
            </p>
          <% end %>
          <% if revision.comment.present? %>
            <p>
              <%= label_tag('', l(:label_comment)) %>
              <div class="wiki">
                <%= textilizable(revision.comment) %>
              </div>
            </p>
          <% end %>
        </div>
        <div class="splitcontentright">
          <p>
            <%= label_tag('', l(:label_file)) %>
            <%= (h("#{revision.dmsf_file.dmsf_folder.dmsf_path_str}/")) if revision.dmsf_file.dmsf_folder %><%= h(revision.name) %>
          </p>
          <p>
            <%= label_tag('', l(:label_mime)) %>
            <%= h(revision.mime_type) %>
          </p>
          <% if revision.digest.present? %>
            <p>
              <%= label_tag('', 'MD5') %>
              <%= revision.digest %>
            </p>
          <% end %>
          <%= render 'dmsf/custom_fields', :object => revision %>
        </div>
      </div>
      <div id="<%= "revision_access-#{revision.id}" %>" style="display:none">
        <%= render(:partial => 'revision_access', :locals => {:revision => revision}) if User.current.allowed_to?(:file_manipulation, @file.project) %>
      </div>
    </div>
  </div>
  <br/>
<% end %>

<span class="pagination"><%= pagination_links_full @revision_pages, @file.dmsf_file_revisions.visible.count %></span>

<script type="text/javascript">
  $('a.delete-revision').click(function(event) {
    if(!window.confirm('<%= l(:text_are_you_sure) %>')) {
      event.preventDefault();
    }
  });

  $('a.delete-entry').click(function(event) {
    if(!window.confirm('<%= l(:text_are_you_sure) %>')) {
      event.preventDefault();
    }
  });

  $('#file_upload').change(function() {
    if($("input[name='version']:checked").val() == '0') {
      $('#fileMinorVersionRadio').prop('checked', true);
    }
    $('#fileSameVersionRadio').prop('disabled', true);
  });

  $('#newRevisionFormContentToggle').click(function() {
    if($('#newRevisionFormContent').is(':visible')) {
      $(this).text('[+]');
      $('#newRevisionFormContent').hide();
    }
    else {
      $(this).text('[-]');
      $('#newRevisionFormContent').show();
    }
  });

  $('.dmsf_list').dataTable({
    'bJQueryUI': true,
    'oLanguage': {
      'sUrl': "<%= plugin_asset_path(:redmine_dmsf, 'javascripts', json_url) %>"
    }
  });
</script>
<% if @revision.valid? && @file.valid? %>
  <script type="text/javascript">
    $('#newRevisionFormContentToggle').text('[+]');
    $('#newRevisionFormContent').hide();
  </script>
<% end %>

<% content_for :header_tags do %>
  <%= stylesheet_link_tag 'jquery.dataTables/jquery-ui.dataTables.css', :plugin => 'redmine_dmsf' %>
  <%= javascript_include_tag 'jquery.dataTables/jquery.dataTables.min.js', :plugin => 'redmine_dmsf' %>
<% end %>