Skip to content

Commit

Permalink
Upgrade updates for relationships, bug fixes for saving fields and pi…
Browse files Browse the repository at this point in the history
…ck_val/pick_object mapping
  • Loading branch information
sc0ttkclark committed Aug 10, 2012
1 parent 87564fb commit fd65f5a
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 56 deletions.
14 changes: 7 additions & 7 deletions classes/PodsAPI.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1173,12 +1173,12 @@ public function save_field ( $params, $table_operation = true ) {
$field[ 'pick_val' ] = pods_str_replace( 'pod-', '', $field[ 'pick_object' ], 1 ); $field[ 'pick_val' ] = pods_str_replace( 'pod-', '', $field[ 'pick_object' ], 1 );
$field[ 'pick_object' ] = 'pod'; $field[ 'pick_object' ] = 'pod';
} }
elseif ( 0 === strpos( 'post-types-', $field[ 'pick_object' ] ) ) { elseif ( 0 === strpos( 'post-type-', $field[ 'pick_object' ] ) ) {
$field[ 'pick_val' ] = pods_str_replace( 'post-types-', '', $field[ 'pick_object' ], 1 ); $field[ 'pick_val' ] = pods_str_replace( 'post-type-', '', $field[ 'pick_object' ], 1 );
$field[ 'pick_object' ] = 'post_type'; $field[ 'pick_object' ] = 'post_type';
} }
elseif ( 0 === strpos( 'taxonomies-', $field[ 'pick_object' ] ) ) { elseif ( 0 === strpos( 'taxonomy-', $field[ 'pick_object' ] ) ) {
$field[ 'pick_val' ] = pods_str_replace( 'taxonomies-', '', $field[ 'pick_object' ], 1 ); $field[ 'pick_val' ] = pods_str_replace( 'taxonomy-', '', $field[ 'pick_object' ], 1 );
$field[ 'pick_object' ] = 'taxonomy'; $field[ 'pick_object' ] = 'taxonomy';
} }
} }
Expand Down Expand Up @@ -2953,10 +2953,10 @@ public function load_fields ( $params, $strict = false ) {


foreach ( $pod[ 'fields' ] as $field ) { foreach ( $pod[ 'fields' ] as $field ) {
if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) ) if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) )
$fields[ $field[ 'id' ] ] = $field; $fields[ $field[ 'name' ] ] = $field;


if ( in_array( $fields[ 'name' ], $params->name ) || in_array( $fields[ 'id' ], $params->id ) || in_array( $fields[ 'type' ], $params->type ) ) if ( in_array( $fields[ 'name' ], $params->name ) || in_array( $fields[ 'id' ], $params->id ) || in_array( $fields[ 'type' ], $params->type ) )
$fields[ $field[ 'id' ] ] = $field; $fields[ $field[ 'name' ] ] = $field;
} }
} }
else { else {
Expand Down Expand Up @@ -2992,7 +2992,7 @@ public function load_fields ( $params, $strict = false ) {
) ); ) );


if ( empty( $params->type ) || in_array( $fields[ 'type' ], $params->type ) ) if ( empty( $params->type ) || in_array( $fields[ 'type' ], $params->type ) )
$fields[ $field[ 'id' ] ] = $field; $fields[ $field[ 'name' ] ] = $field;
} }
} }
} }
Expand Down
157 changes: 126 additions & 31 deletions sql/PodsUpgrade.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -261,7 +261,26 @@ function migrate_pods () {
); );


if ( 'pick' == $field_type ) { if ( 'pick' == $field_type ) {
$field_params[ 'pick_object' ] = 'pod';
$field_params[ 'pick_val' ] = $row->pickval; $field_params[ 'pick_val' ] = $row->pickval;

if ( 'wp_user' == $row->pickval ) {
$field_params[ 'pick_object' ] = 'user';
$field_params[ 'pick_val' ] = '';
}
elseif ( 'wp_post' == $row->pickval ) {
$field_params[ 'pick_object' ] = 'post_type';
$field_params[ 'pick_val' ] = 'post';
}
elseif ( 'wp_page' == $row->pickval ) {
$field_params[ 'pick_object' ] = 'post_type';
$field_params[ 'pick_val' ] = 'page';
}
elseif ( 'wp_taxonomy' == $row->pickval ) {
$field_params[ 'pick_object' ] = 'taxonomy';
$field_params[ 'pick_val' ] = 'category';
}

$field_params[ 'sister_field_id' ] = $row->sister_field_id; $field_params[ 'sister_field_id' ] = $row->sister_field_id;
$field_params[ 'pick_filter' ] = $row->pick_filter; $field_params[ 'pick_filter' ] = $row->pick_filter;
$field_params[ 'pick_orderby' ] = $row->pick_orderby; $field_params[ 'pick_orderby' ] = $row->pick_orderby;
Expand Down Expand Up @@ -323,38 +342,114 @@ function migrate_relationships () {
if ( true === $this->check_progress( __FUNCTION__ ) ) if ( true === $this->check_progress( __FUNCTION__ ) )
return '1'; return '1';


// go through each relationship row global $wpdb;
// convert pod_id to real table id of item
// lookup pod_id in wp_pod as id field, get tbl_row_id
// get real pod_id (Pod ID) of the item being related from
// get datatype field that matches id = pod_id
// get real relate_pod_id (Pod ID) of the item being related to (if pod)
// you can get this by looking at what object the field is related to
// or if a sister_pod_id is given, look up that in wp_pod
// copy tbl_row_id to related_item_id
// copy weight

// OLD TABLE:
/*
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`pod_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL,
`sister_pod_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL,
`field_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`tbl_row_id` BIGINT(15) UNSIGNED NULL DEFAULT NULL,
`weight` INT(10) UNSIGNED NULL DEFAULT '0'
*/


// NEW TABLE: $api = pods_api();
/*
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, $last_id = (int) $this->check_progress( __FUNCTION__ );
`pod_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`field_id` INT(10) UNSIGNED NULL DEFAULT NULL, $sql = "
`item_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL, SELECT *
`related_pod_id` INT(10) UNSIGNED NULL DEFAULT NULL, FROM `@wp_pod_rel`
`related_field_id` INT(10) UNSIGNED NULL DEFAULT NULL, WHERE {$last_id} < `id`
`related_item_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL, ORDER BY `id`
`weight` SMALLINT(5) UNSIGNED NULL DEFAULT '0' LIMIT 0, 800
*/ ";

$rel = pods_query( $sql );

$last_id = true;

$pod_types = pods_query( "SELECT `id`, `name` FROM `@wp_pod_types` ORDER BY `id`" );

$types = array();

$x = 0;

if ( !empty( $rel ) && !empty( $pod_types ) ) {
foreach ( $pod_types as $type ) {
$types[ $type->id ] = $api->load_pod( array( 'name' => $type->name ) );

$pod_fields = pods_query( "SELECT `id`, `name` FROM `@wp_pod_fields` WHERE `datatype` = {$type->id} ORDER BY `id`" );

foreach ( $pod_fields as $field ) {
$types[ 'old_fields' ][ $field->id ] = $field->name;
}
}

foreach ( $rel as $r ) {
$r->pod_id = (int) $r->pod_id;

$p = pods_query( "SELECT `tbl_row_id`, `datatype` FROM `@wp_pod` WHERE `id` = {$r->pod_id}");

if ( empty( $p ) )
continue;

if ( !isset( $types[ $p->datatype ] ) || !isset( $types[ $p->datatype ][ 'old_fields' ][ $r->field_id ] ) )
continue;

if ( !isset( $types[ $p->datatype ][ 'fields' ][ $types[ $p->datatype ][ 'old_fields' ][ $r->field_id ] ] ) )
continue;

$field = $types[ $p->datatype ][ 'fields' ][ $types[ $p->datatype ][ 'old_fields' ][ $r->field_id ] ];

if ( !in_array( $field[ 'type' ], array( 'pick', 'file' ) ) )
continue;

$pod_id = $types[ $p->datatype ][ 'id' ];
$field_id = $field[ 'id' ];
$item_id = $p->tbl_row_id;

$related_pod_id = 0;
$related_field_id = 0;
$related_item_id = $r->tbl_row_id;

if ( 'pick' == $field[ 'type' ] ) {
if ( 0 < (int) $field[ 'sister_field_id' ] ) {
$related_field = $api->load_field( array( 'id' => $field[ 'sister_field_id' ] ) );

if ( empty( $related_field ) )
continue;

$related_pod_id = $related_field[ 'pod_id' ];
$related_field_id = $related_field[ 'id' ];
}
elseif ( 'pod' == $field[ 'pick_object' ] && 0 < strlen( $field[ 'pick_val' ] ) ) {
$related_pod = $api->load_pod( array( 'name' => $field[ 'pick_val' ] ), false );

if ( empty( $related_pod ) )
continue;

$related_pod_id = $related_pod[ 'id' ];
}
}

$sql = "
REPLACE INTO `@wp_pods_rel`
( `id`, `pod_id`, `field_id`, `item_id`, `related_pod_id`, `related_field_id`, `related_item_id`, `weight` )
VALUES ( {$r->id}, {$pod_id}, {$field_id}, {$item_id}, {$related_pod_id}, {$related_field_id}, {$related_item_id}, {$r->weight} )
";

pods_query( $sql );

$last_id = $r->id;

$x++;

if ( 10 < $x ) {
$this->update_progress( __FUNCTION__, $last_id );

$x = 0;
}
}
}

$this->update_progress( __FUNCTION__, $last_id );

if ( 800 == count( $rel ) )
echo '-2';
else
echo '1';
} }


function migrate_settings () { function migrate_settings () {
Expand Down
44 changes: 26 additions & 18 deletions ui/js/jquery.pods.upgrade.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
$( '#pods-wizard-next' ).show(); $( '#pods-wizard-next' ).show();
} }
}, },
migrate : function () { migrate : function ( postdata ) {
var pods_ajaxurl = $( '#pods-wizard-box' ).data( 'url' ); var pods_ajaxurl = $( '#pods-wizard-box' ).data( 'url' );


if ( 'undefined' != typeof pods_ajaxurl ) if ( 'undefined' != typeof pods_ajaxurl )
Expand All @@ -73,22 +73,24 @@
if ( 'undefined' != typeof ajaxurl && ('undefined' == typeof pods_ajaxurl || '' == pods_ajaxurl || '?pods_ajax=1' == pods_ajaxurl || document.location.href == pods_ajaxurl || document.location.href.replace( /\?nojs\=1/, '?pods_ajax=1' ) == pods_ajaxurl) ) if ( 'undefined' != typeof ajaxurl && ('undefined' == typeof pods_ajaxurl || '' == pods_ajaxurl || '?pods_ajax=1' == pods_ajaxurl || document.location.href == pods_ajaxurl || document.location.href.replace( /\?nojs\=1/, '?pods_ajax=1' ) == pods_ajaxurl) )
pods_ajaxurl = ajaxurl + '?pods_ajax=1'; pods_ajaxurl = ajaxurl + '?pods_ajax=1';


if ( $( '#pods-wizard-panel-3 table tbody tr.pods-wizard-table-pending' )[ 0 ] ) { if ( 'undefined' != typeof postdata || $( '#pods-wizard-panel-3 table tbody tr.pods-wizard-table-pending' )[ 0 ] ) {
var $row = $( '#pods-wizard-panel-3 table tbody tr.pods-wizard-table-pending' ).first(); if ( 'undefined' == typeof postdata ) {
var $row = $( '#pods-wizard-panel-3 table tbody tr.pods-wizard-table-pending' ).first();


$row.removeClass( 'pods-wizard-table-pending' ).addClass( 'pods-wizard-table-active' ); $row.removeClass( 'pods-wizard-table-pending' ).addClass( 'pods-wizard-table-active' );


var postdata = { var postdata = {
'action' : $( '#pods-wizard-box' ).data( 'action' ), 'action' : $( '#pods-wizard-box' ).data( 'action' ),
'method' : $( '#pods-wizard-box' ).data( 'method' ), 'method' : $( '#pods-wizard-box' ).data( 'method' ),
'_wpnonce' : $( '#pods-wizard-box' ).data( '_wpnonce' ), '_wpnonce' : $( '#pods-wizard-box' ).data( '_wpnonce' ),
'step' : 'migrate', 'step' : 'migrate',
'type' : $row.data( 'upgrade' ), 'type' : $row.data( 'upgrade' ),
'pod' : '' 'pod' : ''
}; };


if ( 'undefined' != typeof $row.data( 'pod' ) ) if ( 'undefined' != typeof $row.data( 'pod' ) )
postdata[ 'pod' ] = $row.data( 'pod' ); postdata[ 'pod' ] = $row.data( 'pod' );
}


$.ajax( { $.ajax( {
type : 'POST', type : 'POST',
Expand All @@ -97,10 +99,16 @@
data : postdata, data : postdata,
success : function ( d ) { success : function ( d ) {
if ( -1 == d.indexOf( '<e>' ) && -1 != d ) { if ( -1 == d.indexOf( '<e>' ) && -1 != d ) {
$row.removeClass( 'pods-wizard-table-active' ).addClass( 'pods-wizard-table-complete' ); if ( -2 == d ) {

// Run next
// Run next return methods[ 'migrate' ]( postdata );
return methods[ 'migrate' ](); }
else {
$row.removeClass( 'pods-wizard-table-active' ).addClass( 'pods-wizard-table-complete' );

// Run next
return methods[ 'migrate' ]();
}
} }
else if ( -1 == d.indexOf( 'Database Error;' ) ) { else if ( -1 == d.indexOf( 'Database Error;' ) ) {
$row.removeClass( 'pods-wizard-table-active' ).addClass( 'pods-wizard-table-error' ); $row.removeClass( 'pods-wizard-table-active' ).addClass( 'pods-wizard-table-error' );
Expand Down

0 comments on commit fd65f5a

Please sign in to comment.