[ 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          } else {
  83              preg_match( '/\b' . preg_quote( $new_glossary_entry->term, '/' ) . '\b/i', $new_glossary_entry->term, $m );
  84  
  85              if ( $m[0] !== $new_glossary_entry->term ) {
  86                  $this->errors[] = __( 'Glossary terms cannot contain word break or multi-byte characters!', 'glotpress' );
  87                  $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
  88              } else {
  89                  $find_parms = array(
  90                      'glossary_id' => $glossary->id,
  91                      'term'        => $new_glossary_entry->term,
  92                      'translation' => $new_glossary_entry->translation,
  93                  );
  94  
  95                  if ( GP::$glossary_entry->find_one( $find_parms ) ) {
  96                      // Translators: 1: the glossary term that was attempted to be added, 2: the part of speech that was attempted to be added.
  97                      $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 ) );
  98                      $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
  99                  } else {
 100                      $created_glossary_entry = GP::$glossary_entry->create_and_select( $new_glossary_entry );
 101  
 102                      if ( ! $created_glossary_entry ) {
 103                          $this->errors[] = __( 'Error in creating glossary entry!', 'glotpress' );
 104                          $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
 105                      } else {
 106                          $this->notices[] = __( 'The glossary entry was created!', 'glotpress' );
 107                          $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
 108                      }
 109                  }
 110              }
 111          }
 112      }
 113  
 114  	public function glossary_entries_post( $project_path, $locale_slug, $translation_set_slug ) {
 115          $ge_post        = gp_post( 'glossary_entry' );
 116          $ge             = array_shift( $ge_post );
 117          $glossary_entry = GP::$glossary_entry->get( absint( $ge['glossary_entry_id'] ) );
 118  
 119          if ( ! $glossary_entry ) {
 120              return $this->die_with_error( __( 'The glossary entry cannot be found', 'glotpress' ), 200 );
 121          }
 122  
 123          if ( ! $this->verify_nonce( 'edit-glossary-entry_' . $glossary_entry->id ) ) {
 124              return $this->die_with_error( __( 'An error has occurred. Please try again.', 'glotpress' ), 403 );
 125          }
 126  
 127          $glossary        = GP::$glossary->get( $glossary_entry->glossary_id );
 128          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 129          $can_edit        = $this->can( 'approve', 'translation-set', $translation_set->id );
 130  
 131          if ( ! $can_edit ) {
 132              return $this->die_with_error( __( 'Forbidden', 'glotpress' ), 403 );
 133          }
 134  
 135          $project = GP::$project->get( $translation_set->project_id );
 136          $locale  = GP_Locales::by_slug( $translation_set->locale );
 137  
 138          $new_glossary_entry                 = new GP_Glossary_Entry( $ge );
 139          $new_glossary_entry->last_edited_by = get_current_user_id();
 140  
 141          if ( ! $new_glossary_entry->validate() ) {
 142              $this->errors = $new_glossary_entry->errors;
 143          } else {
 144              if ( ! $glossary_entry->update( $new_glossary_entry ) ) {
 145                  $this->errors = $glossary_entry->errors;
 146              }
 147          }
 148  
 149          if ( $this->errors ) {
 150              $error_output = '<ul>';
 151              foreach ( $this->errors as $error ) {
 152                  $error_output .= '<li>' . $error . '</li>';
 153              }
 154              $error_output .= '</ul>';
 155  
 156              return $this->die_with_error( $error_output, 200 );
 157          } else {
 158              $entry                    = $glossary_entry->reload();
 159              $user                     = get_userdata( $entry->last_edited_by );
 160              $entry->user_login        = $user ? $user->user_login : '';
 161              $entry->user_display_name = $user ? $user->display_name : '';
 162              $output                   = gp_tmpl_get_output( 'glossary-entry-row', get_defined_vars() );
 163  
 164              echo wp_json_encode( $output );
 165          }
 166  
 167          exit();
 168      }
 169  
 170  	public function glossary_entry_delete_post() {
 171          $ge_post        = gp_post( 'glossary_entry' );
 172          $ge             = array_shift( $ge_post );
 173          $glossary_entry = GP::$glossary_entry->get( absint( $ge['glossary_entry_id'] ) );
 174  
 175          if ( ! $glossary_entry ) {
 176              return $this->die_with_error( __( 'The glossary entry cannot be found', 'glotpress' ), 200 );
 177          }
 178  
 179          if ( ! $this->verify_nonce( 'delete-glossary-entry_' . $glossary_entry->id ) ) {
 180              return $this->die_with_error( __( 'An error has occurred. Please try again.', 'glotpress' ), 403 );
 181          }
 182  
 183          $glossary        = GP::$glossary->get( $glossary_entry->glossary_id );
 184          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 185          $can_edit        = $this->can( 'approve', 'translation-set', $translation_set->id );
 186  
 187          if ( ! $can_edit ) {
 188              return $this->die_with_error( __( 'Forbidden', 'glotpress' ), 403 );
 189          }
 190  
 191          if ( ! $glossary_entry->delete() ) {
 192              $this->errors[] = __( 'Error in deleting glossary entry!', 'glotpress' );
 193          }
 194  
 195          if ( ! empty( $this->errors ) ) {
 196              $error_output = '<ul>';
 197              foreach ( $this->errors as $error ) {
 198                  $error_output .= '<li>' . $error . '</li>';
 199              }
 200              $error_output .= '</ul>';
 201  
 202              return $this->die_with_error( $error_output, 200 );
 203          }
 204  
 205          exit();
 206      }
 207  
 208  	public function export_glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
 209          $project = GP::$project->by_path( $project_path );
 210          $locale  = GP_Locales::by_slug( $locale_slug );
 211  
 212          if ( ! $project || ! $locale ) {
 213              return $this->die_with_404();
 214          }
 215  
 216          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
 217  
 218          if ( ! $translation_set ) {
 219              return $this->die_with_404();
 220          }
 221  
 222          $glossary = GP::$glossary->by_set_id( $translation_set->id );
 223  
 224          if ( ! $glossary ) {
 225              return $this->die_with_404();
 226          }
 227  
 228          $glossary_entries = GP::$glossary_entry->by_glossary_id( $glossary->id );
 229          $filename         = sprintf( '%s-%s-glossary.csv', str_replace( '/', '-', $project->path ), $locale->slug );
 230          $last_modified    = gmdate( 'D, d M Y H:i:s', gp_gmt_strtotime( GP::$glossary_entry->last_modified( $glossary ) ) ) . ' GMT';
 231  
 232          $this->headers_for_download( $filename, $last_modified );
 233          $this->print_export_file( $locale->slug, $glossary_entries );
 234      }
 235  
 236  	public function import_glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
 237          $project = GP::$project->by_path( $project_path );
 238          $locale  = GP_Locales::by_slug( $locale_slug );
 239  
 240          if ( ! $project || ! $locale ) {
 241              return $this->die_with_404();
 242          }
 243  
 244          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
 245  
 246          if ( ! $translation_set ) {
 247              return $this->die_with_404();
 248          }
 249  
 250          if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
 251              return;
 252          }
 253  
 254          $can_edit = $this->can( 'approve', 'translation-set', $translation_set->id );
 255  
 256          $this->tmpl( 'glossary-import', get_defined_vars() );
 257      }
 258  
 259  	public function import_glossary_entries_post( $project_path, $locale_slug, $translation_set_slug ) {
 260          $project = GP::$project->by_path( $project_path );
 261          $locale  = GP_Locales::by_slug( $locale_slug );
 262  
 263          if ( ! $project || ! $locale ) {
 264              return $this->die_with_404();
 265          }
 266  
 267          $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
 268  
 269          if ( ! $translation_set ) {
 270              return $this->die_with_404();
 271          }
 272  
 273          $glossary = GP::$glossary->by_set_id( $translation_set->id );
 274  
 275          if ( ! $glossary ) {
 276              return $this->die_with_404();
 277          }
 278  
 279          if ( $this->invalid_nonce_and_redirect( 'import-glossary-entries_' . $project->path . $locale_slug . $translation_set_slug ) ) {
 280              return;
 281          }
 282  
 283          if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
 284              return;
 285          }
 286  
 287          if ( ! is_uploaded_file( $_FILES['import-file']['tmp_name'] ) ) {
 288              $this->redirect_with_error( __( 'Error uploading the file.', 'glotpress' ) );
 289              return;
 290          }
 291  
 292          $replace = gp_post( 'import-flush' );
 293          if ( 'on' === $replace && $this->can( 'approve', 'translation-set', $translation_set->id ) ) {
 294              GP::$glossary_entry->delete_many( array( 'glossary_id' => $glossary->id ) );
 295          }
 296  
 297          $glossary_entries_added = $this->read_glossary_entries_from_file( $_FILES['import-file']['tmp_name'], $glossary->id, $locale->slug );
 298  
 299          if ( empty( $this->errors ) && is_int( $glossary_entries_added ) ) {
 300              $this->notices[] = sprintf(
 301                  /* translators: %s: Count number. */
 302                  __( '%s glossary entries were added', 'glotpress' ),
 303                  $glossary_entries_added
 304              );
 305          }
 306  
 307          $this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
 308      }
 309  
 310  	private function print_export_file( $locale_slug, $entries ) {
 311          $outstream = fopen( 'php://output', 'w' );
 312  
 313          fputcsv( $outstream, array( 'en', $locale_slug, 'pos', 'description' ) );
 314  
 315          foreach ( $entries as $entry ) {
 316              $values = array( $entry->term, $entry->translation, $entry->part_of_speech, $entry->comment );
 317              fputcsv( $outstream, $values );
 318          }
 319  
 320          fclose( $outstream );
 321      }
 322  
 323  	private function read_glossary_entries_from_file( $file, $glossary_id, $locale_slug ) {
 324          $f                = fopen( $file, 'r' );
 325          $glossary_entries = 0;
 326  
 327          $data = fgetcsv( $f, 0, ',' );
 328  
 329          if ( ! is_array( $data ) ) {
 330              return;
 331          } elseif ( $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: Thu Jul 16 01:01:50 2020 Cross-referenced by PHPXref 0.7.1