Based on the help I received from @dev_cw and @sottwell, I have built out a modified and genericized version of my unpubdate plugin.
The Automatic Unpublish plugin will automatically insert a value for unpub_date based on a document variable date value or a TV with a date value.
I use it in an events list where there is an event coming up so I set an event date (TV called event_date). This allows the user to set the event date and once the event has happened the document will automatically unpublish. This means the user only needs to set the event date and not worry about another field setting.
The plugin code has expanded and I’ve added some parameters to it via the config.
Here’s the code:
<?php //<-Delete the php open tag
/*
* Automatic Unpublish Plugin
*
* Sets unpub_date using another date field as reference.
*
* Written By Jay Gilmore - June 6, 2009
* With assistance of Susan Otwell (sottwell) and Shane Sponagle (dev_cw)
*
* Version 1.0.1-alpha
*
* Events: OnDocFormSave
*
*/
if(!$fieldName){
return;
}
if (!$fieldType){
$fieldType = 'Document Variable';
}
if($fieldType == 'Template Variable'){
$fieldPrefix = 'tv';
}
else{
$fieldPrefix = '';
}
if(!$offsetQ){
$offsetQ = '1';
}
if(!$offsetU){
$offsetU = 'Day';
}
if($offsetQ){
$offset = '+'.$offsetQ . ' ' . $offsetU;
}
else {
$offset = $offsetU;
}
$postfield = $fieldPrefix . $fieldName;
if($_POST[$postfield] ){
$e = &$modx->Event;
switch ($e->name) {
case "OnDocFormSave":
$ud = strtotime($_POST[$postfield]. $offset);
$unpub_date = $ud;
$table = $modx->getFullTableName( 'site_content' );
$fields = array('unpub_date' => $unpub_date);
// Check to see if there is an unpub_date set and skip the update if it is.
if($_POST['unpub_date'] != $ud){
// Update the db with the field value
$modx->db->update( $fields, $table, 'id = "'.$id.'"' );
}
break;
default :
return;
break;
}
}
Config:
&fieldName=Field Name;text; &fieldType=Field Type;list;Template Variable, Document Variable; &offsetQ=Offset Quantity;text; &offsetU=Offset Unit;list;Day, Week, Month, Year;
Instructions:
1. Copy Plugin Code to a New Plugin
2. Copy Config code to the plugin config field.
3. Set Config paramaters.
4. Save.
I just want people to try it out before I post it as I think there may be some fixes. Please let me know if you need help or to request a change.
Cheers,
Jay