Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
reorder_related_records.php
Go to the documentation of this file.
1 <?php
2 import('Dataface/PermissionsTool.php');
4  function handle(&$params){
5 
6  if ( !isset( $_POST['-redirect'] ) and !isset($_POST['relatedList-body']) ){
7  return PEAR::raiseError('Cannot reorder related records because no redirect url was specified in the POST parameters.'.Dataface_Error::printStackTrace());
8  }
9 
11  $query =& $app->getQuery();
12  if ( !($record = df_get_selected_records($query)) ){
13  $record =& $app->getRecord();
14  } else {
15  $record = $record[0];
16  }
17  if ( PEAR::isError($record) ) return $record;
18  if ( !$record ){
19  return PEAR::raiseError('The specified record could not be found.');
20  }
21 
22 
23  if ( !@$query['-relationship'] ){
24  return PEAR::raiseError("No relationship specified.");
25  }
26 
27  $relationship =& $record->_table->getRelationship($query['-relationship']);
28  if ( PEAR::isError($relationship) ) return $relationship;
29 
30  $orderColumn = $relationship->getOrderColumn();
31  if ( !$orderColumn ){
32  return PEAR::raiseError('Could not reorder records of this relationship because it does not have any order column specified.');
33  }
34 
35 
36 
37 
38  if ( !Dataface_PermissionsTool::checkPermission('reorder_related_records', $record, array('relationship'=>$query['-relationship']) ) ) {
39  return Dataface_Error::permissionDenied('You do not have permission to reorder the records in this relationship.');
40  }
41 
42  if ( isset($_POST['relatedList-body']) ){
43  $relatedIds = array_map('urldecode', $_POST['relatedList-body']);
44  // In this case we are not just moving a record up or down the list,
45  // we may be reordering the list altogether.
46  // We may also just be ordering a subset of the list.
47  // so we will want to be reordering the given set of records
48  // with respect to each other.
49 
50  // First let's see if the ordering has been initialized yet.
51  $records = array();
52  //print_r($relatedIds);exit;
53  foreach ($relatedIds as $recid ){
54  //$recid = urldecode($recid);
55  $records[] = df_get_record_by_id($recid);
56  }
57  $start = ( isset($query['-related:start']) ? $query['-related:start'] : 0);
58  $record->sortRelationship($query['-relationship'], $start, $records);
59 
60  echo 'Sorted Successfully';
61  exit;
62  }
63 
64  if ( !isset( $_POST['-reorder:direction'] ) ){
65  return PEAR::raiseError('Cannot reorder related records because no direction was specified.');
66  }
67 
68  if ( !isset( $_POST['-reorder:index']) ){
69  return PEAR::raiseError('Cannot reorder related records because no index was specified.');
70  }
71 
72  $index = intval($_POST['-reorder:index']);
73 
74  switch ( $_POST['-reorder:direction']){
75  case 'up':
76  //echo "Moving up";exit;
77  $res = $record->moveUp($query['-relationship'], $index);
78 
79  break;
80 
81  case 'down':
82  $res = $record->moveDown($query['-relationship'], $index);
83  break;
84 
85  default:
86  return PEAR::raiseError('Invalid input for direction of reordering. Must be up or down but received "'.$_POST['-reorder:direction'].'"');
87  }
88  if ( PEAR::isError($res) ) return $res;
89  header('Location: '.$_POST['-redirect']);
90  exit;
91 
92 
93  }
94 }
95 ?>