rework plugin handling to access parameters via declareParam, getParamValues, setPara...
authorJeremy Laine <jeremy.laine@m4x.org>
Mon, 15 May 2006 18:20:42 +0000 (18:20 +0000)
committerJeremy Laine <jeremy.laine@m4x.org>
Mon, 15 May 2006 18:20:42 +0000 (18:20 +0000)
include/Plugin/Editor.php
include/Plugin/Skel.php
plugins/FileList.php
plugins/HtmlHead.php
plugins/HttpHeader.php
plugins/LinksMagic.php
plugins/MailForm.php

index 9de3b2f..539b8f0 100644 (file)
@@ -120,7 +120,7 @@ class Diogenes_Plugin_Editor {
       }
       
       foreach ($available as $plugin) {  
-        $plugentry =& $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $plugin);
+        $plugentry = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $plugin);
         if (!is_array($plugentry) and $this->plug_page) {
           $plugentry = $globals->plugins->cacheGet($cache, $this->plug_barrel, 0, $plugin);
           if (is_array($plugentry))
@@ -154,10 +154,10 @@ class Diogenes_Plugin_Editor {
             }
             
             // retrieve parameters from REQUEST
-            foreach ($plug_h->params as $key => $val)
+            foreach ($plug_h->getParamNames() as $key)
             {
               if (isset($_REQUEST[$plug_h->name."_".$key])) {
-                $plug_h->params[$key] = $_REQUEST[$plug_h->name."_".$key];              
+                $plug_h->setParamValue($key, $_REQUEST[$plug_h->name."_".$key]);              
               }
             }
             
index 093000d..7e86546 100644 (file)
@@ -67,22 +67,60 @@ class Diogenes_Plugin_Skel {
   {
     return ($wperms != 'public');
   }
-   
 
+
+  /** Declare a plugin parameter.
+   */
+  function declareParam($key, $val)
+  {
+    $this->params[$key] = $val;
+  }
+
+
+  /** Return an array of parameter names.
+   */
+  function getParamNames()
+  {
+    return array_keys($this->params);
+  }
+
+
+  /** Return the value of a parameter of the plugin.
+   */
+  function getParamValue($key)
+  {
+    return isset($this->params[$key]) ? $this->params[$key] : '';
+  }
+  /** Set the value of a parameter of the plugin.
+   */
+  function setParamValue($key, $val)
+  {
+    if (isset($this->params[$key])) {
+      //echo "$this->name : Calling setParamValue($key, $val)<br/>\n";
+      $this->params[$key] = $val; 
+    } else {
+      //echo "$this->name : skipping setParamValue($key, $val)<br/>\n";
+    }
+  }
   /** Set plugin parameters.
    *
    * @param $params
    */
   function setParams($params)
   {
-    $bits = explode("\0", $params);      
+    $bits = explode("\0", $params);
     foreach ($bits as $bit)
     {
       $frags = explode("=", $bit, 2);
       $key = $frags[0];
-      $val = isset($frags[1]) ? $frags[1] : '';
-      if (isset($this->params[$key])) {
-        $this->params[$key] = $val;
+      if (!empty($key))
+      {
+        $val = isset($frags[1]) ? $frags[1] : '';
+        $this->setParamValue($key, $val);
       }
     }
   }
@@ -97,14 +135,15 @@ class Diogenes_Plugin_Skel {
   {
     global $globals;
     
-    //echo $this->name . " : deleteParams($barrel, $page)<br/>\n";
+    //echo $this->name . " : eraseParams($barrel, $page)<br/>\n";
     $globals->db->query("delete from diogenes_plugin where plugin='{$this->name}' and barrel='$barrel' and page='$page'");
     
     $this->active = 0;
     unset($this->pos);
-    foreach ($this->params as $key => $val)
+    foreach ($this->getParamNames() as $key)
     {
-      $this->params[$key] = '';
+      //echo "$this->name : erasing param<br/>\n";
+      $this->setParamValue($key, '');
     }
   }
    
@@ -123,9 +162,10 @@ class Diogenes_Plugin_Skel {
     $this->active = 1;
     
     $params = '';
-    foreach ($this->params as $key => $val)
-    { 
-      //echo $this->name . " $key=$val<br/>";
+    foreach ($this->getParamNames() as $key)
+    {
+      $val = $this->getParamValue($key);
+      //echo "$this->name : $key = $val<br/>\n";
       $params .= "$key=$val\0";     
     }        
     $globals->db->query("replace into diogenes_plugin set plugin='{$this->name}', barrel='$barrel', page='$page', pos='$pos', params='$params'");
index f97197a..07acbfd 100644 (file)
@@ -35,9 +35,17 @@ class FileList extends Diogenes_Plugin_Filter {
   /** Plugin description */
   var $description = "This plugin allows you to insert a directory listing with icons and modification date. To make use of this plugin, insert <b>{FileList}</b> in your page where the file list should appear.";
   
-  /** Plugin parameters */
-  var $params = array('dirbase' => "", 'urlbase' => "", 'match' => "");
-    
+  /** Constructor.
+   */
+  function FileList()
+  {
+    global $page;
+    $this->declareParam('dirbase', '');
+    $this->declareParam('urlbase', '');
+    $this->declareParam('match', '');
+  }
+
   
   /** Prepare the output for a single file of the list.
    *
@@ -97,21 +105,21 @@ class FileList extends Diogenes_Plugin_Filter {
   {
     global $page;
     $bbarel = $page->barrel;
-    
+   
     // process arguments
-    $params = array();
-    foreach($this->params as $key => $val) {
-      $params[$key] = isset($args[$key]) ? $args[$key] : $this->params[$key];
+    $instance_vals = array();
+    foreach($this->getParamNames() as $key) {
+      $instance_vals[$key] = isset($args[$key]) ? $args[key] : $this->getParamValue($key);
     }
     
-   //print_r($params);
-    if (empty($params['dirbase'])) {
-      $params['dirbase'] = $bbarel->spool->spoolPath($page->curpage->props['PID']);
+   //print_r($instance_vals);
+    if (empty($instance_vals['dirbase'])) {
+      $instance_vals['dirbase'] =  $bbarel->spool->spoolPath($page->curpage->props['PID']);
     }
 
     // process parameters 
     $output = '';
-    $dir = $params['dirbase'];    
+    $dir = $instance_vals['dirbase'];
     if (is_dir($dir) && ($dh = opendir($dir))) {
       $output .= 
        '<table class="light">
@@ -125,7 +133,7 @@ class FileList extends Diogenes_Plugin_Filter {
       /* get the matching files */
       while (($fname = readdir($dh)) !== false) {
         if ( is_file("$dir/$fname") 
-              && preg_match('/^'.$params['match'].'/',$fname) )
+              && preg_match('/^'.$instance_vals['match'].'/',$fname) )
           $filelist[] = $fname;
       }
       closedir($dh);
@@ -134,7 +142,7 @@ class FileList extends Diogenes_Plugin_Filter {
       if (is_array($filelist)) {
         rsort($filelist);
         while(list ($key,$val) = each($filelist)) {
-          $output .= $this->list_file("$dir/$val",$val,$params['urlbase'].$val);
+          $output .= $this->list_file("$dir/$val",$val,$instance_vals['urlbase'].$val);
         }
       } else {
         $output .= '<tr><td colspan="4"><i>'.__("no files").'</i></td></tr>';
index a1be65c..8e53ffa 100644 (file)
@@ -30,9 +30,15 @@ class HtmlHead extends Diogenes_Plugin_Filter
   
   /** Plugin description */
   var $description = "This plugin allows you to add entries to a page's &lt;head&gt; block.";
-  
-  /** Plugin parameters */
-  var $params = array('contents' => '');
+
+  /** Constructor.
+   */
+  function HtmlHead()
+  {
+    $this->declareParam('contents', '');
+  }
+
 
   /** Apply filtering to the input and return an output.
    *
@@ -41,10 +47,10 @@ class HtmlHead extends Diogenes_Plugin_Filter
   function filter($input)
   {
     global $page;
-   
-    if ($this->params['contents'])
+    $contents = $this->getParamValue('content'); 
+    if (!empty($contents))
     {
-      array_push($page->head, $this->params['contents']);
+      array_push($page->head, $contents);
     }
 
     return $input;
index 9c89fb4..ec9dba4 100644 (file)
@@ -31,8 +31,14 @@ class HttpHeader extends Diogenes_Plugin_Filter
   /** Plugin description */
   var $description = "This plugin allows you to add an HTTP header to a page.";
   
-  /** Plugin parameters */
-  var $params = array('contents' => '');
+
+  /** Constructor.
+   */
+  function HttpHeader()
+  {
+    $this->declareParam('contents', '');
+  }
+   
 
   /** Apply filtering to the input and return an output.
    *
@@ -40,13 +46,11 @@ class HttpHeader extends Diogenes_Plugin_Filter
    */
   function filter($input)
   {
-    $header = $this->params['contents'];
-
+    $header = $this->getParamValue('contents');
     if ($header) 
     {
       header($header);
     }
-   
     return $input;
   }
   
index 7301c0f..6c1f83c 100644 (file)
@@ -31,10 +31,17 @@ class LinksMagic extends Diogenes_Plugin_Filter
   
   /** Plugin description */
   var $description = "This plugin allows you to mark external and secure (HTTPS) links in your pages.";
-  
-  /** Plugin parameters */
-  var $params = array('main' => 1, 'sidebar' => 1);
 
+  /** Constructor.
+   */
+  function LinksMagic()
+  {
+    $this->declareParam('main', 1);
+    $this->declareParam('sidebar', 1);
+  }
+
+  
   /** Apply filtering to the input and return an output.
    *
    * @param $input
@@ -43,12 +50,12 @@ class LinksMagic extends Diogenes_Plugin_Filter
   {
     global $page;
  
-    if ($this->params['sidebar'])
+    if ($this->getParamValue('sidebar'))
     {
       array_unshift($page->head, '<link rel="stylesheet" href="'.$page->url('links-sidebar.css').'" type="text/css" />');
     }
 
-    if ($this->params['main'])
+    if ($this->getParamValue('main'))
     {
       array_unshift($page->head, '<link rel="stylesheet" href="'.$page->url('links-main.css').'" type="text/css" />');
     }
index 8125ebb..b87d905 100644 (file)
@@ -35,8 +35,16 @@ class MailForm extends Diogenes_Plugin_Filter
   /** Plugin description */
   var $description = "This plugin allows you to insert a form to send an e-mail to a fixed recipient. To make use of this plugin, insert <b>{MailForm}</b> in your page where the mail form should appear.";
   
-  /** Plugin parameters */
-  var $params = array('email' => '', 'title' => '', 'subject_tag' => '[web form] ');
+
+  /** Constructor.
+   */
+  function MailForm()
+  {
+    $this->declareParam('email', '');
+    $this->declareParam('title', '');
+    $this->declareParam('subject_tag', '[web form]');
+  }
+
 
   /** Show an instance of the MailForm plugin.
    */
@@ -44,9 +52,9 @@ class MailForm extends Diogenes_Plugin_Filter
   {
     global $page;
 
-    // get params
-    $to_email = $this->params['email'];
-    $form_title = $this->params['title'];
+    // get parameters
+    $to_email = $this->getParamValue('email');
+    $form_title = $this->getParamValue('title');
     
     if (!isvalid_email($to_email)) {
       return '<p>You must specify a valid e-mail in the "email" parameter to make use of the MailForm plugin.<p>';
@@ -80,7 +88,7 @@ class MailForm extends Diogenes_Plugin_Filter
       
       $mymail = new HermesMailer();
       $mymail->setFrom($from);
-      $mymail->setSubject($this->params['subject_tag'].$subject);
+      $mymail->setSubject($this->getParamValue('subject_tag').$subject);
       $mymail->addTo($to_email);
       $mymail->setTxtBody($message);
       $mymail->send();