function onsave($do_preview = false) { $id = $this->id; $data = $this->_getposteddata(); if ($this->draft) { $success = draft_save($data, $id, true); $this->smarty->assign('success', $success ? 1 : -1); } else { $success = entry_save($data, $id); $this->smarty->assign('success', is_numeric($success) ? $success : 1); } // if ($success) sess_remove('entry'); if ($do_preview) { $this->_makePreview($data); } if ($success < 0) { $this->main(); return PANEL_NOREDIRECT; } return 1; }
/** * * @param array entry contents * @param string|null entry id, null if can be deducted from the date field of $entry; * defaults to null * * @param bool updates entry index; defaults to true * * * @return integer -1 failure while storing preliminar draft, abort. Index not touched. * -2 index updated succesfully, but draft doesn't exist anymore * (should never happen!) OR * failure while trying to move draft to entry path, draft does not exist anymore * index not touched * -3 error while moving draft still exists, index written succesfully but rolled back * -4 failure while saving to index, aborted (draft still exists) * * */ function entry_save($entry, $id = null, $update_index = true) { // PHASE 1 : prepare entry if (!$id) { if (!@$entry['date']) { $entry['date'] = date_time(); } $id = bdb_idfromtime(BDB_ENTRY, $entry['date']); } // PHASE 2 : Store // secure data as DRAFT // (entry is also implicitly entry_prepare()'d here) $ret = draft_save($entry, $id); do_action('publish_post', $id, $entry); if ($ret === false) { return -1; // FAILURE: ABORT } // PHASE 3 : Update index $delete_cats = array(); $all_cats = @$entry['categories']; $update_title = true; if ($old_entry = entry_parse($id)) { if ($all_cats) { $delete_cats = array_diff($old_entry['categories'], $all_cats); } $all_cats = $all_cats ? array_merge($all_cats, $old_entry['categories']) : $old_entry['categories']; $update_title = $entry['subject'] != $old_entry['subject']; } /* echo 'old'; print_r($old_entry['categories']); echo 'new'; print_r($entry['categories']); echo 'del'; print_r($delete_cats); echo 'all'; print_r($all_cats); */ $INDEX =& entry_init(); $ok = $update_index ? $INDEX->add($id, $entry, $delete_cats, $update_title) : true; // PHASE 4 : index updated; let's move back the entry if ($ok) { $entryd = entry_dir($id, true); $entryf = $entryd . $id . EXT; $draftf = draft_exists($id); if ($draftf === false) { // this should never happen! if ($update_index) { $INDEX->delete($id, $all_cats); } return -2; } fs_delete($entryf); fs_mkdir($entryd); $ret = rename($draftf, $entryf); if (!$ret) { if (draft_exists($id)) { // rollback changes in the index // (keep the draft file) if ($update_index) { $INDEX->delete($id, $all_cats); } return -3; } else { return -2; } } else { // SUCCESS : delete draft, move comments along draft_to_entry($id); return $id; } } return -4; }