Code

Updated fonreports
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 18 May 2006 05:13:03 +0000 (05:13 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 18 May 2006 05:13:03 +0000 (05:13 +0000)
search str will effect results now.
Settings will be stored in session now.
Cleanup code

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@3392 594d385d-05f5-0310-b6e9-bd551577e9d8

plugins/gofon/fonreports/class_fonreport.inc

index 4c478938432c15b72df6f2061fd1557a746b8283..47e051b98049b0f17dcb7d7db3b655d26e21bbe0 100644 (file)
@@ -7,292 +7,336 @@ class fonreport extends plugin
   var $plDescription= "This does something";
 
   /* For internal use */
-  var $start= 0;
-  var $search_for= "*";
-  var $search_base= "";
-  var $year= "";
-  var $month= "";
-  var $sort= 0;
-  var $sort_direction= "down";
-  var $report_list= array();
-  var $userfilter= "";
-  var $ui= NULL;
-  var $fields= array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
-  var $range = 20;
+  var $start                             = 0;
+  var $search_for                = "*";
+  var $search_base       = "";
+  var $fields                            = array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
+  var $year                                      = "";
+  var $month                             = "";
+  var $sort                                      = 0;
+  var $sort_direction  = "down";
+  var $report_list             = array();
+  var $userfilter                      = "";
+  var $ui                                          = NULL;
+  var $range                             = 20;
+  var $EntryPerPage   = 20;
 
   /* attribute list for save action */
-  var $attributes= array();
-  var $objectclasses= array();
+  var $attributes_SO  = array("start","search_for","search_base","range","month","sort_direction","sort","year");
+  var $objectclasses  = array();
 
+  
+  /* Construct class */
   function fonreport ($config, $ui)
   {
-         /* Include config object */
-         $this->config= $config;
-         $this->ui= $ui;
-
-         /* Try to get matching search base for user provided
-                by 'dn' */
-         $tmp= preg_replace ("/^[^,]+,[^,]+,/", "", $ui->dn);
-         $sb= array_search($tmp, $this->config->departments);
-         if ($sb === FALSE){
-                 $sb= "/";
-         }
-         $this->search_base= $sb;
-
-         /* Get global filter config */
-         if (!is_global("fonfilter")){
-                 $ui= get_userinfo();
-                 $base= get_base_from_people($ui->dn);
-                 $fonfilter= array("year" => date("Y"),
-                                 "month" => date("m"),
-                                 "search_base" => $base,
-                                 "search_for" => "*");
-                 register_global("fonfilter", $fonfilter);
-         }
+    /* Include config object */
+    $this->config                      = $config;
+    $this->ui                            = $ui;
+    $this->search_base= get_base_from_people($ui->dn);
+  
+    $this->month      = date("m");
+    $this->year       = date("Y");
+
+    /* Use filter settings if we have already searched */
+    if (!is_global("fonfilter")){
+      $fonfilter = array();
+      foreach($this->attributes_SO as $name){
+        $fonfilter[$name]=$this->$name;
+      }
+      register_global("fonfilter", $fonfilter);
+    }else{
+      $fonfilter = get_global("fonfilter");
+      foreach($this->attributes_SO as $name){
+        $this->$name = $fonfilter[$name];
+      }
+    }
   }
 
+
+  /* Save ui interactions and store results in session, 
+      to remember settings */
+  function save_object()
+  {
+    $fonfilter= get_global("fonfilter");
+    if(isset($_POST['EntryPerPage'])){
+      $this->range = $_POST['EntryPerPage'];
+    }
+    if (isset($_GET['start'])){
+      $this->start= (int)$_GET['start'];
+    }
+    foreach( array("year", "month", "search_for", "search_base") as $type){
+      if (isset($_POST[$type])){
+        $this->$type= $_POST[$type];
+      }
+    }
+    
+    /* Adapt sorting */
+    if (isset($_GET['sort'])){
+      if ($this->sort == (int)$_GET['sort']){
+        if ($this->sort_direction == "down"){
+          $this->sort_direction= "up";
+        } else {
+          $this->sort_direction= "down";
+        }
+      }
+      $this->sort= (int)$_GET['sort'];
+      if ($this->sort < 0 || $this->sort > 6){
+        $this->sort= 0;
+      }
+    }
+    
+    foreach($this->attributes_SO as $name){
+      $fonfilter[$name] = $this->$name;
+    }
+    register_global("fonfilter", $fonfilter);
+  }
+
+
+  /* Search & display results */
   function execute()
   {
-       /* Call parent execute */
-       plugin::execute();
-
-       /* GVet template engine */
-       $smarty= get_smarty();
-
-       foreach($this->fields as $field){
-               if(chkacl($this->acl,$field)==""){
-                       $smarty->assign($field."ACL","");       
-               }else{
-                       $smarty->assign($field."ACL"," disabled ");     
-               }
-       }
-
-       $months= array();
-       $months[1]= _("January"); $months[2]= _("February"); $months[3]= _("March"); $months[4]= _("April");
-       $months[5]= _("May"); $months[6]= _("June"); $months[7]= _("July"); $months[8]= _("August");
-       $months[9]= _("September"); $months[10]= _("October"); $months[11]= _("November"); $months[12]= _("December");
-
-       /* Prepare template */
-       $current= date("Y");
-       $years= array();
-       for ($y= $current - 5; $y<=$current; $y++){
-               $years[]= $y;
-       }
-
-       $smarty->assign("plug"                  , "?plug=".validate($_GET['plug']));
-       $smarty->assign("launchimage"   , get_template_path('images/launch.png'));
-       $smarty->assign("search_image"  , get_template_path('images/search.png'));
-       $smarty->assign("search_for"    , $this->search_for);
-       $smarty->assign("bases"                 , $this->config->idepartments);
-       $smarty->assign("base_select"   , $this->search_base);
-       $smarty->assign("months"                , $months);
-       $smarty->assign("month_select"  , $this->month);
-       $smarty->assign("years"                 , $years);
-       $smarty->assign("year_select"   , $this->year);
-       $smarty->assign("search_result" , "");
-       
-       /* Connecting, selecting database */
-       if (!isset($this->config->data['SERVERS']['FON'])){
-               print_red(_("Can't connect to phone database, no reports can be shown!"));
-               return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
-       }elseif(!is_callable("mysql_connect")){
-               print_red(_("There is no mysql extension available, please check your php setup."));
-               return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
-       }else{
-
-               $cfg= $this->config->data['SERVERS']['FON'];
-               $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
-               if ($link === FALSE){
-                       print_red(_("Can't connect to phone database, no reports can be shown!"));
-                       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
-               }
-               if (! @mysql_select_db("gophone")){
-                       print_red(_("Can't select phone database for report generation!"));
-                       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
-               }
-       }
-
-
-       if(isset($_POST['EntryPerPage'])){
-               $this->range = $_POST['EntryPerPage'];
-       }
-
-         $fonfilter= get_global("fonfilter");
-         foreach( array("year", "month", "search_for", "search_base") as $type){
-                 if (isset($_POST[$type])){
-                         $fonfilter[$type]= $_POST[$type];
-                 }
-                 $this->$type= $fonfilter[$type];
-         }
-         register_global("fonfilter", $fonfilter);
-
-         /* Adapt sorting */
-         if (isset($_GET['sort'])){
-                 if ($this->sort == (int)$_GET['sort']){
-                         if ($this->sort_direction == "down"){
-                                 $this->sort_direction= "up";
-                         } else {
-                                 $this->sort_direction= "down";
-                         }
-                 }
-                 $this->sort= (int)$_GET['sort'];
-                 if ($this->sort < 0 || $this->sort > 6){
-                         $this->sort= 0;
-                 }
-         }
-
-         /* Search button has been pressed */
-         if ($this->search_for != ""){
-                 $this->start= 0;
-
-                 if (is_integer (strpos($this->search_for, "*"))){
-                         $s= $this->search_for;
-                 } else {
-                         $s= "*".$this->search_for."*";
-                 }
-                 $ldap= $this->config->get_ldap_link();
-                 $ldap->cd ($this->search_base);
-
-                 /* Perform ldap search for potential users */
-                 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
-                                 "(objectClass=gofonAccount)".
-                                 "(|(uid=$s)(l=$s)(homePhone=$s)".
-                                 "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
-                                 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
-                                 "(title=$s)))" ,array("uid"));
-
-                 $fon_users= array();
-                 while ($attrs= $ldap->fetch()){
-                         $acl= get_permissions ($ldap->getDN(), $this->ui->subtreeACL);
-                         $acl= get_module_permission($acl, "fax", $ldap->getDN());
-
-                         if (chkacl ($acl, "faxReport") == ""){
-                                 $fon_users[]= $attrs["uid"][0];
-                         }
-                 }
-
-                 /* Prepare SQL query */
-                 $this->userfilter= "";
-                 foreach ($fon_users as $user){
-                         $this->userfilter.= "uid = '$user' OR ";
-                 }
-                 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
-         }
-
-       $fields_str = "calldate, ";
-
-       foreach($this->fields as $field){
-               if($field == "calldate") continue;      
-               if(chkacl($this->acl,$field)==""){
-                       $fields_str .= $field.", ";
-               }
-       }
-
-       $fields_str = preg_replace("/, $/","",$fields_str);
-
-       if ($this->sort_direction == "down"){
-               $desc= "DESC";
-       } else {
-               $desc= "";
-       }
-       $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
-       $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
-       $query = "SELECT ".$fields_str." FROM cdr ".
-               "WHERE calldate <= $end AND ".
-               "calldate >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
-
-       $cfg= $this->config->data['SERVERS']['FON'];
-       $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
-
-       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
-                       $query, "Database query");
-
-       $result = @mysql_query($query);
-       if ($result === false){
-               print_red(_("Query for phone database failed!"));
-               return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
-       }
-       $report_list= array();
-       
-       while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
-               $append_str = "";
-               $hour=  substr($line["calldate"], 11, 2);
-               $minute=substr($line["calldate"], 14, 2);
-               $format= _("Y-M-D");
-               $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
-               $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
-               $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
-               $append_str .=  "<td>$date $hour:$minute</td>";
-               foreach(array("src","dst","channel","lastapp","disposition") as $atr){
-                       if(isset($line[$atr])){
-                               $append_str .=  "<td>".$line[$atr]."</td>";
-                       }
-               }
-               if(isset($line['duration'])){
-                       $append_str .=  "<td>".$this->gen_duration($line["duration"])."</td>";
-               }
-               $report_list[] = $append_str;
-       }
-
-       $this->report_list= $report_list;
-       @mysql_close($link);
-#   }
-
-       /* Generate output */
-       $mod= 0;
-       if (isset($_GET['start'])){
-               $this->start= (int)$_GET['start'];
-       }
-
-       $output= "";
-       foreach ($this->report_list as $val){
-               if ($mod < $this->start) {
-                       $mod++;
-                       continue;
-               }
-               if ($mod >= ($this->start + $this->range)){
-                       $mod++;
-                       break;
-               }
-               if ( ($mod++) & 1){
-                       $col= "background-color: #ECECEC;";
-               } else {
-                       $col= "background-color: #F5F5F5;";
-               }
-               $output.= "<tr style=\"height:22px; $col\">$val</tr>";
-       }
-
-
-       if ($output != ""){
-               $smarty->assign("search_result", $output);
-               $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
-       } else {
-               $smarty->assign("search_result", "");
-       }
-
-       /* Show main page */
-        for($i= 0; $i<7; $i++){
-                $smarty->assign("mode$i", "");
+    /* Call parent execute */
+    plugin::execute();
+
+    /* GVet template engine */
+    $smarty= get_smarty();
+
+
+    /*****************
+      Variable Init
+     *****************/
+
+    $fields_str = "";
+
+    $months= array();
+    for($i = 1 ; $i <= 12 ; $i ++ ){
+      $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
+    }
+
+    /* Prepare template */
+    $current= date("Y");
+    $years= array();
+    for ($y= $current - 5; $y<=$current; $y++){
+      $years[$y]= $y;
+    }
+
+    /*****************
+      Smarty 
+     *****************/
+
+    foreach($this->fields as $field){
+      if(chkacl($this->acl,$field)==""){
+        $smarty->assign($field."ACL","");      
+      }else{
+        $smarty->assign($field."ACL"," disabled ");    
+      }
+    }
+
+    $smarty->assign("plug",           "?plug=".validate($_GET['plug']));
+    $smarty->assign("launchimage",    get_template_path('images/launch.png'));
+    $smarty->assign("search_image",   get_template_path('images/search.png'));
+    $smarty->assign("search_for",     $this->search_for);
+    $smarty->assign("bases",          $this->config->idepartments);
+    $smarty->assign("base_select",    $this->search_base);
+    $smarty->assign("months",         $months);
+    $smarty->assign("month_select",   $this->month);
+    $smarty->assign("years",          $years);
+    $smarty->assign("year_select",    $this->year);
+    $smarty->assign("search_result",  "");
+
+
+    /*****************
+      Check Database , Table , Connection 
+     *****************/
+
+    /* Connecting, selecting database */
+    if (!isset($this->config->data['SERVERS']['FON'])){
+      print_red(_("Can't connect to phone database, no reports can be shown!"));
+      return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
+    }elseif(!is_callable("mysql_connect")){
+      print_red(_("There is no mysql extension available, please check your php setup."));
+      return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
+    }else{
+
+      $cfg= $this->config->data['SERVERS']['FON'];
+      $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
+      if ($link === FALSE){
+        print_red(_("Can't connect to phone database, no reports can be shown!"));
+        return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
+      }
+      if (! @mysql_select_db("gophone")){
+        print_red(_("Can't select phone database for report generation!"));
+        return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
+      }
+    }
+
+
+    /*****************
+      Get Query String && Search
+     *****************/
+
+    $query  = $this->CreateQuerySyntax();
+    $cfg    = $this->config->data['SERVERS']['FON'];
+    $link   = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
+
+    @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
+
+    $result = @mysql_query($query);
+    if ($result === false){
+      print_red(_("Query for phone database failed!"));
+      return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
+    }
+
+    /*****************
+      Fetch results 
+     *****************/
+  
+    $report_list= array();
+    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
+      $append_str = "";
+      $hour=  substr($line["calldate"], 11, 2);
+      $minute=substr($line["calldate"], 14, 2);
+      $format= _("Y-M-D");
+      $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
+      $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
+      $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
+      $append_str .=   "<td>$date $hour:$minute</td>";
+      foreach(array("src","dst","channel","lastapp","disposition") as $atr){
+        if(isset($line[$atr])){
+          $append_str .=       "<td>".$line[$atr]."</td>";
         }
-       $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
-                       ".png\" border=0 align=middle>");
-       $smarty->assign("month_select"  , $this->month);
-       $smarty->assign("years"                 , $years);
-       $smarty->assign("year_select"   , $this->year);
-       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
+      }
+      if(isset($line['duration'])){
+        $append_str .=         "<td>".$this->gen_duration($line["duration"])."</td>";
+      }
+      $report_list[] = $append_str;
+    }
+
+    $this->report_list= $report_list;
+    @mysql_close($link);
+
+    /* Generate output */
+    $mod= 0;
+
+
+    /*****************
+      Create list of results  
+     *****************/
+
+    $output= "";
+    foreach ($this->report_list as $val){
+      if ($mod < $this->start) {
+        $mod++;
+        continue;
+      }
+      if ($mod >= ($this->start + $this->range)){
+        $mod++;
+        break;
+      }
+      if ( ($mod++) & 1){
+        $col= "background-color: #ECECEC;";
+      } else {
+        $col= "background-color: #F5F5F5;";
+      }
+      $output.= "<tr style=\"height:22px; $col\">$val</tr>";
+    }
+
+
+    /*****************
+      Tell smarty what we have found  
+     *****************/
+
+    if ($output != ""){
+      $smarty->assign("search_result", $output);
+      $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
+    } else {
+      $smarty->assign("search_result", "");
+    }
+
+    /* Show main page */
+    for($i= 0; $i<7; $i++){
+      $smarty->assign("mode$i", "");
+    }
+    $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
+        ".png\" border=0 align=middle>");
+    return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
   }
 
+  
+  /* Create duration string   12'11" */
   function gen_duration($seconds)
   {
-       if ($seconds / 60 > 1){
-               $minutes= (int)($seconds / 60);
-               $seconds= $seconds % 60;
-               return ("$minutes&rsquo;$seconds&rdquo;");
-       }
-       
-       return ("$seconds&rdquo;");
+    if ($seconds / 60 > 1){
+      $minutes= (int)($seconds / 60);
+      $seconds= $seconds % 60;
+      return ("$minutes&rsquo;$seconds&rdquo;");
+    }
+    return ("$seconds&rdquo;");
   }
 
-}
 
+  /* Create WHERE part for our mysql serach */
+  function GetUidMatchingFilter()
+  {
+    $ldap       = $this->config->get_ldap_link();
+    $ldap->cd     ($this->search_base);
+    $s          = $this->search_for;
+    $s          = preg_replace("/\%/","",$s);  
+    $s2         = preg_replace("/\*/","%",$s);  
+
+    /* Perform ldap search for potential users */
+    $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(objectClass=gofonAccount)".
+        "(|(uid=$s)(l=$s)(homePhone=$s)(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
+        "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)(title=$s)))" ,array("uid"));
+    $str = " AND (";
+    $fields = array("dstchannel","channel");
+    if($ldap->count()){
+      while ($attrs= $ldap->fetch()){
+        $uid =  $attrs["uid"][0];
+        foreach($fields as $name){
+          $str .= $name." like '%".$uid."%' OR ";
+        }
+      }
+    }
+    $str .= " dst like '".$s2."' OR src like '".$s2."' OR lastapp like '".$s2."')"; 
+    return($str);
+  }
+
+
+  /* Create query string */
+  function CreateQuerySyntax()
+  {
+    /* Get extended search filter which contain uids and so on */
+    $uidstring = $this->GetUidMatchingFilter();
+
+    /* Create string with all fields seperated by ,*/
+    $fields_str ="";
+    foreach($this->fields as $field){
+      if($field == "calldate") continue;       
+      if(chkacl($this->acl,$field)==""){
+        $fields_str .= $field.", ";
+      }
+    }
+    $fields_str = preg_replace("/, $/","",$fields_str);
+
+    /* Create Sort tag */
+    if ($this->sort_direction == "down"){
+      $desc= "DESC";
+    } else {
+      $desc= "ASC";
+    }
+    /* Create times */
+    $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
+    $end=   date ("YmdHis", mktime(23,59,59,($this->month +1),0,$this->year));
+    $query = "SELECT ".$fields_str.",calldate FROM cdr ".
+      "WHERE   
+      calldate <= $end 
+      AND 
+      calldate >= $start 
+      ". $uidstring." 
+      ORDER BY ".$this->fields[$this->sort]." $desc;";
+    return($query);
+  }
+}
+// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>