[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/include/ -> ArchiveTable.class.php (source)

   1  <?php
   2  /* 
   3   * phpMyVisites : website statistics and audience measurements
   4   * Copyright (C) 2002 - 2006
   5   * http://www.phpmyvisites.net/ 
   6   * phpMyVisites is free software (license GNU/GPL)
   7   * Authors : phpMyVisites team
   8  */
   9  
  10  // $Id: ArchiveTable.class.php 198 2007-01-17 16:47:59Z matthieu_ $
  11  
  12  
  13  
  14  /**
  15   * Class that manages with database tables a_*
  16   * It records all datas, getId associated with Names, Name from Id, etc.
  17   */
  18  class ArchiveTable
  19  {
  20      /**
  21       * @var string suffix of the table
  22       */
  23      var $tableName; // suffix of a_* tables
  24      
  25      /**
  26       * @var array contains NameToId relation
  27       */
  28      var $nameToId;
  29      
  30      /**
  31       * @var array contains idToName relation
  32       */
  33      var $idToName;
  34      
  35      var $arrayAlreadyLoaded;
  36      /**
  37       * Constructor
  38       * 
  39       * @param string $type suffix of the table
  40       */
  41  	function ArchiveTable($type)
  42      {
  43          $this->tableSuffix=$type;
  44          $this->tableName = DB_TABLES_PREFIX.'a_'.$type;
  45          $this->nameToId = array();
  46          $this->idToName = array();
  47          $this->arrayAlreadyLoaded = array();
  48      }
  49  
  50      /**
  51       * returns name associated to int id
  52       * 
  53       * @param int|array $id
  54       * 
  55       * @return string 
  56       */
  57  	function getName($id)
  58      {
  59          if(empty($id) || $id == -1)
  60          {
  61              return "default";
  62          }
  63          
  64          if(!isset($this->idToName[$id]))
  65          {
  66              $this->loadName($id);
  67          }
  68          
  69          return stripslashes($this->idToName[$id]);
  70      }
  71      
  72  	function loadName($id)
  73      {
  74          if(is_array($id))
  75          {
  76              foreach($id as $eachId)
  77              {
  78                  $this->idToName[$eachId] = 'default';
  79              }
  80              sort($id);
  81  
  82              $r = query("SELECT name, id " .
  83                  " FROM ".$this->tableName.
  84                  " WHERE id IN (".implode(',',$id).")"
  85              );
  86          }
  87          else
  88          {
  89              $id = (int)$id; // in some case its not int, why??
  90              $r = query("SELECT name, id 
  91                       FROM ".$this->tableName."
  92                       WHERE id = $id");
  93          
  94          }
  95          
  96          if(isset($r) && mysql_num_rows($r) != 0)
  97          {
  98              while($l = mysql_fetch_assoc($r))
  99              {
 100                  //fix FS#209 - Unicode <title> problem
 101                  $l['name'] = U2U($l['name']);
 102                  
 103                  $this->nameToId[$l['name']] = $l['id'];
 104                  $this->idToName[$l['id']] = $l['name'];
 105              }
 106          }
 107          else
 108          {
 109              if(is_array($id))
 110              {
 111                  foreach($id as $eachId)
 112                  {
 113                      // case http://www.phpmyvisites.us/forums/index.php/m/13777/
 114                      // first day with unknown ID
 115                      if($eachId >= 0)
 116                      {
 117                          $this->idToName[$eachId] = "Unknown ($eachId in ".$this->tableName.")";
 118                      }
 119                  }
 120              }
 121              else {
 122                  $id = (int)$id; // in some case its not int, why??
 123                  $this->idToName[$id] = "Unknown ($id in ".$this->tableName.")";
 124              }
 125          }
 126                  
 127      }
 128      
 129      /**
 130       * returns id associated to string name
 131       * 
 132       * @param string $name
 133       * 
 134       * @return id
 135       */
 136  	function getId($name)
 137      {
 138          //if($this->tableSuffix=='page') trigger_error("stop dude $name", E_USER_ERROR);
 139          $name = databaseEscape(stripslashes($name));
 140          
 141          if(empty($name))
 142          {
 143              return -1;
 144          }
 145          if(isset($this->nameToId[$name]))
 146          {
 147              return $this->nameToId[$name];
 148          }
 149          else
 150          {
 151              $r = query("SELECT id
 152                          FROM ".$this->tableName."
 153                          WHERE name = '$name'
 154                          LIMIT 1");
 155              if(mysql_num_rows($r) == 0)
 156              {
 157                  return $this->save($name);
 158              }
 159              else
 160              {
 161                  $l = mysql_fetch_assoc($r);
 162                  $this->nameToId[$name] = $l['id']; 
 163                  $this->idToName[$l['id']] = $name; 
 164                  //printDebug("$name is id ".$l['id']);
 165                  return $this->getId($name);
 166              }
 167          }
 168      }
 169  
 170      /**
 171       * Saves the name in the table
 172       * Called by this->getId when asked id<->name doesn't exist yet
 173       * 
 174       * @param string $name
 175       * 
 176       * @return id of the row inserted
 177       */
 178  	function save($name)
 179      {
 180          //if($this->tableSuffix=='page') trigger_error("stop dude $name", E_USER_ERROR);
 181          $r = query("INSERT INTO ".$this->tableName." (name)
 182                      VALUES ('$name')");
 183          $i = mysql_insert_id();
 184          //print("$i <br>");
 185          return $i;
 186      }
 187  }
 188  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics