FOF30\View\DataView\Csv::display PHP Метод

display() публичный Метод

Instead of loadTemplate is uses loadAnyTemplate.
public display ( string $tpl = null ) : boolean
$tpl string The name of the template file to parse
Результат boolean True on success
    public function display($tpl = null)
    {
        $eventName = 'onBefore' . ucfirst($this->doTask);
        $this->triggerEvent($eventName, array($tpl));
        // Load the model
        /** @var DataModel $model */
        $model = $this->getModel();
        $items = $model->get();
        $this->items = $items;
        $platform = $this->container->platform;
        $document = $platform->getDocument();
        if ($document instanceof \JDocument) {
            $document->setMimeEncoding('text/csv');
        }
        $platform->setHeader('Pragma', 'public');
        $platform->setHeader('Expires', '0');
        $platform->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
        $platform->setHeader('Cache-Control', 'public', false);
        $platform->setHeader('Content-Description', 'File Transfer');
        $platform->setHeader('Content-Disposition', 'attachment; filename="' . $this->csvFilename . '"');
        if (is_null($tpl)) {
            $tpl = 'csv';
        }
        $hasFailed = false;
        try {
            $result = $this->loadTemplate($tpl, true);
            if ($result instanceof \Exception) {
                $hasFailed = true;
            }
        } catch (\Exception $e) {
            $hasFailed = true;
        }
        if (!$hasFailed) {
            echo $result;
        } else {
            // Default CSV behaviour in case the template isn't there!
            if (count($items) === 0) {
                throw new AccessForbidden();
            }
            $item = $items->last();
            $keys = $item->getData();
            $keys = array_keys($keys);
            reset($items);
            if (!empty($this->csvFields)) {
                $temp = array();
                foreach ($this->csvFields as $f) {
                    $exist = false;
                    // If we have a dot and it isn't part of the field name, we are dealing with relations
                    if (!$model->hasField($f) && strpos($f, '.')) {
                        $methods = explode('.', $f);
                        $object = $item;
                        // Let's see if the relation exists
                        foreach ($methods as $method) {
                            if (isset($object->{$method})) {
                                $exist = true;
                                $object = $object->{$method};
                            } else {
                                $exist = false;
                                break;
                            }
                        }
                    }
                    if (in_array($f, $keys)) {
                        $temp[] = $f;
                    } elseif ($exist) {
                        $temp[] = $f;
                    }
                }
                $keys = $temp;
            }
            if ($this->csvHeader) {
                $csv = array();
                foreach ($keys as $k) {
                    $k = str_replace('"', '""', $k);
                    $k = str_replace("\r", '\\r', $k);
                    $k = str_replace("\n", '\\n', $k);
                    $k = '"' . $k . '"';
                    $csv[] = $k;
                }
                echo implode(",", $csv) . "\r\n";
            }
            foreach ($items as $item) {
                $csv = array();
                foreach ($keys as $k) {
                    // If our key contains a dot and it isn't part of the field name, we are dealing with relations
                    if (!$model->hasField($k) && strpos($k, '.')) {
                        $methods = explode('.', $k);
                        $v = $item;
                        foreach ($methods as $method) {
                            $v = $v->{$method};
                        }
                    } else {
                        $v = $item->{$k};
                    }
                    if (is_array($v)) {
                        $v = 'Array';
                    } elseif (is_object($v)) {
                        $v = 'Object';
                    }
                    $v = str_replace('"', '""', $v);
                    $v = str_replace("\r", '\\r', $v);
                    $v = str_replace("\n", '\\n', $v);
                    $v = '"' . $v . '"';
                    $csv[] = $v;
                }
                echo implode(",", $csv) . "\r\n";
            }
        }
        $eventName = 'onAfter' . ucfirst($this->doTask);
        $this->triggerEvent($eventName, array($tpl));
        return true;
    }