Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

View Single Enquiry and Create Enquiry #660

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
155 changes: 155 additions & 0 deletions app/controllers/enquiries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
class EnquiriesController < ApplicationController
skip_before_filter :verify_authenticity_token
skip_before_filter :check_authentication, :only => [:reindex]

before_filter :load_enquiry_or_redirect, :only => [:show, :edit, :destroy]
before_filter :current_user, :except => [:reindex]

# GET /enquiries
def index
# Skeleton based on child controller code
authorize! :index, Enquiry

@page_name = t("home.view_enquiries")
@aside = 'shared/sidebar_links'
@filter = params[:filter] || params[:status] || "all"
@order = params[:order_by] || 'enquirer_name'
setup_fields!
per_page = params[:per_page] || EnquiriesHelper::View::PER_PAGE
per_page = per_page.to_i unless per_page == 'all'

filter_enquiries per_page
end

# GET /enquiries/1
def show
authorize! :read, @enquiry

@page_name = t("enquiry.view")
@body_class = 'profile-page'
@enquiry = Enquiry.get(params[:id])
setup_fields!
end

# GET /enquiries/new
def new
# Skeleton based on child controller code
authorize! :create, Enquiry

@page_name = t("enquiries.create_new_enquiry")
setup_fields!
@exclude_tabs = ["e98c765c"]
@enquiry = Enquiry.new
end

# GET /enquiries/1/edit
def edit
# Skeleton based on child controller code
authorize! :update, @enquiry

@page_name = t("enquiry.edit")
setup_fields!
end

# POST /enquiries
def create
# Skeleton based on child controller code
authorize! :create, Enquiry

enquirer_name = params[:child].delete('enquirer_name')
create_enquiry({:enquirer_name => enquirer_name,
:criteria => params[:child]})
@enquiry['created_by_full_name'] = current_user_full_name

respond_to do |format|
if @enquiry.save
format.html { redirect_to(@enquiry) }
format.xml { render :xml => @enquiry, :status => :created, :location => @enquiry }
format.json {
render :json => @enquiry.compact.to_json
}
else
format.html {
@form_sections = get_form_sections
render :action => "new"
}
format.xml { render :xml => @enquiry.errors, :status => :unprocessable_entity }
end
end
end

def update
# Not yet implemented
end

private

def load_enquiry_or_redirect
@enquiry = Enquiry.get(params[:id])

if @enquiry.nil?
flash[:error] = "Enquiry with the given id is not found"
redirect_to :action => :index and return
end
end

def filter_enquiries(per_page)
total_rows, enquiries = enquiries_by_user_access(@filter, per_page)
@enquiries = paginated_collection enquiries, total_rows
end

def enquiries_by_user_access(filter_option, per_page)
# Skeleton based on child controller code
keys = [filter_option] # Not currently using keys
options = {}
all_rows = Enquiry.view("all")
return all_rows.length, all_rows
end

def paginated_collection instances, total_rows
page = params[:page] || 1
WillPaginate::Collection.create(page, EnquiriesHelper::View::PER_PAGE, total_rows) do |pager|
pager.replace(instances)
end
end

def setup_fields!
@form_sections = get_form_sections
@fields = @form_sections.collect{ |section| section["fields"] }.flatten
@exclude_tabs = []
end

def get_form_sections
JSON.parse(File.read(Rails.root.join("config", "enquiry_form_sections.json").to_s)).collect do |form_section|
form_section["fields"].each do |field|
field["display_name"].each do |lang, str|
field["display_name_#{lang}"] = str
end
field.delete("display_name")

if field.has_key? "option_strings_text"
field["option_strings_text"].each do |lang, arr|
field["option_strings_text_#{lang}"] = arr.join("\n")
end
field.delete("option_strings_text")
end

Field.new(field)
end

["name", "help_text", "description"].each do |property|
form_section[property].each do |lang, str|
form_section["#{property}_#{lang}"] = str
end
form_section.delete(property)
end

FormSection.new(form_section)
end
end

def create_enquiry(enquiry_params)
@enquiry = Enquiry.new_with_user_name(current_user, enquiry_params)
end

end
11 changes: 11 additions & 0 deletions app/helpers/enquiries_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module EnquiriesHelper

module View
PER_PAGE = 20
MAX_PER_PAGE = 9999
end

def text_to_identify_enquiry enquiry
enquiry['enquirer_name'].blank? ? enquiry.id : "Enquiry by #{enquiry['enquirer_name']}: #{enquiry.id}"
end
end
5 changes: 3 additions & 2 deletions app/models/enquiry.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class Enquiry < CouchRest::Model::Base
use_database :enquiry
include RecordHelper

include RecordHelper
include RapidFTR::CouchRestRailsBackward
before_save :find_matching_children

property :enquirer_name
Expand Down Expand Up @@ -49,7 +50,7 @@ def find_matching_children
self.potential_matches = children.map { |child| child.id }
verify_format_of(previous_matches)

unless previous_matches.eql?(self.potential_matches)
if previous_matches.empty? or not previous_matches.eql?(self.potential_matches)
self.match_updated_at = Clock.now.to_s
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class Field
NUMERIC_FIELD = "numeric_field",
PHOTO_UPLOAD_BOX = "photo_upload_box",
AUDIO_UPLOAD_BOX = "audio_upload_box",
DATE_FIELD = "date_field"
DATE_FIELD = "date_field",
LIST_RECORD_FIELD = "list_record_field"
]

validates_presence_of "display_name_#{I18n.default_locale}", :message=> I18n.t("errors.models.field.display_name_presence")
Expand Down
6 changes: 6 additions & 0 deletions app/views/enquiries/_field_display_basic.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<p>
<label class="key"><%= t(field.name, :default => field.display_name) %>:</label>
<span class="value"><%= field_value_for_display (@enquiry[field.name] ? @enquiry[field.name] :
(@enquiry[:criteria] || @enquiry)[field.name]) %></span>
</p>
22 changes: 22 additions & 0 deletions app/views/enquiries/_field_display_photo.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- Photo fields are not shown inline-->

<% content_for :javascript do %>
<script type="text/javascript" language="javascript">

$(function() {
$('.thumbnails .thumbnail a').lightBox({fixedNavigation:true,
imageLoading: '/images/lightbox-ico-loading.gif',
imageBtnPrev: '/images/lightbox-btn-prev.gif',
imageBtnNext: '/images/lightbox-btn-next.gif',
imageBtnClose: '/images/lightbox-btn-close.gif',
imageBlank: '/images/lightbox-blank.gif'
});
});
</script>
<% end %>

<div class="clearfix"></div>
<div class="thumbnails photos">
<% # Not yet implemented %>
</div>
<div class="clearfix"></div>
11 changes: 11 additions & 0 deletions app/views/enquiries/_form_section.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% unless @exclude_tabs.include?(form_section.unique_id) %>
<fieldset id="tab_<%= form_section.section_name %>" class="<%= form_section.section_name %> tab">
<%= render :partial => "form_section_info", :locals => {:form_section => form_section} %>
<% (form_section.fields.select {|field| field.visible? }).each do |field| %>
<% if field.type != "list_record_field" && field.type != "photo_upload_box" %>
<%= render :partial => field.type, :object => field %>
<% end %>
<% end %>

</fieldset>
<% end %>
15 changes: 15 additions & 0 deletions app/views/enquiries/_form_section_info.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2><%= t(form_section.unique_id,:default => form_section.name) %></h2>
<div class="clearfix"></div>

<div class="form_info">
<% if form_section.description.present? %>
<div class="form-section-description">
<%= form_section.description %>
</div>
<% end %>
<% if form_section.help_text.present? %>
<div class="form-section-help-text">
<%= form_section.help_text %>
</div>
<% end %>
</div>
4 changes: 4 additions & 0 deletions app/views/enquiries/_header.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1 class="no_border float_left"><%= t("enquiries.label") %></h1>
<% if can? :create, Enquiry %>
<%= link_to content_tag(:span, t("enquiries.create_new_enquiry"), :class => 'create_user'), new_enquiry_path, :class=>'btn' %>
<% end %>
Empty file.
22 changes: 22 additions & 0 deletions app/views/enquiries/_photo_upload_box.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<p>
Photos should be no larger than 10mb. Audio files should be no larger than 10mb. The smaller the better.
</p>
<% ChildrenHelper::EditView::ONETIME_PHOTOS_UPLOAD_LIMIT.times do |i|%>
<p class="file">
<span class="tool-tip-label">
<%= label_tag "enquiry[photo]#{i}", "Add Photo", :class=>'key' %>

</span>
<%= file_field_tag "enquiry[photo]#{i}" %>

<%= image_tag "new/icon_help.png", title: photo_upload_box.help_text, class: 'tool-tip-icon vtip' if photo_upload_box.help_text.present? %>
</p>
<% end %>

<% @enquiry.photos.each do |photo| %>
<p class="thumbnail">
<%= label_tag "delete_enquiry_photo[#{photo.name}]", t("enquiry.actions.delete_photo") %>
<%= check_box_tag("delete_enquiry_photo[#{photo.name}]") %>
<%= thumbnail_tag(@enquiry, photo.name) %>
</p>
<% end %>
8 changes: 8 additions & 0 deletions app/views/enquiries/_select_box.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>
<span class="tool-tip-label">
<%= label_tag select_box.tag_name_attribute, t(select_box.name, :default => select_box.display_name), :class=>'key' %>
</span>
<%= select_tag select_box.tag_name_attribute, options_for_select(select_box.select_options, (@enquiry[select_box.name] || '')) %>

<%= image_tag "new/icon_help.png", title: select_box.help_text, class: 'tool-tip-icon vtip' if select_box.help_text.present? %>
</p>
13 changes: 13 additions & 0 deletions app/views/enquiries/_show_form_section.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% @form_sections.each do |section| %>

<fieldset id='tab_<%= section.section_name.underscore %>' class='<%= section.section_name %> tab '>
<div class="clearfix"></div>

<%= render :partial => "form_section_info", :locals => {:form_section => section} %>

<% (section.fields.select{|field| field.visible?}).each do |field| %>
<%= render :partial => "field_display_#{field.display_type}", :locals=>{:field => field, :enquiry=>@enquiry} %>
<% end %>

</fieldset>
<% end %>
39 changes: 39 additions & 0 deletions app/views/enquiries/_summary_row.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!-- BEGIN: item -->
<div id="enquiry_<%= enquiry.id %>" class="child_summary_panel">

<div class="summary_panel">

<h2><%= link_to text_to_identify_enquiry(enquiry), enquiry_path(enquiry) %></h2>
<div class="clearfix"></div>

<% @fields.each do |relevant_field| %>
<% if relevant_field.visible? and relevant_field.is_highlighted? %>
<div class="summary_item">
<div class="key"><%= t(relevant_field.display_name) %>:</div>
<div class="value"><%= enquiry[relevant_field[:name]] %></div>
</div>
<% end %>
<% end %>
<% if enquiry.match_updated_at %>
<div class="summary_item">
<div class="key"><%= t("enquiry.match_updated") %>:</div>
<div class="value"><%= current_user.localize_date(enquiry.match_updated_at) %></div>
</div>
<% end %>
</div>

<div class="clearfix"></div>
<div class="action_panel">
<% show_links = true unless local_assigns.has_key? :show_links %>
<% if show_links %>
<ul class="view-edit">
<% if can?(:update, enquiry) %>
<li>
<%= link_to(t('buttons.edit'), edit_enquiry_path(enquiry), :class=>'edit') %> |
</li>
<% end %>
</ul>
<% end %>
</div>
</div>
<!-- END: item -->
15 changes: 15 additions & 0 deletions app/views/enquiries/_tabs.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<ul class="tab-handles">
<% is_first_tab = true %>
<% tabs.each do |form_section| %>
<% unless @exclude_tabs.include?(form_section.unique_id) %>
<% if is_first_tab %>
<li class="current">
<% else %>
<li>
<% end %>
<a href="#tab_<%= form_section.section_name %>"><%= t(form_section.unique_id, :default => form_section.name) %></a>
</li>
<% is_first_tab = false %>
<% end %>
<% end %>
</ul>
10 changes: 10 additions & 0 deletions app/views/enquiries/_text_field.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p>
<span class="tool-tip-label">
<%= label_tag text_field.tag_name_attribute, t(text_field.name, :default => text_field.display_name), :class=>'key' %>
</span>
<%= text_field_tag text_field.tag_name_attribute,
(@enquiry[text_field.name] ? @enquiry[text_field.name] :
(@enquiry[:criteria] || @enquiry)[text_field.name]) %>

<%= image_tag "new/icon_help.png", title: text_field.help_text, class: 'tool-tip-icon vtip' if text_field.help_text.present? %>
</p>
9 changes: 9 additions & 0 deletions app/views/enquiries/_textarea.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>
<span class="tool-tip-label">
<%= label_tag textarea.tag_name_attribute, t(textarea.name,:default => textarea.display_name), :class=>'key' %>
</span>
<%= text_area_tag textarea.tag_name_attribute,
(@enquiry[textarea.name] ? @enquiry[textarea.name] :
(@enquiry[:criteria] || @enquiry)[textarea.name]) %>
<%= image_tag "new/icon_help.png", title: textarea.help_text, class: 'tool-tip-icon vtip' if textarea.help_text.present? %>
</p>
10 changes: 10 additions & 0 deletions app/views/enquiries/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= render :partial => "enquiries/header" %>
<div class="clearfix"></div>

<div class="child_list">
<%= render :partial => "enquiries/summary_row", :collection => @enquiries, :as => :enquiry %>

<%= render :partial => 'shared/pagination', :locals => { :results => @enquiries } %>
</div>

<br/>
Loading