The idea is based on te Wordpress shortcode functionality. However, the function of the extension is not subject of my question (For now it's just a practice toy) But what I want to know is how I did building it. E.g. is it according PivotX conventions, are there any important things I missed, are there any tips to do it better? etc.
I know, it's very short and basic, but since I'm at the beginning of a long carreer as PivotX extension writer I thought it's better to start from a decent basis

- Code: Select all
<?php
// - Extension: shortcode
// - Version: 1.0
// - Author: Toine Ermens
// - Email: toine@ermens.net
// - Site: http://www.pivotx.net
// - Description: A snippet to output custom content from the admin panel
// - Date: 2011-08-25
// - Identifier: shortcode
// Set defaults
global $shortcode_config;
$shortcode_config = array(
'default_valueA' => 'Adjust this shortcode (scID=1) in the adminpanel',
'default_valueB' => 'Adjust this shortcode (scID=2) in the adminpanel',
'default_valueC' => 'Adjust this shortcode (scID=3) in the adminpanel',
);
// Register '[[ shortcode ]]' as a smarty tag.
$PIVOTX['template']->register_function('shortcode', 'smarty_shortcode');
// Add the hook for the Admin configurationpanel
$this->addHook(
'configuration_add',
'shortcode',
array("shortcodeAdmin", "Shortcodes")
);
/* ===========================
Function register smartytag
=========================== */
function smarty_shortcode($params, &$smarty) {
global $PIVOTX, $shortcode_config;
$params = cleanParams($params);
// Check parameter
if (isset($params['scid'])) {
$parameter = $params['scid'];
}
else {
$parameter = 1; // scID: 1 is default if no parameter is set
}
// Assign the correct value to the shortcode
switch ($parameter) {
case 1:
$returnvalue = getDefault($PIVOTX['config']->get('shortcode_valueA'), $shortcode_config['default_valueA']);
break;
case 2:
$returnvalue = getDefault($PIVOTX['config']->get('shortcode_valueB'), $shortcode_config['default_valueB']);
break;
case 3:
$returnvalue = getDefault($PIVOTX['config']->get('shortcode_valueC'), $shortcode_config['default_valueC']);
break;
default:
$returnvalue = "<!-- [[ shortcode ]] parameter 'scid' is out of range -->";
}
return $returnvalue;
}
/* ===========================
Function create adminform
=========================== */
function shortcodeAdmin(&$form_html) {
global $PIVOTX, $shortcode_config;
$form = $PIVOTX['extensions']->getAdminForm('shortcode');
$form->add( array(
'type' => 'textarea',
'name' => 'shortcode_valueA',
'label' => "Shortcode (scID) 1",
'error' => 'Error!',
'size' => 20,
'cols' => 70,
'rows' => 5,
'validation' => 'ifany|string|minlen=2|maxlen=4000'
));
$form->add( array(
'type' => 'textarea',
'name' => 'shortcode_valueB',
'label' => "Shortcode (scID) 2",
'error' => 'Error!',
'size' => 20,
'cols' => 70,
'rows' => 5,
'validation' => 'ifany|string|minlen=2|maxlen=4000'
));
$form->add( array(
'type' => 'textarea',
'name' => 'shortcode_valueC',
'label' => "Shortcode (scID) 3",
'error' => 'Error!',
'size' => 20,
'cols' => 70,
'rows' => 5,
'validation' => 'ifany|string|minlen=2|maxlen=4000'
));
$form_html['shortcode'] = $PIVOTX['extensions']->getAdminFormHtml($form, $shortcode_config);
}
?>