[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Routes: GP_Route_Glossary class
   4   *
   5   * @package GlotPress
   6   * @subpackage Routes
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the glossary route.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Route_Glossary extends GP_Route_Main {
  16  
  17  	public function new_get() {
  18          $glossary                     = new GP_Glossary();
  19          $glossary->translation_set_id = gp_get( 'translation_set_id' );
  20  
  21          $translation_set = $glossary->translation_set_id ? GP::$translation_set->get( $glossary->translation_set_id ) : null;
  22  
  23          if ( ! $translation_set ) {
  24              $this->redirect_with_error( __( 'Couldn&#8217;t find translation set with this ID.', 'glotpress' ) );
  25              return;
  26          }
  27  
  28          $project = GP::$project->get( $translation_set->project_id );
  29  
  30          if ( GP::$glossary->by_set_id( $glossary->translation_set_id ) ) {
  31              $glossary_url = gp_url_join( gp_url_project( $project, array( $translation_set->locale, $translation_set->slug ) ), array( 'glossary' ) );
  32              $this->redirect_with_error( __( 'The glossary for this translation set already exists.', 'glotpress' ), $glossary_url );
  33              return;
  34          }
  35  
  36          if ( $this->cannot_edit_glossary_and_redirect( $glossary ) ) {
  37              return;
  38          }
  39  
  40          $locale = GP_Locales::by_slug( $translation_set->locale );
  41  
  42          $this->tmpl( 'glossary-new', get_defined_vars() );
  43      }
  44  
  45  	public function new_post() {
  46          if ( $this->invalid_nonce_and_redirect( 'add-glossary' ) ) {
  47              return;
  48          }
  49  
  50          $new_glossary    = new GP_Glossary( gp_post( 'glossary' ) );
  51          $translation_set = $new_glossary->translation_set_id ? GP::$translation_set->get( $new_glossary->translation_set_id ) : null;
  52  
  53          if ( ! $translation_set ) {
  54              $this->redirect_with_error( __( 'Couldn&#8217;t find translation set with this ID.', 'glotpress' ), gp_url( '/glossaries/-new', array( 'translation_set_id' => $new_glossary->translation_set_id ) ) );
  55              return;
  56          }
  57  
  58          if ( GP::$glossary->by_set_id( $new_glossary->translation_set_id ) ) {
  59              $this->redirect_with_error( __( 'The glossary for this translation set already exists.', 'glotpress' ), gp_url( '/glossaries/-new', array( 'translation_set_id' => $new_glossary->translation_set_id ) ) );
  60              return;
  61          }
  62  
  63          if ( $this->cannot_edit_glossary_and_redirect( $new_glossary ) ) {
  64              return;
  65          }
  66  
  67          $created_glossary = GP::$glossary->create_and_select( $new_glossary );
  68  
  69          if ( $created_glossary ) {
  70              $this->notices[] = __( 'The glossary was created!', 'glotpress' );
  71              $set_project     = GP::$project->get( $translation_set->project_id );
  72  
  73              $this->redirect( $created_glossary->path() );
  74          } else {
  75              $this->errors[] = __( 'Error in creating glossary!', 'glotpress' );
  76              $this->redirect( gp_url( '/glossaries/-new', array( 'translation_set_id' => $new_glossary->translation_set_id ) ) );
  77          }
  78      }
  79  
  80  	public function edit_get( $glossary_id ) {
  81          $glossary = GP::$glossary->get( $glossary_id );
  82  
  83          if ( ! $glossary ) {
  84              $this->redirect_with_error( __( 'Cannot find glossary.', 'glotpress' ) );
  85          }
  86  
  87          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
  88          $locale          = GP_Locales::by_slug( $translation_set->locale );
  89          $project         = GP::$project->get( $translation_set->project_id );
  90  
  91          $this->tmpl( 'glossary-edit', get_defined_vars() );
  92      }
  93  
  94  	public function edit_post( $glossary_id ) {
  95          if ( $this->invalid_nonce_and_redirect( 'edit-glossary_' . $glossary_id ) ) {
  96              return;
  97          }
  98  
  99          $glossary     = GP::$glossary->get( $glossary_id );
 100          $new_glossary = new GP_Glossary( gp_post( 'glossary' ) );
 101  
 102          if ( $this->cannot_edit_glossary_and_redirect( $glossary ) ) {
 103              return;
 104          }
 105  
 106          if ( ! $glossary->update( $new_glossary ) ) {
 107              $this->errors[] = __( 'Error in updating glossary!', 'glotpress' );
 108              $this->redirect();
 109              return;
 110          }
 111  
 112          $this->notices[] = __( 'The glossary was updated!', 'glotpress' );
 113          $translation_set = $new_glossary->translation_set_id ? GP::$translation_set->get( $new_glossary->translation_set_id ) : null;
 114          $set_project     = GP::$project->get( $translation_set->project_id );
 115  
 116          $this->redirect( $glossary->path() );
 117      }
 118  
 119      /**
 120       * Displays the delete page for glossaries.
 121       *
 122       * @since 2.0.0
 123       *
 124       * @param int $glossary_id The id of the glossary to delete.
 125       */
 126  	public function delete_get( $glossary_id ) {
 127          $glossary = GP::$glossary->get( $glossary_id );
 128  
 129          if ( ! $glossary ) {
 130              $this->redirect_with_error( __( 'Cannot find glossary.', 'glotpress' ) );
 131          }
 132  
 133          if ( $this->cannot_delete_glossary_and_redirect( $glossary ) ) {
 134              return;
 135          }
 136  
 137          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 138          $locale          = GP_Locales::by_slug( $translation_set->locale );
 139          $project         = GP::$project->get( $translation_set->project_id );
 140  
 141          $this->tmpl( 'glossary-delete', get_defined_vars() );
 142      }
 143  
 144      /**
 145       * Delete a glossary.
 146       *
 147       * @since 2.0.0
 148       *
 149       * @param int $glossary_id The id of the glossary to delete.
 150       */
 151  	public function delete_post( $glossary_id ) {
 152          if ( $this->invalid_nonce_and_redirect( 'delete-glossary_' . $glossary_id ) ) {
 153              return;
 154          }
 155  
 156          $glossary = GP::$glossary->get( $glossary_id );
 157  
 158          if ( $this->cannot_delete_glossary_and_redirect( $glossary ) ) {
 159              return;
 160          }
 161  
 162          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 163          $project         = GP::$project->get( $translation_set->project_id );
 164  
 165          if ( ! $glossary->delete() ) {
 166              $this->errors[] = __( 'Error deleting glossary!', 'glotpress' );
 167              $this->redirect();
 168              return;
 169          }
 170  
 171          $this->notices[] = __( 'The glossary was deleted!', 'glotpress' );
 172  
 173          $this->redirect( gp_url_join( gp_url_project( $project ), array( $translation_set->locale, $translation_set->slug ) ) );
 174      }
 175  
 176      /**
 177       * Checks to see if the current user can edit a glossary or not.  If they cannot it redirects back to the project page.
 178       *
 179       * @since 1.0.0
 180       *
 181       * @param GP_Glossary $glossary The glossary object to check.
 182       *
 183       * @return bool
 184       */
 185  	private function cannot_edit_glossary_and_redirect( $glossary ) {
 186          return $this->cannot_and_redirect( 'approve', 'translation-set', $glossary->translation_set_id );
 187      }
 188  
 189      /**
 190       * Checks to see if the current user can delete a glossary or not.  If they cannot it redirects back to the project page.
 191       *
 192       * @since 2.0.0
 193       *
 194       * @param GP_Glossary $glossary The glossary object to check.
 195       *
 196       * @return bool
 197       */
 198  	private function cannot_delete_glossary_and_redirect( $glossary ) {
 199          return $this->cannot_and_redirect( 'delete', 'translation-set', $glossary->translation_set_id );
 200      }
 201  
 202  }


Generated: Sat Apr 20 01:01:09 2024 Cross-referenced by PHPXref 0.7.1