Example #1
0
 protected function generateUniqueMessages($entity, $messages = null, $identityProperty = null) {
     $listAnnotations = Annotation::extractAnnotations($entity);
     if ($messages == null) {
         $messages = array();
     }
     if ($listAnnotations[Annotation::PROPERTIES]) {
         $listBusinessRules = array(Annotation::T_UNIQUE);
         foreach ($listAnnotations[Annotation::PROPERTIES] as $property=>$annotations) {
             foreach ($annotations as $annotation) {
                 if (in_array($annotation[Annotation::BEHAVIOR], $listBusinessRules)) {
                     $value = ReflectionHelper::getMethodResultFromInstance($entity, 'get'.ucfirst($property));
                     $check = explode('.', $annotation[Annotation::VALUES][Annotation::O_CHECK]);
                     $result = \helper\ReflectionHelper::getMethodResultFromName($check[0], $check[1], $value);
                     if (isset($result) && count($result) > 0) {
                         $showMessage = true;
                         if (\helper\StringHelper::isNotNull($identityProperty)) {
                             $tmpProp = 'get'.ucfirst($identityProperty);
                             $simpleResult = null;
                             if (is_array($result)) {
                                 $simpleResult = $result[0];
                             } else {
                                 $simpleResult = $result;
                             }
                             if (\helper\ReflectionHelper::getMethodResultFromInstance($entity, $tmpProp) == \helper\ReflectionHelper::getMethodResultFromInstance($simpleResult, $tmpProp)) {
                                 $showMessage = false;
                             }
                         }
                         if ($showMessage) {
                             $messages[$property] = $annotation[Annotation::VALUES][Annotation::O_MESSAGE];
                         }
                     }
                 }
             }
         }
     }
     if (!count($messages)) {
         return null;
     }
     return $messages;
 }
Example #2
0
    public static function createInput($type, $id, $name, $value, $maxLength = null, $callback = null, $style = null) {
        $params = array();
        if (strlen($value) > 0) {
            if ($type == self::INPUT_TYPE_CHECKBOX) {
                //$hidden = self::createInput(self::INPUT_TYPE_HIDDEN, $id, $name, 0);
                $params[] = sprintf(self::$inputValue, 1);
                if ($value) {
                    $params[] = self::$inputChecked;
                }
            } else {
                $params[] = sprintf(self::$inputValue, $value);
            }

        } else if ($type == self::INPUT_TYPE_CHECKBOX) {
            $params[] = sprintf(self::$inputValue, 1);
        }
        if ($maxLength) {
            $params[] = sprintf(self::$inputMaxLength, $maxLength);
        }
        if (\helper\StringHelper::isNotNull($style)) {
            $params[] = sprintf(self::$inputStyleClass, $style);
        } else if ($type == self::INPUT_TYPE_TEXT) {
            $params[] = sprintf(self::$inputStyleClass, 'formText');
        }
        if ($callback) {
            $params[] = sprintf('onclick="%1$s"', $callback);
        }
        $input = sprintf(self::$input, $type, $id, $name, implode(' ', $params));
        if (!isset($hidden) || !$hidden) {
            return $input;
        } else {
            return $hidden.$input;
        }
    }
Example #3
0
    public function init() {
        ob_start();
        try {
            SessionManager::getInstance()->init();

            $mappingFound = false;

            $listRequestURI = array();
            $listRequestURI[] = $_SERVER['REQUEST_URI'];
            if (strpos($_SERVER['REQUEST_URI'], '?')) {
                $urlArray = preg_split('/\?/', $_SERVER['REQUEST_URI']);
                $listRequestURI[] = $urlArray[0];
            }
            if (preg_match('/(.)*\/(\w)+$/', $_SERVER['REQUEST_URI'])) {
                $listRequestURI[] = $_SERVER['REQUEST_URI'].'/';
            }

            $presentationClasses = \helper\FolderHelper::listFilesByType(PAPRIKA_CUSTOM_PATH.'/'.Config::CUSTOM_CODE_PRESENTATION, '.php', true);

            foreach ($listRequestURI as $requestURI) {
                $basePrefix = preg_replace('/\/'.Config::APPLICATION_FILE_NAME.'$/', '', $_SERVER['SCRIPT_NAME']);
                if ($basePrefix) {
                    $this->setBasePrefix($basePrefix);
                    $requestURI = substr($requestURI, strlen($this->getBasePrefix()), strlen($requestURI));
                }

                $requestURI = strtolower($requestURI);

                $mappingFound = $this->findMapping($presentationClasses, $requestURI);

                if ($mappingFound) {
                    break;
                }
            }

            if (!$mappingFound) {
                if (\helper\StringHelper::isNotNull(Config::MAPPING_NOT_FOUNDED)) {

                    $aux = explode('.', Config::MAPPING_NOT_FOUNDED);

                    $annotation = array();
                    $annotation[Annotation::VALUES][Annotation::O_METHOD] = $aux[1];

                    $reflectionClass = new \ReflectionClass($aux[0]);
                    $classInstance = $reflectionClass->newInstance();

                    $this->invokeMappingClass($classInstance, $annotation, $this->getQueryString(), $requestURI);

                } else {
                    throw new \core\exception\MappingNotFoundException($requestURI);
                }
            }

        } catch (\Exception $e) {
            $this->handleException($e);
        }
        ob_flush();
    }
Example #4
0
    /**
     * Generate HTML form
     * @param $className
     * @param $formAnnotations
     * @param $operationName
     * @param $formAction
     * @param $disabled
     * @param $submitCallback
     */
    protected function generateHtmlForm($className, $formAnnotations, $operationName, $formAction, $disabled = false, $submitCallback = null, $isJson = false) {
        $htmlForm = array();
        $className = strtolower($className);

        // FORM_ELEMENTS
        if ($formAnnotations) {
            $formElements = array();
            foreach ($formAnnotations as $formAnnotation) {
                $inputId = $className.'-'.$formAnnotation[Annotation::O_REFERS_TO];
                $label = '';
                $input = '';
                if ($formAnnotation[Annotation::VALUES][Annotation::O_TYPE] == HtmlHelper::SELECT) {
                    $fetchList = explode('.', $formAnnotation[Annotation::VALUES][Annotation::O_FETCH_LIST]);
                    $list = ReflectionHelper::getMethodResultFromName($fetchList[0], $fetchList[1]);
                    $optionList = array();
                    if (count($list) > 1) {
                        $optionList = array(0=>'Selecione');
                    }
                    foreach ($list as $item) {
                        $key = ReflectionHelper::getMethodResultFromInstance($item, 'get'.ucfirst($formAnnotation[Annotation::VALUES][Annotation::O_ID]));
                        $value = ReflectionHelper::getMethodResultFromInstance($item, 'get'.ucfirst($formAnnotation[Annotation::VALUES][Annotation::O_VALUE]));
                        $optionList[$key] = $value;
                    }
                    if (!$disabled) {
                        $input = HtmlHelper::createSelect($inputId, $inputId, $optionList, $formAnnotation[Annotation::O_VALUE]);
                    } else {
                        $input = HtmlHelper::createSelectDisabled($inputId, $inputId, $optionList, $formAnnotation[Annotation::O_VALUE]);
                    }

                } else if ($formAnnotation[Annotation::VALUES][Annotation::O_TYPE] == HtmlHelper::RADIO) {
                    $fetchList = explode('.', $formAnnotation[Annotation::VALUES][Annotation::O_FETCH_LIST]);
                    $list = ReflectionHelper::getMethodResultFromName($fetchList[0], $fetchList[1]);
                    $optionList = array();
                    foreach ($list as $item) {
                        $key = ReflectionHelper::getMethodResultFromInstance($item, 'get'.ucfirst($formAnnotation[Annotation::VALUES][Annotation::O_ID]));
                        $value = ReflectionHelper::getMethodResultFromInstance($item, 'get'.ucfirst($formAnnotation[Annotation::VALUES][Annotation::O_VALUE]));
                        $optionList[$key] = $value;
                    }
                    if (!$disabled) {
                        $input = HtmlHelper::createRadioList($inputId, $inputId, $optionList, $formAnnotation[Annotation::O_VALUE]);
                    } else {
                        p($optionList);
                    }

                } else if ($formAnnotation[Annotation::VALUES][Annotation::O_TYPE] == HtmlHelper::TEXTAREA) {
                    $value = $formAnnotation[Annotation::O_VALUE];
                    $input = HtmlHelper::createTextArea(
                            $inputId,
                            $inputId,
                            $formAnnotation[Annotation::VALUES][Annotation::O_COLS],
                            $formAnnotation[Annotation::VALUES][Annotation::O_ROWS],
                            $value
                    );

                } else {
                    $value = $formAnnotation[Annotation::O_VALUE];
                    $style = '';
                    if (isset($formAnnotation[Annotation::T_FORMAT]) && isset($formAnnotation[Annotation::T_FORMAT][Annotation::O_TYPE])) {
                        if ($formAnnotation[Annotation::T_FORMAT][Annotation::O_TYPE] == HtmlHelper::INPUT_FORMAT_DATE) {
                            $style = 'formDate';
                            $pattern = '';
                            if (isset($formAnnotation[Annotation::T_FORMAT][Annotation::O_PATTERN_JS])) {
                                $pattern = $formAnnotation[Annotation::T_FORMAT][Annotation::O_PATTERN_JS];
                            }
                            if (isset($formAnnotation[Annotation::T_FORMAT][Annotation::O_PATTERN_PHP]) && \helper\StringHelper::isNotNull($value)) {
                                $value = DateHelper::getAsString($value, $formAnnotation[Annotation::T_FORMAT][Annotation::O_PATTERN_PHP]);
                            }
                            JavascriptManager::getInstance()->putCommand(JavascriptHelper::createDatePicker($inputId, $pattern));
                        } else if ($formAnnotation[Annotation::T_FORMAT][Annotation::O_TYPE] == HtmlHelper::INPUT_FORMAT_NUMBER) {
                            $style = 'formNumber';
                        }
                        if (isset($formAnnotation[Annotation::T_FORMAT][Annotation::O_MASK])) {
                            if ($formAnnotation[Annotation::T_FORMAT][Annotation::O_TYPE] == HtmlHelper::INPUT_FORMAT_DECIMAL) {
                                $style = 'formDecimal';
                                $mask = strrev($formAnnotation[Annotation::T_FORMAT][Annotation::O_MASK]);
                                $mask = preg_replace('/[.]/', ',', $mask, 1);
                                JavascriptManager::getInstance()->putCommand(JavascriptHelper::createMask($inputId, $mask, true));
                            } else {
                                JavascriptManager::getInstance()->putCommand(JavascriptHelper::createMask($inputId, $formAnnotation[Annotation::T_FORMAT][Annotation::O_MASK]));
                            }
                        }
                    }
                    if (!$disabled) {
                        $input = HtmlHelper::createInput(
                                $formAnnotation[Annotation::VALUES][Annotation::O_TYPE],
                                $inputId,
                                $inputId,
                                $value,
                                isset($formAnnotation[Annotation::VALUES][Annotation::O_MAX_LENGTH]) ? $formAnnotation[Annotation::VALUES][Annotation::O_MAX_LENGTH] : null,
                                null,
                                $style
                        );
                    } else {
                        $input = HtmlHelper::createInputDisabled(
                                $formAnnotation[Annotation::VALUES][Annotation::O_TYPE],
                                $value
                        );
                    }
                }
                if ($formAnnotation[Annotation::T_LABEL]) {
                    $label = HtmlHelper::createLabel(
                            $formAnnotation[Annotation::T_LABEL],
                            $inputId
                    );
                }
                if ($formAnnotation[Annotation::VALUES][Annotation::O_TYPE] == HtmlHelper::INPUT_TYPE_CHECKBOX) {
                    $formElements[$inputId] = array(self::FORM_INPUT => $label, self::FORM_LABEL => $input);

                } else {
                    $formElements[$inputId] = array(self::FORM_INPUT => $input, self::FORM_LABEL => $label);
                }
            }
            $htmlForm[self::FORM_ELEMENTS] = $formElements;
        }

        // FORM_PARAMS
        $formId = 'form-'.$className.'-'.strtolower($operationName);
        $htmlForm[self::FORM_PARAMS] = HtmlHelper::createFormParameters($formId, $formAction, 'POST', $submitCallback);

        // FORM SUBMIT
        $submitId = $formId.'-'.HtmlHelper::INPUT_TYPE_SUBMIT;
        if (!$isJson) {
            $htmlForm[self::FORM_SUBMIT] = HtmlHelper::createInput(HtmlHelper::INPUT_TYPE_SUBMIT, $submitId, $submitId, ucfirst($operationName));
        } else {
            $htmlForm[self::FORM_SUBMIT] = HtmlHelper::createInput(HtmlHelper::INPUT_TYPE_BUTTON, $submitId, $submitId, ucfirst($operationName));
            JavascriptManager::getInstance()->putCommand(JavascriptHelper::bindAjaxPostInClick($submitId, $formId));
        }

        // FORM CANCEL
        $cancelOperation = 'cancelar';
        $cancelId = $formId.'-'.$cancelOperation;
        $cancelCallback = JavascriptHelper::createLocationHrefChange(\App::getInstance()->getURIPrevious());
        $htmlForm[self::FORM_CANCEL] = HtmlHelper::createInput(HtmlHelper::INPUT_TYPE_BUTTON, $cancelId, $cancelId, ucfirst($cancelOperation), null, $cancelCallback);

        return $htmlForm;
    }