nextTableId++; $this[$this->nextTableId] = $table; return $this->nextTableId; } /** * 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. * * @param int $idTable * @throws Exception If the table can't be found * @return DataTable The table */ public function getTable($idTable) { if (!isset($this[$idTable])) { throw new TableNotFoundException(sprintf("Error: table id %s not found in memory. (If this error is causing you problems in production, please report it in Matomo issue tracker.)", $idTable)); } return $this[$idTable]; } /** * Returns the latest used table ID * * @return int */ public function getMostRecentTableId() { return $this->nextTableId; } /** * Delete all the registered DataTables from the manager */ public function deleteAll($deleteWhenIdTableGreaterThan = 0) { foreach ($this as $id => $table) { if ($id > $deleteWhenIdTableGreaterThan) { $this->deleteTable($id); } } if ($deleteWhenIdTableGreaterThan == 0) { $this->exchangeArray(array()); $this->nextTableId = 0; } } /** * Deletes (unsets) the datatable given its id and removes it from the manager * Subsequent get for this table will fail * * @param int $id */ public function deleteTable($id) { if (isset($this[$id])) { Common::destroy($this[$id]); $this->setTableDeleted($id); } } /** * Deletes all tables starting from the $firstTableId to the most recent table id except the ones that are * supposed to be ignored. * * @param int[] $idsToBeIgnored * @param int $firstTableId */ public function deleteTablesExceptIgnored($idsToBeIgnored, $firstTableId = 0) { $lastTableId = $this->getMostRecentTableId(); for ($index = $firstTableId; $index <= $lastTableId; $index++) { if (!in_array($index, $idsToBeIgnored)) { $this->deleteTable($index); } } } /** * Remove the table from the manager (table has already been unset) * * @param int $id */ public function setTableDeleted($id) { $this[$id] = null; } /** * Debug only. Dumps all tables currently registered in the Manager */ public function dumpAllTables() { echo "
Manager->dumpAllTables()
"; foreach ($this as $id => $table) { if (!($table instanceof DataTable)) { echo "Error table $id is not instance of datatable
"; var_export($table); } else { echo "
"; echo "Table (index=$id) TableId = " . $table->getId() . "
"; echo $table; echo "
"; } } echo "
-- End Manager->dumpAllTables()
"; } }