Method: WidgetList::Widgets.widget_select

Defined in:
lib/widget_list/widgets.rb

.widget_select(sql = '', list = {}) ⇒ Object

WidgetSelect - todo - never tested one line of code in here!!!



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
# File 'lib/widget_list/widgets.rb', line 73

def self.widget_select(sql = '', list={})
  valid = true
  selectOutput = []

  #Parameter evaluation identification
  #
  if list.empty? && sql.class.name == 'Hash'
    list = sql
  end

  if ! sql.empty? && ! list.key?('sql') && ! list.key?('view')
    list['sql'] = sql
  end

  #
  # Default configurations
  #
  items = {}

  items['name']              = ''
  items['id']                = ''
  items['multiple']          = false
  items['insertUnknown']     = false
  items['required']          = ''
  items['strLenBrk']         = 50
  items['optGroupIndicator'] = ''
  items['label']             = ''
  items['selectTxt']         = 'Select One'
  items['showId']            = false
  items['showIdLeft']        = false
  items['passive']           = false
  items['freeflow']          = false
  items['no_style']          = false

  #
  # Select box attributes
  #
  items['attribs']   = []
  items['size']      = 1
  items['disabled']  = false
  items['noDataMsg'] = 'No Data'
  items['selectOne'] = 0
  items['selected']  = {}

  #
  # SQL
  #
  items['bindVars'] = {}
  items['view']     = ''
  items['sql']      = ''
  items['orderBy']  = ''
  items['filter']   = ''

  #
  # Actions
  #
  items['onchange'] = ''
  items['onclick']  = ''

  #
  # Style
  #
  items['style']       = ''             #meant for internal use as nothing is appended only replaced.
  items['inner_class'] = 'select-inner' #Left long piece of image
  items['outer_class'] = 'select-outer' #Right corner of image
  items['outer_style'] = ''             #Basically the width of the select box ends up here
  items['extra_class'] = ''             #Not used currently
  items['width']       = 184            #width of the select-outer element

  #
  # @deprecated
  #
  items['class'] = ''


  #
  # Setup templates
  #
  if items['freeflow']
    #
    # Freeflow
    #
    items['template']['wrapper'] = $G_TEMPLATE['widget']['selectfree']['wrapper']
    items['template']['option']  = $G_TEMPLATE['widget']['selectfree']['option']
    items['template']['initial'] = ''
    items['template']['passive'] = ''
  else
    #
    # Standard
    #
    items['template']['wrapper'] = $G_TEMPLATE['widget']['selectbox']['wrapper']
    items['template']['option']  = $G_TEMPLATE['widget']['selectbox']['option']
    items['template']['initial'] = $G_TEMPLATE['widget']['selectbox']['initial']
    items['template']['passive'] = $G_TEMPLATE['widget']['selectbox']['passive']
  end

  # if numeric or string passed, convert to array
  if list.key?('selected') && list['selected'].class.name != 'Array'
    list['selected'] = [ list['selected'] ]
  end

  #
  # Merge settings
  #
  items = populate_items(list,items)

  if items['no_style']
    items['template']['wrapper'] = $G_TEMPLATE['widget']['selectbox_nostyle']['wrapper']
  end


  #
  # If multiple then override above settings and classes
  #
  if items['multiple']
    items['outer_style'] = ';width:280px;'
    items['width'] = 280

    items['inner_class'] = 'select-inner-multiple'
    items['outer_class'] = 'select-outer-multiple'
  end

  if items['showId']
    items['template']['option'] = $G_TEMPLATE['widget']['selectbox']['option_showid']
  end

  if items['showIdLeft']
    items['template']['option'] = $G_TEMPLATE['widget']['selectbox']['option_showid_left']
  end

  if items['disabled']
    items['disabled']  = 'disabled="disabled"'
    items['class']    += ' disabled'
  end

  if items['multiple']
    items['multiple'] = 'multiple="multiple"'
  end

  if items['required']
    items['required'] = $G_TEMPLATE['widget']['required']
  end

  if ! items['sql'].empty?
    sql   = items['sql']
  elsif !items['view'].empty?
    sql   = "SELECT id, value FROM " + items['view']
  else
    valid = false
  end

  if ! items['filter'].empty?
    sql += ' WHERE ' + items['filter'].join(' AND ')
  end

  if ! items['orderBy'].empty?
    sql += ' ORDER BY ' + items['orderBy']
  end

  rows = WidgetList::List.get_database._select(sql, [],  items['bindVars'])
  selectRows = WidgetList::List.get_database.final_results


  if rows > 0 && valid
    if ! items['passive']
      addedGroup   = false
      groupByRow   = false
      groupByValue = -1
      if selectRows.key?('GROUPING_VAL')
        groupByRow = true
      end

      max      = rows-1

      selected = ''

      if (items['selectOne'] > 0 || (items['selectOne'].class.name == 'Array' && ! items['selectOne'].empty?) && ! items['freeflow'])
        theVal = ''
        theClk = ''
        theSel = ''
        theTxt = items['selectTxt']

        if items['selectOne'].class.name == 'Array'
          if(items['selectOne'].key?('text'))
            theTxt = items['selectOne']['text']
          end

          if(items['selectOne'].key?('value'))
            theVal = items['selectOne']['value']
          end
        end

        pieces = { '<!--VALUE-->' => theVal,
                   '<!--ONCLICK-->' => theClk,
                   '<!--SELECTED-->' => theSel,
                   '<!--CONTENT-->' => theTxt
        }


        selectOutput << WidgetList::Utils::fill(pieces, items['template']['initial'])
      end

      hasASelectedMatch = false
      startedGrouping   = false
      i    = 0
      for i in j..max
        if items['selected'].class.name == 'Array' && items['selected'].count > 0
          if items['selected'].include?(selectRows['ID'][i]) || items['selected'].include?(selectRows['VALUE'][i])
            hasASelectedMatch = true
            selected = ' selected'
          end
        end

        if selectRows['VALUE'][i].length > items['strLenBrk']
          first = CGI.escapeHTML(selectRows['VALUE'][i].to_s[0,10])
          last  = CGI.escapeHTML(selectRows['VALUE'][i].to_s[-40])
          content = first + '...' + last
        else
          content = CGI.escapeHTML(selectRows['VALUE'][i])
        end

        pieces = {
          '<!--VALUE-->'    => selectRows['ID'][i],
          '<!--ONCLICK-->'  => items['onclick'],
          '<!--SELECTED-->' => selected,
          '<!--CONTENT-->'  => content,
          '<!--COUNTER-->'  => i,
          '<!--NAME-->'     => items['name'],
        }


        if groupByRow
          if  selectRows['GROUPING_VAL'][i] != groupByValue
            if (addedGroup)
              selectOutput << '</optgroup>'
            end

            addedGroup = true
            # Start new optgroup
            groupByValue = selectRows['GROUPING_VAL'][i]

            selectOutput << '<optgroup label="' + groupByValue + '">'
            selectOutput << WidgetList::Utils::fill(pieces, items['template']['option'])
          else
            selectOutput << WidgetList::Utils::fill(pieces, items['template']['option'])
          end
        else
          if selectRows['ID'][i] === 'GROUPING'
            # Output OPTGROUP tags and labels no one can select this
            if startedGrouping
              selectOutput << '</optgroup>'
            end
            selectOutput   << '<optgroup label="' + selectRows['VALUE'][i] + '">'
            startedGrouping = true
          else
            # Output regular option tag
            selectOutput << WidgetList::Utils::fill(pieces, items['template']['option'])
          end
        end

        selected = ''
      end

      if (groupByRow && addedGroup)
        selectOutput << '</optgroup>'
      end

      if hasASelectedMatch === false && items['insertUnknown'] === true
        # in this mode, you wish to inject an option into the select box even though the results didnt find the match


        items['selected'].each{ |vals|
          pieces = {
            '<!--VALUE-->'    => vals,
            '<!--ONCLICK-->'  => items['onclick'],
            '<!--SELECTED-->' => ' selected ',
            '<!--CONTENT-->'  => vals
          }
          selectOutput << WidgetList::Utils::fill(pieces, items['template']['option'])
        }
      end
    end
  else
    pieces = {
      '<!--VALUE-->'    => '',
      '<!--ONCLICK-->'  => items['onclick'],
      '<!--SELECTED-->' => '',
      '<!--CONTENT-->'  => items['noDataMsg']
    }

    selectOutput << WidgetList::Utils::fill(pieces, items['template']['initial'])
  end

  if items['id'].empty? && ! items['name'].empty?
    items['id'] = items['name']
  end


  pieces =     { '<!--SIZE-->'         => items['size'],
                 '<!--ID-->'           => items['id'],
                 '<!--NAME-->'         => items['name'],
                 '<!--MULTIPLE-->'     => items['multiple'],
                 '<!--OPTIONS-->'      => selectOutput.join(''),
                 '<!--ONCHANGE-->'     => items['onchange'],
                 '<!--REQUIRED-->'     => items['required'],
                 '<!--CLASS-->'        => items['class'],
                 '<!--DISABLED_FLG-->' => items['disabled'],
                 '<!--STYLE-->'        => items['style'],
                 '<!--ATTRIBUTES-->'   => items['attribs'].join(' '),
                 '<!--INNER_CLASS-->'  => items['inner_class'],
                 '<!--OUTER_CLASS-->'  => items['outer_class'],
                 '<!--OUTER_STYLE-->'  => items['outer_style'],
                 '<!--INNER_STYLE-->'  => '',
                 '<!--OUTER_ACTION-->' => ''
  }

  finalTemplate = items['template']['wrapper']

  if items['passive'] && items['selected'].class.name == 'Array' && items['selected'].count > 0
    #passKeys                    = array_keys(selectRows['ID'], items['selected'][0])
    #pieces['<!--OPTIONS-->']    = selectRows['VALUE'][passKeys[0]]
    finalTemplate               = items['template']['passive']
  end

  WidgetList::Utils::fill(pieces, finalTemplate)
end