| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Epesi\Core\System\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use atk4\ui\Form as BaseForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Epesi\Core\System\Modules\Concerns\Notifies; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use atk4\ui\Label; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Form extends BaseForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	use Notifies; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	public $buttonSave = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	protected $controlRules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	protected $validationRules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	public function addElements($elements, $parent = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		$parent = $parent?: $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		foreach ($elements as $name => $desc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			$name = $desc['name']?? $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			$this->addControlRules($name, $desc['rules']?? []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			switch ($desc['type']?? 'field') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 				case 'field': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 					$desc = is_string($desc)? [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 						'decorator' => [$desc] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 					]: $desc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 					$field = $parent->addControl($name, $desc['decorator']?? [], $desc['options']?? []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 					if ($default = $desc['default']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 						$field->set($default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 					if ($desc['display']?? false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 						$this->addControlDisplayRules([$name => $desc['display']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 					break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 				case 'group': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 					$seed = $desc['seed']?? [$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 					$group = $parent->addGroup($seed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 					$this->addElements($desc['elements'], $group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 					if ($desc['display']?? false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 						$this->addGroupDisplayRules([$name => $desc['display']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 					break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				case 'header': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 					$seed = $desc['seed']?? [$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 					$parent->addHeader($seed); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 					break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 				case 'view': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 					$seed = $desc['seed']?? [Label::class, $name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 					$region = $desc['region']?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 					$this->add($seed, $region); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 					break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 					 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 				default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 					; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 					break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	public function addControlDisplayRules($controlDisplayRules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$this->setControlsDisplayRules(array_merge($this->controlDisplayRules?: [], $controlDisplayRules)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	public function addGroupDisplayRules($groupDisplayRules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$controlDisplayRules = $this->controlDisplayRules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$this->setGroupDisplayRules($groupDisplayRules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$this->addControlDisplayRules($controlDisplayRules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	public function addControlRules($field, $rules = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if (! $rules) return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$this->controlRules[$field] = $rules['rules']?? [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 				'identifier' => $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 				'rules' => $rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | 	 | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	public function validate($callback) | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 		$this->setApiConfig([ | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 				'beforeSend' => new \atk4\ui\JsFunction(['settings'], [ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 						new \atk4\ui\JsExpression('return $(this).form("is valid")') | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 				]), | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 		]); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 		$this->setFormConfig([ | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 				'fields' => $this->controlRules | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 		]); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 		 | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 		$this->onSubmit(function ($form) use ($callback) { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 			$errors = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 			foreach ($this->validationRules?: [] as $ruleCallback) { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  | 				if (! is_callable($ruleCallback)) continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  | 				 | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 				$ruleErrors = $ruleCallback($form); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  | 				 | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 				$errors = array_merge($errors, $ruleErrors?: []); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  | 			 | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 			return $errors?: $callback($this); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 		}); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 			 | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	public function submit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		return $this->js()->form('submit'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	public function confirmLeave($confirm = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$this->canLeave = ! $confirm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 140 |  |  | } |