Class: ArgsHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/args-handler.rb

Class Method Summary collapse

Class Method Details

.attr_html(attrs) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/args-handler.rb', line 50

def self.attr_html(attrs)
  return "" if attrs.length <= 0
  
  html = ""
  attrs.each do |key, val|
    html << " #{key}=\"#{html_escape(val)}\""
  end
  
  return html
end

.checkval(value, val1, val2 = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/args-handler.rb', line 5

def self.checkval(value, val1, val2 = nil)
  if val2 != nil
    if !value or value == "" or value == "false"
      return val2
    else
      return val1
    end
  else
    if !value or value == "" or value == "false"
      return val1
    else
      return value
    end
  end
end

.html_escape(str) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
# File 'lib/args-handler.rb', line 355

def self.html_escape(str)
  str = str.to_s
  
  if Kernel.const_defined?("CGI")
    return CGI.escape_html(str)
  elsif Kernel.const_defined?("Knj")
    return Knj::Web.html(str)
  else
    raise "Dont know how to HTML-escape string..."
  end
end

.input(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/args-handler.rb', line 61

def self.input(args)
  if args.key?(:value)
    if args[:value].is_a?(Array) and (args[:value].first.is_a?(NilClass) or args[:value].first == false)
      value = nil
    elsif args[:value].is_a?(Array)
      if !args[:value][2] or args[:value][2] == :key
        value = args[:value].first[args[:value][1]]
      elsif args[:value][2] == :callb
        value = args[:value].first.send(args[:value][1])
      else
        value = args[:value]
      end
    elsif args[:value].is_a?(String) or args[:value].is_a?(Integer)
      value = args[:value].to_s
    else
      value = args[:value]
    end
  end
  
  args[:value_default] = args[:default] if args[:default]
  
  if value.is_a?(NilClass) and args[:value_default]
    value = args[:value_default]
  elsif value.is_a?(NilClass)
    value = ""
  end
  
  if value and args.key?(:value_func) and args[:value_func]
    cback = args[:value_func]
    
    if cback.is_a?(Method)
      value = cback.call(value)
    elsif cback.is_a?(Array)
      value = Php4r.call_user_func(args[:value_func], value)
    elsif cback.is_a?(Proc)
      value = cback.call(value)
    else
      raise "Unknown class: #{cback.class.name}."
    end
  end
  
  value = args[:values] if args[:values]
  args[:id] = args[:name] if !args[:id]
  
  if !args[:type]
    if args[:opts]
      args[:type] = :select
    elsif args[:name] and args[:name].to_s[0..2] == "che"
      args[:type] = :checkbox
    elsif args[:name] and args[:name].to_s[0..3] == "file"
      args[:type] = :file
    else
      args[:type] = :text
    end
  else
    args[:type] = args[:type].to_sym
  end
  
  attr = {
    "name" => args[:name],
    "id" => args[:id],
    "type" => args[:type],
    "class" => "input_#{args[:type]}"
  }
  attr.merge!(args[:attr]) if args[:attr]
  attr["disabled"] = "disabled" if args[:disabled]
  attr["maxlength"] = args[:maxlength] if args.key?(:maxlength)
  
  raise "No name given to the ArgsHandler::input()-method." if !args[:name] and args[:type] != :info and args[:type] != :textshow and args[:type] != :plain and args[:type] != :spacer and args[:type] != :headline
  
  css = {}
  css["text-align"] = args[:align] if args.key?(:align)
  css.merge!(args[:css]) if args.key?(:css)
  
  attr_keys = [:onchange]
  attr_keys.each do |tag|
    if args.key?(tag)
      attr[tag] = args[tag]
    end
  end
  
  classes_tr = []
  classes_tr += args[:classes_tr] if args[:classes_tr]
  
  tr_attrs = {}
  label_attrs = {}
  td_attrs = {
    :class => :tdc
  }
  
  if !args[:id].to_s.empty?
    tr_attrs[:id] = "#{args[:id]}_tr"
    td_attrs[:id] = "#{args[:id]}_content"
    label_attrs[:for] = args[:id]
  end
  
  if !classes_tr.empty?
    classes_tr_html = " class=\"#{classes_tr.join(" ")}\""
  else
    classes_tr_html = ""
  end
  
  if args.key?(:title)
    title_html = html_escape(args[:title])
  elsif args.key?(:title_html)
    title_html = args[:title_html]
  end
  
  html = ""
  
  classes = ["input_#{args[:type]}"]
  classes = classes | args[:classes] if args.key?(:classes)
  attr["class"] = classes.join(" ")
  
  if args[:type] == :checkbox
    attr["value"] = args[:value_active] if args.key?(:value_active)
    attr["checked"] = "checked" if value.is_a?(String) and value == "1" or value.to_s == "1" or value.to_s == "on" or value.to_s == "true"
    attr["checked"] = "checked" if value.is_a?(TrueClass)
    
    html << "<tr#{classes_tr_html}>"
    html << "<td colspan=\"2\" class=\"tdcheck\">"
    html << "<input#{self.attr_html(attr)} />"
    html << "<label#{attr_html(label_attrs)}\">#{title_html}</label>"
    html << "</td>"
    html << "</tr>"
  elsif args[:type] == :headline
    html << "<tr#{classes_tr_html}><td colspan=\"2\"><h2 class=\"input_headline\">#{title_html}</h2></td></tr>"
  elsif args[:type] == :spacer
    html << "<tr#{classes_tr_html}><td colspan=\"2\">&nbsp;</td></tr>"
  else
    html << "<tr#{classes_tr_html}#{attr_html(tr_attrs)}>"
    html << "<td class=\"tdt\" id=\"#{html_escape("#{args[:id]}_label")}\"><div><label>"
    html << title_html
    html << "</label></div></td>"
    html << "<td#{self.style_html(css)}#{attr_html(td_attrs)}><div>"
    
    if args[:type] == :textarea
      if args.key?(:height)
        if (Float(args[:height]) rescue false)
          css["height"] = "#{args[:height]}px"
        else
          css["height"] = args[:height]
        end
      end
      
      attr["class"] = "input_textarea"
      attr["name" ] = args[:name]
      attr["id"] = args[:id]
      attr.delete("type")
      
      html << "<textarea#{self.style_html(css)}#{self.attr_html(attr)}>#{value}</textarea>"
    elsif args[:type] == :fckeditor
      args[:height] = 400 if !args[:height]
      
      require "/usr/share/fckeditor/fckeditor.rb" if !Kernel.const_defined?(:FCKeditor)
      fck = FCKeditor.new(args[:name])
      fck.Height = args[:height].to_i
      fck.Value = value
      html << fck.CreateHtml
    elsif args[:type] == :ckeditor
      args[:height] = 400 if !args[:height]
      require "ckeditor4ruby" if !Kernel.const_defined?(:CKEditor)
      ck = CKEditor.new
      ck.return_output = true
      html << ck.editor(args[:name], value)
    elsif args[:type] == :select
      attr[:multiple] = "multiple" if args[:multiple]
      attr[:size] = args[:size] if args[:size]
      
      html << "<select#{self.attr_html(attr)}>"
      html << ArgsHandler.opts(args[:opts], value, args[:opts_args])
      html << "</select>"
      
      if args[:moveable]
        html << "<div style=\"padding-top: 3px;\">"
        html << "<input type=\"button\" value=\"#{_("Up")}\" onclick=\"select_moveup($('##{args[:id]}'));\" />"
        html << "<input type=\"button\" value=\"#{_("Down")}\" onclick=\"select_movedown($('##{args[:id]}'));\" />"
        html << "</div>"
      end
    elsif args[:type] == :imageupload
      html << "<table class=\"designtable\"><tr#{classes_tr_html}><td style=\"width: 100%;\">"
      html << "<input type=\"file\" name=\"#{args[:name].html}\" class=\"input_file\" />"
      html << "</td><td style=\"padding-left: 5px;\">"
      
      raise "No path given for imageupload-input." if !args.key?(:path)
      raise "No value given in arguments for imageupload-input." if !args.key?(:value)
      
      path = args[:path].gsub("%value%", value.to_s).untaint
      if File.exists?(path)
        html << "<img src=\"image.rhtml?path=#{html_escape(self.urlenc(path))}&smartsize=100&rounded_corners=10&border_color=black&force=true&ts=#{Time.new.to_f}\" alt=\"Image\" />"
        
        if args[:dellink]
          dellink = args[:dellink].gsub("%value%", value.to_s)
          html << "<div style=\"text-align: center;\">(<a href=\"javascript: if (confirm('#{_("Do you want to delete the image?")}')){location.href='#{dellink}';}\">#{_("delete")}</a>)</div>"
        end
      end
      
      html << "</td></tr></table>"
    elsif args[:type] == :file
      attr["type"] = args[:type]
      attr["class"] = "input_#{args[:type]}"
      attr["name"] = args[:name]
      
      html << "<input#{self.attr_html(attr)} />"
    elsif args[:type] == :textshow or args[:type] == :info
      html << value.to_s
    elsif args[:type] == :plain
      html << "#{Php4r.nl2br(html_escape(value))}"
    elsif args[:type] == :editarea
      css["width"] = "100%"
      css["height"] = args[:height] if args.key?(:height)
      
      attr["id"] = args[:id]
      attr["name"] = args[:name]
      
      html << "<textarea#{self.attr_html(attr)}#{self.style_html(css)}>#{value}</textarea>"
      
      jshash = {
        "id" => args[:id],
        "start_highlight" => true
      }
      
      pos_keys = [:skip_init, :allow_toggle, :replace_tab_by_spaces, :toolbar, :syntax]
      pos_keys.each do |key|
        jshash[key.to_s] = args[key] if args.key?(key)
      end
      
      html << "<script type=\"text/javascript\">"
      html << "function knj_web_init_#{args[:name]}(){"
      html << "editAreaLoader.init(#{Php4r.json_encode(jshash)});"
      html << "}"
      html << "</script>"
    elsif args[:type] == :numeric
      attr[:type] = :text
      attr[:value] = value
      html << "<input#{self.attr_html(attr)} />"
    else
      attr[:value] = value
      html << "<input#{self.attr_html(attr)} />"
    end
    
    html << "</div></td></tr>"
  end
  
  html << "<tr#{classes_tr_html}><td colspan=\"2\" class=\"tdd\">#{args[:descr]}</td></tr>" if args[:descr]
  html = html.html_safe if html.respond_to?(:html_safe)
  
  return html
end

.input_sf(args) ⇒ Object

Returns the HTML using simle-form.



368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/args-handler.rb', line 368

def self.input_sf(args)
  arg = args[:arg]
  f = args[:sf]
  
  input_args = {:label => arg[:title], :id => arg[:id], :value => arg[:value]}
  
  if arg[:type] == :select
    input_args[:collection] = args[:opts].collect{ |key, val| [val, key] }
    input_args[:include_blank] = false
  end
  
  return f.input(arg[:name], input_args)
end

.inputs(arr) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/args-handler.rb', line 21

def self.inputs(arr)
  html = ""
  arr.each do |args|
    if RUBY_ENGINE == "rbx"
      html << self.input(args).to_s.encode(html.encoding)
    else
      html << self.input(args)
    end
  end
  
  html = html.html_safe if html.respond_to?(:html_safe)
  
  return html
end

.opts(opthash, curvalue = nil, opts_args = {}) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/args-handler.rb', line 311

def self.opts(opthash, curvalue = nil, opts_args = {})
  opts_args = {} if !opts_args
  
  return "" if !opthash
  cname = curvalue.class.name
  curvalue = curvalue.id if (cname == "Knj::Db_row" or cname == "Knj::Datarow" or cname == "Baza::Model" or cname == "Baza::ModelCustom")
  
  html = ""
  addsel = " selected=\"selected\"" if !curvalue
  
  html << "<option#{addsel} value=\"\">#{_("Add new")}</option>" if opts_args and (opts_args[:add] or opts_args[:addnew])
  html << "<option#{addsel} value=\"\">#{_("Choose")}</option>" if opts_args and opts_args[:choose]
  html << "<option#{addsel} value=\"\">#{_("None")}</option>" if opts_args and opts_args[:none]
  html << "<option#{addsel} value=\"\">#{_("All")}</option>" if opts_args and opts_args[:all]
  
  if opthash.is_a?(Hash) or opthash.class.to_s == "Dictionary"
    opthash.each do |key, value|
      html << "<option"
      sel = false
      
      if curvalue.is_a?(Array) and curvalue.index(key) != nil
        sel = true
      elsif curvalue.to_s == key.to_s
        sel = true
      elsif curvalue and curvalue.respond_to?(:is_knj?) and curvalue.id.to_s == key.to_s
        sel = true
      end
      
      html << " selected=\"selected\"" if sel
      html << " value=\"#{html_escape(key)}\">#{html_escape(value)}</option>"
    end
  elsif opthash.is_a?(Array)
    opthash.each_index do |key|
      if opthash[key.to_i] != nil
        html << "<option"
        html << " selected=\"selected\"" if curvalue.to_s == key.to_s
        html << " value=\"#{html_escape(key)}\">#{html_escape(opthash[key])}</option>"
      end
    end
  end
  
  return html
end

.style_html(css) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/args-handler.rb', line 36

def self.style_html(css)
  return "" if css.length <= 0
  
  str = " style=\""
  
  css.each do |key, val|
    str << "#{key}: #{val};"
  end
  
  str << "\""
  
  return str
end