Index: branches/5.2.x/core/units/content/content_eh.php =================================================================== diff -u -N -r14628 -r14856 --- branches/5.2.x/core/units/content/content_eh.php (.../content_eh.php) (revision 14628) +++ branches/5.2.x/core/units/content/content_eh.php (.../content_eh.php) (revision 14856) @@ -1,6 +1,6 @@ finalizePermissionCheck($event, $perm_status); } + + /** + * Saves changes to a content block (+ creates draft if missing) + * + * @param kEvent $event + */ + function OnSaveContentBlock(&$event) + { + if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + $event->status = erFAIL; + return ; + } + + if ( !$this->saveContentBlock($event, false) ) { + $event->status = erFAIL; + } + + $event->SetRedirectParam('opener', 'u'); + } + + /** + * Performs auto-save of current content block (will create draft too) + * + * @param kEvent $event + */ + function OnAutoSave(&$event) + { + $event->status = erSTOP; + + if ( $this->Application->GetVar('ajax') != 'yes' ) { + return ; + } + + echo $this->saveContentBlock($event, true); + } + + /** + * Saves content block + * + * @param kEvent $event + * @param bool $is_draft + * @return string + */ + function saveContentBlock(&$event, $is_draft) + { + $object =& $event->getObject( Array('skip_autoload' => true) ); + /* @var $object kDBItem */ + + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); + if ( !$items_info ) { + return ; + } + + list ($object, $revision) = $this->getContentBlockAndRevision($event); + + list (, $field_values) = each($items_info); + $object->SetFieldsFromHash($field_values); + $updated = $object->Update(); + + if ( $updated ) { + $revision->SetDBField('AutoSavedOn_date', adodb_mktime()); + $revision->SetDBField('AutoSavedOn_time', adodb_mktime()); + $revision->Update(); + } + + if ( $is_draft ) { + if ( $updated ) { + $page_helper =& $this->Application->recallObject('PageHelper'); + /* @var $page_helper PageHelper */ + + return $revision->GetField('AutoSavedOn') . ' (' . $page_helper->getAgoTime( $revision->GetDBField('AutoSavedOn') ) . ')'; + } + } + else { + return $updated; + } + + return ''; + } + + /** + * Returns last autosave time + * + * @param kEvent $event + */ + function OnGetAutoSaveTime(&$event) + { + $event->status = erSTOP; + + if ( $this->Application->GetVar('ajax') != 'yes' ) { + return ; + } + + list ($object, $revision) = $this->getContentBlockAndRevision($event); + + $page_helper =& $this->Application->recallObject('PageHelper'); + /* @var $page_helper PageHelper */ + + $time = $revision->GetField('AutoSavedOn'); + + if ( $time ) { + echo $time . ' (' . $page_helper->getAgoTime( $revision->GetDBField('AutoSavedOn') ) . ')'; + } + } + + /** + * Loads content block from given revision + * + * @param kDBItem $object + * @param kDBItem $revision + */ + function loadFromRevision(&$object, &$revision) + { + $load_keys = Array ( + 'PageId' => $object->GetDBField('PageId'), + 'ContentNum' => $object->GetDBField('ContentNum'), + 'RevisionId' => $revision->GetID(), + ); + + $object->Load($load_keys); + } + + function getContentBlockAndRevision(&$event) + { + $object =& $event->getObject( Array('skip_autoload' => true) ); + /* @var $object kDBItem */ + + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); + if ( !$items_info ) { + return ; + } + + list ($id, $field_values) = each($items_info); + $object->Load($id); + + $revision =& $this->Application->recallObject('page-revision', null, Array ('skip_autoload' => true)); + /* @var $revision kDBItem */ + + $revision->Load( $object->GetDBField('RevisionId') ); + + if ( !$revision->GetDBField('IsDraft') ) { + // editing live revision of a page's content block -> get draft for current user and page + $load_keys = Array ( + 'PageId' => $revision->GetDBField('PageId'), + 'IsDraft' => 1, + 'CreatedById' => $this->Application->RecallVar('user_id'), + ); + + $revision->Load($load_keys); + + if ( $revision->isLoaded() ) { + // draft found -> use draft's content block version + $this->loadFromRevision($object, $revision); + } + else { + // draft not found -> create new + $revision->SetDBFieldsFromHash($load_keys); + $revision->SetDBField('FromRevisionId', $object->GetDBField('RevisionId')); + + if ( $revision->Create() ) { + $this->loadFromRevision($object, $revision); + } + } + } + + return Array (&$object, &$revision); + } } \ No newline at end of file