drg_cms 0.5.52.12 → 0.5.52.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/app/assets/javascripts/drg_cms/drg_cms.js +17 -2
  3. data/app/assets/stylesheets/drg_cms/drg_cms.css +16 -3
  4. data/app/assets/stylesheets/drg_cms/select-multiple.css +1 -1
  5. data/app/controllers/cmsedit_controller.rb +56 -16
  6. data/app/controllers/dc_application_controller.rb +83 -1
  7. data/app/controllers/dc_common_controller.rb +2 -52
  8. data/app/forms/all_options.yml +27 -4
  9. data/app/forms/cms_menu.yml +5 -0
  10. data/app/forms/dc_gallery.yml +53 -0
  11. data/app/forms/dc_link.yml +16 -10
  12. data/app/forms/dc_menu_item.yml +5 -0
  13. data/app/forms/dc_page.yml +1 -2
  14. data/app/forms/dc_removed_url.yml +42 -0
  15. data/app/helpers/cmsedit_helper.rb +63 -22
  16. data/app/helpers/dc_application_helper.rb +35 -11
  17. data/app/helpers/dc_gallery_renderer.rb +94 -0
  18. data/app/helpers/dc_page_renderer.rb +20 -3
  19. data/app/helpers/dc_poll_renderer.rb +6 -7
  20. data/app/models/concerns/dc_page_concern.rb +1 -1
  21. data/app/models/dc_filter.rb +15 -7
  22. data/app/models/dc_gallery.rb +64 -0
  23. data/app/models/dc_link.rb +1 -0
  24. data/app/models/dc_memory.rb +19 -4
  25. data/app/models/dc_page.rb +1 -1
  26. data/app/models/dc_removed_url.rb +54 -0
  27. data/app/models/drgcms_form_fields.rb +5 -1649
  28. data/app/models/drgcms_form_fields/check_box.rb +69 -0
  29. data/app/models/drgcms_form_fields/comment.rb +49 -0
  30. data/app/models/drgcms_form_fields/date_picker.rb +102 -0
  31. data/app/models/drgcms_form_fields/date_select.rb +68 -0
  32. data/app/models/drgcms_form_fields/date_time_picker.rb +87 -0
  33. data/app/models/drgcms_form_fields/datetime_select.rb +73 -0
  34. data/app/models/drgcms_form_fields/drgcms_field.rb +241 -0
  35. data/app/models/drgcms_form_fields/drgcms_form_fields.rb +25 -0
  36. data/app/models/drgcms_form_fields/embedded.rb +84 -0
  37. data/app/models/drgcms_form_fields/file_select.rb +70 -0
  38. data/app/models/drgcms_form_fields/hidden_field.rb +52 -0
  39. data/app/models/drgcms_form_fields/html_field.rb +70 -0
  40. data/app/models/drgcms_form_fields/journal_diff.rb +60 -0
  41. data/app/models/drgcms_form_fields/link_to.rb +69 -0
  42. data/app/models/drgcms_form_fields/multitext_autocomplete.rb +195 -0
  43. data/app/models/drgcms_form_fields/number_field.rb +83 -0
  44. data/app/models/drgcms_form_fields/password_field.rb +62 -0
  45. data/app/models/drgcms_form_fields/readonly.rb +79 -0
  46. data/app/models/drgcms_form_fields/select.rb +164 -0
  47. data/app/models/drgcms_form_fields/submit_tag.rb +58 -0
  48. data/app/models/drgcms_form_fields/text_area.rb +68 -0
  49. data/app/models/drgcms_form_fields/text_autocomplete.rb +143 -0
  50. data/app/models/drgcms_form_fields/text_field.rb +56 -0
  51. data/app/models/drgcms_form_fields/text_with_select.rb +92 -0
  52. data/app/models/drgcms_form_fields/tree_select.rb +150 -0
  53. data/config/locales/drgcms_en.yml +1 -0
  54. data/config/locales/drgcms_sl.yml +2 -1
  55. data/config/locales/models_en.yml +42 -6
  56. data/config/locales/models_sl.yml +38 -3
  57. data/lib/drg_cms.rb +1 -1
  58. data/lib/drg_cms/version.rb +1 -1
  59. data/lib/tasks/dc_cleanup.rake +1 -1
  60. metadata +33 -4
@@ -0,0 +1,62 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Implementation of password DRG CMS form field.
27
+ #
28
+ # ===Form options:
29
+ # * +type:+ password_field (required)
30
+ # * +name:+ Field name (required)
31
+ # * +html:+ html options which apply to password field (optional)
32
+ #
33
+ # Form example:
34
+ # 20:
35
+ # name: password
36
+ # type: pasword_field
37
+ # html:
38
+ # size: 20
39
+ #
40
+ # 30:
41
+ # name: password_confirmation
42
+ # type: pasword_field
43
+ # html:
44
+ # size: 20
45
+ #
46
+ ###########################################################################
47
+ class PasswordField < DrgcmsField
48
+
49
+ ###########################################################################
50
+ # Render password field html code
51
+ #
52
+ ###########################################################################
53
+ def render
54
+ return self if @readonly
55
+ @yaml['html'] ||= {}
56
+ record = record_text_for(@yaml['name'])
57
+ @html << @parent.password_field(record, @yaml['name'], @yaml['html'])
58
+ self
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,79 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Implementation of readonly DRG CMS form field.
27
+ #
28
+ # Readonly field value is just painted on form.
29
+ #
30
+ # ===Form options:
31
+ # * +name:+ field name
32
+ # * +type:+ readonly
33
+ # * +eval:+ value will be provided by evaluating expression. Usually dc_name4_id helper
34
+ # can be used to get value. Example: dc_name4_id,model_name_in_lower_case,field_name
35
+ #
36
+ # * +readonly:+ yes (can be applied to any field type)
37
+ #
38
+ # Form example:
39
+ # 10:
40
+ # name: user
41
+ # type: readonly
42
+ # html:
43
+ # size: 50
44
+ # 20:
45
+ # name: created_by
46
+ # type: readonly
47
+ # eval: dc_name4_id,dc_user,name
48
+ ###########################################################################
49
+ class Readonly < DrgcmsField
50
+
51
+ ###########################################################################
52
+ # Render readonly field html code
53
+ ###########################################################################
54
+ def render
55
+ @html << @parent.hidden_field('record', @yaml['name']) # retain field as hidden field
56
+ @html << '<div class="dc-readonly">'
57
+
58
+ @html << if @yaml['eval']
59
+ if @yaml['eval'].match('dc_name4_id')
60
+ a = @yaml['eval'].split(',')
61
+ if a.size == 3
62
+ @parent.dc_name4_id(a[1], a[2], nil, @record[ @yaml['name'] ])
63
+ else
64
+ @parent.dc_name4_id(a[1], a[2], a[3], @record[ @yaml['name'] ])
65
+ end
66
+
67
+ # @parent.dc_name4_id(a[1], a[2], @record[ @yaml['name'] ])
68
+ else
69
+ eval( "#{@yaml['eval']} '#{@record[ @yaml['name'] ]}'")
70
+ end
71
+ else
72
+ @parent.dc_format_value(@record[@yaml['name']],@yaml['format'])
73
+ end
74
+ @html << '</div>'
75
+ self
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,164 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Implementation of select DRG CMS form field.
27
+ #
28
+ # ===Form options:
29
+ # * +name:+ field name (required)
30
+ # * +type:+ select (required)
31
+ # * +choices:+ Values for choices separated by comma. Values can also be specified like description:value.
32
+ # In the example description will be shown to user, but value will be saved to document.
33
+ # choices: 'OK:0,Ready:1,Error:2'
34
+ # choices: Ruby,Pyton,PHP
35
+ # * +eval:+ Choices will be provided by evaluating expression
36
+ # * eval: dc_choices4('model_name','description_field_name','_id'); dc_choices4 helper will provide data for select field.
37
+ # * eval: ModelName.choices4_field; ModelName class will define method choices4_field which
38
+ # will provide data for select field.
39
+ # * collection_name.search_field_name.method_name; When searching is more complex custom search
40
+ # method may be defined in CollectionName model which will provide result set for search.
41
+ # * If choices or eval is not defined choices will be provided from translation helpers. For example:
42
+ # Collection has field status choices for field may be provided by en.helpers.model_name.choices4_status
43
+ # entry of english translation. English is of course default translation. If you provide translations in
44
+ # your local language then select choices will be localized.
45
+ # en.helpers.model_name.choices4_status: 'OK:0,Ready:1,Error:2'
46
+ # sl.helpers.model_name.choices4_status: 'V redu:0,Pripravljen:1,Napaka:2'
47
+ # * +html:+ html options which apply to select field (optional)
48
+ #
49
+ # Form example:
50
+ # 30:
51
+ # name: type
52
+ # type: select
53
+ # 40:
54
+ # name: parent
55
+ # type: select
56
+ # eval: DcCategory.values_for_parent
57
+ # html:
58
+ # include_blank: true
59
+ ###########################################################################
60
+ class Select < DrgcmsField
61
+
62
+ ###########################################################################
63
+ # Return values, when choices options will be returned by evaluating expression
64
+ ###########################################################################
65
+ def do_eval(e)
66
+ e.strip!
67
+ method = e.split(/\ |\(/).first
68
+ return eval(e) if respond_to?(method) # id method defined here
69
+ return eval('@parent.'+e) if @parent.respond_to?(method) # is method defined in helpers
70
+ # eval whatever it is
71
+ eval e
72
+ end
73
+
74
+ ###########################################################################
75
+ # Create choices array for select field.
76
+ ###########################################################################
77
+ def get_choices
78
+ begin
79
+ choices = case
80
+ when @yaml['choices'] then
81
+ (@yaml['choices'].match('helpers.') ) ? t(@yaml['choices']) : @yaml['choices']
82
+ when @yaml['eval'] then
83
+ do_eval(@yaml['eval'])
84
+ else
85
+ c = t('helpers.label.' + @form['table'] + '.choices4_' + @yaml['name'] )
86
+ c = 'Error' if c.match( 'translation missing' )
87
+ c
88
+ end
89
+ # Convert string to Array
90
+ choices.class == String ?
91
+ choices.chomp.split(',').inject([]) {|r,v| r << (v.match(':') ? v.split(':') : v )} :
92
+ choices
93
+ rescue Exception => e
94
+ Rails.logger.debug "Error in select eval. #{e.message}\n"
95
+ ['error'] # return empty array when error occures
96
+ end
97
+ end
98
+
99
+ ###########################################################################
100
+ # Return value when readonly is required
101
+ ###########################################################################
102
+ def ro_standard
103
+ value = @record.respond_to?(@yaml['name']) ? @record[@yaml['name']] : nil
104
+ return self if value.nil?
105
+ #
106
+ choices = get_choices()
107
+ if value.class == Array # multiple choices
108
+ html = ''
109
+ value.each do |element|
110
+ choices.each do |choice|
111
+ if choice.to_s == element.to_s
112
+ html << '<br>' if html.size > 0
113
+ html << "#{element.to_s}"
114
+ end
115
+ end
116
+ end
117
+ return super(html)
118
+ else
119
+ choices.each do |choice|
120
+ if choice.class == Array
121
+ return super(choice.first) if choice.last.to_s == value.to_s
122
+ else
123
+ return super(choice) if choice.to_s == value.to_s
124
+ end
125
+ end
126
+ end
127
+ super('')
128
+ end
129
+
130
+ ###########################################################################
131
+ # Render select field html code
132
+ ###########################################################################
133
+ def render
134
+ return ro_standard if @readonly
135
+ set_initial_value('html','selected')
136
+ #
137
+ @yaml['html'].symbolize_keys!
138
+ record = record_text_for(@yaml['name'])
139
+ if @yaml['multiple']
140
+ @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'], {multiple: true})
141
+ @js << "$('##{record}_#{@yaml['name']}').selectMultiple();"
142
+ else
143
+ @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'])
144
+ end
145
+ self
146
+ end
147
+
148
+ ###########################################################################
149
+ # Return value.
150
+ ###########################################################################
151
+ def self.get_data(params, name)
152
+ if params['record'][name].class == Array
153
+ params['record'][name].delete_if {|e| e.blank? }
154
+ return nil if params['record'][name].size == 0
155
+ # convert to BSON objects
156
+ is_id = BSON::ObjectId.legal?(params['record'][name].first)
157
+ return params['record'][name].map{ |e| BSON::ObjectId.from_string(e) } if is_id
158
+ return params['record'][name]
159
+ end
160
+ params['record'][name]
161
+ end
162
+
163
+ end
164
+ end
@@ -0,0 +1,58 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Create submit_tag form field. submit_tag form field is mostly used by polls but can
27
+ # be also incorporated in the middle of form.
28
+ #
29
+ # ===Form options:
30
+ # * +type:+ submit_tag (required)
31
+ # * +caption:+ Submit field caption
32
+ # * +icon:+ Icon
33
+ # * +html:+ html options which apply to link_to (optional)
34
+ #
35
+ # Form example:
36
+ # 40:
37
+ # type: submit_tag
38
+ # caption: translate.this
39
+ # icon: check
40
+ ###########################################################################
41
+ class SubmitTag < DrgcmsField
42
+
43
+ ###########################################################################
44
+ # Render submit_tag field html code
45
+ ###########################################################################
46
+ def render
47
+ @yaml['html'] ||= {}
48
+ @yaml['html']['class'] ||= 'dc-submit'
49
+ @yaml['html'].symbolize_keys!
50
+ text = @yaml['caption'] || @yaml['text']
51
+ text = t(@yaml['text']) if text.match(/\./)
52
+
53
+ @html << @parent.submit_tag(text, @yaml['html'])
54
+ self
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,68 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Implementation of text_area DRG CMS form field.
27
+ #
28
+ # ===Form options:
29
+ # * +type:+ text_area (required)
30
+ # * +name:+ Field name (required)
31
+ # * +html:+ html options which apply to text_area field (optional)
32
+ #
33
+ # Form example:
34
+ # 10:
35
+ # name: css
36
+ # type: text_area
37
+ # html:
38
+ # size: 100x30
39
+ ###########################################################################
40
+ class TextArea < DrgcmsField
41
+
42
+ ###########################################################################
43
+ # Return value for readonly field
44
+ ###########################################################################
45
+ def ro_standard
46
+ value = @record[@yaml['name']]
47
+ @html << "<div class='dc-readonly'>#{value.gsub("\n",'<br>')}</div>" unless value.blank?
48
+ self
49
+ end
50
+
51
+ ###########################################################################
52
+ # Render text_area field html code
53
+ ###########################################################################
54
+ def render
55
+ return ro_standard if @readonly
56
+ set_initial_value
57
+ #
58
+ # @yaml['html'] ||= {}
59
+ # value_send_as = 'p_' + @yaml['name']
60
+ # @yaml['html']['value'] = @parent.params[value_send_as] if @parent.params[value_send_as]
61
+
62
+ record = record_text_for(@yaml['name'])
63
+ @html << @parent.text_area(record, @yaml['name'], @yaml['html'])
64
+ self
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,143 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Implementation of text_autocomplete DRG CMS form field.
27
+ #
28
+ # ===Form options:
29
+ # * +name:+ field name (required)
30
+ # * +type:+ text_autocomplete (required)
31
+ # * +table+ Collection (table) name. When defined search must contain field name
32
+ # * +search:+ Search may consist of three parameters from which are separated either by dot (.)
33
+ # * search_field_name; when table option is defined search must define field name which will be used for search query
34
+ # * collection_name.search_field_name; Same as above except that table options must be ommited.
35
+ # * collection_name.search_field_name.method_name; When searching is more complex custom search
36
+ # method may be defined in CollectionName model which will provide result set for search.
37
+ #
38
+ # Form example:
39
+ # 10:
40
+ # name: user_id
41
+ # type: text_autocomplete
42
+ # search: dc_user.name
43
+ # html:
44
+ # size: 30
45
+ ###########################################################################
46
+ class TextAutocomplete < DrgcmsField
47
+
48
+ ###########################################################################
49
+ # Render text_autocomplete field html code
50
+ ###########################################################################
51
+ def render
52
+ # Return descriptive text and put it into input field
53
+ # search field name
54
+ if @yaml['search'].class == Hash
55
+ table = @yaml['search']['table']
56
+ ret_name = @yaml['search']['field']
57
+ method = @yaml['search']['method']
58
+ elsif @yaml['search'].match(/\./)
59
+ table, ret_name, method = @yaml['search'].split('.')
60
+ else
61
+ ret_name = @yaml['search']
62
+ end
63
+ # determine table name
64
+ if @yaml['table']
65
+ table = if @yaml['table'].class == String
66
+ @yaml['table']
67
+ # eval(how_to_get_my_table_name)
68
+ elsif @yaml['table']['eval']
69
+ eval @yaml['table']['eval']
70
+ else
71
+ Rails.logger.error "Field #{@yaml['name']}: Invalid table parameter!"
72
+ nil
73
+ end
74
+ end
75
+ return 'Table or field keyword not defined!' unless (table and ret_name)
76
+ # TODO check if table exists
77
+ t = table.classify.constantize
78
+ # find record and return value of field
79
+ value_send_as = 'p_' + @yaml['name']
80
+ value = if @parent.params[value_send_as]
81
+ @parent.params[value_send_as]
82
+ elsif @record and @record[@yaml['name']]
83
+ @record[@yaml['name']]
84
+ end
85
+ # Found value to be written in field. If field is not found write out value.
86
+ if value
87
+ record = t.find(value)
88
+ value_displayed = record ? record.send(ret_name) : value
89
+ end
90
+ # return if readonly
91
+ return ro_standard(value_displayed) if @readonly
92
+ # Add method back, so autocomplete will know that it must search for method inside class
93
+ ret_name = "#{ret_name}.#{method}" if method
94
+ @yaml['html'] ||= {}
95
+ @yaml['html']['value'] = value_displayed
96
+ @yaml['html']['placeholder'] ||= t('drgcms.search_placeholder') || nil
97
+ #
98
+ _name = '_' + @yaml['name']
99
+ record = record_text_for(@yaml['name'])
100
+ @html << @parent.text_field(record, _name, @yaml['html'])
101
+ if @yaml['with_new']
102
+ @html << ' ' +
103
+ @parent.fa_icon('plus-square lg', class: 'in-edit-add', title: t('drgcms.new'),
104
+ style: "vertical-align: top;", 'data-table' => @yaml['with_new'] )
105
+ end
106
+ @html << @parent.hidden_field(record, @yaml['name'], value: value) # actual value will be in hidden field
107
+ # JS stuff
108
+ @js << <<EOJS
109
+ $(document).ready(function() {
110
+ $("##{record}_#{_name}").autocomplete( {
111
+ source: function(request, response) {
112
+ $.ajax({
113
+ url: '/dc_common/autocomplete',
114
+ type: "POST",
115
+ dataType: "json",
116
+ data: { input: request.term, table: "#{table}", search: "#{ret_name}" #{(',id: "'+@yaml['id'] + '"') if @yaml['id']} },
117
+ success: function(data) {
118
+ response( $.map( data, function(key) {
119
+ return key;
120
+ }));
121
+ }
122
+ });
123
+ },
124
+ change: function (event, ui) {
125
+ $("##{record}_#{@yaml['name']}").val(ui.item.id);
126
+ },
127
+ minLength: 2
128
+ });
129
+ });
130
+ EOJS
131
+
132
+ self
133
+ end
134
+
135
+ ###########################################################################
136
+ # Return value. Return nil if input field is empty
137
+ ###########################################################################
138
+ def self.get_data(params, name)
139
+ params['record']["_#{name}"].blank? ? nil : params['record'][name]
140
+ end
141
+
142
+ end
143
+ end