Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
TreeTable.php
Go to the documentation of this file.
1 <?php
2 import('Dataface/ResultList.php');
10 
14  var $record;
15 
21 
22 
29  $this->record =& $record;
30  $this->relationship = $relationship;
31  }
32 
40  function &getRecordByRowId($rowid){
41  $path = explode('-', $rowid);
42  if ( empty($path[0]) ) array_shift($path);
43 
44  if ( empty($path) ) return $this->record;
45 
46  $root =& $this->record;
47 
48  if ( isset($this->relationship) ){
49  // first level comes from relationship
50  $index = intval(array_shift($path));
51  $record =& $this->record->getRelatedRecord($this->relationship, $index-1);
52  if ( !isset($record) ) {
53  $null = null;
54  return $null;
55  }
56  unset($root);
57  $root =& $record->toRecord();
58  unset($record);
59  }
60 
61  while ( !empty($path) ){
62  $currid = array_shift($path);
63  $record =& $root->getChild(intval($currid)-1);
64  if ( !isset($record) ){
65  $null = null; return $null;
66  }
67  unset($root);
68  $root =& $record;
69  unset($record);
70  }
71 
72  return $root;
73  }
74 
86  function &getSubrows(&$rows, $rowid, &$record, $depth=3){
87  if ( $depth == 0 ) return $rows;
88  if ( isset($record) ) $root =& $record;
89  else {
90  $root =& $this->getRecordByRowId($rowid);
91  if ( !isset($root ) ) return $rows;
92  }
93  if ( empty($rowid) and isset($this->relationship) ){
94  // we are starting from the root and the first level should from the given relationship.
95  $it = $root->getRelationshipIterator($this->relationship);
96  $i = 1;
97  while ( $it->hasNext() ){
98  $curr_rowid = strval($i++);
99  $row =& $it->next();
100  $rowRecord =& $row->toRecord();
101  $rows[$curr_rowid] = array('record'=>& $rowRecord, 'rowid'=>$curr_rowid, 'hasChildren'=>false);
102  $numrows = count($rows);
103  $this->getSubrows($rows, $curr_rowid, $rowRecord, $depth-1);
104  if ( count($rows)>$numrows) $rows[$curr_rowid]['hasChildren'] = true;
105  unset($rowRecord);unset($row);
106  }
107  } else {
108  $children = $root->getChildren();
109  if ( isset($children) ){
110  $i=1;
111  foreach (array_keys($children) as $childkey){
112  $curr_rowid = $rowid.(!empty($rowid)?'-':'').strval($i++);
113  $rowRecord =& $children[$childkey];
114  $rows[$curr_rowid] = array('record'=>&$rowRecord, 'rowid'=>$curr_rowid, 'hasChildren'=>false);
115  $numrows = count($rows);
116  $this->getSubrows($rows, $curr_rowid, $rowRecord, $depth-1);
117  if ( count($rows)>$numrows) $rows[$curr_rowid]['hasChildren'] = true;
118  unset($rowRecord);
119  }
120  }
121  }
122  return $rows;
123  }
124 
132  function getSubrowsAsHTML($rowid, $depth=3, $treetableid='treetable'){
133  if ( isset($this->relationship) ){
134  $rel =& $this->record->_table->getRelationship($this->relationship);
135  $table =$rel->getDomainTable();
136  if ( PEAR::isError($table) ){
137  $destTables =& $rel->getDestinationTables();
138  $table = $destTables[0]->tablename;
139  }
140  } else {
141  $table = $this->record->_table->tablename;
142  $rel =& $this->record->_table->getChildrenRelationship();
143  }
144 
145  $default_order_column = $rel->getOrderColumn();
146 
147  $resultList = new Dataface_ResultList($table);
148  $columns = $resultList->_columns;
149  $rows = array();
150  $null = null;
151 
152  $this->getSubrows($rows, $rowid, $null, $depth);
153 
154  ob_start();
155  foreach ( array_keys($rows) as $curr_rowid ){
156  $path = explode('-',$curr_rowid);
157  $level = count($path);
158  $class = ( $rows[$curr_rowid]['hasChildren'] ? 'folder':'doc');
159  echo "<tr id=\"$curr_rowid\">";
160 
161  $keyString = implode('-',$rows[$curr_rowid]['record']->getValuesAsStrings(
162  array_keys($rows[$curr_rowid]['record']->_table->keys())
163  )
164  );
165  echo "<td class=\"$rowClass\"><input id=\"remove_".$keyString."_checkbox\" type=\"checkbox\" name=\"--remkeys[]\" value=\"".$keyString."\"/></td>";
166  echo "
167  <td><div class=\"tier{$level}\"><a href=\"#\" ";
168  if ( $class == 'folder'){
169  echo "onclick=\"TreeTable.prototype.trees['$treetableid'].toggleRows(this)\" ";
170  }
171  $url = $rows[$curr_rowid]['record']->getURL();
172  $editURL = $rows[$curr_rowid]['record']->getURL('-action=edit');
173  $deleteURL = $rows[$curr_rowid]['record']->getURL('-action=delete');
174  echo "class=\"$class\"></a></td>
175  <td><a href=\"$url\">".$rows[$curr_rowid]['record']->getTitle()."</a></td>";
176 
177  foreach ( $columns as $col ){
178  echo "<td>";
179  if ( $col == $default_order_column ){
180  // If this is the order column, we will provide links to move the record up or down in order.
181 
182  if ( $path[count($path)-1] !== '1' ){
183  echo "<a href=\"#\" onclick=\"moveUp(".(intval($path[count($path)-1])-1).")\" title=\"Move up\"><img src=\"".DATAFACE_URL."/images/arrowUp.gif\"/></a>";
184  }
185  //if ( $i != $this->_start+$limit-1 ){
186  echo "
187  <a href=\"#\" onclick=\"moveDown(".(intval($path[count($path)-1])-1).")\" title=\"Move down\"><img src=\"".DATAFACE_URL."/images/arrowDown.gif\"/></a>";
188  //}
189  //echo "</td>
190  // ";
191 
192  } else {
193  if ( $rows[$curr_rowid]['record']->_table->hasField($col) ){
194 
195  echo "<a href=\"$url\">".$rows[$curr_rowid]['record']->htmlValue($col)."</a>";
196  } else {
197  echo '--';
198  }
199  }
200 
201  echo "
202  </td>";
203  }
204  echo "
205  </tr>
206  ";
207 
208  }
209  $out = ob_get_contents();
210  ob_end_clean();
211  return $out;
212  }
213 
217  function toHtml($depth=3, $treetableid='treetable'){
219  if ( isset($this->relationship) ){
220  $rel =& $this->record->_table->getRelationship($this->relationship);
221  $table =$rel->getDomainTable();
222  if ( PEAR::isError($table) ){
223  $destTables =& $rel->getDestinationTables();
224  $table = $destTables[0]->tablename;
225  }
226  } else {
227  $table = $this->record->_table->tablename;
228  $rel =& $this->record->_table->getChildrenRelationship();
229  }
230 
231  //echo "Def order col = $default_order_column";
232  //ob_start();
233 
234  //$moveUpForm = ob_get_contents();
235  //ob_end_clean();
236 
237  $resultList = new Dataface_ResultList($table);
238  $columns = $resultList->_columns;
239  ob_start();
240  $default_order_column = $rel->getOrderColumn();
241  if ( isset($default_order_column) ){
242  //echo "<script language=\"javascript\" type=\"text/javascript\"><!--";
243  df_display(array('redirectUrl'=>$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']), 'Dataface_MoveUpForm.html');
244  //echo "//--></script>";
245  }
246 
247  if ( !defined('Dataface_TreeTable_JAVASCRIPT_LOADED') ){
248  define('Dataface_TreeTable_JAVASCRIPT_LOADED',true);
249  echo '<script language="javascript" type="text/javascript" src="'.DATAFACE_URL.'/js/TreeTable.js"></script>';
250  }
251  echo '<form action="'.$_SERVER['PHP_SELF'].'" method="GET" onsubmit="return validateTTForm(this);">';
252  echo "<table width=\"100%\" id=\"$treetableid\" class=\"treetable\">";
253 
254  echo "<thead><tr><th><!-- checkbox column --></th><th><!-- Icon column --></th><th>Title</th>";
255  foreach ($columns as $col){
256  echo "<th>$col</th>";
257  }
258  echo "</tr></thead><tbody>";
259  echo $this->getSubrowsAsHTML('',$depth,$treetableid);
260 
261  echo "</tbody></table>";
262  import('Dataface/ActionTool.php');
263  $actionsTool =& Dataface_ActionTool::getInstance();
264  $actions = $actionsTool->getActions(array('category'=>'selected_records_actions'));
265  if (count($actions)>0 ){
266  echo " Perform on selected records:
267  <select name=\"-action\">";
268  foreach (array_keys($actions) as $i){
269  echo "<option value=\"".$actions[$i]['name']."\">".$actions[$i]['label']."</option>
270  ";
271  }
272 
273 
274  echo "
275 
276  </select>
277 
278  ";
279  echo "<input type=\"submit\" value=\"Submit\"/>";
280  }
281 
282  import('Dataface/Utilities.php');
283 
284  // We need to build a query.
285  $q = array('-table'=>$this->record->_table->tablename);
286  foreach ( array_keys($this->record->_table->keys()) as $tkey){
287  $q['--__keys__'][$tkey] = '='.$this->record->strval($tkey);
288  }
289  $q['-relationship'] = $this->relationship;
290 
291  echo Dataface_Utilities::query2html($q, array('-action'));
292  echo '<input type="hidden" name="-redirect" value="'.$_SERVER['REQUEST_URI'].'"/>';
293  echo "</form>";
294  echo "
295  <script language=\"javascript\" type=\"text/javascript \"><!--
296  TreeTable.prototype.trees['$treetableid'] = new TreeTable('$treetableid','');
297  //--></script>
298  ";
299  $out = ob_get_contents();
300  ob_end_clean();
301  return $out;
302  }
303 
304 }