Jump to content

Issue with Modifying the AnThemeBlock Module – Importing Blocks from JSON


Recommended Posts

Hello PrestaShop community,

I recently purchased the AnThemeBlock module and am customising it to add the ability to import blocks from a JSON file. While the export process works fine, I’m encountering issues with the import functionality, particularly when trying to save the blocks after reading them from the JSON.

 What we’ve done:
1. Exporting Blocks: We’ve successfully implemented the ability to export blocks and their child blocks into a JSON file. I attached one of the generated json

Modifying the import function: We added a function to read this JSON and attempt to import the blocks:

   ```php
    public function importBlocksFromJson()
    {
        $jsonDir = '/var/www/prestashop/prestashop_blocks/json';
        $imageDir = '/var/www/prestashop/prestashop_blocks/images';

        $jsonFiles = glob($jsonDir . '/*.json');
        foreach ($jsonFiles as $file) {
            $jsonData = json_decode(file_get_contents($file), true);
            if (!$jsonData || empty($jsonData['title'])) {
                $this->_errors[] = 'Title missing or invalid JSON in ' . $file;
                continue;
            }

            // Start importing the block
            $this->importBlock($jsonData, 0, $imageDir);
        }
    }

    private function importBlock($jsonData, $parentId = 0, $imageDir)
    {
        $block = new AnThemeBlock();
        $block->id_parent = $parentId;
        $block->status = (int) $jsonData['status'];
        $block->title = pSQL($jsonData['title']); // <-- Issue here
        $block->content = pSQL($jsonData['content']);
        
        if (!$block->save()) {
            $this->_errors[] = 'Error saving block: ' . $jsonData['title'];
        }
    }
    ```

The problem:
When trying to import the blocks, we get the following error:

```
[PrestaShopException]
Property AnThemeBlock->title is empty.
```

Even though the `title` field is present and correctly populated in the JSON, it seems the data isn’t being processed or passed correctly to the `AnThemeBlock` object.

 What we've tried:
- Direct SQL injections: We attempted bypassing the `ObjectModel` by running raw SQL queries to insert data directly into the database, but this also results in null or empty values for the `title` and `content` fields:

    ```php
    $query = "INSERT INTO ps_anthemeblock (id_parent, status, title, content) VALUES ($parentId, $status, '$title', '$content')";
    Db::getInstance()->execute($query);
    ```

    The query executes, but `title` remains empty in the database.

- Decoding and validating JSON: We verified the JSON is correctly structured and the `title` is present and non-empty. However, when passed into the object or the query, it appears as null.

What we need help with:
1. Why isn’t the `title` field being saved, when importing via the `ObjectModel` or direct SQL query?
2. Is there additional validation in PrestaShop that could be preventing this field from being saved?
3. Any advice on how to properly structure the import so that `title` and other fields are saved correctly?

Any insights or suggestions would be greatly appreciated. Thanks in advance!

I tried attaching the PHP file, but I think it's not allowed, so I'm sending a txt with the code instead. However, it seems the forum doesn't allow .txt attachments either. Therefore, I've uploaded the PHP code online, and you can view or download it using the following link:

LINK TO CODE: REMOVED

671918fb06a3e.json

Edited by endriu107
removed link (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...