Editor.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2004 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 // dependency on PEAR
00022 require_once 'System.php';
00023 
00024 
00027 class Diogenes_Plugin_Editor {
00029   var $show_params = 1;
00030   
00032   var $readonly = false;
00033 
00035   var $plug_barrel;
00036 
00038   var $plug_page;
00039       
00041   var $plug_page_wperms;
00042   
00049   function Diogenes_Plugin_Editor($plug_barrel, $plug_page, $plug_page_wperms = '')
00050   {
00051     $this->plug_barrel = $plug_barrel;
00052     $this->plug_page = $plug_page;
00053     $this->plug_page_wperms = $plug_page_wperms;
00054   }
00055   
00056   
00062   function run(&$page, $outputvar = '')
00063   {
00064     global $globals;
00065     
00066     $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';    
00067     $target = isset($_REQUEST['plug_target']) ? $_REQUEST['plug_target'] : '';
00068          
00069     // load all available plugins
00070     $cachefile = $globals->plugins->cacheFile($this->plug_barrel);
00071     
00072     // if the tree cache does not exits, try to init it
00073     if (!file_exists($cachefile)) {
00074       $globals->plugins->compileCache($cachefile, $this->plug_barrel);
00075     }        
00076     $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
00077     $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);
00078            
00079     
00080     // handle updates
00081     switch ($action) {
00082     case "move_up": case "move_down":
00083       if ($this->readonly) die("Sorry, this plugin view is read-only.");
00084       
00085       $delta = ($action == "move_down") ? 1 : -1;
00086       //$page->info("moving plugin '$target'..");
00087       $plugcache_a = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $target);
00088       $plug_a =& $globals->plugins->load($plugcache_a);
00089       //$plug_a =& $globals->plugins->get($target);
00090       
00091       if (is_object($plug_a) && ($plug_a->active)) {
00092         $old_pos = $plug_a->pos;
00093         //$plug_b =& $globals->plugins->getAtPos($old_pos + $delta);        
00094         $plugcache_b = $globals->plugins->cacheGetAtPos($cache, $this->plug_barrel, $this->plug_page, $old_pos + $delta);
00095         
00096         if (is_array($plugcache_b))
00097         {
00098           $plug_b =& $globals->plugins->load($plugcache_b);
00099   
00100           // swap the current plugin and the next plugin
00101           if (is_object($plug_b) && ($plug_b->active)) 
00102           {
00103             $plug_a->writeParams($this->plug_barrel, $this->plug_page, $old_pos + $delta);        
00104             $plug_b->writeParams($this->plug_barrel, $this->plug_page, $old_pos);
00105           }        
00106         }
00107       }     
00108       $globals->plugins->compileCache($cachefile, $this->plug_barrel);
00109       $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);  
00110       $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);       
00111       break;
00112       
00113     case "update":
00114       if ($this->readonly) die("Sorry, this plugin view is read-only.");
00115     
00116       // list of active plugins
00117       $active = array();
00118       if (isset($_REQUEST['plugins_active'])) {
00119         $active = array_values($_REQUEST['plugins_active']);
00120       }
00121       
00122       foreach ($available as $plugin) {  
00123         $plugentry =& $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $plugin);
00124         if (!is_array($plugentry) and $this->plug_page) {
00125           $plugentry = $globals->plugins->cacheGet($cache, $this->plug_barrel, 0, $plugin);
00126           if (is_array($plugentry))
00127           {
00128             $plugentry['active'] = 0;
00129           }
00130         }
00131         
00132         // check we have a valid cache entry
00133         if (!is_array($plugentry)) {
00134           $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");          
00135           return;
00136         }
00137         
00138         $plug_h =& $globals->plugins->load($plugentry);
00139         
00140         if (is_object($plug_h) && is_array($plugentry)) {
00141           $pos = array_search($plugin, $active);
00142           
00143           if ($pos !== false) {
00144             // check the plugin is allowed in the current context
00145             if ($this->plug_barrel and $this->plug_page) {
00146               $wperms = $this->plug_page_wperms;
00147               
00148               // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
00149               if (!$plug_h->allow_wperms($wperms))
00150               {
00151                 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
00152                 break;
00153               }             
00154             }
00155             
00156             // retrieve parameters from REQUEST
00157             foreach ($plug_h->params as $key => $val)
00158             {
00159               if (isset($_REQUEST[$plug_h->name."_".$key])) {
00160                 $plug_h->params[$key] = $_REQUEST[$plug_h->name."_".$key];              
00161               }
00162             }
00163             
00164             // write parameters to database
00165             $plug_h->writeParams($this->plug_barrel, $this->plug_page, $pos);
00166           } else {        
00167             // erase parameters from database
00168             $plug_h->eraseParams($this->plug_barrel, $this->plug_page);          
00169           }        
00170         }
00171       }
00172       
00173       // log this action
00174       if ($this->plug_barrel)
00175       { 
00176         if ($this->plug_page)
00177         {
00178           $page->log('page_plugins', $this->plug_barrel.":".$this->plug_page);        
00179         } else {
00180           $page->log('barrel_plugins', $this->plug_barrel.":*");        
00181         }
00182       }
00183       
00184       $globals->plugins->compileCache($cachefile, $this->plug_barrel);
00185       $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);  
00186       $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);    
00187       break;
00188     }
00189     
00190     // get dump of plugins to fill out form
00191     $page->assign('plug_barrel', $this->plug_barrel);
00192     $page->assign('plug_page', $this->plug_page);
00193     
00194     $plugs = array();
00195     
00196     // start by adding the active plugins
00197     foreach ($cache as $plugcache)
00198     {
00199       if (in_array($plugcache['plugin'], $available) and ($plugcache['page'] == $this->plug_page) and ($plugcache['active']))
00200       {
00201         // check we have a valid plugin handle
00202         $plug_h = $globals->plugins->load($plugcache);
00203         if (!is_object($plug_h)) {
00204 
00205           $page->info("could not load disabled plugin '{$plugcache['plugin']}' in barrel '{$this->plug_barrel}'"); 
00206 
00207         } else {
00208                 
00209           $plugentry = $plug_h->dump();
00210           $plugentry['icon'] = $globals->icons->get_action_icon('plugins');            
00211           $type = $plugentry['type'];        
00212           if (!empty($plugs[$type])) {
00213             $plugentry['move_up'] = 1;
00214             $last = count($plugs[$type]) - 1;
00215             $plugs[$type][$last]['move_down'] = 1;              
00216           } else {
00217             $plugs[$type] = array();      
00218           }                
00219           array_push($plugs[$type], $plugentry);
00220 
00221         }
00222       }    
00223     }
00224     
00225     // next we add the disabled plugins
00226     if (!$this->readonly)
00227     {
00228       foreach ($available as $plugname)
00229       {
00230         $plugcache = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $plugname);
00231         if (!is_array($plugcache) or !$plugcache['active'])
00232         {
00233           $plugcache = $globals->plugins->cacheGet($cache, $this->plug_barrel, 0, $plugname);
00234           $plugcache['active'] = 0;        
00235           $plug_h = $globals->plugins->load($plugcache);
00236           if (!is_object($plug_h)) {
00237             $page->info("could not load disabled plugin '$plugname' in barrel '{$this->plug_barrel}'"); 
00238             return;
00239           }
00240                     
00241           $plugentry = $plug_h->dump();        
00242           $plugentry['icon'] = $globals->icons->get_action_icon('plugins');                        
00243           $type = $plugentry['type'];          
00244           if (empty($plugs[$type])) {
00245             $plugs[$type] = array();
00246           }
00247           array_push($plugs[$type], $plugentry);
00248         }
00249       }
00250     }
00251     
00252     /*
00253     echo "plugins <pre>";
00254     print_r($plugs);
00255     echo "</pre>";
00256     */
00257     $page->assign('plugins', $plugs);
00258     
00259     // values
00260     $page->assign('show_params', $this->show_params);
00261     $page->assign('readonly',$this->readonly);
00262 
00263     // translations    
00264     $page->assign('msg_submit', __("Submit"));
00265     $page->assign('msg_plugedit_plugin', __("plugin"));    
00266     $page->assign('msg_plugedit_plugins', __("plugins"));
00267     $page->assign('msg_plugedit_description', __("description"));
00268     $page->assign('msg_plugedit_parameters', __("parameters"));
00269     $page->assign('msg_move_up', __("move up"));
00270     $page->assign('msg_move_down', __("move down"));
00271     
00272     // if requested, assign the content to be displayed
00273     if (!empty($outputvar)) {
00274       $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
00275     }
00276   }
00277   
00278   
00283   function hide_params($hide)
00284   {
00285     $this->show_params = !$hide;
00286   }
00287   
00288 }
00289 
00290 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1