blob: 5fc34c83141b01d2f626b22b0321ed9b49934573 [file] [log] [blame]
# Methods added to this helper will be available to all templates in the application.
#--######################################################################
# Copyright (c) 2006 LogicaCMG
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#
# Onno van der Straaten:: initial implementation
#++######################################################################
# {Copyright (c) 2006 LogicaCMG}[link:files/COPYRIGHT.html]
#--
# TODO formatting.. RadRails screws up formatting of this code
#++
module ApplicationHelper
class TabularFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(radio_button hidden_field)).each do |selector|
src = <<-END_SRC
def #{selector}(field, options = {})
@template.content_tag("tr",
@template.content_tag("th", field.to_s.humanize, :width => 200) +
@template.content_tag("td", super))
end
END_SRC
class_eval src, __FILE__, __LINE__
end
end
def tabular_form_for(name, object = nil, options = nil, &proc)
if @table_heading
concat("<div class='sectionHeading'>#{@table_heading}</div>", proc.binding)
else
concat("<div class='sectionHeading'>#{name.to_s.humanize}</div>", proc.binding)
end
concat("<div class=\"sectionContent\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"sectionTable\"><tr valign=\"top\"><td class=\"sectionTableCell\"><table>",
proc.binding)
form_for(name,
object,
(options||{}).merge(:builder => TabularFormBuilder),
&proc)
concat("</table></td></tr></table></div>", proc.binding)
end
def url_for_site(site)
returning url = [] do
if site.baseline_process?
url << url_for("/#{ENV['EPFWIKI_SITES_FOLDER']}/#{site.folder}/")
else
url << url_for("/#{ENV['EPFWIKI_WIKIS_FOLDER']}/#{site.folder}/")
end
end.join("\n")
end
# call #tinymce in your view to render textarea's as TinyMCE textarea's
def tinymce(theme = 'simple')
@tinymce = theme
end
# helper #link_to_versions generates a link to the versions (version history)
# of the specified Page and Site.
def link_to_versions(site, page, text = '', popup=false)
if page.versions_count > 0
all_versions = Version.find_history_excl_baseversions1(page)
if all_versions.length > 0
versions = Version.find_history_excl_baseversions2(site, page)
title = "No versions exist in site \"#{site.title}\" but #{pluralize(all_versions.length, 'version')} exist in another site or baseline"
img = "version_zw"
if versions.length > 0
title = "#{pluralize(versions.length, 'version')} of \"#{page.presentation_name}\" exist in \"#{site.title}\""
img = "version"
end
return link_to(image_tag(img, :border => 0, :title => title, :align => 'middle') + ' ' + text, {:controller => 'versions', :action => 'list', :site_id => site, :page_id => page}, :popup => popup, :title => title)
end
end
end
# render link to Comments about a Page in a Site
def link_to_comments(site, page, text = '' ,popup=false)
returning url = [] do
if page.comments_count > 0
comments = page.comments.find_all_by_site_id_and_baseline_id( site.id, site.baseline.id)
title = "There are no comments for the current site but there are some for another site and/or baseline"
img = "comment2_zw"
if comments.length > 0
title = "#{pluralize(comments.length, 'comment')} of \"#{page.presentation_name}\" exist in \"#{site.title}\""
img = "comment2"
end
url << link_to(image_tag(img, :border => 0, :title => title, :align => 'middle') + ' '+ text, {:controller => "comments", :action => "list", :site_id => site, :page_id => page}, :popup => popup, :title => title)
end
end
end
# #link_to_difference_analysis helper for a DifferenceAnalysis-record
def link_to_difference_analysis_from(differenceAnalysis)
returning html = [] do
html << "From " + link_to_baseline(differenceAnalysis.baseline_from,nil)
html << " to " + link_to_baseline(differenceAnalysis.baseline,nil)
html << ", [" + link_to("View results", :controller => 'difference_analyses', :action =>'show', :id => differenceAnalysis.id) + "]"
end.join("\n")
end
# #link_to_difference_analysis_item helper for a DifferenceAnalysisItem-record
def link_to_difference_analysis_item(differenceAnalysisItem)
differenceAnalysis = differenceAnalysisItem.difference_analysis
site = differenceAnalysis.site
site_from = differenceAnalysis.site_from
baseline = differenceAnalysis.baseline
baseline_from = differenceAnalysis.baseline_from
page = differenceAnalysisItem.page
returning html = [] do
if differenceAnalysisItem.result == "NEW" || differenceAnalysisItem.result == "CHANGED" || differenceAnalysisItem.result == "EQUAL"
html << link_to_page(site, page)
elsif differenceAnalysisItem.result == "REMOVED"
html << link_to_page(site_from, page)
end
html << link_to(image_tag("compare", :border => 0, :title => "Show changes in " + @page.presentation_name, :align => 'middle'), {:controller => "difference_analyses", :action => "show_item", :item_id => differenceAnalysisItem}) if differenceAnalysisItem.result == "CHANGED"
html << link_to(image_tag("compare", :border => 0, :title => "Show changes in " + @page.presentation_name, :align => 'middle'), {:controller => "difference_analyses", :action => "show_item", :item_id => differenceAnalysisItem, :tab => "html"}) if differenceAnalysisItem.result == "HTMLCHANGED"
html << differenceAnalysisItem.result.downcase + " from baseline " + link_to_baseline(baseline_from, nil) + " to " + link_to_baseline(baseline, nil)
end.join("\n")
end
# #link_to_baseline helper for a Baseline-record
def link_to_baseline(baseline, site)
returning url = [] do
if site
url << link_to(baseline.baseline, :controller => 'baselines', :action => 'show', :id => baseline.id, :site_id => site.id)
else
url << link_to(baseline.baseline, :controller => 'baselines', :action => 'show', :id => baseline.id)
end
end.join("\n")
end
def link_to_notification_toggle(site, page, notification_type)
returning html = [] do
div_id = "notification_" + site.id.to_s + "_" + page.id.to_s + "_" + notification_type
html << "<span id=\"" + div_id + "\">"
notification = Notification.find(:first, :conditions => ["user_id=? and site_id=? and page_id=? and notification_type=?", session['user'].id, site.id, page.id, notification_type])
if notification_type == "Comment"
txt = "<input type=checkbox>notify me of follow-up comments"
txt = "<input type=checkbox checked>notify me of follow-up comments" if notification
elsif notification_type == "Version"
txt = "<input type=checkbox>notify me of new versions"
txt = "<input type=checkbox checked>notify me of new versions" if notification
end
html << link_to_remote(txt, :update => div_id, :url => {:controller => "users", :action => "notification", :page_id => @page.id, :site_id => site.id,:notification_type=> notification_type}, :post => true)
html << "</span>"
end.join("\n")
end
def link_to_change_report_notification_toggle(type = 'D')
returning html = [] do
user = User.find(session['user'].id)
div_id = "change_report_" + user.id.to_s + "_" + type
checked = ""
checked = "checked" if (type == 'D' && user.notify_daily == 1) || (type == 'M' && user.notify_monthly == 1) || (type == 'W' && user.notify_weekly == 1)
#checked = "checked" if type == 'M' && user.notify_monthly == "1"
#checked = "checked" if type == 'D' && user.notify_weekly == "1"
txt = "<input type=checkbox #{checked}> Daily" if type == 'D'
txt = "<input type=checkbox #{checked}> Monthly" if type == 'M'
txt = "<input type=checkbox #{checked}> Weekly" if type == 'W'
html << "<span id=\"" + div_id + "\">"
html << link_to_remote(txt, :update => div_id, :url => {:controller => "users", :action => "toggle_change_report_notification", :type => type}, :post => true)
html << "</span>"
end.join("\n")
end
def link_to_notification(notification)
site = notification.site
page = notification.page
txt = "follow-up comments on "
txt = "new versions of " if notification.notification_type == "Version"
returning html = [] do
html << "<span id=\"" + div_id(notification,"") + "\">"
html << txt.capitalize + link_to_page(site, page) + " in site " + link_to_site(site) + " "
html << link_to_remote(image_tag("delete", :border => 0, :title => "Don't notify me of " + txt + page.presentation_name), :update=>div_id(notification, ""), :url => {:controller => "users", :action => "destroy_notification", :notification_id => notification.id}, :post => true) if mine?(notification)
html << "</span>"
end.join("\n")
end
def link_to_done_toggle(record)
returning html = [] do
html << "<span id=\"" + div_id(record, "done_toggle") + "\">"
if record.done == 'Y'
title = 'Click to mark this record \'todo\''
html4checkbox = "<input type=checkbox checked>"
else
title = 'Check to mark this record \'done\''
html4checkbox = "<input type=checkbox>"
end
if !session["user"] || !admin?
html << html4checkbox
else
html << link_to_remote(html4checkbox, :update => div_id(record, 'done_toggle'), :url => { :controller => record.class.to_s.downcase.pluralize, :action => "toggle_done", :id => record}, :title => title)
end
html << "</span>"
end.join("\n")
end
# #link_to_reviewer helper for Comment and Version-record to set the "reviewer".
# The controller involved needs a method 'review',
# see for instance CommentsController.review and VersionsController.review
def link_to_reviewer(record)
returning url = [] do
url << "<span id=\"" + div_id(record, "reviewer") + "\">"
if !session["user"]
if record.reviewer_id != nil
url << link_to_user(record.reviewer)
end
else
if record.reviewer_id == nil
if admin?
url << link_to_remote("_______", :update => div_id(record, "reviewer"), :url => { :controller => record.class.to_s.downcase.pluralize, :action => "review", :id => record} )
else
url << "" #TODO
end
else
if record.reviewer_id == session["user"].id
url << link_to_remote(record.reviewer.name, :update => div_id(record, "reviewer"), :url => { :controller => record.class.to_s.downcase.pluralize, :action => "review", :id => record} )
else
url << link_to_user(record.reviewer)
end
end
url << "</span> "
end
end.join("\n")
end
def url_for_page(page, site)
return url_for_site(site) + page.rel_path
end
# link_to helper method for a Page-record
# is there another way to
def link_to_page(site,page, extlink_only = false)
img = "extlink.gif"
img = "extlink2.gif" if site && site.wiki?
returning url = [] do
vsns = page.versions_in_site(site) if page.versions_count > 0
comments= page.comments_in_site(site) if page.comments_count > 0
if !extlink_only
url << link_to(page.presentation_name,:controller => 'pages',:action => 'show',:id => page.id,:site_id => site.id)
end
url << " <a href='" + url_for_page(page, site) + "'>"
url << image_tag(img,:border => 0,:title => "Activate page \"#{page.presentation_name}\" in site \"#{site.title}\"")
url << "</a>"
url << link_to_versions(site, page) if vsns
if site && comments
url << link_to_comments(site, page)
end
end.join("\n")
end
# link_to helper method for a Version. Renders a link with a given prefix (often "version") with possibly a lot of clickable images displaying status
def link_to_version(version,urlprefix)
returning link = [] do
link << link_to(urlprefix + " " + version.version.to_s,:controller => 'versions',:action => 'show',:id => version.id)
if !version.version_id
else
source_version = version.source_version
if version.site_id != source_version.site_id
from_site = source_version.site
to_site = version.site
link << link_to(image_tag("site", :border => 0, :align => 'middle', :title => 'Based on version ' + source_version.version.to_s + " from site " + from_site.title) ,:controller => 'versions',:action => 'show',:id => source_version.id)
else
if version.baseline_id != source_version.baseline_id
from_baseline = source_version.baseline
to_baseline = version.baseline
link << link_to(image_tag("baseline", :border => 0, :align => 'middle', :title => 'Based on version ' + source_version.version.to_s + " from baseline " + from_baseline.baseline) ,:controller => 'versions',:action => 'show',:id => source_version.id)
else
if version.page_id != source_version.page_id
base_page = source_version.page
link << link_to(image_tag("new", :border => 0, :align => 'middle', :title => 'New page based on ' + source_version.page.presentation_name + " version " + source_version.version.to_s) ,:controller => 'versions',:action => 'show',:id => source_version.id)
else
if version.version != source_version.version + 1
link << " (based on "
link << link_to(" version " + source_version.version.to_s,:controller => 'versions',:action => 'show',:id => source_version.id)
link << ")"
else
end
end
end
end
end
checkout = version.checkout
if checkout
user = checkout.user
link << link_to(image_tag("checkout2", :border => 0, :align=>"middle", :title=>'Version is checked-out by ' + user.name ),:controller => 'versions',:action => 'show',:id => version.id)
link << " "
if user == session['user']
link << link_to(image_tag("edit.gif", :border => 0, :align=>"middle", :title=>'Version is checked-out by you. Click to continue editing.' ),:controller => 'pages',:action => 'edithtml',:checkout_id => checkout.id)
end
end
link << link_to(image_tag("compare", :border => 0, :align=>"middle", :title=>'Compare with previous version' ),:controller => 'versions',:action => 'compare',:id => version) if version.version != 0
link << link_to(image_tag("format_txt", :border => 0, :align=>"middle", :title=>'View as plain text' ), {:controller => 'versions',:action => 'markdown',:id => version})
end.join("\n")
end
# link_to helper method for a Version. Renders links to the Page as well as the Version.
def link_to_version2 (version)
if version
page = version.page
site = version.site
returning link = [] do
link << link_to_page(site,page)
link << " "
link << link_to_version(version,"version")
end.join("\n")
end
end
# link_to helper method for a Site
def link_to_site(site, page = nil)
img = "extlink.gif"
img = "extlink2.gif" if site.wiki?
returning link = [] do
link << link_to(site.title,:controller => 'sites',:action => 'show',:id => site.id) #if !admin?
link << link_to(image_tag(img,:border => 0,:title => "Activate site \"#{site.title}\""), "/#{site.rel_path}/", :title => "Activate site \"#{site.title}\"") if !page
link << link_to_page(site, page, true) if page
vars = [site.baseline_process, 'updated with site'] if (site.status == 'U')
vars = [site.baseline_process, 'created based on site'] if (site.status == 'P')
link << image_tag("progress.gif",:border => 0,:title => 'This site is scheduled to ' + vars[1] + ' ' + vars[0].title + '(' + vars[0].baseline.baseline + ')') if vars
if site.baseline_process? && cadmin?
link << link_to(image_tag("compare2", :border => 0, :title => "Compare this site with another site"), :controller => "difference_analyses", :action => "new", :site_id => site.id)
end
end.join("\n")
end
# link_to helper method for a User
def link_to_user(user)
returning link = [] do
link << link_to(user.name,:controller => 'users',:action => 'show',:id => user.id) if admin?
link << user.name if !admin?
link << image_tag("user" + user.admin, :border => 0, :title => "") if user.admin?
end.join("\n")
end
# link_to helper method for a Comment
def link_to_comment(comment)
page = comment.page
site = comment.site
user = comment.site
returning link = [] do
#link << link_to(comment.title,:controller => 'comments',:action => 'list',:site_id => site.id, :page_id => page.id)
url = url_for(:controller => 'comments', :action=> 'list', :site_id => site.id, :page_id => page.id) + "\#" + div_id(comment,"")
link << "<a href=\"#{url}\" title=\"More...\">" + truncate(strip_tags(comment.text)) + "</a>"
#link << link_to_reviewer(comment) if admin?
if session['user']
if user == session['user']
link << link_to(image_tag("edit.gif", :border => 0, :align=>"middle", :title=>'Edit this comment'), :controller => 'comments', :action => 'edit', :id => comment.id )
end
end
end.join("\n")
end
# returns unique div id from a record in a page
def div_id(record, call_id)
return record.class.to_s + record.id.to_s + "_" + call_id
end
# helper method for a displaying navigation links to first, next, previous, last pages to use when displaying records in pages.
# See preserving parameters [http://64.233.183.104/search?q=cache:VZkWDCGLNBIJ:wiki.rubyonrails.com/rails/show/HowtoPagination+pagination+how+to+ruby+on+rails&hl=nl&gl=nl&ct=clnk&cd=4]
def links_to_pages(pages)
if pages.length > 1
returning link = [] do
link << link_to(image_tag('first_page', :border => 0, :align=>"middle", :title => "First Page"), {:params => params.merge(:page => 1)} ) if pages.current.previous
link << image_tag('first_page_disabled', :border => 0, :align=>"middle", :title => "First Page") if !pages.current.previous
link << link_to(image_tag('previous_page', :border => 0, :align=>"middle", :title => "Previous Page"), {:params => params.merge(:page => pages.current.previous)}) if pages.current.previous
link << image_tag('previous_page_disabled', :border => 0, :align=>"middle", :title => "Previous Page") if !pages.current.previous
link << "<small>[" + pages.current.number.to_s + "/" + pages.length.to_s + "]</small>"
link << link_to(image_tag('next_page', :border => 0, :align=>"middle", :title => "Next Page"), {:params => params.merge(:page => pages.current.next)}) if pages.current.next
link << image_tag('next_page_disabled', :border => 0, :align=>"middle", :title => "Next Page") if !pages.current.next
link << link_to(image_tag('last_page', :border => 0, :align=>"middle", :title => "Last Page"), {:params => params.merge( :page => pages.last)}) if pages.current.next
link << image_tag('last_page_disabled', :border => 0, :align=>"middle", :title => "Last Page") if !pages.current.next
end.join("\n")
end
end
# link_to helper method for displaying tabs, example
# <tt><% tabs([["All", url_for(:action => 'listall'), true],["To Do", url_for(:action=> 'listtodo')], ["Done", url_for(:action=>'listdone')], 75$]) %> </tt>
# will display three tabs with titles 'All', 'To Do', 'Done' for actions <tt>listall</tt>, <tt>listtodo</tt>, <tt>listdone</tt>.
# the last parameter is the 100% minus the width of the tabs together (the remaining space after the tabs)
def tabs(tabs, widthPerc)
logger.debug('tabs')
#<% tabs([["All", url_for(:action => 'listall'), true],["To Do", url_for(:action=> 'listtodo')], ["Done", url_for(:action=>'listdone')]]) %>
returning link = [] do
link << "<!-- start application_helper tabs -->"
link << "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">"
link << "<tr valign=\"middle\">"
#link << "<td class=\"tab\" width=\"10\">" + image_tag("tab/shim.gif", :height =>17, :width => 10) + "</td>"
i = 0
logger.debug('tabs: ' + tabs.inspect)
for tab in tabs
logger.debug('tab: ' + tab.inspect)
active = false
txt = tab[0]
url = tab[1]
active = tab[2] if tab[2]
element_class = "tab"
element_class = "activeTab" if active
if i == 0
logger.debug("First tab: adding shim")
link << "<td class=\"" + element_class + "\" width=\"10\">" + image_tag("tab/shim.gif", :height => 17, :width => 10)+ "</td>"
elsif i > 0 && i < tabs.length - 1
end
if active
active_index = i
link << "<td class=\"activeTab\">" + txt + "</td>"
#link << "<td width=\"21\">" + image_tag("tab/tab_middle-a_i.gif", :height => 17, :width => 21, :align => 'absmiddle') + "</td>"
else
link << "<td class=\"tab\"><a class=\"tab\" href=\"" + url + "\"><span style=\"white-space:nowrap;\">" + txt + "</span></a></td>"
end
img_tab = "tab/tab_middle-i_i.gif" # default assume we are between inactive and inactive
img_tab = "tab/tab_middle-a_i.gif" if active_index == i
img_tab = "tab/tab_middle-i_a.gif" if !active_index && tabs[i+1][2] == true #active index not determined there must be a next tab, so check if it is the active one
if i == (tabs.length - 1) # last tab
if i == active_index # last tab is active
link << "<td width=\"21\">" + image_tag("tab/tab_end-a.gif", :height => 17, :width => 21) + "</td><td width=\"" + widthPerc.to_s + "%\">" + image_tag("tab/shim.gif", :height => 17, :width => 10) + "</td>"
else
link << "<td width=\"21\">" + image_tag("tab/tab_end-i.gif", :height => 17, :width => 21) + "</td><td width=\"" + widthPerc.to_s + "%\">" + image_tag("tab/shim.gif", :height => 17, :width => 10) + "</td>"
end
else
link << "<td width=\"21\">" + image_tag(img_tab, :height => 17, :width => 21, :align => 'absmiddle') + "</td>"
end
logger.info("Adding tab " + txt + " with link " + url + ". Is active? " + active.to_s)
i = i + 1
end
#logger.info("last tab: adding tab-end-i")
#link << "<td width=\"21\">" + image_tag("tab/tab_end-i.gif", :height => 17, :width => 21) + "</td><td width=\"50%\">" + image_tag("tab/shim.gif", :height => 17, :width => 10) + "</td>"
link << "</tr><tr>"
if active_index == 0
link << "<td colspan=\"2\" class=\"activeTab\">" + image_tag("tab/shim.gif", :height => 2)+ "</td>"
link << "<td class=\"activeTab\">" + image_tag("tab/tab_space.gif", :height => 2, :width => 21) + "</td>"
link << "<td colspan=\"" + (tabs.length+1-3).to_s + "\">" + image_tag("tab/shim.gif", :height => 2) + "</td>"
else
link << "<td colspan=\"" + (active_index*2).to_s + "\">" + image_tag("tab/shim.gif", :height => 2)+ "</td>"
link << "<td>" + image_tag("tab/tab_space_middle.gif", :height => 2 , :width => 21) + "</td><td class=\"activeTab\">"
link << image_tag("tab/shim.gif", :height=>2, :width =>10) + "</td>"
link << "<td>" + image_tag("tab/tab_space.gif", :height => 2, :width => 21) + "</td>"
link << "<td colspan=\"" + ((tabs.length*2+2) - (active_index*2) -3).to_s + "\">" + image_tag("tab/shim.gif", :height => 2) + "</td>"
end
i = i + 1
link << "</tr>"
link << "<tr><td class=\"activeTab\" colspan=\"10\">" + image_tag("tab/shim.gif", :height => 5) + "</td>"
link << "</tr>"
link << "</table>"
link << "<!-- end application_helper tabs -->"
end.join("\n")
end
end