* @license PHP License * @package WB * @subpackage db */ WBClass::load( 'WBDatasource_SQLCommon' , 'WBDatasource_Callback' , 'WBDatasource_TableMicroCache' , 'WBDatasource_Table' , 'WBEvent' , 'WBLog' ); /** * Generic Table Sheet * * @version 0.2.1 * @package WB * @subpackage db */ class WBDatasource_Table_Sheet extends WBStdClass { const TABLE_TABLESHEET = 'tablesheet'; const TABLE_TABLECELL = 'tablesheetcell'; /** * @var WBDatasource_Table */ private $table; /** * List of rows */ private $rows = array(); /** * Constructor */ public function __construct($params = array()) { $this->table = WBClass::create('WBDatasource_Table'); $this->table->switchEventTrigger(false); } /** * Get Pager For Tables * * @return WBDatasource_Pager */ public function getPager() { $clause = array(); $options = array( 'limit' => 100 ); return $this->table->getPager(__CLASS__ . ':' . self::TABLE_TABLESHEET, self::TABLE_TABLESHEET, null, $clause, $options); } /** * Get Table Meta * * Try to load table from database. Meta data as well as table rows. * @param string $id * @return array */ public function get($id) { $this->rows = array(); $table = $this->table->get(self::TABLE_TABLESHEET, $id); if (1 != count($table)) { return array(); } $table = $table[0]; $table['row_count'] = 0; $table['column_count'] = 0; $table['cell_count'] = 0; $cells = $this->table->get(self::TABLE_TABLECELL, null, $id); // Normalize Cells $cellNoMax = 0; $row = array(); foreach ($cells as $c) { if ($table['row_count'] < $c['rowno']) { $cellNoMax = max($cellNoMax, count($row)); ++$table['row_count']; $this->rows[] = $row; $row = array(); } $row[] = $c; ++$table['cell_count']; } if (!empty($cells)) { $cellNoMax = max($cellNoMax, count($row)); ++$table['row_count']; $this->rows[] = $row; $row = array(); } $table['column_count'] = $cellNoMax; // fix column count foreach ($this->rows as &$row) { while ($table['column_count'] > count($row)) { $r = $row[0]; $r['columnno'] = count($row); $r['class'] = ''; $r['body'] = ''; $row[] = $r; } while ($table['column_count'] < count($row)) { array_pop($row); } } return $table; } /** * Save Table Meta Info * * @param string $id * @param array $data */ public function set($id, $data) { return $this->table->save(self::TABLE_TABLESHEET, $id, $data); } /** * Get Table Rows * * Get all rows of selected table * @return array */ public function getRows() { return $this->rows; } /** * Save Table Rows * * Store table's rows in database. Remove old records and insert current ones * * @param string $id of table * @param array $rows */ public function setRows($id, $rows) { $this->table->delete(self::TABLE_TABLECELL, null, $id); $save = array(); foreach ($rows as $i => $r) { $cell = array( $this->table->getIdentifier(self::TABLE_TABLESHEET) => $id, 'rowno' => $i, 'columnno' => 0, 'class' => '', 'body' => '' ); foreach ($r as $j => $c) { $cell['columnno'] = $j; $cell['class'] = $c['class']; $cell['body'] = $c['body']; $save[] = $cell; } } $this->table->save(self::TABLE_TABLECELL, '__new', $save); } }