public static function update(array $item)
{
$db = BackendModel::getContainer()->get('database');
// check if new version is active
if ($item['status'] == 'active') {
// archive all older active versions
$db->update('blog_posts', array('status' => 'archived'), 'id = ? AND status = ?', array($item['id'], $item['status']));
// get the record of the exact item we're editing
$revision = self::getRevision($item['id'], $item['revision_id']);
// assign values
$item['created_on'] = BackendModel::getUTCDate('Y-m-d H:i:s', $revision['created_on']);
$item['num_comments'] = $revision['num_comments'];
// if it used to be a draft that we're now publishing, remove drafts
if ($revision['status'] == 'draft') {
$db->delete('blog_posts', 'id = ? AND status = ?', array($item['id'], $revision['status']));
}
}
// don't want revision id
unset($item['revision_id']);
// how many revisions should we keep
$rowsToKeep = (int) BackendModel::get('fork.settings')->get('Blog', 'max_num_revisions', 20);
// set type of archive
$archiveType = $item['status'] == 'active' ? 'archived' : $item['status'];
// get revision-ids for items to keep
$revisionIdsToKeep = (array) $db->getColumn('SELECT i.revision_id
FROM blog_posts AS i
WHERE i.id = ? AND i.status = ? AND i.language = ?
ORDER BY i.edited_on DESC
LIMIT ?', array($item['id'], $archiveType, BL::getWorkingLanguage(), $rowsToKeep));
// delete other revisions
if (!empty($revisionIdsToKeep)) {
// get meta-ids that will be deleted
$metasIdsToRemove = (array) $db->getColumn('SELECT i.meta_id
FROM blog_posts AS i
WHERE i.id = ? AND revision_id NOT IN (' . implode(', ', $revisionIdsToKeep) . ')', array($item['id']));
// get all the images of the revisions that will NOT be deleted
$imagesToKeep = $db->getColumn('SELECT image FROM blog_posts
WHERE id = ? AND revision_id IN (' . implode(', ', $revisionIdsToKeep) . ')', array($item['id']));
// get the images of the revisions that will be deleted
$imagesOfDeletedRevisions = $db->getColumn('SELECT image FROM blog_posts
WHERE id = ? AND status = ? AND revision_id NOT IN (' . implode(', ', $revisionIdsToKeep) . ')', array($item['id'], $archiveType));
// make sure that an image that will be deleted, is not used by a revision that is not to be deleted
foreach ($imagesOfDeletedRevisions as $imageOfDeletedRevision) {
if (!in_array($imageOfDeletedRevision, $imagesToKeep)) {
BackendModel::deleteThumbnails(FRONTEND_FILES_PATH . '/blog/images', $imageOfDeletedRevision);
}
}
$db->delete('blog_posts', 'id = ? AND status = ? AND revision_id NOT IN (' . implode(', ', $revisionIdsToKeep) . ')', array($item['id'], $archiveType));
if (!empty($metasIdsToRemove)) {
$db->delete('meta', 'id IN (' . implode(', ', $metasIdsToRemove) . ')');
}
}
// insert new version
$item['revision_id'] = BackendModel::getContainer()->get('database')->insert('blog_posts', $item);
// return the new revision id
return $item['revision_id'];
}