Export the Data into a temp CSV.

@return array
@throws ErrorException
431216
public function actionExport()
    {
        $this->checkAccess('export');
        
        $header = Yii::$app->request->getBodyParam('header', 1);
        $type = Yii::$app->request->getBodyParam('type');
        $attributes = Yii::$app->request->getBodyParam('attributes', []);
        $fields = ArrayHelper::getColumn($attributes, 'value');
        
        switch (strtolower($type)) {
            case "csv":
                $mime = 'application/csv';
                $extension = 'csv';
                break;
            case "xlsx":
                $mime = 'application/vnd.ms-excel';
                $extension = 'xlsx';
                break;
        }
        
        $query = $this->prepareListQuery()->select($fields);
        $tempData = ExportHelper::$type($query, $fields, (bool) $header);
        
        $key = uniqid('ngrestexport', true);
        
        $store = FileHelper::writeFile('@runtime/'.$key.'.tmp', $tempData);
        
        $menu = Yii::$app->adminmenu->getApiDetail($this->model->ngRestApiEndpoint());
        
        $route = $menu['route'];
        $route = str_replace("/index", "/export-download", $route);
        
        if ($store) {
            Yii::$app->session->set('tempNgRestFileName', Inflector::slug($this->model->tableName())  . '-export-'.date("Y-m-d-H-i").'.' . $extension);
            Yii::$app->session->set('tempNgRestFileMime', $mime);
            Yii::$app->session->set('tempNgRestFileKey', $key);

            $url = Url::toRoute(['/'.$route], true);
            $param = http_build_query(['key' => base64_encode($key), 'time' => time()]);

            if (StringHelper::contains('?', $url)) {
                $route = $url . "&" . $param;
            } else {
                $route = $url . "?" . $param;
            }
            return [
                'url' => $route,
            ];
        }
        
        throw new ErrorException("Unable to write the temporary file. Make sure the runtime folder is writeable.");
    }
699199
public function readFile() {
        if (!file_exists($this->filename)) {
            throw new Exception(__CLASS__ . ' couldn\'t find the CSV file.');
        }
        //Prepare fgetcsv parameters
        $length = isset($this->fgetcsvOptions['length']) ? $this->fgetcsvOptions['length'] : 0;
        $delimiter = isset($this->fgetcsvOptions['delimiter']) ? $this->fgetcsvOptions['delimiter'] : ',';
        $enclosure = isset($this->fgetcsvOptions['enclosure']) ? $this->fgetcsvOptions['enclosure'] : '"';
        $escape = isset($this->fgetcsvOptions['escape']) ? $this->fgetcsvOptions['escape'] : "\\";

        $lines = []; //Clear and set rows
        if (($fp = fopen($this->filename, 'r')) !== FALSE) {
            while (($line = fgetcsv($fp, $length, $delimiter, $enclosure, $escape)) !== FALSE) {
                array_push($lines, $line);
            }
        }
        //Remove unused lines from all lines
        for ($i = 0; $i < $this->startFromLine; $i++) {
            unset($lines[$i]);
        }
        return $lines;
    }
542921
public function toString()
    {
        $temp = [];
        foreach ($this->data as $queryItem) {
            $temp[$queryItem->name] = $queryItem->value;
        }
        return http_build_query($temp);
    }
1241161
public function exportToArray()
    {
        $data = $this->toArray();

        if (count($this->exportable) === 0) {
            return $data;
        }

        $exportData = [];
        foreach ($data as $key => $value) {
            if (in_array($key, $this->exportable)) {
                $exportData[$key] = $data[$key];
            }
        }
        return $exportData;
    }
337927
public function writeData(): array
    {
        $dataBuilder = new DataBuilder();
        $dataBuilder->setString('m', $this->requiredModName, '')
                    ->setString('v', $this->requiredVersion, '')
                    ->setInteger('r', $this->isMandatory ? 1 : 0, 0);
        return $dataBuilder->getData();
    }
710031
public function exportValues()
    {
        $ret = [];
        foreach ($this->fields as $field) {
            if ($field->getExportHandler() !== Field::EXPORT_SKIP) {
                $ret[$field->getName()] = $field->exportValue();
            }
        }
        return $ret;
    }
66221
public function toArray()
	{
		$result = parent::toArray();

		if (isset($result['suspended']))
		{
			$result['suspended'] = $this->getSuspendedAttribute($result['suspended']);
		}
		if (isset($result['banned']))
		{
			$result['banned'] = $this->getBannedAttribute($result['banned']);
		}
		if (isset($result['last_attempt_at']) and $result['last_attempt_at'] instanceof DateTime)
		{
			$result['last_attempt_at'] = $result['last_attempt_at']->format('Y-m-d H:i:s');
		}
		if (isset($result['suspended_at']) and $result['suspended_at'] instanceof DateTime)
		{
			$result['suspended_at'] = $result['suspended_at']->format('Y-m-d H:i:s');
		}

		return $result;
	}
1181904
public function export() : array
    {
        return array_combine(
            $this->keys,
            array_map(function ($f) {
                return $this->getField($f);
            }, $this->keys)
        );
    }
914507
public function deleteAll()
    {
        // Count rows
        $count = $this->count();
        $tmpFile = tempnam("/tmp", uniqid() . '.tmp');
        $tempHandler = fopen($tmpFile, 'w');

        // Write the headings only and right away closes file
        fputcsv($tempHandler, $this->columns, $this->csvDelimiter);
        fclose($tempHandler);

        // Changes the original file to a temporary one.
        if (!rename($tmpFile, $this->filename)) {
            throw new DataStoreException("Failed to write the results to a file.");
        }

        return $count;
    }
666896
public function export()
	{
		$result = '';

		$fields = [];
		foreach ($this->getListBuilder()->getMapper()->getFields() as $field) {
			$fields[] = $field->getLabel();
		}

		$result.= implode(',', $fields) . PHP_EOL;

		foreach ($this->getListBuilder()->getResult() as $item) {
			$row = [];
			foreach ($item->getFields() as $field) {
				$row[] = $field->getValue();
			}

			$result.= implode(',', $row) . PHP_EOL;
		}

		return $this->createResponse($result);
	}
1347309
protected function buildErrorData()
    {
        if (is_null($this->errorCode)) {
            return null;
        }

        $data = [
            'code' => $this->errorCode,
            'message' => $this->message ?: $this->resolveMessage()
        ];

        return array_merge($data, $this->data);
    }
980797
public function writeData(): array
    {
        $dataBuilder = new DataBuilder();
        $dataBuilder->setString('n', $this->name, '')
                    ->setArray('l', $this->labels->writeData(), null, [])
                    ->setArray('d', $this->descriptions->writeData(), null, [])
                    ->setArray('c', array_values(array_unique($this->craftingCategories)), 'strval', [])
                    ->setFloat('s', $this->craftingSpeed, 1.)
                    ->setInteger('i', $this->numberOfItemSlots, 0)
                    ->setInteger('f', $this->numberOfFluidInputSlots, 0)
                    ->setInteger('u', $this->numberOfFluidOutputSlots, 0)
                    ->setInteger('m', $this->numberOfModuleSlots, 0)
                    ->setFloat('e', $this->energyUsage, 0.)
                    ->setString('r', $this->energyUsageUnit, 'W')
                    ->setString('o', $this->iconHash, '');
        return $dataBuilder->getData();
    }
794914
public function toArray()
    {
        $ref = new \ReflectionObject($this);
        $export = array();

        foreach ($ref->getProperties() as $property) {
            $property->setAccessible(true);
            $name = strtolower(
                preg_replace('@([A-Z])@', '_\1', $property->getName())
            );
            $export[$name] = $property->getValue($this);

            if ($export[$name] instanceof DataWrapperInterface) {
                $export[$name] = $export[$name]->toArray();
            } elseif ($export[$name] instanceof \DateTime) {
                $export[$name] = $export[$name]->format('Y-m-d');
            }
        }

        return $export;
    }
1363230
public function toCsv()
    {
        $csv = '';

        foreach ($this->toArray() as $csvRow) {
            if (is_array($csvRow)) {
                $data = implode($this->_csvDelimiter, $csvRow);
            } else {
                $data = $csvRow;
            }

            $csv .= $data . $this->_csvLineDelimiter;
        }

        return rtrim($csv, $this->_csvLineDelimiter);
    }
251968
public function export()
	{
		$result = [];
		foreach ($this->getListBuilder()->getResult() as $item) {
			foreach ($item->getFields() as $field) {
				$result[$item->getIdentifier()][$field->getName()] = $field->getValue();
			}
		}

		return $this->createResponse(Response::json($result)->getContent());
	}
482068
public function serialize()
    {
        $clone = clone $this;

        $clone->setExtractFlags(self::EXTR_BOTH);

        $data = [];

        foreach ($clone as $item) {
            $data[] = $item;
        }

        return serialize($data);
    }
829912
protected function _exportData() {
		$data = $this->_entity ? $this->_entity->export() : $this->_data;

		if (!$list = $this->_config['whitelist']) {
			return $data;
		}
		$list = array_combine($list, $list);

		if (!$this->_entity) {
			return array_intersect_key($data, $list);
		}

		foreach ($data as $type => $values) {
			if (!is_array($values)) {
				continue;
			}
			$data[$type] = array_intersect_key($values, $list);
		}
		return $data;
	}