[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Routes: GP_Route_Project class
   4   *
   5   * @package GlotPress
   6   * @subpackage Routes
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the project route.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Route_Project extends GP_Route_Main {
  16  
  17  	public function index() {
  18          $title = __( 'Projects', 'glotpress' );
  19          $projects = GP::$project->top_level();
  20          $this->tmpl( 'projects', get_defined_vars() );
  21      }
  22  
  23  	public function single( $project_path ) {
  24          $project = GP::$project->by_path( $project_path );
  25  
  26          if ( ! $project ) {
  27              return $this->die_with_404();
  28          }
  29  
  30          $sub_projects = $project->sub_projects();
  31          $translation_sets = GP::$translation_set->by_project_id( $project->id );
  32  
  33          foreach( $translation_sets as $set ) {
  34              $locale = GP_Locales::by_slug( $set->locale );
  35  
  36              $set->name_with_locale = $set->name_with_locale();
  37              $set->current_count = $set->current_count();
  38              $set->untranslated_count = $set->untranslated_count();
  39              $set->waiting_count = $set->waiting_count();
  40              $set->fuzzy_count = $set->fuzzy_count();
  41              $set->percent_translated = $set->percent_translated();
  42              $set->all_count = $set->all_count();
  43              $set->wp_locale = $locale->wp_locale;
  44              if ( $this->api ) {
  45                  $set->last_modified = $set->current_count ? $set->last_modified() : false;
  46              }
  47          }
  48  
  49          usort( $translation_sets, function( $a, $b ) {
  50              return( $a->current_count < $b->current_count );
  51          });
  52  
  53          /**
  54           * Filter the list of translation sets of a project.
  55           *
  56           * Can also be used to sort the sets to a custom order.
  57           *
  58           * @since 1.0.0
  59           *
  60           * @param GP_Translation_Sets[] $translation_sets An array of translation sets.
  61           */
  62          $translation_sets = apply_filters( 'gp_translation_sets_sort', $translation_sets );
  63  
  64          $title = sprintf(
  65              /* translators: %s: project name */
  66              __( '%s project', 'glotpress' ),
  67              esc_html( $project->name )
  68          );
  69          $can_write = $this->can( 'write', 'project', $project->id );
  70          $this->tmpl( 'project', get_defined_vars() );
  71      }
  72  
  73  	public function personal_options_post( $project_path ) {
  74          $project = GP::$project->by_path( $project_path );
  75  
  76          if ( ! $project ) {
  77              return $this->die_with_404();
  78          }
  79  
  80          if ( $this->invalid_nonce_and_redirect( 'set-personal-options_' . $project->id ) ) {
  81              return;
  82          }
  83  
  84          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
  85              return;
  86          }
  87  
  88          $source_url_templates = get_user_meta( get_current_user_id(), 'gp_source_url_templates', true );
  89          if ( !is_array( $source_url_templates ) ) $source_url_templates = array();
  90          $source_url_templates[$project->id] = gp_post( 'source-url-template' );
  91          if ( update_user_meta( get_current_user_id(), 'gp_source_url_templates', $source_url_templates ) ) {
  92              $this->notices[] = 'Source URL template was successfully updated.';
  93          } else {
  94              $this->errors[] = 'Error in updating source URL template.';
  95          }
  96          $this->redirect( gp_url_project( $project ) );
  97      }
  98  
  99  	public function import_originals_get( $project_path ) {
 100          $project = GP::$project->by_path( $project_path );
 101  
 102           if ( ! $project ) {
 103              return $this->die_with_404();
 104          }
 105  
 106          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 107              return;
 108          }
 109  
 110          $kind = 'originals';
 111          $this->tmpl( 'project-import', get_defined_vars() );
 112      }
 113  
 114  	public function import_originals_post( $project_path ) {
 115          $project = GP::$project->by_path( $project_path );
 116  
 117          if ( ! $project ) {
 118              return $this->die_with_404();
 119          }
 120  
 121          if ( $this->invalid_nonce_and_redirect( 'import-originals_' . $project->id ) ) {
 122              return;
 123          }
 124  
 125          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 126              return;
 127          }
 128  
 129          if ( ! is_uploaded_file( $_FILES['import-file']['tmp_name'] ) ) {
 130              // TODO: different errors for different upload conditions
 131              $this->redirect_with_error( __( 'Error uploading the file.', 'glotpress' ) );
 132              return;
 133          }
 134  
 135          $format = gp_get_import_file_format( gp_post( 'format', 'po' ), $_FILES[ 'import-file' ][ 'name' ] );
 136  
 137          if ( ! $format ) {
 138              $this->redirect_with_error( __( 'No such format.', 'glotpress' ) );
 139              return;
 140          }
 141  
 142          $translations = $format->read_originals_from_file( $_FILES['import-file']['tmp_name'] );
 143  
 144          if ( ! $translations ) {
 145              $this->redirect_with_error( __( 'Couldn&#8217;t load translations from file!', 'glotpress' ) );
 146              return;
 147          }
 148  
 149          list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error ) = GP::$original->import_for_project( $project, $translations );
 150  
 151          $notice = sprintf(
 152              __( '%1$s new strings added, %2$s updated, %3$s fuzzied, and %4$s obsoleted.', 'glotpress' ),
 153              $originals_added,
 154              $originals_existing,
 155              $originals_fuzzied,
 156              $originals_obsoleted
 157          );
 158  
 159          if ( $originals_error ) {
 160              $notice .= ' ' . sprintf(
 161                  /* translators: %s: number of errors */
 162                  _n( '%s new string was not imported due to an error.', '%s new strings were not imported due to an error.', $originals_error, 'glotpress' ),
 163                  $originals_error
 164              );
 165          }
 166  
 167          $this->notices[] = $notice;
 168  
 169          $this->redirect( gp_url_project( $project ) );
 170      }
 171  
 172  	public function edit_get( $project_path ) {
 173          $project = GP::$project->by_path( $project_path );
 174  
 175          if ( ! $project ) {
 176              return $this->die_with_404();
 177          }
 178  
 179          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 180              return;
 181          }
 182  
 183          $this->tmpl( 'project-edit', get_defined_vars() );
 184      }
 185  
 186  	public function edit_post( $project_path ) {
 187          $project = GP::$project->by_path( $project_path );
 188  
 189          if ( !$project ) {
 190              $this->die_with_404();
 191          }
 192  
 193          if ( $this->invalid_nonce_and_redirect( 'edit-project_' . $project->id ) ) {
 194              return;
 195          }
 196  
 197          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 198              return;
 199          }
 200  
 201          $updated_project = new GP_Project( gp_post( 'project' ) );
 202          if ( $this->invalid_and_redirect( $updated_project, gp_url_project( $project, '-edit' ) ) ) {
 203              return;
 204          }
 205  
 206          // TODO: add id check as a validation rule
 207          if ( $project->id == $updated_project->parent_project_id ) {
 208              $this->errors[] = __( 'The project cannot be parent of itself!', 'glotpress' );
 209          }
 210          elseif ( $project->save( $updated_project ) ) {
 211              $this->notices[] = __( 'The project was saved.', 'glotpress' );
 212          }
 213          else {
 214              $this->errors[] = __( 'Error in saving project!', 'glotpress' );
 215          }
 216  
 217          $project->reload();
 218  
 219          $this->redirect( gp_url_project( $project ) );
 220      }
 221  
 222      /**
 223       * Deletes a project, including sub projects, glossaries, originals, translations sets and translations.
 224       *
 225       * @since 2.0.0
 226       *
 227       * @param int $project_path The path of the project to delete.
 228       */
 229  	public function delete_post( $project_path ) {
 230          $project = GP::$project->by_path( $project_path );
 231  
 232          if ( ! is_object( $project ) ) {
 233  
 234              $this->redirect( gp_url_public_root() );
 235              $this->errors[] = __( 'Error in deleting project!', 'glotpress' );
 236  
 237              return;
 238          }
 239  
 240          if ( $this->invalid_nonce_and_redirect( 'delete-project_' . $project->id ) ) {
 241              return;
 242          }
 243  
 244          if ( $this->cannot_and_redirect( 'delete', 'project', $project->id ) ) {
 245              return;
 246          }
 247  
 248          if ( $project->delete() ) {
 249              $this->notices[] = sprintf( __( 'The project "%s" was deleted.', 'glotpress' ), $project->name );
 250          }
 251          else {
 252              $this->errors[] = sprintf( __( 'Error deleting project "%s"!', 'glotpress' ), $project->name );
 253          }
 254  
 255          $this->redirect( gp_url_public_root() );
 256      }
 257  
 258      /**
 259       * Displays the delete page for projects.
 260       *
 261       * @since 2.0.0
 262       *
 263       * @param string $project_path The path of the project to delete.
 264       */
 265  	public function delete_get( $project_path ) {
 266          $project = GP::$project->by_path( $project_path );
 267  
 268          if ( $this->cannot_and_redirect( 'delete', 'project', $project->id ) ) {
 269              return;
 270          }
 271  
 272          $this->tmpl( 'project-delete', get_defined_vars() );
 273      }
 274  
 275  
 276  	public function new_get() {
 277          $project = new GP_Project();
 278          $project->active = 1;
 279          $project->parent_project_id = gp_get( 'parent_project_id', null );
 280  
 281          if ( $this->cannot_and_redirect( 'write', 'project', $project->parent_project_id ) ) {
 282              return;
 283          }
 284  
 285          $this->tmpl( 'project-new', get_defined_vars() );
 286      }
 287  
 288  	public function new_post() {
 289          if ( $this->invalid_nonce_and_redirect( 'add-project' ) ) {
 290              return;
 291          }
 292  
 293          $post = gp_post( 'project' );
 294          $parent_project_id = gp_array_get( $post, 'parent_project_id', null );
 295  
 296          if ( $this->cannot_and_redirect( 'write', 'project', $parent_project_id ) ) {
 297              return;
 298          }
 299  
 300          $new_project = new GP_Project( $post );
 301  
 302          if ( $this->invalid_and_redirect( $new_project ) ) {
 303              return;
 304          }
 305  
 306          $project = GP::$project->create_and_select( $new_project );
 307  
 308          if ( ! $project ) {
 309              $project = new GP_Project();
 310              $this->errors[] = __( 'Error in creating project!', 'glotpress' );
 311              $this->tmpl( 'project-new', get_defined_vars() );
 312          } else {
 313              $this->notices[] = __( 'The project was created!', 'glotpress' );
 314              $this->redirect( gp_url_project( $project ) );
 315          }
 316      }
 317  
 318  	public function permissions_get( $project_path ) {
 319          $project = GP::$project->by_path( $project_path );
 320  
 321          if ( ! $project ) {
 322              return $this->die_with_404();
 323          }
 324  
 325          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 326              return;
 327          }
 328  
 329          $path_to_root = array_slice( $project->path_to_root(), 1 );
 330          $permissions = GP::$validator_permission->by_project_id( $project->id );
 331          $cmp_fn = function( $x, $y ){
 332              return strcmp( $x->locale_slug, $y->locale_slug );
 333          };
 334          usort( $permissions, $cmp_fn );
 335          $parent_permissions = array();
 336  
 337          foreach( $path_to_root as $parent_project ) {
 338              $this_parent_permissions = GP::$validator_permission->by_project_id( $parent_project->id );
 339              usort( $this_parent_permissions, $cmp_fn );
 340              foreach( $this_parent_permissions as $permission ) {
 341                  $permission->project = $parent_project;
 342              }
 343              $parent_permissions = array_merge( $parent_permissions, (array)$this_parent_permissions );
 344          }
 345          // we can't join on users table
 346          foreach( array_merge( (array)$permissions, (array)$parent_permissions ) as $permission ) {
 347              $permission->user = get_user_by( 'id', $permission->user_id );
 348          }
 349          $this->tmpl( 'project-permissions', get_defined_vars() );
 350      }
 351  
 352  	public function permissions_post( $project_path ) {
 353          $project = GP::$project->by_path( $project_path );
 354  
 355          if ( ! $project ) {
 356              return $this->die_with_404();
 357          }
 358  
 359          if ( $this->invalid_nonce_and_redirect( 'add-project-permissions_' . $project->id ) ) {
 360              return;
 361          }
 362  
 363          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 364              return;
 365          }
 366  
 367          if ( 'add-validator' == gp_post( 'action' ) ) {
 368              $user = get_user_by( 'login', gp_post( 'user_login' ) );
 369              if ( !$user ) {
 370                  $this->redirect_with_error( __( 'User wasn&#8217;t found!', 'glotpress' ), gp_url_current() );
 371                  return;
 372              }
 373              $new_permission = new GP_Validator_Permission( array(
 374                  'user_id' => $user->ID,
 375                  'action' => 'approve',
 376                  'project_id' => $project->id,
 377                  'locale_slug' => gp_post( 'locale' ),
 378                  'set_slug' => gp_post( 'set-slug' ),
 379              ) );
 380              if ( $this->invalid_and_redirect( $new_permission, gp_url_current() ) ) return;
 381              $permission = GP::$validator_permission->create( $new_permission );
 382              $permission?
 383                  $this->notices[] = __( 'Validator was added.', 'glotpress' ) : $this->errors[] = __( 'Error in adding validator.', 'glotpress' );
 384          }
 385          $this->redirect( gp_url_current() );
 386      }
 387  
 388  	public function permissions_delete( $project_path, $permission_id ) {
 389          if ( $this->invalid_nonce_and_redirect( 'delete-project-permission_' . $permission_id ) ) {
 390              return;
 391          }
 392  
 393          $project = GP::$project->by_path( $project_path );
 394  
 395          if ( ! $project ) {
 396              $this->die_with_404();
 397          }
 398  
 399          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 400              return;
 401          }
 402  
 403          $permission = GP::$permission->get( $permission_id );
 404          if ( $permission ) {
 405              if ( $permission->delete() ) {
 406                  $this->notices[] = __( 'Permission was deleted.', 'glotpress' );
 407              } else {
 408                  $this->errors[] = __( 'Error in deleting permission!', 'glotpress' );
 409              }
 410          } else {
 411              $this->errors[] = __( 'Permission wasn&#8217;t found!', 'glotpress' );
 412          }
 413          $this->redirect( gp_url_project( $project, '-permissions' ) );
 414      }
 415  
 416  	public function mass_create_sets_get( $project_path ) {
 417          $project = GP::$project->by_path( $project_path );
 418  
 419          if ( ! $project ) {
 420              return $this->die_with_404();
 421          }
 422  
 423          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 424              return;
 425          }
 426  
 427          $this->tmpl( 'project-mass-create-sets', get_defined_vars() );
 428      }
 429  
 430  	public function mass_create_sets_post( $project_path ) {
 431          $project = GP::$project->by_path( $project_path );
 432          if ( ! $project ) {
 433              return $this->die_with_404();
 434          }
 435  
 436          if ( $this->invalid_nonce_and_redirect( 'mass-create-transation-sets_' . $project->id ) ) {
 437              return;
 438          }
 439  
 440          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 441              return;
 442          }
 443  
 444          $other_project = GP::$project->get( gp_post( 'project_id' ) );
 445  
 446          if ( ! $other_project ) {
 447              return $this->die_with_error( __( 'Project wasn&#8217;found', 'glotpress' ) );
 448          }
 449  
 450          $changes = $project->set_difference_from( $other_project );
 451  
 452          foreach( $changes['added'] as $to_add ) {
 453              if ( !GP::$translation_set->create( array('project_id' => $project->id, 'name' => $to_add->name, 'locale' => $to_add->locale, 'slug' => $to_add->slug) ) ) {
 454                  $this->errors[] = sprintf( __( 'Couldn&#8217;t add translation set named %s', 'glotpress' ), esc_html( $to_add->name ) );
 455              }
 456          }
 457          foreach( $changes['removed'] as $to_remove ) {
 458              if ( !$to_remove->delete() ) {
 459                  $this->errors[] = sprintf( __( 'Couldn&#8217;t delete translation set named %s', 'glotpress' ), esc_html( $to_remove->name ) );
 460              }
 461          }
 462          if ( empty( $this->errors ) ) {
 463              $this->notices[] = __( 'Translation sets were added and removed successfully', 'glotpress' );
 464          }
 465  
 466          $this->redirect( gp_url_project( $project ) );
 467      }
 468  
 469  	public function mass_create_sets_preview_post( $project_path ) {
 470          $project = GP::$project->by_path( $project_path );
 471  
 472          if ( ! $project ) {
 473              return $this->die_with_404();
 474          }
 475  
 476          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 477              return;
 478          }
 479  
 480          $other_project = GP::$project->get( gp_post( 'project_id' ) );
 481  
 482          if ( ! $other_project ) {
 483              return $this->die_with_error( __( 'Project wasn&#8217;found', 'glotpress' ) );
 484          }
 485  
 486          header('Content-Type: application/json');
 487          echo wp_json_encode( $project->set_difference_from( $other_project ) );
 488      }
 489  
 490  	public function branch_project_get( $project_path ) {
 491          $project = GP::$project->by_path( $project_path );
 492  
 493          if ( ! $project ) {
 494              return $this->die_with_404();
 495          }
 496  
 497          if ( $this->cannot_and_redirect( 'write', 'project', $project->id ) ) {
 498              return;
 499          }
 500  
 501          $this->tmpl( 'project-branch', get_defined_vars() );
 502      }
 503  
 504  
 505  	public function branch_project_post( $project_path ) {
 506          $post = gp_post( 'project' );
 507          $project = GP::$project->by_path( $project_path );
 508  
 509          if ( ! $project ) {
 510              return $this->die_with_404();
 511          }
 512  
 513          if ( $this->invalid_nonce_and_redirect( 'branch-project_' . $project->id ) ) {
 514              return;
 515          }
 516  
 517          $parent_project_id = gp_array_get( $post, 'parent_project_id', null );
 518  
 519          if ( $this->cannot_and_redirect( 'write', 'project', $parent_project_id ) ) {
 520              return;
 521          }
 522  
 523          $new_project_data = new GP_Project( $post );
 524          if ( $this->invalid_and_redirect( $new_project_data ) ){
 525              return;
 526          }
 527  
 528          $new_project_data->active = $project->active;
 529          $new_project = GP::$project->create_and_select( $new_project_data );
 530  
 531          if ( !$new_project ) {
 532              $new_project = new GP_Project();
 533              $this->errors[] = __( 'Error in creating project!', 'glotpress' );
 534              $this->tmpl( 'project-branch', get_defined_vars() );
 535          } else {
 536              $new_project->duplicate_project_contents_from( $project );
 537          }
 538  
 539          $this->redirect( gp_url_project( $new_project ) );
 540      }
 541  
 542  }


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