Example #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');
        }
    }
Example #2
0
 /**
  * Imports a class by name or path.
  *
  * The full path is either provided in the $path argument or
  * constructued using the $path and $className.
  *
  * @param string $path      The path to the class.
  * @param string $className The name of the class.
  *
  * @return void
  */
 public static function importClass($path, $className = null)
 {
     if (StringHelper::endsWith($path, ".php")) {
         require_once $path;
     } else {
         require_once $path . "/" . $className . ".php";
     }
 }
Example #3
0
 public function destroy($key) {
     if (\helper\StringHelper::isNull($key)) {
         throw new \Exception('$key is null');
     }
     if (isset($_SESSION[$key])) {
         unset($_SESSION[$key]);
     }
 }
Example #4
0
 /**
  * Finds the class name of a file.
  *
  * @param string $filename The filename of PHP file.
  *
  * @return string the class name
  * @throws \Exception If the class could not be found.
  */
 private static function findClass($filename)
 {
     $classFileName = basename($filename, ".php");
     foreach (get_declared_classes() as $declaredClass) {
         if (StringHelper::endsWith($declaredClass, $classFileName)) {
             return $declaredClass;
         }
     }
     throw new \Exception($classFileName . " not found. Maybe you are missing a require() statement.");
 }
Example #5
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 #6
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 #7
0
 private function controlAccess($routeRole) {
     $sessionRoles = SessionManager::getInstance()->getValue(SessionManager::USER_ROLE);
     $routeRoles = explode('|', $routeRole);
     $allowed = false;
     if (isset($sessionRoles) && isset($routeRoles)) {
         $sessionRoles = explode('|', $sessionRoles);
         foreach ($routeRoles as $role) {
             if (in_array($role, $sessionRoles)) {
                 $allowed = true;
                 break;
             }
         }
     }
     if (!$allowed && !\helper\StringHelper::isNull(Config::LOGIN)) {
         $aux = explode('.', Config::LOGIN);
         $this->redirect($this->findRoute($aux[0], $aux[1]), \core\Messages::RESTRICTED_AREA);
         exit;
     }
 }
Example #8
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;
    }
    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);
    }