Xataface Tagger Module 0.3
Tagging Widget & Tag Cloud component for Xataface
actions/tagger_autocomplete.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Xataface Tagger 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  */
00052 class actions_tagger_autocomplete {
00053 
00057         function handle($params){
00058                 session_write_close();
00059                 header('Connection: close');
00060                 try {
00061                         $app = Dataface_Application::getInstance();
00062                         
00063                         $query = $app->getQuery();
00064                         //print_r($query);exit;
00065                         if ( !$query['-field'] ){
00066                                 throw new Exception("No -field parameter found in tagger_autocomplete.");
00067                         }
00068                         
00069                         $table = Dataface_Table::loadTable($query['-table']);
00070                         
00071                         if ( PEAR::isError($table) ) throw new Exception($table->getMessage());
00072                         
00073                         
00074                         $field = $table->getField($query['-field']);
00075                         if ( PEAR::isError($field) ) throw new Exception($field->getMessage());
00076                         
00077                         
00078                         if ( !@$field['relationship'] ){
00079                                 throw new Exception("No relationship for field '".$field['name']."' of table '".$table->tablename."' to load autocomplete values for the tagger widget.");
00080                                 
00081                         }
00082                         
00083                         $rperms = array();
00084                         $fperms = array();
00085                         $record = null;
00086                         if ( @$query['-new'] ){
00087                                 $rperms = $table->getPermissions(array(
00088                                         'relationship'=>$field['relationship']
00089                                 ));
00090                                 $fperms = $table->getPermissions(array(
00091                                         'field'=>$field['name']
00092                                 ));
00093                         } else {
00094                                 $rec = df_get_record_by_id($query['-record-id']);
00095                                 $rperms = $rec->getPermissions(array(
00096                                         'relationship'=>$field['relationship']
00097                                 ));
00098                                 $fperms = $rec->getPermissions(array(
00099                                         'field'=>$field['name']
00100                                 ));
00101                                 $record = $rec;
00102                         
00103                         }
00104                         
00105                         if ( !@$rperms['add existing related record'] ){
00106                                 throw new Exception("Cannot obtain autocomplete information because you don't have permission to add existing records to this relationship.");
00107                                 
00108                         }
00109                         
00110                         if ( @$query['-new'] and !@$fperms['new'] ){
00111                                 throw new Exception('Cannot obtain autocomplete information because you don\'t have permission to input data into this field.');
00112                                 
00113                         }
00114                         
00115                         if ( !@$query['-new'] and !@$fperms['edit'] ){
00116                                 throw new Exception("Cannot obtain autocomplete information because you don't have permission to edit this field.");
00117                         }
00118                         
00119                         
00120                         
00121                         
00122                         
00123                         
00124                         
00125                         
00126                         if ( $field['widget']['type'] != 'tagger' ){
00127                                 throw new Exception("Attempt to get autocomplete values for the '".$field['name']."' field of the '".$table->tablename."' table but field is not set up to use the tagger widget.  Please set widget:type=tagger on this field.");
00128                                 
00129                         }
00130                         
00131                         $relationship = $table->getRelationship($field['relationship']);
00132                         if ( PEAR::isError($relationship) ){
00133                                 throw new Exception($relationship->getMessage());
00134                         }
00135                         
00136                         $domainTableName = $relationship->getDomainTable();
00137                         if ( PEAR::isError($domainTableName) ){
00138                                 throw new Exception($relationship->getMessage());
00139                         }
00140                         
00141                         $domainTable = Dataface_Table::loadTable($domainTableName);
00142                         if ( PEAR::isError($domainTable) ){
00143                                 throw new Exception($relationship->getMessage());
00144                         }
00145                         
00146                         $qt = Dataface_QueryTool::loadResult($domainTableName, null, array('-skip'=>0, '-limit'=>500));
00147                         $count = $qt->cardinality();
00148                         
00149                         
00150                         $labelCol = null;
00151                         if (  @$field['tagger_label']  ){
00152                                 $labelCol = $field['tagger_label'];
00153                         } else {
00154                                 
00155                                 $labelCol = $domainTable->guessField(
00156                                         array('varchar'=>10, 'char'=>8, 'enum'=>3, 'text'=>1),
00157                                         array('/name|title|value/'=>10, '/nom/'=>2)
00158                                 );
00159                                 
00160                         }
00161                         //echo $domainTable->tablename;
00162                         //print_r($domainTable->fields());
00163                         //exit;
00164                         if ( !$labelCol ){
00165                                 throw new Exception("No label column could be found for the field ".$field['name'].".  Please specify a tagger_label directive.");
00166                                 
00167                         }
00168                         
00169                         //echo $labelCol.'/';
00170                         //echo $domainTableName;exit;
00171                         
00172                         if ( $count < 500 ){
00173                                 // If there are less than 500, let's just load all of them
00174                                 //$qt->loadSet(array($labelCol));
00175                                 //$recs = $qt->getRecordsArray();
00176                                 $recs = df_get_records_array($domainTableName,array('-limit'=>500));
00177                                 $matches = array();
00178                                 foreach ($recs as $rec){
00179                                         $perms = array();
00180                                         if ( $record ){
00181                                                 $perms = $record->getPermissions(array(
00182                                                         'relationship'=>$field['relationship'],
00183                                                         'domain_record'=>$rec
00184                                                 ));
00185                                         } else {
00186                                                 $perms = $table->getPermissions(array(
00187                                                         'relationship'=>$field['relationship'],
00188                                                         'domain_record'=>$rec
00189                                                 ));
00190                                         }
00191                                         if ( @$perms['add existing related record'] ){
00192                                                 $matches[] = $rec->val($labelCol);
00193                                         } 
00194                                 }
00195                                 
00196                                 $this->out(array(
00197                                         'code'=>200,
00198                                         'message'=>'Successfully loaded records',
00199                                         'matches'=>$matches,
00200                                         'all'=>true
00201                                 ));
00202                                 exit;
00203                                 
00204                         } else {
00205                                 $recs = df_get_records_array($domainTableName, array($labelCol=>$query['-search']));
00206                                 if ( PEAR::isError($recs) ){
00207                                         throw new Exception($recs->getMessage());
00208                                 }
00209                                 $matches = array();
00210                                 foreach ($recs as $rec){
00211                                         $perms = array();
00212                                         if ( $record ){
00213                                                 $perms = $record->getPermissions(array(
00214                                                         'relationship'=>$field['relationship'],
00215                                                         'domain_record'=>$rec
00216                                                 ));
00217                                         } else {
00218                                                 $perms = $table->getPermissions(array(
00219                                                         'relationship'=>$field['relationship'],
00220                                                         'domain_record'=>$rec
00221                                                 ));
00222                                         }
00223                                         if ( @$perms['add existing related record'] ){
00224                                                 $matches[] = $rec->val($labelCol);
00225                                         } 
00226                                 }
00227                                 $this->out(array(
00228                                         'code'=>200,
00229                                         'message'=>'Successfully loaded records',
00230                                         'matches'=>$matches,
00231                                         'all'=>false
00232                                 ));
00233                                 exit;
00234                         }
00235                 } catch (Exception $ex){
00236                         $this->out(array(
00237                                 'code'=>$ex->getCode(),
00238                                 'message'=>$ex->getMessage()
00239                         ));
00240                         exit;
00241                 }
00242                 
00243                 
00244         }
00245         
00246         
00251         function out($params){
00252                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00253                 echo json_encode($params);      
00254         }
00255 }
 All Data Structures Files Functions