Plugins

Version Numbers for your Pages

gourmet fare

Resource Versioning

Keep track of the updates to your site pages.

romainfalle asked...

I have this small plugin that is fired on the "onDocFormSave" event to increment a TV value each time a resource is saved, in order to make a version system. If the resource page is not reloaded, the TV value is not incremented. I don't know why, but when we save the resource for the second time, it ALWAYS get the old tv value. Is there another way ?

The Solution:

Bruno17 says...

Couldn't you save the values somewhere else, for example in a custom-table? You could read that value on every OnDocFormSave, increment it, save the incremented value back to your custom-table and to your TV at the same time.

This way the value would really increment on each save.

romainfalle says...

Here is my complete procedure to achieve my version system:

Step 1: Create a new table in the MODX database called "modx_site_content_version" with three columns "id", "contentid" and "version" (set "id" column as INDEX):

CREATE TABLE `modx_site_content_version` (
  `id` int(11) NOT NULL,
  `contentid` int(11) NOT NULL,
  `version` int(11) NOT NULL
) DEFAULT CHARSET=utf8;
ALTER TABLE `modx_site_content_version` ADD PRIMARY KEY(`id`);
ALTER TABLE `modx_site_content_version` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `modx_site_content_version` ADD INDEX(`id`);

Step 2: Create a new snippet called "createXpdoClasses" and fill it with the code available at Bob's Guides, thanks to Bob!

Step 3: Create a new resource called "createXpdoClasses" with no richtext editor and put this code in it:

[[!CreateXpdoClasses? &myPackage=`Version` &myPrefix=`modx_site_content_`]]

-> View the resource one time in your browser.

-> Delete your "createXpdoClasses" resources and snippet. We do not need them anymore.

Step 4: Create a new plugin called "version" on the "onDocFormSave" event. This will increment the version.


addPackage('Version', $path . 'model/','modx_site_content_');
$currentResource= $modx->getObject('Version', array('contentid' => $id));
     
if (empty($currentResource)) {
    $newResource = $modx->newObject('Version', array(
        'contentid' => $id,
        'version' => 1
    ));
    $newResource->save();
}
else {
    $currentResource->set('version', $currentResource->get('version') + 1);
    $currentResource->save();
}
 
unset($path, $result, $currentResource, $newResource, $id);

Step 5: To get the version on templates and chunks, we will use this little snippet called "getVersion":

<?php
$result = $modx->query("SELECT version FROM modx_site_content_version WHERE contentid=$id");
$row = $result->fetch(PDO::FETCH_ASSOC);
return $row['version'];
unset($result, $row);

We use it this way in templates:

[[getVersion?id=`[[*id]]`]]

Step 6: To generate the version on all existing resources, simply create and run a new snippet "versionAllResources":

<?php
$resources = $modx->getCollection('modResource', array('class_key'=>'modDocument'));
  
foreach ($resources as $resource) {
    $path = MODX_CORE_PATH . 'components/version/';
    $result = $modx->addPackage('Version', $path . 'model/','modx_site_content_');
    $currentResource= $modx->getObject('Version', array('contentid' => $resource->get('id')));
     
    if (empty($currentResource)) {
        $newResource = $modx->newObject('Version', array(
            'contentid' => $resource->get('id'),
            'version' => 1
        ));
        $newResource->save();
    }
    else {
        $currentResource->set('version', $currentResource->get('version') + 1);
        $currentResource->save();
    }
}

What's the Story?

There are times when a custom database table is the best way to store custom data. This is one of those times, and with BobRay's handy helper function to build our class for us, we can do all kind of interesting things, like this simple method for versioning resources.