-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrys_helpers.rb
592 lines (525 loc) · 15.1 KB
/
terrys_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
310
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
module Terrys_helpers
def admin_nav
render :partial=>'layouts/admin_nav'
end
def body(admin=nil)
if admin
render :partial=>'layouts/body_for_admin'
else
render :partial=>'layouts/body'
end
end
def boilerplate
render :partial=>'layouts/boilerplate'
end
def button(text='Doh! You forgot to add text')
'<button>'+text+'</button>'
end
def check_or_cross(v=nil)
cross_or_check(v)
end
def check_or_blank(v=nil)
if v==true or (v and v>0)
image_tag('check.gif', :alt=>'Yes')
else
''
end
end
def comment_tag_selector(thing=nil)
if thing and not thing.comment_tag_list.empty?
@tag_object_id=thing.rails_id
@tags=thing.comment_tag_list.sort
@tag_controller=thing.class.to_s.downcase.pluralize
@update=thing.class.to_s.downcase+'_table'
render :partial=>'layouts/tag_selector'
end
end
def cross_or_check(v=nil)
if v
image_tag('check.gif', :alt=>'Yes')
else
image_tag('cross.gif', :alt=>'No')
end
end
# takes a number and options hash and outputs a string in any currency format
def currencify(number, options={})
if number
# :currency_before => false puts the currency symbol after the number
# default format: £12,345,678.90
options = {:currency_symbol => "£", :delimiter => ",", :decimal_symbol => ".", :currency_before => true}.merge(options)
# split integer and fractional parts
int, frac = ("%.2f" % number).split('.')
# insert the delimiters
int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
if options[:currency_before]
options[:currency_symbol] + int + options[:decimal_symbol] + frac
else
int + options[:decimal_symbol] + frac + options[:currency_symbol]
end
end
end
def cycle_rowcolors
cycle('#ffffff','#dddddd')
end
def default_javascript
if @default_javascript
#javascript_include_tag(:defaults)+javascript_include_tag('control.tabs.2.1.1.js')
#javascript_include_tag(:defaults)+calendar_date_select_includes+javascript_include_tag('control.tabs.2.1.1.js')
javascript_include_tag(:defaults)+javascript_include_tag('calendar_date_select/calendar_date_select')+javascript_include_tag('control.tabs.2.1.1.js')
end
end
def description
@description||'Set Description'
end
def editable_tag_list(subject,specs={})
specs={:spinner=>'tag_editor', :url=>{:controller=>'home',:action=>nil, :id=>nil}}.merge(specs)
if subject
if @loggeduser.is_authorized?(specs[:url][:controller],specs[:url][:action])
tags=subject.tag_list.uniq.sort
t='<form action="post", method="home">'
t+='Tags:'
t+=token_tag
t+=hidden_field_tag 'comment_id', subject.rails_id
t+=text_area_tag 'tag_list', tags, :rows=>1, :style=>'width: 80%'
t+=submit_to_remote 'tag_list_submit_'+subject.rails_id.to_s, 'update', :url=>specs[:url], :update=>specs[:update], :before => "Element.show('spinner_"+specs[:spinner]+"')", :success => "Element.hide('spinner_"+specs[:spinner]+"')"
t+=spinner(specs[:spinner])
t+='</form>'
t
else
if subject.tag_list.empty?
'No tags for this '+subject.class.to_s
else
'Tags: '+subject.tag_list.uniq.sort.join(', ')
end
end
end
end
def flash_show
unless @no_flash
if flash[:error]
render :partial=>'layouts/flash_error'
elsif flash[:warning]
render :partial=>'layouts/flash_warning'
elsif flash[:notice]
render :partial=>'layouts/flash_notice'
end
end
flash=nil
end
def form_row(a=[],rowoptions={:bgcolor=>cycle_rowcolors})
@rowoptions=rowoptions
if a.empty?
a=[' ']
else
a.each_with_index do |element,index|
if element.blank?
a[index]=' '
end
end
end
return render(:partial=>'layouts/form_row', :collection=>[a])
end
def function_toggler(subject,action,specs={})
specs.symbolize_keys!
if subject and action
span_id=subject.class.to_s.downcase+'_'+subject.rails_id.to_s
specs={ :on_text=>'on',
:off_text=>'off',
:update=>span_id}.merge(specs)
end
'<span id="'+specs[:update]+'">'+function_toggler_guts(subject,action,specs)+'</span>'
end
def function_toggler_guts(subject,action,specs={})
specs.symbolize_keys!
specs={ :on_text=>'on',
:off_text=>'off'}.merge(specs)
image=on_off_image(subject,action,specs)
link_to_remote(image,:update=>specs[:update],:url=>{:controller=>subject.class.to_s.downcase.pluralize,:action=>'toggle_function',:id=>subject,:class=>subject.class.to_s,:function=>action, :specs=>specs})
end
def generic_link(asset,thumbnail,specs={})
link_to generic_show(asset,thumbnail,specs), asset.public_filename
end
def generic_show(asset,thumbnail,specs={})
if asset
if asset.is_image?
image_tag(asset.public_filename(thumbnail),specs)
elsif asset.is_video?
image_tag('video_icon.gif', specs)
end
else
if specs[:alt]
specs[:alt]
else
asset.full_filename
end
end
end
#shows a block of coloured text based on value of given attribute
#assumes that good is >0 and bad is <0
#:invert=>true inverts the assumption
def good_or_bad(v=nil,specs={})
specs={ :good_text=>'GOOD',
:bad_text=>'BAD',
:good_color=>'#ffffff',
:good_background=>'#00ff00',
:bad_color=>'#ffffff',
:bad_background=>'#ff0000',
:zero_good=>nil,
:zero_bad=>nil,
:nil_good=>nil,
:nil_bad=>nil,
:invert=>nil
}.merge(specs)
if v
if specs[:invert]
v=v*-1
end
if v>0
background=specs[:good_background]
color=specs[:good_color]
text=specs[:good_text]
elsif v<0
background=specs[:bad_background]
color=specs[:bad_color]
text=specs[:bad_text]
elsif specs[:zero_good]
background=specs[:good_background]
color=specs[:good_color]
text=specs[:good_text]
elsif specs[:zero_bad]
background=specs[:bad_background]
color=specs[:bad_color]
text=specs[:bad_text]
else
return
end
'<span style="background: '+background+'; color: '+color+'; padding: 2px; font-weight: 600;">'+text+'</span>'
else
if specs[:nil_good]
background=specs[:good_background]
color=specs[:good_color]
text=specs[:good_text]
return '<span style="background: '+background+'; color: '+color+'; padding: 2px; font-weight: 600;">'+text+'</span>'
elsif specs[:nil_bad]
background=specs[:bad_background]
color=specs[:bad_color]
text=specs[:bad_text]
return '<span style="background: '+background+'; color: '+color+'; padding: 2px; font-weight: 600;">'+text+'</span>'
end
end
end
def head_show
render :partial=>'layouts/head'
end
def job_finder
if @loggeduser.is_client?
@[email protected]_jobs.paginate(:order=>'title', :limit=>20, :per_page => 20, :page=>params[:page])
else
@jobs=Job.find(:all,:limit=>0).paginate(:order=>'title', :limit=>20, :per_page => 20, :page=>params[:page])
end
render :partial=>'jobs/finder'
end
def job_results
if @loggeduser.is_client?
render :partial=>'jobs/clientproperty', :collection=>@properties
else
render :partial=>'jobs/property', :collection=>@properties
end
end
def job_scheduled_status(job=nil)
if job
if job.scheduled?
nicedate(job.start_time)
else
'NOT SCHEDULED'
end
else
''
end
end
def keywords
@keywords||'Set Keywords'
end
#decides if the user sees an admin nav or a client nav
def main_nav
if @loggeduser
render :partial=>'layouts/main_nav'
end
end
def nav
render :partial=>'layouts/nav'
end
def nicedateandtime(t,separator=' - ')
nicedate(t)+separator+nicetime(t)
end
def nicestate_pickup(p)
if p
case(p.state)
when 'scheduled'
'Scheduled'
when 'scheduled_on_trip'
'Scheduled'
when 'no_access'
'No Access'
when 'postponed'
'Postponed'
when 'late'
'LATE'
when 'refused'
'REFUSED'
when 'cancelled'
'Cancelled'
when 'complete'
'Complete'
end
end
end
def nicetime(t=nil)
if t
t.strftime('%H:%M:%S')
end
end
def on_off_image(subject,action,specs={})
specs.symbolize_keys!
specs={ :on_text=>'on',
:off_text=>'off'}.merge(specs)
if subject.send(action)
if specs[:on_image]
image=image_tag(specs[:on_image], :alt=>specs[:on_text])
else
image=specs[:on_text]
end
else
if specs[:off_image]
image=image_tag(specs[:off_image], :alt=>specs[:off_text])
else
image=specs[:off_text]
end
end
image
end
def pencil(alt='edit')
image_tag 'pencil.gif', :alt=>alt
end
def ready_for_work
@ready_for_work=User.workers.select{|u| u.ready_for_work?}
render :partial=>'users/ready_for_work'
end
def refresh_bar(specs={})
specs={:spinner=>'refresh'}.merge(specs)
t='<div class="refresh">'
t+=link_to_remote 'Refresh', :url=>specs[:url], :update=>specs[:update], :before => "Element.show('spinner_"+specs[:spinner]+"')", :success => "Element.hide('spinner_"+specs[:spinner]+"')"
t+=spinner(specs[:spinner])
t+='</div>'
end
def site_title
@site_title||'Set Site Title in Application Controller'
end
def spinner(id='no_id')
image_tag("spinner.gif", :alt=>'spinner here', :align => 'absmiddle', :border=> 0, :id => "spinner_"+id, :style=>"display: none;" )
end
def time_select_from_commercial_weeks(specs={})
if specs[:finish]
iv=specs[:initial_value]||Time.end_of_this_commercial_week
else
iv=specs[:initial_value].start_of_the_commercial_week||Time.start_of_this_commercial_week
end
name=specs[:name]
id=specs[:id]
unless name
return
end
lower_limit=specs[:lower_limit]||20
upper_limit=specs[:upper_limit]||20
times=[iv]
count=iv
lower_limit.times do
count-=7.days
times<<count
end
count=iv
upper_limit.times do
count+=7.days
times<<count
end
times.sort!
select_tag name, options_for_select(times.collect{|t| ['Week '+(t.to_date.cweek).to_s+' '+t.year.to_s+' ('+nicedate(t)+')',t]}, iv), :id=>id
end
def tiny_mce
if @tiny_mce or @tinymce
render :partial=>'layouts/tiny_mce'
end
end
def title_show(title='You forgot to set a title')
if @title
title=@title
elsif @loggeduser and @loggeduser.circuit
end
if RAILS_ENV!='production'
title+=' ['+RAILS_ENV+']'
end
title
end
def top
render :partial=>'layouts/top'
end
def trashcan
image_tag('trash.gif',:alt=>'Delete')
end
def user_photo_50(user)
if user.photo
image_tag(user.photo.public_filename(:thumb50),:alt=>'Photo of '+user.full_name)
else
image_tag('anonymous_user_50.jpg', :alt=>'no photo')
end
end
def user_photo_100(user)
if user.photo
image_tag(user.photo.public_filename(:thumb100),:alt=>'Photo of '+user.full_name)
else
image_tag('anonymous_user_100.jpg', :alt=>'no photo')
end
end
def vehicle_photo_50(vehicle)
if vehicle.photo
image_tag(vehicle.photo.public_filename(:thumb50),:alt=>'Photo of '+vehicle.registration)
else
image_tag('anonymous_vehicle_50.gif', :alt=>'no photo')
end
end
def vehicle_photo_100(vehicle)
if vehicle.photo
image_tag(vehicle.photo.public_filename(:thumb100),:alt=>'Photo of '+vehicle.registration)
else
image_tag('anonymous_vehicle_100.gif', :alt=>'no photo')
end
end
def check_box_table(subject,potentials,variabletoshow, params={})
unless (subject.nil? || potentials.nil? || potentials.empty?)
contentarray=[]
potentials.each do |p|
itemarray=[]
pv=p.class.to_s.gsub(/^([A-Z].*)([A-Z])/,'\1_\2').gsub(/^_/,'').downcase
itemarray << check_box_tag(subject.class.to_s.downcase+"["+pv+"_ids][]",p.id,subject.send(pv.pluralize).include?(p))
itemarray << p.send(variabletoshow)+"\n"
contentarray << itemarray
end
return print_in_table_columns(:content=>contentarray, :cols=>params[:cols], :bunches=>params[:bunches], :cell=>params[:cell])
end
end
def nicedate(dateortime, time=nil)
if dateortime
if time
return dateortime.strftime("%b %d, %Y - %H:%M:%S")
else
return dateortime.strftime("%b %d, %Y")
end
end
end
def nl2br(s)
s.gsub(/\n/,'<br/>')
end
def pad_with_leading_zeros(string, length)
if string.size< length
(length-string.size).times do
string="0"+string
end
end
return string
end
def print_in_table_columns(specs)
content = specs[:content]||0
cols = specs[:cols]||0
bunches = specs[:bunches]||0
tableoptions = specs[:table]
rowoptions = specs[:row]
celloptions = specs[:cell]
colcount=0
bunchcount=0
contentcount=0
if content.nil? || content.empty?
return nil
end
table_tag="<table"
if tableoptions
tableoptions.each do |key,value|
table_tag+=' '+key.to_s+'="'+value.to_s+'"'
end
end
table_tag+=">"
row_tag="<tr"
if rowoptions
rowoptions.each do |key,value|
row_tag+=' '+key.to_s+'="'+value.to_s+'"'
end
end
row_tag+=">"
cell_tag="<td"
if celloptions
celloptions.each do |key,value|
cell_tag+=' '+key.to_s+'="'+value.to_s+'"'
end
end
cell_tag+=">"
if cols>0
itemspercol=content.size/cols
end
tablecount=0
output=""
output+=table_tag
tablecount+=1
if bunches>0 && cols>1
output+=row_tag
output+=cell_tag
output+=table_tag
tablecount+=1
end
content.each do |c|
output+=row_tag
c.each do |item|
output+=cell_tag
output+=item
output+="</td>"
end
output+="</tr>"
contentcount+=1
bunchcount+=1
if bunches>0 && cols<2 && bunchcount==bunches
output+=row_tag
output+=cell_tag
output+=" "
output+="</td>"
output+="</tr>"
bunchcount=0
elsif bunches>0 && cols>1 && bunchcount==bunches
output+="</table>"
tablecount-=1
output+="</td>"
bunchcount=0
colcount+=1
if colcount==cols
output+="</tr>"
output+=row_tag
output+="<td colspan="+cols.to_s+">"
output+=" "
output+="</td>"
output+="</tr>"
output+=row_tag
colcount=0
end
output+=cell_tag
output+=table_tag
tablecount+=1
end
end
output+="</table>"
tablecount-=1
if tablecount>0
output+="</td>"
output+="</tr>"
output+="</table>"
end
output
end
end