Exemplo n.º 1
0
    /**
     * Generate html form based on Entity @Input annotated params
     * @param \entity\BaseEntity $entity
     * @param $customKeys
     * @throws AnnotationException
     * @return
     */
    protected function extractFormAnnotationsFromEntity(\entity\BaseEntity $entity, $customKeys = null, $showNeedsConfirmation = true) {
        $className = get_class($entity);
        $listAnnotations = Annotation::extractAnnotations($className);

        if (!$listAnnotations) {
            throw new AnnotationException(\core\Messages::CLASS_HAS_NO_ANNOTATIONS);
        }

        $elements = array();

        $listFormBehavior = array(Annotation::T_INPUT);

        foreach ($listAnnotations[Annotation::PROPERTIES] as $key=>$propertiesAnnotations) {
            if ($customKeys && is_array($customKeys) && !in_array($key, $customKeys)) {
                continue;
            }
            foreach ($propertiesAnnotations as $annotation) {
                if (in_array($annotation[Annotation::BEHAVIOR], $listFormBehavior)) {
                    $label = '';
                    $format = '';
                    foreach ($propertiesAnnotations as $subAnnotation) {
                        if ($subAnnotation[Annotation::BEHAVIOR] == Annotation::T_LABEL) {
                            $label = $subAnnotation[Annotation::VALUES][Annotation::O_VALUE];
                        } else if ($subAnnotation[Annotation::BEHAVIOR] == Annotation::T_FORMAT) {
                            $format = $subAnnotation[Annotation::VALUES];
                        }
                    }
                    $value = \helper\ReflectionHelper::getMethodResultFromInstance($entity, 'get'.ucfirst($key));
                    $elements[] = array(
                            Annotation::O_REFERS_TO => $key,
                            Annotation::O_VALUE => $value,
                            Annotation::T_LABEL => $label,
                            Annotation::VALUES => $annotation[Annotation::VALUES],
                            Annotation::T_FORMAT => $format
                    );
                    if ($showNeedsConfirmation) {
                        foreach ($propertiesAnnotations as $subAnnotation) {
                            if ($subAnnotation[Annotation::BEHAVIOR] == Annotation::T_NEEDS_CONFIRMATION) {
                                $tmpKey = 'confirm'.$key;
                                $values = $annotation[Annotation::VALUES];
                                $values[Annotation::T_NEEDS_CONFIRMATION] = $tmpKey;
                                $elements[] = array(
                                        Annotation::O_REFERS_TO => 'confirm-'.$key,
                                        Annotation::O_VALUE => isset($entity->$tmpKey) ? $entity->$tmpKey : null,
                                        Annotation::T_LABEL => $subAnnotation[Annotation::VALUES][Annotation::O_VALUE],
                                        Annotation::VALUES => $values
                                );
                                JavascriptManager::getInstance()->putCommand(JavascriptHelper::createPreventCutCopyPaste(strtolower(get_class($entity)).'-'.$key));
                                JavascriptManager::getInstance()->putCommand(JavascriptHelper::createPreventCutCopyPaste(strtolower(get_class($entity)).'-confirm-'.$key));
                                break;
                            }
                        }
                    }
                }
            }
        }

        return $elements;
    }