Newer
Older
mattpiwik
a validé
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
mattpiwik
a validé
*
mattpiwik
a validé
*/
/**
* The DataTable_Manager registers all the instanciated DataTable and provides an
* easy way to access them. This is used to store all the DataTable during the archiving process.
* At the end of archiving, the ArchiveProcessing will read the stored datatable and record them in the DB.
*
mattpiwik
a validé
*/
class Piwik_DataTable_Manager
{
static private $instance = null;
/**
* Returns instance
*
* @return Piwik_DataTable_Manager
*/
static public function getInstance()
{
if (self::$instance == null)
{
self::$instance = new self;
mattpiwik
a validé
}
return self::$instance;
}
/**
* Array used to store the DataTable
*
* @var array
*/
protected $tables = array();
mattpiwik
a validé
/**
* Id of the next inserted table id in the Manager
mattpiwik
a validé
* @var int
*/
mattpiwik
a validé
mattpiwik
a validé
/**
* Add a DataTable to the registry
*
* @param Piwik_DataTable
* @return int Index of the table in the manager array
mattpiwik
a validé
*/
public function addTable( $table )
{
$this->tables[$this->nextTableId] = $table;
$this->nextTableId++;
return $this->nextTableId - 1;
mattpiwik
a validé
}
/**
* Returns the DataTable associated to the ID $idTable.
* NB: The datatable has to have been instanciated before!
* This method will not fetch the DataTable from the DB.
*
* @exception If the table can't be found
* @return Piwik_DataTable The table
*/
public function getTable( $idTable )
{
if(!isset($this->tables[$idTable]))
{
throw new Exception(sprintf("The requested table (id = %d) couldn't be found in the DataTable Manager", $idTable));
}
return $this->tables[$idTable];
}
/**
* Delete all the registered DataTables from the manager
*/
public function deleteAll()
{
mattpiwik
a validé
foreach($this->tables as $id => $table)
{
mattpiwik
a validé
}
mattpiwik
a validé
$this->tables = array();
mattpiwik
a validé
}
/**
mattpiwik
a validé
* Deletes (unsets) the datatable given its id and removes it from the manager
* Subsequent get for this table will fail
*
* @param int $id
*/
mattpiwik
a validé
public function deleteTable( $id )
{
if(isset($this->tables[$id]))
{
mattpiwik
a validé
$this->setTableDeleted($id);
destroy($this->tables[$id]);
mattpiwik
a validé
}
}
mattpiwik
a validé
/**
* Remove the table from the manager (table has already been unset)
* @param int $id
mattpiwik
a validé
*/
public function setTableDeleted($id)
{
$this->tables[$id] = null;
}
mattpiwik
a validé
/**
mattpiwik
a validé
* Debug only. Dumps all tables currently registered in the Manager
mattpiwik
a validé
*/
mattpiwik
a validé
public function dumpAllTables()
mattpiwik
a validé
{
echo "<hr />Piwik_DataTable_Manager->dumpAllTables()<br />";
mattpiwik
a validé
foreach($this->tables as $id => $table)
{
if(!($table instanceof Piwik_DataTable ))
{
echo "Error table $id is not instance of datatable<br />";
mattpiwik
a validé
var_dump($table);
}
else
{
echo "<hr />";
echo "Table (index=$id) TableId = ". $table->getId() . "<br />";
mattpiwik
a validé
echo $table;
echo "<br />";
mattpiwik
a validé
}
}
echo "<br />-- End Piwik_DataTable_Manager->dumpAllTables()<hr />";