Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
RemoveRelatedRecordForm.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 
33 require_once 'Dataface/IO.php';
34 require_once 'HTML/QuickForm.php';
35 
36 
40 class Dataface_RemoveRelatedRecordForm extends HTML_QuickForm {
41 
42  /*
43  * Reference to parent record.
44  * @type Dataface_Record
45  */
46  var $_record;
47 
48  /*
49  * The relationship name.
50  * @type string
51  */
53 
54  /*
55  * Reference to the relationship object.
56  * @type Dataface_Relationship
57  */
59 
60  /*
61  * Array of urlencoded key/value strings representing records to be deleted.
62  * @type array(string)
63  */
64  //var $_toBeRemoved;
65 
66  /*
67  * Flag to indicate if the form is built.
68  * @type boolean
69  */
70  var $_isBuilt = false;
71 
72  var $query;
73 
79  var $records;
80 
87 
96 
107  function Dataface_RemoveRelatedRecordForm(&$record, $relationshipName, $query=null){
108  if ( !isset($query) ){
110  $this->query = $app->getQuery();
111  unset($query);
112  $query =& $this->query;
113  } else {
114  $this->query =& $query;
115  }
116 
117  if ( !$record || !isset($record)){
118  $record =& $this->getRecord(); //df_get_record($query['-table'], $query);
119  }
120  $this->_record =& $record;
121  $this->_relationshipName = $relationshipName;
122 
123  $this->_relationship = $record->_table->getRelationship($relationshipName);
124  //$this->_toBeRemoved = $toBeRemoved;
125  $this->HTML_QuickForm('RemoveRecord');
126 
127 
128  }
129 
137  function formSubmitted(){
138  return ( isset( $_POST['--__keys__']) and isset( $_POST['-table']) );
139  }
140 
149  function &getRecord(){
151  $rec =& $app->getRecord();
152  return $rec;
153 
154  }
155 
156  function allowDeleteRecords(){
157  if ( !isset($this->allowDelete) ){
158  $records =& $this->getSelectedRecords();
159  $domainTable =& $this->getDomainTable();
160  // We are allowed to delete the records if for each record in the
161  // set, we have either:
162  // 1. Delete permission on the domain record.
163  // 2. delete related record permission on the parent record
164  $this->allowDelete = true;
165  foreach ( $records as $record ){
166  $domainRecord =& $record->toRecord($domainTable->tablename);
167  if ( !$record->_record->checkPermission('delete related record', array('relationship'=>$this->_relationshipName)) and
168  !$domainRecord->checkPermission('delete') ){
169  $this->allowDelete = false;
170  }
171  }
172  }
173  return $this->allowDelete;
174  }
175 
181  function deleteRequired(){
182  return ( $this->_relationship->getDistance( $this->_relationship->getDomainTable() ) == 1);
183  }
184 
190  function _build(){
191  if ( $this->_isBuilt ) return;
192  $keys = array_keys($this->_record->_table->keys());
193  $factory = new HTML_QuickForm('factory');
194  $keyEls = array();
195  foreach ($keys as $key){
196  $keyEls[] =& $factory->addElement('hidden', $key);
197 
198  }
199  $this->addGroup($keyEls, '--__keys__');
200  $this->addElement('hidden', '--selected-ids');
201  $this->addElement('hidden', '-table');
202  $this->addElement('hidden', '-relationship');
203  $this->addElement('hidden', '-queryString');
204  $this->addElement('hidden', '-action');
205 
206  if ( $this->allowDeleteRecords() and !$this->deleteRequired()){
207  $this->addElement('checkbox', 'delete',df_translate('scripts.Dataface.RemoveRelatedRecordForm._build.LABEL_ALSO_DELETE_FROM_DB','Also delete record(s) from database?'));
208  } else {
209  $this->addElement('hidden', 'delete');
210  }
211  $this->addElement('hidden','-confirm_delete_hidden','1');
212  $this->addElement('submit','-confirm_delete',df_translate('scripts.Dataface.RemoveRelatedRecordForm._build.LABEL_REMOVE',"Remove"));
213 
214 
215 
216  $this->setDefaults(array(
217  '--__keys__'=>$this->_record->getValues(array_keys($this->_record->_table->keys())),
218  '-table'=>$this->_record->_table->tablename,
219  '-relationship' => $this->_relationshipName,
220  '--selected-ids'=> $_POST['--selected-ids'],
221  '-queryString'=> $_SERVER['QUERY_STRING'],
222  '-action'=>'remove_related_record'
223  ));
224  if ( $this->deleteRequired() ){
225  $this->setDefaults(array('delete'=>1));
226  }
227  $this->_isBuilt = true;
228 
229 
230  }
231 
232 
236  function display(){
237 
238  $this->_build();
239  $domainTable = $this->_relationship->getDomainTable();
240  if ( PEAR::isError($domainTable) ){
241  $domainTable = $this->_relationship->_schema['selected_tables'][0];
242  }
243  $domainTable = Dataface_Table::loadTable($domainTable);
244  $io = new Dataface_IO($domainTable->tablename);
245  echo "<p>".df_translate(
246  'scripts.Dataface.RemoveRelatedRecordForm.display.MESSAGE_ARE_YOU_SURE',
247  "Are you sure you want to remove the following records from the relationship '".$this->_relationshipName."'?",
248  array('relationship'=>$this->_relationshipName)
249  )
250  ."</p>";
251  echo "<ul>";
252 
253  $records =& df_get_selected_records($this->query);
254 
255  foreach ($records as $record){
256 
257  echo "<li>".$record->getTitle()."</li>\n";
258 
259 
260  }
261  echo "</ul>";
262 
263  parent::display();
264  }
265 
269  function &getSelectedRecords(){
270  if ( !isset($this->records) ) $this->records = df_get_selected_records($this->query);
271  return $this->records;
272  }
273 
279  function &getDomainTable(){
280  if ( !isset($this->_domainTable) ){
281  $domainTable = $this->_relationship->getDomainTable();
282  if ( PEAR::isError($domainTable) ){
283  /*
284  * Dataface_Relationship::getDomainTable() throws an error if there are
285  * no join tables. We account for that by explicitly setting the domain
286  * table to the first table in the list.
287  */
288  $domainTable = $this->_relationship->_schema['selected_tables'][0];
289  }
290  $this->_domainTable =& Dataface_Table::loadTable($domainTable);
291  }
292  return $this->_domainTable;
293 
294  }
295 
299  function delete($values){
300 
301  /*
302  * Next we construct an IO object to write to the domain table.
303  */
304  $domainTable =& $this->getDomainTable();
305  $domainIO = new Dataface_IO($domainTable->tablename);
306 
307  $records =& $this->getSelectedRecords();
308  $messages = array();
309  $confirmations = array();
310  $warnings = array();
311  $table =& $this->_record->_table; // This is the table of the parent record.
312  $io = new Dataface_IO($table->tablename);
313  $removePermission = $this->_record->checkPermission('remove related record', array('relationship'=>$this->_relationshipName));
314  //if ($removePermission ){
315  // $mask = array('delete'=>1);
316  //} else {
317  // $mask = array();
318  //}
319 
320  $deleteRequired = $this->deleteRequired(); // Do we have to delete the domain record
321  // to make the removal effective
322  foreach ($records as $record){
323 
324  // If deletion is required, we will do ou
325  $res = $io->removeRelatedRecord($record, @$values['delete'], true /* Use security */);
326  if ( PEAR::isError($res) ){
327  $warnings[] = $res->getMessage();
328  } else {
329  $confirmations[] = $confirmations[] = df_translate(
330  'Successfully deleted record',
331  "Successfully deleted entry for record '".$record->getTitle()."' in table '".$table->tablename."'",
332  array('title'=>$record->getTitle(), 'table'=>$table->tablename)
333  );
334  }
335 
336 
337  }
338  return array('confirmations'=>$confirmations, "warnings" => $warnings);
339 
340 
341  }
342 
343 
344 
345 
346 
347 }