Xataface Depselect Module  0.1
Dependent Select List Widget for Xataface Forms
 All Data Structures Files Functions Pages
depselect_load.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Xataface Depselect Module
4  * Copyright (C) 2011 Steve Hannah <steve@weblite.ca>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
79 
80 
86  static function errorMessage($fieldname){
87 
88  return "Failed to load values for field '".$fieldname."'. See server error log for details.";
89  }
90 
94  function handle($params){
95 
96  session_write_close();
97  header('Connection: close');
98 
99  $app = Dataface_Application::getInstance();
100  $query = $app->getQuery();
101  $tableName = $query['-table'];
102  if ( @$query['--depselect-table'] ){
103  $tableName = $query['--depselect-table'];
104  }
105  $table = Dataface_Table::loadTable($tableName);
106  $field = $table->getField($query['-field']);
107  //$table = Dataface_Table::loadTable($field['widget']['table']);
108  $this->loadFromTable($table, $field, $query);
109 
110  }
111 
112 
123  private function loadFromTable(Dataface_Table $table, &$field, $query){
124 
125  try {
126 
127  $perms = $table->getPermissions(array('field'=>$field['name']));
128  if ( !@$perms['edit'] and !@$perms['new'] ){
129  // The user doesn't have permission to edit the column... so we
130  // need to cut off access right now!!!
131  error_log("Insufficient permissions to access field $field[name] by current user.");
132  throw new Exception("Failed to get options for field $filed[name] because you don't have view permission for this field.", 401);
133 
134  }
135 
136 
137 
138 
139  if ( !@$field['widget']['table'] ){
140  error_log("widget:table not defined for field ".$field['name']." of table ".$table->tablename.".");
141  throw new Exception(self::errorMessage($fieldname), 500);
142  }
143  $targetTable = Dataface_Table::loadTable($field['widget']['table']);
144 
145  $filters = array();
146  if ( @$field['widget']['filters'] and is_array($field['widget']['filters'])){
147  foreach ($field['widget']['filters'] as $key=>$val){
148  if ( isset($query[$key]) ){
149  $filters[$key]=$query[$key];
150  } else if ( strpos($val,'$') === 0 ){
151  $filters[$key] = '=';
152  } else {
153  $filters[$key] = $val;
154  }
155  }
156  }
157 
158 
159 
160  $limit = 250;
161  if ( @$field['widget']['limit'] ){
162  $limit = intval($field['widget']['limit']);
163  }
164 
165  $filters['-limit'] = $limit;
166  $records = df_get_records_array($field['widget']['table'], $filters);
167  //if ( count($filters) > 1 ){
168  // echo "Num records found: ".count($records);
169  // print_r($filters);
170  //}
171  $keyCol = null;
172  $labelCol = null;
173 
174  if ( @$field['widget']['keycol'] ){
175  $keyCol = $field['widget']['keycol'];
176  } else {
177  foreach ($targetTable->keys() as $k=>$v){
178  $keyCol = $k;
179  break;
180  }
181  }
182 
183  if ( @$field['widget']['labelcol'] ){
184  $labelCol = $field['widget']['labelcol'];
185  }
186 
187  $out = array();
188  foreach ($records as $r){
189  if ( @$field['widget']['ignore_permissions'] ){
190  $r->secureDisplay = false;
191  } else {
192  //if ( !$r->checkPermission('view') ) continue;
193  if ( !$r->checkPermission('view', array('field'=>$keyCol))) continue;
194  if ( $labelCol and !$r->checkPermission('view', array('field'=>$labelCol))) continue;
195  }
196 
197  if ( $labelCol ){
198  $temp = array (
199  ($r->val($keyCol))=>($r->display($labelCol))
200  );
201 
202  } else {
203  $temp = array (
204  ($r->val($keyCol))=>($r->getTitle())
205  );
206  }
207  $out[] = $temp;
208 
209 
210  }
211 
212  $this->out(array(
213  'code'=>200,
214  'message'=>'Received default valuelist',
215  'values'=>$out
216  ));
217  exit;
218 
219 
220  } catch (Exception $ex){
221 
222  $this->out(array(
223  'code'=>$ex->getCode(),
224  'message'=>$ex->getMessage()
225  ));
226  exit;
227  }
228 
229  }
230 
231 
235  private function out($params){
236  header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
237  echo json_encode($params);
238  }
239 
240 
241 
242 }