Exemple #1
0
 public function seedTemplates()
 {
     $css = '';
     $js = '';
     $dir = new DirectoryIterator($this->app['base_dir'] . '/templates');
     foreach ($dir as $item) {
         if ($item->isDir() && !$item->isDot()) {
             $name = $this->nameFromPath($item->getRealPath());
             //check if template doesn't already exist in db
             $template = DB::table('templates')->where('name', $name)->first();
             if (!$template) {
                 //if no config file exists for template continue
                 if (!file_exists($item->getRealPath() . '/config.php')) {
                     continue;
                 }
                 $config = (require $item->getRealPath() . '/config.php');
                 //create a new template in db
                 DB::table('templates')->insert(array('name' => $config['name'], 'color' => $config['color'], 'category' => $config['category'], 'thumbnail' => 'templates/' . strtolower(str_replace(' ', '-', $config['name'])) . '/thumbnail.png'));
                 $id = DB::getPdo()->lastInsertId();
                 //get css
                 if (file_exists($item->getRealPath() . '/css/styles.css')) {
                     $css = file_get_contents($item->getRealPath() . '/css/styles.css');
                 }
                 //get js
                 if (file_exists($item->getRealPath() . '/js/scripts.js')) {
                     $js = file_get_contents($item->getRealPath() . '/js/scripts.js');
                 }
                 //loop trough all .html files and create a page in db for each one
                 $items = new \DirectoryIterator($item->getRealPath());
                 foreach ($items as $file) {
                     if (pathinfo($file->getFilename(), PATHINFO_EXTENSION) === 'html') {
                         DB::table('pages')->insert(array('name' => str_replace('.' . pathinfo($file->getFilename(), PATHINFO_EXTENSION), '', $file->getFileName()), 'html' => file_get_contents($file->getRealPath()), 'css' => $css, 'js' => $js, 'libraries' => isset($config['libraries']) ? json_encode($config['libraries']) : '', 'theme' => isset($config['theme']) ? $config['theme'] : 'default', 'pageable_id' => $id, 'pageable_type' => 'Template'));
                     }
                 }
             }
         }
     }
 }