| [ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 class GP_Original extends GP_Thing { 3 4 var $table_basename = 'originals'; 5 var $field_names = array( 'id', 'project_id', 'context', 'singular', 'plural', 'references', 'comment', 'status', 'priority', 'date_added' ); 6 var $non_updatable_attributes = array( 'id', 'path' ); 7 8 static $priorities = array( '-2' => 'hidden', '-1' => 'low', '0' => 'normal', '1' => 'high' ); 9 static $count_cache_group = 'active_originals_count_by_project_id'; 10 11 function restrict_fields( $original ) { 12 $original->singular_should_not_be('empty'); 13 $original->status_should_not_be('empty'); 14 $original->project_id_should_be('positive_int'); 15 $original->priority_should_be('int'); 16 $original->priority_should_be('between', -2, 1); 17 } 18 19 function normalize_fields( $args ) { 20 $args = (array)$args; 21 foreach ( array('plural', 'context', 'references', 'comment') as $field ) { 22 if ( isset( $args['parent_project_id'] ) ) { 23 $args[$field] = $this->force_false_to_null( $args[$field] ); 24 } 25 } 26 return $args; 27 } 28 29 function by_project_id( $project_id ) { 30 return $this->many( "SELECT * FROM $this->table WHERE project_id= %d AND status = '+active'", $project_id ); 31 } 32 33 function count_by_project_id( $project_id ) { 34 if ( false !== ( $cached = wp_cache_get( $project_id, self::$count_cache_group ) ) ) { 35 return $cached; 36 } 37 $count = $this->value( "SELECT COUNT(*) FROM $this->table WHERE project_id= %d AND status = '+active'", $project_id ); 38 wp_cache_set( $project_id, $count, self::$count_cache_group ); 39 return $count; 40 } 41 42 43 function by_project_id_and_entry( $project_id, $entry, $status = null ) { 44 global $gpdb; 45 $where = array(); 46 // now each condition has to contain a %s not to break the sequence 47 $where[] = is_null( $entry->context )? '(context IS NULL OR %s IS NULL)' : 'BINARY context = %s'; 48 $where[] = 'BINARY singular = %s'; 49 $where[] = is_null( $entry->plural )? '(plural IS NULL OR %s IS NULL)' : 'BINARY plural = %s'; 50 $where[] = 'project_id = %d'; 51 if ( !is_null( $status ) ) $where[] = $gpdb->prepare( 'status = %s', $status ); 52 $where = implode( ' AND ', $where ); 53 return $this->one( "SELECT * FROM $this->table WHERE $where", $entry->context, $entry->singular, $entry->plural, $project_id ); 54 } 55 56 function import_for_project( $project, $translations ) { 57 global $gpdb; 58 wp_cache_delete( $project->id, self::$count_cache_group ); 59 $originals_added = $originals_existing = 0; 60 $all_originals_for_project = $this->many_no_map( "SELECT * FROM $this->table WHERE project_id= %d", $project->id ); 61 $originals_by_key = array(); 62 foreach( $all_originals_for_project as $original ) { 63 $entry = new Translation_Entry( array( 'singular' => $original->singular, 'plural' => $original->plural, 'context' => $original->context ) ); 64 $originals_by_key[$entry->key()] = $original; 65 } 66 foreach( $translations->entries as $entry ) { 67 $gpdb->queries = array(); 68 $data = array('project_id' => $project->id, 'context' => $entry->context, 'singular' => $entry->singular, 69 'plural' => $entry->plural, 'comment' => $entry->extracted_comments, 70 'references' => implode( ' ', $entry->references ), 'status' => '+active' ); 71 72 // TODO: do not obsolete similar translations 73 $original = $originals_by_key[$entry->key()]; 74 if ( isset( $original ) ) { 75 if ( GP::$original->should_be_updated_with( $data, $original ) ) { 76 $this->update( $data, array( 'id' => $original->id ) ); 77 } 78 $originals_existing++; 79 } else { 80 GP::$original->create( $data ); 81 $originals_added++; 82 } 83 } 84 // Mark previously active, but now removed strings as obsolete 85 foreach ( $originals_by_key as $key => $value) { 86 if ( !key_exists($key, $translations->entries ) ) { 87 $this->update( array('status' => '-obsolete'), array( 'id' => $value->id ) ); 88 } 89 } 90 return array( $originals_added, $originals_existing ); 91 } 92 93 function should_be_updated_with( $data, $original = null ) { 94 if ( !$original ) $original = $this; 95 foreach( $data as $field => $value ) { 96 if ( $original->$field != $value ) return true; 97 } 98 return false; 99 } 100 } 101 GP::$original = new GP_Original();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed May 22 03:59:55 2013 | Hosted by follow the white rabbit. |