| [ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 class GP_Translation extends GP_Thing { 3 4 var $per_page = 15; 5 var $table_basename = 'translations'; 6 var $field_names = array( 'id', 'original_id', 'translation_set_id', 'translation_0', 'translation_1', 'translation_2', 'translation_3', 'translation_4', 'translation_5','user_id', 'status', 'date_added', 'date_modified', 'warnings'); 7 var $non_updatable_attributes = array( 'id', ); 8 9 static $statuses = array('current', 'waiting', 'rejected', 'fuzzy', 'old', ); 10 static $number_of_plural_translations = 6; 11 12 function normalize_fields( $args ) { 13 $args = (array)$args; 14 if ( isset( $args['translations'] ) && is_array( $args['translations'] ) ) { 15 foreach( range( 0, $this->get_static( 'number_of_plural_translations') ) as $i ) { 16 if ( isset( $args['translations'][$i] ) ) $args["translation_$i"] = $args['translations'][$i]; 17 } 18 unset( $args['translations'] ); 19 } 20 foreach( range( 0, $this->get_static( 'number_of_plural_translations' ) ) as $i ) { 21 if ( isset( $args["translation_$i"] ) ) { 22 $args["translation_$i"] = $this->fix_translation( $args["translation_$i"] ); 23 } 24 } 25 if ( gp_array_get( $args, 'warnings' ) == array() ) { 26 $args['warnings'] = null; 27 } 28 return $args; 29 } 30 31 function prepare_fields_for_save( $args ) { 32 $args = parent::prepare_fields_for_save( $args ); 33 if ( is_array( gp_array_get( $args, 'warnings' ) ) ) { 34 $args['warnings'] = serialize( $args['warnings'] ); 35 } 36 return $args; 37 } 38 39 function fix_translation( $translation ) { 40 // when selecting some browsers take the newlines and some don't 41 // that's why we don't want to insert too many newlines for each ↵ 42 $translation = str_replace( "↵\n", "↵", $translation ); 43 return str_replace( '↵', "\n", $translation ); 44 } 45 46 function restrict_fields( $translation ) { 47 $translation->translation_0_should_not_be( 'empty' ); 48 $translation->status_should_not_be( 'empty' ); 49 $translation->original_id_should_be( 'positive_int' ); 50 $translation->translation_set_id_should_be( 'positive_int' ); 51 $translation->user_id_should_be( 'positive_int' ); 52 } 53 54 55 function set_fields( $db_object ) { 56 parent::set_fields( $db_object ); 57 if ( $this->warnings ) { 58 $this->warnings = maybe_unserialize( $this->warnings ); 59 } 60 } 61 62 function for_export( $project, $translation_set, $filters = null ) { 63 return GP::$translation->for_translation( $project, $translation_set, 'no-limit', $filters? $filters : array( 'status' => 'current_or_untranslated' ) ); 64 } 65 66 function for_translation( $project, $translation_set, $page, $filters = array(), $sort = array() ) { 67 global $gpdb; 68 $locale = GP_Locales::by_slug( $translation_set->locale ); 69 $status_cond = ''; 70 $join_type = 'INNER'; 71 72 $sort_bys = array('original' => 'o.singular %s', 'translation' => 't.translation_0 %s', 'priority' => 'o.priority %s, o.date_added DESC', 73 'random' => 'o.priority DESC, RAND()', 'translation_date_added' => 't.date_added %s', 'original_date_added' => 'o.date_added %s', 74 'references' => 'o.references' ); 75 76 $default_sort = GP::$user->current()->get_meta('default_sort'); 77 if ( ! is_array($default_sort) ) { 78 $default_sort = array( 79 'by' => 'priority', 80 'how' => 'desc' 81 ); 82 } 83 84 $sort_by = gp_array_get( $sort_bys, gp_array_get( $sort, 'by' ), gp_array_get( $sort_bys, $default_sort['by'] ) ); 85 $sort_hows = array('asc' => 'ASC', 'desc' => 'DESC', ); 86 $sort_how = gp_array_get( $sort_hows, gp_array_get( $sort, 'how' ), gp_array_get( $sort_hows, $default_sort['how'] ) ); 87 88 $where = array(); 89 if ( gp_array_get( $filters, 'term' ) ) { 90 $like = "LIKE '%" . ( $gpdb->escape( like_escape ( gp_array_get( $filters, 'term' ) ) ) ) . "%'"; 91 $where[] = '(' . implode( ' OR ', array_map( lambda('$x', '"($x $like)"', compact('like')), array('o.singular', 't.translation_0', 'o.plural', 't.translation_1', 'o.context', 'o.references' ) ) ) . ')'; 92 } 93 if ( gp_array_get( $filters, 'before_date_added' ) ) { 94 $where[] = $gpdb->prepare( 't.date_added > %s', gp_array_get( $filters, 'before_date_added' ) ); 95 } 96 if ( gp_array_get( $filters, 'translation_id' ) ) { 97 $where[] = $gpdb->prepare( 't.id = %d', gp_array_get( $filters, 'translation_id' ) ); 98 } 99 if ( gp_array_get( $filters, 'original_id' ) ) { 100 $where[] = $gpdb->prepare( 'o.id = %d', gp_array_get( $filters, 'original_id' ) ); 101 } 102 if ( 'yes' == gp_array_get( $filters, 'warnings' ) ) { 103 $where[] = 't.warnings IS NOT NULL'; 104 } elseif ( 'no' == gp_array_get( $filters, 'warnings' ) ) { 105 $where[] = 't.warnings IS NULL'; 106 } 107 if ( 'yes' == gp_array_get( $filters, 'with_context' ) ) { 108 $where[] = 'o.context IS NOT NULL'; 109 } 110 if ( 'yes' == gp_array_get( $filters, 'with_comment' ) ) { 111 $where[] = 'o.comment IS NOT NULL AND o.comment <> ""'; 112 } 113 114 if ( gp_array_get( $filters, 'user_login' ) ) { 115 $user = GP::$user->by_login( $filters['user_login'] ); 116 // do not return any entries if the user doesn't exist 117 $where[] = $gpdb->prepare( 't.user_id = %d', ($user && $user->id)? $user->id : -1 ); 118 } 119 120 if ( !GP::$user->current()->can( 'write', 'project', $project->id ) ) { 121 $where[] = 'o.priority > -2'; 122 } 123 124 $join_where = array(); 125 $status = gp_array_get( $filters, 'status', 'current_or_waiting_or_fuzzy_or_untranslated' ); 126 $statuses = explode( '_or_', $status ); 127 if ( in_array( 'untranslated', $statuses ) ) { 128 if ( $statuses == array( 'untranslated' ) ) { 129 $where[] = 't.translation_0 IS NULL'; 130 } 131 $join_type = 'LEFT'; 132 $join_where[] = 't.status != "rejected"'; 133 $statuses = array_filter( $statuses, lambda( '$x', '$x != "untranslated"' ) ); 134 } 135 136 $statuses = array_filter( $statuses, lambda( '$s', 'in_array($s, $statuses)', array( 'statuses' => $this->get_static( 'statuses' ) ) ) ); 137 if ( $statuses ) { 138 $statuses_where = array(); 139 foreach( $statuses as $single_status ) { 140 $statuses_where[] = $gpdb->prepare( 't.status = %s', $single_status ); 141 } 142 $statuses_where = '(' . implode( ' OR ', $statuses_where ) . ')'; 143 $join_where[] = $statuses_where; 144 } 145 146 $where = implode( ' AND ', $where ); 147 if ( $where ) { 148 $where = 'AND '.$where; 149 } 150 151 $join_where = implode( ' AND ', $join_where ); 152 if ( $join_where ) { 153 $join_where = 'AND '.$join_where; 154 } 155 156 $sql_sort = sprintf( $sort_by, $sort_how ); 157 158 $limit = $this->sql_limit_for_paging( $page, $this->per_page ); 159 160 $sql_for_translations = " 161 SELECT SQL_CALC_FOUND_ROWS t.*, o.*, t.id as id, o.id as original_id, t.status as translation_status, o.status as original_status, t.date_added as translation_added, o.date_added as original_added 162 FROM $gpdb->originals as o 163 $join_type JOIN $gpdb->translations AS t ON o.id = t.original_id AND t.translation_set_id = ".$gpdb->escape($translation_set->id)." $join_where 164 WHERE o.project_id = ".$gpdb->escape( $project->id )." AND o.status LIKE '+%' $where ORDER BY $sql_sort $limit"; 165 $rows = $this->many_no_map( $sql_for_translations ); 166 $this->found_rows = $this->found_rows(); 167 $translations = array(); 168 foreach( (array)$rows as $row ) { 169 if ( $row->user_id && $this->per_page != 'no-limit' ) { 170 $user = GP::$user->get( $row->user_id ); 171 if ( $user ) { 172 $row->user_login = $user->user_login; 173 $row->user_display_name = $user->display_name; 174 } 175 } else { 176 $row->user_login = $row->user_display_name = ''; 177 } 178 $row->translations = array(); 179 for( $i = 0; $i < $locale->nplurals; $i++ ) { 180 $row->translations[] = $row->{"translation_".$i}; 181 } 182 $row->references = preg_split('/\s+/', $row->references, -1, PREG_SPLIT_NO_EMPTY); 183 $row->extracted_comment = $row->comment; 184 $row->warnings = $row->warnings? maybe_unserialize( $row->warnings ) : null; 185 unset($row->comment); 186 foreach( range( 0, $this->get_static( 'number_of_plural_translations' ) ) as $i ) { 187 $member = "translation_$i"; 188 unset($row->$member); 189 } 190 $row->row_id = $row->original_id . ( $row->id? "-$row->id" : '' ); 191 $translations[] = new Translation_Entry( (array)$row ); 192 } 193 unset( $rows ); 194 return $translations; 195 } 196 197 function set_as_current() { 198 return $this->update( array('status' => 'old'), 199 array('original_id' => $this->original_id, 'translation_set_id' => $this->translation_set_id, 'status' => 'current') ) 200 && $this->update( array('status' => 'old'), 201 array('original_id' => $this->original_id, 'translation_set_id' => $this->translation_set_id, 'status' => 'waiting') ) 202 && $this->update( array('status' => 'old'), 203 array('original_id' => $this->original_id, 'translation_set_id' => $this->translation_set_id, 'status' => 'fuzzy') ) 204 && $this->update( array('status' => 'current') ); 205 } 206 207 function reject() { 208 $this->set_status( 'rejected' ); 209 } 210 211 function set_status( $status ) { 212 if ( 'current' == $status ) 213 return $this->set_as_current(); 214 else 215 return $this->update( array( 'status' => $status ) ); 216 } 217 218 function translations() { 219 $translations = array(); 220 foreach( range( 0, $this->get_static( 'number_of_plural_translations' ) ) as $i ) { 221 $translations[$i] = isset( $this->{"translation_$i"} )? $this->{"translation_$i"} : null; 222 } 223 return $translations; 224 } 225 226 function last_modified( $translation_set ) { 227 global $gpdb; 228 229 return $gpdb->get_var( $gpdb->prepare( "SELECT date_modified FROM {$this->table} WHERE translation_set_id = %d AND status = %s ORDER BY date_modified DESC LIMIT 1", $translation_set->id, 'current' ) ); 230 } 231 } 232 GP::$translation = new GP_Translation();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun May 19 03:59:52 2013 | Hosted by follow the white rabbit. |