* improved - after adding a page, return to the calling url
[diogenes.git] / include / Barrel.php
index aab8e3f..7eb24a5 100644 (file)
@@ -21,7 +21,6 @@
 require_once 'Barrel/Page.php';
 require_once 'Barrel/Options.php';
 require_once 'diogenes/diogenes.flagset.inc.php';
-require_once 'Plugin/Skel.php';
  
 
 /** This class describes a Diogenes Barrel.
@@ -165,12 +164,14 @@ class Diogenes_Barrel
     $homepage = mysql_insert_id();
     $globals->db->query("update {$alias}_page set location='',title='Home',perms='public' where PID='$homepage'");
 
-    /* create home page & copy CSS template */
+    /* create home page */
     $rcs = new $globals->rcs($caller,$alias,$_SESSION['session']->username,true);
     $rcs->newdir("",$homepage);
     $rcs->commit($homepage,$globals->htmlfile,"");
-    $rcs->commit($homepage,$globals->cssfile,
-                        file_get_contents("{$globals->root}/{$globals->cssfile}") );
+
+    /* copy CSS template */
+    $def_css = file_get_contents("{$globals->root}/styles/{$globals->barrel_style_sheet}.css");
+    $rcs->commit($homepage,$globals->cssfile, $def_css);
   }
 
   
@@ -245,6 +246,24 @@ class Diogenes_Barrel
   }
   
   
+  /** List the plugins that are active in a given context
+   *
+   * @param $page   
+   */
+  function getPlugins($page = 0)
+  {
+    $plugins = array();
+    foreach ($this->pluginsCache as $plug)
+    {
+      if ($plug['page'] == $page) 
+      {
+        array_push($plugins, $plug);
+      }
+    }
+    return $plugins;    
+  }
+
+  
   /** Check whether the barrel has a given flag
    *
    * @param $flag
@@ -254,7 +273,74 @@ class Diogenes_Barrel
     return $this->flags->hasFlag($flag);
   }
 
+
+  /** Create a page with the given path, and return its PID.
+   *
+   * @param $path
+   * @param $caller
+   */
+  function makePath($path, &$caller)
+  {
+    $pathbits = split("/", $path);
+    $curpath = '';
+    $curpid = $this->getPID($curpath);;
+    foreach ($pathbits as $pathbit)
+    {
+      $newpath = ($curpath ? "$curpath/" : "") . $pathbit;
+      $newpid = $this->getPID($newpath);
+      if (!$newpid)
+      {
+        $tpage = new Diogenes_Barrel_Page($this, array('parent' => $curpid, 'location' => $pathbit));
+        $tpage->toDb(0, $caller);
+        $newpid = $this->getPID($newpath);
+      }
+      $curpath = $newpath;
+      $curpid = $newpid;
+    }
+    return $curpid;
+  }
+  
+
+  /** Recursively delete a given path.
+   *
+   * @param $path
+   * @param $caller
+   */
+  function rmPath($path, &$caller)
+  {
+    global $globals;
+
+    if (!$path) {
+      $caller->info("rmPath: will not delete from root!");
+      return false;
+    }
+    $curpid = $this->getPID($path);
+    if (!$curpid) {
+      $caller->info("rmPath: could not find '$path'");
+      return false;
+    }
+
+    // lookup children
+    $res = $globals->db->query("select PID from {$this->table_page} where parent=$curpid");
+    $children = array();
+    while (list($child) = mysql_fetch_row($res))
+      array_push($children, $child);
+    mysql_free_result($res);
+
+    // recurse into children
+    foreach ($children as $child)
+    {
+      $childpath = $this->getLocation($child);
+      if ($childpath) {
+        if (!$this->rmPath($childpath, $caller)) 
+          return false;
+      }
+    }
     
+    // delete this page
+    return Diogenes_Barrel_Page::delete($this, $curpid, $caller);
+  }
+
   /** Compile the directory tree
    */
   function compileTree()
@@ -309,7 +395,7 @@ class Diogenes_Barrel
   }
 
   
-  /** Load all active plugins for the specified page.
+  /** Load all plugins for the specified page.
    *
    * @param $bpage
    */
@@ -317,11 +403,12 @@ class Diogenes_Barrel
   {
     global $globals;
   
-    $plugins = $globals->plugins->cacheGetActive($this->pluginsCache, $this->alias, $page);
+    $plugins = $this->getPlugins($bpage->props['PID']);
+    
     $loaded = array();
-    foreach ($plugins as $plugname => $plugentry) 
+    foreach ($plugins as $plugentry) 
     {
-      $loaded[$plugname] =& $globals->plugins->load($plugname, $plugentry);
+      $loaded[$plugentry['plugin']] =& $globals->plugins->load($plugentry);
     }
     return $loaded;
   }