| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace Grav\Plugin\Admin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-21 14:44:41 +02:00
										 |  |  | use Grav\Common\Filesystem\Folder; | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | use Grav\Common\Grav; | 
					
						
							| 
									
										
										
										
											2020-05-07 21:20:08 +01:00
										 |  |  | use Grav\Framework\File\File; | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  | use RocketTheme\Toolbox\Event\Event; | 
					
						
							| 
									
										
										
										
											2020-04-19 23:52:23 -06:00
										 |  |  | use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; | 
					
						
							| 
									
										
										
										
											2020-05-07 21:20:08 +01:00
										 |  |  | use Symfony\Component\Yaml\Yaml; | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 09:57:44 -06:00
										 |  |  | class WhiteLabel | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     protected $grav; | 
					
						
							|  |  |  |     protected $scss; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->grav = Grav::instance(); | 
					
						
							|  |  |  |         $this->scss = new ScssCompiler(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  |     public function compilePresetScss($config, $options = [ | 
					
						
							| 
									
										
										
										
											2020-04-19 23:52:23 -06:00
										 |  |  |             'input' => 'plugin://admin/themes/grav/scss/preset.scss', | 
					
						
							|  |  |  |             'output' => 'asset://admin-preset.css' | 
					
						
							|  |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (is_array($config)) { | 
					
						
							|  |  |  |             $color_scheme   = $config['color_scheme']; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2020-04-20 09:57:44 -06:00
										 |  |  |             $color_scheme   = $config->get('whitelabel.color_scheme'); | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($color_scheme) { | 
					
						
							| 
									
										
										
										
											2020-04-19 23:52:23 -06:00
										 |  |  |             /** @var UniformResourceLocator $locator */ | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  |             $locator       = $this->grav['locator']; | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  |             // Use ScssList object to make it easier ot handle in event
 | 
					
						
							|  |  |  |             $scss_list     = new ScssList($locator->findResource($options['input'])); | 
					
						
							|  |  |  |             $output_css    = $locator->findResource(($options['output']), true, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-21 14:44:41 +02:00
										 |  |  |             Folder::create(dirname($output_css)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  |             Grav::instance()->fireEvent('onAdminCompilePresetSCSS', new Event(['scss' => $scss_list])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Convert bak to regular array now we have run the event
 | 
					
						
							|  |  |  |             $input_scss = $scss_list->all(); | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 23:52:23 -06:00
										 |  |  |             $imports = [$locator->findResource('plugin://admin/themes/grav/scss')]; | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  |             foreach ($input_scss as $scss) { | 
					
						
							|  |  |  |                 $input_path = dirname($scss); | 
					
						
							|  |  |  |                 if (!in_array($input_path, $imports)) { | 
					
						
							|  |  |  |                     $imports[] = $input_path; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-04-19 23:52:23 -06:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-04-19 18:14:12 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 18:31:11 -06:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  |                 $compiler = $this->scss->reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $compiler->setVariables($color_scheme['colors'] + $color_scheme['accents']); | 
					
						
							|  |  |  |                 $compiler->setImportPaths($imports); | 
					
						
							|  |  |  |                 $compiler->compileAll($input_scss, $output_css); | 
					
						
							| 
									
										
										
										
											2020-04-19 18:31:11 -06:00
										 |  |  |             } catch (\Exception $e) { | 
					
						
							|  |  |  |                 return [false, $e->getMessage()]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 13:08:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 18:14:12 -06:00
										 |  |  |             return [true, 'Recompiled successfully']; | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-19 18:14:12 -06:00
										 |  |  |         return [false, ' Could not be recompiled, missing color scheme...']; | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-12 18:08:22 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function exportPresetScsss($config, $location = 'asset://admin-theme-export.yaml') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($config['color_scheme'])) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $color_scheme = $config['color_scheme']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $body = Yaml::dump($color_scheme); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $file = new File($location); | 
					
						
							|  |  |  |             $file->save($body); | 
					
						
							|  |  |  |             // todo: handle errors/exceptions?
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return [true, 'File created successfully']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return [false, ' Could not export, missing color scheme...']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-18 17:45:11 -06:00
										 |  |  | } |