Beispiel #1
0
    public function main() {
        if (\helper\StringHelper::isNull($this->callback)) {
            throw new \Exception('Callback is required');
        }

        if (!isset($_POST['exec'])) {
            $curl = curl_init();

            curl_setopt($curl, CURLOPT_URL, 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, array('exec' => 'true'));
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

            $result = trim(curl_exec($curl));

            echo $result;

            curl_close($curl);

        } else {
            \core\Logger::info('start');

            \helper\ReflectionHelper::getMethodResultFromInstance($this, $this->callback);

            \core\Logger::info('stop');
        }
    }
Beispiel #2
0
    public function __toString() {
        $rc = new \ReflectionClass($this);
        $properties = $rc->getProperties(\ReflectionProperty::IS_PRIVATE);

        $string = array();

        foreach ($properties as $property) {
            $method = 'get'.ucfirst($property->getName());
            if (method_exists($this, $method)) {
                $result = \helper\ReflectionHelper::getMethodResultFromInstance($this, $method);
                $string[] = sprintf('%s = %s', $property->getName(), $result == null ? 'null' : '\''.$result.'\'');
            }
        }

        return sprintf('class %s { %s }', $rc->getName(), implode(', ', $string));
    }
Beispiel #3
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;
 }
    protected function prepareParameters($operationType, $listAnnotation, $classInstance) {
        $parameters = array();
        $propertyBehavior = array();

        switch ($operationType) {
            case self::OPERATION_INSERT:
                $propertyBehavior[] = Annotation::T_COLUMN;
                break;

            case self::OPERATION_UPDATE:
                $propertyBehavior[] = Annotation::T_COLUMN;
                $propertyBehavior[] = Annotation::T_ID;
                break;

            case self::OPERATION_SELECTBYID:
                $propertyBehavior[] = Annotation::T_ID;
                break;
        }

        if ($listAnnotation[Annotation::PROPERTIES]) {
            foreach ($listAnnotation[Annotation::PROPERTIES] as $property=>$annotations) {
                foreach ($annotations as $annotation) {
                    if (in_array($annotation[Annotation::BEHAVIOR], $propertyBehavior)) {
                        $value = ReflectionHelper::getMethodResultFromInstance($classInstance, 'get'.ucfirst($property));
                        $parameters[SQLHelper::MYSQL_PDO_PARAM_DELIMITER.$annotation[Annotation::VALUES][Annotation::O_NAME]] = $value;
                        if ($annotation[Annotation::BEHAVIOR] == Annotation::T_ID) {
                            $identity = SQLHelper::MYSQL_PDO_PARAM_DELIMITER.$annotation[Annotation::VALUES][Annotation::O_NAME];
                        }
                    }
                }
            }
        }

        return $parameters;
    }
Beispiel #5
0
 private function validate($entity, $identityProperty = null) {
     $messages = $this->generateNullableMessages($entity);
     $messages = $this->generateNeedsConfirmationMessages($entity, $messages);
     $messages = $this->generateUniqueMessages($entity, $messages, $identityProperty);
     foreach ($this->validations as $validation) {
         $messages = \helper\ReflectionHelper::getMethodResultFromName(get_class($this), $validation, array($entity, $messages));
     }
     if ($messages) {
         throw new ValidationException($messages);
     }
 }
    /**
     * 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;
    }
    protected function extractListViewFromEntityList($entity, $listViewColumns, $list, ListViewControl $listViewControl, $showPaginator = false) {
        $listAnnotations = Annotation::extractAnnotations($entity);

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

        } else if (!$listViewColumns) {
            throw new PresentationException('List of ListViewColumn is required');
        }

        // MAKE HEADER
        $header = array();
        foreach ($listViewColumns as $listViewColumn) {
            $headerLabel = '';
            foreach ($listAnnotations[Annotation::PROPERTIES] as $input=>$annotations) {
                if ($input == $listViewColumn->getName()) {
                    foreach ($annotations as $annotation) {
                        if ($annotation[Annotation::BEHAVIOR] == Annotation::T_LABEL) {
                            $headerLabel = $annotation[Annotation::VALUES][Annotation::O_VALUE];
                        } else if ($listViewColumn instanceof ListViewTextColumn) {
                            if ($annotation[Annotation::BEHAVIOR] == Annotation::T_INPUT) {
                                $listViewColumn->setType($annotation[Annotation::VALUES]);
                            }
                            if ($annotation[Annotation::BEHAVIOR] == Annotation::T_FORMAT) {
                                $listViewColumn->setFormat($annotation[Annotation::VALUES]);
                            }
                        }
                    }
                }
            }
            if ($headerLabel) {
                $linkOrderBy = null;
                if ($listViewColumn instanceof ListViewTextColumn) {
                    if ($listViewColumn->getOrderEnabled()) {
                        $linkOrderBy = $listViewControl->mountOrderByLink($listViewColumn->getName());
                    }
                }
                $header[] = array(HtmlHelper::ELEMENT_VALUE => $headerLabel, HtmlHelper::ELEMENT_LINK => $linkOrderBy);
            } else {
                $header[] = '';
            }
        }

        // MAKE CONTENT
        $content = array();
        foreach ($list as $item) {
            $row = array();
            foreach ($listViewColumns as $listViewColumn) {
                $type = null;
                $value = null;
                $link = null;
                $align = null;
                if ($listViewColumn instanceof ListViewTextColumn) {
                    $type = $listViewColumn->getType();
                    $format = $listViewColumn->getFormat();
                    $methodName = 'get'.ucfirst($listViewColumn->getName());
                    $value = ReflectionHelper::getMethodResultFromInstance($item, $methodName);
                    if (isset($format[Annotation::O_TYPE])) {
                        if ($format[Annotation::O_TYPE] == \helper\HtmlHelper::INPUT_FORMAT_DECIMAL) {
                            $value = \helper\StringHelper::convertToDecimal($value);
                            $align = 'right';
                        } else if ($format[Annotation::O_TYPE] == \helper\HtmlHelper::INPUT_FORMAT_DATE && isset($format[Annotation::O_PATTERN_PHP])) {
                            $value = \helper\DateHelper::getAsString($value, $format[Annotation::O_PATTERN_PHP]);
                            $align = 'center';
                        }
                        if (isset($format[Annotation::O_PREFIX])) {
                            $value = $format[Annotation::O_PREFIX].' '.$value;
                        }
                    }
                    $fetchEntity = null;
                    $fetchTypes = array(HtmlHelper::SELECT, HtmlHelper::RADIO);
                    if (in_array($type[Annotation::O_TYPE], $fetchTypes)) {
                        $fetch = preg_split('/\./', $type[Annotation::O_FETCH_ITEM]);
                        $fetchEntity = ReflectionHelper::getMethodResultFromName($fetch[0], $fetch[1], $value);
                        if ($fetchEntity) {
                            $value = ReflectionHelper::getMethodResultFromInstance($fetchEntity, 'get'.$type[Annotation::O_VALUE]);
                        } else {
                            $value = '-';
                        }
                    }
                    $callback = $listViewColumn->getCallback();
                    if ($callback) {
                        while (preg_match('/{(?P<element>.+?)}/', $callback, $matches)) {
                            $tempEntity = isset($fetchEntity) ? $fetchEntity : $item;
                            $param = ReflectionHelper::getMethodResultFromInstance($tempEntity, 'get'.ucfirst($matches['element']));
                            $callback = str_replace('{'.$matches['element'].'}', $param, $callback);
                        }
                        $link = $callback;
                    }
                } else if ($listViewColumn instanceof ListViewCommandColumn) {
                    $value = $listViewColumn->getName();
                    $callback = $listViewColumn->getCallback();
                    if ($callback) {
                        while (preg_match('/{(?P<element>.+?)}/', $callback, $matches)) {
                            $get = 'get'.ucfirst($matches['element']);
                            $param = ReflectionHelper::getMethodResultFromInstance($item, $get);
                            $callback = str_replace('{'.$matches['element'].'}', $param, $callback);
                        }
                        $link = $callback;
                    }
                }
                if ($link && strpos($link, 'http://') !== 0) {
                    $link = \App::getInstance()->getBasePrefix().$link;
                }
                $row[] = array(
                        HtmlHelper::ELEMENT_VALUE => $value,
                        HtmlHelper::ELEMENT_LINK => $link,
                        HtmlHelper::ELEMENT_TYPE => $type[Annotation::O_TYPE],
                        HtmlHelper::ELEMENT_ALIGN => $align
                );
            }
            $content[] = $row;
        }

        // MAKE PAGINATOR
        $paginator = null;
        if ($showPaginator) {
            $paginator = $listViewControl->makePagination();
        }

        return array(self::LIST_VIEW_HEADER => $header, self::LIST_VIEW_CONTENT => $content, self::LIST_VIEW_PAGINATOR => $paginator);
    }