$v) $return[toUtf8($i)] = toUtf8($v); return $return; } } require_once 'api/controller.php'; $mounted = array(); $synced = array(); if(!function_exists('prepare')) { function prepare( $concept, $id, $dt, &$data, &$oldIds, &$mounted, &$synced ) { $oldIds[] = $id; if( $dt === 'false' || $dt === false ) return( false ); if( !preg_match( '#^([a-zA-Z0-9-_]+)\(.*\)$#', $id ) ) $dt['id'] = $id; elseif( isset($dt['id']) && $dt['id'] === $id ) unset($dt['id']); $links = Controller::links( $concept ); foreach( $links as $linkName => $linkTarget ) { if( isset( $dt[$linkName] ) ) { if( $notArray = Controller::hasOne( $concept, $linkName ) ) $dt[$linkName] = array( $dt[$linkName] ); foreach( $dt[$linkName] as $i => $d ) { $currentURI = formatURI($links[$linkName], $d); if( isset( $mounted[ $currentURI ] ) ) { unset( $dt[$linkName][$i] ); } elseif( isset( $synced[ $d ] ) ) { $dt[$linkName][$i] = $synced[ $d ]; } elseif( isset($data[ $currentURI ]) ) { $value = $data[$currentURI]; unset( $data[ $currentURI ] ); $mounted[ $currentURI ] = true; $dt[$linkName][$i] = prepare( $links[$linkName], $d, $value, $data, $oldIds, $mounted, $synced ); } } if( empty( $dt[$linkName] ) ) unset( $dt[$linkName] ); elseif( $notArray ) $dt[$linkName] = $dt[$linkName][0]; } } return( $dt ); } } $return = array(); if( !isset( $args[0] ) ) $args = array( $args ); Controller::addFallbackHandler( 0, function($e, $URI){ throw new Exception( $e->getMessage(), 100, $e ); } ); Controller::addFallbackHandler( 100, function( $e, $URI ){ Controller::rollback( $URI ); throw $e; }); foreach( $args as $i => $data ) { foreach( $data as $uri => $dt ) { if( !isset($data[$uri]) ) continue; list( , $concept, $id ) = parseURI( $uri ); unset( $data[$uri] ); $mounted[$uri] = true; $oldIds = array(); $dt = prepare( $concept, $id, $dt, $data, $oldIds, $mounted, $synced ); try{ $result = Controller::put( array( 'concept' => $concept, 'id' => $id ), $dt ); } catch( Exception $e ){ $return[ $uri ] = toUtf8( $e->getMessage() ); unset( $data[$uri] ); continue; } if( !$result ) { $return[ $uri ] = 'ROLLBACK'; unset( $data[$uri] ); continue; } foreach( $result as $ii => $tx ) { if( !isset($tx['order']) ) continue; $oldId = $oldIds[ $tx['order'] ]; if( isset( $oldId ) && $oldId ) { $synced[ $oldId ] = $tx['id']; unset( $oldIds[ $tx['order'] ] ); } $oldURI = formatURI( $tx['concept'], $oldId ); unset( $data[$oldURI] ); $return[ $oldURI ] = !$tx['rollback'] ? $dt ? array( 'id' => $tx['id'] ) : false : 'ROLLBACK'; } } foreach( $data as $oldURI => $oldVal ) $return[ $oldURI ] = 'ROLLBACK'; } echo json_encode( $return ); Controller::closeAll(); // ob_start(); // print "\n"; // print "result: "; // print_r( $result ); // $output = ob_get_clean(); // file_put_contents( "/tmp/prototype.log", $output , FILE_APPEND );