21 import(
'HTML/QuickForm.php');
22 import(
'Smarty/Smarty.class.php');
23 import(
'Dataface/Globals.php');
24 import(
'Dataface/QueryBuilder.php');
43 $this->_query = $query;
44 $this->_isBuilt =
false;
46 if ( !isset( $this->_query[
'-cursor'] ) ){
47 $this->_query[
'-cursor'] = 0;
50 parent::HTML_QuickForm(
'deleteForm');
57 if ( !$this->_isBuilt ){
60 $this->addElement(
'hidden',
'-action');
61 $this->setConstants( array(
"-action"=>
'delete') );
62 $this->_isBuilt =
true;
63 if ( isset( $this->_query[
'-delete-one'] ) ){
64 $this->addElement(
'hidden',
'-delete-one');
65 $this->setDefaults( array(
'-delete-one'=>1) );
66 $q = array(
'-skip'=>$this->_query[
'-cursor'],
'-limit'=>1);
67 $sql = $b->select(
'', $q);
68 $res = mysql_query($sql, $this->_db);
70 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);
73 if ( mysql_num_rows($res)==0 ) {
74 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);
77 $row = mysql_fetch_array($res);
78 $fields =& $this->_table->keys();
84 foreach (
$keys as $key){
85 $this->addElement(
'hidden',$key);
86 $this->setDefaults(array( $key=>$row[$key]) );
93 foreach ($this->_query as $key=>$value){
94 $this->addElement(
'hidden', $key);
95 $this->setConstants(array($key=>$value));
98 $this->removeElement(
'-submit');
99 $this->addElement(
'submit',
'-submit',df_translate(
'scripts.Dataface.DeleteForm._build.LABEL_DELETE',
'Delete'), array(
'id'=>
'delete_submit_button'));
101 $this->addFormRule(array(&$this,
'checkPermissions'));
111 if ( isset( $this->_query[
'-delete-one'] ) ){
112 $q = array(
'-skip'=>$this->_query[
'-cursor'],
'-limit'=>1);
113 $sql = $b->select(
'', $q);
114 $res = mysql_query($sql, $this->_db);
116 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);
119 if ( mysql_num_rows($res)==0 ) {
120 $msg = df_translate(
'scripts.Dataface.DeleteForm._build.ERROR_NO_RECORD_SELECTED',
"No record is currently selected so no record can be deleted.");
123 $row = mysql_fetch_array($res);
125 $displayCol = $rowRec->getTitle();
127 $msg = df_translate(
'scripts.Dataface.DeleteForm.display.ARE_YOU_SURE',
"Are you sure you want to delete this record: "$displayCol"?",array(
'displayCol'=>$displayCol));
130 }
else if ( isset($this->_query[
'-delete-found']) ) {
131 $q = $b->select_num_rows();
132 $res = mysql_query($q, $this->_db);
134 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);
138 list( $num ) = mysql_fetch_row($res);
140 $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.");
143 $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));
146 $msg = df_translate(
'scripts.Dataface.DeleteForm.display.ERROR_GET_VARS',
"Error: You must specify either '-delete-one' or '-delete-found' in GET vars.");
153 $form = ob_get_contents();
159 $context = array(
'msg'=>
'foo'.$msg,
'form'=>$form);
160 import(
'Dataface/SkinTool.php');
167 $skinTool->display($context,
'Dataface_DeleteForm.html');
173 function delete($values){
174 require_once
'Dataface/IO.php';
180 $it =& df_get_records($this->_tablename, $query);
182 while ( $it->hasNext() ){
183 $record =& $it->next();
184 $res = $io->delete($record);
195 if ( count($warnings) > 0 ){
208 if ( isset( $values[
'-delete-one']) ){
209 $keys = array_keys( $this->_table->keys() );
210 foreach (
$keys as $key){
211 if ( !isset( $values[$key] ) ){
215 'Missing key while trying to delete record',
217 'Attempt to delete single record when not all keys were specified. Missing key \''.$key.
'\'',
224 $val = $values[$key];
226 if ( $val{0} !=
'=' ) $val =
'='.$val;
230 $query[
'-limit'] = 9999;
232 if ( isset( $values[
'-search'] ) ){
233 $query[
'-search'] = $values[
'-search'];
235 if ( isset( $values[
'-sort'] ) ){
236 $query[
'-sort'] = $values[
'-sort'];
238 foreach ($values as $key=>$value){
239 if ( strpos($key,
'-')===0 )
continue;
240 $query[$key] = $value;
253 if ( $this->isSubmitted() ){
255 import(
'Dataface/PermissionsTool.php');
256 import(
'dataface-public-api.php');
259 $errors[$errCounter++] = $query->getMessage();
261 $records =& df_get_records_array($this->_tablename, $query);
263 $errors[$errCounter++] = $query->getMessage();
266 if ( !is_array($records) ){
267 $errors[$errCounter++] = df_translate(
'scripts.Dataface.DeleteForm.display.ERROR_NO_RECORDS_FOUND',
"No records matched the query, so no records can be deleted.");
270 foreach ( array_keys($records) as
$index ){
272 $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()));
278 if ( count($errors) > 0 ) {