Guest User

Untitled

a guest
Feb 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.28 KB | None | 0 0
  1. require 'date'
  2. require 'src/helper/error'
  3. require 'src/helper/localization'
  4. require 'src/helper/auth'
  5.  
  6. class Array
  7. def flip_flop
  8. unshift(pop).first
  9. end
  10.  
  11. def flipflop
  12. unshift(pop).first
  13. end
  14.  
  15. def flopflip
  16. push(shift).first
  17. end
  18.  
  19. def to_h
  20. a = flatten
  21. return a.inject({}){|s,v| s.merge(v)} if all?{|c| c.respond_to? :to_hash}
  22. if a.size.even?
  23. Hash[*a]
  24. else
  25. Hash[*(a + [nil])]
  26. end
  27. end
  28. end
  29.  
  30. class Numeric
  31. # nice convert
  32. def nc(from = nil, to = nil)
  33. amount = self
  34. from ||= Session.current[:project_currency]
  35. to ||= Session.current[:view_currency]
  36. return Financial::Currency.nice_convert(from, to, amount)
  37. end
  38.  
  39. def even?
  40. self % 2 == 0
  41. end
  42.  
  43. def odd?
  44. !even?
  45. end
  46. end
  47.  
  48. class Time
  49. def my_strftime
  50. strftime("%Y/%m/%d")
  51. end
  52. end
  53.  
  54. module Enumerable
  55. def localize prefix = nil, postfix = nil
  56. prefix << '_' if prefix
  57. postfix = '_' << postfix if postfix
  58. map{|e| "[[#{prefix}#{e}#{postfix}]]"}
  59. end
  60.  
  61. def table_sum n
  62. sum = to_a.inject(0){|s,v| s+v[n]}.to_s
  63. a = Array.new(self[0].size){''}
  64. a[0] = '[[cf_total]]'
  65. a[n] = sum
  66. self + [a]
  67. end
  68. end
  69.  
  70. class String
  71. def wordinize
  72. split("_").map{|w| w.capitalize}.join(" ")
  73. end
  74. end
  75.  
  76. module LocalizationAspect
  77. def localize
  78. @locale = Session.current[:LOCALE]
  79. @lc = Glue::Localization[@locale]
  80. end
  81. end
  82.  
  83. # the new Various
  84. module Various
  85. def d *args
  86. p args.map{|dd| dd}
  87. return *args
  88. end
  89.  
  90. private
  91.  
  92. =begin
  93. The horror before
  94.  
  95. #{my_date_tag(@l_start_date,'tenant_start_date','start_date',nil)}
  96. #{my_date_tag(@l_renew_date,'tenant_renew_date','renew_date',nil)}
  97. #{my_date_tag(@l_end_date,'tenant_end_date','end_date',nil)}
  98.  
  99. #{my_field_tag(@l_title_en,"tenant_title[en]","title[en]",nil)}
  100. #{my_field_tag(@l_title_jp,"tenant_title[jp]","title[jp]",nil)}
  101. #{my_field_tag(@l_rent_base,"tenant_rent_base","rent_base",nil)}
  102. #{my_field_tag(@l_rent_extra,"tenant_rent_extra","rent_extra",nil)}
  103. #{my_field_tag(@l_util_base,"tenant_util_base","util_base",nil)}
  104. #{my_field_tag(@l_util_extra,"tenant_util_extra","util_extra",nil)}
  105. #{my_field_tag(@l_parking,"tenant_parking","parking",nil)}
  106. #{my_field_tag(@l_deposit,"tenant_deposit","deposit",nil)}
  107. #{my_field_tag(@l_guarenteur,"tenant_guarenteur","guarenteur",nil)}
  108. #{my_field_tag(@l_notice,"tenant_notice","notice",nil)}
  109. #{my_field_tag(@l_options,"tenant_options","options",nil)}
  110. #{my_field_tag(@l_comment,"tenant_comment","comment",nil)}
  111.  
  112. #{my_sel_tag(@l_unit,'tenant_unit','unit',@ls_unit,@vs_unit,nil)}
  113. #{my_sel_tag(@l_rent_type,'tenant_rent_type','rent_type',@ls_rent_type,@vs_rent_type,@v_rent_type)}
  114. #{my_sel_tag(@l_payment,'tenant_payment','payment',@ls_payment,@vs_payment,@v_payment)}
  115. #{my_sel_tag(@l_locked,'tenant_locked','locked',@ls_locked,@vs_locked,@v_locked)}
  116.  
  117.  
  118. The horror after
  119.  
  120. #{ date_tags :tenant, :start_date, :renew_date, :end_date }
  121.  
  122. #{ field_tags :tenant,
  123. :title, :rent_base, :rent_extra, :util_base, :util_extra,
  124. :parking, :deposit, :guarenteur, :notice, :options, :comment}
  125.  
  126. #{ sel_tags :tenant,
  127. :rent_type,
  128. :payment,
  129. :locked }
  130. =end
  131.  
  132. def extract_name(title)
  133. title.match(/^(.*?)(?:_[^_]*)?$/)[1]
  134. end
  135.  
  136. def locked_tag default = 1
  137. select_tag('', 'locked', default, '[[locked]]' => 0, '[[unlocked]]' => 1)
  138. end
  139.  
  140. def select_tags_edit obj, *opts
  141. klass = "@#{obj.to_s.downcase}"
  142. klass = instance_variable_get(klass)
  143. opts = opts.flatten.map do |o|
  144. [o].to_h.map do |prop, default|
  145. default = klass.send(prop)
  146. default = default.respond_to?(:oid) ? default.oid : default
  147. [prop, default]
  148. end
  149. end
  150. select_tags(obj, opts.to_h)
  151. end
  152.  
  153. def select_tags obj, *opts
  154. opts.flatten.map do |opt|
  155. [opt].to_h.map do |prop, default|
  156. # need that to prevent stuff like :type one a usual object...
  157. if default.is_a? Og::EntityMixin
  158. default = default.send(prop) if default.respond_to? prop
  159. end
  160. iv = "#{obj}_#{prop}"
  161. data = instance_variable_get("@#{iv}_data")
  162. selected = default || instance_variable_get("@#{iv}_selected")
  163. select_tag obj, prop, selected, data
  164. end
  165. end .join("\n")
  166. end
  167.  
  168. def select_tag(obj, prop, default = nil, data = {})
  169. obj, prop = obj.to_s, prop.to_s
  170. title = obj.empty? ? prop : "#{obj}_#{prop}"
  171. default = default.to_s
  172. options = data.sort_by{|h| h[1]}.map do |label, value|
  173. selected = (value.to_s == default.to_s ? ' selected="selected"' : '')
  174. %{<option value="#{value}"#{selected}>#{lc label}</option>\n}
  175. end
  176. tag = %{<tr>}
  177. tag << %{<th>#{lc title}</th>\n}
  178. tag << %{<td><select size="1" name="#{prop}">\n#{options.join}</select></td>}
  179. tag << %{</tr>\n}
  180. end
  181.  
  182. def field_tags_edit obj, *opts
  183. klass = "@#{obj.to_s.downcase}"
  184. klass = instance_variable_get(klass)
  185. opts = opts.flatten.map do |o|
  186. [o].to_h.map do |prop, default|
  187. default = klass.send(prop)
  188. default = default.respond_to?(:oid) ? default.oid : default
  189. [prop, default]
  190. end
  191. end
  192. field_tags(obj, opts.to_h)
  193. end
  194.  
  195. def field_tags obj, *opts
  196. opts.flatten.map do |opt|
  197. [opt].to_h.sort_by{|a| a.to_s}.map do |prop, default|
  198. default = default.send(prop) if default.respond_to? prop.to_s
  199. field_tag(obj, prop, default)
  200. end
  201. end .join("\n")
  202. end
  203.  
  204. def field_tag(obj, prop, default = nil)
  205. title = "#{obj}_#{prop}"
  206. str = %{<tr><th>#{lc title}</th><td>}
  207. str << %{<input name="#{prop}" type="text"}
  208. str << %{ value="#{default}"} if default
  209. str << %{ class="text"/>}
  210. str << %{</td></tr>}
  211. end
  212.  
  213. def date_tags_edit obj, *opts
  214. klass = "@#{obj.to_s.downcase}"
  215. klass = instance_variable_get(klass)
  216. opts = opts.flatten.map do |o|
  217. [o].to_h.map do |prop, default|
  218. default = klass.send(prop)
  219. default = default.respond_to?(:oid) ? default.oid : default
  220. [prop, default]
  221. end
  222. end
  223. date_tags(obj, opts.to_h)
  224. end
  225.  
  226. def date_tags obj, *opts
  227. opts.flatten.map do |opt|
  228. [opt].to_h.map do |prop, default|
  229. default = default.send(prop) if default.respond_to? prop.to_s
  230. date_tag(obj, prop, default)
  231. end
  232. end .join("\n")
  233. end
  234.  
  235. def date_tag(obj, prop, default = nil)
  236. title = "#{obj}_#{prop}"
  237. str = %{<tr><th>#{lc title}</th><td>}
  238. str << date_select(obj, prop, default)
  239. str << %{</td></tr>}
  240. end
  241.  
  242. def date_select obj, prop, default = nil
  243. date = default || Date.today
  244. str = ""
  245.  
  246. days = (1..31)
  247. months = Date::MONTHS.invert.sort
  248. years = ((date.year-50)...(date.year+50))
  249.  
  250. [
  251. [ :day, days ],
  252. [ :month, months ],
  253. [ :year, years ]
  254. ].each do |key, items|
  255. str << %{<select class="date_#{key}" name="#{prop}.#{key}">\n}
  256. items.each do |item, i|
  257. selected = (item == date.send(key) ? ' selected="selected"' : '')
  258. label = i ? "#{item} - #{i.to_s.capitalize}" : item
  259. str << %{<option value="#{item}"#{selected}>#{label}</option>}
  260. end
  261. str << %{</select>\n}
  262. end
  263.  
  264. str << %{</td></tr>\n}
  265.  
  266. return str
  267. end
  268.  
  269. # options:
  270. # :properties => [:update_time, :creation_time]
  271. def control_for(obj, *options)
  272. options = options.first
  273. properties = options[:properties] ? options[:properties] : obj.class.properties
  274. props = properties.map do |prop|
  275. obj.class.properties[prop]
  276. end
  277. @props = props.map do |prop|
  278. control = Nitro::Form::Control.fetch(obj, prop, options).render
  279. Nitro::FormHelper::FormBuilder.element(prop, control)
  280. end
  281. end
  282.  
  283. def nc_map(values)
  284. values.map! do |v|
  285. v.map do |vv|
  286. vv == vv.to_f ? (vv.to_f.nan? ? nc(0.0) : nc(vv.to_f)) : vv
  287. end
  288. end
  289. return values
  290. end
  291.  
  292. def summup(values)
  293. sum = values.first.dup
  294. traverse = values - sum
  295.  
  296. sum.map!{|x| x.to_f.to_s == x.to_s ? x : ""}
  297.  
  298. (0...sum.size).each do |i|
  299. traverse.each do |t|
  300. sum[i] += t[i] unless sum[i] == "" || t[i].to_f.nan?
  301. end
  302. end
  303.  
  304. sum[0] = @lc['cf_total']
  305.  
  306. return nc_map( values + [sum] )
  307. end
  308.  
  309. def button text, goto = nil
  310. p goto
  311. goto = goto ? %{onClick="goto('/#{goto}')"} : ''
  312. %{<input type="submit" class="button" value="#{lc text}"#{goto}/>}
  313. end
  314.  
  315. def button_submit
  316. button :save
  317. end
  318.  
  319. def button_save
  320. button :save
  321. end
  322.  
  323. def button_update
  324. button :update
  325. end
  326.  
  327. def button_cancel goto
  328. button :cancel, goto
  329. end
  330. end
  331.  
  332. module Various
  333. private
  334.  
  335. def link_tag label, *path
  336. %{<a href="#{path.join('/')}">#{label}</a>}
  337. end
  338.  
  339. ################################################################################
  340. # Create a link tag
  341. ################################################################################
  342. def link_set(function_s = "", label_s = "", id_s = "")
  343. "<a href=\""+function_s+"/"+id_s+"\">"+label_s+"</a>"
  344. end
  345.  
  346. ################################################################################
  347. # Create a link to a popup
  348. ################################################################################
  349. def link_pop(function_s = "", label_s = "", caller_s = "", id_s = "")
  350. s = function_s+"/"+caller_s+"/"+id_s
  351. opt = {:text => label_s, :url => s, :width => 550, :height => 700,
  352. :resizable => true, :scrollbars => true , :menubar => false }
  353. %|#{popup(opt)}|
  354. end
  355.  
  356. ################################################################################
  357. # Create a link to a popup for printing (Report Style)
  358. ################################################################################
  359. def link_pop_print(function_s = "", label_s = "", caller_s = "", id_s = "")
  360. s = function_s+"/"+caller_s+"/"+id_s
  361. opt = {:text => label_s, :url => s, :width => 1100, :height => 700,
  362. :resizable => true, :scrollbars => true , :menubar => true }
  363. %|#{popup(opt)}|
  364. end
  365.  
  366. def link_edit_delete to, oid
  367. [
  368. link_edit(to, oid),
  369. link_delete(to, oid)
  370. ]
  371. end
  372.  
  373. def link_select to, oid
  374. link "[[select]]", "#{to}_select/#{oid}"
  375. end
  376.  
  377. def link_edit to, oid
  378. link "[[edit]]", "#{to}_edit/#{oid}"
  379. end
  380.  
  381. def link_delete to, oid
  382. link "[[delete]]", "javascript:question('#{lc 'del_sure'}', '#{to}_delete/#{oid}/')"
  383. end
  384.  
  385. def link_view to, oid
  386. link "[[view]]", "#{to}_view/#{oid}"
  387. end
  388.  
  389. def link_calc to, oid
  390. link "[[cal_tag]]", "#{to}_cal/#{oid}"
  391. end
  392.  
  393. def link_app to, oid
  394. link "[[app_tag]]", "#{to}_app/#{oid}"
  395. end
  396.  
  397. alias link_calculate link_calc
  398.  
  399. def link text, href
  400. %{<a href="#{href}">#{text}</a>}
  401. end
  402.  
  403.  
  404. ################################################################################
  405. # Loads the templates
  406. ################################################################################
  407. def load_default_tpl(filename)
  408. array = File.open(filename) { |yf| YAML::load(yf) }
  409. title = array[0]
  410. array.delete(array.first)
  411. if Tpl.find(:condition => "title = '#{title}'").length == 0
  412. # first we make the template
  413. tpl = Tpl.new(title)
  414. tpl.save
  415. array.each {|a|
  416. if filename == 'conf/tpl_def.yaml'
  417. # then we make the labels
  418. label = Label.new(a[0], a[1], a[2], a[3], a[4], a[5], a[6])
  419. label.save
  420. # finally we make the views of the templates
  421. tplview = Tplview.new(a[7], a[8], tpl.oid, label.oid)
  422. tplview.save
  423. else
  424. # find the label with the correct tag
  425. label = Label.find(:condition => "tag = #{a[1]}")
  426. # finally we make the views of the templates
  427. tplview = Tplview.new(a[7], a[8], tpl.oid, label[0].oid)
  428. tplview.save
  429. end
  430. }
  431. end
  432. end
  433.  
  434. #############################################################################
  435. # creates a selections tag for use as a table row <TR></TR> included
  436. #############################################################################
  437. # label: the label after localization or just plain string
  438. # id: the id name, e.g. property_title
  439. # name: the name, e.g. title
  440. # opt_labs: array of option labels after localization or plain strings
  441. # opt_vals: array of option values; either localized string, plain strings or other values
  442. # sel_val: the value in opt_vals that should be selected, if nil default selection occurs
  443. #############################################################################
  444.  
  445. #############################################################################
  446. # code generation for templates
  447. def my_sel_tag(label, id, name, opt_labs, opt_vals, sel_val)
  448. str = %{
  449. <tr><th>#{label}</th><td>
  450. <select size="1" id="#{id}" name="#{name}">
  451. }
  452. if (opt_vals != nil && opt_labs != nil)
  453. for i in 0 ... opt_labs.length do
  454. str << %{<option }
  455. str << %{selected="selected" } if sel_val.to_s == opt_vals[i].to_s
  456. str << %{value="#{opt_vals[i]}">#{opt_labs[i]}</option>}
  457. end
  458. end
  459. str << %{</select></td></tr>}
  460. return str
  461. end
  462.  
  463. def my_mult_sel_tag(label, id, name, opt_labs, opt_vals, sel_val)
  464. str = %{
  465. <tr><th>#{label}</th><td>
  466. <select multiple size="3" id="#{id}" name="#{name}">
  467. }
  468. if (opt_vals != nil && opt_labs != nil)
  469. for i in 0 ... opt_labs.length do
  470. str << %{<option }
  471. sel_val.each{ |ss|
  472. str << %{selected } if ss == opt_vals[i]
  473. }
  474. str << %{value="#{opt_vals[i]}">#{opt_labs[i]}</option>}
  475. end
  476. end
  477. str << %{</select></td></tr>}
  478. return str
  479. end
  480.  
  481. def my_pass_field_tag(label, id, name, val = nil)
  482. str = %{
  483. <tr><th>#{label}</th><td>
  484. <input id="#{id}" name="#{name}" type="password"
  485. }
  486. str << %{value="#{val}"} if val != nil
  487. str << %{size="40"/>
  488. </td></tr>
  489. }
  490. return str
  491. end
  492.  
  493. # options:
  494. # :properties => [:update_time, :creation_time]
  495. def control_for(obj, *options)
  496. options = options.first
  497. properties = options[:properties] ? options[:properties] : obj.class.properties
  498. props = properties.map do |prop|
  499. obj.class.properties[prop]
  500. end
  501. @props = props.map do |prop|
  502. control = Nitro::Form::Control.fetch(obj, prop, options).render
  503. Nitro::FormHelper::FormBuilder.element(prop, control)
  504. end
  505. end
  506.  
  507. def nc_map(values)
  508. values.map! do |v|
  509. v.map do |vv|
  510.  
  511. case vv
  512. when vv.is_a?(Hash)
  513. vv[@locale.to_s]
  514. when vv.to_f
  515. vv.to_f.nan? ? nc(0.0) : nc(vv.to_f)
  516. else
  517. vv
  518. end
  519. end
  520. end
  521. return values
  522. end
  523.  
  524. def summup(values)
  525. values = values.map{|v|
  526. v.map{|vv|
  527. vv.is_a?(Hash) ? vv[@locale.to_s] : vv
  528. }
  529. }
  530. sum = values.first.dup
  531. traverse = values - sum
  532.  
  533. sum.map!{ |x|
  534. x.to_f.to_s == x.to_s ? x : ""
  535. }
  536.  
  537. (0...sum.size).each do |i|
  538. traverse.each do |t|
  539. sum[i] += t[i] unless t[i].nil? || sum[i] == "" || t[i].to_f.nan?
  540. end
  541. end
  542.  
  543. sum[0] = @lc['cf_total']
  544.  
  545. return nc_map( values + [sum] )
  546. end
  547. end
  548.  
  549. =begin
  550. module Form
  551. # select :values => [1,2,3], :labels => [:first, :second, :third], :class => "foo", :selected => 1
  552. # =>
  553. def self.select(options)
  554. values = options.delete :values
  555. labels = options.delete :labels
  556. selected = options.delete :selected
  557.  
  558. tag = '<select' << options.inject(''){ |s,v| s+%{ #{v[0]}="#{v[1]}"} } << '>'
  559. tag << values.zip(labels).map{ |value,label|
  560. %{<option value="#{value}"#{' selected="selected"' if value == selected}>#{label}</option>}
  561. }.join
  562. tag << '</select>'
  563. end
  564. end
  565. =end
  566.  
  567. module Nitro
  568. class Controller
  569. include Various
  570. include AuthHelper
  571. include ErrorHelper
  572. include Nitro::XhtmlHelper
  573. include Nitro::TableHelper
  574. @lc = Glue::Localization.get
  575.  
  576. wrap LocalizationAspect, :pre => :localize
  577.  
  578. def lc *args
  579. return @lc if args.empty?
  580. ar = args.map{|a| a.to_s.gsub(/\[\[(.*?)\]\]/, '\1')}
  581. ar.map!{|a| @lc[a] ? @lc[a] : a}
  582. ar = ar.size <= 1 ? ar.first : ar
  583. end
  584. end
  585. end
Add Comment
Please, Sign In to add comment