Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
LinkTool.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 import( 'Dataface/Application.php');
22 import( 'Dataface/Table.php');
23 
24 
36 
37  public static function &getMask(){
38  static $mask = -1;
39 
40  if ( $mask == -1 ){
42  $query =& $app->getQuery();
43  $table =& Dataface_Table::loadTable($query['-table']);
44  $mask = $_GET;
45  //echo "GET: "; print_r($_GET);
46  foreach ( $query as $key=>$value){
47  //if ( strpos($key,'--')!== 0 ){
48  if ( isset($table->_fields[$key]) or ($key{0} == '-' and $key != '-new')){
49  //echo "Key $key";
50  $mask[$key] = $value;
51 
52  }
53  }
54  //print_r($mask);
55  }
56 
57  return $mask;
58  }
59 
60 
61  public static function buildSetLink($query, $useContext=true, $forceContext=false){
62 
63  return Dataface_LinkTool::buildLink($query, $useContext, $forceContext, true);
64  }
65 
71  public static function buildLink($query, $useContext=true, $forceContext=false, $stripRecordId=false){
73  $appQuery =& $app->getQuery();
74 
75  if ( $stripRecordId and isset($query['-recordid']) ) unset($query['-recordid']);
76 
77  if ( is_string($query) ){
78  $terms = explode('&', $query);
79  $query = array();
80  foreach ( $terms as $term){
81  $key = urldecode(substr($term, 0, strpos($term,'=')));
82  $value = urldecode(substr($term, strpos($term,'=')+1));
83  if ( strlen($value) == 0 ){
84  $query[$key] = null;
85  } else {
86  $query[$key] = $value;
87  }
88  }
89 
90  }
91 
92  if ( !isset($query['-table']) ) $query['-table'] = $appQuery['-table'];
93 
94  if ( !$forceContext and $useContext ){
95  // We check if the query parameters have changed. If they have, then it doesn't
96  // make a whole lot of sense to maintain context.
97  foreach ( $query as $key=>$val) {
98  if ( !$key ) continue;
99  if ( $key{0} != '-' and $query[$key] != @$appQuery[$key] ){
100  $useContext = false;
101  break;
102  }
103  }
104  }
105 
106  if ( $useContext){
107  $request = Dataface_LinkTool::getMask();
108  if ( $stripRecordId and isset($request['-recordid']) ) unset($request['-recordid']);
109 
110  if ( isset( $query['-relationship'] ) ){
111  if ( $query['-relationship'] != @$appQuery['-relationship'] ){
112  foreach ( $request as $qkey=>$qval ){
113  if ( strstr($qkey, '-related:') == $qkey ) unset($request[$qkey]);
114  }
115  }
116  }
117 
118  if ( isset($request['-sort']) and $request['-table'] != $appQuery['-table'] ){
119  unset($request['-sort']);
120  }
121 
122  //print_r($query);
123  $query = array_merge($request, $query);
124  }
125 
126  if ( !isset($query['-search']) ) $query['-search'] = null;
127  if ( isset( $_REQUEST['-search'] ) and strlen($_REQUEST['-search'])>0 and $query['-search'] !== null ){
128  $query['-search'] = $_REQUEST['-search'];
129  }
130 
131  foreach ($query as $key=>$value) {
132  if ( $value === null || strpos($key, '--') === 0 ){
133  unset($query[$key]);
134  }
135  }
136 
137  $str = '';
138  foreach ($query as $key=>$value) {
139 
140  if ( is_array($value) ){
141 
142  foreach ( $value as $vkey=>$vval ){
143  $str .= urlencode($key.'['.$vkey.']').'='.urlencode($vval).'&';
144  }
145  }
146  else {
147  $str .= urlencode($key).'='.urlencode($value).'&';
148  }
149  }
150  $str = substr($str,0, strlen($str)-1);
151 
152 
153  $url = DATAFACE_SITE_HREF;
154  if ( strpos('?', $url) !== false ){
155  $url .= '&'.$str;
156  } else {
157  $url .= '?'.$str;
158  }
159 
160  $url = $app->filterUrl($url);
161  return df_absolute_url($url);
162  }
163 
164 
165  public static function &getInstance(){
166  static $instance = 0;
167  if ( !$instance ){
168  $instance = new Dataface_LinkTool();
169  }
170  return $instance;
171  }
172 
173 }