[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/routes/ -> translation-set.php (source)

   1  <?php
   2  /**
   3   * Routes: GP_Route_Translation_Set class
   4   *
   5   * @package GlotPress
   6   * @subpackage Routes
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the translation set route.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Route_Translation_Set extends GP_Route_Main {
  16  	public function new_get() {
  17          $set = new GP_Translation_Set;
  18          $set->project_id = gp_get( 'project_id' );
  19          $project = $set->project_id? GP::$project->get( $set->project_id ) : null;
  20          if ( $this->cannot_edit_set_and_redirect( $set ) ) return;
  21          $this->tmpl( 'translation-set-new', get_defined_vars() );
  22      }
  23  
  24  	public function new_post() {
  25          if ( $this->invalid_nonce_and_redirect( 'add-translation-set' ) ) {
  26              return;
  27          }
  28  
  29          $new_set = new GP_Translation_Set( gp_post( 'set', array() ) );
  30          if ( $this->cannot_edit_set_and_redirect( $new_set ) ) return;
  31          if ( $this->invalid_and_redirect( $new_set ) ) return;
  32          $created_set = GP::$translation_set->create_and_select( $new_set );
  33          if ( !$created_set ) {
  34              $this->errors[] = __( 'Error in creating translation set!', 'glotpress' );
  35              $this->redirect( gp_url( '/sets/-new', array( 'project_id' => $new_set->project_id ) ) );
  36          } else {
  37              GP::$project->get( $created_set->project_id );
  38              $this->notices[] = __( 'The translation set was created!', 'glotpress' );
  39              $this->redirect( gp_url_project_locale( GP::$project->get( $created_set->project_id ), $created_set->locale, $created_set->slug ) );
  40          }
  41      }
  42  
  43  	public function single( $set_id ) {
  44          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
  45          if ( !$items) return;
  46          list( $set, $project, ) = $items;
  47          $this->redirect( gp_url_project( $project, array( $set->locale, $set->slug ) ) );
  48      }
  49  
  50  	public function edit_get( $set_id ) {
  51          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
  52          if ( !$items ) return;
  53          list( $set, $project, $locale ) = $items;
  54          if ( $this->cannot_and_redirect( 'write', 'project', $set->project_id, gp_url_project( $project ) ) ) return;
  55          $url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) );
  56          $this->tmpl( 'translation-set-edit', get_defined_vars() );
  57      }
  58  
  59      /**
  60       * Saves settings for a translation set and redirects back to the project locales page.
  61       *
  62       * @since 1.0.0
  63       *
  64       * @param int $set_id A translation set id to edit the settings of.
  65       */
  66  	public function edit_post( $set_id ) {
  67          if ( $this->invalid_nonce_and_redirect( 'edit-translation-set_' . $set_id ) ) {
  68              return;
  69          }
  70  
  71          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
  72  
  73          if ( ! $items ) {
  74              return;
  75          }
  76  
  77          list( $set, ,  ) = $items;
  78  
  79          $new_set = new GP_Translation_Set( gp_post( 'set', array() ) );
  80  
  81          if ( $this->cannot_edit_set_and_redirect( $new_set ) ) {
  82              return;
  83          }
  84  
  85          if ( $this->invalid_and_redirect( $new_set, gp_url( '/sets/' . $set_id . '/-edit' ) ) ) {
  86              return;
  87          }
  88  
  89          if ( ! $set->update( $new_set ) ) {
  90              $this->errors[] = __( 'Error in updating translation set!', 'glotpress' );
  91              $this->redirect();
  92              return;
  93          }
  94  
  95          $project = GP::$project->get( $new_set->project_id );
  96  
  97          $this->notices[] = __( 'The translation set was updated!', 'glotpress' );
  98  
  99          $this->redirect( gp_url_project_locale( $project, $new_set->locale, $new_set->slug ) );
 100      }
 101  
 102      /**
 103       * Deletes a translation set.
 104       *
 105       * @since 2.0.0
 106       *
 107       * @param int $set_id The id of the translation set to delete.
 108       */
 109  	public function delete_post( $set_id ) {
 110          if ( $this->invalid_nonce_and_redirect( 'delete-translation-set_' . $set_id ) ) {
 111              return;
 112          }
 113  
 114          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
 115          if ( ! $items ) {
 116              return;
 117          }
 118  
 119          list( $set, $project, $locale ) = $items;
 120          if ( $this->cannot_and_redirect( 'delete', 'project', $set->project_id, gp_url_project( $project ) ) ) {
 121              return;
 122          }
 123  
 124          if ( ! $set->delete() ) {
 125              $this->errors[] = __( 'Error deleting translation set!', 'glotpress' );
 126              $this->redirect();
 127              return;
 128          }
 129  
 130          $this->notices[] = __( 'The translation set was deleted!', 'glotpress' );
 131          $this->redirect( gp_url_project( $project ) );
 132      }
 133  
 134      /**
 135       * Displays the delete page for translations sets.
 136       *
 137       * @since 2.0.0
 138       *
 139       * @param int $set_id The id of the translation set to delete.
 140       */
 141  	public function delete_get( $set_id ) {
 142          $items = $this->get_set_project_and_locale_from_set_id_or_404( $set_id );
 143          if ( ! $items ) {
 144              return;
 145          }
 146  
 147          list( $set, $project, $locale ) = $items;
 148          if ( $this->cannot_and_redirect( 'delete', 'project', $set->project_id, gp_url_project( $project ) ) ) {
 149              return;
 150          }
 151  
 152          $url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) );
 153          $this->tmpl( 'translation-set-delete', get_defined_vars() );
 154      }
 155  
 156      /**
 157       * Determines whether the current user can edit a translation set.
 158       *
 159       * @param GP_Translation_Set $set The translation set to edit.
 160       */
 161  	private function cannot_edit_set_and_redirect( $set ) {
 162          return $this->cannot_and_redirect( 'write', 'project', $set->project_id );
 163      }
 164  
 165  	private function get_set_project_and_locale_from_set_id_or_404( $set_id ) {
 166          $set = GP::$translation_set->get( $set_id );
 167          if ( !$set ) {
 168              $this->die_with_404( array( 'title' => "Translation set wasn't found" ) );
 169              return;
 170          }
 171          $project =  GP::$project->get( $set->project_id );
 172          if ( !$project ) {
 173              $this->die_with_404( array( 'title' => "The project associated with this translation set wasn't found" ) );
 174              return;
 175          }
 176          $locale = $locale = GP_Locales::by_slug( $set->locale );
 177          if ( !$locale ) {
 178              $this->die_with_404( array( 'title' => "The locale associated with this translation set wasn't found" ) );
 179              return;
 180          }
 181          return array( $set, $project, $locale );
 182      }
 183  }


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