[ 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          }
  75          else {
  76              $this->errors[] = __( 'Error in creating glossary!', 'glotpress' );
  77              $this->redirect( gp_url( '/glossaries/-new', array( 'translation_set_id' => $new_glossary->translation_set_id ) ) );
  78          }
  79      }
  80  
  81  	public function edit_get( $glossary_id ) {
  82          $glossary = GP::$glossary->get( $glossary_id );
  83  
  84          if ( ! $glossary ) {
  85              $this->redirect_with_error( __( 'Cannot find glossary.', 'glotpress' ) );
  86          }
  87  
  88          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
  89          $locale          = GP_Locales::by_slug( $translation_set->locale );
  90          $project         = GP::$project->get( $translation_set->project_id );
  91  
  92          $this->tmpl( 'glossary-edit', get_defined_vars() );
  93      }
  94  
  95  	public function edit_post( $glossary_id ) {
  96          if ( $this->invalid_nonce_and_redirect( 'edit-glossary_' . $glossary_id ) ) {
  97              return;
  98          }
  99  
 100          $glossary     = GP::$glossary->get( $glossary_id );
 101          $new_glossary = new GP_Glossary( gp_post('glossary') );
 102  
 103          if ( $this->cannot_edit_glossary_and_redirect( $glossary ) ) {
 104              return;
 105          }
 106  
 107          if ( ! $glossary->update( $new_glossary ) ) {
 108              $this->errors[] = __( 'Error in updating glossary!', 'glotpress' );
 109              $this->redirect();
 110              return;
 111          }
 112  
 113          $this->notices[] = __( 'The glossary was updated!', 'glotpress' );
 114          $translation_set = $new_glossary->translation_set_id ? GP::$translation_set->get( $new_glossary->translation_set_id ) : null;
 115          $set_project     = GP::$project->get( $translation_set->project_id );
 116  
 117          $this->redirect( $glossary->path() );
 118      }
 119  
 120      /**
 121       * Displays the delete page for glossaries.
 122       *
 123       * @since 2.0.0
 124       *
 125       * @param int $glossary_id The id of the glossary to delete.
 126       */
 127  	public function delete_get( $glossary_id ) {
 128          $glossary = GP::$glossary->get( $glossary_id );
 129  
 130          if ( ! $glossary ) {
 131              $this->redirect_with_error( __( 'Cannot find glossary.', 'glotpress' ) );
 132          }
 133  
 134          if ( $this->cannot_delete_glossary_and_redirect( $glossary ) ) {
 135              return;
 136          }
 137  
 138          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 139          $locale          = GP_Locales::by_slug( $translation_set->locale );
 140          $project         = GP::$project->get( $translation_set->project_id );
 141  
 142          $this->tmpl( 'glossary-delete', get_defined_vars() );
 143      }
 144  
 145      /**
 146       * Delete a glossary.
 147       *
 148       * @since 2.0.0
 149       *
 150       * @param int $glossary_id The id of the glossary to delete.
 151       */
 152  	public function delete_post( $glossary_id ) {
 153          if ( $this->invalid_nonce_and_redirect( 'delete-glossary_' . $glossary_id ) ) {
 154              return;
 155          }
 156  
 157          $glossary = GP::$glossary->get( $glossary_id );
 158  
 159          if ( $this->cannot_delete_glossary_and_redirect( $glossary ) ) {
 160              return;
 161          }
 162  
 163          $translation_set = GP::$translation_set->get( $glossary->translation_set_id );
 164          $project         = GP::$project->get( $translation_set->project_id );
 165  
 166          if ( ! $glossary->delete() ) {
 167              $this->errors[] = __( 'Error deleting glossary!', 'glotpress' );
 168              $this->redirect();
 169              return;
 170          }
 171  
 172          $this->notices[] = __( 'The glossary was deleted!', 'glotpress' );
 173  
 174          $this->redirect( gp_url_join( gp_url_project( $project ), array( $translation_set->locale, $translation_set->slug ) ) );
 175      }
 176  
 177      /**
 178       * Checks to see if the current user can edit a glossary or not.  If they cannot it redirects back to the project page.
 179       *
 180       * @since 1.0.0
 181       *
 182       * @param GP_Glossary $glossary The glossary object to check.
 183       *
 184       * @return bool
 185       */
 186  	private function cannot_edit_glossary_and_redirect( $glossary ) {
 187          return $this->cannot_and_redirect( 'approve', 'translation-set', $glossary->translation_set_id );
 188      }
 189  
 190      /**
 191       * Checks to see if the current user can delete a glossary or not.  If they cannot it redirects back to the project page.
 192       *
 193       * @since 2.0.0
 194       *
 195       * @param GP_Glossary $glossary The glossary object to check.
 196       *
 197       * @return bool
 198       */
 199  	private function cannot_delete_glossary_and_redirect( $glossary ) {
 200          return $this->cannot_and_redirect( 'delete', 'translation-set', $glossary->translation_set_id );
 201      }
 202  
 203  }


Generated: Fri Nov 22 01:01:58 2019 Cross-referenced by PHPXref 0.7.1