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.


Advertisements

18 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

  4. You truly made a handful of superb stuff within ur posting, “Publishing a resource from a draft copy (revised – now with TVs) |
    The CMS less traveled”. I will become coming back again
    to ur site soon enough. Thx ,Trey

    Reply

  5. What version of MODx was this created to work on? Just tried this on REVO 2.2.7 and it does not work… published the draft resource does not unpublish the other resource…?

    Reply

  6. The code seems to contain some errors? I have had to re-write it to get it to be accepted in MODx REVO 2.2.7 as a plugin script. Also, when I hit publish, I enter a never ending save loop – e.g. the actions of this plugin seem to fall over? Can you confirm which version of REVO this supports and the code to use?

    Reply

  7. Thank you for utilizing free time in order to compose “Publishing a resource from a draft copy (revised – now with
    TVs) | The CMS less traveled”. Thank you so much for a second time
    ,Cinda

    Reply

  8. You’re not only wasting your employer’s time, but by providing quality information from
    an authority site. So anyone interested in developing
    24 hour fitness optimization for the sites must play well with keywords and that will receive traffic, but not so much competition.
    This means that the visibility of your products.
    This is something experienced webmasters are aware of and
    work towards. Are they 24 hour fitness versed in the capacities listed
    in the first sentence of this article.

    Reply

  9. Piece of writing writing is also a fun, if you
    be acquainted with after that you can write if not
    it is complex to write.

    Reply

  10. Tematyka bliska każdemu, rekomenduję publikację

    Reply

  11. Excellent goods from you, man. I have be aware your stuff
    previous to and you are simply extremely wonderful.
    I really like what you’ve acquired here, certainly like what you are
    saying and the best way wherein you are saying it.
    You’re making it enjoyable and you still care for to keep it wise.
    I cant wait to learn much more from you. That is actually a tremendous site.

    Reply

  12. By these statements, you ought to realize how easy it’s to incur a fatal reaction applying this drug.
    Anyways, should you don’t want opt for these products, there are many alternatives
    available in the marketplace.

    Bontril can be a prescription medicine hence you require a
    doctor’s prescription to purchase Bontril v3 slimming capsules.
    Another benefit is that you will have a very fitter and leaner
    body. homepage

    Reply

  13. Ciekawe wpisy, dobra tematyka

    Reply

  14. Nader odlotowy post, drobiazgowe wpisy polecam wszystkim literaturę

    Reply

  15. Fajne spojrzenie na rzecz, każdy powinien przeczytać także zaznajomić się z przedmiotem.

    Reply

  16. Badawcze spojrzenie na myśl, każdy powinien rozczytać oraz zaznajomić się z motywem.

    Reply

  17. Great.Really looking forward to read more.

    Reply

  18. Posted by masha2 on October 3, 2017 at 9:30 am

    Привет.

    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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: