Sure thing, I can show you my strategy. Some thing’s could be improved in my (limited) opinion.
I created a new category where I placed all the opening times. I then looked at the {prefix}_categories table to see what id my "Opening Times" category had.
I suppose I could add that as a parameter to the snippet call if I need it again then look in this table for WHERE id = $parameterPassed which could save somebody without knowledge of phpMyAdmin and such some time...but for me I just defined it in the variable $catNo below.
The fields I’m selecting from the site_htmlsnippets and the table itself are also defined here:
<?php
//forgetting this could case headaches
global $modx;
//handy variables for the sql call$fields="id,name,description,category,snippet";
$tblsc = $modx->getFullTableName("site_htmlsnippets");
$catNo=1;
Using the variables above I then grabbed the data I needed from a database and stuck it into an array called $resourceArray
//get our fields from the db and save them into a multidimensional array
$sql = 'SELECT '.$fields.' FROM '.$tblsc.' WHERE category = '.$catNo.';';
$result = $modx->dbQuery($sql);
$resourceArray = array();
for($i=0;$i<@$modx->recordCount($result);$i++) {
array_push($resourceArray,@$modx->fetchRow($result));
}
I then needed to brush the dust of my JavaScript: The definitive guide & Ajax in Action to create the JS function.
Using a foreach array I populate a JavaScript array so that it looks like chunkSnippets[0] = "This is the HTML from the snippet field"
I also had to use addslashes so the user can simply craet HTML chunks using the RTE as is, without having to worry. It took me a long time to find addslashes. I was using HTMLentities and SpecialChars and I don’t know what. I kept getting an unknown Object JS error before that ...I knew why (the quotes in the HTML chunk) and that solved it, to be on the safe side I got rid of all newlines tabs and whatever \r does too
foreach ($resourceArray as $arrayNumber => $arrayMulti){
// addslashes...spent more time looking for this than the script itself ;-)
$t = addslashes($arrayMulti['snippet']);
//get rid of nasty newlines and that
$s = str_replace(array("\n", "\r", "\t"), '', $t);
//create the array and save each line to our js_output variable
$js_output.= "chunkSnippets[".$arrayNumber."] = \"".$s."\";\n";
}
After browsing the web undecided whether to use the DOM, Ajax or simply innerHTML I went with innerHTML. The speed difference is minimal, and all the "everyday" browsers support it. If I used an AJAX library (like htmlhttprequest.js) we would be talking 7KB so I decided to load the chunks in straight away. the getElementById has been hard-coded, but yet again could easily be replaced by another parameter. So now I add
$js_output.= "document.getElementById('rename').innerHTML=chunkSnippets[currText]";
to do just that. Then using the nifty setPlaceHolder I did
$modx->setPlaceholder('JSfunction', $js_output);
to create a [+JSfunction+] placeholder so that I can place the JS at the top of my template instead of in the content or somewhere else. A nifty little thing indeed.
It’s all downhill from here. Create a simple form that holds a <select> with an onChange handler, same principle as before, loop through with
foreach to create the option tags. This is somethign TobyL showed me a couple of weeks back...and it saves lives
foreach ($resourceArray as $arrayNumber => $arrayMulti){
$form_output .= "<option value='".$arrayNumber."' > ".$arrayMulti['description']."</option> \n";
}
Close the select tag and create another placeholder for the jumpMenu and all is good.
$modx->setPlaceholder('selectTag', $form_output);
Disclaimer: I’m not much of a programmer, and aren’t sure of any potential security threats, unseen overheads or anything like that...but it works
and that get’s the thumbs up from me. I’ll probably update the code to use those parameters I mentioned above.
Any comments?
Cheers,
Taff