Ejemplo n.º 1
0
 public function onAfterConstruct(DataModel &$model)
 {
     // This only applies to the front-end
     if (!$model->getContainer()->platform->isFrontend()) {
         return;
     }
     // Get the page parameters
     /** @var \JRegistry $params */
     $params = \JFactory::getApplication()->getPageParameters();
     // Extract the page parameter keys
     $asArray = $params->toArray();
     if (empty($asArray)) {
         // There are no keys; no point in going on.
         return;
     }
     $keys = array_keys($asArray);
     unset($asArray);
     // Loop all page parameter keys
     foreach ($keys as $key) {
         // This is the current model state
         $currentState = $model->getState($key);
         // This is the explicitly requested state in the input
         $explicitInput = $model->input->get($key, null, 'raw');
         // If the current state is empty and there's no explicit input we'll use the page parameters instead
         if (is_null($currentState) && is_null($explicitInput)) {
             $model->setState($key, $params->get($key));
         }
     }
 }
Ejemplo n.º 2
0
Archivo: Json.php Proyecto: Joal01/fof
 /**
  * Creates a \FOF30\Hal\Document using the provided data
  *
  * @param   mixed|array  $data   The data to put in the document
  * @param   DataModel    $model  The model of this view
  *
  * @return  \FOF30\Hal\Document  A HAL-enabled document
  */
 protected function _createDocumentWithHypermedia($data, $model = null)
 {
     // Create a new HAL document
     if (is_array($data)) {
         $count = count($data);
     } else {
         $count = null;
     }
     if ($count == 1) {
         reset($data);
         $document = new Document(end($data));
     } else {
         $document = new Document($data);
     }
     // Create a self link
     $uri = (string) \JUri::getInstance();
     $uri = $this->_removeURIBase($uri);
     $uri = \JRoute::_($uri);
     $document->addLink('self', new Link($uri));
     // Create relative links in a record list context
     if (is_array($data) && $model instanceof DataModel) {
         if (!isset($this->total)) {
             $this->total = $model->count();
         }
         if (!isset($this->limitStart)) {
             $this->limitStart = $model->getState('limitstart', 0);
         }
         if (!isset($this->limit)) {
             $this->limit = $model->getState('limit', 0);
         }
         $pagination = new \JPagination($this->total, $this->limitStart, $this->limit);
         if ($pagination->pagesTotal > 1) {
             // Try to guess URL parameters and create a prototype URL
             // NOTE: You are better off specialising this method
             $protoUri = $this->_getPrototypeURIForPagination();
             // The "first" link
             $uri = clone $protoUri;
             $uri->setVar('limitstart', 0);
             $uri = \JRoute::_($uri);
             $document->addLink('first', new Link($uri));
             // Do we need a "prev" link?
             if ($pagination->pagesCurrent > 1) {
                 $prevPage = $pagination->pagesCurrent - 1;
                 $limitstart = ($prevPage - 1) * $pagination->limit;
                 $uri = clone $protoUri;
                 $uri->setVar('limitstart', $limitstart);
                 $uri = \JRoute::_($uri);
                 $document->addLink('prev', new Link($uri));
             }
             // Do we need a "next" link?
             if ($pagination->pagesCurrent < $pagination->pagesTotal) {
                 $nextPage = $pagination->pagesCurrent + 1;
                 $limitstart = ($nextPage - 1) * $pagination->limit;
                 $uri = clone $protoUri;
                 $uri->setVar('limitstart', $limitstart);
                 $uri = \JRoute::_($uri);
                 $document->addLink('next', new Link($uri));
             }
             // The "last" link?
             $lastPage = $pagination->pagesTotal;
             $limitstart = ($lastPage - 1) * $pagination->limit;
             $uri = clone $protoUri;
             $uri->setVar('limitstart', $limitstart);
             $uri = \JRoute::_($uri);
             $document->addLink('last', new Link($uri));
         }
     }
     return $document;
 }
Ejemplo n.º 3
0
    /**
     * Renders a Form for a Browse view and returns the corresponding HTML
     *
     * @param   Form   &$form  The form to render
     * @param   DataModel  $model  The model providing our data
     *
     * @return  string    The HTML rendering of the form
     */
    public function renderFormBrowse(Form &$form, DataModel $model)
    {
        $html = '';
        \JHtml::_('behavior.multiselect');
        \JHtml::_('bootstrap.tooltip');
        \JHtml::_('dropdown.init');
        $view = $form->getView();
        $order = $view->escape($view->getLists()->order);
        $html .= <<<HTML
<script type="text/javascript">
\tJoomla.orderTable = function() {
\t\tvar table = document.getElementById("sortTable");
\t\tvar direction = document.getElementById("directionTable");
\t\tvar order = table.options[table.selectedIndex].value;
\t\tvar dirn = 'asc';

\t\tif (order != '{$order}')
\t\t{
\t\t\tdirn = 'asc';
\t\t}
\t\telse {
\t\t\tdirn = direction.options[direction.selectedIndex].value;
\t\t}

\t\tJoomla.tableOrdering(order, dirn);
\t};
</script>

HTML;
        // Getting all header row elements
        $headerFields = $form->getHeaderset();
        // Get form parameters
        $show_header = $form->getAttribute('show_header', 1);
        $show_filters = $form->getAttribute('show_filters', 1);
        $show_pagination = $form->getAttribute('show_pagination', 1);
        $norows_placeholder = $form->getAttribute('norows_placeholder', '');
        // Joomla! 3.x sidebar support
        $form_class = '';
        if ($show_filters) {
            \JHtmlSidebar::setAction("index.php?option=" . $this->container->componentName . "&view=" . $this->container->inflector->pluralize($form->getView()->getName()));
        }
        // Reorder the fields with ordering first
        $tmpFields = array();
        $i = 1;
        foreach ($headerFields as $tmpField) {
            if ($tmpField instanceof HeaderOrdering) {
                $tmpFields[0] = $tmpField;
            } else {
                $tmpFields[$i] = $tmpField;
            }
            $i++;
        }
        $headerFields = $tmpFields;
        ksort($headerFields, SORT_NUMERIC);
        // Pre-render the header and filter rows
        $header_html = '';
        $filter_html = '';
        $sortFields = array();
        if ($show_header || $show_filters) {
            foreach ($headerFields as $headerField) {
                $header = $headerField->header;
                $filter = $headerField->filter;
                $buttons = $headerField->buttons;
                $options = $headerField->options;
                $sortable = $headerField->sortable;
                $tdwidth = $headerField->tdwidth;
                // If it's a sortable field, add to the list of sortable fields
                if ($sortable) {
                    $sortFields[$headerField->name] = \JText::_($headerField->label);
                }
                // Get the table data width, if set
                if (!empty($tdwidth)) {
                    $tdwidth = 'width="' . $tdwidth . '"';
                } else {
                    $tdwidth = '';
                }
                if (!empty($header)) {
                    $header_html .= "\t\t\t\t\t<th {$tdwidth}>" . "\n";
                    $header_html .= "\t\t\t\t\t\t" . $header;
                    $header_html .= "\t\t\t\t\t</th>" . "\n";
                }
                if (!empty($filter)) {
                    $filter_html .= '<div class="filter-search btn-group pull-left">' . "\n";
                    $filter_html .= "\t" . '<label for="title" class="element-invisible">';
                    $filter_html .= \JText::_($headerField->label);
                    $filter_html .= "</label>\n";
                    $filter_html .= "\t{$filter}\n";
                    $filter_html .= "</div>\n";
                    if (!empty($buttons)) {
                        $filter_html .= '<div class="btn-group pull-left hidden-phone">' . "\n";
                        $filter_html .= "\t{$buttons}\n";
                        $filter_html .= '</div>' . "\n";
                    }
                } elseif (!empty($options)) {
                    $label = $headerField->label;
                    $filterName = $headerField->filterFieldName;
                    $filterSource = $headerField->filterSource;
                    \JHtmlSidebar::addFilter('- ' . \JText::_($label) . ' -', $filterName, \JHtml::_('select.options', $options, 'value', 'text', $model->getState($filterSource, ''), true));
                }
            }
        }
        // Start the form
        $filter_order = $form->getView()->getLists()->order;
        $filter_order_Dir = $form->getView()->getLists()->order_Dir;
        $actionUrl = $this->container->platform->isBackend() ? 'index.php' : \JUri::root() . 'index.php';
        if ($this->container->platform->isFrontend() && $this->container->input->getCmd('Itemid', 0) != 0) {
            $itemid = $this->container->input->getCmd('Itemid', 0);
            $uri = new \JUri($actionUrl);
            if ($itemid) {
                $uri->setVar('Itemid', $itemid);
            }
            $actionUrl = \JRoute::_($uri->toString());
        }
        $html .= '<form action="' . $actionUrl . '" method="post" name="adminForm" id="adminForm" ' . $form_class . '>' . "\n";
        // Get and output the sidebar, if present
        $sidebar = \JHtmlSidebar::render();
        if ($show_filters && !empty($sidebar)) {
            $html .= '<div id="j-sidebar-container" class="span2">' . "\n";
            $html .= "\t{$sidebar}\n";
            $html .= "</div>\n";
            $html .= '<div id="j-main-container" class="span10">' . "\n";
        } else {
            $html .= '<div id="j-main-container">' . "\n";
        }
        // Render header search fields, if the header is enabled
        $pagination = $form->getView()->getPagination();
        if ($show_header) {
            $html .= "\t" . '<div id="filter-bar" class="btn-toolbar">' . "\n";
            $html .= "{$filter_html}\n";
            if ($show_pagination) {
                // Render the pagination rows per page selection box, if the pagination is enabled
                $html .= "\t" . '<div class="btn-group pull-right hidden-phone">' . "\n";
                $html .= "\t\t" . '<label for="limit" class="element-invisible">' . \JText::_('JFIELD_PLG_SEARCH_SEARCHLIMIT_DESC') . '</label>' . "\n";
                $html .= "\t\t" . $pagination->getLimitBox() . "\n";
                $html .= "\t" . '</div>' . "\n";
            }
            if (!empty($sortFields)) {
                // Display the field sort order
                $asc_sel = $form->getView()->getLists()->order_Dir == 'asc' ? 'selected="selected"' : '';
                $desc_sel = $form->getView()->getLists()->order_Dir == 'desc' ? 'selected="selected"' : '';
                $html .= "\t" . '<div class="btn-group pull-right hidden-phone">' . "\n";
                $html .= "\t\t" . '<label for="directionTable" class="element-invisible">' . \JText::_('JFIELD_ORDERING_DESC') . '</label>' . "\n";
                $html .= "\t\t" . '<select name="directionTable" id="directionTable" class="input-medium" onchange="Joomla.orderTable()">' . "\n";
                $html .= "\t\t\t" . '<option value="">' . \JText::_('JFIELD_ORDERING_DESC') . '</option>' . "\n";
                $html .= "\t\t\t" . '<option value="asc" ' . $asc_sel . '>' . \JText::_('JGLOBAL_ORDER_ASCENDING') . '</option>' . "\n";
                $html .= "\t\t\t" . '<option value="desc" ' . $desc_sel . '>' . \JText::_('JGLOBAL_ORDER_DESCENDING') . '</option>' . "\n";
                $html .= "\t\t" . '</select>' . "\n";
                $html .= "\t" . '</div>' . "\n\n";
                // Display the sort fields
                $html .= "\t" . '<div class="btn-group pull-right">' . "\n";
                $html .= "\t\t" . '<label for="sortTable" class="element-invisible">' . \JText::_('JGLOBAL_SORT_BY') . '</label>' . "\n";
                $html .= "\t\t" . '<select name="sortTable" id="sortTable" class="input-medium" onchange="Joomla.orderTable()">' . "\n";
                $html .= "\t\t\t" . '<option value="">' . \JText::_('JGLOBAL_SORT_BY') . '</option>' . "\n";
                $html .= "\t\t\t" . \JHtml::_('select.options', $sortFields, 'value', 'text', $form->getView()->getLists()->order) . "\n";
                $html .= "\t\t" . '</select>' . "\n";
                $html .= "\t" . '</div>' . "\n";
            }
            $html .= "\t</div>\n\n";
            $html .= "\t" . '<div class="clearfix"> </div>' . "\n\n";
        }
        // Start the table output
        $html .= "\t\t" . '<table class="table table-striped" id="itemsList">' . "\n";
        // Open the table header region if required
        if ($show_header) {
            $html .= "\t\t\t<thead>" . "\n";
        }
        // Render the header row, if enabled
        if ($show_header) {
            $html .= "\t\t\t\t<tr>" . "\n";
            $html .= $header_html;
            $html .= "\t\t\t\t</tr>" . "\n";
        }
        // Close the table header region if required
        if ($show_header) {
            $html .= "\t\t\t</thead>" . "\n";
        }
        // Loop through rows and fields, or show placeholder for no rows
        $html .= "\t\t\t<tbody>" . "\n";
        $fields = $form->getFieldset('items');
        $num_columns = count($fields);
        $items = $model->get();
        if ($count = count($items)) {
            $m = 1;
            foreach ($items as $i => $item) {
                $rowHtml = '';
                $form->bind($item);
                $m = 1 - $m;
                $rowClass = 'row' . $m;
                $fields = $form->getFieldset('items');
                // Reorder the fields to have ordering first
                $tmpFields = array();
                $j = 1;
                foreach ($fields as $tmpField) {
                    if ($tmpField instanceof FieldOrdering) {
                        $tmpFields[0] = $tmpField;
                    } else {
                        $tmpFields[$j] = $tmpField;
                    }
                    $j++;
                }
                $fields = $tmpFields;
                ksort($fields, SORT_NUMERIC);
                /** @var FieldInterface $field */
                foreach ($fields as $field) {
                    $field->rowid = $i;
                    $field->item = $item;
                    $labelClass = $field->labelclass;
                    $class = $labelClass ? 'class ="' . $labelClass . '"' : '';
                    if (!method_exists($field, 'getRepeatable')) {
                        throw new \Exception('getRepeatable not found in class ' . get_class($field));
                    }
                    // Let the fields change the row (tr element) class
                    if (method_exists($field, 'getRepeatableRowClass')) {
                        $rowClass = $field->getRepeatableRowClass($rowClass);
                    }
                    $rowHtml .= "\t\t\t\t\t<td {$class}>" . $field->getRepeatable() . '</td>' . "\n";
                }
                $html .= "\t\t\t\t<tr class=\"{$rowClass}\">\n" . $rowHtml . "\t\t\t\t</tr>\n";
            }
        } elseif ($norows_placeholder) {
            $html .= "\t\t\t\t<tr><td colspan=\"{$num_columns}\">";
            $html .= \JText::_($norows_placeholder);
            $html .= "</td></tr>\n";
        }
        $html .= "\t\t\t</tbody>" . "\n";
        // End the table output
        $html .= "\t\t" . '</table>' . "\n";
        // Render the pagination bar, if enabled, on J! 3.0+
        $html .= $pagination->getListFooter();
        // Close the wrapper element div on Joomla! 3.0+
        $html .= "</div>\n";
        $html .= "\t" . '<input type="hidden" name="option" value="' . $this->container->componentName . '" />' . "\n";
        $html .= "\t" . '<input type="hidden" name="view" value="' . $this->container->inflector->pluralize($form->getView()->getName()) . '" />' . "\n";
        $html .= "\t" . '<input type="hidden" name="task" value="' . $form->getView()->getTask() . '" />' . "\n";
        $html .= "\t" . '<input type="hidden" name="layout" value="' . $form->getView()->getLayout() . '" />' . "\n";
        // The id field is required in Joomla! 3 front-end to prevent the pagination limit box from screwing it up.
        if ($this->container->platform->isFrontend()) {
            $html .= "\t" . '<input type="hidden" name="id" value="" />' . "\n";
        }
        $html .= "\t" . '<input type="hidden" name="boxchecked" value="" />' . "\n";
        $html .= "\t" . '<input type="hidden" name="hidemainmenu" value="" />' . "\n";
        $html .= "\t" . '<input type="hidden" name="filter_order" value="' . $filter_order . '" />' . "\n";
        $html .= "\t" . '<input type="hidden" name="filter_order_Dir" value="' . $filter_order_Dir . '" />' . "\n";
        $html .= "\t" . '<input type="hidden" name="' . \JFactory::getSession()->getFormToken() . '" value="1" />' . "\n";
        // End the form
        $html .= '</form>' . "\n";
        return $html;
    }