Skip to content

Commit 461168d

Browse files
committed
Jet Core
------------ * New feature: DataModel - data export and import infrastructure Jet Studio ------------ * New feature: DataModel - data export and import tool * Minor bug fixes
1 parent 45ec694 commit 461168d

File tree

23 files changed

+863
-57
lines changed

23 files changed

+863
-57
lines changed

_tools/studio/application/Modules/DataModel/DataModel/Definition/Property/Trait.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,7 @@ public function getIcons(): string
339339

340340
return $icon;
341341
}
342-
343-
344-
/**
345-
*
346-
* @param mixed &$value
347-
*/
348-
public function checkValueType( mixed &$value ): void
349-
{
350-
}
351-
342+
352343

353344
/**
354345
* @return string
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
*
4+
* @copyright Copyright (c) Miroslav Marek <mirek.marek@web-jet.cz>
5+
* @license http://www.php-jet.net/license/license.txt
6+
* @author Miroslav Marek <mirek.marek@web-jet.cz>
7+
*/
8+
namespace JetStudioModule\DataModelExport;
9+
10+
use Error;
11+
use Exception;
12+
use Jet\DataModel_ImportExport;
13+
use Jet\Form;
14+
use Jet\Form_Field_Input;
15+
use Jet\Form_Field_Select;
16+
use Jet\Http_Request;
17+
use Jet\IO_Dir;
18+
use JetStudio\JetStudio;
19+
use JetStudio\JetStudio_Conf_Path;
20+
use JetStudio\JetStudio_Module_Controller;
21+
22+
class Controller extends JetStudio_Module_Controller
23+
{
24+
protected function resolve(): string
25+
{
26+
$action = Http_Request::GET()->getString( 'action' );
27+
return $action ? : 'default';
28+
}
29+
30+
public function default_Action(): void
31+
{
32+
33+
$default_dir = JetStudio_Conf_Path::getTmp().'_data_model_exports/';
34+
if(!IO_Dir::exists($default_dir)) {
35+
IO_Dir::create($default_dir);
36+
}
37+
38+
$data_model_classes = JetStudio::getModule_DataModel()?->getDataModelClasses()??[];
39+
40+
$options = [];
41+
foreach($data_model_classes as $class_name => $class) {
42+
$options[$class_name] = $class->getFullClassName();
43+
}
44+
45+
$select_class = new Form_Field_Select('class', 'DataModel Class:');
46+
$select_class->setSelectOptions( $options );
47+
48+
$dir = new Form_Field_Input('dir', 'Export dir:');
49+
$dir->setIsRequired( true );
50+
$dir->setDefaultValue( $default_dir );
51+
$dir->setErrorMessages([
52+
Form_Field_Input::ERROR_CODE_EMPTY => 'Please enter export directory path',
53+
'dir_does_not_exist' => "Directory '%DIR%' does not exist",
54+
'dir_is_writable' => "Directory '%DIR%' is not writable",
55+
]);
56+
$dir->setValidator( function( Form_Field_Input $input ) : bool {
57+
$value = $input->getValueRaw();
58+
59+
if(!IO_Dir::exists($value)) {
60+
$input->setError('dir_does_not_exist', ['DIR' => $value]);
61+
return false;
62+
}
63+
64+
if(!IO_Dir::isWritable($value)) {
65+
$input->setError('dir_is_writable', ['DIR' => $value]);
66+
return false;
67+
}
68+
69+
return true;
70+
} );
71+
72+
$form = new Form('export', [$select_class, $dir]);
73+
74+
$this->view->setVar( 'form', $form );
75+
76+
if($form->catch()) {
77+
$dir = $form->field('dir')->getValue();
78+
$class = $form->field('class')->getValue();
79+
80+
set_time_limit(0);
81+
82+
try {
83+
$where_is_export = DataModel_ImportExport::export( $class, $dir );
84+
85+
$this->view->setVar('where_is_export', $where_is_export);
86+
$this->output('done');
87+
88+
} catch( Error|Exception $e ) {
89+
90+
$this->view->setVar('error', $e->getMessage());
91+
$this->output('error');
92+
93+
}
94+
95+
96+
} else {
97+
$this->output( 'main' );
98+
}
99+
100+
}
101+
102+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
*
5+
* @copyright Copyright (c) Miroslav Marek <mirek.marek@web-jet.cz>
6+
* @license http://www.php-jet.net/license/license.txt
7+
* @author Miroslav Marek <mirek.marek@web-jet.cz>
8+
*/
9+
10+
namespace JetStudioModule\DataModelExport;
11+
12+
use JetStudio\JetStudio_Module;
13+
use JetStudio\JetStudio_Module_Manifest;
14+
15+
class Main extends JetStudio_Module
16+
{
17+
public function __construct( JetStudio_Module_Manifest $manifest )
18+
{
19+
$this->manifest = $manifest;
20+
}
21+
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
return [
3+
'vendor' => 'Miroslav Marek <mirek.marek@web-jet.cz>',
4+
'version' => '',
5+
'label' => 'ORM - Data Export',
6+
'url_path_part' => 'datamodel-export',
7+
'icon' => 'download',
8+
'sort_order' => 10,
9+
];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace JetStudioModule\DataModelExport;
3+
4+
use Jet\Tr;
5+
use Jet\UI_messages;
6+
use Jet\MVC_View;
7+
8+
/**
9+
* @var MVC_View $this
10+
*/
11+
12+
13+
?>
14+
<div style="padding: 10%">
15+
<?=UI_messages::createSuccess( Tr::_('Export has been successfully completed') )->setCloseable( false ); ?>
16+
17+
<div class="card card-body">
18+
<?=Tr::_('<b>You can find the data in this directory:</b> %DATA%', ['DATA'=>$this->getRaw('where_is_export')]); ?>
19+
</div>
20+
21+
</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace JetStudioModule\DataModelExport;
3+
4+
use Jet\Tr;
5+
use Jet\UI_messages;
6+
use Jet\MVC_View;
7+
8+
/**
9+
* @var MVC_View $this
10+
*/
11+
12+
13+
?>
14+
<div style="padding: 10%">
15+
<?=UI_messages::createDanger( Tr::_('Error during export') )->setCloseable( false ); ?>
16+
17+
<div class="card card-body">
18+
<?=$this->getRaw('error'); ?>
19+
</div>
20+
21+
</div>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace JetStudioModule\DataModelExport;
3+
4+
use Jet\MVC_View;
5+
use Jet\Form;
6+
use Jet\Tr;
7+
use Jet\UI;
8+
use Jet\UI_button;
9+
10+
/**
11+
* @var MVC_View $this
12+
* @var Form $form
13+
*/
14+
$form = $this->getRaw('form');
15+
16+
$form->renderer()->addJsAction('onsubmit', "$('#__progress__').show();return true;");
17+
?>
18+
19+
20+
<?=$form->start()?>
21+
<?=$form->getCommonMessage();?>
22+
<div style="padding: 2%">
23+
<div class="form-fields">
24+
<?=$form->field('class')?>
25+
<?=$form->field('dir')?>
26+
<div></div>
27+
<div><?=UI::button(Tr::_('Perform export'))->setClass(UI_button::CLASS_INFO)->setType(UI_button::CLASS_SUCCESS)?></div>
28+
</div>
29+
30+
</div>
31+
<?=$form->end()?>
32+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
*
4+
* @copyright Copyright (c) Miroslav Marek <mirek.marek@web-jet.cz>
5+
* @license http://www.php-jet.net/license/license.txt
6+
* @author Miroslav Marek <mirek.marek@web-jet.cz>
7+
*/
8+
namespace JetStudioModule\DataModelImport;
9+
10+
use Error;
11+
use Exception;
12+
use Jet\DataModel_ImportExport;
13+
use Jet\DataModel_ImportExport_MetaInfo;
14+
use Jet\Http_Request;
15+
use Jet\IO_Dir;
16+
use Jet\IO_File;
17+
use JetStudio\JetStudio_Conf_Path;
18+
use JetStudio\JetStudio_Module_Controller;
19+
20+
class Controller extends JetStudio_Module_Controller
21+
{
22+
protected function resolve(): string
23+
{
24+
$action = Http_Request::GET()->getString( 'action' );
25+
return $action ? : 'default';
26+
}
27+
28+
public function default_Action(): void
29+
{
30+
31+
$dir = JetStudio_Conf_Path::getTmp().'_data_model_imports/';
32+
if(!IO_Dir::exists($dir)) {
33+
IO_Dir::create($dir);
34+
}
35+
36+
$this->view->setVar( 'imports_dir', $dir );
37+
38+
$_imports = IO_Dir::getSubdirectoriesList( $dir );
39+
40+
$imports = [];
41+
$import_dirs = [];
42+
foreach($_imports as $path=>$dir_name) {
43+
$meta_info_file_path = $path.DataModel_ImportExport_MetaInfo::META_INFO_FILE_NAME;
44+
45+
if(!IO_File::isReadable($meta_info_file_path)) {
46+
continue;
47+
}
48+
49+
$imports[$dir_name] = DataModel_ImportExport_MetaInfo::read( $meta_info_file_path );
50+
$import_dirs[$dir_name] = $path;
51+
}
52+
53+
$selected_import_name = Http_Request::GET()->getString( 'import', valid_values: array_keys($imports) );
54+
55+
$this->view->setVar( 'imports', $imports );
56+
$this->view->setVar( 'selected_import_name', $selected_import_name );
57+
58+
59+
if(
60+
$selected_import_name &&
61+
Http_Request::GET()->getString('perform_import')=='do'
62+
) {
63+
try {
64+
DataModel_ImportExport::import( $import_dirs[$selected_import_name] );
65+
66+
$this->output('done');
67+
68+
} catch( Error|Exception $e ) {
69+
$this->view->setVar('error', $e->getMessage());
70+
$this->output('error');
71+
}
72+
73+
} else {
74+
75+
$this->output('main');
76+
77+
}
78+
79+
80+
81+
}
82+
83+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
*
5+
* @copyright Copyright (c) Miroslav Marek <mirek.marek@web-jet.cz>
6+
* @license http://www.php-jet.net/license/license.txt
7+
* @author Miroslav Marek <mirek.marek@web-jet.cz>
8+
*/
9+
10+
namespace JetStudioModule\DataModelImport;
11+
12+
use JetStudio\JetStudio_Module;
13+
use JetStudio\JetStudio_Module_Manifest;
14+
15+
class Main extends JetStudio_Module
16+
{
17+
public function __construct( JetStudio_Module_Manifest $manifest )
18+
{
19+
$this->manifest = $manifest;
20+
}
21+
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
return [
3+
'vendor' => 'Miroslav Marek <mirek.marek@web-jet.cz>',
4+
'version' => '',
5+
'label' => 'ORM - Data Import',
6+
'url_path_part' => 'datamodel-import',
7+
'icon' => 'upload',
8+
'sort_order' => 11,
9+
];

0 commit comments

Comments
 (0)