Ejemplo n.º 1
0
    public function getList(HttpRequest $httpRequest, $paginationMaxRows = null) {
        $httpResponse = new HttpResponse();
        try {
            if (!isset($paginationMaxRows)) {
                $paginationMaxRows = \core\Config::PAGINANTION_MAX_ROWS;
            }

            $listViewControl = new ListViewControl($httpRequest->getQueryString(), $this->business->getListCount(), $paginationMaxRows);

            $list = $this->business->getList(
                    $listViewControl->getOffset(),
                    $paginationMaxRows,
                    $listViewControl->getOrderBy(),
                    $listViewControl->getOrderDirection()
            );

            $content = null;

            if ($httpRequest->getContentType() == Config::CONTENT_TYPE_TEXT) {
                if ($list) {
                    $listView = $this->extractListViewFromEntityList($this->entity, $this->listViewColumns, $list, $listViewControl, true);
                    $content[self::LIST_VIEW] = HtmlHelper::createTable($listView[self::LIST_VIEW_HEADER], $listView[self::LIST_VIEW_CONTENT], $listView[self::LIST_VIEW_PAGINATOR]);
                } else {
                    \App::getInstance()->setOutputMessage(Messages::NO_RECORDS_FOUND);
                    $content[self::LIST_VIEW] = null;
                }

            } else if ($httpRequest->getContentType() == Config::CONTENT_TYPE_JSON) {
                if ($list) {
                    $content = $list;
                } else {
                    $httpResponse->setSuccess(false);
                    $content = Messages::NO_RECORDS_FOUND;
                }
            }

            $httpResponse->setContent($content);

            return $httpResponse;

        } catch (\Exception $e) {
            throw new PresentationException($e);
        }
    }
Ejemplo n.º 2
0
    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);
    }