Back to Versioning class

Method store

public static bool
store
(mixed $typeAlias, mixed $id, mixed $data, mixed $note = '')
Method to save a version snapshot to the content history table.
Parameters
  • string $typeAlias Typealias of the content type
  • int $id ID of the content item
  • mixed $data Array or object of data that can be en- and decoded into JSON
  • string $note Note for the version to store
Returns
  • bool True on success, otherwise false.
Since
  • 4.0.0
Class: Versioning
Project: Joomla

Method store - Source code

/**
 * Method to save a version snapshot to the content history table.
 *
 * @param   string   $typeAlias  Typealias of the content type
 * @param   integer  $id         ID of the content item
 * @param   mixed    $data       Array or object of data that can be
 *                               en- and decoded into JSON
 * @param   string   $note       Note for the version to store
 *
 * @return  boolean  True on success, otherwise false.
 *
 * @since   4.0.0
 */
public static function store($typeAlias, $id, $data, $note = '')
{
    $typeTable = Table::getInstance('Contenttype', 'JTable');
    $typeTable->load(array('type_alias' => $typeAlias));
    $historyTable = Table::getInstance('Contenthistory', 'JTable');
    $historyTable->item_id = $typeAlias . '.' . $id;
    $aliasParts = explode('.', $typeAlias);
    // Don't store unless we have a non-zero item id
    if (!$historyTable->item_id) {
        return true;
    }
    // We should allow workflow items interact with the versioning
    $component = Factory::getApplication()->bootComponent($aliasParts[0]);
    if ($component instanceof WorkflowServiceInterface && $component->isWorkflowActive($typeAlias)) {
        PluginHelper::importPlugin('workflow');
        // Pre-processing by observers
        $event = AbstractEvent::create('onContentVersioningPrepareTable', ['subject' => $historyTable, 'extension' => $typeAlias]);
        Factory::getApplication()->getDispatcher()->dispatch('onContentVersioningPrepareTable', $event);
    }
    $historyTable->version_data = json_encode($data);
    $historyTable->version_note = $note;
    // Don't save if hash already exists and same version note
    $historyTable->sha1_hash = $historyTable->getSha1($data, $typeTable);
    if ($historyRow = $historyTable->getHashMatch()) {
        if (!$note || $historyRow->version_note === $note) {
            return true;
        } else {
            // Update existing row to set version note
            $historyTable->version_id = $historyRow->version_id;
        }
    }
    $result = $historyTable->store();
    // Load history_limit config from extension.
    $context = $aliasParts[1] ?? '';
    $maxVersionsContext = ComponentHelper::getParams($aliasParts[0])->get('history_limit_' . $context, 0);
    if ($maxVersionsContext) {
        $historyTable->deleteOldVersions($maxVersionsContext);
    } elseif ($maxVersions = ComponentHelper::getParams($aliasParts[0])->get('history_limit', 0)) {
        $historyTable->deleteOldVersions($maxVersions);
    }
    return $result;
}