Index: branches/5.3.x/core/kernel/db/db_tag_processor.php =================================================================== diff -u -N -r16519 -r16600 --- branches/5.3.x/core/kernel/db/db_tag_processor.php (.../db_tag_processor.php) (revision 16519) +++ branches/5.3.x/core/kernel/db/db_tag_processor.php (.../db_tag_processor.php) (revision 16600) @@ -1,6 +1,6 @@ Application->Phrase($button_title, false, true); } + if ( !isset($params['pass']) ) { + $params['pass'] = 'm,' . $item_prefix; + } + + $edit_prefix = $item_prefix; + list($parent_prefix, $parent_id) = $this->getParentPrefixAndId($object); + + if ( $parent_prefix !== false ) { + $edit_prefix = $parent_prefix; + $params[$parent_prefix . '_id'] = $parent_id; + $params['pass'] = $parent_prefix . ',' . $params['pass']; + } + $icon_url = $this->Application->BaseURL() . 'core/admin_templates/img/top_frame/icons/' . $button_icon; - $button_onclick = '$form_name = ' . json_encode($form_name) . '; std_edit_item(' . json_encode($item_prefix) . ', ' . json_encode($template) . '); return false;'; + if ( !isset($params['temp_mode']) || (isset($params['temp_mode']) && $params['temp_mode']) ) { + $edit_function = 'std_edit_item'; + } + else { + $edit_function = 'std_edit_temp_item'; + } + + $button_onclick = '$form_name = ' . json_encode($form_name) . '; ' . $edit_function . '(' . json_encode($edit_prefix) . ', ' . json_encode($template) . '); return false;'; + $button_code = ''; - if ( !isset($params['pass']) ) { - $params['pass'] = 'm,' . $item_prefix; - } - $params['m_opener'] = 'd'; $params[$item_prefix . '_id'] = $object->GetID(); - - if ( !isset($params['temp_mode']) || (isset($params['temp_mode']) && $params['temp_mode']) ) { - $params[$item_prefix . '_mode'] = 't'; - $params[$item_prefix . '_event'] = 'OnEdit'; - } - $params['front'] = 1; // to make opener stack work properly $params['__NO_REWRITE__'] = 1; // since admin link @@ -2717,6 +2728,54 @@ } /** + * Returns parent record ID. + * + * @param kDBBase $object Object. + * + * @return array + */ + protected function getParentPrefixAndId(kDBBase $object) + { + static $parent_mapping = array(); + + $unit_config = $object->getUnitConfig(); + $parent_prefix = $unit_config->getParentPrefix(); + + if ( $parent_prefix === false ) { + return array(false, null); + } + + $foreign_key = $unit_config->getForeignKey($parent_prefix); + $parent_table_key = $unit_config->getParentTableKey($parent_prefix); + + $parent_unit_config = $this->Application->getUnitConfig($parent_prefix); + $parent_id_field = $parent_unit_config->getIDField(); + $parent_id = $object->GetDBField($foreign_key); + + if ( $parent_table_key != $parent_id_field ) { + $cache_key = $object->getPrefixSpecial(); + + if ( !isset($parent_mapping[$cache_key]) ) { + $foreign_key_values = array_unique($object->GetCol($foreign_key)); + + $sql = 'SELECT ' . $parent_id_field . ', ' . $parent_table_key . ' + FROM ' . $parent_unit_config->getTableName() . ' + WHERE ' . $parent_table_key . ' IN (' . implode(',', $foreign_key_values) . ')'; + $parent_mapping[$cache_key] = $this->Conn->GetCol($sql, $parent_table_key); + } + + // Parent record was deleted, but child record is still referencing it. + if ( !isset($parent_mapping[$cache_key][$parent_id]) ) { + return array(false, null); + } + + $parent_id = $parent_mapping[$cache_key][$parent_id]; + } + + return array($parent_prefix, $parent_id); + } + + /** * Calls OnNew event from template, when no other event submitted * * @param Array $params