Xataface 2.0
Xataface Application Framework
Dataface/TreeTable.php
Go to the documentation of this file.
00001 <?php
00002 import('Dataface/ResultList.php');
00009 class Dataface_TreeTable {
00010 
00014         var $record;
00015         
00020         var $relationship;
00021 
00022         
00028         function Dataface_TreeTable(&$record, $relationship=null){
00029                 $this->record =& $record;
00030                 $this->relationship = $relationship;
00031         }
00032         
00040         function &getRecordByRowId($rowid){
00041                 $path = explode('-', $rowid);
00042                 if ( empty($path[0]) ) array_shift($path);
00043                 
00044                 if ( empty($path) ) return $this->record;
00045                 
00046                 $root =& $this->record;
00047                 
00048                 if ( isset($this->relationship) ){
00049                         // first level comes from relationship
00050                         $index = intval(array_shift($path));
00051                         $record =& $this->record->getRelatedRecord($this->relationship, $index-1);
00052                         if ( !isset($record) ) {
00053                                 $null = null;
00054                                 return $null;
00055                         }
00056                         unset($root);
00057                         $root =& $record->toRecord();
00058                         unset($record);
00059                 }
00060                 
00061                 while ( !empty($path) ){
00062                         $currid = array_shift($path);
00063                         $record =& $root->getChild(intval($currid)-1);
00064                         if ( !isset($record) ){
00065                                 $null = null; return $null;
00066                         }
00067                         unset($root);
00068                         $root =& $record;
00069                         unset($record);
00070                 }
00071                 
00072                 return $root;
00073         }
00074         
00086         function &getSubrows(&$rows, $rowid, &$record, $depth=3){
00087                 if ( $depth == 0 ) return $rows;
00088                 if ( isset($record) ) $root =& $record;
00089                 else {
00090                         $root =& $this->getRecordByRowId($rowid);
00091                         if ( !isset($root ) ) return $rows;
00092                 }
00093                 if ( empty($rowid) and isset($this->relationship) ){
00094                         // we are starting from the root and the first level should from the given relationship.
00095                         $it = $root->getRelationshipIterator($this->relationship);
00096                         $i = 1;
00097                         while ( $it->hasNext() ){
00098                                 $curr_rowid = strval($i++);
00099                                 $row =& $it->next();
00100                                 $rowRecord =& $row->toRecord();
00101                                 $rows[$curr_rowid] = array('record'=>& $rowRecord, 'rowid'=>$curr_rowid, 'hasChildren'=>false);
00102                                 $numrows = count($rows);
00103                                 $this->getSubrows($rows, $curr_rowid, $rowRecord, $depth-1);
00104                                 if ( count($rows)>$numrows) $rows[$curr_rowid]['hasChildren'] = true;
00105                                 unset($rowRecord);unset($row);
00106                         }
00107                 } else {
00108                         $children = $root->getChildren();
00109                         if ( isset($children) ){
00110                                 $i=1;
00111                                 foreach (array_keys($children) as $childkey){
00112                                         $curr_rowid = $rowid.(!empty($rowid)?'-':'').strval($i++);
00113                                         $rowRecord =& $children[$childkey];
00114                                         $rows[$curr_rowid] = array('record'=>&$rowRecord, 'rowid'=>$curr_rowid, 'hasChildren'=>false);
00115                                         $numrows = count($rows);
00116                                         $this->getSubrows($rows, $curr_rowid, $rowRecord, $depth-1);
00117                                         if ( count($rows)>$numrows) $rows[$curr_rowid]['hasChildren'] = true;
00118                                         unset($rowRecord);
00119                                 }
00120                         }
00121                 }
00122                 return $rows;
00123         }
00124         
00132         function getSubrowsAsHTML($rowid, $depth=3, $treetableid='treetable'){
00133                 if ( isset($this->relationship) ){
00134                         $rel =& $this->record->_table->getRelationship($this->relationship);
00135                         $table =$rel->getDomainTable();
00136                         if ( PEAR::isError($table) ){
00137                                 $destTables =& $rel->getDestinationTables();
00138                                 $table = $destTables[0]->tablename;
00139                         }
00140                 } else {
00141                         $table = $this->record->_table->tablename;
00142                         $rel =& $this->record->_table->getChildrenRelationship();
00143                 }
00144                 
00145                 $default_order_column = $rel->getOrderColumn();
00146                 
00147                 $resultList = new Dataface_ResultList($table);
00148                 $columns = $resultList->_columns;
00149                 $rows = array();
00150                 $null = null;
00151                 
00152                 $this->getSubrows($rows, $rowid, $null, $depth);
00153 
00154                 ob_start();
00155                 foreach ( array_keys($rows) as $curr_rowid ){
00156                         $path = explode('-',$curr_rowid);
00157                         $level = count($path);
00158                         $class = ( $rows[$curr_rowid]['hasChildren'] ? 'folder':'doc');
00159                         echo "<tr id=\"$curr_rowid\">";
00160                         
00161                         $keyString = implode('-',$rows[$curr_rowid]['record']->getValuesAsStrings(
00162                                         array_keys($rows[$curr_rowid]['record']->_table->keys())
00163                                 )
00164                         );
00165                         echo "<td class=\"$rowClass\"><input id=\"remove_".$keyString."_checkbox\" type=\"checkbox\" name=\"--remkeys[]\" value=\"".$keyString."\"/></td>";
00166                         echo "
00167                                 <td><div class=\"tier{$level}\"><a href=\"#\" ";
00168                         if ( $class == 'folder'){
00169                                 echo "onclick=\"TreeTable.prototype.trees['$treetableid'].toggleRows(this)\" ";
00170                         } 
00171                         $url = $rows[$curr_rowid]['record']->getURL();
00172                         $editURL = $rows[$curr_rowid]['record']->getURL('-action=edit');
00173                         $deleteURL = $rows[$curr_rowid]['record']->getURL('-action=delete');
00174                         echo "class=\"$class\"></a></td>
00175                                 <td><a href=\"$url\">".$rows[$curr_rowid]['record']->getTitle()."</a></td>";
00176 
00177                         foreach ( $columns as $col ){
00178                                 echo "<td>";
00179                                 if ( $col == $default_order_column ){
00180                                 // If this is the order column, we will provide links to move the record up or down in order.
00181                                                 
00182                                                 if ( $path[count($path)-1] !== '1' ){
00183                                                         echo "<a href=\"#\" onclick=\"moveUp(".(intval($path[count($path)-1])-1).")\" title=\"Move up\"><img src=\"".DATAFACE_URL."/images/arrowUp.gif\"/></a>";
00184                                                 }
00185                                                 //if ( $i != $this->_start+$limit-1 ){
00186                                                         echo "
00187                                                                 <a href=\"#\" onclick=\"moveDown(".(intval($path[count($path)-1])-1).")\" title=\"Move down\"><img src=\"".DATAFACE_URL."/images/arrowDown.gif\"/></a>";
00188                                                 //}
00189                                                 //echo "</td>
00190                                                 //              ";
00191                                 
00192                                 } else {
00193                                         if ( $rows[$curr_rowid]['record']->_table->hasField($col) ){
00194                                                 
00195                                                 echo "<a href=\"$url\">".$rows[$curr_rowid]['record']->htmlValue($col)."</a>";
00196                                         } else {
00197                                                 echo '--';
00198                                         }
00199                                 }
00200                                         
00201                                 echo "
00202                                 </td>";
00203                         }
00204                         echo "
00205                         </tr>
00206                         ";
00207                         
00208                 }
00209                 $out = ob_get_contents();
00210                 ob_end_clean();
00211                 return $out;
00212         }
00213         
00217         function toHtml($depth=3, $treetableid='treetable'){
00218                 $app =& Dataface_Application::getInstance();
00219                 if ( isset($this->relationship) ){
00220                         $rel =& $this->record->_table->getRelationship($this->relationship);
00221                         $table =$rel->getDomainTable();
00222                         if ( PEAR::isError($table) ){
00223                                 $destTables =& $rel->getDestinationTables();
00224                                 $table = $destTables[0]->tablename;
00225                         }
00226                 } else {
00227                         $table = $this->record->_table->tablename;
00228                         $rel =& $this->record->_table->getChildrenRelationship();
00229                 }
00230         
00231                 //echo "Def order col = $default_order_column";
00232                 //ob_start();
00233                 
00234                 //$moveUpForm = ob_get_contents();
00235                 //ob_end_clean();
00236                 
00237                 $resultList = new Dataface_ResultList($table);
00238                 $columns = $resultList->_columns;
00239                 ob_start();
00240                 $default_order_column = $rel->getOrderColumn();
00241                 if ( isset($default_order_column) ){
00242                         //echo "<script language=\"javascript\" type=\"text/javascript\"><!--";
00243                         df_display(array('redirectUrl'=>$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']), 'Dataface_MoveUpForm.html');
00244                         //echo "//--></script>";
00245                 }
00246                 
00247                 if ( !defined('Dataface_TreeTable_JAVASCRIPT_LOADED') ){
00248                         define('Dataface_TreeTable_JAVASCRIPT_LOADED',true);
00249                         echo '<script language="javascript" type="text/javascript" src="'.DATAFACE_URL.'/js/TreeTable.js"></script>';
00250                 }
00251                 echo '<form action="'.$_SERVER['PHP_SELF'].'" method="GET" onsubmit="return validateTTForm(this);">';
00252                 echo "<table width=\"100%\" id=\"$treetableid\" class=\"treetable\">";
00253                 
00254                 echo "<thead><tr><th><!-- checkbox column --></th><th><!-- Icon column --></th><th>Title</th>";
00255                 foreach ($columns as $col){
00256                         echo "<th>$col</th>";
00257                 }
00258                 echo "</tr></thead><tbody>";
00259                 echo $this->getSubrowsAsHTML('',$depth,$treetableid);
00260                 
00261                 echo "</tbody></table>";
00262                 import('Dataface/ActionTool.php');
00263                 $actionsTool =& Dataface_ActionTool::getInstance();
00264                 $actions = $actionsTool->getActions(array('category'=>'selected_records_actions'));
00265                 if (count($actions)>0 ){
00266                         echo "   Perform on selected records:
00267                         <select name=\"-action\">";
00268                         foreach (array_keys($actions) as $i){
00269                                 echo "<option value=\"".$actions[$i]['name']."\">".$actions[$i]['label']."</option>
00270                                 ";
00271                         }
00272                 
00273                 
00274                         echo "
00275                         
00276                         </select>
00277                         
00278                         ";
00279                         echo "<input type=\"submit\" value=\"Submit\"/>";
00280                 }
00281                 
00282                 import('Dataface/Utilities.php');
00283                 
00284                 // We need to build a query.
00285                 $q = array('-table'=>$this->record->_table->tablename);
00286                 foreach ( array_keys($this->record->_table->keys()) as $tkey){
00287                         $q['--__keys__'][$tkey] = '='.$this->record->strval($tkey);
00288                 }
00289                 $q['-relationship'] = $this->relationship;
00290                 
00291                 echo Dataface_Utilities::query2html($q, array('-action'));
00292                 echo '<input type="hidden" name="-redirect" value="'.$_SERVER['REQUEST_URI'].'"/>';
00293                 echo "</form>";
00294                 echo "
00295                 <script language=\"javascript\" type=\"text/javascript  \"><!--
00296                         TreeTable.prototype.trees['$treetableid'] = new TreeTable('$treetableid','');
00297                 //--></script>
00298                 ";
00299                 $out = ob_get_contents();
00300                 ob_end_clean();
00301                 return $out;
00302         }
00303 
00304 }
 All Data Structures Namespaces Files Functions Variables Enumerations