Page MenuHomeIn-Portal Phabricator

D253.id614.diff
No OneTemporary

File Metadata

Created
Wed, Feb 26, 9:03 AM

D253.id614.diff

Index: core/kernel/db/db_event_handler.php
===================================================================
--- core/kernel/db/db_event_handler.php
+++ core/kernel/db/db_event_handler.php
@@ -1601,6 +1601,10 @@
$this->_update($event);
$event->SetRedirectParam('opener', 'u');
+
+ if ( $event->status == kEvent::erSUCCESS ) {
+ $this->saveChangesToLiveTable($event->Prefix);
+ }
}
/**
@@ -1638,6 +1642,26 @@
}
/**
+ * Automatically saves data to live table after sub-item was updated in Content Mode.
+ *
+ * @param string $prefix Prefix.
+ *
+ * @return void
+ */
+ protected function saveChangesToLiveTable($prefix)
+ {
+ $parent_prefix = $this->Application->getUnitOption($prefix, 'ParentPrefix');
+
+ if ( $parent_prefix === false ) {
+ return;
+ }
+
+ if ( $this->Application->GetVar('admin') && $this->Application->IsTempMode($parent_prefix) ) {
+ $this->Application->HandleEvent(new kEvent($parent_prefix . ':OnSave'));
+ }
+ }
+
+ /**
* Delete's kDBItem object
*
* @param kEvent $event
Index: core/kernel/db/db_tag_processor.php
===================================================================
--- core/kernel/db/db_tag_processor.php
+++ core/kernel/db/db_tag_processor.php
@@ -2578,8 +2578,29 @@
$button_title = $this->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) . ');';
+
+ 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) . ');';
$button_code = '<button
style="background-image: url(' . $icon_url . ');"
@@ -2588,18 +2609,8 @@
kUtil::escape($button_title, kUtil::ESCAPE_HTML) . '
</button>';
- 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
@@ -2622,6 +2633,55 @@
}
/**
+ * Returns parent record ID.
+ *
+ * @param kDBBase $object Object.
+ *
+ * @return array
+ */
+ protected function getParentPrefixAndId(kDBBase $object)
+ {
+ static $parent_mapping = array();
+
+ $parent_prefix = $this->Application->getUnitOption($object->Prefix, 'ParentPrefix');
+
+ if ( $parent_prefix === false ) {
+ return array(false, null);
+ }
+
+ $foreign_key = $this->Application->getUnitOption($object->Prefix, 'ForeignKey');
+ $foreign_key = is_array($foreign_key) ? $foreign_key[$parent_prefix] : $foreign_key;
+
+ $parent_table_key = $this->Application->getUnitOption($object->Prefix, 'ParentTableKey');
+ $parent_table_key = is_array($parent_table_key) ? $parent_table_key[$parent_prefix] : $parent_table_key;
+
+ $parent_id_field = $this->Application->getUnitOption($parent_prefix, 'IDField');
+ $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 ' . $this->Application->getUnitOption($parent_prefix, 'TableName') . '
+ 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
Index: core/units/content/content_tp.php
===================================================================
--- core/units/content/content_tp.php
+++ core/units/content/content_tp.php
@@ -29,11 +29,6 @@
return '';
}
- $object = $this->getObject($params);
- /* @var $object kDBItem */
-
- $params['pass'] = 'm,c,content';
- $params['c_id'] = $object->GetDBField('PageId');
$params['admin'] = 1;
$params['temp_mode'] = 0;
@@ -50,4 +45,5 @@
return parent::AdminEditButton($params);
}
-}
\ No newline at end of file
+
+}

Event Timeline