# -*- encoding : utf-8 -*-
# Wiki UFC
# Copyright (C) 2007, Adriano, Alinson, Andre, Rafael e Bustamante
#
# This program 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, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
module AttachmentsHelper
def attachments_to_nested_hash(atts)
paths = atts.collect { |item| item.path.nil? ? [] : item.path.split("/") }
return nest_path(atts, paths, 0, paths.size-1, 0)
end
def nest_path(items, paths, from, to, level)
result = {}
base = from - 1
base = base + 1 while base+1 <= to and paths[base+1][level].nil?
if base >= from then
result['/'] = items[from..base]
end
start = base+1
return result if start > to
folder = paths[start][level]
(base+1).upto(to) do |i|
if paths[i][level] != folder
result[folder] = nest_path(items, paths, start, i-1, level+1)
start = i
folder = paths[i][level]
end
end
if start <= to then
result[folder] = nest_path(items, paths, start, to, level+1)
end
return result
end
def nested_attachments_to_html(atts, level=0)
out = (level > 0 ? "
" : "")
keys = atts.keys.sort
for att in atts['/'] do
out = out + "- #{link_to h(att.file_name), course_attachment_url(@course, att)}
"
end if atts['/']
for key in keys - ['/'] do
out = out + "- #{link_to h(key), '#'}
"
out = out + nested_attachments_to_html(atts[key], level+1)
end
out = out + "
"
end
end