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


Generated: Sun Oct 24 01:01:06 2021 Cross-referenced by PHPXref 0.7.1