Ticket #173: 173.0.patch
| File 173.0.patch, 3.2 KB (added by petervanderdoes, 17 months ago) |
|---|
-
gp-includes/things/original.php
1 1 <?php 2 2 class GP_Original extends GP_Thing { 3 3 4 4 var $table_basename = 'originals'; 5 5 var $field_names = array( 'id', 'project_id', 'context', 'singular', 'plural', 'references', 'comment', 'status', 'priority', 'date_added' ); 6 6 var $non_updatable_attributes = array( 'id', 'path' ); 7 7 8 8 static $priorities = array( '-2' => 'hidden', '-1' => 'low', '0' => 'normal', '1' => 'high' ); 9 9 static $count_cache_group = 'active_originals_count_by_project_id'; 10 10 … … 25 25 } 26 26 return $args; 27 27 } 28 28 29 29 function by_project_id( $project_id ) { 30 30 return $this->many( "SELECT * FROM $this->table WHERE project_id= %d AND status = '+active'", $project_id ); 31 31 } … … 33 33 function count_by_project_id( $project_id ) { 34 34 if ( false !== ( $cached = wp_cache_get( $project_id, self::$count_cache_group ) ) ) { 35 35 return $cached; 36 } 36 } 37 37 $count = $this->value( "SELECT COUNT(*) FROM $this->table WHERE project_id= %d AND status = '+active'", $project_id ); 38 38 wp_cache_set( $project_id, $count, self::$count_cache_group ); 39 39 return $count; … … 52 52 $where = implode( ' AND ', $where ); 53 53 return $this->one( "SELECT * FROM $this->table WHERE $where", $entry->context, $entry->singular, $entry->plural, $project_id ); 54 54 } 55 55 56 56 function import_for_project( $project, $translations ) { 57 57 global $gpdb; 58 58 wp_cache_delete( $project->id, self::$count_cache_group ); 59 59 $originals_added = $originals_existing = 0; 60 60 $all_originals_for_project = $this->many_no_map( "SELECT * FROM $this->table WHERE project_id= %d", $project->id ); 61 $this->update( array( 'status' => '+obsolete' ), array( 'project_id' => $project->id, 'status' => '+active' ) );62 61 $originals_by_key = array(); 63 62 foreach( $all_originals_for_project as $original ) { 64 63 $entry = new Translation_Entry( array( 'singular' => $original->singular, 'plural' => $original->plural, 'context' => $original->context ) ); … … 69 68 $data = array('project_id' => $project->id, 'context' => $entry->context, 'singular' => $entry->singular, 70 69 'plural' => $entry->plural, 'comment' => $entry->extracted_comments, 71 70 'references' => implode( ' ', $entry->references ), 'status' => '+active' ); 72 71 73 72 // TODO: do not obsolete similar translations 74 73 $original = $originals_by_key[$entry->key()]; 75 74 if ( isset( $original ) ) { … … 82 81 $originals_added++; 83 82 } 84 83 } 85 $this->update( array('status' => '-obsolete'), array('project_id' => $project->id, 'status' => '+obsolete')); 84 // Run over the originals and if the translation doesn't exists in the newly imported translatio, make the entry obsolete 85 foreach ( $originals_by_key as $key => $value) { 86 if ( !key_exists($key, $translations->entries ) ) { 87 $this->update( array('status' => '-obsolete'), array( 'id' => $original->id ) ); 88 } 89 } 86 90 return array( $originals_added, $originals_existing ); 87 91 } 88 92 89 93 function should_be_updated_with( $original, $data ) { 90 94 foreach( $data as $field => $value ) { 91 95 if ( $original->$field != $value ) return true;
