Index: core/admin_templates/js/uploader/uploader.js IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/admin_templates/js/uploader/uploader.js (revision 15748) +++ core/admin_templates/js/uploader/uploader.js (revision ) @@ -7,7 +7,7 @@ // normalize params if (isNaN(parseInt(params.multiple))) { - // ensure that maximal file number is greather then zero + // ensure that maximal file number is greater then zero params.multiple = 1; } @@ -21,17 +21,26 @@ this.params = params; this._ensureDefaultValues(); + this.files = []; this.files_count = 0; - this.files = new Array(); - this.deleted = new Array(); + this.deleted = []; // because used outside this class this.deleteURL = params.deleteURL; this.enableUploadButton(); + this._attachEventHandler(); } /* ==== Private methods ==== */ +Uploader.prototype._attachEventHandler = function() { + var $me = this; + + $(document).bind('UploadsManager.Uploader.' + crc32(this.id), function ($e, $method) { + $me[$method].apply($me, Array.prototype.slice.call(arguments, 2)); + }); +}; + Uploader.prototype._ensureDefaultValues = function() { // Upload backend settings var $defaults = { @@ -49,6 +58,9 @@ multiple : 0, field : '', thumb_format: '', + urls : '', + names : '', + sizes : '', fileQueueLimit : 0, buttonImageURL : '', buttonWidth : 1, @@ -70,7 +82,7 @@ this.params[$param_name] = $defaults[$param_name]; } } -} +}; Uploader.prototype._normalizeFilesize = function($file_size) { var $normalize_size = parseInt($file_size); @@ -80,19 +92,20 @@ // in kilobytes (flash doesn't recognize numbers, that are longer, then 9 digits) return $normalize_size / 1024; -} +}; Uploader.prototype._prepareFiles = function() { var ids = ''; var names = ''; + // process uploaded files for (var f = 0; f < this.files.length; f++) { if (isset(this.files[f].uploaded) && !isset(this.files[f].temp)) { continue; } - ids += this.files[f].id + '|' - names += this.files[f].name + '|' + ids += this.files[f].id + '|'; + names += this.files[f].name + '|'; } ids = ids.replace(/\|$/, '', ids); @@ -101,7 +114,7 @@ document.getElementById(this.id+'[tmp_ids]').value = ids; document.getElementById(this.id+'[tmp_names]').value = names; document.getElementById(this.id+'[tmp_deleted]').value = this.deleted.join('|'); -} +}; Uploader.prototype._formatSize = function (bytes) { var kb = Math.round(bytes / 1024); @@ -113,7 +126,7 @@ var mb = Math.round(kb / 1024 * 100) / 100; return mb + ' MB'; -} +}; Uploader.prototype._executeNextEvent = function () { var f = this._eventQueue ? this._eventQueue.shift() : null; @@ -181,14 +194,38 @@ } ) + this.refreshQueue(); +}; + +Uploader.prototype.refreshQueue = function($params) { + if ( $params !== undefined ) { + $.extend(true, this.params, $params); + + document.getElementById(this.id+'[upload]').value = this.params.names; + document.getElementById(this.id+'[order]').value = this.params.names; + } + + // 1. remove queue DIVs for files, that doesn't exist after upload was made + var $new_file_ids = this.getFileIds(this.params.names); + + for (var $i = 0; $i < this.files.length; $i++) { + if ( !in_array(this.files[$i].id, $new_file_ids) ) { + this.updateQueueFile($i, true); + } + } + + this.files = []; + this.files_count = 0; + this.deleted = []; + if (this.params.urls != '') { - var urls = this.params.urls.split('|'); - var names = this.params.names.split('|'); - var sizes = this.params.sizes.split('|'); + var urls = this.params.urls.split('|'), + names = this.params.names.split('|'), + sizes = this.params.sizes.split('|'); for (var i = 0; i < urls.length; i++) { var a_file = { - id : 'uploaded_' + crc32(names[i]), + id : this.getUploadedFileId(names[i]), name : names[i], url : urls[i], size: sizes[i], @@ -196,14 +233,36 @@ progress: 100 }; - this.files.push(a_file); this.files_count++; + this.files.push(a_file); } this.updateInfo(); } +}; + +Uploader.prototype.getFileIds = function($file_names) { + var $ret = []; + + if ( !$file_names.length ) { + return $ret; -} + } + if ( !$.isArray($file_names) ) { + $file_names = $file_names.split('|'); + } + + for (var i = 0; i < $file_names.length; i++) { + $ret.push(this.getUploadedFileId($file_names[i])) + } + + return $ret; +}; + +Uploader.prototype.getUploadedFileId = function($file_name) { + return 'uploaded_' + crc32($file_name); +}; + Uploader.prototype.enableUploadButton = function() { var $me = this; @@ -217,14 +276,14 @@ ) } ); -} +}; Uploader.prototype.renderBrowseButton = function() { var holder = document.getElementById(this.params.buttonPlaceholderId); this.swf.write(holder); this.flash = document.getElementById(this.flash_id); -} +}; Uploader.prototype.remove = function() { var id = this.params.buttonPlaceholderId; @@ -252,7 +311,7 @@ obj.parentNode.removeChild(obj); } } -} +}; Uploader.prototype.removeObjectInIE = function(id) { var obj = document.getElementById(id); @@ -264,7 +323,7 @@ } obj.parentNode.removeChild(obj); } -} +}; Uploader.prototype.isImage = function($filename) { this.removeTempExtension($filename).match(/\.([^.]*)$/); @@ -272,7 +331,7 @@ var $ext = RegExp.$1.toLowerCase(); return $ext.match(/^(bmp|gif|jpg|jpeg|png)$/); -} +}; Uploader.prototype.getFileIcon = function($filename) { this.removeTempExtension($filename).match(/\.([^.]*)$/); @@ -299,11 +358,11 @@ var $icon = $ext.match(/^(ai|avi|bmp|cs|dll|doc|dot|exe|fla|gif|htm|html|jpg|js|mdb|mp3|pdf|ppt|rdp|swf|swt|txt|vsd|xls|xml|zip)$/) ? $ext : 'default.icon'; return this.IconPath + '/' + $icon + '.gif'; -} +}; Uploader.prototype.removeTempExtension = function ($file) { return $file.replace(/(_[\d]+)?\.tmp$/, ''); -} +}; Uploader.prototype.getQueueElement = function($file) { var $ret = ''; @@ -326,7 +385,7 @@ $ret += ''; } - $ret += '' + $ret += ''; // add filename + preview link $ret += '
' + $file_label + '
'; @@ -379,7 +438,7 @@ } return $ret; -} +}; Uploader.prototype.getSortedFiles = function($ordered_queue) { var $me = this; @@ -392,10 +451,10 @@ }); return $ret; -} +}; Uploader.prototype.updateQueueFile = function($file_index, $delete_file) { - $queue_container = $( jq('#' + this.id + '_queueinfo') ); + var $queue_container = $( jq('#' + this.id + '_queueinfo') ); if ($delete_file !== undefined && $delete_file) { $( jq('#' + this.files[$file_index].id + '_queue_row') ).remove(); @@ -406,8 +465,8 @@ return ; } - $ret = this.getQueueElement( this.files[$file_index] ); - var $row = $( jq('#' + this.files[$file_index].id + '_queue_row') ); + var $ret = this.getQueueElement(this.files[$file_index]), + $row = $(jq('#' + this.files[$file_index].id + '_queue_row')); if ($row.length > 0) { // file round -> replace @@ -418,7 +477,7 @@ $( jq('#' + this.id + '_queueinfo') ).append($ret); $queue_container.css('margin-top', '8px'); } -} +}; Uploader.prototype.updateInfo = function($file_index, $prepare_only) { if ($prepare_only === undefined || !$prepare_only) { @@ -433,20 +492,19 @@ } this._prepareFiles(); -} +}; Uploader.prototype.updateProgressOnly = function ($file_index) { var $progress_code = '
'; $('#' + this.files[$file_index].id + '_progress').html($progress_code); -} +}; Uploader.prototype.removeFile = function (file) { - var count = 0; - var n_files = new Array(); + var count = 0, + n_files = [], + $to_delete = []; - var $to_delete = []; - for (var f = 0; f < this.files.length; f++) { if (this.files[f].id != file.id && this.files[f].name != file.id) { n_files.push(this.files[f]); @@ -464,7 +522,7 @@ this.files = n_files; this.files_count = count; this.updateInfo(undefined, true); -} +}; Uploader.prototype.hasQueue = function() { for (var f = 0; f < this.files.length; f++) { @@ -476,7 +534,7 @@ } return false; -} +}; Uploader.prototype.startUpload = function() { this.uploadCancelled = false; @@ -486,7 +544,7 @@ } this.callFlash('StartUpload'); -} +}; Uploader.prototype.cancelUpload = function() { this.callFlash('StopUpload'); @@ -498,7 +556,7 @@ } this.uploadCancelled = true; -} +}; Uploader.prototype.UploadFileStart = function(file) { var $file_index = this.getFileIndex(file); @@ -511,13 +569,13 @@ // we can prevent user from adding any files here :) this.callFlash('ReturnUploadStart', [true]); -} +}; Uploader.prototype.UploadProgress = function(file, bytesLoaded, bytesTotal) { var $file_index = this.getFileIndex(file); this.files[$file_index].progress = Math.round(bytesLoaded / bytesTotal * 100); this.updateProgressOnly($file_index); -} +}; Uploader.prototype.UploadSuccess = function(file, serverData, receivedResponse) { if (!receivedResponse) { @@ -530,7 +588,7 @@ this.files[f].name = serverData; } } -} +}; Uploader.prototype.UploadFileComplete = function(file) { // file was uploaded OR file upload was cancelled @@ -554,7 +612,7 @@ else { UploadsManager.UploadQueueComplete(this); } -} +}; Uploader.prototype.getUrl = function($file, $preview) { var $url = this.params.previewURL.replace('#FILE#', encodeURIComponent($file.name)).replace('#FIELD#', this.params.field); @@ -568,7 +626,7 @@ } return $url; -} +}; Uploader.prototype.getFileIndex = function(file) { for (var f = 0; f < this.files.length; f++) { @@ -578,7 +636,7 @@ } return false; -} +}; Uploader.prototype.queueEvent = function (function_body) { // Warning: Don't call this.debug inside here or you'll create an infinite loop @@ -613,7 +671,7 @@ }, 0 ); -} +}; // Private: callFlash handles function calls made to the Flash element. // Calls are made with a setTimeout for some functions to work around @@ -681,4 +739,4 @@ // process events, queued before flash load this._executeQueuedEvents(); -} \ No newline at end of file +}; \ No newline at end of file Index: core/units/helpers/ajax_form_helper.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/helpers/ajax_form_helper.php (revision 15716) +++ core/units/helpers/ajax_form_helper.php (revision ) @@ -46,6 +46,42 @@ } /** + * Returns information about all uploader fields. + * + * @param kDBItem $object Object to process + * @param array|null $fields Fields filter. + * + * @return array + * @access public + */ + public function getUploaderInfo(kDBItem $object, $fields = null) + { + $ret = array(); + + if ( !isset($fields) ) { + $fields = array_keys($object->getFields()); + } + + foreach ($fields as $field) { + $formatter = $object->GetFieldOption($field, 'formatter'); + + if ( !$formatter ) { + continue; + } + + if ( $formatter == 'kUploadFormatter' || in_array('kUploadFormatter', class_parents($formatter)) ) { + $ret[$field] = array( + 'urls' => $object->GetField($field, 'file_urls'), + 'names' => $object->GetField($field, 'file_names'), + 'sizes' => $object->GetField($field, 'file_sizes'), + ); + } + } + + return $ret; + } + + /** * Sends JSON-encoded response as event result to the browser * * @param kEvent $event @@ -79,6 +115,15 @@ { $params['status'] = 'OK'; $event->CallSubEvent($call_event); + + $form_data = $event->getEventParam('form_data'); + + if ( $form_data !== false ) { + $object = $event->getObject(); + /* @var $object kDBItem */ + + $params['uploader_info'] = $this->getUploaderInfo($object, array_keys($form_data)); + } if ( is_callable($callback) ) { call_user_func($callback, $event); \ No newline at end of file Index: core/kernel/db/db_event_handler.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/kernel/db/db_event_handler.php (revision 15733) +++ core/kernel/db/db_event_handler.php (revision ) @@ -1576,6 +1576,7 @@ list($id, $field_values) = each($items_info); $object->SetFieldsFromHash($field_values, $this->getRequestProtectedFields($field_values)); + $event->setEventParam('form_data', $field_values); $this->customProcessing($event, 'before'); @@ -1629,6 +1630,7 @@ foreach ($items_info as $id => $field_values) { $object->Load($id); $object->SetFieldsFromHash($field_values, $this->getRequestProtectedFields($field_values)); + $event->setEventParam('form_data', $field_values); $this->customProcessing($event, 'before'); if ( $object->Update($id) ) { @@ -2188,7 +2190,7 @@ $field_values = $this->getSubmittedFields($event); $object->SetFieldsFromHash($field_values, $this->getRequestProtectedFields($field_values)); - + $event->setEventParam('form_data', $field_values); $this->customProcessing($event, 'before'); if ( $object->Create() ) { @@ -3432,6 +3434,7 @@ list ($id, $field_values) = each($items_info); $object->Load($id); $object->SetFieldsFromHash($field_values, $this->getRequestProtectedFields($field_values)); + $event->setEventParam('form_data', $field_values); $object->setID($id); $response = Array ('status' => 'OK'); @@ -3449,6 +3452,7 @@ /* @var $ajax_form_helper AjaxFormHelper */ $response['other_errors'] = $ajax_form_helper->getErrorMessages($object); + $response['uploader_info'] = $ajax_form_helper->getUploaderInfo($object, array_keys($field_values)); $event->status = kEvent::erSTOP; // since event's OnBefore... events can change this event status echo json_encode($response); \ No newline at end of file