Daniel@0: /* Part of DML (Digital Music Laboratory) Daniel@0: Copyright 2014-2015 Samer Abdallah, University of London Daniel@0: Daniel@0: This program is free software; you can redistribute it and/or Daniel@0: modify it under the terms of the GNU General Public License Daniel@0: as published by the Free Software Foundation; either version 2 Daniel@0: of the License, or (at your option) any later version. Daniel@0: Daniel@0: This program is distributed in the hope that it will be useful, Daniel@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: GNU General Public License for more details. Daniel@0: Daniel@0: You should have received a copy of the GNU General Public Daniel@0: License along with this library; if not, write to the Free Software Daniel@0: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: */ Daniel@0: Daniel@0: :- module(csv_ui, []). Daniel@0: Daniel@0: /** UI for viewing CSV files Daniel@0: */ Daniel@0: :- use_module(library(http/html_write)). Daniel@0: :- use_module(library(http/html_head)). Daniel@0: :- use_module(library(http/http_dispatch)). Daniel@0: :- use_module(library(http/http_parameters)). Daniel@0: :- use_module(library(decoration)). Daniel@0: :- use_module(library(htmlutils), [paginator//3]). Daniel@0: :- use_module(library(csvutils), [uri_to_csv/2]). Daniel@0: :- use_module(library(listutils), [drop/3,take/3]). Daniel@0: :- use_module(components(table), [table_from_goal//2]). Daniel@0: :- use_module(components(icons)). Daniel@0: :- use_module(cliopatria(hooks)). Daniel@0: Daniel@0: :- set_prolog_flag(double_quotes,string). Daniel@0: Daniel@0: :- http_handler(root(dml/csv/view), csv_view, []). Daniel@0: Daniel@0: decoration:resource_view(URI,_) --> Daniel@0: { sub_string(URI,_,_,0,".csv") }, Daniel@0: { http_link_to_id(archive_get,[uri(URI)],EntryURL) }, Daniel@0: { http_link_to_id(csv_view,[uri(URI)],ViewURL) }, Daniel@0: html_requires(font_awesome), Daniel@0: html([ a(href=EntryURL, [\icon(download)," Download"]), &('MediumSpace') Daniel@0: , a(href=ViewURL, [\icon(table)," View"])]). Daniel@0: Daniel@0: decoration:resource_decoration(URI,Link) --> Daniel@0: { sub_string(URI,_,_,0,".csv") }, !, Daniel@0: { http_link_to_id(csv_view,[uri(URI)],ViewURL) }, Daniel@0: html_requires(font_awesome), Daniel@0: html( span( [ a(href(ViewURL),\icon(table)) Daniel@0: , &(nbsp), \Link Daniel@0: ])). Daniel@0: Daniel@0: csv_view(Request) :- Daniel@0: http_parameters(Request, Daniel@0: [ uri(URI, [ optional(false), description("URI of CSV file")]) Daniel@0: , page(Page, [ nonneg, default(1) ]) Daniel@0: , limit(Limit, [ nonneg, default(50) ]) Daniel@0: ]), Daniel@0: uri_to_csv(URI,Rows), Daniel@0: length(Rows,Total), Daniel@0: Offset is Limit*(Page-1), Daniel@0: Pages is ceil(Total/Limit), Daniel@0: insist(Page= Rows1=Rows2; take(Limit,Rows1,Rows2)), Daniel@0: format(string(FullTitle),"CSV view for ~w",[URI]), Daniel@0: reply_html_page(cliopatria(demo), [title(FullTitle)], Daniel@0: [ h1(FullTitle) Daniel@0: , \paginator(csv_view-[uri(URI),limit(Limit)],Page,Pages) Daniel@0: , \table_from_goal(csv_row(Rows2),[]) Daniel@0: , \paginator(csv_view-[uri(URI),limit(Limit)],Page,Pages) Daniel@0: ]). Daniel@0: Daniel@0: csv_row(Rows,Row) :- member(R,Rows), R=..[_|Row]. Daniel@0: