Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'date'
- require 'src/helper/error'
- require 'src/helper/localization'
- require 'src/helper/auth'
- class Array
- def flip_flop
- unshift(pop).first
- end
- def flipflop
- unshift(pop).first
- end
- def flopflip
- push(shift).first
- end
- def to_h
- a = flatten
- return a.inject({}){|s,v| s.merge(v)} if all?{|c| c.respond_to? :to_hash}
- if a.size.even?
- Hash[*a]
- else
- Hash[*(a + [nil])]
- end
- end
- end
- class Numeric
- # nice convert
- def nc(from = nil, to = nil)
- amount = self
- from ||= Session.current[:project_currency]
- to ||= Session.current[:view_currency]
- return Financial::Currency.nice_convert(from, to, amount)
- end
- def even?
- self % 2 == 0
- end
- def odd?
- !even?
- end
- end
- class Time
- def my_strftime
- strftime("%Y/%m/%d")
- end
- end
- module Enumerable
- def localize prefix = nil, postfix = nil
- prefix << '_' if prefix
- postfix = '_' << postfix if postfix
- map{|e| "[[#{prefix}#{e}#{postfix}]]"}
- end
- def table_sum n
- sum = to_a.inject(0){|s,v| s+v[n]}.to_s
- a = Array.new(self[0].size){''}
- a[0] = '[[cf_total]]'
- a[n] = sum
- self + [a]
- end
- end
- class String
- def wordinize
- split("_").map{|w| w.capitalize}.join(" ")
- end
- end
- module LocalizationAspect
- def localize
- @locale = Session.current[:LOCALE]
- @lc = Glue::Localization[@locale]
- end
- end
- # the new Various
- module Various
- def d *args
- p args.map{|dd| dd}
- return *args
- end
- private
- =begin
- The horror before
- #{my_date_tag(@l_start_date,'tenant_start_date','start_date',nil)}
- #{my_date_tag(@l_renew_date,'tenant_renew_date','renew_date',nil)}
- #{my_date_tag(@l_end_date,'tenant_end_date','end_date',nil)}
- #{my_field_tag(@l_title_en,"tenant_title[en]","title[en]",nil)}
- #{my_field_tag(@l_title_jp,"tenant_title[jp]","title[jp]",nil)}
- #{my_field_tag(@l_rent_base,"tenant_rent_base","rent_base",nil)}
- #{my_field_tag(@l_rent_extra,"tenant_rent_extra","rent_extra",nil)}
- #{my_field_tag(@l_util_base,"tenant_util_base","util_base",nil)}
- #{my_field_tag(@l_util_extra,"tenant_util_extra","util_extra",nil)}
- #{my_field_tag(@l_parking,"tenant_parking","parking",nil)}
- #{my_field_tag(@l_deposit,"tenant_deposit","deposit",nil)}
- #{my_field_tag(@l_guarenteur,"tenant_guarenteur","guarenteur",nil)}
- #{my_field_tag(@l_notice,"tenant_notice","notice",nil)}
- #{my_field_tag(@l_options,"tenant_options","options",nil)}
- #{my_field_tag(@l_comment,"tenant_comment","comment",nil)}
- #{my_sel_tag(@l_unit,'tenant_unit','unit',@ls_unit,@vs_unit,nil)}
- #{my_sel_tag(@l_rent_type,'tenant_rent_type','rent_type',@ls_rent_type,@vs_rent_type,@v_rent_type)}
- #{my_sel_tag(@l_payment,'tenant_payment','payment',@ls_payment,@vs_payment,@v_payment)}
- #{my_sel_tag(@l_locked,'tenant_locked','locked',@ls_locked,@vs_locked,@v_locked)}
- The horror after
- #{ date_tags :tenant, :start_date, :renew_date, :end_date }
- #{ field_tags :tenant,
- :title, :rent_base, :rent_extra, :util_base, :util_extra,
- :parking, :deposit, :guarenteur, :notice, :options, :comment}
- #{ sel_tags :tenant,
- :rent_type,
- :payment,
- :locked }
- =end
- def extract_name(title)
- title.match(/^(.*?)(?:_[^_]*)?$/)[1]
- end
- def locked_tag default = 1
- select_tag('', 'locked', default, '[[locked]]' => 0, '[[unlocked]]' => 1)
- end
- def select_tags_edit obj, *opts
- klass = "@#{obj.to_s.downcase}"
- klass = instance_variable_get(klass)
- opts = opts.flatten.map do |o|
- [o].to_h.map do |prop, default|
- default = klass.send(prop)
- default = default.respond_to?(:oid) ? default.oid : default
- [prop, default]
- end
- end
- select_tags(obj, opts.to_h)
- end
- def select_tags obj, *opts
- opts.flatten.map do |opt|
- [opt].to_h.map do |prop, default|
- # need that to prevent stuff like :type one a usual object...
- if default.is_a? Og::EntityMixin
- default = default.send(prop) if default.respond_to? prop
- end
- iv = "#{obj}_#{prop}"
- data = instance_variable_get("@#{iv}_data")
- selected = default || instance_variable_get("@#{iv}_selected")
- select_tag obj, prop, selected, data
- end
- end .join("\n")
- end
- def select_tag(obj, prop, default = nil, data = {})
- obj, prop = obj.to_s, prop.to_s
- title = obj.empty? ? prop : "#{obj}_#{prop}"
- default = default.to_s
- options = data.sort_by{|h| h[1]}.map do |label, value|
- selected = (value.to_s == default.to_s ? ' selected="selected"' : '')
- %{<option value="#{value}"#{selected}>#{lc label}</option>\n}
- end
- tag = %{<tr>}
- tag << %{<th>#{lc title}</th>\n}
- tag << %{<td><select size="1" name="#{prop}">\n#{options.join}</select></td>}
- tag << %{</tr>\n}
- end
- def field_tags_edit obj, *opts
- klass = "@#{obj.to_s.downcase}"
- klass = instance_variable_get(klass)
- opts = opts.flatten.map do |o|
- [o].to_h.map do |prop, default|
- default = klass.send(prop)
- default = default.respond_to?(:oid) ? default.oid : default
- [prop, default]
- end
- end
- field_tags(obj, opts.to_h)
- end
- def field_tags obj, *opts
- opts.flatten.map do |opt|
- [opt].to_h.sort_by{|a| a.to_s}.map do |prop, default|
- default = default.send(prop) if default.respond_to? prop.to_s
- field_tag(obj, prop, default)
- end
- end .join("\n")
- end
- def field_tag(obj, prop, default = nil)
- title = "#{obj}_#{prop}"
- str = %{<tr><th>#{lc title}</th><td>}
- str << %{<input name="#{prop}" type="text"}
- str << %{ value="#{default}"} if default
- str << %{ class="text"/>}
- str << %{</td></tr>}
- end
- def date_tags_edit obj, *opts
- klass = "@#{obj.to_s.downcase}"
- klass = instance_variable_get(klass)
- opts = opts.flatten.map do |o|
- [o].to_h.map do |prop, default|
- default = klass.send(prop)
- default = default.respond_to?(:oid) ? default.oid : default
- [prop, default]
- end
- end
- date_tags(obj, opts.to_h)
- end
- def date_tags obj, *opts
- opts.flatten.map do |opt|
- [opt].to_h.map do |prop, default|
- default = default.send(prop) if default.respond_to? prop.to_s
- date_tag(obj, prop, default)
- end
- end .join("\n")
- end
- def date_tag(obj, prop, default = nil)
- title = "#{obj}_#{prop}"
- str = %{<tr><th>#{lc title}</th><td>}
- str << date_select(obj, prop, default)
- str << %{</td></tr>}
- end
- def date_select obj, prop, default = nil
- date = default || Date.today
- str = ""
- days = (1..31)
- months = Date::MONTHS.invert.sort
- years = ((date.year-50)...(date.year+50))
- [
- [ :day, days ],
- [ :month, months ],
- [ :year, years ]
- ].each do |key, items|
- str << %{<select class="date_#{key}" name="#{prop}.#{key}">\n}
- items.each do |item, i|
- selected = (item == date.send(key) ? ' selected="selected"' : '')
- label = i ? "#{item} - #{i.to_s.capitalize}" : item
- str << %{<option value="#{item}"#{selected}>#{label}</option>}
- end
- str << %{</select>\n}
- end
- str << %{</td></tr>\n}
- return str
- end
- # options:
- # :properties => [:update_time, :creation_time]
- def control_for(obj, *options)
- options = options.first
- properties = options[:properties] ? options[:properties] : obj.class.properties
- props = properties.map do |prop|
- obj.class.properties[prop]
- end
- @props = props.map do |prop|
- control = Nitro::Form::Control.fetch(obj, prop, options).render
- Nitro::FormHelper::FormBuilder.element(prop, control)
- end
- end
- def nc_map(values)
- values.map! do |v|
- v.map do |vv|
- vv == vv.to_f ? (vv.to_f.nan? ? nc(0.0) : nc(vv.to_f)) : vv
- end
- end
- return values
- end
- def summup(values)
- sum = values.first.dup
- traverse = values - sum
- sum.map!{|x| x.to_f.to_s == x.to_s ? x : ""}
- (0...sum.size).each do |i|
- traverse.each do |t|
- sum[i] += t[i] unless sum[i] == "" || t[i].to_f.nan?
- end
- end
- sum[0] = @lc['cf_total']
- return nc_map( values + [sum] )
- end
- def button text, goto = nil
- p goto
- goto = goto ? %{onClick="goto('/#{goto}')"} : ''
- %{<input type="submit" class="button" value="#{lc text}"#{goto}/>}
- end
- def button_submit
- button :save
- end
- def button_save
- button :save
- end
- def button_update
- button :update
- end
- def button_cancel goto
- button :cancel, goto
- end
- end
- module Various
- private
- def link_tag label, *path
- %{<a href="#{path.join('/')}">#{label}</a>}
- end
- ################################################################################
- # Create a link tag
- ################################################################################
- def link_set(function_s = "", label_s = "", id_s = "")
- "<a href=\""+function_s+"/"+id_s+"\">"+label_s+"</a>"
- end
- ################################################################################
- # Create a link to a popup
- ################################################################################
- def link_pop(function_s = "", label_s = "", caller_s = "", id_s = "")
- s = function_s+"/"+caller_s+"/"+id_s
- opt = {:text => label_s, :url => s, :width => 550, :height => 700,
- :resizable => true, :scrollbars => true , :menubar => false }
- %|#{popup(opt)}|
- end
- ################################################################################
- # Create a link to a popup for printing (Report Style)
- ################################################################################
- def link_pop_print(function_s = "", label_s = "", caller_s = "", id_s = "")
- s = function_s+"/"+caller_s+"/"+id_s
- opt = {:text => label_s, :url => s, :width => 1100, :height => 700,
- :resizable => true, :scrollbars => true , :menubar => true }
- %|#{popup(opt)}|
- end
- def link_edit_delete to, oid
- [
- link_edit(to, oid),
- link_delete(to, oid)
- ]
- end
- def link_select to, oid
- link "[[select]]", "#{to}_select/#{oid}"
- end
- def link_edit to, oid
- link "[[edit]]", "#{to}_edit/#{oid}"
- end
- def link_delete to, oid
- link "[[delete]]", "javascript:question('#{lc 'del_sure'}', '#{to}_delete/#{oid}/')"
- end
- def link_view to, oid
- link "[[view]]", "#{to}_view/#{oid}"
- end
- def link_calc to, oid
- link "[[cal_tag]]", "#{to}_cal/#{oid}"
- end
- def link_app to, oid
- link "[[app_tag]]", "#{to}_app/#{oid}"
- end
- alias link_calculate link_calc
- def link text, href
- %{<a href="#{href}">#{text}</a>}
- end
- ################################################################################
- # Loads the templates
- ################################################################################
- def load_default_tpl(filename)
- array = File.open(filename) { |yf| YAML::load(yf) }
- title = array[0]
- array.delete(array.first)
- if Tpl.find(:condition => "title = '#{title}'").length == 0
- # first we make the template
- tpl = Tpl.new(title)
- tpl.save
- array.each {|a|
- if filename == 'conf/tpl_def.yaml'
- # then we make the labels
- label = Label.new(a[0], a[1], a[2], a[3], a[4], a[5], a[6])
- label.save
- # finally we make the views of the templates
- tplview = Tplview.new(a[7], a[8], tpl.oid, label.oid)
- tplview.save
- else
- # find the label with the correct tag
- label = Label.find(:condition => "tag = #{a[1]}")
- # finally we make the views of the templates
- tplview = Tplview.new(a[7], a[8], tpl.oid, label[0].oid)
- tplview.save
- end
- }
- end
- end
- #############################################################################
- # creates a selections tag for use as a table row <TR></TR> included
- #############################################################################
- # label: the label after localization or just plain string
- # id: the id name, e.g. property_title
- # name: the name, e.g. title
- # opt_labs: array of option labels after localization or plain strings
- # opt_vals: array of option values; either localized string, plain strings or other values
- # sel_val: the value in opt_vals that should be selected, if nil default selection occurs
- #############################################################################
- #############################################################################
- # code generation for templates
- def my_sel_tag(label, id, name, opt_labs, opt_vals, sel_val)
- str = %{
- <tr><th>#{label}</th><td>
- <select size="1" id="#{id}" name="#{name}">
- }
- if (opt_vals != nil && opt_labs != nil)
- for i in 0 ... opt_labs.length do
- str << %{<option }
- str << %{selected="selected" } if sel_val.to_s == opt_vals[i].to_s
- str << %{value="#{opt_vals[i]}">#{opt_labs[i]}</option>}
- end
- end
- str << %{</select></td></tr>}
- return str
- end
- def my_mult_sel_tag(label, id, name, opt_labs, opt_vals, sel_val)
- str = %{
- <tr><th>#{label}</th><td>
- <select multiple size="3" id="#{id}" name="#{name}">
- }
- if (opt_vals != nil && opt_labs != nil)
- for i in 0 ... opt_labs.length do
- str << %{<option }
- sel_val.each{ |ss|
- str << %{selected } if ss == opt_vals[i]
- }
- str << %{value="#{opt_vals[i]}">#{opt_labs[i]}</option>}
- end
- end
- str << %{</select></td></tr>}
- return str
- end
- def my_pass_field_tag(label, id, name, val = nil)
- str = %{
- <tr><th>#{label}</th><td>
- <input id="#{id}" name="#{name}" type="password"
- }
- str << %{value="#{val}"} if val != nil
- str << %{size="40"/>
- </td></tr>
- }
- return str
- end
- # options:
- # :properties => [:update_time, :creation_time]
- def control_for(obj, *options)
- options = options.first
- properties = options[:properties] ? options[:properties] : obj.class.properties
- props = properties.map do |prop|
- obj.class.properties[prop]
- end
- @props = props.map do |prop|
- control = Nitro::Form::Control.fetch(obj, prop, options).render
- Nitro::FormHelper::FormBuilder.element(prop, control)
- end
- end
- def nc_map(values)
- values.map! do |v|
- v.map do |vv|
- case vv
- when vv.is_a?(Hash)
- vv[@locale.to_s]
- when vv.to_f
- vv.to_f.nan? ? nc(0.0) : nc(vv.to_f)
- else
- vv
- end
- end
- end
- return values
- end
- def summup(values)
- values = values.map{|v|
- v.map{|vv|
- vv.is_a?(Hash) ? vv[@locale.to_s] : vv
- }
- }
- sum = values.first.dup
- traverse = values - sum
- sum.map!{ |x|
- x.to_f.to_s == x.to_s ? x : ""
- }
- (0...sum.size).each do |i|
- traverse.each do |t|
- sum[i] += t[i] unless t[i].nil? || sum[i] == "" || t[i].to_f.nan?
- end
- end
- sum[0] = @lc['cf_total']
- return nc_map( values + [sum] )
- end
- end
- =begin
- module Form
- # select :values => [1,2,3], :labels => [:first, :second, :third], :class => "foo", :selected => 1
- # =>
- def self.select(options)
- values = options.delete :values
- labels = options.delete :labels
- selected = options.delete :selected
- tag = '<select' << options.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } << '>'
- tag << values.zip(labels).map{ |value,label|
- %{<option value="#{value}"#{' selected="selected"' if value == selected}>#{label}</option>}
- }.join
- tag << '</select>'
- end
- end
- =end
- module Nitro
- class Controller
- include Various
- include AuthHelper
- include ErrorHelper
- include Nitro::XhtmlHelper
- include Nitro::TableHelper
- @lc = Glue::Localization.get
- wrap LocalizationAspect, :pre => :localize
- def lc *args
- return @lc if args.empty?
- ar = args.map{|a| a.to_s.gsub(/\[\[(.*?)\]\]/, '\1')}
- ar.map!{|a| @lc[a] ? @lc[a] : a}
- ar = ar.size <= 1 ? ar.first : ar
- end
- end
- end
Add Comment
Please, Sign In to add comment