| 1 | 2 | simandl | <?php |
| 2 | | | |
| 3 | | | namespace Phem\Libraries\Forms; |
| 4 | | | |
| 5 | | | use Srovnavac\Libraries\Environment\EnvironmentManager; |
| 6 | | | use Doctrine\Common\Annotations\AnnotationReader; |
| 7 | | | use Phem\Core\Collection; |
| 8 | | | use Phem\Core\CollectionSortType; |
| 9 | | | use Phem\Libraries\Forms\Annotations\Form as AnnotationForm; |
| 10 | | | use Phem\Libraries\Forms\Model\Form; |
| 11 | | | use Phem\Libraries\Forms\Model\FormField; |
| 12 | | | use Phem\Libraries\Forms\Model\FormGroup; |
| 13 | | | use ReflectionClass; |
| 14 | | | use ReflectionProperty; |
| 15 | | | |
| 16 | | | /** |
| 17 | | | * @author kubapet |
| 18 | | | */ |
| 19 | | | class AnnotationFormBuilder |
| 20 | | | { |
| 21 | | | |
| 22 | | | private $readOnlyAll = false; |
| 23 | | | private $version = 0; |
| 24 | | | private $basePrefix = "Phem_"; |
| 25 | | | |
| 26 | | | public function buildForm($className, $object = null, $form = null, $form_id = null, $prefix = 'Phem_') |
| 27 | | | { |
| 28 | | | |
| 29 | | | |
| 30 | | | $form = $this->recursionBuildForm($className, $object, $form, $prefix); |
| 31 | | | |
| 32 | | | /* insert form_id */ |
| 33 | | | $formIdField = new FormField(); |
| 34 | | | $formIdField->setName($prefix . "version"); |
| 35 | | | $formIdField->setValue($this->version); |
| 36 | | | $formIdField->setType("hidden"); |
| 37 | | | $form->getGroups()->get($form->getDefaultGroup())->getFields()->add($formIdField); |
| 38 | | | |
| 39 | | | $form->getGroups()->sort(CollectionSortType::NUMERIC | CollectionSortType::ASC | CollectionSortType::VALUE, "Order"); |
| 40 | | | |
| 41 | | | foreach ($form->getGroups() as $group) |
| 42 | | | { |
| 43 | | | $group->getFields()->sort(CollectionSortType::NUMERIC | CollectionSortType::ASC | CollectionSortType::VALUE, "Order"); |
| 44 | | | } |
| 45 | | | |
| 46 | | | return $form; |
| 47 | | | } |
| 48 | | | |
| 49 | | | private function recursionBuildForm($className, $object = null, $form = null, $prefix = 'Phem_') |
| 50 | | | { |
| 51 | | | $r = new ReflectionClass($className); |
| 52 | | | |
| 53 | | | $annotationReader = new AnnotationReader(); |
| 54 | | | $classAnnotations = $annotationReader->getClassAnnotations($r); |
| 55 | | | |
| 56 | | | |
| 57 | | | if ($form == null) |
| 58 | | | { |
| 59 | | | $groups = new Collection(); |
| 60 | | | } |
| 61 | | | else |
| 62 | | | $groups = $form->getGroups(); |
| 63 | | | |
| 64 | | | $formSourceField = null; |
| 65 | | | if ($form == null) |
| 66 | | | { |
| 67 | | | $formSourceField = new FormField(); |
| 68 | | | $formSourceField->setName('form_source'); |
| 69 | | | $formSourceField->setValue($className); |
| 70 | | | $formSourceField->setType("hidden"); |
| 71 | | | } |
| 72 | | | |
| 73 | | | $hasFormAnnotation = false; |
| 74 | | | foreach ($classAnnotations as $ca) |
| 75 | | | { |
| 76 | | | if ($ca instanceof AnnotationForm) |
| 77 | | | { |
| 78 | | | $hasFormAnnotation = true; |
| 79 | | | if ($form == null) |
| 80 | | | { |
| 81 | | | $form = new Form(); |
| 82 | | | $form->setCriticalFields(new Collection()); |
| 83 | | | $form->setAction($ca->action == null ? null : EnvironmentManager::getLinkBuilder()->link($ca->action)); |
| 84 | | | $form->setName($ca->name); |
| 85 | | | $form->setMethod($ca->method); |
| 86 | | | $form->setDefaultGroup($ca->defaultGroup); |
| 87 | | | $form->setAjax($ca->ajax); |
| 88 | | | $form->setSubmittedButton($ca->submittedButton); |
| 89 | | | $form->setGroups($groups); |
| 90 | | | } |
| 91 | | | elseif ($ca->defaultGroup != null) |
| 92 | | | $form->setDefaultGroup($ca->defaultGroup); |
| 93 | | | } |
| 94 | | | elseif ($ca instanceof Annotations\Group) |
| 95 | | | { |
| 96 | | | $group = new FormGroup(); |
| 97 | | | $group->setEnabled($ca->enabled); |
| 98 | | | $group->setOrder($ca->order); |
| 99 | | | $group->setShow($object == null ? true : false); |
| 100 | | | $group->setDefaultOpen($ca->defaultOpen); |
| 101 | | | $group->setFields(new Collection()); |
| 102 | | | if ($ca->localGroupScope && $prefix != $this->basePrefix) |
| 103 | | | { |
| 104 | | | $parentGroup = $groups->get($form->getDefaultGroup()); |
| 105 | | | $group->setCaption($parentGroup->getCaption() . ":" . $ca->caption); |
| 106 | | | $groups->put($form->getDefaultGroup() . "_" . $ca->name, $group); |
| 107 | | | } |
| 108 | | | else |
| 109 | | | { |
| 110 | | | $group->setCaption($ca->caption); |
| 111 | | | $groups->put($ca->name, $group); |
| 112 | | | } |
| 113 | | | } |
| 114 | | | } |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | /* check and validate class annotations */ |
| 120 | | | /* @var $formAnotation AnnotationForm */ |
| 121 | | | if (!$hasFormAnnotation) |
| 122 | | | { |
| 123 | | | return; |
| 124 | | | } |
| 125 | | | |
| 126 | | | |
| 127 | | | |
| 128 | | | $properties = $r->getProperties(); |
| 129 | | | |
| 130 | | | $fields = array(); |
| 131 | | | foreach ($properties as $prop) |
| 132 | | | { |
| 133 | | | /* @var $prop ReflectionProperty */ |
| 134 | | | $getMethod = "get" . ucFirst($prop->name); |
| 135 | | | if (!method_exists($className, $getMethod)) |
| 136 | | | { |
| 137 | | | throw new FormBuilderException( |
| 138 | | | "FormBuilder: Missing get method for property " |
| 139 | | | . $prop->name . " of class " |
| 140 | | | . $className |
| 141 | | | ); |
| 142 | | | } |
| 143 | | | //echo "className: ".$className; |
| 144 | | | $fields[$prop->name] = (method_exists($object, $getMethod) ? $object->$getMethod() : null); |
| 145 | | | } |
| 146 | | | |
| 147 | | | if ($groups->isEmpty()) |
| 148 | | | { |
| 149 | | | $group = new FormGroup(); |
| 150 | | | $group->setCaption($className); |
| 151 | | | $group->setEnabled(1); |
| 152 | | | $group->setOrder(1); |
| 153 | | | $group->setShow(true); |
| 154 | | | $group->setFields(new Collection()); |
| 155 | | | $groups->put("default", $group); |
| 156 | | | $form->setDefaultGroup("default"); |
| 157 | | | } |
| 158 | | | if ($prefix == $this->basePrefix) |
| 159 | | | if ($groups->containsKey($form->getDefaultGroup())) |
| 160 | | | $groups->get($form->getDefaultGroup())->getFields()->add($formSourceField); |
| 161 | | | else |
| 162 | | | throw new FormBuilderException( |
| 163 | | | "FormBuilder: defaultGroup '" . $form->getDefaultGroup() . "' does not exist in $className" |
| 164 | | | ); |
| 165 | | | |
| 166 | | | |
| 167 | | | /* |
| 168 | | | * Iterate over fields |
| 169 | | | * and process them according to their annotations |
| 170 | | | */ |
| 171 | | | |
| 172 | | | $childs = array(); |
| 173 | | | foreach ($fields as $fieldName => $fieldValue) |
| 174 | | | { |
| 175 | | | $formField = null; |
| 176 | | | |
| 177 | | | $annotations = $annotationReader->getPropertyAnnotations($r->getProperty($fieldName)); |
| 178 | | | $generatedValue = false; |
| 179 | | | $fieldEnabled = true; |
| 180 | | | $selectedGroup = $form->getDefaultGroup(); |
| 181 | | | |
| 182 | | | foreach ($annotations as $annotation) |
| 183 | | | { |
| 184 | | | if ($annotation instanceof Annotations\Input) |
| 185 | | | { |
| 186 | | | /*if ($this->version != 0 && empty($annotation->formVersion)) |
| 187 | | | { |
| 188 | | | if (!in_array($this->version, $annotation->formVersion)) |
| 189 | | | continue; |
| 190 | | | }*/ |
| 191 | | | /* @var $annotation Annotations\Input */ |
| 192 | | | $formField = new FormField(); |
| 193 | | | $caption = ($annotation->caption != null ? $annotation->caption : $fieldName); |
| 194 | | | $formField->setCaption(($annotation->required == false ? $caption : '* ' . $caption)); |
| 195 | | | $formField->setName($prefix . $fieldName); |
| 196 | | | $formField->setEnabled($annotation->enabled); |
| 197 | | | $formField->setOrder($annotation->order); |
| 198 | | | $formField->setReadOnly($annotation->readOnly); |
| 199 | | | $formField->setValue($fieldValue); |
| 200 | | | switch ($annotation->type) |
| 201 | | | { |
| 202 | | | case "password": |
| 203 | | | $formField->setType('text'); |
| 204 | | | $formField->setRequired($annotation->required); |
| 205 | | | break; |
| 206 | | | case "text": |
| 207 | | | $formField->setType('text'); |
| 208 | | | $formField->setRequired($annotation->required); |
| 209 | | | break; |
| 210 | | | case "file": |
| 211 | | | $formField->setType('file'); |
| 212 | | | $formField->setRequired($annotation->required); |
| 213 | | | break; |
| 214 | | | case "date": |
| 215 | | | $formField->setType('date'); |
| 216 | | | $formField->setValue($fieldValue instanceof \DateTime ? $fieldValue->format("d.m.Y H:i:s") : null); |
| 217 | | | $formField->setRequired($annotation->required); |
| 218 | | | break; |
| 219 | | | case "checkbox": |
| 220 | | | $formField->setType('checkbox'); |
| 221 | | | $formField->setRequired($annotation->required); |
| 222 | | | break; |
| 223 | | | case "hidden": |
| 224 | | | $formField->setType('hidden'); |
| 225 | | | break; |
| 226 | | | } |
| 227 | | | } |
| 228 | | | elseif ($annotation instanceof Annotations\Select) |
| 229 | | | { |
| 230 | | | $formField = new FormField(); |
| 231 | | | $formField->setCaption(($annotation->caption != null ? $annotation->caption : $fieldName)); |
| 232 | | | if ($annotation->required) |
| 233 | | | $formField->setCaption('* ' . $formField->getCaption()); |
| 234 | | | $formField->setName($prefix . $fieldName); |
| 235 | | | $formField->setType("select"); |
| 236 | | | $formField->setOrder($annotation->order); |
| 237 | | | $formField->setEnabled($annotation->enabled); |
| 238 | | | $formField->setValue($fieldValue); |
| 239 | | | $formField->setRequired($annotation->required); |
| 240 | | | $getMethod = $annotation->options; |
| 241 | | | if (!method_exists($className, $getMethod)) |
| 242 | | | { |
| 243 | | | throw new FormBuilderException( |
| 244 | | | "FormBuilder: Missing get method " . |
| 245 | | | $getMethod . "() of class " |
| 246 | | | . get_class($object) |
| 247 | | | ); |
| 248 | | | } |
| 249 | | | |
| 250 | | | $formField->setOptions($className::$getMethod($object)); |
| 251 | | | } |
| 252 | | | elseif ($annotation instanceof Annotations\Textarea) |
| 253 | | | { |
| 254 | | | $formField = new FormField(); |
| 255 | | | $formField->setCaption(($annotation->caption != null ? $annotation->caption : $fieldName)); |
| 256 | | | $formField->setName($prefix . $fieldName); |
| 257 | | | $formField->setType("textarea"); |
| 258 | | | $formField->setOrder($annotation->order); |
| 259 | | | $formField->setEnabled($annotation->enabled); |
| 260 | | | $formField->setValue($fieldValue); |
| 261 | | | $formField->setRequired($annotation->required); |
| 262 | | | } |
| 263 | | | elseif ($annotation instanceof Annotations\GeneratedValue) |
| 264 | | | { |
| 265 | | | $generatedValue = true; |
| 266 | | | } |
| 267 | | | elseif ($annotation instanceof Annotations\AssignToGroup) |
| 268 | | | { |
| 269 | | | if ($annotation->localGroupScope) |
| 270 | | | { |
| 271 | | | $selectedGroup = $form->getDefaultGroup() . "_" . $annotation->name; |
| 272 | | | } |
| 273 | | | else |
| 274 | | | { |
| 275 | | | $selectedGroup = $annotation->name; |
| 276 | | | } |
| 277 | | | } |
| 278 | | | elseif ($annotation instanceof Annotations\Relation) |
| 279 | | | { |
| 280 | | | $getMethod = "get" . ucFirst($fieldName); |
| 281 | | | |
| 282 | | | if (!method_exists($className, $getMethod)) |
| 283 | | | { |
| 284 | | | throw new FormBuilderException( |
| 285 | | | "FormBuilder: Missing get method " . |
| 286 | | | $getMethod . "() of class " |
| 287 | | | . $className |
| 288 | | | ); |
| 289 | | | } |
| 290 | | | |
| 291 | | | $child = null; |
| 292 | | | if (isset($object)) |
| 293 | | | $child = $object->$getMethod(); |
| 294 | | | |
| 295 | | | |
| 296 | | | $childs[] = array("source" => $annotation->source, "child" => $child, "prefix" => $prefix . $fieldName . "_", "assignToGroup" => $selectedGroup); |
| 297 | | | } |
| 298 | | | elseif ($annotation instanceof Annotations\Condition) |
| 299 | | | { |
| 300 | | | switch ($annotation->matchType) |
| 301 | | | { |
| 302 | | | case "EQ": |
| 303 | | | $form->getCriticalFields()->put($prefix . $annotation->sourceField, "criticalField"); |
| 304 | | | if ($fields[$annotation->sourceField] == $annotation->triggerValue) |
| 305 | | | { |
| 306 | | | if (!isset($formField)) |
| 307 | | | continue; |
| 308 | | | if ($annotation->action == "disable") |
| 309 | | | { |
| 310 | | | $formField->setEnabled(false); |
| 311 | | | } |
| 312 | | | else |
| 313 | | | { |
| 314 | | | $formField->setEnabled(true); |
| 315 | | | } |
| 316 | | | } |
| 317 | | | break; |
| 318 | | | case "IN": |
| 319 | | | $form->getCriticalFields()->put($prefix . $annotation->sourceField, "criticalField"); |
| 320 | | | if (in_array($fields[$annotation->sourceField], $annotation->triggerValue)) |
| 321 | | | { |
| 322 | | | if (!isset($formField)) |
| 323 | | | continue; |
| 324 | | | if ($annotation->action == "disable") |
| 325 | | | { |
| 326 | | | $formField->setEnabled(false); |
| 327 | | | } |
| 328 | | | else |
| 329 | | | { |
| 330 | | | $formField->setEnabled(true); |
| 331 | | | } |
| 332 | | | } |
| 333 | | | break; |
| 334 | | | } |
| 335 | | | } |
| 336 | | | } |
| 337 | | | |
| 338 | | | if ($generatedValue && isset($formField)) |
| 339 | | | { |
| 340 | | | if ($formField->getValue() == null) |
| 341 | | | $formField->setEnabled(false); |
| 342 | | | else |
| 343 | | | $formField->setReadOnly(true); |
| 344 | | | } |
| 345 | | | |
| 346 | | | if ($formField != null && $formField->getEnabled() == true) |
| 347 | | | { |
| 348 | | | if (!$groups->containsKey($selectedGroup)) |
| 349 | | | { |
| 350 | | | throw new FormBuilderException( |
| 351 | | | "FormBuilder: Missing group " . |
| 352 | | | $selectedGroup . " in FormField " . $formField->getName() |
| 353 | | | ); |
| 354 | | | } |
| 355 | | | if ($this->readOnlyAll) |
| 356 | | | $formField->setReadOnly(true); |
| 357 | | | if ($formField->getValue() != null && $groups->get($selectedGroup)->getDefaultOpen() == true) |
| 358 | | | $groups->get($selectedGroup)->setShow(true); |
| 359 | | | $groups->get($selectedGroup)->getFields()->add($formField); |
| 360 | | | } |
| 361 | | | } |
| 362 | | | |
| 363 | | | |
| 364 | | | foreach ($childs as $child) |
| 365 | | | { |
| 366 | | | $form->setDefaultGroup($child["assignToGroup"]); |
| 367 | | | $a = $this->buildForm($child["source"], $child["child"], $form, $child["prefix"]); |
| 368 | | | } |
| 369 | | | |
| 370 | | | return $form; |
| 371 | | | } |
| 372 | | | |
| 373 | | | public function getReadOnlyAll() |
| 374 | | | { |
| 375 | | | return $this->readOnlyAll; |
| 376 | | | } |
| 377 | | | |
| 378 | | | public function setReadOnlyAll($readOnlyAll) |
| 379 | | | { |
| 380 | | | $this->readOnlyAll = $readOnlyAll; |
| 381 | | | } |
| 382 | | | |
| 383 | | | public function getVersion() |
| 384 | | | { |
| 385 | | | return $this->version; |
| 386 | | | } |
| 387 | | | |
| 388 | | | public function setVersion($version) |
| 389 | | | { |
| 390 | | | $this->version = $version; |
| 391 | | | } |
| 392 | | | |
| 393 | | | } |