Playing with extensions: Feedback requested

Discuss PivotX 2.0.x extensions here, and view extensions that are available for download.

Playing with extensions: Feedback requested

Postby Gooly » Thu Aug 25, 2011 11:17 am

I'm trying to learn more about the wonderful world of PivotX extensions. The last few months I experimented a bit (with mixed results) and this morning I build a very basic, super simple extension which basically does not much more than outputting the content of a text area in the adminpanel.

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 :-D
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);
}

?>
That's the trouble with reality,
it's taken far too seriously
User avatar
Gooly
 
Posts: 77
Joined: Thu Nov 19, 2009 5:52 pm
Location: Lat 52° 4' 11.4874" Lon 4° 17' 28.001"

Re: Playing with extensions: Feedback requested

Postby hansfn » Thu Aug 25, 2011 8:34 pm

Looks fine to me - the general structure is correct for PivotX 2.x extensions.

There is one minor issue related to our coding guidelines: The if-else statement should be written as

Code: Select all
if (some condition) {
    some code;
} else {
    some other code;
}

Another minor detail: You can save some code by using default_value1 in stead of default_valueA (and so on). Then you can replace the switch statement with just

Code: Select all
$shortcode_key = 'shortcode_value' . $parameter;
$returnvalue = getDefault($PIVOTX['config']->get($shortcode_key), $shortcode_config[$shortcode_key]);

(The check for valid scID could be moved into the same place as the isset($params['scid']) block.)
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Playing with extensions: Feedback requested

Postby Gooly » Fri Aug 26, 2011 8:31 am

Thanks for the reply.
I'll use the construction for 'if-else' from now on.
Also a big tnx for the coding suggestion. It will not only save some code in the smarty_shortcode function, but I can also use it to parse the (admin)form textareas in a loop. And on top of that I can make the script more flexible this way, because it creates the opportunity to set the amount of available shortcodes in one single variable in top of the script.

I also have another extension question, but I'll open another topic for that one, since it's a complete different question than teh one from this topic.

[edit: to complete]
Code: Select all
<?php
// - Extension: shortcode
// - Version: 0.1
// - 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_quant;
$shortcode_quant = 4;

for ($i=1; $i<=$shortcode_quant; $i++)
{
$shortcode_config['default_value'.$i] = 'Adjust this shortcode (scID='. $i .') 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, $shortcode_quant;
   $params = cleanParams($params);

   // Check parameter
   if (isset($params['scid'])) {
   $parameter = $params['scid'];
   } else {
   $parameter = 1; // scID: 1 is default if no parameter is set
   }

   if($parameter <= $shortcode_quant) {   
      // Assign the correct value to the shortcode   
      $shortcode_key = 'shortcode_value' . $parameter;
      $default_shortcode_key = 'default_value' . $parameter;
      $returnvalue = getDefault($PIVOTX['config']->get($shortcode_key), $shortcode_config[$default_shortcode_key]);
   } else {
      $returnvalue = "<!-- [[ shortcode ]] parameter 'scid' is out of range -->";
   }
   
   return $returnvalue;   
}

/* ===========================
   Function create adminform
   =========================== */
function shortcodeAdmin(&$form_html) {
    global $PIVOTX, $shortcode_config, $shortcode_quant;

    $form = $PIVOTX['extensions']->getAdminForm('shortcode');
   
   for ($i=1; $i<=$shortcode_quant; $i++)
   {
      $fieldname = "shortcode_value" . $i;
      $fieldlabel = "Shortcode (scID) " . $i;
      $form->add( array(
         'type' => 'textarea',
         'name' => $fieldname,
         'label' => $fieldlabel,
         'error' => 'Error!',
         'size' => 20,
         'cols' => 70,
         'rows' => 5,
         'validation' => 'ifany|string|minlen=2|maxlen=4000'
      ));   
   }
      
   $form_html['shortcode'] = $PIVOTX['extensions']->getAdminFormHtml($form, $shortcode_config);
}

?>

[/edit]
That's the trouble with reality,
it's taken far too seriously
User avatar
Gooly
 
Posts: 77
Joined: Thu Nov 19, 2009 5:52 pm
Location: Lat 52° 4' 11.4874" Lon 4° 17' 28.001"


Return to 2.x Extensions

Who is online

Users browsing this forum: No registered users and 2 guests

cron