Permalink
Browse files

Upgrade updates for relationships, bug fixes for saving fields and pi…

…ck_val/pick_object mapping
  • Loading branch information...
1 parent 87564fb commit fd65f5a92499b5119e713356a7bd0176cac1dce5 @sc0ttkclark sc0ttkclark committed Aug 10, 2012
Showing with 159 additions and 56 deletions.
  1. +7 −7 classes/PodsAPI.php
  2. +126 −31 sql/PodsUpgrade.php
  3. +26 −18 ui/js/jquery.pods.upgrade.js
View
@@ -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_object' ] = 'pod';
}
- elseif ( 0 === strpos( 'post-types-', $field[ 'pick_object' ] ) ) {
- $field[ 'pick_val' ] = pods_str_replace( 'post-types-', '', $field[ 'pick_object' ], 1 );
+ elseif ( 0 === strpos( 'post-type-', $field[ 'pick_object' ] ) ) {
+ $field[ 'pick_val' ] = pods_str_replace( 'post-type-', '', $field[ 'pick_object' ], 1 );
$field[ 'pick_object' ] = 'post_type';
}
- elseif ( 0 === strpos( 'taxonomies-', $field[ 'pick_object' ] ) ) {
- $field[ 'pick_val' ] = pods_str_replace( 'taxonomies-', '', $field[ 'pick_object' ], 1 );
+ elseif ( 0 === strpos( 'taxonomy-', $field[ 'pick_object' ] ) ) {
+ $field[ 'pick_val' ] = pods_str_replace( 'taxonomy-', '', $field[ 'pick_object' ], 1 );
$field[ 'pick_object' ] = 'taxonomy';
}
}
@@ -2953,10 +2953,10 @@ public function load_fields ( $params, $strict = false ) {
foreach ( $pod[ 'fields' ] as $field ) {
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 ) )
- $fields[ $field[ 'id' ] ] = $field;
+ $fields[ $field[ 'name' ] ] = $field;
}
}
else {
@@ -2992,7 +2992,7 @@ public function load_fields ( $params, $strict = false ) {
) );
if ( empty( $params->type ) || in_array( $fields[ 'type' ], $params->type ) )
- $fields[ $field[ 'id' ] ] = $field;
+ $fields[ $field[ 'name' ] ] = $field;
}
}
}
View
@@ -261,7 +261,26 @@ function migrate_pods () {
);
if ( 'pick' == $field_type ) {
+ $field_params[ 'pick_object' ] = 'pod';
$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[ 'pick_filter' ] = $row->pick_filter;
$field_params[ 'pick_orderby' ] = $row->pick_orderby;
@@ -323,38 +342,114 @@ function migrate_relationships () {
if ( true === $this->check_progress( __FUNCTION__ ) )
return '1';
- // go through each relationship row
- // 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'
- */
+ global $wpdb;
- // NEW TABLE:
- /*
- `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
- `pod_id` INT(10) UNSIGNED NULL DEFAULT NULL,
- `field_id` INT(10) UNSIGNED NULL DEFAULT NULL,
- `item_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
- `related_pod_id` INT(10) UNSIGNED NULL DEFAULT NULL,
- `related_field_id` INT(10) UNSIGNED NULL DEFAULT NULL,
- `related_item_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
- `weight` SMALLINT(5) UNSIGNED NULL DEFAULT '0'
- */
+ $api = pods_api();
+
+ $last_id = (int) $this->check_progress( __FUNCTION__ );
+
+ $sql = "
+ SELECT *
+ FROM `@wp_pod_rel`
+ WHERE {$last_id} < `id`
+ ORDER BY `id`
+ 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 () {
@@ -64,7 +64,7 @@
$( '#pods-wizard-next' ).show();
}
},
- migrate : function () {
+ migrate : function ( postdata ) {
var pods_ajaxurl = $( '#pods-wizard-box' ).data( 'url' );
if ( 'undefined' != typeof pods_ajaxurl )
@@ -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) )
pods_ajaxurl = ajaxurl + '?pods_ajax=1';
- if ( $( '#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 || $( '#pods-wizard-panel-3 table tbody tr.pods-wizard-table-pending' )[ 0 ] ) {
+ 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 = {
- 'action' : $( '#pods-wizard-box' ).data( 'action' ),
- 'method' : $( '#pods-wizard-box' ).data( 'method' ),
- '_wpnonce' : $( '#pods-wizard-box' ).data( '_wpnonce' ),
- 'step' : 'migrate',
- 'type' : $row.data( 'upgrade' ),
- 'pod' : ''
- };
+ var postdata = {
+ 'action' : $( '#pods-wizard-box' ).data( 'action' ),
+ 'method' : $( '#pods-wizard-box' ).data( 'method' ),
+ '_wpnonce' : $( '#pods-wizard-box' ).data( '_wpnonce' ),
+ 'step' : 'migrate',
+ 'type' : $row.data( 'upgrade' ),
+ 'pod' : ''
+ };
- if ( 'undefined' != typeof $row.data( 'pod' ) )
- postdata[ 'pod' ] = $row.data( 'pod' );
+ if ( 'undefined' != typeof $row.data( 'pod' ) )
+ postdata[ 'pod' ] = $row.data( 'pod' );
+ }
$.ajax( {
type : 'POST',
@@ -97,10 +99,16 @@
data : postdata,
success : function ( d ) {
if ( -1 == d.indexOf( '<e>' ) && -1 != d ) {
- $row.removeClass( 'pods-wizard-table-active' ).addClass( 'pods-wizard-table-complete' );
-
- // Run next
- return methods[ 'migrate' ]();
+ if ( -2 == d ) {
+ // Run next
+ return methods[ 'migrate' ]( postdata );
+ }
+ else {
+ $row.removeClass( 'pods-wizard-table-active' ).addClass( 'pods-wizard-table-complete' );
+
+ // Run next
+ return methods[ 'migrate' ]();
+ }
}
else if ( -1 == d.indexOf( 'Database Error;' ) ) {
$row.removeClass( 'pods-wizard-table-active' ).addClass( 'pods-wizard-table-error' );

0 comments on commit fd65f5a

Please sign in to comment.