-
Notifications
You must be signed in to change notification settings - Fork 358
/
application_helper.rb
1364 lines (1229 loc) · 47.3 KB
/
application_helper.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module ApplicationHelper
include ApplicationHelper::ViewsShared
include ApplicationHelper::Flash
include ApplicationHelper::Listnav
include ApplicationHelper::Navbar
include ApplicationHelper::PageLayouts
include ApplicationHelper::Tasks
include Mixins::Sandbox
include JsHelper
include StiRoutingHelper
include ToolbarHelper
include TextualSummaryHelper
ActionView::Helpers::NumberHelper.prepend NumberHelper # override rails number helper with our needs for number_to_human_size
include Title
include ReactjsHelper
include Webpack
VALID_PERF_PARENTS = {
"EmsCluster" => :ems_cluster,
"Host" => :host
}.freeze
# Need to generate paths w/o hostname by default to make proxying work.
#
def url_for_only_path(args)
url_for(:only_path => true, **args)
end
def api_collection_path(klass, *options)
identifier = api_identifier_by_class(klass)
send("api_#{identifier}_path", *options)
end
def api_resource_path(record, *options)
identifier = api_identifier_by_class(record.class).to_s.singularize
send("api_#{identifier}_path", nil, record.id, *options)
end
def api_identifier_by_class(klass)
raise "API plugin not loaded" unless defined?(Api::ApiConfig)
@api_cc ||= Api::CollectionConfig.new
@api_cc.name_for_klass(klass) || @api_cc.name_for_subclass(klass)
end
def settings(*path)
@settings ||= {}
@settings.fetch_path(*path)
end
def settings_default(default, *path)
settings(*path) || default
end
def websocket_origin
proto = request.ssl? ? 'wss' : 'ws'
# Retrieve the host that needs to be explicitly allowed for websocket connections
host = if request.env['HTTP_X_FORWARDED_HOST']
# Use the first proxy (production)
request.env['HTTP_X_FORWARDED_HOST'].split(/,\s*/).first
else
# Use the HOST header (development)
request.env['HTTP_HOST']
end
"#{proto}://#{host}"
end
# Create a hidden div area based on a condition (using for hiding nav panes)
def hidden_div_if(condition, options = {}, &block)
hidden_tag_if(:div, condition, options, &block)
end
# Create a hidden span tag based on a condition (using for hiding nav panes)
def hidden_span_if(condition, options = {}, &block)
hidden_tag_if(:span, condition, options, &block)
end
def hidden_tag_if(tag, condition, options = {}, &block)
options[:style] = "display: none" if condition
if block_given?
content_tag(tag, options, &block)
else
# TODO: Remove this old open-tag-only way in favor of block style
tag(tag, options, true)
end
end
def hover_class(item)
if item.fetch_path(:link) ||
item.fetch_path(:value).kind_of?(Array) &&
item[:value].any? { |val| val[:link] }
''
else
'no-hover'
end
end
# Check role based authorization for a UI task
def role_allows?(**options)
features = Array(options[:feature])
if features.blank?
$log.debug("Auth failed - no feature was specified (required)")
return false
end
# Detect if queried features are missing from the database and possibly invalid
if !Rails.env.production? && features.detect { |feature| !MiqProductFeature.feature_exists?(feature) }
message = "#{__method__} no feature was found with identifier: #{features.inspect}. Correct the identifier or add it to miq_product_features.yml."
identifiers = MiqProductFeature.features.keys
if Rails.env.development?
raise message
elsif Rails.env.test? && identifiers.length >= 5
raise("#{message} Note: detected features: #{identifiers.inspect}")
end
end
Rbac.role_allows?(:user => User.current_user, **options) rescue false
end
module_function :role_allows?
public :role_allows?
# NB: This differs from controller_for_model; until they're unified,
# make sure you have the right one.
def model_to_controller(record)
record.class.base_model.name.underscore
end
CONTROLLER_TO_MODEL = {
"ManageIQ::Providers::CloudManager::Template" => VmOrTemplate,
"ManageIQ::Providers::CloudManager::Vm" => VmOrTemplate,
"ManageIQ::Providers::InfraManager::Template" => VmOrTemplate,
"ManageIQ::Providers::InfraManager::Vm" => VmOrTemplate,
"ManageIQ::Providers::AutomationManager" => ConfigurationScript
}.freeze
def controller_to_model
model = self.class.model
CONTROLLER_TO_MODEL[model.to_s] || model
end
MODEL_STRING = {
"all_vms" => VmOrTemplate,
"all_miq_templates" => MiqTemplate,
"based_volumes" => CloudVolume,
"instances" => Vm,
"images" => MiqTemplate,
"groups" => Account,
"users" => Account,
"host_services" => SystemService,
"chargebacks" => ChargebackRate,
"playbooks" => ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Playbook,
"physical_servers_with_host" => PhysicalServer,
"manageiq/providers/automation_managers" => ConfigurationScript,
"vms" => VmOrTemplate,
"ServiceCatalog" => ServiceTemplate
}.freeze
HAS_ASSOCATION = {
"groups" => "groups",
"users" => "users",
"event_logs" => "event_logs",
"OsProcess" => "processes",
"scan_histories" => "scan_histories",
"based_volumes" => "based_volumes",
"PersistentVolume" => "persistent_volumes",
"PhysicalSwitch" => "physical_switches"
}.freeze
def model_to_report_data
# @report_data_additional_options[:model] is most important, others can be removed
return @report_data_additional_options[:model] if @report_data_additional_options && @report_data_additional_options[:model]
return @display.classify if @display && @display != "main"
return params[:db].classify if params[:db]
return params[:display].classify if params[:display]
controller.class.model.to_s if defined? controller.class.model
end
def model_string_to_constant(model_string)
MODEL_STRING[model_string] || model_string.singularize.classify.constantize
end
def restful_routed?(record_or_model)
model = if record_or_model.kind_of?(Class)
record_or_model
else
record_or_model.class
end
model = ui_base_model(model)
respond_to?("#{model.model_name.route_key}_path")
end
# Returns whether records support feature or not.
#
# Params:
# records - an array of record instances or a single instance of a record
# feature - symbol
# Returns:
# boolean - true if all records support the feature
# - false in case the record (or one of many records) does not
# support the feature
def records_support_feature?(records, feature)
Array.wrap(records).all? { |record| record.supports?(feature.to_sym) }
end
# Create a url for a record that links to the proper controller
def url_for_db(db, action = "show", item = nil)
if item && restful_routed?(item)
return polymorphic_path(item)
end
if @vm && %w[Account User Group Patch GuestApplication].include?(db)
return url_for_only_path(:controller => "vm_or_template",
:action => @lastaction,
:id => @vm,
:show => @id)
elsif @host && %w[Patch GuestApplication].include?(db)
return url_for_only_path(:controller => "host", :action => @lastaction, :id => @host, :show => @id)
else
controller, action = db_to_controller(db, action)
return url_for_only_path(:controller => controller, :action => action, :id => @id)
end
end
TREE_WITH_TAB = {
"diagnostics_server_list" => "svr",
"db_details" => "tb",
"report_info" => "msc"
}.freeze
# Create a url to show a record from the passed in view
def view_to_url(view, parent = nil)
association = view_to_association(view, parent)
if association.nil?
controller, action = db_to_controller(view.db)
if controller == "ems_cloud" && action == "show"
return ems_clouds_path
end
if controller == "ems_infra" && action == "show"
return ems_infras_path
end
if controller == "ems_physical_infra" && action == "show"
return ems_physical_infras_path
end
if controller == "ems_container" && action == "show"
return ems_containers_path
end
if controller == "ems_network" && action == "show"
return ems_networks_path
end
if controller == "ems_storage" && action == "show"
return ems_storages_path
end
if request[:controller] == 'service' && view.db == 'GenericObject'
action = 'show'
return url_for_only_path(:action => action, :id => params[:id]) + "?display=generic_objects&generic_object_id="
end
if @explorer
# showing a list view of another CI inside vmx
if %w[SecurityGroup
SecurityPolicy
SecurityPolicyRule
FloatingIp
NetworkRouter
NetwokrService
NetworkPort
CloudNetwork
CloudSubnet
LoadBalancer
CloudVolume].include?(view.db)
return url_for_only_path(:controller => controller, :action => "show") + "/"
elsif ["Vm"].include?(view.db) && parent && request.parameters[:controller] != "vm"
# this is to handle link to a vm in vm explorer from service explorer
return url_for_only_path(:controller => "vm_or_template", :action => "show") + "/"
elsif %w[MiqWidget
ConfigurationScript
MiqReportResult].include?(view.db) &&
%w[report].include?(request.parameters[:controller])
suffix = ''
if params[:tab_id] == "saved_reports" || params[:pressed] == "miq_report_run" || params[:action] == "reload"
suffix = x_node
end
return "/" + request.parameters[:controller] + "/tree_select?id=" + suffix
elsif %w[User MiqGroup MiqUserRole Tenant].include?(view.db) &&
%w[ops].include?(request.parameters[:controller])
if @tagging
return false # when tagging Users, Groups, Roles and Tenants, the table is non-clickable
else
return "/" + request.parameters[:controller] + "/tree_select/?id=" + x_node.split("-")[1]
end
elsif view.db == "MiqServer" &&
%w[ops report].include?(request.parameters[:controller])
return "/" + request.parameters[:controller] + "/tree_select/?id=" + TREE_WITH_TAB[active_tab]
elsif %w[ScanItemSet
MiqSchedule
PxeServer
PxeImageType
Storage
CustomizationTemplate].include?(view.db) &&
%w[ops pxe report].include?(params[:controller])
return "/#{params[:controller]}/tree_select/?id=#{TreeBuilder.get_prefix_for_model(view.db)}"
else
return url_for_only_path(:action => action) + "/" # In explorer, don't jump to other controllers
end
else
controller = case controller
when 'template_cloud'
'vm_cloud'
when 'template_infra'
'vm_infra'
when 'miq_ae_domain'
'miq_ae_class'
else
controller
end
return url_for_only_path(:controller => 'restful_redirect', :model => 'ExtManagementSystem') if controller == 'ext_management_system'
return url_for_only_path(:controller => controller, :action => action, :id => nil) + "/"
end
else
# need to add a check for @explorer while setting controller incase building a link for details screen to show items
# i.e users list view screen inside explorer needs to point to vm_or_template controller
return url_for_only_path(:controller => parent.kind_of?(VmOrTemplate) && !@explorer ? parent.class.base_model.to_s.underscore : request.parameters["controller"],
:action => association,
:id => parent.id) + "?#{@explorer ? "x_show" : "show"}="
end
end
def view_to_association(view, parent)
case view.db
when "OrchestrationStackOutput" then "outputs"
when "OrchestrationStackParameter" then "parameters"
when "OrchestrationStackResource" then "resources"
when 'AdvancedSetting', 'Filesystem', 'FirewallRule', 'GuestApplication', 'Patch',
'RegistryItem', 'ScanHistory', 'OpenscapRuleResult'
then view.db.tableize
when "SystemService"
case parent.class.base_class.to_s.downcase
when "host" then "host_services"
when "vm" then @lastaction
end
when "CloudService" then "host_cloud_services"
else view.scoped_association
end
end
# Convert a db name to a controller name and an action
def db_to_controller(db, action = "show")
action = "x_show" if @explorer
case db
when "ActionSet"
controller = "miq_action"
action = "show_set"
when "AutomationRequest", "MiqProvision"
controller = "miq_request"
action = "show"
when "ConditionSet"
controller = "condition"
when "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource"
controller = "ansible_repository"
when "ScanItemSet"
controller = "ops"
action = "ap_show"
when "User", "Group", "Patch", "GuestApplication"
controller = "vm"
action = @lastaction
when "Host" && action == 'x_show'
controller = "infra_networking"
action = @lastaction
when "MiqReportResult"
if params[:controller] == "chargeback_report"
controller = "chargeback_report"
action = "show"
else
controller = "report"
action = "show_saved"
end
when "MiqSchedule"
if request.parameters["controller"] == "report"
controller = "report"
action = "show_schedule"
else
controller = "ops"
action = "schedule_show"
end
when "MiqAeClass"
controller = "miq_ae_class"
action = "show_instances"
when "MiqAeInstance"
controller = "miq_ae_class"
action = "show_details"
when "PlacementGroup"
controller = "placement_group"
action = "show"
when "SecurityGroup"
controller = "security_group"
action = "show"
when "ServiceResource", "ServiceTemplate"
controller = "catalog"
when "ManageIQ::Providers::ExternalAutomationManager::ConfigurationScript"
controller = "configuration_script"
when "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Playbook"
controller = "ansible_playbook"
when "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Credential"
controller = "ansible_credential"
when "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Credential"
controller = "embedded_terraform_credential"
when "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::ConfigurationScriptSource"
controller = "embedded_terraform_repository"
when "ManageIQ::Providers::EmbeddedTerraform::AutomationManager::Template"
controller = "embedded_terraform_template"
when "ManageIQ::Providers::Workflows::AutomationManager::Workflow"
controller = "workflow"
when "ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource"
controller = "workflow_repository"
when "ManageIQ::Providers::Workflows::AutomationManager::Credential"
controller = "workflow_credential"
when "MiqWorker"
controller = request.parameters[:controller]
when "ManageIQ::Providers::ExternalAutomationManager", "OrchestrationStackOutput", "OrchestrationStackParameter", "OrchestrationStackResource",
"ManageIQ::Providers::CloudManager::OrchestrationStack",
"ManageIQ::Providers::CloudManager::CloudDatabase",
"ManageIQ::Providers::ConfigurationManager",
"ManageIQ::Providers::AnsibleTower::AutomationManager::ConfiguredSystem",
"ManageIQ::Providers::AnsibleTower::AutomationManager::Job", "ConfigurationScript"
controller = request.parameters[:controller]
when "ContainerVolume"
controller = "persistent_volume"
when /^ManageIQ::Providers::(\w+)Manager$/
controller = "ems_#{$1.underscore}"
when /^ManageIQ::Providers::(\w+)Manager::(\w+)$/
controller = "#{$2.underscore}_#{$1.underscore}"
when "EmsAutomation"
controller = "ems_automation"
when "GenericObject" && request.parameters[:controller] == 'service'
controller = request.parameters[:controller]
action = 'generic_object'
else
controller = db.underscore
end
return controller, action
end
# Method to create the center toolbar XML
def build_toolbar(tb_name)
_toolbar_builder.build_toolbar(tb_name)
end
def _toolbar_builder
ToolbarBuilder.new(
self,
binding,
:active => @active,
:changed => @changed,
:condition => @condition,
:condition_policy => @condition_policy,
:dashboard => @dashboard,
:display => @display,
:edit => @edit,
:explorer => @explorer,
:ght_type => @ght_type,
:html => @html,
:lastaction => @lastaction,
:layout => @layout,
:miq_request => @miq_request,
:msg_title => @msg_title,
:perf_options => @perf_options,
:policy => @policy,
:record => @record,
:report => @report,
:report_result_id => @report_result_id,
:request_tab => @request_tab,
:resolve => @resolve,
:sb => @sb,
:selected_zone => @selected_zone,
:settings => @settings,
:showtype => @showtype,
:tabform => @tabform,
:widget_running => @widget_running,
:widgetsets => @widgetsets,
:render_chart => @render_chart,
)
end
# Convert a field (Vm.hardware.disks-size) to a col (disks.size)
def field_to_col(field)
dbs, fld = field.split("-")
(dbs.include?(".") ? "#{dbs.split(".").last}.#{fld}" : fld)
end
def controller_model_name(controller)
ui_lookup(:model => (controller.camelize + "Controller").constantize.model.name)
end
def is_browser?(name)
browser_name = browser_info(:name)
name.kind_of?(Array) ? name.include?(browser_name) : (browser_name == name)
end
def is_browser_os?(os)
browser_os = browser_info(:os)
os.kind_of?(Array) ? os.include?(browser_os) : (browser_os == os)
end
def browser_info(typ)
session.fetch_path(:browser, typ).to_s
end
############# Following methods generate JS lines for render page blocks
def javascript_for_timer_type(timer_type)
case timer_type
when "Monthly"
[
javascript_hide("weekly_span"),
javascript_hide("daily_span"),
javascript_hide("hourly_span"),
javascript_show("monthly_span")
]
when "Weekly"
[
javascript_hide("daily_span"),
javascript_hide("hourly_span"),
javascript_hide("monthly_span"),
javascript_show("weekly_span")
]
when "Daily"
[
javascript_hide("hourly_span"),
javascript_hide("monthly_span"),
javascript_hide("weekly_span"),
javascript_show("daily_span")
]
when "Hourly"
[
javascript_hide("daily_span"),
javascript_hide("monthly_span"),
javascript_hide("weekly_span"),
javascript_show("hourly_span")
]
when nil
[]
else
[
javascript_hide("daily_span"),
javascript_hide("hourly_span"),
javascript_hide("monthly_span"),
javascript_hide("weekly_span")
]
end
end
# Show/hide the Save and Reset buttons based on whether changes have been made
def javascript_for_miq_button_visibility(display, prefix = nil)
if prefix
"miqButtons('#{display ? 'show' : 'hide'}', '#{prefix}');".html_safe
else
"miqButtons('#{display ? 'show' : 'hide'}');".html_safe
end
end
def javascript_for_miq_button_visibility_changed(changed)
return "" if changed == session[:changed]
session[:changed] = changed
javascript_for_miq_button_visibility(changed)
end
# reload all toolbars
def javascript_reload_toolbars
"sendDataWithRx({redrawToolbar: #{toolbar_from_hash.to_json}});"
end
def set_edit_timer_from_schedule(schedule)
@edit[:new][:timer] ||= ReportHelper::Timer.new
if schedule.run_at.nil?
t = Time.now.in_time_zone(@edit[:tz]) + 1.day # Default date/time to tomorrow in selected time zone
@edit[:new][:timer].typ = 'Once'
@edit[:new][:timer].start_date = "#{t.month}/#{t.day}/#{t.year}"
else
@edit[:new][:timer].update_from_miq_schedule(schedule.run_at, @edit[:tz])
end
end
# Check if a parent chart has been selected and applies
def perf_parent?
@perf_options[:model] == "VmOrTemplate" &&
@perf_options[:typ] != "realtime" &&
VALID_PERF_PARENTS.keys.include?(@perf_options[:parent])
end
# Determine the type of report (performance/trend/chargeback) based on the model
def model_report_type(model)
if model
if model.ends_with?("Performance", "MetricsRollup")
return :performance
elsif model == ApplicationController::TREND_MODEL
return :trend
elsif model.starts_with?("Chargeback")
return model.downcase.to_sym
end
end
nil
end
def show_taskbar_in_header?
return false if @explorer
return false if controller.action_name.end_with?("tagging_edit")
hide_actions = %w[
auth_error
change_tab
show
]
return false if @layout == "" && hide_actions.include?(controller.action_name)
hide_layouts = %w[
about
chargeback
container_dashboard
ems_infra_dashboard
exception
miq_ae_automate_button
miq_ae_class
miq_ae_export
miq_ae_tools
miq_policy_export
miq_policy_rsop
ops
pxe
report
server_build
]
return false if hide_layouts.include?(@layout)
return false if @layout == "configuration" && @tabform != "ui_4"
true
end
def taskbar_in_header?
# this is just @show_taskbar ||= show_taskbar_in_header? .. but nil
if @show_taskbar.nil?
@show_taskbar = show_taskbar_in_header?
else
@show_taskbar
end
end
# checking if any of the toolbar is visible
def toolbars_visible?
(@toolbars['history_tb'] || @toolbars['center_tb'] || @toolbars['view_tb']) &&
(@toolbars['history_tb'] != 'blank_view_tb' && @toolbars['history_tb'] != 'blank_view_tb' && @toolbars['view_tb'] != 'blank_view_tb')
end
def check_if_button_is_implemented
if !@flash_array && !@refresh_partial # if no button handler ran, show not implemented msg
add_flash(_("Button not yet implemented"), :error)
@refresh_partial = "layouts/flash_msg"
@refresh_div = "flash_msg_div"
elsif @flash_array && @lastaction == "show"
@ems = @record = identify_record(params[:id])
@refresh_partial = "layouts/flash_msg"
@refresh_div = "flash_msg_div"
end
end
# Return a blank tb if a placeholder is needed for AJAX explorer screens, return nil if no center toolbar to be shown
delegate :center_toolbar_filename, :to => :_toolbar_chooser
delegate :x_view_toolbar_filename, :to => :_toolbar_chooser
delegate :view_toolbar_filename, :to => :_toolbar_chooser
def _toolbar_chooser
ToolbarChooser.new(
self,
binding,
:alert_profiles => @alert_profiles,
:conditions => @conditions,
:dialog => @dialog,
:display => @display,
:explorer => @explorer,
:in_a_form => @in_a_form,
:lastaction => @lastaction,
:layout => @layout,
:nodetype => @nodetype,
:policies => @policies,
:record => @record,
:report => @report,
:sb => @sb,
:showtype => @showtype,
:tabform => @tabform,
:view => @view,
:center_toolbar => @center_toolbar
)
end
# Calculate hash of toolbars to render
#
# keys are toolbar <div> names and values are toobar identifiers (now YAML files)
#
def calculate_toolbars
toolbars = {}
if display_back_button? # taskbar branch
toolbars['summary_center_tb'] = controller.restful? ? "summary_center_restful_tb" : "summary_center_tb"
end
# FIXME: singular vs plural for controller.class.toolbar_singular
toolbars['center_tb'] = if controller.class.toolbar_plural.present? && params[:action] == 'show_list'
"#{controller.class.toolbar_plural}_center_tb"
elsif controller.class.toolbar_singular.present?
"#{controller.class.toolbar_singular}_center_tb"
elsif controller.try(:toolbar)
controller.toolbar.to_s
else
center_toolbar_filename
end
toolbars['custom_tb'] = controller.custom_toolbar
toolbars['view_tb'] = inner_layout_present? ? x_view_toolbar_filename : view_toolbar_filename
toolbars
end
# check if back to summary button needs to be show
def display_back_button?
# don't need to back button if @record is not there or @record doesnt have name or
# evm_display_name column, i.e MiqProvisionRequest
return false if @display == "dashboard"
if (@lastaction != "show" || (@lastaction == "show" && @display != "main")) &&
@record &&
(@record.respond_to?('name') && !@record.name.nil?)
return true
else
return false
end
end
def display_adv_search?
%w[auth_key_pair_cloud
storage_service
storage_resource
host_initiator
physical_storage
automation_manager_configured_system
availability_zone
ems_automation
cloud_database
cloud_network
cloud_object_store_container
cloud_object_store_object
cloud_subnet
cloud_tenant
cloud_volume
cloud_volume_backup
cloud_volume_snapshot
cloud_volume_type
configuration_job
configuration_profile
configuration_script
configured_system
container
container_build
container_group
container_image
container_image_registry
container_node
container_project
container_replicator
container_route
container_service
container_template
ems_cloud
ems_cluster
ems_configuration
ems_container
ems_infra
ems_network
ems_physical_infra
ems_storage
flavor
floating_ip
host
host_initiator_group
host_aggregate
load_balancer
miq_template
network_port
network_router
network_service
network_service_entry
offline
orchestration_stack
persistent_volume
physical_server
placement_group
provider_foreman
resource_pool
retired
security_group
security_policy
security_policy_rule
service
services
storage
templates
volume_mapping
vm].include?(@layout)
end
def default_search?(search_name)
@default_search.present? && @default_search.name == search_name
end
def no_default_search?(search_id)
@default_search.blank? && search_id.to_i.zero?
end
def expression_selected_id_or_name(id_or_name, search)
@edit[:expression][:selected] && @edit[:expression][:selected][id_or_name] == search && !@edit[:custom_search]
end
def expression_selected_nil_or_id(search_id)
@edit[:expression][:selected].nil? && @edit[:selected].nil? || expression_selected_id_or_name(:id, search_id.to_i)
end
# Returns description of a filter, for default filter also with "(Default)"
def search_description(search)
if default_search?(search.name) ||
@edit && no_default_search?(search.id) &&
settings_default('0', :default_search, @edit&.dig(@expkey, :exp_model).to_s.to_sym).to_s == '0'
_("%{description} (Default)") % {:description => search.description}
else
_("%{description}") % {:description => search.description}
end
end
# Returns class for a filter from Global filters to highlight it
def def_searches_active_filter?(search)
if @edit && @edit[:expression] &&
((default_search?(search.name) || no_default_search?(search.id)) && expression_selected_nil_or_id(search.id) ||
(@edit[:expression][:selected] && @edit[:expression][:selected][:id].zero? && search.id.to_i.zero? ||
expression_selected_id_or_name(:name, search.name)))
'active'
else
''
end
end
# Returns class for a filter from My filters to highlight it
def my_searches_active_filter?(search)
if @edit && @edit[:expression] &&
(default_search?(search.name) && expression_selected_nil_or_id(search.id) ||
(@edit[:expression][:selected].nil? && search.id.to_i.zero? ||
expression_selected_id_or_name(:name, search.name)))
'active'
else
''
end
end
# Do we show or hide the clear_search link in the list view title
def clear_search_status
!!(@edit&.fetch_path(:adv_search_applied, :text))
end
# Should we allow the user input checkbox be shown for an atom in the expression editor
QS_VALID_USER_INPUT_OPERATORS = ["=", "!=", ">", ">=", "<", "<=", "INCLUDES", "STARTS WITH", "ENDS WITH", "CONTAINS"].freeze
QS_VALID_FIELD_TYPES = %i[string boolean integer float percent bytes megabytes].freeze
def qs_show_user_input_checkbox?
return true if @edit[:expression_method]
return false unless @edit[:adv_search_open] # Only allow user input for advanced searches
return false unless QS_VALID_USER_INPUT_OPERATORS.include?(@edit[@expkey][:exp_key])
val = (@edit[@expkey][:exp_typ] == "field" && # Field atoms with certain field types return true
QS_VALID_FIELD_TYPES.include?(@edit[@expkey][:val1][:type])) ||
(@edit[@expkey][:exp_typ] == "tag" && # Tag atoms with a tag category chosen return true
@edit[@expkey][:exp_tag]) ||
(@edit[@expkey][:exp_typ] == "count" && # Count atoms with a count col chosen return true
@edit[@expkey][:exp_count])
val
end
# Should we allow the field alias checkbox to be shown for an atom in the expression editor
def adv_search_show_alias_checkbox?
@edit[:adv_search_open] # Only allow field aliases for advanced searches
end
def pressed2model_action(pressed)
pressed =~ /^(ems_cluster|miq_template|infra_networking|ems_automation)_(.*)$/ ? [$1, $2] : pressed.split('_', 2)
end
def model_for_vm(record)
raise _("Record is not VmOrTemplate class") unless record.kind_of?(VmOrTemplate)
if record.kind_of?(ManageIQ::Providers::CloudManager::Vm)
ManageIQ::Providers::CloudManager::Vm
elsif record.kind_of?(ManageIQ::Providers::InfraManager::Vm)
ManageIQ::Providers::InfraManager::Vm
elsif record.kind_of?(ManageIQ::Providers::CloudManager::Template)
ManageIQ::Providers::CloudManager::Template
elsif record.kind_of?(ManageIQ::Providers::InfraManager::Template)
ManageIQ::Providers::InfraManager::Template
end
end
def controller_for_vm(model)
case model.to_s
when "ManageIQ::Providers::CloudManager::Template", "ManageIQ::Providers::CloudManager::Vm"
"vm_cloud"
when "ManageIQ::Providers::InfraManager::Template", "ManageIQ::Providers::InfraManager::Vm"
"vm_infra"
else
"vm_or_template"
end
end
def model_from_active_tree(tree)
case tree
when :instances_filter_tree
"ManageIQ::Providers::CloudManager::Vm"
when :images_filter_tree
"ManageIQ::Providers::CloudManager::Template"
when :vms_filter_tree
"ManageIQ::Providers::InfraManager::Vm"
when :templates_filter_tree
"ManageIQ::Providers::InfraManager::Template"
when :templates_images_filter_tree
"MiqTemplate"
when :vms_instances_filter_tree
"Vm"
end
end
def object_types_for_flash_message(klass, record_ids)
if klass == VmOrTemplate
object_ary = klass.where(:id => record_ids).collect { |rec| ui_lookup(:model => model_for_vm(rec).to_s) }
obj_hash = object_ary.each.with_object(Hash.new(0)) { |obj, h| h[obj] += 1 }
obj_hash.collect { |k, v| v == 1 ? k : k.pluralize }.sort.to_sentence
else
object = ui_lookup(:model => klass.to_s)
record_ids.length == 1 ? object : object.pluralize
end
end
def pagination_or_gtl_request?
%i[ppsetting searchtag entry sortby sort_choice page].find { |k| params[k] }
end
def update_gtl_div(action_url = 'explorer', button_div = 'center_tb')
render :update do |page|
page << javascript_prologue
page.replace("gtl_div",
:partial => "layouts/x_gtl",
:locals => {:action_url => action_url, :button_div => button_div})
page << "miqSparkle(false)"
end
end
def perfmenu_click?
return false unless params[:menu_click]
perf_menu_click
true
end
def javascript_process_redirect_args(args)
# there's no model for ResourceController - defaulting to traditional routing
begin
model = self.class.model
rescue StandardError => _err
model = nil
end
if model && args.class == Hash && args[:action] == 'show' && restful_routed?(model)
args.delete(:action)
polymorphic_path_redirect(model, args)
else
args
end
end
def javascript_redirect(args)
render :update do |page|
page << javascript_prologue
page.redirect_to(args)
end
end
def polymorphic_path_redirect(model, args)
record = args[:record] ? args[:record] : model.find(args[:id] || params[:id])
args.delete(:record)
args.delete(:id)
polymorphic_path(record, args)
end
def javascript_open_window(url)
ex = ExplorerPresenter.open_window(url)
ex.spinner_off
render :json => ex.for_render
end
# this keeps the main_div wrapping tag, replaces only the inside