08/04/17 06:33:38 /home/den/projects/ftl-db/addons/default/defr/ftl-theme/src/Page/PageSeeder.php
   1 
<?php namespace Defr\FtlTheme\Page;
   2 
 
   3 
use Anomaly\FilesModule\File\Contract\FileRepositoryInterface;
   4 
use Anomaly\FilesModule\File\FileUploader;
   5 
use Anomaly\FilesModule\Folder\Contract\FolderRepositoryInterface;
   6 
use Anomaly\PagesModule\Page\Contract\PageRepositoryInterface;
   7 
use Anomaly\PagesModule\Type\Contract\TypeRepositoryInterface;
   8 
use Illuminate\Filesystem\Filesystem;
   9 
use Illuminate\Http\UploadedFile;
  10 
 
  11 
class PageSeeder extends \Anomaly\PagesModule\Page\PageSeeder
  12 
{
  13 
 
  14 
    /**
  15 
     * The files repository.
  16 
     *
  17 
     * @var FileRepositoryInterface
  18 
     */
  19 
    protected $files;
  20 
 
  21 
    /**
  22 
     * Uploader
  23 
     *
  24 
     * @var FileUploader
  25 
     */
  26 
    protected $uploader;
  27 
 
  28 
    /**
  29 
     * The folders repository.
  30 
     *
  31 
     * @var FolderRepositoryInterface
  32 
     */
  33 
    protected $folders;
  34 
 
  35 
    /**
  36 
     * Create a new ProductSeeder instance.
  37 
     *
  38 
     * @param Filesystem                $filesystem The filesystem
  39 
     * @param FileUploader              $uploader   The uploader
  40 
     * @param PageRepositoryInterface   $pages      The pages
  41 
     * @param TypeRepositoryInterface   $types      The types
  42 
     * @param FileRepositoryInterface   $files      The files
  43 
     * @param FolderRepositoryInterface $folders    The folders
  44 
     */
  45 
    public function __construct(
  46 
        Filesystem $filesystem,
  47 
        FileUploader $uploader,
  48 
        PageRepositoryInterface $pages,
  49 
        TypeRepositoryInterface $types,
  50 
        FileRepositoryInterface $files,
  51 
        FolderRepositoryInterface $folders
  52 
    )
  53 
    {
  54 
        parent::__construct($pages, $types);
  55 
 
  56 
        $this->files      = $files;
  57 
        $this->folders    = $folders;
  58 
        $this->uploader   = $uploader;
  59 
        $this->filesystem = $filesystem;
  60 
    }
  61 
 
  62 
    /**
  63 
     * Run the seeder
  64 
     */
  65 
    public function run()
  66 
    {
  67 
        echo "\n\033[37;5;228mStarting pages seeder!\n";
  68 
 
  69 
        $this->pages->truncate();
  70 
        $this->files->truncate();
  71 
        $this->folders->truncate();
  72 
 
  73 
        echo "\033[35;5;228mPages truncated!\n";
  74 
 
  75 
        $data = include_once __DIR__ . '/../../resources/seeder/modx_site_content.php';
  76 
 
  77 
        $home = true;
  78 
 
  79 
        foreach ($data as $item)
  80 
        {
  81 
            $title   = array_get($item, 'pagetitle', '');
  82 
            $slug    = array_get($item, 'alias', '');
  83 
            $content = array_get($item, 'content', '');
  84 
            $parent  = array_get($item, 'parent', '');
  85 
            $summary = array_get($item, 'introtext', '');
  86 
 
  87 
            $type = ($parent == '2') ? $this->types->findBySlug('event') : $this->types->findBySlug('default');
  88 
 
  89 
            $entry = $type->getEntryModel()->create([
  90 
                'en' => [
  91 
                    'content' => $content,
  92 
                ],
  93 
                'ru' => [
  94 
                    'content' => $content,
  95 
                ],
  96 
            ]);
  97 
 
  98 
            $this->pages->create([
  99 
                'ru'           => [
 100 
                    'title'            => $title,
 101 
                    'meta_title'       => $title,
 102 
                    'meta_description' => $summary,
 103 
                ],
 104 
                'en'           => [
 105 
                    'title'            => $title,
 106 
                    'meta_title'       => $title,
 107 
                    'meta_description' => $summary,
 108 
                ],
 109 
                'slug'         => $slug,
 110 
                'entry'        => $entry,
 111 
                'type'         => $type,
 112 
                'enabled'      => true,
 113 
                'home'         => $home,
 114 
                'parent'       => $parent,
 115 
                'theme_layout' => 'theme::layouts/default.twig',
 116 
            ])->allowedRoles()->sync([]);
 117 
 
 118 
            $home = false;
 119 
 
 120 
            echo "\033[36;5;228mCreated page \033[31;5;228m{$title}. Slug: {$slug}\n";
 121 
 
 122 
            if ($parent == '2')
 123 
            {
 124 
                if ($folder = $this->folders->findBySlug(md5($slug)))
 125 
                {
 126 
                    $images = $this->filesystem->glob(__DIR__ . '/../../resources/seeder/gallery/' . $slug . '/*.jpg');
 127 
 
 128 
                    $count = count($images);
 129 
 
 130 
                    echo "\033[31;5;228mFound {$count} images. \033[36;5;228mStarting images upload!\n";
 131 
 
 132 
                    $i   = 1;
 133 
                    $ids = [];
 134 
 
 135 
                    foreach ($images as $image)
 136 
                    {
 137 
                        $uploaded = new UploadedFile(
 138 
                            $image,
 139 
                            $slug . '_' . $i . '.jpg',
 140 
                            'image/jpeg',
 141 
                            filesize($image),
 142 
                            null,
 143 
                            true
 144 
                        );
 145 
 
 146 
                        $file = $this->uploader->upload($uploaded, $folder);
 147 
 
 148 
                        $ids[] = $file->getId();
 149 
 
 150 
                        ++$i;
 151 
 
 152 
                        echo "\033[31;5;228mUploaded {$image}.\n";
 153 
                    }
 154 
 
 155 
                    echo "\033[36;5;228mAll images was uploaded!\n";
 156 
 
 157 
                    $entry->gallery()->sync($ids);
 158 
                    $entry->save();
 159 
                }
 160 
            }
 161 
        }
 162 
 
 163 
        echo "\033[32;5;228mPages was seeded successfully!\n";
 164 
    }
 165 
}