-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
433 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\controllers; | ||
|
||
use Craft; | ||
use craft\base\ElementInterface; | ||
use craft\helpers\ElementHelper; | ||
use craft\helpers\FileHelper; | ||
use craft\web\Controller; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Writer\Csv; | ||
use PhpOffice\PhpSpreadsheet\Writer\Ods; | ||
use PhpOffice\PhpSpreadsheet\Writer\Xls; | ||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | ||
use yii\web\BadRequestHttpException; | ||
use yii\web\Response; | ||
|
||
/** | ||
* The ElementIndexesController class is a controller that handles various element index related actions. | ||
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]]. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 3.2.0 | ||
*/ | ||
class ExportController extends Controller | ||
{ | ||
// Properties | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public $defaultAction = 'export'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected $allowAnonymous = true; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* Exports element data. | ||
* | ||
* @param string $elementType | ||
* @param string $sourceKey | ||
* @param array $criteria | ||
* @param string $format | ||
* @return Response | ||
* @throws BadRequestHttpException | ||
*/ | ||
public function actionExport(string $elementType, string $sourceKey, array $criteria, string $format): Response | ||
{ | ||
$this->requireToken(); | ||
|
||
/** @var string|ElementInterface $elementType */ | ||
$query = $elementType::find(); | ||
$source = ElementHelper::findSource($elementType, $sourceKey, 'index'); | ||
|
||
if ($source === null) { | ||
throw new BadRequestHttpException('Invalid source key: ' . $sourceKey); | ||
} | ||
|
||
// Does the source specify any criteria attributes? | ||
if (isset($source['criteria'])) { | ||
Craft::configure($query, $source['criteria']); | ||
} | ||
|
||
// Override with the request's params | ||
if ($criteria !== null) { | ||
if (isset($criteria['trashed'])) { | ||
$criteria['trashed'] = (bool)$criteria['trashed']; | ||
} | ||
Craft::configure($query, $criteria); | ||
} | ||
|
||
/** @var array $results */ | ||
$results = $query->asArray()->all(); | ||
$columns = array_keys(reset($results)); | ||
|
||
foreach ($results as &$result) { | ||
$result = array_values($result); | ||
} | ||
unset($result); | ||
|
||
// Populate the spreadsheet | ||
$spreadsheet = new Spreadsheet(); | ||
if (!empty($results)) { | ||
$worksheet = $spreadsheet->setActiveSheetIndex(0); | ||
$worksheet->fromArray($columns, null, 'A1'); | ||
$worksheet->fromArray($results, null, 'A2'); | ||
} | ||
|
||
// Could use the writer factory with a $format <-> phpspreadsheet string map, but this is more simple for now. | ||
switch ($format) { | ||
case 'csv': | ||
$writer = new Csv($spreadsheet); | ||
break; | ||
case 'xls': | ||
$writer = new Xls($spreadsheet); | ||
break; | ||
case 'xlsx': | ||
$writer = new Xlsx($spreadsheet); | ||
break; | ||
case 'ods': | ||
$writer = new Ods($spreadsheet); | ||
break; | ||
default: | ||
throw new BadRequestHttpException('Invalid export format: ' . $format); | ||
} | ||
|
||
$file = tempnam(sys_get_temp_dir(), 'export'); | ||
$writer->save($file); | ||
$contents = file_get_contents($file); | ||
unlink($file); | ||
|
||
$filename = mb_strtolower($elementType::pluralDisplayName()) . '.' . $format; | ||
$mimeType = FileHelper::getMimeTypeByExtension($filename); | ||
|
||
$response = Craft::$app->getResponse(); | ||
$response->content = $contents; | ||
$response->format = Response::FORMAT_RAW; | ||
$response->setDownloadHeaders($filename, $mimeType); | ||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.