[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Things: GP_Glossary class 4 * 5 * @package GlotPress 6 * @subpackage Things 7 * @since 1.0.0 8 */ 9 10 /** 11 * Core class used to implement the glossaries. 12 * 13 * @since 1.0.0 14 */ 15 class GP_Glossary extends GP_Thing { 16 17 var $table_basename = 'gp_glossaries'; 18 var $field_names = array( 'id', 'translation_set_id', 'description' ); 19 var $int_fields = array( 'id', 'translation_set_id' ); 20 var $non_updatable_attributes = array( 'id' ); 21 22 public $id; 23 public $translation_set_id; 24 public $description; 25 26 /** 27 * Caches the array of Glossary_Entry objects. 28 * 29 * @since 2.3.0 30 * @var entries 31 */ 32 private $entries = array(); 33 34 /** 35 * Sets restriction rules for fields. 36 * 37 * @since 1.0.0 38 * 39 * @param GP_Validation_Rules $rules The validation rules instance. 40 */ 41 public function restrict_fields( $rules ) { 42 $rules->translation_set_id_should_not_be( 'empty' ); 43 } 44 45 /** 46 * Get the path to the glossary. 47 * 48 * @return string 49 */ 50 public function path() { 51 $translation_set = GP::$translation_set->get( $this->translation_set_id ); 52 $project = GP::$project->get( $translation_set->project_id ); 53 54 return gp_url_join( gp_url_project_locale( $project->path, $translation_set->locale, $translation_set->slug ), 'glossary' ); 55 } 56 57 /** 58 * Get the glossary by set/project. 59 * If there's no glossary for this specific project, get the nearest parent glossary 60 * 61 * @param GP_Project $project 62 * @param GP_Translation_Set $translation_set 63 * 64 * @return GP_Glossary|bool 65 */ 66 public function by_set_or_parent_project( $translation_set, $project ) { 67 $glossary = $this->by_set_id( $translation_set->id ); 68 69 if ( ! $glossary ) { 70 if ( 0 === $project->id ) { 71 // Auto-create the Locale Glossary. 72 $glossary = $this->create( array( 'translation_set_id' => $translation_set->id ) ); 73 } elseif ( $project->parent_project_id ) { 74 $locale = $translation_set->locale; 75 $slug = $translation_set->slug; 76 77 while ( ! $glossary && $project->parent_project_id ) { 78 $project = GP::$project->get( $project->parent_project_id ); 79 $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $slug, $locale ); 80 81 if ( $translation_set ) { 82 $glossary = $this->by_set_id( $translation_set->id ); 83 } 84 } 85 } 86 } 87 88 return $glossary; 89 } 90 91 public function by_set_id( $set_id ) { 92 return $this->one( 93 " 94 SELECT * FROM $this->table 95 WHERE translation_set_id = %d LIMIT 1", 96 $set_id 97 ); 98 99 } 100 101 /** 102 * Merges entries of a glossary with another one. 103 * 104 * @since 2.3.0 105 * 106 * @param GP_Glossary $merge The Glossary to merge into the current one. 107 * @return array Array of Glossary_Entry. 108 */ 109 public function merge_with_glossary( GP_Glossary $merge ) { 110 $entry_map = array(); 111 foreach ( $this->get_entries() as $i => $entry ) { 112 $entry_map[ $entry->key() ] = $i; 113 } 114 115 foreach ( $merge->get_entries() as $entry ) { 116 if ( ! isset( $entry_map[ $entry->key() ] ) ) { 117 $this->entries[] = $entry; 118 } 119 } 120 121 return $this->entries; 122 } 123 124 /** 125 * Retrieves entries and cache them. 126 * 127 * @since 2.3.0 128 * 129 * @return array Array of Glossary_Entry. 130 */ 131 public function get_entries() { 132 if ( ! $this->id ) { 133 return false; 134 } 135 136 if ( ! empty( $this->entries ) ) { 137 return $this->entries; 138 } 139 140 return $this->entries = GP::$glossary_entry->by_glossary_id( $this->id ); 141 } 142 143 144 /** 145 * Copies glossary items from a glossary to the current one 146 * This function does not merge then, just copies unconditionally. If a translation already exists, it will be duplicated. 147 * 148 * @param int $source_glossary_id 149 * 150 * @return mixed 151 */ 152 public function copy_glossary_items_from( $source_glossary_id ) { 153 global $wpdb; 154 155 $current_date = $this->now_in_mysql_format(); 156 157 return $this->query( 158 "INSERT INTO $wpdb->gp_glossary_items ( 159 id, term, type, examples, comment, suggested_translation, last_update 160 ) 161 SELECT 162 %s AS id, term, type, examples, comment, suggested_translation, %s AS last_update 163 FROM $wpdb->gp_glossary_items WHERE id = %s", 164 $this->id, 165 $current_date, 166 $source_glossary_id 167 ); 168 } 169 170 /** 171 * Deletes a glossary and all of it's entries. 172 * 173 * @since 2.0.0 174 * 175 * @return bool 176 */ 177 public function delete() { 178 GP::$glossary_entry->delete_many( array( 'glossary_id', $this->id ) ); 179 180 return parent::delete(); 181 } 182 183 /** 184 * Get the virtual Locale Glossary project 185 * 186 * @since 2.3.0 187 * 188 * @return GP_Project The project 189 */ 190 public function get_locale_glossary_project() { 191 /** 192 * Filters the prefix for the locale glossary path. 193 * 194 * @since 2.3.1 195 * 196 * @param string $locale_glossary_path_prefix Prefix for the locale glossary path. 197 */ 198 $locale_glossary_path_prefix = apply_filters( 'gp_locale_glossary_path_prefix', '/languages' ); 199 200 // A leading double-slash prevents gp_url_project() from prepending /projects/ to the URL. 201 $locale_glossary_path_prefix = '//' . ltrim( $locale_glossary_path_prefix, '/' ); 202 203 return new GP::$project( 204 array( 205 'id' => 0, 206 'name' => 'Locale Glossary', 207 'slug' => 0, 208 'path' => $locale_glossary_path_prefix, 209 ) 210 ); 211 } 212 } 213 214 GP::$glossary = new GP_Glossary();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:01:04 2024 | Cross-referenced by PHPXref 0.7.1 |