[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/routes/ -> glossary-entry.php (source)

   1  <?php
   2  /**
   3   * Routes: GP_Route_Glossary_Entry class
   4   *
   5   * @package GlotPress
   6   * @subpackage Routes
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the glossary entry route.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Route_Glossary_Entry extends GP_Route_Main {
  16  
  17  	public function glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
  18          $project         = GP::$project->by_path( $project_path );
  19          $locale          = GP_Locales::by_slug( $locale_slug );
  20  
  21          if ( ! $project || ! $locale ) {
  22              return $this->die_with_404();
  23          }
  24  
  25          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
  26  
  27          if ( ! $translation_set ){
  28              return $this->die_with_404();
  29          }
  30  
  31          $glossary = GP::$glossary->by_set_or_parent_project( $translation_set, $project );
  32  
  33          if ( ! $glossary ){
  34              return $this->die_with_404();
  35          }
  36  
  37          $glossary_entries = GP::$glossary_entry->by_glossary_id( $glossary->id );
  38  
  39          foreach ( $glossary_entries as $key => $entry ) {
  40              $user = get_userdata( $entry->last_edited_by );
  41  
  42              if ( $user ) {
  43                  $glossary_entries[$key]->user_login = $user->user_login;
  44                  $glossary_entries[$key]->user_display_name = $user->display_name;
  45              }
  46          }
  47  
  48          $can_edit = $this->can( 'approve', 'translation-set', $translation_set->id );
  49          $url      = gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) );
  50  
  51          $this->tmpl( 'glossary-view', get_defined_vars() );
  52      }
  53  
  54  	public function glossary_entry_add_post( $project_path, $locale_slug, $translation_set_slug ) {
  55          $project         = GP::$project->by_path( $project_path );
  56          $locale          = GP_Locales::by_slug( $locale_slug );
  57  
  58          if ( ! $project || ! $locale ) {
  59              return $this->die_with_404();
  60          }
  61  
  62          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
  63          if ( ! $translation_set ){
  64              return $this->die_with_404();
  65          }
  66  
  67          if ( $this->invalid_nonce_and_redirect( 'add-glossary-entry_' . $project->path . $locale_slug . $translation_set_slug ) ) {
  68              return;
  69          }
  70  
  71          if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
  72              return;
  73          }
  74          $new_glossary_entry = new GP_Glossary_Entry( gp_post('new_glossary_entry') );
  75          $new_glossary_entry->last_edited_by = get_current_user_id();
  76  
  77          $glossary = GP::$glossary->get( $new_glossary_entry->glossary_id );
  78  
  79          if ( ! $new_glossary_entry->validate() ) {
  80              $this->errors = $new_glossary_entry->errors;
  81              $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
  82          }
  83          else {
  84              preg_match( '/\b' . preg_quote( $new_glossary_entry->term, '/' ) . '\b/i', $new_glossary_entry->term, $m );
  85  
  86              if ( $m[0] !== $new_glossary_entry->term ) {
  87                  $this->errors[] = __( 'Glossary terms cannot contain word break or multi-byte characters!', 'glotpress' );
  88                  $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
  89              } else {
  90                  $find_parms = array(
  91                      'glossary_id' => $glossary->id,
  92                      'term'        => $new_glossary_entry->term,
  93                      'translation' => $new_glossary_entry->translation,
  94                  );
  95  
  96                  if ( GP::$glossary_entry->find_one( $find_parms ) ) {
  97                      // Translators: 1: the glossary term that was attempted to be added, 2: the part of speach that was attempted to be added.
  98                      $this->errors[] = sprintf( __( 'Identical glossary entry already exists for "%1$s" as "%2$s"!', 'glotpress' ), esc_html( $new_glossary_entry->term ), esc_html( $new_glossary_entry->part_of_speech ) );
  99                      $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
 100                  } else {
 101                      $created_glossary_entry = GP::$glossary_entry->create_and_select( $new_glossary_entry );
 102  
 103                      if ( ! $created_glossary_entry ) {
 104                          $this->errors[] = __( 'Error in creating glossary entry!', 'glotpress' );
 105                          $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
 106                      } else {
 107                          $this->notices[] = __( 'The glossary entry was created!', 'glotpress' );
 108                          $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
 109                      }
 110                  }
 111              }
 112          }
 113      }
 114  
 115  	public function glossary_entries_post( $project_path, $locale_slug, $translation_set_slug ) {
 116          $ge_post         = gp_post('glossary_entry');
 117          $ge             = array_shift( $ge_post );
 118          $glossary_entry  = GP::$glossary_entry->get( absint( $ge['glossary_entry_id'] ) );
 119  
 120          if ( ! $glossary_entry ){
 121              return $this->die_with_error( __( 'The glossary entry cannot be found', 'glotpress' ), 200 );
 122          }
 123  
 124          if ( ! $this->verify_nonce( 'edit-glossary-entry_' . $glossary_entry->id ) ) {
 125              return $this->die_with_error( __( 'An error has occurred. Please try again.', 'glotpress' ), 403 );
 126          }
 127  
 128          $glossary        = GP::$glossary->get( $glossary_entry->glossary_id );
 129          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 130          $can_edit        = $this->can( 'approve', 'translation-set', $translation_set->id );
 131  
 132          if ( ! $can_edit ) {
 133              return $this->die_with_error( __( 'Forbidden', 'glotpress' ), 403 );
 134          }
 135  
 136          $project = GP::$project->get( $translation_set->project_id );
 137          $locale  = GP_Locales::by_slug( $translation_set->locale );
 138  
 139          $new_glossary_entry = new GP_Glossary_Entry( $ge );
 140          $new_glossary_entry->last_edited_by = get_current_user_id();
 141  
 142          if ( ! $new_glossary_entry->validate() ) {
 143              $this->errors = $new_glossary_entry->errors;
 144          }
 145          else {
 146              if ( ! $glossary_entry->update( $new_glossary_entry ) ) {
 147                  $this->errors = $glossary_entry->errors;
 148              }
 149          }
 150  
 151          if ( $this->errors ) {
 152              $error_output = '<ul>';
 153              foreach ( $this->errors as $error ) {
 154                  $error_output .= '<li>' . $error . '</li>';
 155              }
 156              $error_output .= '</ul>';
 157  
 158              return $this->die_with_error( $error_output, 200 );
 159          }
 160          else {
 161              $entry = $glossary_entry->reload();
 162              $user = get_userdata( $entry->last_edited_by );
 163              $entry->user_login = $user ? $user->user_login : '';
 164              $entry->user_display_name = $user ? $user->display_name : '';
 165              $output = gp_tmpl_get_output( 'glossary-entry-row', get_defined_vars() );
 166  
 167              echo wp_json_encode( $output );
 168          }
 169  
 170          exit();
 171      }
 172  
 173  	public function glossary_entry_delete_post() {
 174          $ge_post         = gp_post('glossary_entry');
 175          $ge             = array_shift( $ge_post );
 176          $glossary_entry = GP::$glossary_entry->get( absint( $ge['glossary_entry_id'] ) );
 177  
 178          if ( ! $glossary_entry ) {
 179              return $this->die_with_error( __( 'The glossary entry cannot be found', 'glotpress' ), 200 );
 180          }
 181  
 182          if ( ! $this->verify_nonce( 'delete-glossary-entry_' . $glossary_entry->id ) ) {
 183              return $this->die_with_error( __( 'An error has occurred. Please try again.', 'glotpress' ), 403 );
 184          }
 185  
 186          $glossary        = GP::$glossary->get( $glossary_entry->glossary_id );
 187          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 188          $can_edit        = $this->can( 'approve', 'translation-set', $translation_set->id );
 189  
 190          if ( ! $can_edit ) {
 191              return $this->die_with_error( __( 'Forbidden', 'glotpress' ), 403 );
 192          }
 193  
 194          if ( ! $glossary_entry->delete() ) {
 195              $this->errors[] = __( 'Error in deleting glossary entry!', 'glotpress' );
 196          }
 197  
 198          if ( ! empty( $this->errors ) ) {
 199              $error_output = '<ul>';
 200              foreach ( $this->errors as $error ) {
 201                  $error_output .= '<li>' . $error . '</li>';
 202              }
 203              $error_output .= '</ul>';
 204  
 205              return $this->die_with_error( $error_output, 200 );
 206          }
 207  
 208          exit();
 209      }
 210  
 211  	public function export_glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
 212          $project = GP::$project->by_path( $project_path );
 213          $locale  = GP_Locales::by_slug( $locale_slug );
 214  
 215          if ( ! $project || ! $locale ) {
 216              return $this->die_with_404();
 217          }
 218  
 219          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
 220  
 221          if ( ! $translation_set ) {
 222              return $this->die_with_404();
 223          }
 224  
 225          $glossary = GP::$glossary->by_set_id( $translation_set->id );
 226  
 227          if ( ! $glossary ) {
 228              return $this->die_with_404();
 229          }
 230  
 231          $glossary_entries = GP::$glossary_entry->by_glossary_id( $glossary->id );
 232          $filename         = sprintf( '%s-%s-glossary.csv', str_replace( '/', '-', $project->path ), $locale->slug );
 233          $last_modified    = gmdate( 'D, d M Y H:i:s', gp_gmt_strtotime( GP::$glossary_entry->last_modified( $glossary ) ) ) . ' GMT';
 234  
 235          $this->headers_for_download( $filename, $last_modified );
 236          $this->print_export_file( $locale->slug, $glossary_entries );
 237      }
 238  
 239  	public function import_glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
 240          $project = GP::$project->by_path( $project_path );
 241          $locale  = GP_Locales::by_slug( $locale_slug );
 242  
 243          if ( ! $project || ! $locale ) {
 244              return $this->die_with_404();
 245          }
 246  
 247          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
 248  
 249          if ( ! $translation_set ) {
 250              return $this->die_with_404();
 251          }
 252  
 253          if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
 254              return;
 255          }
 256  
 257          $can_edit = $this->can( 'approve', 'translation-set', $translation_set->id );
 258  
 259          $this->tmpl( 'glossary-import', get_defined_vars() );
 260      }
 261  
 262  	public function import_glossary_entries_post( $project_path, $locale_slug, $translation_set_slug ) {
 263          $project = GP::$project->by_path( $project_path );
 264          $locale  = GP_Locales::by_slug( $locale_slug );
 265  
 266          if ( ! $project || ! $locale ) {
 267              return $this->die_with_404();
 268          }
 269  
 270          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
 271  
 272          if ( ! $translation_set ) {
 273              return $this->die_with_404();
 274          }
 275  
 276          $glossary = GP::$glossary->by_set_id( $translation_set->id );
 277  
 278          if ( ! $glossary ) {
 279              return $this->die_with_404();
 280          }
 281  
 282          if ( $this->invalid_nonce_and_redirect( 'import-glossary-entries_' . $project->path . $locale_slug . $translation_set_slug ) ) {
 283              return;
 284          }
 285  
 286          if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
 287              return;
 288          }
 289  
 290          if ( ! is_uploaded_file( $_FILES['import-file']['tmp_name'] ) ) {
 291              $this->redirect_with_error( __( 'Error uploading the file.', 'glotpress' ) );
 292              return;
 293          }
 294  
 295          $replace = gp_post( 'import-flush' );
 296          if ( 'on' === $replace && $this->can( 'approve', 'translation-set', $translation_set->id ) ) {
 297              GP::$glossary_entry->delete_many( array( 'glossary_id' => $glossary->id ) );
 298          }
 299  
 300          $glossary_entries_added = $this->read_glossary_entries_from_file( $_FILES['import-file']['tmp_name'], $glossary->id, $locale->slug );
 301  
 302          if ( empty( $this->errors ) && is_int( $glossary_entries_added ) ) {
 303              $this->notices[] = sprintf( __( '%s glossary entries were added', 'glotpress' ), $glossary_entries_added );
 304          }
 305  
 306          $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
 307      }
 308  
 309  	private function print_export_file( $locale_slug, $entries ) {
 310          $outstream = fopen("php://output", 'w');
 311  
 312          fputcsv( $outstream, array( 'en', $locale_slug, 'pos', 'description' ) );
 313  
 314          foreach ( $entries as $entry ) {
 315              $values = array( $entry->term, $entry->translation, $entry->part_of_speech, $entry->comment );
 316              fputcsv( $outstream, $values );
 317          }
 318  
 319          fclose( $outstream );
 320      }
 321  
 322  	private function read_glossary_entries_from_file( $file, $glossary_id, $locale_slug ) {
 323          $f = fopen( $file, 'r' );
 324          $glossary_entries = 0;
 325  
 326          $data = fgetcsv( $f, 0, ',');
 327  
 328          if ( ! is_array( $data ) ) {
 329              return;
 330          }
 331          else if ( $data[1] !== $locale_slug ) {
 332              $this->redirect_with_error( __( 'Unexpected values in the CSV file header row.', 'glotpress' ) );
 333              return;
 334          }
 335  
 336          while ( ( $data = fgetcsv( $f, 0, ',') ) !== FALSE ) {
 337              // We're only parsing one locale per file right now
 338              if ( count ($data) > 4 ) {
 339                  $data = array_splice( $data, 2, -2 );
 340              }
 341  
 342              $entry_data = array(
 343                  'glossary_id' => $glossary_id,
 344                  'term' => $data[0],
 345                  'translation' => $data[1],
 346                  'part_of_speech' => $data[2],
 347                  'comment' => $data[3],
 348                  'last_edited_by' => get_current_user_id()
 349              );
 350  
 351              $new_glossary_entry = new GP_Glossary_Entry( $entry_data );
 352  
 353              if ( ! $new_glossary_entry->validate() ) {
 354                  continue;
 355              } else {
 356                  $entry_exists = GP::$glossary_entry->find_one( $entry_data );
 357                  if ( $entry_exists ) {
 358                      continue;
 359                  }
 360                  $created_glossary_entry = GP::$glossary_entry->create_and_select( $new_glossary_entry );
 361                  if ( $created_glossary_entry ) {
 362                      $glossary_entries++;
 363                  }
 364              }
 365          }
 366  
 367          fclose($f);
 368          return $glossary_entries;
 369      }
 370  
 371  }


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