rulecachedcollection.class.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * @version $Id: rulecachedcollection.class.php 20129 2013-02-04 16:53:59Z moyo $
00004  -------------------------------------------------------------------------
00005  GLPI - Gestionnaire Libre de Parc Informatique
00006  Copyright (C) 2003-2013 by the INDEPNET Development Team.
00007 
00008  http://indepnet.net/   http://glpi-project.org
00009  -------------------------------------------------------------------------
00010 
00011  LICENSE
00012 
00013  This file is part of GLPI.
00014 
00015  GLPI is free software; you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation; either version 2 of the License, or
00018  (at your option) any later version.
00019 
00020  GLPI is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with GLPI. If not, see <http://www.gnu.org/licenses/>.
00027  --------------------------------------------------------------------------
00028  */
00029 
00030 /** @file
00031 * @brief 
00032 */
00033 if (!defined('GLPI_ROOT')) {
00034    die("Sorry. You can't access directly to this file");
00035 }
00036 
00037 /**
00038  * Specific rule collection for dictionnary : got a function initialize rule's caching system
00039 **/
00040 class RuleCachedCollection extends RuleCollection {
00041 
00042    // Specific ones
00043    /// Cache table used
00044    var $cache_table;
00045    /// Cache parameters
00046    var $cache_params;
00047 
00048 
00049    /**
00050     * Init a cache rule collection
00051     *
00052     * @param $cache_table           cache table used
00053     * @param $input_params    array input parameters to store (default "name" => "old_value")
00054     * @param $output_params   array output parameters to store (default "name" => "new_value")
00055     *
00056     * @return nothing
00057    **/
00058    function initCache($cache_table, $input_params=array("name" => "old_value"),
00059                       $output_params=array("name" => "new_value")) {
00060 
00061       $this->can_replay_rules             = true;
00062       $this->stop_on_first_match          = true;
00063       $this->cache_table                  = $cache_table;
00064       $this->cache_params["input_value"]  = $input_params;
00065       $this->cache_params["output_value"] = $output_params;
00066    }
00067 
00068 
00069    /**
00070     * Show the list of rules
00071     *
00072     * @param $target  where to go
00073     *
00074     * @return nothing
00075    **/
00076    function showAdditionalInformationsInForm($target) {
00077       global $CFG_GLPI;
00078 
00079       echo "<div class='spaced center'>";
00080       echo "<a class='vsubmit' href='#' onClick=\"var w = window.open('".$CFG_GLPI["root_doc"].
00081             "/front/popup.php?popup=show_cache&amp;sub_type=".$this->getRuleClassName()."' , ".
00082             "'glpipopup', 'height=400, width=1000, top=100, left=100, scrollbars=yes' );w.focus();\">".
00083             __('Cache information')."</a></div>";
00084    }
00085 
00086 
00087    /**
00088     * Process all the rules collection
00089     *
00090     * @param input            array the input data used to check criterias
00091     * @param output           array the initial ouput array used to be manipulate by actions
00092     * @param params           array parameters for all internal functions
00093     * @param force_no_cache         don't write rule's result into cache (for preview mode mainly)
00094     *                               (false by default)
00095     *
00096     * @return the output array updated by actions
00097    **/
00098    function processAllRules($input=array(), $output=array(), $params=array(),
00099                             $force_no_cache=false) {
00100 
00101       //If cache enabled : try to get value from the cache
00102       $new_values = $this->checkDataInCache($input);
00103 
00104       if ($new_values != Rule::RULE_NOT_IN_CACHE) {
00105          $output["_rule_process"] = true;
00106          return Toolbox::addslashes_deep(array_merge($output, $new_values));
00107       }
00108       $output = parent::processAllRules($input, $output, $params, $force_no_cache);
00109 
00110       if (!$force_no_cache
00111           && isset($output["_ruleid"])) {
00112          $this->insertDataInCache(Toolbox::addslashes_deep($input), $output);
00113          unset($output["_ruleid"]);
00114       }
00115 
00116       return $output;
00117    }
00118 
00119 
00120    /**
00121     * Show cache status by rules
00122    **/
00123    function showCacheStatusForRuleType() {
00124       global $DB, $CFG_GLPI;
00125 
00126       echo "<div class='center'>";
00127       echo "<table  class='tab_cadre_fixe'>";
00128 
00129       $query = "SELECT `name`,
00130                        `rules_id`,
00131                         COUNT(`rules_id`) AS cpt
00132                 FROM `".$this->cache_table."`,
00133                      `glpi_rules`
00134                 WHERE `".$this->cache_table."`.`rules_id` = `glpi_rules`.`id`
00135                 GROUP BY `rules_id`
00136                 ORDER BY `name`";
00137       $res_count = $DB->query($query);
00138 
00139       echo "<tr><th>".__('Cache information')."</th><th>".$this->getTitle()."</th></tr>\n";
00140       echo "<tr><td class='tab_bg_1'>".__('Rule name')."</td>";
00141       echo "<td class='tab_bg_1'>".__('Objects in cache')."</td></tr>\n";
00142 
00143       $total = 0;
00144       while ($datas = $DB->fetch_assoc($res_count)) {
00145          echo "<tr><td class='tab_bg_2'>";
00146          echo "<a href='".$CFG_GLPI['root_doc']."/front/popup.php?popup=show_cache&amp;sub_type=".
00147                 $this->getRuleClassName()."&amp;rules_id=".$datas["rules_id"]."'>".$datas["name"].
00148               "</a></td>";
00149          echo "<td class='tab_bg_2 numeric'>".$datas["cpt"]."</td></tr>\n";
00150          $total += $datas["cpt"];
00151       }
00152 
00153       echo "<tr>\n";
00154       echo "<td class='tab_bg_2 b'>".sprintf(__('%1$s (%2$s)'), __('Total'),
00155                                              $DB->numrows($res_count))."</td>";
00156       echo "<td class='tab_bg_2 b numeric'>".$total."</td>";
00157       echo "</tr></table></div>\n";
00158    }
00159 
00160 
00161    /**
00162     * Check if a data is in cache
00163     *
00164     * @param input data array to search
00165     *
00166     * @return boolean : is in cache ?
00167    **/
00168    function checkDataInCache($input) {
00169       global $DB;
00170 
00171       $where = "";
00172       $first = true;
00173       $input = Toolbox::addslashes_deep($input);
00174       
00175       foreach ($this->cache_params["input_value"] as $param => $value) {
00176          if (isset($input[$param])) {
00177             $where .= (!$first?" AND ":"")." `".$value."` = '".$input[$param]."'";
00178             $first  = false;
00179          }
00180       }
00181       $sql = "SELECT *
00182               FROM `".$this->cache_table."`
00183               WHERE ".$where;
00184 
00185       if ($res_check = $DB->query($sql)) {
00186          $output_values = array();
00187 
00188          if ($DB->numrows($res_check) > 0) {
00189             $data = $DB->fetch_assoc($res_check);
00190 
00191             foreach ($this->cache_params["output_value"] as $param => $param_value) {
00192                if (isset($data[$param_value])) {
00193                   $output_values[$param] = $data[$param_value];
00194                }
00195             }
00196             return $output_values;
00197          }
00198       }
00199 
00200       return Rule::RULE_NOT_IN_CACHE;
00201    }
00202 
00203 
00204    /**
00205     * Insert data in cache
00206     *
00207     * @param input   input data array
00208     * @param $output output data array
00209    **/
00210    function insertDataInCache($input, $output) {
00211       global $DB;
00212 
00213       $old_values = "";
00214       $into_old   = "";
00215 
00216       foreach ($this->cache_params["input_value"] as $param => $value) {
00217          $into_old   .= "`".$value."`, ";
00218          // Input are slashes protected...
00219          $old_values .= "'".$input[$param]."', ";
00220       }
00221 
00222       $into_new   = "";
00223       $new_values = "";
00224 
00225       foreach ($this->cache_params["output_value"] as $param => $value) {
00226          if (!isset($output[$param])) {
00227             $output[$param] = "";
00228          }
00229          $into_new   .= ", `".$value."`";
00230          // Output are not slashes protected...
00231          $new_values .= " ,'".$output[$param]."'";
00232       }
00233 
00234       $sql = "INSERT INTO `".$this->cache_table."`
00235                      (".$into_old."`rules_id`".$into_new.")
00236               VALUES (".$old_values.$output["_ruleid"].$new_values.")";
00237       $DB->query($sql);
00238    }
00239 
00240 
00241 }
00242 ?>