![]() |
Xataface 2.0
Xataface Application Framework
|
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( 'HTML/QuickForm.php'); 00022 import('Smarty/Smarty.class.php'); 00023 import('Dataface/Globals.php'); 00024 import('Dataface/QueryBuilder.php'); 00025 00026 00030 class Dataface_DeleteForm extends HTML_QuickForm { 00031 00032 var $_tablename; 00033 var $_db; 00034 var $_query; 00035 var $_table; 00036 var $_isBuilt; 00037 00038 00039 function Dataface_DeleteForm( $tablename, $db, $query){ 00040 $this->_tablename = $tablename; 00041 $this->_table =& Dataface_Table::loadTable($tablename); 00042 $this->_db = $db; 00043 $this->_query = $query; 00044 $this->_isBuilt = false; 00045 00046 if ( !isset( $this->_query['-cursor'] ) ){ 00047 $this->_query['-cursor'] = 0; 00048 } 00049 00050 parent::HTML_QuickForm('deleteForm'); 00051 00052 00053 } 00054 00055 function _build(){ 00056 00057 if ( !$this->_isBuilt ){ 00058 $b = new Dataface_QueryBuilder($this->_tablename, $this->_query); 00059 00060 $this->addElement('hidden','-action'); 00061 $this->setConstants( array( "-action"=>'delete') ); 00062 $this->_isBuilt = true; 00063 if ( isset( $this->_query['-delete-one'] ) ){ 00064 $this->addElement('hidden','-delete-one'); 00065 $this->setDefaults( array( '-delete-one'=>1) ); 00066 $q = array('-skip'=>$this->_query['-cursor'], '-limit'=>1); 00067 $sql = $b->select('', $q); 00068 $res = mysql_query($sql, $this->_db); 00069 if ( !$res ){ 00070 throw new Exception( df_translate('scripts.Dataface.DeleteForm._build.ERROR_TRYING_TO_FETCH',"Error trying to fetch element to be deleted.: ").mysql_error($this->_db), E_USER_ERROR); 00071 00072 } 00073 if ( mysql_num_rows($res)==0 ) { 00074 throw new Exception( df_translate('scripts.Dataface.DeleteForm._build.ERROR_NO_RECORD_SELECTED',"No record is currently selected so no record can be deleted."), E_USER_ERROR); 00075 00076 } else { 00077 $row = mysql_fetch_array($res); 00078 $fields =& $this->_table->keys(); 00079 00080 $keys = array_keys($fields); 00081 00082 00083 00084 foreach ($keys as $key){ 00085 $this->addElement('hidden',$key); 00086 $this->setDefaults(array( $key=>$row[$key]) ); 00087 00088 } 00089 } 00090 } else { 00091 00092 00093 foreach ($this->_query as $key=>$value){ 00094 $this->addElement('hidden', $key); 00095 $this->setConstants(array($key=>$value)); 00096 } 00097 } 00098 $this->removeElement('-submit'); 00099 $this->addElement('submit','-submit',df_translate('scripts.Dataface.DeleteForm._build.LABEL_DELETE','Delete'), array('id'=>'delete_submit_button')); 00100 00101 $this->addFormRule(array(&$this, 'checkPermissions')); 00102 } 00103 00104 00105 } 00106 00107 function display(){ 00108 $this->_build(); 00109 $showform = true; 00110 $b = new Dataface_QueryBuilder($this->_tablename, $this->_query); 00111 if ( isset( $this->_query['-delete-one'] ) ){ 00112 $q = array('-skip'=>$this->_query['-cursor'], '-limit'=>1); 00113 $sql = $b->select('', $q); 00114 $res = mysql_query($sql, $this->_db); 00115 if ( !$res ){ 00116 throw new Exception( df_translate('scripts.Dataface.DeleteForm._build.ERROR_TRYING_TO_FETCH',"Error trying to fetch element to be deleted.: ").mysql_error($this->_db), E_USER_ERROR); 00117 00118 } 00119 if ( mysql_num_rows($res)==0 ) { 00120 $msg = df_translate('scripts.Dataface.DeleteForm._build.ERROR_NO_RECORD_SELECTED',"No record is currently selected so no record can be deleted."); 00121 $showform = false; 00122 } else { 00123 $row = mysql_fetch_array($res); 00124 $rowRec = new Dataface_Record($this->_tablename, $row); 00125 $displayCol = $rowRec->getTitle(); 00126 00127 $msg = df_translate('scripts.Dataface.DeleteForm.display.ARE_YOU_SURE',"Are you sure you want to delete this record: "$displayCol"?",array('displayCol'=>$displayCol)); 00128 } 00129 00130 } else if ( isset($this->_query['-delete-found']) ) { 00131 $q = $b->select_num_rows(); 00132 $res = mysql_query($q, $this->_db); 00133 if ( !$res ){ 00134 throw new Exception( df_translate('scripts.Dataface.DeleteForm.display.ERROR_ESTIMATING',"Error estimating number of rows that will be deleted: "). mysql_error($this->_db), E_USER_ERROR); 00135 00136 } 00137 00138 list( $num ) = mysql_fetch_row($res); 00139 if ( $num <= 0 ){ 00140 $msg = df_translate('scripts.Dataface.DeleteForm.display.ERROR_NO_RECORDS_FOUND',"There are no records in the current found set so no records can be deleted."); 00141 $showform = false; 00142 } else { 00143 $msg = df_translate('scripts.Dataface.DeleteForm.display.ARE_YOU_SURE_MULTIPLE',"Are you sure you want to delete the found records. $num records will be deleted.",array('num'=>$num)); 00144 } 00145 } else { 00146 $msg = df_translate('scripts.Dataface.DeleteForm.display.ERROR_GET_VARS',"Error: You must specify either '-delete-one' or '-delete-found' in GET vars."); 00147 $showform=false; 00148 } 00149 00150 if ( $showform ){ 00151 ob_start(); 00152 parent::display(); 00153 $form = ob_get_contents(); 00154 ob_end_clean(); 00155 } else { 00156 $form = ''; 00157 } 00158 00159 $context = array('msg'=>'foo'.$msg, 'form'=>$form); 00160 import('Dataface/SkinTool.php'); 00161 $skinTool =& Dataface_SkinTool::getInstance(); 00162 //$smarty = new Smarty; 00163 //$smarty->template_dir = $GLOBALS['Dataface_Globals_Templates']; 00164 //$smarty->compile_dir = $GLOBALS['Dataface_Globals_Templates_c']; 00165 //$smarty->assign($context); 00166 //$smarty->display('Dataface_DeleteForm.html'); 00167 $skinTool->display($context, 'Dataface_DeleteForm.html'); 00168 00169 00170 00171 } 00172 00173 function delete($values){ 00174 require_once 'Dataface/IO.php'; 00175 00176 $query = $this->_buildDeleteQuery($values); 00177 if ( PEAR::isError($query) ) return $query; 00178 $io = new Dataface_IO($this->_tablename); 00179 00180 $it =& df_get_records($this->_tablename, $query); 00181 $warnings = array(); 00182 while ( $it->hasNext() ){ 00183 $record =& $it->next(); 00184 $res = $io->delete($record); 00185 if ( PEAR::isError($res) && Dataface_Error::isError($res) ){ 00186 // this is a serious error... kill it 00187 return $res; 00188 } else if ( PEAR::isError($res) ){ 00189 // this is a warning or a notice 00190 $warnings[] = $res; 00191 } 00192 unset($record); 00193 } 00194 00195 if ( count($warnings) > 0 ){ 00196 return $warnings; 00197 } 00198 return true; 00199 00200 00201 } 00202 00206 function _buildDeleteQuery($values = array()){ 00207 $query = array(); 00208 if ( isset( $values['-delete-one']) ){ 00209 $keys = array_keys( $this->_table->keys() ); 00210 foreach ($keys as $key){ 00211 if ( !isset( $values[$key] ) ){ 00212 return PEAR::raiseError( 00213 Dataface_LanguageTool::translate( 00214 /* i18n id */ 00215 'Missing key while trying to delete record', 00216 /* default error message */ 00217 'Attempt to delete single record when not all keys were specified. Missing key \''.$key.'\'', 00218 /* i18n parameters */ 00219 array('key'=>$key) 00220 ), 00221 DATAFACE_E_MISSING_KEY 00222 ); 00223 } 00224 $val = $values[$key]; 00225 00226 if ( $val{0} != '=' ) $val = '='.$val; 00227 $query[$key] = $val; 00228 } 00229 } else { 00230 $query['-limit'] = 9999;//isset($values['-limit']) ? $values['-limit'] : 1; 00231 $query['-skip'] = 0;//isset($values['-skip']) ? $values['-skip'] : 0; 00232 if ( isset( $values['-search'] ) ){ 00233 $query['-search'] = $values['-search']; 00234 } 00235 if ( isset( $values['-sort'] ) ){ 00236 $query['-sort'] = $values['-sort']; 00237 } 00238 foreach ($values as $key=>$value){ 00239 if ( strpos($key, '-')===0 ) continue; 00240 $query[$key] = $value; 00241 } 00242 } 00243 00244 return $query; 00245 00246 } 00247 00251 function checkPermissions(){ 00252 $errors = array(); 00253 if ( $this->isSubmitted() ){ 00254 $errCounter = 1; 00255 import('Dataface/PermissionsTool.php'); 00256 import('dataface-public-api.php'); 00257 $query = $this->_buildDeleteQuery($this->exportValues()); 00258 if ( PEAR::isError($query) ){ 00259 $errors[$errCounter++] = $query->getMessage(); 00260 } 00261 $records =& df_get_records_array($this->_tablename, $query); 00262 if ( PEAR::isError($records) ){ 00263 $errors[$errCounter++] = $query->getMessage(); 00264 // we attach this error to the '-submit' field because I don't know how to attach it to the form. 00265 } 00266 if ( !is_array($records) ){ 00267 $errors[$errCounter++] = df_translate('scripts.Dataface.DeleteForm.display.ERROR_NO_RECORDS_FOUND',"No records matched the query, so no records can be deleted."); 00268 } 00269 else { 00270 foreach ( array_keys($records) as $index ){ 00271 if ( !Dataface_PermissionsTool::delete($records[$index]) ) { 00272 $errors[$errCounter++] = df_translate('scripts.Dataface.DeleteForm.checkPermissions.ERROR_PERMISSION_DENIED',"Permission Denied: You do not have permission to delete this record (".$records[$index]->getTitle().")",array('title'=>$records[$index]->getTitle())); 00273 // we attach this error to the '-submit' field because I don't know how to attach it to the form. 00274 } 00275 } 00276 } 00277 } 00278 if ( count($errors) > 0 ) { 00279 00280 return $errors; 00281 } 00282 return true; 00283 00284 00285 } 00286 00287 function validate(){ 00288 $this->_build(); 00289 return parent::validate(); 00290 } 00291 00292 00293 00294 00295 00296 }