| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Epesi\Core\System\Logo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Epesi\Core\System\Modules\ModuleView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Epesi\Core\System\View\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Epesi\Core\System\Model\Variable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Facades\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Epesi\Core\Layout\View\ActionBar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use atk4\ui\View; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class LogoSettings extends ModuleView | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 	protected $label = 'Title & Logo'; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | 	 | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	protected static $defaultLogo = 'epesi-logo.png'; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	public static function access() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	{ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  | 		return Auth::user()->can('modify system settings'); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	public function body() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$layout = View::addTo($this)->addStyle('max-width:1200px;margin:auto;'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		\atk4\ui\Header::addTo($layout, [ __($this->label)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$segment = View::addTo($layout, ['ui' => 'segment']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		$form = Form::addTo($segment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$form->addControl('title', __('Base page title')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$form->addControl('custom_logo', [\atk4\ui\Form\Control\Checkbox::class, 'caption' => __('Use custom logo')]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$form->model->setMulti([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		        'title' => Variable::recall('system.title'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		        'custom_logo' => (bool) Variable::recall('system.logo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$logo = $form->addControl('logo', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 				\atk4\ui\Form\Control\UploadImage::class,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 				'defaultSrc' => url('logo'),  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 				'thumbnail' => (new View(['element'=>'img', 'class' => ['right', 'floated', 'image'], 'ui' => true]))->setStyle('max-width', '150px'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 				'placeholder' => __('Upload file to replace system logo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$form->addControlDisplayRules(['logo' => ['custom_logo' => 'checked']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$logo->onDelete(function($fileName) use ($logo) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			$this->storage()->delete(self::alias() . '/tmp/' . $fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			$logo->setThumbnailSrc(asset('storage/' . self::alias() . '/' . self::$defaultLogo)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$logo->onUpload(function ($files) use ($form, $logo) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			if ($files === 'error')	return $form->error('logo', __('Error uploading image')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			$tmpPath = self::alias() . '/tmp/' . $files['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			$logo->setThumbnailSrc(asset('storage/' . $tmpPath)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$this->storage()->put($tmpPath, file_get_contents($files['tmp_name'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$form->onSubmit(function($form) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			if ($logo = $form->model->get('custom_logo') ? $form->model->get('logo') : null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 				$storage = $this->storage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 				$from = self::alias() . '/tmp/' . $logo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 				$to = self::alias() . '/' . $logo;				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				if ($storage->exists($to)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 					$storage->delete($to); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 				$storage->move($from, $to); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	        Variable::memorize('system.logo', $logo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			Variable::memorize('system.title', $form->model->get('title')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			return $this->notifySuccess(__('Title and logo updated! Refresh page to see changes ...')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		ActionBar::addItemButton('back')->link(url('view/system')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | 		ActionBar::addItemButton('save')->on('click', $form->submit()); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 	 | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 	public static function getLogoFile() | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 		return self::storage()->path(self::alias() . '/' . Variable::recall('system.logo', self::$defaultLogo)); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	public static function getTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	{ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | 		return Variable::recall('system.title', config('epesi.ui.title')); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 99 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	public static function storage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	{ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | 		return Storage::disk('public'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |