public function bulk_translate($project_path) { $project_path = urldecode($project_path); $url = gp_url_project($project_path); // If we don't have rights, just redirect back to the project. if (!GP::$user->current()->can('write', 'project')) { gp_redirect($url); } // Create a project class to use to get the project object. $project_class = new GP_Project(); // Get the project object from the project path that was passed in. $project_obj = $project_class->by_path($project_path); // Get the translations sets from the project ID. $translation_sets = GP::$translation_set->by_project_id($project_obj->id); // Loop through all the sets. foreach ($translation_sets as $set) { //Array ( [action] => gtranslate [priority] => 0 [redirect_to] => http://localhost/wp40/gp/projects/sample/bg/my [row-ids] => Array ( [0] => 1 [1] => 2 ) ) $bulk = array('action' => 'gtranslate', 'priority' => 0, 'row-ids' => array()); $translation = new GP_Translation(); $strings = $translation->for_translation($project_obj, $set, null, array('status' => 'untranslated')); foreach ($strings as $string) { $bulk['row-ids'][] .= $string->row_id; } $locale = GP_Locales::by_slug($set->locale); $this->gp_translation_set_bulk_action_post($project_obj, $locale, $set, $bulk); } $url = gp_url_project($project_path); gp_redirect($url); }
public function bulk_export($project_path) { // The project path is url encoded, so decode before we do anything with it. $project_path = urldecode($project_path); // Determine our temporary directory. $temp_dir = gp_const_get('GP_BULK_DOWNLOAD_TRANSLATIONS_TEMP_DIR', sys_get_temp_dir()); // Get a temporary file, use bdt as the first three letters of it. $temp_dir = tempnam($temp_dir, 'bdt'); // Now delete the file and recreate it as a directory. unlink($temp_dir); mkdir($temp_dir); // Create a project class to use to get the project object. $project_class = new GP_Project(); // Get the project object from the project path that was passed in. $project_obj = $project_class->by_path($project_path); // Export the files to the temporary directory. $files = $this->generate_export_files($project_obj, $temp_dir); // Setup the zip file name to use, it's the project name + .zip. $zip_file = $temp_dir . '/' . $project_path . '.zip'; // Create the new archive. $zip = new ZipArchive(); if ($zip->open($zip_file, ZipArchive::CREATE) === TRUE) { // Loop through all of the files we created and add them to the zip file. foreach ($files as $file) { // The first parameter is the full path to the local file, the second is the name as it will appear in the zip file. // Note this does not actually write data to the zip file. $zip->addFile($temp_dir . '/' . $file, $file); } // Close the zip file, this does the actual writing of the data. $zip->close(); } // Since we can't delete the export files until after we close the zip, loop through the files once more // and delete them. foreach ($files as $file) { unlink($temp_dir . '/' . $file); } // Generate our headers for the file download. header('Content-Description: File Transfer'); header('Pragma: public'); header('Expires: 0'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Disposition: attachment; filename=' . $project_path . '.zip'); header('Content-Type: application/octet-stream'); header('Connection: close'); // Write the zip file out to the client. readfile($zip_file); // Delete the zip file. unlink($zip_file); // Remove the temporary directory and we're done! rmdir($temp_dir); }
public function auto_extract($project_path) { // First let's ensure we have decoded the project path for use later. $project_path = urldecode($project_path); // Get the URL to the project for use later. $url = gp_url_project($project_path); // Create a project class to use to get the project object. $project_class = new GP_Project(); // Get the project object from the project path that was passed in. $project_obj = $project_class->by_path($project_path); if (GP::$permission->user_can(wp_get_current_user(), 'write', 'project', $project->id)) { // Get the project settings. $project_settings = (array) get_option('gp_auto_extract', array()); // Since we're running on the front end we need to load the download_url() function from the wp-admin/includes directory. include ABSPATH . 'wp-admin/includes/file.php'; // Extract the strings, the third parameter disables HTML formating of the returned messages as GP doesn't need them. $message = $this->extract_project($project_obj, $project_settings, false); } else { $message = 'You do not have rights to auto extract originals!'; } gp_notice_set($message); // Redirect back to the project home. wp_redirect($url); }
public function duplicate_project_contents_from($source_project) { $source_sub_projects = $source_project->inclusive_sub_projects(); //Duplicate originals, translations sets and translations for the root project $this->copy_originals_from($source_project->id); $this->copy_sets_and_translations_from($source_project->id); //Keep a list of parents to preserve hierarchy $parents = array(); $parents[$source_project->id] = $this->id; //Duplicate originals, translations sets and translations for the child projects foreach ($source_sub_projects as $sub) { $copy_project = new GP_Project($sub->fields()); $copy_project->parent_project_id = $parents[$sub->parent_project_id]; $parent_project = $copy_project->get($copy_project->parent_project_id); $copy_project->path = gp_url_join($parent_project->path, $copy_project->slug); $copy = GP::$project->create($copy_project); $parents[$sub->id] = $copy->id; $copy->copy_originals_from($sub->id); $copy->copy_sets_and_translations_from($sub->id); } }
function test_create_and_select() { $project = new GP_Project(array('name' => '@@@@', 'slug' => '')); $verdict = $project->validate(); $this->assertFalse($verdict); }
public function branch_project_post($project_path) { $post = gp_post('project'); $project = GP::$project->by_path($project_path); if (!$project) { return $this->die_with_404(); } $parent_project_id = gp_array_get($post, 'parent_project_id', null); if ($this->cannot_and_redirect('write', 'project', $parent_project_id)) { return; } $new_project_data = new GP_Project($post); if ($this->invalid_and_redirect($new_project_data)) { return; } $new_project_data->active = $project->active; $new_project = GP::$project->create_and_select($new_project_data); if (!$new_project) { $new_project = new GP_Project(); $this->errors[] = __('Error in creating project!', 'glotpress'); $this->tmpl('project-branch', get_defined_vars()); } else { $new_project->duplicate_project_contents_from($project); } $this->redirect(gp_url_project($new_project)); }
public function bulk_export($project_path) { // The project path is url encoded, so decode before we do anything with it. $project_path = urldecode($project_path); // Loop through the supported format options and determine which ones we're exporting. $include_formats = array(); foreach (GP::$formats as $slug => $format) { if (gp_const_get('GP_BULK_DOWNLOAD_TRANSLATIONS_FORMAT_' . strtoupper(str_replace('.', '-', $slug)), false) == true) { $include_formats[] = $slug; } } // If we didn't have any formats set for export, use PO files by default. if (count($include_formats) == 0) { $include_formats = array('po'); } // Determine our temporary directory. $temp_dir = gp_const_get('GP_BULK_DOWNLOAD_TRANSLATIONS_TEMP_DIR', sys_get_temp_dir()); // Get a temporary file, use bdt as the first three letters of it. $temp_dir = tempnam($temp_dir, 'bdt'); // Now delete the file and recreate it as a directory. unlink($temp_dir); mkdir($temp_dir); // Create a project class to use to get the project object. $project_class = new GP_Project(); // Get the project object from the project path that was passed in. $project_obj = $project_class->by_path($project_path); // Get the translations sets from the project ID. $translation_sets = GP::$translation_set->by_project_id($project_obj->id); // Setup an array to use to track the file names we're creating. $files = array(); // Loop through all the sets. foreach ($translation_sets as $set) { // Loop through all the formats we're exporting foreach ($include_formats as $format) { // Export the PO file for this translation set. $files[] .= $this->_export_to_file($format, $temp_dir, $project_obj, $locale, $set); } } // Setup the zip file name to use, it's the project name + .zip. $zip_file = $temp_dir . '/' . $project_path . '.zip'; // Create the new archive. $zip = new ZipArchive(); if ($zip->open($zip_file, ZipArchive::CREATE) === TRUE) { // Loop through all of the files we created and add them to the zip file. foreach ($files as $file) { // The first parameter is the full path to the local file, the second is the name as it will appear in the zip file. // Note this does not actually write data to the zip file. $zip->addFile($temp_dir . '/' . $file, $file); } // Close the zip file, this does the actual writing of the data. $zip->close(); } // Since we can't delete the export files until after we close the zip, loop through the files once more // and delete them. foreach ($files as $file) { unlink($temp_dir . '/' . $file); } // Generate our headers for the file download. $last_modified = gmdate('D, d M Y H:i:s') . ' GMT'; header('Content-Description: File Transfer'); header('Pragma: public'); header('Expires: 0'); header('Last-Modified: ' . $last_modified); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Disposition: attachment; filename=' . $project_path . '.zip'); header('Content-Type: application/octet-stream'); header('Connection: close'); // Write the zip file out to the client. readfile($zip_file); // Delete the zip file. unlink($zip_file); // Remove the temporary directory and we're done! rmdir($temp_dir); }