Pop\Data\Type\Html::encode PHP Метод

encode() публичный статический Метод

Encode the data into its native format.
public static encode ( mixed $data, array $options = null, integer $perPage, integer $range = 10, integer $total = null ) : string
$data mixed
$options array
$perPage integer
$range integer
$total integer
Результат string
    public static function encode($data, array $options = null, $perPage = 0, $range = 10, $total = null)
    {
        $output = '';
        $header = '';
        $row = '';
        $footer = '';
        $sep = isset($options['separator']) ? $options['separator'] : ' | ';
        $indent = isset($options['indent']) ? $options['indent'] : '    ';
        $date = isset($options['date']) ? $options['date'] : 'M j, Y';
        $exclude = isset($options['exclude']) ? $options['exclude'] : array();
        $process = null;
        $submit = null;
        if (!is_array($exclude)) {
            $exclude = array($exclude);
        }
        if (isset($options['form']) && is_array($options['form'])) {
            $process = isset($options['form']['process']) ? $options['form']['process'] : null;
            $submit = isset($options['form']['submit']) ? $options['form']['submit'] : null;
            $output .= $indent . '<form';
            foreach ($options['form'] as $attrib => $value) {
                if ($attrib != 'process' && $attrib != 'submit') {
                    $output .= ' ' . $attrib . '="' . $value . '"';
                }
            }
            $output .= '>' . PHP_EOL;
        }
        $output .= $indent . '    <table';
        if (isset($options['table']) && is_array($options['table'])) {
            foreach ($options['table'] as $attrib => $value) {
                if ($attrib != 'headers') {
                    $output .= ' ' . $attrib . '="' . $value . '"';
                }
            }
        }
        $output .= '>' . PHP_EOL;
        // Initialize and clean the header fields.
        foreach ($data as $ary) {
            $tempAry = array_keys((array) $ary);
        }
        $headerAry = array();
        $headerKeysAry = array();
        foreach ($tempAry as $value) {
            if (!in_array($value, $exclude)) {
                $headerKeysAry[] = $value;
                if (isset($options['table']) && isset($options['table']['headers']) && is_array($options['table']['headers']) && array_key_exists($value, $options['table']['headers'])) {
                    $headerAry[] = $options['table']['headers'][$value];
                } else {
                    $headerAry[] = ucwords(str_replace('_', ' ', (string) $value));
                }
            }
        }
        if (isset($options['form'])) {
            if (isset($options['table']) && isset($options['table']['headers']) && is_array($options['table']['headers']) && isset($options['table']['headers']['process'])) {
                $headerAry[] = $options['table']['headers']['process'];
                $headerKeysAry[] = 'process';
            } else {
                $headerAry[] = null !== $submit && is_array($submit) && isset($submit['value']) ? $submit['value'] : '&nbsp;';
                $headerKeysAry[] = 'process';
            }
        }
        // Set header output.
        $output .= $indent . '        <tr><th class="first-th">' . implode('</th><th>', $headerAry) . '</th></tr>' . PHP_EOL;
        $pos = strrpos($output, '<th') + 3;
        $output = substr($output, 0, $pos) . ' class="last-th"' . substr($output, $pos);
        // Set header and row templates
        $header = $indent . '<div class="page-links">[{page_links}]</div>' . PHP_EOL . $output;
        $row = $indent . '        <tr><td class="first-td">[{' . implode('}]</td><td>[{', $headerKeysAry) . '}]</td></tr>' . PHP_EOL;
        $pos = strrpos($row, '<td') + 3;
        $row = substr($row, 0, $pos) . ' class="last-td"' . substr($row, $pos);
        $rowValuesAry = array();
        // Initialize and clean the field values.
        $i = 1;
        foreach ($data as $value) {
            $rowAry = array();
            foreach ($value as $key => $val) {
                if (!in_array($key, $exclude)) {
                    if (strtotime((string) $val) !== false && (stripos($key, 'date') !== false || stripos($key, 'time') !== false)) {
                        $v = date($date, strtotime($val)) != '12/31/1969' ? date($date, strtotime((string) $val)) : '';
                    } else {
                        $v = (string) $val;
                    }
                    if (isset($options[$key])) {
                        $tmpl = $options[$key];
                        foreach ($value as $ky => $vl) {
                            if (strtotime((string) $vl) !== false && (stripos($key, 'date') !== false || stripos($key, 'time') !== false)) {
                                $vl = date($date, strtotime($vl)) != '12/31/1969' ? date($date, strtotime((string) $vl)) : '';
                            } else {
                                $vl = (string) $vl;
                            }
                            $tmpl = str_replace('[{' . $ky . '}]', $vl, $tmpl);
                        }
                        $v = $tmpl;
                    }
                    $rowAry[] = $v;
                }
            }
            if (isset($options['form'])) {
                if (null !== $process) {
                    $tmpl = str_replace('[{i}]', $i, $process);
                    foreach ($value as $ky => $vl) {
                        $tmpl = str_replace('[{' . $ky . '}]', $vl, $tmpl);
                    }
                    if (isset($exclude['process'])) {
                        $keys = array_keys($exclude['process']);
                        if (isset($keys[0]) && $exclude['process'][$keys[0]] == $value[$keys[0]]) {
                            $tmpl = '&nbsp;';
                        }
                    }
                } else {
                    $tmpl = '&nbsp;';
                }
                $rowAry[] = $tmpl;
            }
            $i++;
            foreach ($rowAry as $k => $r) {
                $rowAry[$headerKeysAry[$k]] = $r;
                unset($rowAry[$k]);
            }
            $rowValuesAry[] = $rowAry;
            // Set field output.
            $output .= $indent . '        <tr><td class="first-td">' . implode('</td><td>', $rowAry) . '</td></tr>' . PHP_EOL;
            $pos = strrpos($output, '<td') + 3;
            $output = substr($output, 0, $pos) . ' class="last-td"' . substr($output, $pos);
        }
        if (isset($options['form'])) {
            if (null !== $submit && is_array($submit)) {
                $submitBtn = '<input type="submit" name="submit"';
                if (!isset($submit['id'])) {
                    $submitBtn .= ' id="submit"';
                }
                foreach ($submit as $attrib => $value) {
                    $submitBtn .= ' ' . $attrib . '="' . $value . '"';
                }
                $submitBtn .= ' />';
            } else {
                $submitBtn = '<input type="submit" name="submit" id="submit" value="Submit" />';
            }
            $output .= $indent . '        <tr class="table-bottom-row"><td colspan="' . count($headerAry) . '" class="table-bottom-row">' . $submitBtn . '</td></tr>' . PHP_EOL;
            $output .= $indent . '    </table>' . PHP_EOL;
            $output .= $indent . '</form>' . PHP_EOL;
            $footer = $indent . '        <tr class="table-bottom-row"><td colspan="' . count($headerAry) . '" class="table-bottom-row">' . $submitBtn . '<div class="page-links">[{page_links}]</div></td></tr>' . PHP_EOL;
            $footer .= $indent . '    </table>' . PHP_EOL;
            $footer .= $indent . '</form>' . PHP_EOL;
        } else {
            $output .= $indent . '    </table>' . PHP_EOL;
            $footer = $indent . '    </table>' . PHP_EOL;
            $footer .= $indent . '<div class="page-links">[{page_links}]</div>' . PHP_EOL;
        }
        if ($perPage > 0) {
            $pages = new Paginator($rowValuesAry, $perPage, $range, $total);
            $pages->setHeader($header)->setRowTemplate($row)->setFooter($footer)->setSeparator($sep);
            $output = (string) $pages;
        }
        return $output;
    }

Usage Example

Пример #1
0
 /**
  * Get all sites method
  *
  * @param  string $sort
  * @param  string $page
  * @return void
  */
 public function getAll($sort = null, $page = null)
 {
     $order = $this->getSortOrder($sort, $page);
     $sites = Table\Sites::findAll($order['field'] . ' ' . $order['order']);
     if ($this->data['acl']->isAuth('Phire\\Controller\\Config\\SitesController', 'remove')) {
         $removeCheckbox = '<input type="checkbox" name="remove_sites[]" id="remove_sites[{i}]" value="[{id}]" />';
         $removeCheckAll = '<input type="checkbox" id="checkall" name="checkall" value="remove_sites" />';
         $submit = array('class' => 'remove-btn', 'value' => $this->i18n->__('Remove'));
     } else {
         $removeCheckbox = '&nbsp;';
         $removeCheckAll = '&nbsp;';
         $submit = array('class' => 'remove-btn', 'value' => $this->i18n->__('Remove'), 'style' => 'display: none;');
     }
     $options = array('form' => array('id' => 'sites-remove-form', 'action' => BASE_PATH . APP_URI . '/config/sites/remove', 'method' => 'post', 'process' => $removeCheckbox, 'submit' => $submit), 'table' => array('headers' => array('id' => '<a href="' . BASE_PATH . APP_URI . '/sites?sort=id">#</a>', 'edit' => '<span style="display: block; margin: 0 auto; width: 100%; text-align: center;">' . $this->i18n->__('Edit') . '</span>', 'domain' => '<a href="' . BASE_PATH . APP_URI . '/sites?sort=domain">' . $this->i18n->__('Domain') . '</a>', 'document_root' => '<a href="' . BASE_PATH . APP_URI . '/sites?sort=document_root">' . $this->i18n->__('Document Root') . '</a>', 'title' => '<a href="' . BASE_PATH . APP_URI . '/sites?sort=title">' . $this->i18n->__('Title') . '</a>', 'live' => '<a href="' . BASE_PATH . APP_URI . '/sites?sort=live">' . $this->i18n->__('Live') . '</a>', 'process' => $removeCheckAll), 'class' => 'data-table', 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0), 'separator' => '', 'exclude' => array('force_ssl'), 'indent' => '        ');
     $siteAry = array();
     foreach ($sites->rows as $site) {
         if ($this->data['acl']->isAuth('Phire\\Controller\\Config\\SitesController', 'edit')) {
             $edit = '<a class="edit-link" title="' . $this->i18n->__('Edit') . '" href="' . BASE_PATH . APP_URI . '/config/sites/edit/' . $site->id . '">Edit</a>';
         } else {
             $edit = null;
         }
         $sAry = array('id' => $site->id, 'title' => $site->title, 'domain' => $site->domain, 'document_root' => $site->document_root, 'base_path' => $site->base_path == '' ? '&nbsp;' : $site->base_path, 'live' => $site->live == 1 ? $this->i18n->__('Yes') : $this->i18n->__('No'));
         if (null !== $edit) {
             $sAry['edit'] = $edit;
         }
         $siteAry[] = $sAry;
     }
     if (isset($siteAry[0])) {
         $this->data['table'] = Html::encode($siteAry, $options, $this->config->pagination_limit, $this->config->pagination_range);
     }
 }
All Usage Examples Of Pop\Data\Type\Html::encode