drg_cms 0.5.8 → 0.5.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/drg_cms/drg_cms.js +53 -0
  3. data/app/assets/javascripts/drg_cms/jquery.the-modal.js +260 -0
  4. data/app/assets/javascripts/drg_cms_cms.js +3 -0
  5. data/app/assets/stylesheets/drg_cms/drg_cms.css +68 -33
  6. data/app/assets/stylesheets/drg_cms/the-modal.css +33 -0
  7. data/app/assets/stylesheets/drg_cms_cms.css +1 -0
  8. data/app/controllers/cmsedit_controller.rb +69 -54
  9. data/app/controllers/dc_application_controller.rb +21 -17
  10. data/app/controllers/dc_common_controller.rb +0 -0
  11. data/app/forms/all_options.yml +3 -2
  12. data/app/forms/cms_menu.yml +30 -17
  13. data/app/forms/dc_big_table_value.yml +3 -0
  14. data/app/forms/dc_filter.yml +56 -0
  15. data/app/forms/dc_key_value_store.yml +33 -0
  16. data/app/helpers/cmsedit_helper.rb +32 -82
  17. data/app/models/dc_ad.rb +27 -1
  18. data/app/models/dc_ad_stat.rb +10 -1
  19. data/app/models/dc_big_menu.rb +18 -1
  20. data/app/models/dc_big_table.rb +15 -2
  21. data/app/models/dc_big_table_locale.rb +13 -2
  22. data/app/models/dc_big_table_value.rb +14 -2
  23. data/app/models/dc_category.rb +16 -2
  24. data/app/models/dc_design.rb +17 -1
  25. data/app/models/dc_dummy.rb +5 -1
  26. data/app/models/dc_filter.rb +172 -0
  27. data/app/models/dc_folder_permission.rb +11 -1
  28. data/app/models/dc_ident.rb +9 -1
  29. data/app/models/dc_internals.rb +58 -0
  30. data/app/models/dc_journal.rb +14 -1
  31. data/app/models/dc_key_value_store.rb +8 -2
  32. data/app/models/dc_link.rb +14 -1
  33. data/app/models/dc_menu.rb +16 -1
  34. data/app/models/dc_menu_item.rb +19 -1
  35. data/app/models/dc_page.rb +43 -1
  36. data/app/models/dc_part.rb +28 -1
  37. data/app/models/dc_permission.rb +11 -1
  38. data/app/models/dc_piece.rb +25 -1
  39. data/app/models/dc_policy.rb +14 -1
  40. data/app/models/dc_policy_role.rb +10 -1
  41. data/app/models/dc_policy_rule.rb +9 -1
  42. data/app/models/dc_poll.rb +23 -1
  43. data/app/models/dc_poll_item.rb +16 -1
  44. data/app/models/dc_simple_menu.rb +16 -1
  45. data/app/models/dc_simple_menu_item.rb +17 -1
  46. data/app/models/dc_site.rb +43 -13
  47. data/app/models/dc_stat.rb +14 -3
  48. data/app/models/dc_user.rb +38 -1
  49. data/app/models/dc_user_role.rb +13 -1
  50. data/app/models/dc_visit.rb +11 -1
  51. data/app/models/drgcms_form_fields.rb +20 -4
  52. data/config/locales/drgcms_en.yml +11 -6
  53. data/config/locales/drgcms_sl.yml +8 -4
  54. data/config/locales/models_en.yml +20 -1
  55. data/config/locales/models_sl.yml +18 -0
  56. data/lib/drg_cms/version.rb +1 -1
  57. data/lib/generators/new_drg_form/new_drg_form_generator.rb +1 -1
  58. data/lib/tasks/browse_models.rake +17 -2
  59. metadata +8 -2
@@ -0,0 +1,33 @@
1
+ ## YAML Template for dc_key_value_store
2
+ ---
3
+ table: dc_key_value_store
4
+
5
+ index:
6
+ actions: standard
7
+
8
+ result_set:
9
+ actions:
10
+ 1: edit
11
+ 2: delete
12
+
13
+ columns:
14
+ 1:
15
+ name: key
16
+ 2:
17
+ name: value
18
+
19
+ form:
20
+ title:
21
+ field: key
22
+
23
+ fields:
24
+ 10:
25
+ name: key
26
+ type: text_field
27
+ size: 30
28
+
29
+ 20:
30
+ name: value
31
+ type: text_field
32
+ html:
33
+ size: 50
@@ -52,7 +52,7 @@ def dc_actions_for_index()
52
52
  <ul class="dc-action-menu">
53
53
  EOT
54
54
  #
55
- actions.each do |k,v|
55
+ actions.each do |k,v|
56
56
  session[:form_processing] = "index:actions: #{k}=#{v}"
57
57
  next if v.nil? # must be
58
58
  url = @parms.clone
@@ -71,12 +71,14 @@ EOT
71
71
  url['table'] = yaml['table'] if yaml['table']
72
72
  url['formname'] = yaml['formname'] if yaml['formname']
73
73
  end
74
- # html options of the link
74
+ # html link options
75
75
  yhtml = yaml['html'] || {}
76
76
  yhtml['title'] = yaml['title'] if yaml['title']
77
77
  html << '<li class="dc-animate">'
78
+ #
78
79
  html << case
79
- when action == 'sort' then # sort
80
+ # sort
81
+ when action == 'sort' then
80
82
  choices = [['id','id']]
81
83
  if @form['index']['sort']
82
84
  @form['index']['sort'].split(',').each do |s|
@@ -87,20 +89,21 @@ EOT
87
89
  fa_icon('sort-alpha-asc') + ' ' + t('drgcms.sort') + ' ' +
88
90
  select('sort', 'sort', choices, { include_blank: true },
89
91
  { class: 'drgcms_sort', 'data-table' => @form['table']} )
90
-
91
- when action == 'filter' then # filter
92
+ # filter
93
+ when action == 'filter' then
92
94
  caption = t('drgcms.filter')
95
+ caption << '&nbsp;' + fa_icon('caret-down lg') + DcFilter.menu_filter(self)
96
+ # add check image, so user will know that filter is active
93
97
  s = session[@form['table']]
94
- # add checked image to filter, so user will know that data is filtered
95
- # caption << '&nbsp;'+image_tag('drg_cms/checkbox-checked.png') if s and s[:filter]
96
- yhtml['onclick'] = "$('#drgcms_filter').toggle(300);"
97
- # link_to(caption.html_safe, '#', yhtml )
98
- dc_link_to('drgcms.filter','filter', '#', yhtml )
99
- when action == 'new' then # new
98
+ caption << ('&nbsp;' << fa_icon('check')) if s and s[:filter]
99
+ caption
100
+ # new
101
+ when action == 'new' then
100
102
  caption = yaml['caption'] || 'drgcms.new'
101
103
  dc_link_to(caption,'plus', url, yhtml )
102
- when action == 'menu' then # menu
103
- caption = t(v['caption'], v['caption'])
104
+ # menu
105
+ when action == 'menu' then
106
+ caption = t(v['caption'], v['caption']) + '&nbsp;' + fa_icon('caret-down lg')
104
107
  caption + eval(v['eval'])
105
108
  else
106
109
  caption = yaml['caption'] || yaml['text']
@@ -109,101 +112,45 @@ EOT
109
112
  end
110
113
  html << '</li>'
111
114
  end
112
- html << '</ul></div>'
115
+ html << '</ul>'
116
+ html << DcFilter.get_filter_field(self)
117
+ html << '</div>'
113
118
  html.html_safe
114
119
  end
115
120
 
116
- ############################################################################
117
- # Will return field form definition if field is defined on form. Subroutine of dc_div_filter
118
- ############################################################################
119
- def _get_field_def(name) #:nodoc:
120
- @form['form']['tabs'].each do |tab|
121
- tab.each do |field|
122
- next if field.class == String # tab name
123
- field.each {|k,v| return v if v['name'] == name }
124
- end
125
- end if @form['form']['tabs'] # I know. But nice.
126
- #
127
- @form['form']['fields'].each do |field|
128
- return field.last if field.last['name'] == name
129
- end if @form['form']['fields']
130
- nil
131
- end
132
-
133
- ############################################################################
134
- # Finds field definition on form and use it for filter input. Subroutine of dc_div_filter.
135
- ############################################################################
136
- def _get_field_div(name) #:nodoc:
137
- filter = nil
138
- # old filter saved to session
139
- if session[@form['table']] and session[@form['table']][:filter]
140
- filter, operation, value = session[@form['table']][:filter].split("\t")
141
- end
142
- # field not defined on form. Must be defined: name as form_field_type
143
- if name.match(' as ')
144
- name, dummy, type = name.split(' ')
145
- field = {"name" => name, "type" => type, "html"=>{"size"=>20}}
146
- else
147
- field = _get_field_def(name)
148
- field = {"name" => name, "type" => 'text_field', "html"=>{"size"=>20}} if field.nil?
149
- end
150
- #
151
- div_hidden = 'div-hidden'
152
- if filter
153
- div_hidden = '' if name == filter
154
- if field['html']
155
- field['html']['value'] = value
156
- else
157
- field['html'] = { "value" => value }
158
- end
159
- end
160
- klas_string = field['type'].camelize
161
- klas = DrgcmsFormFields::const_get(klas_string) || nil
162
- return 'error' if klas.nil?
163
- #
164
- o = klas.new(self, @record, field).render
165
- "<span id=\"filter_#{field['name']}\" class=\"#{div_hidden}\">" << o.html << (o.js.size > 2 ? javascript_tag(o.js) : '') << '</span>'
166
- end
167
-
168
121
  ############################################################################
169
122
  # Creates filter div for cmsedit index/filter action.
170
123
  ############################################################################
171
124
  def dc_div_filter()
172
- choices, inputs = [], ''
125
+ choices = []
173
126
  filter = (@form['index'] and @form['index']['filter']) ? @form['index']['filter'] + ',' : ''
174
127
  filter << 'id as text_field' # filter id is added by default
175
128
  filter.split(',').each do |f|
176
129
  f.strip!
177
130
  name = f.match(' as ') ? f.split(' ').first : f
178
- choices << [ t("helpers.label.#{@form['table']}.#{name}", name), name ]
179
- # find field definition on form and use it for input field definition
180
- inputs << _get_field_div(f)
131
+ choices << [ t("helpers.label.#{@form['table']}.#{name}", name), f ]
181
132
  end
182
133
  choices4_operators = t('drgcms.choices4_filter_operators').chomp.split(',').inject([]) {|r,v| r << (v.match(':') ? v.split(':') : v )}
183
- s = session[@form['table']]
184
- is_hidden = (s and s[:filter]) ? '' : ' class="div-hidden" '
185
- #
134
+ # currently selected options
186
135
  if session[@form['table']] and session[@form['table']][:filter]
187
136
  field_name, operators_value, dummy = session[@form['table']][:filter].split("\t")
188
137
  else
189
138
  field_name, operators_value = nil, nil
190
139
  end
140
+ #{ form_tag :table => @form['table'], filter: :on, filter_input: 1, action: :index, method: :post }
141
+ url = url_for(:table => @form['table'], filter: :on, filter_input: 1, action: :index, controller: :cmsedit)
191
142
  html =<<EOT
192
- <div id="drgcms_filter" #{is_hidden}>
143
+ <div id="drgcms_filter" class="div-hidden">
193
144
  <table class="dc-menu"><td>
194
- #{ form_tag :table => @form['table'], filter: :on, action: :index, method: :post }
195
145
  #{ select(nil, 'filter_field', options_for_select(choices, field_name), { include_blank: true }) }
196
146
  #{ select(nil, 'filter_oper', options_for_select(choices4_operators, operators_value)) }
197
- #{ inputs }
147
+
198
148
  </td>
199
- <td class="dc-link-submit dc-animate">#{fa_icon('check-square-o')} #{submit_tag(t('drgcms.filter_on'), :class => 'dc-submit')}</td>
149
+ <td class="dc-link dc-animate drgcms_popup_submit" data-url="#{url}">#{fa_icon('check-square-o')} #{t('drgcms.filter_on')}</td>
200
150
  <td class="dc-link dc-animate">#{dc_link_to('drgcms.filter_off','close', {action: 'index', filter: 'off', :table => @form['table']}) }</td>
201
151
  </table>
202
- </form>
203
152
  </div>
204
-
205
153
  EOT
206
- # <td class="dc-link dc-animate">#{link_to t('drgcms.filter_off'), action: 'index', filter: 'off', :table => @form['table']}</td>
207
154
  html.html_safe
208
155
  end
209
156
 
@@ -379,8 +326,11 @@ end
379
326
  ############################################################################
380
327
  # Defines style or class for row (tr) or column (td)
381
328
  ############################################################################
382
- def dc_style_or_class(selector, yaml, value, document)
329
+ def dc_style_or_class(selector, yaml, value, record)
383
330
  return '' if yaml.nil?
331
+ # alias record and value so both names can be used
332
+ field = value
333
+ document = record
384
334
  html = selector ? "#{selector}=\"" : ''
385
335
  html << if yaml.class == String
386
336
  yaml
@@ -451,7 +401,7 @@ def dc_columns_for_result(document)
451
401
  #
452
402
  td = '<td '
453
403
  td << dc_style_or_class('class',v['td_class'],value,document)
454
- td << dc_style_or_class('style',v['td_style'],value,document)
404
+ td << dc_style_or_class('style',v['td_style'] || v['style'],value,document)
455
405
  html << "#{td}>#{value}</td>"
456
406
  end
457
407
  end
data/app/models/dc_ad.rb CHANGED
@@ -22,7 +22,33 @@
22
22
  #++
23
23
 
24
24
  ##########################################################################
25
- # Mongoid::Document model for dc_ads collection.
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_ad : Ads
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time updated_at
32
+ # position String Position (div id) where this ad will be displayed
33
+ # description String Short description
34
+ # type Integer Content type of add
35
+ # file String Picture or flash filename
36
+ # script String JavaScript script, when script type
37
+ # link String Link to page (site, dokument) when ad is clicked
38
+ # link_target String Define if link is open in new window or same
39
+ # height Integer Height of ad
40
+ # width Integer Width of ad
41
+ # valid_from DateTime Ad is valid from
42
+ # valid_to DateTime Ad is valid to
43
+ # displays Integer Maximum number of time this add is displayed
44
+ # clicks Integer Maximum number of clicks this ad will receive
45
+ # priority Integer Priority. Higher priority means ad is shown more often. Priority is calculated only between candidats to be displayed.
46
+ # displayed Integer No. of times this add has been displayed
47
+ # clicked Integer No. of times this ad has been clicked
48
+ # active Mongoid::Boolean Ad is active
49
+ # created_by BSON::ObjectId created_by
50
+ # updated_by BSON::ObjectId Record last updated by
51
+ # dc_site_id Object Ad is valid for the site
26
52
  #
27
53
  # Ads can be defined as picture file, flash file or script.
28
54
  #
@@ -22,7 +22,16 @@
22
22
  #++
23
23
 
24
24
  ##########################################################################
25
- # Mongoid::Document model for dc_ad_stats collection.
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_ad_stat : Ads statistics
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time updated_at
32
+ # dc_ad_id BSON::ObjectId dc_ad_id
33
+ # ip String ip
34
+ # type Integer type
26
35
  #
27
36
  # Documents in this collection holds data about displays and clicks on ads which
28
37
  # can be further processed for statistical purposes.
@@ -22,7 +22,24 @@
22
22
  #++
23
23
 
24
24
  ##########################################################################
25
- # Mongoid::Document model for dc_big_menus collection.
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_big_menu : Big menus
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time updated_at
32
+ # caption String caption
33
+ # picture String picture
34
+ # parent BSON::ObjectId parent
35
+ # link String link
36
+ # page_id BSON::ObjectId page_id
37
+ # order Integer order
38
+ # active Mongoid::Boolean active
39
+ # policy_id BSON::ObjectId policy_id
40
+ # created_by BSON::ObjectId created_by
41
+ # updated_by BSON::ObjectId updated_by
42
+ # dc_site_id Object dc_site_id
26
43
  #
27
44
  # This menu system is still experimental. It can be used for sites with menu
28
45
  # which has many sub menus each linked to its own document. Unlike other menu
@@ -22,8 +22,21 @@
22
22
  #++
23
23
 
24
24
  ##########################################################################
25
- # Mongoid::Document model for dc_big_table collection.
26
- #
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_big_table : Big Table
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time updated_at
32
+ # key String Key (ident) used to retrieve key/values
33
+ # description String description
34
+ # site_id BSON::ObjectId Data will be used only for defined site. If empty, then it is default for all sites in database.
35
+ # active Mongoid::Boolean This key is active
36
+ # created_by BSON::ObjectId created_by
37
+ # updated_by BSON::ObjectId updated_by
38
+ # dc_big_table_values Embedded:DcBigTableValue Values defined by this key
39
+ #
27
40
  # Big table is meant to be a common space for defining default choices for select fields on forms.
28
41
  # Documents are organized as key-value pair with the difference that values for the key can
29
42
  # be defined for every site and can also be localized.
@@ -22,8 +22,19 @@
22
22
  #++
23
23
 
24
24
  #####################################################################
25
- # Mongoid::Document model for dc_big_table_locale embedded document.
26
- #
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_big_table_locale : Big Table - locales
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time updated_at
32
+ # locale String Locale
33
+ # description String Short description translated for target locale
34
+ # active Mongoid::Boolean active
35
+ # created_by BSON::ObjectId created_by
36
+ # updated_by BSON::ObjectId updated_by
37
+ #
27
38
  # Documents are embedded in DcBigTableValue documents. Every value defined in
28
39
  # DcBigTableValue parent document can have its locale translation.
29
40
  #####################################################################
@@ -22,8 +22,20 @@
22
22
  #++
23
23
 
24
24
  #####################################################################
25
- # Mongoid::Document model for dc_big_table_values embedded documents.
26
- #
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_big_table_value : Big Table - values
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time updated_at
32
+ # value String Value
33
+ # description String Short description of the value
34
+ # active Mongoid::Boolean Active
35
+ # created_by BSON::ObjectId created_by
36
+ # updated_by BSON::ObjectId updated_by
37
+ # dc_big_table_locales Embedded:DcBigTableLocale Locale translations for the value
38
+ #
27
39
  # Documents are embedded in DcBigTable document. Every value defined has its own description,
28
40
  # which can further be translated in embedded DcBigTableLocale documents.
29
41
  #####################################################################
@@ -22,8 +22,22 @@
22
22
  #++
23
23
 
24
24
  #####################################################################
25
- # Mongoid::Document model for dc_categories collection.
26
- #
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_category : Categories
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time Last update
32
+ # name String Category name
33
+ # description String Additional description of category
34
+ # ctype Integer Category type. Could be used for grouping categories.
35
+ # parent BSON::ObjectId Parent category. Leave blank if this is top level category.
36
+ # active Mongoid::Boolean Category is active.
37
+ # order Integer Additional order, which can be used for sorting.
38
+ # created_by BSON::ObjectId created_by
39
+ # updated_by BSON::ObjectId updated_by
40
+ #
27
41
  # Categories are used on DcPage documents for grouping documents. Categorization
28
42
  # is most useful for grouping news, blog entries ...
29
43
  #####################################################################
@@ -22,7 +22,23 @@
22
22
  #++
23
23
 
24
24
  ########################################################################
25
- # Mongoid::Document model for dc_designs collection.
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_design : Designs
28
+ #
29
+ # _id BSON::ObjectId _id
30
+ # created_at Time created_at
31
+ # updated_at Time updated_at
32
+ # description String Short description of design
33
+ # body String Body of design which will be rendered like any Rails view
34
+ # css String CSS for design
35
+ # rails_view String Rails view (file) name which will be used to render design
36
+ # author String Creater if design
37
+ # active Mongoid::Boolean Is the design active
38
+ # created_by BSON::ObjectId created_by
39
+ # updated_by BSON::ObjectId Last updated by
40
+ # site_id BSON::ObjectId Select site name if this design belongs to singe site
41
+ # dc_parts Embedded:DcPart Parts of design
26
42
  #
27
43
  # Designs are essential parts of DRG CMS. Every DcPage document must have its design document defined.
28
44
  # If DcPage documents are anchors for url addresses, DcDesign documents define how
@@ -22,7 +22,11 @@
22
22
  #++
23
23
 
24
24
  ########################################################################
25
- # Mongoid::Document model for dc_dummy collection.
25
+ # == Schema information
26
+ #
27
+ # Collection name: dc_dummy : Collection name used when form does not belong to database model.
28
+ #
29
+ # _id BSON::ObjectId _id
26
30
  #
27
31
  # Which is not collection at all. DcDummy model is used for entering data on forms
28
32
  # where data will not be saved to database but will instead be processed by custom
@@ -0,0 +1,172 @@
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
+
24
+ ##########################################################################
25
+ # Model and collection for filtering and sorting data.
26
+ ##########################################################################
27
+ class DcFilter
28
+ include Mongoid::Document
29
+ include Mongoid::Timestamps
30
+
31
+ field :dc_user_id, type: BSON::ObjectId
32
+ field :table, type: String
33
+ field :description, type: String
34
+ field :filter, type: String, default: ''
35
+ field :public, type: Boolean
36
+ field :active, type: Boolean, default: true
37
+
38
+ index( { collection: 1, dc_user_id: 1 } )
39
+
40
+ validates :description, presence: true
41
+
42
+ before_save :do_before_save
43
+
44
+ ######################################################################
45
+ # Implementation of before_save callback.
46
+ ######################################################################
47
+ def do_before_save
48
+ self.dc_user_id = nil if self.public
49
+ end
50
+
51
+ ######################################################################
52
+ # Will return model with filter query set
53
+ ######################################################################
54
+ def self.get_filter(filter)
55
+ yaml = YAML.load(filter) rescue nil
56
+ return yaml if yaml.nil?
57
+ return nil if yaml['table'].nil? # old data
58
+ #
59
+ model = yaml['table'].classify.constantize
60
+ field = yaml['field'] == 'id' ? '_id' : yaml['field'] # must be
61
+ # if value == NIL no filter is necessary
62
+ return nil if yaml['value'].class == String and yaml['value'] == '#NIL'
63
+
64
+ # do regex if operation is like
65
+ value = yaml['operation'] == 'like' ? /#{yaml['value']}/i : yaml['value']
66
+ # when field type is ObjectId transform value
67
+ if model.fields[field] and model.fields[field].type == BSON::ObjectId
68
+ value = BSON::ObjectId.from_string(value) rescue nil
69
+ end
70
+ #
71
+ p yaml,'***************'
72
+ if ['eq','like'].include?(yaml['operation'])
73
+ model.where(field => value)
74
+ # TODO in operator
75
+ else
76
+ model.where(field.to_sym.send(yaml['operation']) => value)
77
+ end
78
+ end
79
+
80
+ ############################################################################
81
+ # Will return field form definition if field is defined on form. Field definition
82
+ # will be used for input field on the form.
83
+ ############################################################################
84
+ def self.get_field_form_definition(name, parent) #:nodoc:
85
+ form = parent.form
86
+ form['form']['tabs'].each do |tab|
87
+ tab.each do |field|
88
+ next if field.class == String # tab name
89
+ field.each {|k,v| return v if v['name'] == name }
90
+ end
91
+ end if form['form']['tabs'] # I know. But nice.
92
+ #
93
+ form['form']['fields'].each do |field|
94
+ return field.last if field.last['name'] == name
95
+ end if form['form']['fields']
96
+ nil
97
+ end
98
+
99
+ ############################################################################
100
+ # Return filter input field for entering variable filter values on index form
101
+ ############################################################################
102
+ def self.get_filter_field(parent)
103
+ return '' if parent.session[ parent.form['table'] ].nil?
104
+ filter = parent.session[ parent.form['table'] ][:filter]
105
+ return '' if filter.nil?
106
+ #
107
+ filter = YAML.load(filter) rescue nil
108
+ return '' if filter.nil?
109
+ # return '' if filter['input'].nil?
110
+ #
111
+ p filter
112
+ field = get_field_form_definition(filter['field'], parent)
113
+ return '' if field.nil? and filter['input'].nil?
114
+ field = {} if field.nil?
115
+ # field redefined with input keyword
116
+ field['name'] = 'filter_field'
117
+ field['type'] = filter['input'] if filter['input'].size > 5
118
+ field['html'] = {} if field['html'].nil?
119
+ field['html']['size'] = 20
120
+ field['html']['value'] = filter['value'] unless filter['value'] == '#NIL'
121
+
122
+ field['html']['data-url'] = parent.url_for(
123
+ controller: 'cmsedit',action: :index, filter: 'on',
124
+ table: parent.form['table'], formname: parent.form['formname'])
125
+ url = field['html']['data-url']
126
+ #
127
+ field_type = filter['input'].size > 5 ? filter['input'] : field['type']
128
+ klas_string = field_type.camelize
129
+ klas = DrgcmsFormFields::const_get(klas_string) rescue nil
130
+ return '' if klas.nil?
131
+ #
132
+ object = klas.new(parent, nil, field).render
133
+ js = object.js
134
+ "<span class=\"filter_field\" data-url=\"#{url}\">#{object.html} " <<
135
+ parent.fa_icon('filter lg dc-green', class: 'record_filter_field_icon') <<
136
+ (js.size > 2 ? parent.javascript_tag(js) : '') << '</span>'
137
+ end
138
+
139
+ ######################################################################
140
+ # Create popup menu for filter options.
141
+ ######################################################################
142
+ def self.menu_filter(parent)
143
+ html = '<ul class="menu-filter">'
144
+ table = parent.form['table']
145
+ documents = self.where(table: table, active: true).to_a
146
+ documents.each do |document|
147
+ html << "<li data-filter=\"\">#{document.description}</li>"
148
+ end
149
+
150
+ # add filters defined in model
151
+ model = table.classify.constantize
152
+ filters = model.dc_filters rescue nil
153
+ if filters
154
+ # only single defined. Convert to array.
155
+ filters = [filters] if filters.class == Hash
156
+ filters.each do |filter|
157
+ url = parent.dc_link_to(filter['title'], nil,controller: :cmsedit, action: :index, table: table,
158
+ formname: parent.params[:formname],
159
+ filter_field: filter['field'],
160
+ filter_oper: filter['operation'],
161
+ filter_value: filter['value'],
162
+ filter: 'on')
163
+ html << "<li>#{url}</li>"
164
+ end
165
+ end
166
+ # divide standard and custom filter options
167
+ html << '<hr>' if html.size > 30 #
168
+ # html << '<li onclick="$(\'#drgcms_filter\').toggle(300);">' + I18n.t('drgcms.filter_set') + '</li>'
169
+ html << '<li id="open_drgcms_filter">' + I18n.t('drgcms.filter_set') + '</li>'
170
+ html << '</ul>'
171
+ end
172
+ end