Source for file SimpleTable.php

Documentation is available at SimpleTable.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23.  
  24. import('sylar.common.system.ExceptionInSylar');
  25. import('sylar.common.system.ConfigBox');
  26. import('sylar.common.system.Logger');
  27. import('sylar.common.data.DataContainer');
  28. import('sylar.common.data.SimpleTableRow');
  29.  
  30. /**
  31. * SimpleData
  32. * It's a standard Table. A simple table with some information about data contained
  33. * It's used to swap data with databases by DataBase classes
  34. *
  35. * the row must be an array indexed whit same header. For example
  36. *
  37. * Table name: test
  38. *
  39. * TbHeader: name surname phone email
  40. * TbRow 0 Nik Mallet 090909 nik@nik.ir
  41. * TbRow 1 Bob Bruk 0877689 bon@bob.com
  42. * TbRow ...
  43. * TbRow n Tim Ork 078 778 tim@tim.xx
  44. *
  45. * Header info must be specified only one time and every row must be a Sylar_SimpleTableRow with an array
  46. *
  47. * @see Sylar_SimpleTableRow
  48. * @see Sylar_SimpleTableHeader
  49. * @see Sylar_SimpleTableHeaderColumn
  50. *
  51. * @package Sylar
  52. * @version 1.0
  53. * @since 22/feb/08
  54. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  55. * @copyright Sylar Development Team
  56. */
  57. class Sylar_SimpleTable extends Sylar_DataContainer{
  58. private $id;
  59. private $title;
  60. private $description;
  61. private $tbColumns;
  62. private $flagRowControl;
  63. /** Sylar_SimpleTableHeader that containes all informations about header, columns, ecc... */
  64.  
  65. private $columnsHeader;
  66. /** the array that contains the rows as object Sylar_SimpleTableRow */
  67.  
  68. private $arrayData;
  69. /**
  70. * Constructor
  71. * it sets the main params of object. comprise ID and Title
  72. *
  73. * @since 22/feb/08
  74. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  75. *
  76. * @return void
  77. *
  78. * @param string $title
  79. * @param string $id
  80. */
  81. function __construct($title=false, $id=false){
  82. // call the parent class constructor
  83. parent::__construct();
  84. // Setting the ID
  85. if(!$id){
  86. $this->setTableId( uniqid('SylarId_', true) );
  87. }else{
  88. $this->setTableId( $id );
  89. }
  90. // Setting the Title
  91. if(!$title){
  92. $this->setTableTitle( uniqid('SylarTable_', true) );
  93. }else{
  94. $this->setTableTitle( $title );
  95. }
  96. // Set the row Control policy
  97. $this->setRowControls();
  98. // Start with empty header
  99. $this->setColumns(0);
  100. // reset datacontainers
  101. $this->resetHeader();
  102. $this->resetArrayData();
  103. }
  104. function __destruct(){
  105. // nothing to do at the moment
  106. }
  107. // Getter and Setter
  108. // --------------------------------
  109. public function setTableId($id){
  110. $this->id = $id;
  111. }
  112. public function getTableId(){
  113. return $this->id;
  114. }
  115. public function setTableTitle($titolo){
  116. $this->titolo = $titolo;
  117. }
  118. public function getTableTitle(){
  119. return $this->titolo;
  120. }
  121.  
  122. public function setColumns($colNum){
  123. $this->tbColumns = $colNum;
  124. }
  125. public function getColumns(){
  126. return $this->tbColumns;
  127. }
  128. public function setTableDescription($desc){
  129. $this->description = $desc;
  130. }
  131. public function getTableDescription(){
  132. return $this->description;
  133. }
  134. /**
  135. * Set controls on every Row
  136. * if set true the object controls the row befor insert else it try to insert directly.
  137. *
  138. * @since 22/feb/08
  139. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  140. *
  141. * @return void
  142. * @param boolean $control
  143. */
  144. public function setRowControls($control=true){
  145. $this->flagRowControl = $control;
  146. }
  147. public function getRowControls(){
  148. return $this->flagRowControl;
  149. }
  150. // Public methods
  151. // --------------------------------
  152. /**
  153. * Fill table data
  154. * fill data in the object starting from array $arrayData
  155. * it empty and reset the object data.
  156. *
  157. * @since 22/feb/08
  158. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  159. *
  160. * @todo To be done and test
  161. *
  162. * @return boolean
  163. * @param array $arrayData
  164. */
  165. public function fillDataFromArray($arrayData){
  166. //TODO must array of each row have the same key or the same number of elements? If yes, it must be implement
  167. if(is_array($arrayData)){
  168. $this->resetArrayData();
  169. foreach ($arrayData as $key => $value){
  170. if(is_array($value)){
  171. $this->addSimpleTableRow(new Sylar_SimpleTableRow($value));
  172. }else{
  173. throw new ExceptionInSylar("The array provided to key: ".$key." is not an array. [Sylar_SimpleTable::fillDataFromArray]");
  174. return false;
  175. }
  176. }
  177. }else{
  178. throw new ExceptionInSylar("The array provided to fill Sylar_SimpleTableRow isn't an array.");
  179. return false;
  180. }
  181. }
  182. /**
  183. * Append row in table
  184. * Row must be a Sylar_SimpleTableRow object, this method append it in the last position of table.
  185. *
  186. * @since 22/feb/08
  187. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  188. *
  189. * @return boolean
  190. * @param Sylar_SimpleTableRow $tableRow
  191. */
  192. public function addSimpleTableRow(Sylar_SimpleTableRow $tableRow){
  193. try{
  194. if($this->isRowControlsEnabled()){
  195. if(!$this->rowFitInTable($tableRow)){
  196. return false;
  197. }
  198. }
  199. $this->arrayData[] = $tableRow;
  200. }catch (ExceptionInSylar $ex){
  201. throw $ex;
  202. return false;
  203. }
  204. return true;
  205. }
  206. /**
  207. * Get row from table
  208. * Return the Object row containet at row $rowID. Return false on error
  209. *
  210. * @since 22/feb/08
  211. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  212. *
  213. * @return Sylar_SimpleTableRow
  214. * @param int $rowID
  215. */
  216. public function getRow($rowID){
  217. if($this->getRows()>0 && array_key_exists($rowID, $this->arrayData)){
  218. return $this->arrayData[$rowID];
  219. }
  220. throw new ExceptionInSylar("Searched Row id: ".$rowID." does not exists in the Table. [Sylar_SimpleTable::getRow]");
  221. return false;
  222. }
  223. /**
  224. * Numer of table rows
  225. * Return the number of rows contained in the table. Return false on error.
  226. *
  227. * @since 22/feb/08
  228. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  229. *
  230. * @return int
  231. */
  232. public function getRows(){
  233. if(is_array($this->arrayData) && count($this->arrayData)>0){
  234. return count($this->arrayData);
  235. }
  236. throw new ExceptionInSylar("Table (name: ".$this->getTableTitle().") is empty or not sets. [Sylar_SimpleTable::getRows]");
  237. return false;
  238. }
  239. /**
  240. * Table has row?
  241. * It controls if table has row, return true if rows is > 0 false otherwise
  242. *
  243. * @since 22/feb/08
  244. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  245. *
  246. * @see getRows()
  247. * @return boolean
  248. */
  249. public function hasRow(){
  250. if($this->getRows() > 0){
  251. return true;
  252. }else{
  253. return false;
  254. }
  255. }
  256. /**
  257. * Set the header of table
  258. * Set the information about table header into Table object.
  259. * Usually the columnCode correspond to the column name in DB
  260. *
  261. * @since 03/2008
  262. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  263. *
  264. * @return void
  265. * @param Sylar_SimpleTableHeader $tbHeader object that contain header of table
  266. */
  267. public function setColumnsHeader(Sylar_SimpleTableHeader $tbHeader){
  268. //TODO Check the header befor insert
  269. // throw new ExceptionInSylar("The Header array info provided is not an array or is empty. [Sylar_SimpleTable::fillTableHeader]");
  270. $this->columnsHeader = $tbHeader;
  271. // refresh Table Columns
  272. $this->setColumns($tbHeader->getColumns());
  273. }
  274. /**
  275. * Get Header
  276. * Return an object that contain information about header of table
  277. *
  278. * @since 22/feb/08
  279. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  280. *
  281. * @return Sylar_SimpleTableHeader
  282. */
  283. public function getColumnsHeader(){
  284. //TODO Check the header befor insert
  285. return $this->columnsHeader;
  286. }
  287. /**
  288. * Reset table container
  289. * if is set destroy it and reset the array data container
  290. *
  291. * @since 22/feb/08
  292. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  293. *
  294. * @return void
  295. */
  296. private function resetArrayData(){
  297. if(isset($this->arrayData)){
  298. unset($this->arrayData);
  299. }
  300. $this->arrayData = array();
  301. }
  302. /**
  303. * Reset HeaderInfo
  304. * It inizialize the container of table header information
  305. *
  306. * @since 22/feb/08
  307. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  308. *
  309. * @return void
  310. */
  311. private function resetHeader(){
  312. if(isset($this->columnsHeader)){
  313. unset($this->columnsHeader);
  314. }
  315. $this->columnsHeader = new Sylar_SimpleTableHeader();
  316. }
  317. /**
  318. * Check control
  319. * It verify if the control on the rows is on return true, false otherwise.
  320. *
  321. * @since 22/feb/08
  322. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  323. *
  324. * @return boolean
  325. */
  326. private function isRowControlsEnabled(){
  327. if(!$this->getRowControls()){
  328. return false;
  329. }
  330. return true;
  331. }
  332. /**
  333. * Controls Row befor add it in Table
  334. * It verify if the row is correct and can be added, if it is ok return true, false otherwise.
  335. *
  336. * @since 22/feb/08
  337. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  338. *
  339. * @return boolean
  340. * @param Sylar_SimpleTableRow $tbRow row to add in table
  341. */
  342. private function rowFitInTable(Sylar_SimpleTableRow $tbRow){
  343. if(!$tbRow->hasData()){
  344. throw new ExceptionInSylar("Error Adding Row in Table (name: ".$this->getTableTitle()."). Row is empty or not set. [Sylar_SimpleTable::rowFitInTable]");
  345. return false;
  346. }
  347. if($this->getColumns() != $tbRow->getColumns()){
  348. throw new ExceptionInSylar("Error Adding Row in Table (name: ".$this->getTableTitle()."). Row columns number is different from Table Columns number. [Sylar_SimpleTable::rowFitInTable]");
  349. return false;
  350. }
  351. return true;
  352. }
  353. }
  354. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:21 +0200 by phpDocumentor 1.3.0RC3