[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/things/ -> glossary.php (source)

   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              SELECT * FROM $this->table
  94              WHERE translation_set_id = %d LIMIT 1", $set_id );
  95  
  96      }
  97  
  98      /**
  99       * Merges entries of a glossary with another one.
 100       *
 101       * @since 2.3.0
 102       *
 103       * @param GP_Glossary $merge The Glossary to merge into the current one.
 104       * @return array Array of Glossary_Entry.
 105       */
 106  	public function merge_with_glossary( GP_Glossary $merge ) {
 107          $entry_map = array();
 108          foreach ( $this->get_entries() as $i => $entry ) {
 109              $entry_map[ $entry->key() ] = $i;
 110          }
 111  
 112          foreach ( $merge->get_entries() as $entry ) {
 113              if ( ! isset( $entry_map[ $entry->key() ] ) ) {
 114                  $this->entries[] = $entry;
 115              }
 116          }
 117  
 118          return $this->entries;
 119      }
 120  
 121      /**
 122       * Retrieves entries and cache them.
 123       *
 124       * @since 2.3.0
 125       *
 126       * @return array Array of Glossary_Entry.
 127       */
 128  	public function get_entries() {
 129          if ( ! $this->id ) {
 130              return false;
 131          }
 132  
 133          if ( ! empty( $this->entries ) ) {
 134              return $this->entries;
 135          }
 136  
 137          return $this->entries = GP::$glossary_entry->by_glossary_id( $this->id );
 138      }
 139  
 140  
 141      /**
 142       * Copies glossary items from a glossary to the current one
 143       * This function does not merge then, just copies unconditionally. If a translation already exists, it will be duplicated.
 144       *
 145       * @param int $source_glossary_id
 146       *
 147       * @return mixed
 148       */
 149  	public function copy_glossary_items_from( $source_glossary_id ) {
 150          global $wpdb;
 151  
 152          $current_date = $this->now_in_mysql_format();
 153  
 154          return $this->query("
 155              INSERT INTO $wpdb->gp_glossary_items (
 156                  id, term, type, examples, comment, suggested_translation, last_update
 157              )
 158              SELECT
 159                  %s AS id, term, type, examples, comment, suggested_translation, %s AS last_update
 160              FROM $wpdb->gp_glossary_items WHERE id = %s", $this->id, $current_date, $source_glossary_id
 161          );
 162      }
 163  
 164      /**
 165       * Deletes a glossary and all of it's entries.
 166       *
 167       * @since 2.0.0
 168       *
 169       * @return bool
 170       */
 171  	public function delete() {
 172          GP::$glossary_entry->delete_many( array( 'glossary_id', $this->id ) );
 173  
 174          return parent::delete();
 175      }
 176  
 177      /**
 178       * Get the virtual Locale Glossary project
 179       *
 180       * @since 2.3.0
 181       *
 182       * @return GP_Project The project
 183       */
 184  	public function get_locale_glossary_project() {
 185          /**
 186           * Filters the prefix for the locale glossary path.
 187           *
 188           * @since 2.3.1
 189           *
 190           * @param string $locale_glossary_path_prefix Prefix for the locale glossary path.
 191           */
 192          $locale_glossary_path_prefix = apply_filters( 'gp_locale_glossary_path_prefix', '/languages' );
 193  
 194          // A leading double-slash prevents gp_url_project() from prepending /projects/ to the URL.
 195          $locale_glossary_path_prefix = '//' . ltrim( $locale_glossary_path_prefix, '/' );
 196  
 197          return new GP::$project( array(
 198              'id'   => 0,
 199              'name' => 'Locale Glossary',
 200              'slug' => 0,
 201              'path' => $locale_glossary_path_prefix,
 202          ) );
 203      }
 204  }
 205  
 206  GP::$glossary = new GP_Glossary();


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1