<commit_before><?php

namespace common\models;

use Yii;
use yii\helpers\VarDumper;

class TemplateGenerator
{
    protected static $_imagesPath = '@backend/web/templateImages/';
    protected static $_templateImagesPath = 'images';

    public static function create($q)
    {
        $file = tempnam("tmp", uniqid('zip'));
        $zip = new \ZipArchiveEx();
        $zip->open($file, \ZipArchive::OVERWRITE);
        $zip->addEmptyDir('images');

        $commonTemplates = Template::find()->innerJoinWith('category')
            ->where(['template_category.is_basic' => 1])->all();

        $selectedIds = explode(',', $q);
        $map = [];
        foreach ($selectedIds as $id) {
            $selectedTemplate = Template::findOne(intval($id));

            if (empty($selectedTemplate))
                continue;

            foreach ($selectedTemplate->parents as $parent) {
                $map[$parent->id][] = $selectedTemplate;
            }
        }

        $files = self::prepareCommonFiles($commonTemplates);

        foreach ($files as $filename => $fileData) {
            foreach ($fileData as $directory => $fileContent) {
                $path = $directory != '' ? $directory . '/' . $filename : $filename;

                if (isset($fileContent['image'])) { // Add image to archive
                    $zipPath = self::getTemplateImagesPath() . '/' . $path;
                    $zip->addFile(Yii::getAlias(self::getImagesPath() . $path), $zipPath);
                } else {
                    $code = $fileContent['code']; // common code
                    if (isset($map[$fileContent['id']])) { // children files
                        foreach ($map[$fileContent['id']] as $child) {
                            $code .= "\n" . $child->code;

                            /** Linked images **/
                            $images = $child->images; // children can see images
                            if (is_array($images) && count($images)) {
                                foreach ($images as $image) {
                                    $p = $image->directory != '' ? $image->directory . '/' . $image->filename : $image->filename;
                                    $zipPath = self::getTemplateImagesPath() . '/' . $p;
                                    $zip->addFile(Yii::getAlias(self::getImagesPath() . $p), $zipPath);
                                }
                            }
                        }
                    }
                    $zip->addFromString($path, $code);
                }
            }<commit_msg>footer: child code should be before common code<commit_after><?php

namespace common\models;

use Yii;
use yii\helpers\VarDumper;

class TemplateGenerator
{
    protected static $_imagesPath = '@backend/web/templateImages/';
    protected static $_templateImagesPath = 'images';

    public static function create($q)
    {
        $file = tempnam("tmp", uniqid('zip'));
        $zip = new \ZipArchiveEx();
        $zip->open($file, \ZipArchive::OVERWRITE);
        $zip->addEmptyDir('images');

        $commonTemplates = Template::find()->innerJoinWith('category')
            ->where(['template_category.is_basic' => 1])->all();

        $selectedIds = explode(',', $q);
        $map = [];
        foreach ($selectedIds as $id) {
            $selectedTemplate = Template::findOne(intval($id));

            if (empty($selectedTemplate))
                continue;

            foreach ($selectedTemplate->parents as $parent) {
                $map[$parent->id][] = $selectedTemplate;
            }
        }

        $files = self::prepareCommonFiles($commonTemplates);

        foreach ($files as $filename => $fileData) {
            foreach ($fileData as $directory => $fileContent) {
                $path = $directory != '' ? $directory . '/' . $filename : $filename;

                if (isset($fileContent['image'])) { // Add image to archive
                    $zipPath = self::getTemplateImagesPath() . '/' . $path;
                    $zip->addFile(Yii::getAlias(self::getImagesPath() . $path), $zipPath);
                } else {
                    $code = $fileContent['code']; // common code
                    if (isset($map[$fileContent['id']])) { // children files
                        foreach ($map[$fileContent['id']] as $child) {
                            if ($filename == 'footer.php') { // Add code to the footer - child code should be before common code
                                $code = $child->code . "\n" . $code;
                            } else {
                                $code .= "\n" . $child->code;
                            }

                            /** Linked images **/
                            $images = $child->images; // children can see images
                            if (is_array($images) && count($images)) {
                                foreach ($images as $image) {
                                    $p = $image->directory != '' ? $image->directory . '/' . $image->filename : $image->filename;
                                    $zipPath = self::getTemplateImagesPath() . '/' . $p;
                                    $zip->addFile(Yii::getAlias(self::getImagesPath() . $p), $zipPath);
                                }
                            }
                        }
                    }
                    $zip->addFromString($path, $code);
                }
            }
{'fraction_non_alphanumeric': 0.14787566672797497, 'fraction_numerical': 0.0003678499172337686, 'mean_word_length': 1.453971119133574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2473236', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1326, 'n_words': 279}