Index: kernel/db/cat_event_handler.php =================================================================== --- kernel/db/cat_event_handler.php (revision 14858) +++ kernel/db/cat_event_handler.php (working copy) @@ -2464,7 +2464,7 @@ $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array($original_id), null, null, null, true); $object->Load($cloned_ids[0]); - $object->SetFieldsFromHash($field_values); + $object->SetFieldsFromHash($this->fixClonedUploads($field_values, $event)); // 1a. delete record from CategoryItems (about cloned item) that was automatically created during call of Create method of kCatDBItem $ci_table = $this->Application->getUnitOption('ci', 'TableName'); @@ -2511,6 +2511,69 @@ } /** + * Patches submit data for it to contain cloned uploaded filenames instead of original ones + * + * @param Array $field_values + * @param kEvent $event + * @return Array + */ + function fixClonedUploads($field_values, &$event) + { + $object =& $event->getObject(); + /* @var $object kCatDBItem */ + + foreach ($field_values as $field => $value) { + if ( !is_array($value) ) { + continue; + } + + $old_file = $value['upload']; + $new_file = $object->GetDBField($field); + + if ( $new_file == $old_file ) { + continue; + } + + if ( isset($value['tmp_deleted']) && $value['tmp_deleted'] ) { + // replace original deleted image with cloned deleted image + $old_files = explode('|', $old_file); + $deleted = explode('|', $value['tmp_deleted']); + + $var_name = $this->_getPendingActionVariableName($event); + $schedule = $this->Application->RecallVar($var_name); + $schedule = $schedule ? unserialize($schedule) : Array (); + + foreach ($old_files as $old_file) { + $deleted_pos = array_search($old_file, $deleted); + + if ( $deleted_pos === false ) { + continue; + } + + $deleted[$deleted_pos] = $new_file; + + // update file delete queue as well + foreach ($schedule as $index => $data) { + if ( $data['action'] == 'delete' ) { + if ( preg_match('/(.*)' . preg_quote($old_file, '/') . '$/', $data['file'], $regs) ) { + $schedule[$index]['file'] = $regs[1] . $new_file; + } + } + } + } + + $field_values[$field]['tmp_deleted'] = implode('|', $deleted); + $this->Application->StoreVar($var_name, serialize($schedule)); + } + + // replace original image with cloned image + $field_values[$field]['upload'] = $new_file; + } + + return $field_values; + } + + /** * Sets next template to one required for front-end after adding/modifying item * * @param kEvent $event