Publishing a resource from a draft copy (revised – now with TVs)

Lots of feedback on yesterday’s post, much of it to the effect of “call me back when you’ve got it working with Template Variables!”

Alright then.

In addition to syncing the TVs of the Draft resource with the Production resource, I’ve slightly changed the lines of code that dictate which fields NOT to update.  Yesterday I knew that the ‘alias’ field was probably not something we want the draft resource to change (both resources would then have the same alias, which is not allowed).  Today I realized the same thing is true (for me, at least) of the “Parent Resource” field.  Changing either Alias or Parent Resource would alter the URL of the production resource (when friendly URLs are used).

Here’s the newest-latest code for my plugin (changed stuff is bolded):

<?php
$pagetitle = $resource->get(‘pagetitle’);
//if the “draft” flag is in the title AND the user “published” the draft
if (
(preg_match(‘/\(draft-(?P<id>\d+)-\)/’, $pagetitle, $results ))&&
($resource->get(‘published’))
) {
// the ID of the resource  to publish to
$prodID = $results['id'];
// if $prodID represents a real & published resource
if ($prodResource = $modx->getObject(‘modResource’,
array(‘id’=>$prodID,’published’=>1))) {
$newVals = $resource->toArray();
$restrictedFields = array(‘alias’=>”,’parent’=>”); //fields not to update
$filteredVals = array_diff_key($newVals,$restrictedFields);

$prodResource->fromArray($filteredVals);

//take out the “draft” token before setting pagetitle
$prodResource->set(‘pagetitle’, str_replace($results[0], ”, $pagetitle));
$prodResource->save();

$newTVs = $resource->getMany(‘TemplateVars’);
foreach ($newTVs as $TV) {
$newval = $TV->getValue($resource->get(‘id’));
$TV->setValue($prodResource->get(‘id’), $newval);
$TV->save();

}

}
//unpublish the draft resource
$resource->set(‘published’, 0);
$resource->save();
}
return;
?>

You can put any fields that you DON’T want updated into the $restrictedFields array(in my code they are array keys… if you don’t like that feel free to rewrite the PHP to your liking).

Try this out and let me know how it works.  I fully expect there to be glitches with TVs and in other places (it can’t be this easy, can it?)

I’ll probably continue to use this blog space as an RFC forum for this plugin, until such a time as it seems robust enough to actually submit as an add-on package.  Eventually I’d like to automate the “create a draft copy” process, add some permissions checking, and probably some other stuff I’m forgetting or haven’t thought of yet.  Meanwhile, the more of you that try it out, the better (hopefully) I’ll be able to make it.


Advertisement

3 responses to this post.

  1. [...] « Its all happening… Publishing a resource from a draft copy (revised – now with TVs) [...]

    Reply

  2. [...] Publishing a resource from a draft copy (revised – now with TVs) by James Rotering [...]

    Reply

  3. Posted by okben on October 26, 2010 at 8:49 am

    Excellent stuff, should be on the modx site.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.