Alter the createdon

on 03-Jun-2010 | Comments ( 2 ) Tags: Evolution

To fake a document creation date in MODx there are 2 solution:

  1. use the Doc Manager module (Modules/Doc Manager/Other Properties)
  2. 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:

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

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
 
Zaigham
Posts: 1
Comment
MODxRules Rules!
Reply #2 on : Fri June 04, 2010, 01:29:07
Very clever solution. Thanks for sharing. :)

ps. the dark design was awesome.
cipa
Posts: 1
Comment
Re: Alter the createdon
Reply #1 on : Fri June 04, 2010, 08:54:56
I wanted something lighter but I'm not very good with colors :)

Quick modx Evolution Tags

  • cached [[snippet]] or uncached [!snippet!]
  • {{chunk}}
  • [+placeholder+]
  • [*resourceField/TV*]
  • [^timing^]
  • [~link~]

Quick modx Revolution Tags

  • [[snippet]]
  • [[$chunk]]
  • [[+placeholder]]
  • [[*resourceField/TV]]
  • [[~link]]
  • [^timing^]
  • [[++systemSetting]]
  • [[%languageStringKey]]
  • all tags can be called un-cached like: [[! snippet]]

Timing Tags (Evo and Revo)

  • [^qt^] - Query time
  • [^q^] - Query count
  • [^p^] - Parse time
  • [^t^] - Total time
  • [^s^] - Source
© modxRULES! 2009-2014