Xataface 2.0
Xataface Application Framework
Dataface/LinkTool.php
Go to the documentation of this file.
00001 <?php
00002 /*-------------------------------------------------------------------------------
00003  * Xataface Web Application Framework
00004  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *-------------------------------------------------------------------------------
00020  */
00021 import( 'Dataface/Application.php');
00022 import( 'Dataface/Table.php');
00023 
00024 
00035 class Dataface_LinkTool {
00036 
00037         public static function &getMask(){
00038                 static $mask = -1;
00039 
00040                 if ( $mask == -1 ){
00041                         $app =& Dataface_Application::getInstance();
00042                         $query =& $app->getQuery();
00043                         $table =& Dataface_Table::loadTable($query['-table']);
00044                         $mask = $_GET;
00045                         //echo "GET: "; print_r($_GET);
00046                         foreach ( $query as $key=>$value){
00047                                 //if ( strpos($key,'--')!== 0 ){
00048                                 if ( isset($table->_fields[$key]) or ($key{0} == '-' and $key != '-new')){
00049                                         //echo "Key $key";
00050                                         $mask[$key] = $value;
00051                                         
00052                                 }
00053                         }
00054                         //print_r($mask);
00055                 }
00056         
00057                 return $mask;
00058         }
00059         
00060         
00061         public static function buildSetLink($query, $useContext=true, $forceContext=false){
00062         
00063                 return Dataface_LinkTool::buildLink($query, $useContext, $forceContext, true);
00064         }
00065 
00071         public static function buildLink($query, $useContext=true, $forceContext=false, $stripRecordId=false){
00072                 $app =& Dataface_Application::getInstance();
00073                 $appQuery =& $app->getQuery();
00074                 
00075                 if ( $stripRecordId and isset($query['-recordid']) ) unset($query['-recordid']);
00076                 
00077                 if ( is_string($query) ){
00078                         $terms = explode('&', $query);
00079                         $query = array();
00080                         foreach ( $terms as $term){
00081                                 $key = urldecode(substr($term, 0, strpos($term,'=')));
00082                                 $value = urldecode(substr($term, strpos($term,'=')+1));
00083                                 if ( strlen($value) == 0 ){
00084                                         $query[$key] = null;
00085                                 } else {
00086                                         $query[$key] = $value;
00087                                 }
00088                         }
00089                 
00090                 }
00091                 
00092                 if ( !isset($query['-table']) ) $query['-table'] = $appQuery['-table'];
00093                 
00094                 if ( !$forceContext and $useContext ){
00095                         // We check if the query parameters have changed.  If they have, then it doesn't
00096                         // make a whole lot of sense to maintain context.
00097                         foreach ( $query as $key=>$val) {
00098                                 if ( !$key ) continue;
00099                                 if ( $key{0} != '-' and $query[$key] != @$appQuery[$key] ){
00100                                         $useContext = false;
00101                                         break;
00102                                 }
00103                         }
00104                 }
00105                 
00106                 if ( $useContext){
00107                         $request = Dataface_LinkTool::getMask();
00108                         if ( $stripRecordId and isset($request['-recordid']) ) unset($request['-recordid']);
00109                         
00110                         if ( isset( $query['-relationship'] ) ){
00111                                 if ( $query['-relationship'] != @$appQuery['-relationship'] ){
00112                                         foreach ( $request as $qkey=>$qval ){
00113                                                 if ( strstr($qkey, '-related:') == $qkey ) unset($request[$qkey]);
00114                                         }
00115                                 }
00116                         }
00117                         
00118                         if ( isset($request['-sort']) and $request['-table'] != $appQuery['-table'] ){
00119                                 unset($request['-sort']);
00120                         }
00121                         
00122                         //print_r($query);
00123                         $query = array_merge($request, $query);
00124                 }
00125                 
00126                 if ( !isset($query['-search']) ) $query['-search'] = null;
00127                 if ( isset( $_REQUEST['-search'] ) and strlen($_REQUEST['-search'])>0 and $query['-search'] !== null  ){
00128                         $query['-search'] = $_REQUEST['-search'];
00129                 }
00130                 
00131                 foreach ($query as $key=>$value) {
00132                         if ( $value === null || strpos($key, '--') === 0 ){
00133                                 unset($query[$key]);
00134                         }
00135                 }
00136                 
00137                 $str = '';
00138                 foreach ($query as $key=>$value) {
00139                         
00140                         if ( is_array($value) ){
00141                                 
00142                                 foreach ( $value as $vkey=>$vval ){
00143                                         $str .= urlencode($key.'['.$vkey.']').'='.urlencode($vval).'&';
00144                                 }
00145                         }
00146                         else {
00147                                 $str .= urlencode($key).'='.urlencode($value).'&';
00148                         }
00149                 }
00150                 $str = substr($str,0, strlen($str)-1);
00151    
00152                 
00153                 $url = DATAFACE_SITE_HREF;
00154                 if ( strpos('?', $url) !== false ){
00155                         $url .= '&'.$str;
00156                 } else {
00157                         $url .= '?'.$str;
00158                 }
00159 
00160                 $url = $app->filterUrl($url);
00161                 return df_absolute_url($url);
00162         }
00163         
00164         
00165         public static function &getInstance(){
00166                 static $instance = 0;
00167                 if ( !$instance ){
00168                         $instance = new Dataface_LinkTool();
00169                 }
00170                 return $instance;
00171         }
00172 
00173 }
All Data Structures Namespaces Files Functions Variables Enumerations