# This module contains helper methods to assist in including the multi bit shift flash applet for attributes created with the acts_as_mbs_files # method. It will be automatically included into ActionView::Base, thereby making this module�s methods available in all your views. # # Note: This documentation heavily influenced by the open source plugin FileColumn's documentation. module MultiBitShiftHelper # Returns a multi bit shift field input tailored for accessing a specified attribute (identified by method) on an object assigned to # the template (identified by object). A hidden field is used to persist the guid over multiple validation attempts. # The width and height options are set to 420 and 240 by default, and the applicationToken is automatically set to guid of the field. # In addition, the actions for the uploadURL and removeFileURL are automatically set, to upload_file, and remove_files respectively. Finally, # the existing files are passed into the applet. A validation object can be passed using the validation_object index of the options hash. # # By default, this helper is used with the Multi Bit Shift applet. To use with the advanced applet, mbs_version option should be set to 'Advanced', # and the width and height should be set to pixel values. Addtionally, the done button should be disabled. # # The other options that can be specified using the options hash can be found in the queryStringLoader parameters # documentation of the flash applet, which can be located at http://multibitshift.com/documentation/view. These options will be passed to the # flash applet and can be used to customize the applet at runtime. def multi_bit_shift_field(object_name, method, options={}) if options['width'].nil? options['width'] = '420' end if options['height'].nil? options['height'] = '240' end if options['applicationToken'].nil? options['applicationToken'] = self.instance_variable_get("@#{object_name}").method("#{method}_guid").call end if options['uploadURL'].nil? options['uploadURL'] = url_for(:action => 'upload_file') end if options['removeFileURL'].nil? options['removeFileURL'] = url_for(:action => 'remove_files') end if !options['validation_object'].nil? count = 1 for file in Object.const_get(options['validation_object'].file_class).find_all_by_associated_with(options['applicationToken']) options["uploadedFileName#{count}"] = file["file_name"] options["uploadedFileSize#{count}"] = File.size(file.file_name).to_s count = count + 1 end options = options['validation_object'].convert_to_flash_params.merge(options) options.delete('validation_object') end output = [] output << hidden_field(object_name, method) output << multi_bit_shift_flash(options) output.join('') end # Inserts the javascript and HTML to present the multi bit shift flash file. # The options hash contains the options (for all options, see # http://multibitshift.com/docs/multi_bit_shift/docs/files/as/queryStringLoader-as.html) # to pass to the flash file, as well as additional options as listed below: # * 'width' - Width of the applet, defaults to 100% # * 'height' - Height of the applet, defaults to 100% # * 'ajax_safe' - Determines if the applet will be inserted via ajax. If set to true, the applet does not make use of javascript to load, # and instead is directly embedded. Defaults to false. # * 'mbs_version' - What version of the Multi Bit Shift Applet to include. This method sets the file_name attribute to flashFileHelperAdvanced # when "Advanced" is set. Accepted values are "Regular" and "Advanced", defaults to "Regular". # * 'file_name' - Sets the file name of the swf file to load. This parameter should not be modified directly, instead the mbs_version property # should be set. Defaults to "flashFileHelper". # * 'uploadURL' - URL that files should be uploaded to. Defaults to action 'upload_file'. # * 'removeFileURL' - URL that files should be removed with. Defaults to action 'remove_files'. # * 'fileListURL' - URL that lists files. Defaults to action 'files_on_server'. # * 'renameFileURL' - URL that files should be renamed with. Defaults to action 'rename_file'. # * 'rotateCWURL' - URL that clockwise rotation requests will be sent to. Defaults to ''. # * 'rotateCCWURL' - URL that counter-clockwise rotation requests will be sent to. Defaults to ''. # * 'doneURL' - URL that Done button redirects to. Defaults to action 'index'. # * 'jsDoneCallback' - Javascript function to call when done button is clicked. Setting this overrides doneURL. Defaults to ''. # * 'flashCSS' - URL of compiled CSS to use. Defaults to /flash/css/siu.swf. # # The other options can be found in the queryStringLoader parameters # documentation of the flash applet, which can be located at http://multibitshift.com/documentation/view. # # The code that is generated is based entirely on output from Macromedia Flex. def multi_bit_shift_flash(options={}) if options['width'].nil? width = '100%' else width = options['width'] end if options['height'].nil? height = '100%' else height = options['height'] end if options['ajax_safe'].nil? options['ajax_safe'] = false.to_s else options['ajax_safe'] = options['ajax_safe'].to_s end if !options['mbs_version'].nil? and options['mbs_version'] == "Advanced" options['file_name'] = "flashFileHelperAdvanced" end if options['file_name'].nil? file_name = "flashFileHelper" else file_name = options['file_name'] options.delete('file_name') end if options['uploadURL'].nil? options['uploadURL'] = url_for(:action => 'upload_file') end if options['removeFileURL'].nil? options['removeFileURL'] = url_for(:action => 'remove_files') end if options['fileListURL'].nil? options['fileListURL'] = url_for(:action => 'files_on_server') end if options['renameFileURL'].nil? options['renameFileURL'] = url_for(:action => 'rename_file') end if options['doneURL'].nil? options['doneURL'] = url_for(:action => 'index') end if options['flashCSS'].nil? options['flashCSS'] = '/flash/css/siu.swf' end if options['rotateCWImageURL'].nil? options['rotateCWImageURL'] = '/flash/cw.gif' end if options['rotateCCWImageURL'].nil? options['rotateCCWImageURL'] = '/flash/ccw.gif' end options.each{ |key,value| options[key] = URI.escape(value) } parameters = [] options.each{ |key,value| parameters << "#{key}=#{value}" } parameters = parameters.join('&') if options['ajax_safe'] == "true" insert_string = < END else insert_string = < END end return insert_string end private # Calls the compute_public_path function to generate a path link to the public item. def file_path(source) path = compute_public_path(source, 'flash', '') if path[-1..-1] == "." path = path[0..-2] end path end end