1+ <?php
2+
3+ namespace Ajax \semantic \html \collections \table ;
4+
5+ use Ajax \semantic \html \base \HtmlSemDoubleElement ;
6+ use Ajax \semantic \html \content \table \HtmlTableContent ;
7+ use Ajax \semantic \html \base \constants \Variation ;
8+ use Ajax \JsUtils ;
9+
10+ use Ajax \service \JArray ;
11+ use Ajax \semantic \html \content \table \HtmlTR ;
12+ use Ajax \semantic \html \collections \table \traits \TableTrait ;
13+
14+ /**
15+ * Semantic HTML Table component
16+ * @author jc
17+ *
18+ */
19+ class HtmlTable extends HtmlSemDoubleElement {
20+ use TableTrait;
21+ private $ _colCount ;
22+ private $ _compileParts ;
23+ private $ _footer ;
24+ private $ _afterCompileEvents ;
25+
26+ public function __construct ($ identifier , $ rowCount , $ colCount ) {
27+ parent ::__construct ($ identifier , "table " , "ui table " );
28+ $ this ->content =array ();
29+ $ this ->setRowCount ($ rowCount , $ colCount );
30+ $ this ->_variations =[ Variation::CELLED ,Variation::PADDED ,Variation::COMPACT ];
31+ $ this ->_compileParts =["thead " ,"tbody " ,"tfoot " ];
32+ $ this ->_afterCompileEvents =[];
33+ }
34+
35+ /**
36+ * {@inheritDoc}
37+ * @see \Ajax\semantic\html\collections\table\TableTrait::getTable()
38+ */
39+ protected function getTable () {
40+ return $ this ;
41+ }
42+
43+ /**
44+ * Returns/create eventually a part of the table corresponding to the $key : thead, tbody or tfoot
45+ * @param string $key
46+ * @return HtmlTableContent
47+ */
48+ public function getPart ($ key ) {
49+ if (\array_key_exists ($ key , $ this ->content ) === false ) {
50+ $ this ->content [$ key ]=new HtmlTableContent ("" , $ key );
51+ if ($ key !== "tbody " ) {
52+ $ this ->content [$ key ]->setRowCount (1 , $ this ->_colCount );
53+ }
54+ }
55+ return $ this ->content [$ key ];
56+ }
57+
58+ /**
59+ * Returns/create eventually the body of the table
60+ * @return \Ajax\semantic\html\content\table\HtmlTableContent
61+ */
62+ public function getBody () {
63+ return $ this ->getPart ("tbody " );
64+ }
65+
66+ /**
67+ * Returns/create eventually the header of the table
68+ * @return \Ajax\semantic\html\content\table\HtmlTableContent
69+ */
70+ public function getHeader () {
71+ return $ this ->getPart ("thead " );
72+ }
73+
74+ /**
75+ * Returns/create eventually the footer of the table
76+ * @return \Ajax\semantic\html\content\table\HtmlTableContent
77+ */
78+ public function getFooter () {
79+ return $ this ->getPart ("tfoot " );
80+ }
81+
82+ /**
83+ * Checks if the part corresponding to $key exists
84+ * @param string $key
85+ * @return boolean
86+ */
87+ public function hasPart ($ key ) {
88+ return \array_key_exists ($ key , $ this ->content ) === true ;
89+ }
90+
91+ /**
92+ *
93+ * @param int $rowCount
94+ * @param int $colCount
95+ * @return \Ajax\semantic\html\content\table\HtmlTableContent
96+ */
97+ public function setRowCount ($ rowCount , $ colCount ) {
98+ $ this ->_colCount =$ colCount ;
99+ return $ this ->getBody ()->setRowCount ($ rowCount , $ colCount );
100+ }
101+
102+ /**
103+ * Returns the cell (HtmlTD) at position $row,$col
104+ * @param int $row
105+ * @param int $col
106+ * @return \Ajax\semantic\html\content\HtmlTD
107+ */
108+ public function getCell ($ row , $ col ) {
109+ return $ this ->getBody ()->getCell ($ row , $ col );
110+ }
111+
112+ /**
113+ * Retuns the row at $rowIndex
114+ * @param int $rowIndex
115+ * @return \Ajax\semantic\html\content\HtmlTR
116+ */
117+ public function getRow ($ rowIndex ) {
118+ return $ this ->getBody ()->getRow ($ rowIndex );
119+ }
120+
121+ /**
122+ * Adds a new row and sets $values to his cols
123+ * @param array $values
124+ * @return HtmlTR
125+ */
126+ public function addRow ($ values =array ()) {
127+ $ row =$ this ->getBody ()->addRow ($ this ->_colCount );
128+ $ row ->setValues (\array_values ($ values ));
129+ return $ row ;
130+ }
131+
132+ /**
133+ * adds and returns a new row
134+ * @return \Ajax\semantic\html\content\table\HtmlTR
135+ */
136+ public function newRow () {
137+ return $ this ->getBody ()->newRow ($ this ->_colCount );
138+ }
139+
140+ public function setValues ($ values =array ()) {
141+ $ this ->getBody ()->setValues ($ values );
142+ return $ this ;
143+ }
144+
145+ public function setHeaderValues ($ values =array ()) {
146+ return $ this ->getHeader ()->setValues ($ values );
147+ }
148+
149+ public function setFooterValues ($ values =array ()) {
150+ return $ this ->getFooter ()->setValues ($ values );
151+ }
152+
153+ /**
154+ * Sets values to the col at index $colIndex
155+ * @param int $colIndex
156+ * @param array $values
157+ * @return \Ajax\semantic\html\collections\HtmlTable
158+ */
159+ public function setColValues ($ colIndex , $ values =array ()) {
160+ $ this ->getBody ()->setColValues ($ colIndex , $ values );
161+ return $ this ;
162+ }
163+
164+ /**
165+ * Sets values to the row at index $rowIndex
166+ * @param int $rowIndex
167+ * @param array $values
168+ * @return \Ajax\semantic\html\collections\HtmlTable
169+ */
170+ public function setRowValues ($ rowIndex , $ values =array ()) {
171+ $ this ->getBody ()->setRowValues ($ rowIndex , $ values );
172+ return $ this ;
173+ }
174+
175+ public function addColVariations ($ colIndex , $ variations =array ()) {
176+ return $ this ->getBody ()->addColVariations ($ colIndex , $ variations );
177+ }
178+
179+ public function colCenter ($ colIndex ) {
180+ return $ this ->colAlign ($ colIndex , "colCenter " );
181+ }
182+
183+ public function colRight ($ colIndex ) {
184+ return $ this ->colAlign ($ colIndex , "colRight " );
185+ }
186+
187+ public function colLeft ($ colIndex ) {
188+ return $ this ->colAlign ($ colIndex , "colLeft " );
189+ }
190+
191+ private function colAlign ($ colIndex , $ function ) {
192+ if (\is_array ($ colIndex )) {
193+ foreach ( $ colIndex as $ cIndex ) {
194+ $ this ->colAlign ($ cIndex , $ function );
195+ }
196+ } else {
197+ if ($ this ->hasPart ("thead " )) {
198+ $ this ->getHeader ()->$ function ($ colIndex );
199+ }
200+ $ this ->getBody ()->$ function ($ colIndex );
201+ }
202+ return $ this ;
203+ }
204+
205+ public function conditionalCellFormat ($ callback , $ format ) {
206+ $ this ->getBody ()->conditionalCellFormat ($ callback , $ format );
207+ return $ this ;
208+ }
209+
210+ public function conditionalRowFormat ($ callback , $ format ) {
211+ $ this ->getBody ()->conditionalRowFormat ($ callback , $ format );
212+ return $ this ;
213+ }
214+
215+ public function applyCells ($ callback ) {
216+ $ this ->getBody ()->applyCells ($ callback );
217+ return $ this ;
218+ }
219+
220+ public function applyRows ($ callback ) {
221+ $ this ->getBody ()->applyRows ($ callback );
222+ return $ this ;
223+ }
224+
225+ /**
226+ *
227+ * {@inheritDoc}
228+ *
229+ * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
230+ */
231+ public function compile (JsUtils $ js =NULL , &$ view =NULL ) {
232+ if (\sizeof ($ this ->_compileParts )<3 ){
233+ $ this ->_template ="%content% " ;
234+ $ this ->refresh ();
235+ }else {
236+ if ($ this ->propertyContains ("class " , "sortable " )) {
237+ $ this ->addEvent ("execute " , "$('# " . $ this ->identifier . "').tablesort(); " );
238+ }
239+ }
240+ $ this ->content =JArray::sortAssociative ($ this ->content , $ this ->_compileParts );
241+ return parent ::compile ($ js , $ view );
242+ }
243+
244+ /**
245+ *
246+ * {@inheritDoc}
247+ *
248+ * @see \Ajax\common\html\BaseHtml::fromDatabaseObject()
249+ */
250+ public function fromDatabaseObject ($ object , $ function ) {
251+ $ result =$ function ($ object );
252+ if (\is_array ($ result )) {
253+ $ result = $ this ->addRow ($ function ($ object ));
254+ } else {
255+ $ result = $ this ->getBody ()->_addRow ($ result );
256+ }
257+ if (isset ($ this ->_afterCompileEvents ["onNewRow " ])){
258+ if (\is_callable ($ this ->_afterCompileEvents ["onNewRow " ]))
259+ $ this ->_afterCompileEvents ["onNewRow " ]($ result ,$ object );
260+ }
261+ return $ result ;
262+ }
263+
264+ /**
265+ * @param array $parts
266+ * @return \Ajax\semantic\html\collections\HtmlTable
267+ */
268+ public function setCompileParts ($ parts =["tbody " ]) {
269+ $ this ->_compileParts =$ parts ;
270+ return $ this ;
271+ }
272+
273+ public function refresh (){
274+ $ this ->_footer =$ this ->getFooter ();
275+ $ this ->addEvent ("execute " , '$("# ' .$ this ->identifier .' tfoot").replaceWith(" ' .\addslashes ($ this ->_footer ).'"); ' );
276+ }
277+
278+ public function run (JsUtils $ js ){
279+ $ result = parent ::run ($ js );
280+ if (isset ($ this ->_footer ))
281+ $ this ->_footer ->run ($ js );
282+ return $ result ;
283+ }
284+
285+ /**
286+ * The callback function called after the insertion of each row when fromDatabaseObjects is called
287+ * callback function takes the parameters $row : the row inserted and $object: the instance of model used
288+ * @param callable $callback
289+ * @return \Ajax\semantic\html\collections\HtmlTable
290+ */
291+ public function onNewRow ($ callback ) {
292+ $ this ->_afterCompileEvents ["onNewRow " ]=$ callback ;
293+ return $ this ;
294+ }
295+ }
0 commit comments