![]() |
Xataface Depselect Module 0.1
Dependent Select List Widget for Xataface Forms
|
00001 <?php 00002 /* 00003 * Xataface Depselect Module 00004 * Copyright (C) 2011 Steve Hannah <steve@weblite.ca> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library 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 GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public 00017 * License along with this library; if not, write to the 00018 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 */ 00022 00078 class actions_depselect_load { 00079 00080 00086 static function errorMessage($fieldname){ 00087 00088 return "Failed to load values for field '".$fieldname."'. See server error log for details."; 00089 } 00090 00094 function handle($params){ 00095 00096 session_write_close(); 00097 header('Connection: close'); 00098 00099 $app = Dataface_Application::getInstance(); 00100 $query = $app->getQuery(); 00101 $table = Dataface_Table::loadTable($query['-table']); 00102 $field = $table->getField($query['-field']); 00103 //$table = Dataface_Table::loadTable($field['widget']['table']); 00104 $this->loadFromTable($table, $field, $query); 00105 00106 } 00107 00108 00119 private function loadFromTable(Dataface_Table $table, &$field, $query){ 00120 00121 try { 00122 00123 $perms = $table->getPermissions(array('field'=>$field['name'])); 00124 if ( !@$perms['edit'] and !@$perms['new'] ){ 00125 // The user doesn't have permission to edit the column... so we 00126 // need to cut off access right now!!! 00127 error_log("Insufficient permissions to access field $field[name] by current user."); 00128 throw new Exception("Failed to get options for field $filed[name] because you don't have view permission for this field.", 401); 00129 00130 } 00131 00132 00133 00134 00135 if ( !@$field['widget']['table'] ){ 00136 error_log("widget:table not defined for field ".$field['name']." of table ".$table->tablename."."); 00137 throw new Exception(self::errorMessage($fieldname), 500); 00138 } 00139 $targetTable = Dataface_Table::loadTable($field['widget']['table']); 00140 00141 $filters = array(); 00142 if ( @$field['widget']['filters'] and is_array($field['widget']['filters'])){ 00143 foreach ($field['widget']['filters'] as $key=>$val){ 00144 if ( isset($query[$key]) ){ 00145 $filters[$key]=$query[$key]; 00146 } else if ( strpos($val,'$') === 0 ){ 00147 $filters[$key] = '='; 00148 } else { 00149 $filters[$key] = $val; 00150 } 00151 } 00152 } 00153 00154 00155 00156 $limit = 250; 00157 if ( @$field['widget']['limit'] ){ 00158 $limit = intval($field['widget']['limit']); 00159 } 00160 00161 $filters['-limit'] = $limit; 00162 $records = df_get_records_array($field['widget']['table'], $filters); 00163 //if ( count($filters) > 1 ){ 00164 // echo "Num records found: ".count($records); 00165 // print_r($filters); 00166 //} 00167 $keyCol = null; 00168 $labelCol = null; 00169 00170 if ( @$field['widget']['keycol'] ){ 00171 $keyCol = $field['widget']['keycol']; 00172 } else { 00173 foreach ($targetTable->keys() as $k=>$v){ 00174 $keyCol = $k; 00175 break; 00176 } 00177 } 00178 00179 if ( @$field['widget']['labelcol'] ){ 00180 $labelCol = $field['widget']['labelcol']; 00181 } 00182 00183 $out = array(); 00184 foreach ($records as $r){ 00185 if ( @$field['widget']['ignore_permissions'] ){ 00186 $r->secureDisplay = false; 00187 } else { 00188 if ( !$r->checkPermission('view') ) continue; 00189 if ( !$r->checkPermission('view', array('field'=>$keyCol))) continue; 00190 if ( $labelCol and !$r->checkPermission('view', array('field'=>$labelCol))) continue; 00191 } 00192 00193 if ( $labelCol ){ 00194 $out[$r->val($keyCol)] = $r->display($labelCol); 00195 } else { 00196 $out[$r->val($keyCol)] = $r->getTitle(); 00197 } 00198 00199 } 00200 00201 $this->out(array( 00202 'code'=>200, 00203 'message'=>'Received default valuelist', 00204 'values'=>$out 00205 )); 00206 exit; 00207 00208 00209 } catch (Exception $ex){ 00210 00211 $this->out(array( 00212 'code'=>$ex->getCode(), 00213 'message'=>$ex->getMessage() 00214 )); 00215 exit; 00216 } 00217 00218 } 00219 00220 00224 private function out($params){ 00225 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"'); 00226 echo json_encode($params); 00227 } 00228 00229 00230 00231 }