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


Generated: Thu Nov 21 01:01:07 2024 Cross-referenced by PHPXref 0.7.1