![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 import('Dataface/PermissionsTool.php'); 00003 class dataface_actions_reorder_related_records { 00004 function handle(&$params){ 00005 00006 if ( !isset( $_POST['-redirect'] ) and !isset($_POST['relatedList-body']) ){ 00007 return PEAR::raiseError('Cannot reorder related records because no redirect url was specified in the POST parameters.'.Dataface_Error::printStackTrace()); 00008 } 00009 00010 $app =& Dataface_Application::getInstance(); 00011 $query =& $app->getQuery(); 00012 if ( !($record = df_get_selected_records($query)) ){ 00013 $record =& $app->getRecord(); 00014 } else { 00015 $record = $record[0]; 00016 } 00017 if ( PEAR::isError($record) ) return $record; 00018 if ( !$record ){ 00019 return PEAR::raiseError('The specified record could not be found.'); 00020 } 00021 00022 00023 if ( !@$query['-relationship'] ){ 00024 return PEAR::raiseError("No relationship specified."); 00025 } 00026 00027 $relationship =& $record->_table->getRelationship($query['-relationship']); 00028 if ( PEAR::isError($relationship) ) return $relationship; 00029 00030 $orderColumn = $relationship->getOrderColumn(); 00031 if ( !$orderColumn ){ 00032 return PEAR::raiseError('Could not reorder records of this relationship because it does not have any order column specified.'); 00033 } 00034 00035 00036 00037 00038 if ( !Dataface_PermissionsTool::checkPermission('reorder_related_records', $record, array('relationship'=>$query['-relationship']) ) ) { 00039 return Dataface_Error::permissionDenied('You do not have permission to reorder the records in this relationship.'); 00040 } 00041 00042 if ( isset($_POST['relatedList-body']) ){ 00043 $relatedIds = array_map('urldecode', $_POST['relatedList-body']); 00044 // In this case we are not just moving a record up or down the list, 00045 // we may be reordering the list altogether. 00046 // We may also just be ordering a subset of the list. 00047 // so we will want to be reordering the given set of records 00048 // with respect to each other. 00049 00050 // First let's see if the ordering has been initialized yet. 00051 $records = array(); 00052 //print_r($relatedIds);exit; 00053 foreach ($relatedIds as $recid ){ 00054 //$recid = urldecode($recid); 00055 $records[] = df_get_record_by_id($recid); 00056 } 00057 $start = ( isset($query['-related:start']) ? $query['-related:start'] : 0); 00058 $record->sortRelationship($query['-relationship'], $start, $records); 00059 00060 echo 'Sorted Successfully'; 00061 exit; 00062 } 00063 00064 if ( !isset( $_POST['-reorder:direction'] ) ){ 00065 return PEAR::raiseError('Cannot reorder related records because no direction was specified.'); 00066 } 00067 00068 if ( !isset( $_POST['-reorder:index']) ){ 00069 return PEAR::raiseError('Cannot reorder related records because no index was specified.'); 00070 } 00071 00072 $index = intval($_POST['-reorder:index']); 00073 00074 switch ( $_POST['-reorder:direction']){ 00075 case 'up': 00076 //echo "Moving up";exit; 00077 $res = $record->moveUp($query['-relationship'], $index); 00078 00079 break; 00080 00081 case 'down': 00082 $res = $record->moveDown($query['-relationship'], $index); 00083 break; 00084 00085 default: 00086 return PEAR::raiseError('Invalid input for direction of reordering. Must be up or down but received "'.$_POST['-reorder:direction'].'"'); 00087 } 00088 if ( PEAR::isError($res) ) return $res; 00089 header('Location: '.$_POST['-redirect']); 00090 exit; 00091 00092 00093 } 00094 } 00095 ?>