Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import/Export Option #30

Open
ArchBlood opened this issue Aug 11, 2020 · 8 comments
Open

Import/Export Option #30

ArchBlood opened this issue Aug 11, 2020 · 8 comments

Comments

@ArchBlood
Copy link
Contributor

Would be amazing to have an import/export option for this module, I know it's not widely used anymore but would be a lot easier to update the repos on GitHub instead of having to go through all the directories just to get the files to upload to their repos

@luke-
Copy link
Contributor

luke- commented Aug 13, 2020

That would be really practical!

@ArchBlood
Copy link
Contributor Author

@luke- would it be possible to implement something like the following?

https://github.com/kbjr/Git.php/

@luke-
Copy link
Contributor

luke- commented Nov 13, 2023

@ArchBlood hmm not sure. maybe it's better to directly extract the messages from git repos.

@ArchBlood
Copy link
Contributor Author

@luke- how about something like the following?

Of course, the following code can be fixed to better suit HumHub's needs

Export

// Function to dynamically find and create a zip file of the 'messages' directory within a specific module
function createModuleMessagesZip() {
    // Ensure the ZipArchive class is available
    if (!class_exists('ZipArchive')) {
        return 'ZipArchive class is not available.';
    }

    // Assuming 'MyModule' is the name of your module
    $moduleDirectory = Yii::getAlias('@webroot/protected/modules/{module-id}/');

    // Check if the module directory exists
    if (is_dir($moduleDirectory)) {
        // Initialize the directory queue with the module directory
        $directories = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($moduleDirectory),
            RecursiveIteratorIterator::SELF_FIRST
        );

        foreach ($directories as $directory) {
            if ($directory->isDir() && $directory->getBasename() === 'messages') {
                // Found the 'messages' directory within the module

                // Create a new zip archive
                $zip = new ZipArchive();
                $zipFileName = 'module_messages.zip';

                if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
                    return 'Failed to create the zip file.';
                }

                // Add directory contents to the zip file
                $files = new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($directory->getPathname()),
                    RecursiveIteratorIterator::LEAVES_ONLY
                );

                foreach ($files as $name => $file) {
                    if (!$file->isDir()) {
                        $filePath = $file->getRealPath();
                        $relativePath = substr($filePath, strlen($directory->getPath()) + 1);

                        $zip->addFile($filePath, $relativePath);
                    }
                }

                // Close the zip file
                $zip->close();

                // Return the zip file name or a success message
                return 'Zip file created: ' . $zipFileName;
            }
        }
    }

    // If 'messages' directory within the module is not found
    return 'Directory "messages" within the module not found.';
}
// Usage example within your HumHub module or controller action
$result = createModuleMessagesZip();
echo $result;

Import

// Function to handle importing the messages folder into a specific module
function importModuleMessages($zipFile) {
    // Ensure the ZipArchive class is available
    if (!class_exists('ZipArchive')) {
        return 'ZipArchive class is not available.';
    }

    // Assuming 'MyModule' is the name of your module
    $moduleDirectory = Yii::getAlias('@webroot/protected/modules/MyModule/');
    $messagesDirectory = $moduleDirectory . 'messages/';

    // Check if the module directory exists
    if (!is_dir($moduleDirectory)) {
        return 'Module directory not found.';
    }

    // Check if the messages directory exists, if not, create it
    if (!is_dir($messagesDirectory)) {
        mkdir($messagesDirectory, 0777, true);
    }

    // Open the uploaded zip file
    $zip = new ZipArchive();
    if ($zip->open($zipFile) === true) {
        // Extract zip contents to the messages directory
        $zip->extractTo($messagesDirectory);
        $zip->close();

        return 'Import successful.';
    } else {
        return 'Failed to open the zip file.';
    }
}
// Assuming this code is part of an action in a module controller
if (isset($_FILES['zipFile'])) {
    $uploadedFile = $_FILES['zipFile']['tmp_name'];
    $result = importModuleMessages($uploadedFile);
    echo $result;
}

@luke-
Copy link
Contributor

luke- commented Nov 25, 2023

Look good, but I don't know if we need an import/export function. The module is actually only intended for internal use at translate.humhub.org and changes here should always be pushed to the respective GitHub repositories regularly.

@ArchBlood
Copy link
Contributor Author

Look good, but I don't know if we need an import/export function. The module is actually only intended for internal use at translate.humhub.org and changes here should always be pushed to the respective GitHub repositories regularly.

I had that thought on my mind as well, but I also thought it would be easier for module developers that also use the module as well. I could probably implement it in a way that does a push and pull for GitHub repos but thought it would overly complicate the module. 🤔

@luke-
Copy link
Contributor

luke- commented Nov 25, 2023

But if module developers like e.g. GreenMeteor use the module, can they simply push the changed translation messages into the Git repos like we do on the HumHub translation page?

@ArchBlood
Copy link
Contributor Author

But if module developers like e.g. GreenMeteor use the module, can they simply push the changed translation messages into the Git repos like we do on the HumHub translation page?

Currently not possible for GreenMeteor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants