-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export and import widget templates #49
base: master
Are you sure you want to change the base?
Conversation
src/helpers/WidgetTemplateExport.php
Outdated
// Create the tar archive | ||
$phar = new \PharData($this->getTarFilePath()); | ||
// Add generated files | ||
$phar->addFile($this->getTemplateFilePath(), $this->templateFilename); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not add strings to the tar directly instead of creating/removing tmp-files?
$phar->addFromString($this->templateFilename, $this->widgetTemplate->twig_template);
This would significantly reduce the complexity because we would have much less file handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have modified the code to meet your requirements. In this move I also removed the methods that create the temporary files.
src/helpers/WidgetTemplateExport.php
Outdated
* | ||
* @var string | ||
*/ | ||
public $exportDirectory = '@runtime/tmp/dmstr/widgets/templates'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
directories (and/or filenames) used for such exports should always have some randomness to prevent race conditions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now a protected property and is set to a random directory in /tmp (via sys_get_temp_dir)
src/models/WidgetTemplateImport.php
Outdated
} | ||
|
||
// Make sure that if an alias is set, it is resolved correctly | ||
$this->importDirectory = \Yii::getAlias($this->importDirectory); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
directories (and/or filenames) used for imports should always have some randomness to prevent race conditions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now a protected property and is set to a random directory in /tmp (via sys_get_temp_dir)
src/models/WidgetTemplateImport.php
Outdated
]); | ||
|
||
if ($model->save() === false) { | ||
\Yii::error($model->getErrors()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These errors should be passed to the user so that he gets information why the import did not work. (Exceptions?)
see: Line 136
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a custom exceptions. This bundles the error messages that occur when saving. The exception is then caught in the controller and passed on to the user.
src/models/WidgetTemplateImport.php
Outdated
try { | ||
FileHelper::removeDirectory($this->importDirectory); | ||
return true; | ||
} catch (\ErrorException $e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yii\base\ErrorException is imported. Is it intended that another ErrorException is used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line has been removed because it is no longer needed
thanks for your work, but we should change file and error handling to:
see comments above. |
@handcode : I have updated the code and left explanations to your comments respectively |
]); | ||
|
||
if ($model->save() === false) { | ||
$this->addError('tarFiles', print_r($model->getErrors(), true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Export and import widget templates
This PR provides an export and import functionality for the existing widget template CRUD
Export
Exporting a widget can be done via the newly added export buttons either on the widget template overview page or the widget templates detail page
When exporting a widget template a
.tar
file will be created to summarize the following three files:Import
To import a generated
.tar
file to the db you can do this either from the widget overview (/widget/default/index) page or the widget template overview page. You can either upload one or up to 20.tar
files (we can still discuss this value. But more makes no sense in my opinion)For each functionality a new permission check was added:
Only if these permissions are granted to the user, the user will be able to see the links and access the corresponding pages.