Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
DeleteForm.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 import( 'HTML/QuickForm.php');
22 import('Smarty/Smarty.class.php');
23 import('Dataface/Globals.php');
24 import('Dataface/QueryBuilder.php');
25 
26 
30 class Dataface_DeleteForm extends HTML_QuickForm {
31 
33  var $_db;
34  var $_query;
35  var $_table;
36  var $_isBuilt;
37 
38 
39  function Dataface_DeleteForm( $tablename, $db, $query){
40  $this->_tablename = $tablename;
41  $this->_table =& Dataface_Table::loadTable($tablename);
42  $this->_db = $db;
43  $this->_query = $query;
44  $this->_isBuilt = false;
45 
46  if ( !isset( $this->_query['-cursor'] ) ){
47  $this->_query['-cursor'] = 0;
48  }
49 
50  parent::HTML_QuickForm('deleteForm');
51 
52 
53  }
54 
55  function _build(){
56 
57  if ( !$this->_isBuilt ){
58  $b = new Dataface_QueryBuilder($this->_tablename, $this->_query);
59 
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);
69  if ( !$res ){
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);
71 
72  }
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);
75 
76  } else {
77  $row = mysql_fetch_array($res);
78  $fields =& $this->_table->keys();
79 
80  $keys = array_keys($fields);
81 
82 
83 
84  foreach ($keys as $key){
85  $this->addElement('hidden',$key);
86  $this->setDefaults(array( $key=>$row[$key]) );
87 
88  }
89  }
90  } else {
91 
92 
93  foreach ($this->_query as $key=>$value){
94  $this->addElement('hidden', $key);
95  $this->setConstants(array($key=>$value));
96  }
97  }
98  $this->removeElement('-submit');
99  $this->addElement('submit','-submit',df_translate('scripts.Dataface.DeleteForm._build.LABEL_DELETE','Delete'), array('id'=>'delete_submit_button'));
100 
101  $this->addFormRule(array(&$this, 'checkPermissions'));
102  }
103 
104 
105  }
106 
107  function display(){
108  $this->_build();
109  $showform = true;
110  $b = new Dataface_QueryBuilder($this->_tablename, $this->_query);
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);
115  if ( !$res ){
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);
117 
118  }
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.");
121  $showform = false;
122  } else {
123  $row = mysql_fetch_array($res);
124  $rowRec = new Dataface_Record($this->_tablename, $row);
125  $displayCol = $rowRec->getTitle();
126 
127  $msg = df_translate('scripts.Dataface.DeleteForm.display.ARE_YOU_SURE',"Are you sure you want to delete this record: &quot;$displayCol&quot;?",array('displayCol'=>$displayCol));
128  }
129 
130  } else if ( isset($this->_query['-delete-found']) ) {
131  $q = $b->select_num_rows();
132  $res = mysql_query($q, $this->_db);
133  if ( !$res ){
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);
135 
136  }
137 
138  list( $num ) = mysql_fetch_row($res);
139  if ( $num <= 0 ){
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.");
141  $showform = false;
142  } else {
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));
144  }
145  } else {
146  $msg = df_translate('scripts.Dataface.DeleteForm.display.ERROR_GET_VARS',"Error: You must specify either '-delete-one' or '-delete-found' in GET vars.");
147  $showform=false;
148  }
149 
150  if ( $showform ){
151  ob_start();
152  parent::display();
153  $form = ob_get_contents();
154  ob_end_clean();
155  } else {
156  $form = '';
157  }
158 
159  $context = array('msg'=>'foo'.$msg, 'form'=>$form);
160  import('Dataface/SkinTool.php');
161  $skinTool =& Dataface_SkinTool::getInstance();
162  //$smarty = new Smarty;
163  //$smarty->template_dir = $GLOBALS['Dataface_Globals_Templates'];
164  //$smarty->compile_dir = $GLOBALS['Dataface_Globals_Templates_c'];
165  //$smarty->assign($context);
166  //$smarty->display('Dataface_DeleteForm.html');
167  $skinTool->display($context, 'Dataface_DeleteForm.html');
168 
169 
170 
171  }
172 
173  function delete($values){
174  require_once 'Dataface/IO.php';
175 
176  $query = $this->_buildDeleteQuery($values);
177  if ( PEAR::isError($query) ) return $query;
178  $io = new Dataface_IO($this->_tablename);
179 
180  $it =& df_get_records($this->_tablename, $query);
181  $warnings = array();
182  while ( $it->hasNext() ){
183  $record =& $it->next();
184  $res = $io->delete($record);
185  if ( PEAR::isError($res) && Dataface_Error::isError($res) ){
186  // this is a serious error... kill it
187  return $res;
188  } else if ( PEAR::isError($res) ){
189  // this is a warning or a notice
190  $warnings[] = $res;
191  }
192  unset($record);
193  }
194 
195  if ( count($warnings) > 0 ){
196  return $warnings;
197  }
198  return true;
199 
200 
201  }
202 
206  function _buildDeleteQuery($values = array()){
207  $query = array();
208  if ( isset( $values['-delete-one']) ){
209  $keys = array_keys( $this->_table->keys() );
210  foreach ($keys as $key){
211  if ( !isset( $values[$key] ) ){
212  return PEAR::raiseError(
214  /* i18n id */
215  'Missing key while trying to delete record',
216  /* default error message */
217  'Attempt to delete single record when not all keys were specified. Missing key \''.$key.'\'',
218  /* i18n parameters */
219  array('key'=>$key)
220  ),
222  );
223  }
224  $val = $values[$key];
225 
226  if ( $val{0} != '=' ) $val = '='.$val;
227  $query[$key] = $val;
228  }
229  } else {
230  $query['-limit'] = 9999;//isset($values['-limit']) ? $values['-limit'] : 1;
231  $query['-skip'] = 0;//isset($values['-skip']) ? $values['-skip'] : 0;
232  if ( isset( $values['-search'] ) ){
233  $query['-search'] = $values['-search'];
234  }
235  if ( isset( $values['-sort'] ) ){
236  $query['-sort'] = $values['-sort'];
237  }
238  foreach ($values as $key=>$value){
239  if ( strpos($key, '-')===0 ) continue;
240  $query[$key] = $value;
241  }
242  }
243 
244  return $query;
245 
246  }
247 
251  function checkPermissions(){
252  $errors = array();
253  if ( $this->isSubmitted() ){
254  $errCounter = 1;
255  import('Dataface/PermissionsTool.php');
256  import('dataface-public-api.php');
257  $query = $this->_buildDeleteQuery($this->exportValues());
258  if ( PEAR::isError($query) ){
259  $errors[$errCounter++] = $query->getMessage();
260  }
261  $records =& df_get_records_array($this->_tablename, $query);
262  if ( PEAR::isError($records) ){
263  $errors[$errCounter++] = $query->getMessage();
264  // we attach this error to the '-submit' field because I don't know how to attach it to the form.
265  }
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.");
268  }
269  else {
270  foreach ( array_keys($records) as $index ){
271  if ( !Dataface_PermissionsTool::delete($records[$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()));
273  // we attach this error to the '-submit' field because I don't know how to attach it to the form.
274  }
275  }
276  }
277  }
278  if ( count($errors) > 0 ) {
279 
280  return $errors;
281  }
282  return true;
283 
284 
285  }
286 
287  function validate(){
288  $this->_build();
289  return parent::validate();
290  }
291 
292 
293 
294 
295 
296 }