Alter the createdon
To fake a document creation date in MODx there are 2 solution:
- use the Doc Manager module (Modules/Doc Manager/Other Properties)
- create a date TV and alter the createdon date on save or edit
The second solution is harder to implement but simpler for the managers/editors. Basically you need to create a date TV and alter the createdon field in the database when a document is saved/created.
Create the date TV
Create a new TV and and set it up like in the image. The name has no importance so enter whatever makes sense to you.

Do not forget to assign the TV to one or more templates.
Also make sure you write down:
- the id of the newly created TV
- the id(s) of the template(s)
Set up the plugin
Create a new plugin called "Alter createdon date" and paste the following code:
//Make sure this plugin appears before Quick Manager+ plugin
//Make sure the template has a CreatedDate TV assigned and the ID is properly configured in the Configuration tab
$e = &$modx->Event;
if ($e->name == "OnDocFormSave"){
$acdTemplate = $_POST['template'];
$acdRawDate = $_POST['tv'. trim($createdOnDateTvId)];
$aTemplateIds = explode(',', $templateIds);
if(in_array($acdTemplate, $aTemplateIds) && !empty($acdRawDate)){
$acdId = ($_POST['id'])? $_POST['id'] : $e->params['id']; //set id to updated doc or new doc id
$acdCreatedon = convert_datetime($acdRawDate);
$sql = 'UPDATE '.$modx->db->config['table_prefix'].'site_content SET createdon = '.$acdCreatedon.' WHERE id = ' . $acdId;
$rs = $modx->db->query($sql);
if (!$rs) {
$modx->logEvent(5, 3, 'Could not alter the createdon date', 'Alter createdon date');
}
}
}
// from DATE to UNIX timestamp
function convert_datetime($str) {
list($date, $time) = explode(' ', $str);
list($day, $month, $year) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
Configuration
Click on the Configuration tab and paste the following code
&templateIds=Template IDs (comma separated);text; &createdOnDateTvId=Created On Date TV Id;text;
Make sure you click outside the box and update the 2 fields with correct ids.
System Events
Click on the System Events tab and check OnDocFormSave and save the plugin.
Test it
Open a document, alter the date TV and your document will have a new created on date
I use the above with the following Ditto call
[[Ditto? &depth=`1` &tpl=`ArticleTpl` &parents=`2,5,8,37` &total=`10` &sortBy=`createdon` &dateSource=`createdon` &dateFormat=`%d-%b-%Y`]]
Any other fields you would like to alter using a similar solution?
Write a comment
Posts: 1
Reply #2 on : Fri June 04, 2010, 02:29:07
Posts: 1
Reply #1 on : Fri June 04, 2010, 09:54:56