58743b31
Mihail
init commit - bas...
|
1
2
3
4
5
|
<?php
namespace backend\controllers;
use Yii;
use yii\filters\AccessControl;
|
54ada04a
Mihail
add base classes ...
|
6
|
use backend\components\base\BaseController;
|
58743b31
Mihail
init commit - bas...
|
7
|
use yii\filters\VerbFilter;
|
febcec0b
Mihail
final version par...
|
8
|
use backend\models\UploadFileParsingForm;
|
58743b31
Mihail
init commit - bas...
|
9
|
use yii\web\UploadedFile;
|
9bfcfcaf
Mihail
parser csv v1
|
10
|
use yii\data\ArrayDataProvider;
|
2a7a75b8
Mihail
add first version...
|
11
|
use yii\multiparser\DynamicFormHelper;
|
7a80e74c
Mihail
add DynamicFormHe...
|
12
|
use backend\components\parsers\CustomParserConfigurator;
|
2509e17e
Administrator
JSON
|
13
|
|
02e174a3
Mihail
work with custome...
|
14
|
use common\components\CustomVarDamp;
|
58743b31
Mihail
init commit - bas...
|
15
16
|
/**
|
febcec0b
Mihail
final version par...
|
17
|
* Parser controller
|
58743b31
Mihail
init commit - bas...
|
18
|
*/
|
febcec0b
Mihail
final version par...
|
19
|
|
54ada04a
Mihail
add base classes ...
|
20
|
class ParserController extends BaseController
|
58743b31
Mihail
init commit - bas...
|
21
|
{
|
2509e17e
Administrator
JSON
|
22
|
public $layout = "/column";
|
58743b31
Mihail
init commit - bas...
|
23
24
25
26
27
28
29
30
31
32
|
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
|
02e174a3
Mihail
work with custome...
|
33
|
'actions' => ['index','results','write'],
|
58743b31
Mihail
init commit - bas...
|
34
35
36
37
38
|
'allow' => true,
'roles' => ['@'],
],
],
],
|
02e174a3
Mihail
work with custome...
|
39
40
41
42
43
44
|
// 'verbs' => [
// 'class' => VerbFilter::className(),
// 'actions' => [
// 'logout' => ['post'],
// ],
// ],
|
58743b31
Mihail
init commit - bas...
|
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
|
2509e17e
Administrator
JSON
|
60
61
|
|
58743b31
Mihail
init commit - bas...
|
62
63
|
public function actionIndex()
{
|
5be26bf2
Mihail
edit upload form
|
64
|
$model = new UploadFileParsingForm();
|
58743b31
Mihail
init commit - bas...
|
65
|
|
2509e17e
Administrator
JSON
|
66
67
68
69
70
71
|
return $this->render('index', ['model' => $model]);
}
public function actionResults(){
$model = new UploadFileParsingForm();
|
7a80e74c
Mihail
add DynamicFormHe...
|
72
|
$data = [];
|
febcec0b
Mihail
final version par...
|
73
|
if ($model->load(Yii::$app->request->post())) {
|
58743b31
Mihail
init commit - bas...
|
74
75
|
$model->file = UploadedFile::getInstance($model, 'file');
|
165348a4
Mihail
draft commit
|
76
|
if ($model->validate()) {
|
9bfcfcaf
Mihail
parser csv v1
|
77
|
$filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension;
|
2509e17e
Administrator
JSON
|
78
|
|
9bfcfcaf
Mihail
parser csv v1
|
79
|
$model->file->saveAs( $filePath );
|
2509e17e
Administrator
JSON
|
80
81
|
$data = $model->readFile($filePath);
|
7a80e74c
Mihail
add DynamicFormHe...
|
82
|
Yii::$app->getCache()->set( 'parser_data', json_encode($data) );
|
2509e17e
Administrator
JSON
|
83
|
|
58743b31
Mihail
init commit - bas...
|
84
|
}
|
2509e17e
Administrator
JSON
|
85
86
87
|
} else if( Yii::$app->getCache()->get( 'parser_data' )) {
|
7a80e74c
Mihail
add DynamicFormHe...
|
88
|
$data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true );
|
2509e17e
Administrator
JSON
|
89
|
|
58743b31
Mihail
init commit - bas...
|
90
91
|
}
|
7a80e74c
Mihail
add DynamicFormHe...
|
92
93
94
95
96
97
98
|
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
]);
|
2a7a75b8
Mihail
add first version...
|
99
|
// CustomVarDamp::dumpAndDie($data);
|
7a80e74c
Mihail
add DynamicFormHe...
|
100
101
|
$header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) );
|
2a7a75b8
Mihail
add first version...
|
102
|
// CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column'));
|
7a80e74c
Mihail
add DynamicFormHe...
|
103
104
105
|
return $this->render('results',
['model' => $data,
'header_model' => $header_model,
|
2a7a75b8
Mihail
add first version...
|
106
|
'basic_column' => Yii::$app->multiparser->getConfiguration('csv','basic_column'),
|
7a80e74c
Mihail
add DynamicFormHe...
|
107
|
'dataProvider' => $provider]);
|
58743b31
Mihail
init commit - bas...
|
108
|
}
|
7a80e74c
Mihail
add DynamicFormHe...
|
109
|
|
02e174a3
Mihail
work with custome...
|
110
111
|
public function actionWrite()
{
|
7a80e74c
Mihail
add DynamicFormHe...
|
112
113
114
115
116
|
//CustomVarDamp::dumpAndDie(Yii::$app->request->post());
$arr_attributes = Yii::$app->request->post()['DynamicModel'];
$model = DynamicFormHelper::CreateDynamicModel( $arr_attributes );
foreach ($arr_attributes as $key => $value) {
|
2a7a75b8
Mihail
add first version...
|
117
|
$model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]);
|
7a80e74c
Mihail
add DynamicFormHe...
|
118
119
120
121
122
|
}
//CustomVarDamp::dumpAndDie($model);
if ($model->validate()) {
$arr = $model->toArray();
|
492d8ac1
Mihail
finishing with co...
|
123
124
|
$data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true );
|
86295c15
Mihail
XMLparser - read ...
|
125
126
|
// CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr));
CustomVarDamp::dumpAndDie($arr);
|
7a80e74c
Mihail
add DynamicFormHe...
|
127
128
129
|
}
|
58743b31
Mihail
init commit - bas...
|
130
|
|
02e174a3
Mihail
work with custome...
|
131
|
}
|
58743b31
Mihail
init commit - bas...
|
132
133
|
}
|