diogenes.core.page.inc.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 
00022 require_once 'Smarty.class.php';
00023 
00028 class DiogenesCorePage extends Smarty {
00031   function DiogenesCorePage() {
00032     global $globals;
00033 
00034     // smarty definitions
00035     $this->Smarty();
00036     $this->template_dir = $globals->root."/templates";
00037     $this->config_dir = $globals->root."/configs";
00038     $this->compile_dir = $globals->spoolroot."/templates_c";
00039 
00040     $this->register_function("extval","diogenes_func_extval");
00041     $this->register_function("flags","diogenes_func_flags");
00042     $this->register_function("a","diogenes_func_a");
00043     $this->register_function("checkbox","diogenes_func_checkbox");
00044     $this->register_function("diff","diogenes_func_diff");
00045     $this->register_function("menu_item","diogenes_func_menu_item");
00046     $this->register_function("tag","diogenes_func_tag");
00047     $this->register_function("toolbar","diogenes_func_toolbar");
00048     $this->debugging_ctrl = true;
00049 
00050     // smarty assignments
00051     $this->assign('script_self',$this->script_self());
00052   }
00053 
00054 
00057   function checkSession()
00058   {
00059     return isset($_SESSION['session']) && is_object($_SESSION['session']);
00060   }
00061 
00062 
00065   function script_self() {
00066     $url = explode("?",$this->script_uri());
00067     return $url[0];
00068   }
00069 
00070 
00073   function script_uri() {
00074     return $_SERVER['REQUEST_URI'];
00075   }
00076 
00077 
00082   function url($rel) {
00083     global $globals;
00084 
00085     return $globals->rooturl.$rel;
00086   }
00087 
00088 }
00089 
00090 
00099 function diogenes_func_a($params)
00100 {
00101   extract($params);
00102   if (empty($lnk))
00103     return;
00104 
00105   if (is_array($lnk)) {
00106     $text = $lnk[0];
00107     $href = $lnk[1];
00108     $icon = isset($lnk[2]) ? $lnk[2] : '';
00109   } else {
00110     $text = $lnk;
00111   }
00112   
00113   if (empty($href) && empty($class))
00114     $class = "empty";
00115 
00116   // we have either an href or a class
00117   return "<a"
00118          .( empty($class) ? "" : " class=\"$class\"")
00119          .( empty($href) ? "" : " href=\"$href\"")
00120          .">"
00121          .( empty($icon) ? $text : "<img src=\"$icon\" alt=\"$text\" title=\"$text\" />" )
00122          ."</a>";
00123 }
00124 
00125 
00130 function diogenes_func_checkbox($params)
00131 {
00132   extract($params);
00133 
00134   if (empty($name)) $name = "";
00135   if (empty($value)) $value = 1;
00136   $checked = (!empty($checked));
00137 
00138   return "<input type=\"checkbox\"".
00139          ($name ? " name=\"$name\"" : "").
00140          ($checked ? " checked=\"checked\"" : "").
00141          " value=\"$value\" />";
00142 }
00143 
00144 
00149 function diogenes_func_diff($params)
00150 {
00151   extract($params);
00152 
00153   if (empty($block) || empty($op))
00154     return;
00155   $lines=explode("\n",$block);
00156   $out=$out2="";
00157   foreach($lines as $line) {
00158     switch(substr($line,0,2)) {
00159     case "> ":
00160       if ($op == "a")
00161         $class = "add";
00162       else
00163         $class = "change";
00164       break;
00165     case "< ":
00166       if ($op == "d")
00167         $class = "delete";
00168       else
00169         $class = "change";
00170       break;
00171     default:
00172       $class = "other";
00173     }
00174     // strip 2 leading chars
00175     $line = substr($line,2);
00176     // if necessary, drop trailing newline char
00177     if (substr($line,-1) == "\n")
00178       $line = substr($line,0,-1);
00179 
00180     if (isset($old)) {
00181       if ($old != $class)
00182         $out .= "</div>";
00183       else
00184         $out .= "<br />\n";
00185     }
00186     if ($line) {
00187       if (!isset($old) || ($old != $class))
00188         $out .= "<div class=\"$class\">";
00189       $out .= htmlentities($line);
00190     }
00191     $old = $class;
00192   }
00193   return $out;
00194 }
00195 
00196 
00201 function diogenes_func_extval($params) {
00202   global $globals,$diogenes_db_cache;
00203   if(empty($diogenes_db_cache)) $diogenes_db_cache = Array();
00204 
00205   extract($params);
00206   if(empty($table) | empty($field) | empty($vtable) | empty($vjoinid) | empty($vfield))
00207     return;
00208 
00209   $cache_id = "$vtable,$vjoinid,$vfield";
00210 
00211   if(empty($diogenes_db_cache[$cache_id])) {
00212     $res = $globals->db->query("select $vjoinid,$vfield from $vtable order by $vfield");
00213     $diogenes_db_cache[$cache_id] = Array();
00214     while(list($id,$val) = mysql_fetch_row($res))
00215       $diogenes_db_cache[$cache_id][$id] = $val;
00216   }
00217   
00218   $html_out = "";
00219   // if we have a name, display opening select tag
00220   if (isset($value))
00221     return $diogenes_db_cache[$cache_id][$value];
00222 
00223   if(empty($name))
00224     return;
00225     
00226   $html_out .= "<select name=\"$name\">\n";
00227   foreach($diogenes_db_cache[$cache_id] as $id=>$val)
00228     $html_out .= "  <option value=\"$id\"".
00229                     ($selected==$id ? " selected=\"selected\"":"")
00230                     .">".htmlentities($val)."</option>\n";
00231   $html_out .= "</select>\n";
00232 
00233   return $html_out;
00234 }
00235 
00240 function diogenes_func_flags($params)
00241 {
00242   global $globals;
00243   extract($params);
00244 
00245   if (empty($table) | empty($field))
00246     return;
00247   if (empty($selected))
00248     $selected = "";
00249 
00250   $res = $globals->db->query("show columns from $table like '$field'");
00251   $set = mysql_fetch_row($res);
00252   $set = $set[1];
00253 
00254   // examine the type of field
00255   if (substr($set,0,5)=="enum(") {
00256     $multi = false;
00257     $set = substr($set,5);
00258   } else if (substr($set,0,4) == "set(") {
00259     $multi = true;
00260     $set = substr($set,4);
00261   } else {
00262     return "field neither set nor enum";
00263   }
00264 
00265   $html_out = "";
00266   // if we have a name, display opening select tag
00267   if (!empty($name))
00268     $html_out .= "<select name=\"$name".($multi ? "[]\" multiple=\"multiple\"" : "\"").">\n";
00269 
00270   $set = ereg_replace('\)$', '', $set);
00271   $set = explode(',', $set);
00272   for ($vals = explode(',', $selected); list(, $k) = each($vals);) {
00273     $vset[$k] = 1;
00274   }
00275 
00276   $countset = count($set);
00277   for($j=0; $j < $countset; $j++) {
00278     $subset = substr($set[$j], 1, -1);
00279     // Removes automatic MySQL escape format
00280     $subset = str_replace('\'\'', '\'', str_replace('\\\\', '\\', $subset));
00281     $html_out .= "<option value=\"$subset\""
00282                . ((isset($vset[$subset]) && $vset[$subset]) ? " selected=\"selected\"" : "")
00283                . ">".(isset($trans) ? $trans[$subset] : htmlspecialchars($subset))."</option>\n";
00284   }
00285 
00286   // if we have a name, display closing select tag
00287   if (!empty($name))
00288     $html_out .= "</select>\n";
00289 
00290   return $html_out;
00291 }
00292 
00293 
00301 function diogenes_func_menu_item($params)
00302 {
00303   extract($params);
00304   if (empty($item))
00305     return;
00306 
00307   $level = array_shift($item);
00308   if ($level == 0)
00309     $class = "top";
00310   else
00311     $class = ($level % 2) ? "odd" : "even";
00312 
00313   // process link
00314   $lnk = array( $item[0] );
00315   if ( isset($item[1]) )
00316     array_push($lnk, $item[1]);
00317   
00318   $margin = $level * 20;
00319   return "<div class=\"item\" style=\"margin-left: {$margin}px\">".
00320          diogenes_func_a(array("lnk"=>$lnk,"class"=>$class))."</div>";
00321 
00322 }
00323 
00324 
00336 function diogenes_func_tag($params)
00337 {
00338   extract($params);
00339 
00340   if (isset($item) && is_array($item))
00341     extract($item);
00342 
00343   if (empty($tag))
00344     return;
00345   
00346   $out = "<$tag";
00347 
00348   if (is_array($props)) {
00349     foreach($props as $key=>$val)
00350       $out .= " $key=\"$val\"";
00351   }
00352   
00353   $out .= empty($content) ? " />" : ">$content</$tag>";
00354 
00355   return $out;
00356 }
00357 
00358 
00367 function diogenes_func_toolbar($params)
00368 {
00369   extract($params);
00370   if (empty($lnk))
00371     return;
00372 
00373   if (!is_array($lnk))
00374     $lnk = array($lnk);
00375 
00376   // the separator
00377   $sep = "&nbsp;|&nbsp;";
00378   $out = $sep;
00379   foreach($lnk as $mylnk) {
00380     if (empty($class))
00381       $out .= diogenes_func_a(array("lnk"=>$mylnk));
00382     else
00383       $out .= diogenes_func_a(array("lnk"=>$mylnk,"class"=>$class));
00384     $out .= $sep;
00385   }
00386   return $out;
00387 }
00388 
00389 ?>

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