Commit 5c35d76d3831f79d1632694e8446eb8416551673
1 parent
13ffaf8e
add xls parser
Showing
23 changed files
with
2034 additions
and
153 deletions
Show diff stats
backend/controllers/BrandsController.php
... | ... | @@ -28,11 +28,11 @@ class BrandsController extends Controller |
28 | 28 | 'class' => AccessControl::className(), |
29 | 29 | 'rules' => [ |
30 | 30 | [ |
31 | - 'actions' => ['login', 'error', 'download-photo','delete-image' ], | |
31 | + 'actions' => ['login', 'error', 'download-photo', 'delete-image'], | |
32 | 32 | 'allow' => true, |
33 | 33 | ], |
34 | 34 | [ |
35 | - 'actions' => ['logout', 'index','create','update','view','delete',], | |
35 | + 'actions' => ['logout', 'index', 'create', 'update', 'view', 'delete',], | |
36 | 36 | 'allow' => true, |
37 | 37 | 'roles' => ['@'], |
38 | 38 | ], |
... | ... | @@ -82,8 +82,13 @@ class BrandsController extends Controller |
82 | 82 | public function actionCreate() |
83 | 83 | { |
84 | 84 | $model = new Brands(); |
85 | - | |
86 | - return $this->saveIMG( $model, 'create' ); | |
85 | + if ($model->load(Yii::$app->request->post())) { | |
86 | + return $this->saveModelWithImg($model); | |
87 | + } else { | |
88 | + return $this->render('create', [ | |
89 | + 'model' => $model, | |
90 | + ]); | |
91 | + } | |
87 | 92 | |
88 | 93 | } |
89 | 94 | |
... | ... | @@ -96,8 +101,20 @@ class BrandsController extends Controller |
96 | 101 | public function actionUpdate($id) |
97 | 102 | { |
98 | 103 | $model = $this->findModel($id); |
99 | - | |
100 | - return $this->saveIMG( $model, 'update' ); | |
104 | + if ($model->load(Yii::$app->request->post())) { | |
105 | + | |
106 | + if ($model->delete_img) { | |
107 | + // удаляем изображение (как из базы так и с сервера), сохраняем модель | |
108 | + return $this->saveModelWithDeleteImg($model); | |
109 | + } else { | |
110 | + // сохраняем модель с сохранением изображения (как в базу так и на сервер) | |
111 | + return $this->saveModelWithImg($model); | |
112 | + } | |
113 | + } else { | |
114 | + return $this->render('update', [ | |
115 | + 'model' => $model, | |
116 | + ]); | |
117 | + } | |
101 | 118 | |
102 | 119 | } |
103 | 120 | |
... | ... | @@ -136,24 +153,42 @@ class BrandsController extends Controller |
136 | 153 | * сохраняет картинку и валидирует модель |
137 | 154 | * выбрасывает исключение в случае ошибки |
138 | 155 | */ |
139 | - protected function saveIMG( $model, $action ){ | |
140 | - if( $model->load(Yii::$app->request->post()) ) { | |
141 | - $model->file = UploadedFile::getInstance($model, 'file'); | |
142 | - $model->IMG = $model->file->name; | |
143 | - // первый проход - валидируем, сохраняем файл, | |
144 | - if ( $model->validate() ) { | |
145 | - // сохраним файл | |
146 | - $model->file->saveAs( Yii::getAlias('@storage') . '/files/brands/' . $model->IMG ); | |
147 | - if ( $model->save() ) { | |
148 | - return $this->redirect(['index']); | |
149 | - } | |
156 | + protected function saveModelWithImg($model) | |
157 | + { | |
158 | + $model->file = UploadedFile::getInstance($model, 'file'); | |
159 | + $model->IMG = $model->file->name; | |
160 | + // первый проход - валидируем, сохраняем файл, | |
161 | + if ($model->validate()) { | |
162 | + // сохраним файл | |
163 | + $model->file->saveAs(Yii::getAlias('@storage') . '/files/brands/' . $model->IMG); | |
164 | + if ($model->save()) { | |
165 | + return $this->redirect(['index']); | |
166 | + } | |
167 | + } | |
168 | + $model->throwStringErrorException(); | |
169 | + | |
170 | + } | |
171 | + | |
172 | + /** | |
173 | + * @param $model | |
174 | + * @return \yii\web\Response | |
175 | + * сохраняет модель с удалением картинки | |
176 | + * выбрасывает исключение в случае ошибки | |
177 | + */ | |
178 | + protected function saveModelWithDeleteImg($model) | |
179 | + { | |
180 | + if ($model->validate()) { | |
181 | + $img_path = Yii::getAlias('@storage') . '/files/brands/' . $model->IMG; | |
182 | + // удалим файл | |
183 | + if(file_exists( $img_path )) | |
184 | + unlink( $img_path ); | |
185 | + | |
186 | + $model->IMG = ''; | |
187 | + if ($model->save()) { | |
188 | + return $this->redirect(['index']); | |
150 | 189 | } |
151 | - $model->throwStringErrorException(); | |
152 | - } else { | |
153 | - return $this->render($action, [ | |
154 | - 'model' => $model, | |
155 | - ]); | |
156 | 190 | } |
191 | + $model->throwStringErrorException(); | |
157 | 192 | |
158 | 193 | } |
159 | 194 | } | ... | ... |
backend/controllers/CrossingUploadController.php
... | ... | @@ -100,7 +100,6 @@ class CrossingUploadController extends BaseController |
100 | 100 | $model = DynamicFormHelper::CreateDynamicModel($arr_attributes); |
101 | 101 | $crosses_model = new DetailsCrosses(); |
102 | 102 | $arr_keys = array_keys($this->getBasicColumns()); |
103 | - | |
104 | 103 | //добавим правила валидации (колонки должны быть те что в модели) |
105 | 104 | foreach ($arr_attributes as $key => $value) { |
106 | 105 | $model->addRule($key, 'in', ['range' => $arr_keys]); | ... | ... |
backend/controllers/ParserController.php
... | ... | @@ -224,13 +224,13 @@ class ParserController extends BaseController |
224 | 224 | $id = Yii::$app->request->post()['id']; |
225 | 225 | try { |
226 | 226 | $files_model->delete($id); |
227 | - if (file_exists( Yii::getAlias('@temp_upload') . '/' . $id . '.csv' )) { | |
228 | - unlink(Yii::getAlias('@temp_upload') . '/' . $id . '.csv' ); | |
227 | + $arr_supported_extension = Parser::supportedExtension(); | |
228 | + // получим список файлов которые ожидают к загрузке | |
229 | + foreach ($arr_supported_extension as $ext) { | |
230 | + if (file_exists(Yii::getAlias('@temp_upload') . '/' . $id . ".{$ext}")) { | |
231 | + unlink(Yii::getAlias('@temp_upload') . '/' . $id . ".{$ext}"); | |
232 | + } | |
229 | 233 | } |
230 | - if (file_exists( Yii::getAlias('@temp_upload') . '/' . $id . '.xlsx' )) { | |
231 | - unlink(Yii::getAlias('@temp_upload') . '/' . $id . '.xlsx' ); | |
232 | - } | |
233 | - | |
234 | 234 | // сообщим скрипту что все ОК |
235 | 235 | echo 1; |
236 | 236 | } catch (ErrorException $e) { | ... | ... |
backend/models/UploadFileCrossingForm.php
... | ... | @@ -27,7 +27,7 @@ class UploadFileCrossingForm extends Model |
27 | 27 | { |
28 | 28 | return [ |
29 | 29 | ['file', 'required', 'message' => 'Не выбран файл!' ], |
30 | - [['file'], 'file', 'extensions' => 'csv', 'checkExtensionByMimeType'=>false ], | |
30 | + [['file'], 'file', 'extensions' => ['csv'], 'checkExtensionByMimeType'=>false ], | |
31 | 31 | [['delete_prefix1', 'delete_prefix2'], 'boolean' ], |
32 | 32 | |
33 | 33 | ]; | ... | ... |
backend/models/UploadFileParsingForm.php
... | ... | @@ -48,7 +48,7 @@ class UploadFileParsingForm extends Model |
48 | 48 | return [ |
49 | 49 | ['importer_id', 'required', 'message' => 'Не указан поставщик!' ], |
50 | 50 | ['file', 'required', 'message' => 'Не выбран файл!' ], |
51 | - [['file'], 'file', 'extensions' => ['csv', 'xlsx'], 'checkExtensionByMimeType'=>false ], | |
51 | + [['file'], 'file', 'extensions' => ['csv', 'xlsx','txt','xls'], 'checkExtensionByMimeType'=>false ], | |
52 | 52 | ['importer_id', 'integer','max' => 999999, 'min' => 0 ], |
53 | 53 | [['action','delete_prefix', 'delete_price', 'success'], 'boolean', 'except' => 'auto' ], // только для ручной загрузки |
54 | 54 | ['delimiter', 'string', 'max' => 1], | ... | ... |
backend/views/brands/_form.php
... | ... | @@ -25,9 +25,33 @@ use yii\widgets\ActiveForm; |
25 | 25 | ] |
26 | 26 | ) |
27 | 27 | ]); ?> |
28 | + | |
28 | 29 | <?= $form->field($model, 'if_oem')->checkbox() ?> |
30 | + | |
31 | +<div class="container-fluid"> | |
32 | + <fieldset> | |
33 | + <legend>Изображение</legend> | |
34 | + <div class="col-md-4"> | |
29 | 35 | <?= $form->field($model, 'file')->fileInput()->label(false)->hint('имя файла должно иметь только латинские символы и цифры',['tag'=>'span'])?> |
36 | + </div> | |
37 | + | |
38 | + <div class="col-md-4"> | |
39 | + <div> | |
40 | + <?php | |
41 | + if(!$model->isNewRecord && $model->IMG) | |
42 | + echo $form->field($model, 'delete_img')->checkbox(); | |
43 | + ?> | |
44 | + </div> | |
30 | 45 | |
46 | + <?php | |
47 | + if(!$model->isNewRecord && $model->IMG) | |
48 | + echo "Имя файла - $model->IMG"; | |
49 | + ?> | |
50 | + | |
51 | + </div> | |
52 | + <fieldset> | |
53 | + | |
54 | +</div> | |
31 | 55 | |
32 | 56 | <div class="form-group"> |
33 | 57 | <?= Html::submitButton($model->isNewRecord ? 'Добавить' : 'Редактировать', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | ... | ... |
backend/views/brands/create.php
... | ... | @@ -6,7 +6,7 @@ use yii\helpers\Html; |
6 | 6 | /* @var $this yii\web\View */ |
7 | 7 | /* @var $model common\models\Brands */ |
8 | 8 | |
9 | -$this->title = 'Добавить Brands'; | |
9 | +$this->title = 'Добавить бренд'; | |
10 | 10 | $this->params['breadcrumbs'][] = ['label' => 'Brands', 'url' => ['index']]; |
11 | 11 | $this->params['breadcrumbs'][] = $this->title; |
12 | 12 | ?> | ... | ... |
backend/views/brands/index.php
... | ... | @@ -18,7 +18,7 @@ $img1 = '/storage/checkbox1.gif'; |
18 | 18 | <?php // echo $this->render('_search', ['model' => $searchModel]); ?> |
19 | 19 | |
20 | 20 | <p> |
21 | - <?= Html::a('Создать', ['create'], ['class' => 'btn btn-success']) ?> | |
21 | + <?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?> | |
22 | 22 | </p> |
23 | 23 | |
24 | 24 | <?= GridView::widget([ | ... | ... |
backend/views/margins/_form.php
... | ... | @@ -17,7 +17,7 @@ use yii\widgets\ActiveForm; |
17 | 17 | <?= $form->field($model, 'koef')->textInput() ?> |
18 | 18 | |
19 | 19 | <div class="form-group"> |
20 | - <?= Html::submitButton($model->isNewRecord ? 'Добавить' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | |
20 | + <?= Html::submitButton($model->isNewRecord ? 'Добавить' : 'Обновить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | |
21 | 21 | </div> |
22 | 22 | |
23 | 23 | <?php ActiveForm::end(); ?> | ... | ... |
common/components/parsers/CsvParser.php
1 | +<?php | |
2 | +// Test CVS | |
3 | + | |
4 | +require_once 'Excel/reader.php'; | |
5 | + | |
6 | + | |
7 | +// ExcelFile($filename, $encoding); | |
8 | +$data = new Spreadsheet_Excel_Reader(); | |
9 | + | |
10 | + | |
11 | +// Set output Encoding. | |
12 | +$data->setOutputEncoding('CP1251'); | |
13 | + | |
14 | +/*** | |
15 | +* if you want you can change 'iconv' to mb_convert_encoding: | |
16 | +* $data->setUTFEncoder('mb'); | |
17 | +* | |
18 | +**/ | |
19 | + | |
20 | +/*** | |
21 | +* By default rows & cols indeces start with 1 | |
22 | +* For change initial index use: | |
23 | +* $data->setRowColOffset(0); | |
24 | +* | |
25 | +**/ | |
26 | + | |
27 | + | |
28 | + | |
29 | +/*** | |
30 | +* Some function for formatting output. | |
31 | +* $data->setDefaultFormat('%.2f'); | |
32 | +* setDefaultFormat - set format for columns with unknown formatting | |
33 | +* | |
34 | +* $data->setColumnFormat(4, '%.3f'); | |
35 | +* setColumnFormat - set format for column (apply only to number fields) | |
36 | +* | |
37 | +**/ | |
38 | + | |
39 | +$data->read('jxlrwtest.xls'); | |
40 | + | |
41 | +/* | |
42 | + | |
43 | + | |
44 | + $data->sheets[0]['numRows'] - count rows | |
45 | + $data->sheets[0]['numCols'] - count columns | |
46 | + $data->sheets[0]['cells'][$i][$j] - data from $i-row $j-column | |
47 | + | |
48 | + $data->sheets[0]['cellsInfo'][$i][$j] - extended info about cell | |
49 | + | |
50 | + $data->sheets[0]['cellsInfo'][$i][$j]['type'] = "date" | "number" | "unknown" | |
51 | + if 'type' == "unknown" - use 'raw' value, because cell contain value with format '0.00'; | |
52 | + $data->sheets[0]['cellsInfo'][$i][$j]['raw'] = value if cell without format | |
53 | + $data->sheets[0]['cellsInfo'][$i][$j]['colspan'] | |
54 | + $data->sheets[0]['cellsInfo'][$i][$j]['rowspan'] | |
55 | +*/ | |
56 | + | |
57 | +error_reporting(E_ALL ^ E_NOTICE); | |
58 | + | |
59 | +for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { | |
60 | + for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { | |
61 | + echo "\"".$data->sheets[0]['cells'][$i][$j]."\","; | |
62 | + } | |
63 | + echo "\n"; | |
64 | + | |
65 | +} | |
66 | + | |
67 | + | |
68 | +//print_r($data); | |
69 | +//print_r($data->formatRecords); | |
70 | +?> | ... | ... |
1 | +<? | |
2 | +$allow_url_override = 1; // Set to 0 to not allow changed VIA POST or GET | |
3 | +if(!$allow_url_override || !isset($file_to_include)) | |
4 | +{ | |
5 | + $file_to_include = "jxlrwtest.xls"; | |
6 | +} | |
7 | +if(!$allow_url_override || !isset($max_rows)) | |
8 | +{ | |
9 | + $max_rows = 0; //USE 0 for no max | |
10 | +} | |
11 | +if(!$allow_url_override || !isset($max_cols)) | |
12 | +{ | |
13 | + $max_cols = 5; //USE 0 for no max | |
14 | +} | |
15 | +if(!$allow_url_override || !isset($debug)) | |
16 | +{ | |
17 | + $debug = 0; //1 for on 0 for off | |
18 | +} | |
19 | +if(!$allow_url_override || !isset($force_nobr)) | |
20 | +{ | |
21 | + $force_nobr = 1; //Force the info in cells not to wrap unless stated explicitly (newline) | |
22 | +} | |
23 | + | |
24 | +require_once 'Excel/reader.php'; | |
25 | +$data = new Spreadsheet_Excel_Reader(); | |
26 | +$data->setOutputEncoding('CPa25a'); | |
27 | +$data->read($file_to_include); | |
28 | +error_reporting(E_ALL ^ E_NOTICE); | |
29 | +echo " | |
30 | +<STYLE> | |
31 | +.table_data | |
32 | +{ | |
33 | + border-style:ridge; | |
34 | + border-width:1; | |
35 | +} | |
36 | +.tab_base | |
37 | +{ | |
38 | + background:#C5D0DD; | |
39 | + font-weight:bold; | |
40 | + border-style:ridge; | |
41 | + border-width:1; | |
42 | + cursor:pointer; | |
43 | +} | |
44 | +.table_sub_heading | |
45 | +{ | |
46 | + background:#CCCCCC; | |
47 | + font-weight:bold; | |
48 | + border-style:ridge; | |
49 | + border-width:1; | |
50 | +} | |
51 | +.table_body | |
52 | +{ | |
53 | + background:#F0F0F0; | |
54 | + font-wieght:normal; | |
55 | + font-size:12; | |
56 | + font-family:sans-serif; | |
57 | + border-style:ridge; | |
58 | + border-width:1; | |
59 | + border-spacing: 0px; | |
60 | + border-collapse: collapse; | |
61 | +} | |
62 | +.tab_loaded | |
63 | +{ | |
64 | + background:#222222; | |
65 | + color:white; | |
66 | + font-weight:bold; | |
67 | + border-style:groove; | |
68 | + border-width:1; | |
69 | + cursor:pointer; | |
70 | +} | |
71 | +</STYLE> | |
72 | +"; | |
73 | +function make_alpha_from_numbers($number) | |
74 | +{ | |
75 | + $numeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
76 | + if($number<strlen($numeric)) | |
77 | + { | |
78 | + return $numeric[$number]; | |
79 | + } | |
80 | + else | |
81 | + { | |
82 | + $dev_by = floor($number/strlen($numeric)); | |
83 | + return "" . make_alpha_from_numbers($dev_by-1) . make_alpha_from_numbers($number-($dev_by*strlen($numeric))); | |
84 | + } | |
85 | +} | |
86 | +echo "<SCRIPT LANGUAGE='JAVASCRIPT'> | |
87 | +var sheet_HTML = Array();\n"; | |
88 | +for($sheet=0;$sheet<count($data->sheets);$sheet++) | |
89 | +{ | |
90 | + $table_output[$sheet] .= "<TABLE CLASS='table_body'> | |
91 | + <TR> | |
92 | + <TD> </TD>"; | |
93 | + for($i=0;$i<$data->sheets[$sheet]['numCols']&&($i<=$max_cols||$max_cols==0);$i++) | |
94 | + { | |
95 | + $table_output[$sheet] .= "<TD CLASS='table_sub_heading' ALIGN=CENTER>" . make_alpha_from_numbers($i) . "</TD>"; | |
96 | + } | |
97 | + for($row=1;$row<=$data->sheets[$sheet]['numRows']&&($row<=$max_rows||$max_rows==0);$row++) | |
98 | + { | |
99 | + $table_output[$sheet] .= "<TR><TD CLASS='table_sub_heading'>" . $row . "</TD>"; | |
100 | + for($col=1;$col<=$data->sheets[$sheet]['numCols']&&($col<=$max_cols||$max_cols==0);$col++) | |
101 | + { | |
102 | + if($data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'] >=1 && $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'] >=1) | |
103 | + { | |
104 | + $this_cell_colspan = " COLSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan']; | |
105 | + $this_cell_rowspan = " ROWSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan']; | |
106 | + for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$i++) | |
107 | + { | |
108 | + $data->sheets[$sheet]['cellsInfo'][$row][$col+$i]['dontprint']=1; | |
109 | + } | |
110 | + for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];$i++) | |
111 | + { | |
112 | + for($j=0;$j<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$j++) | |
113 | + { | |
114 | + $data->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1; | |
115 | + } | |
116 | + } | |
117 | + } | |
118 | + else if($data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'] >=1) | |
119 | + { | |
120 | + $this_cell_colspan = " COLSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan']; | |
121 | + $this_cell_rowspan = ""; | |
122 | + for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$i++) | |
123 | + { | |
124 | + $data->sheets[$sheet]['cellsInfo'][$row][$col+$i]['dontprint']=1; | |
125 | + } | |
126 | + } | |
127 | + else if($data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'] >=1) | |
128 | + { | |
129 | + $this_cell_colspan = ""; | |
130 | + $this_cell_rowspan = " ROWSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan']; | |
131 | + for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];$i++) | |
132 | + { | |
133 | + $data->sheets[$sheet]['cellsInfo'][$row+$i][$col]['dontprint']=1; | |
134 | + } | |
135 | + } | |
136 | + else | |
137 | + { | |
138 | + $this_cell_colspan = ""; | |
139 | + $this_cell_rowspan = ""; | |
140 | + } | |
141 | + if(!($data->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint'])) | |
142 | + { | |
143 | + $table_output[$sheet] .= "<TD CLASS='table_data' $this_cell_colspan $this_cell_rowspan> "; | |
144 | + if($force_nobr) | |
145 | + { | |
146 | + $table_output[$sheet] .= "<NOBR>"; | |
147 | + } | |
148 | + $table_output[$sheet] .= nl2br(htmlentities($data->sheets[$sheet]['cells'][$row][$col])); | |
149 | + if($force_nobr) | |
150 | + { | |
151 | + $table_output[$sheet] .= "</NOBR>"; | |
152 | + } | |
153 | + $table_output[$sheet] .= "</TD>"; | |
154 | + } | |
155 | + } | |
156 | + $table_output[$sheet] .= "</TR>"; | |
157 | + } | |
158 | + $table_output[$sheet] .= "</TABLE>"; | |
159 | + $table_output[$sheet] = str_replace("\n","",$table_output[$sheet]); | |
160 | + $table_output[$sheet] = str_replace("\r","",$table_output[$sheet]); | |
161 | + $table_output[$sheet] = str_replace("\t"," ",$table_output[$sheet]); | |
162 | + if($debug) | |
163 | + { | |
164 | + $debug_output = print_r($data->sheets[$sheet],true); | |
165 | + $debug_output = str_replace("\n","\\n",$debug_output); | |
166 | + $debug_output = str_replace("\r","\\r",$debug_output); | |
167 | + $table_output[$sheet] .= "<PRE>$debug_output</PRE>"; | |
168 | + } | |
169 | + echo "sheet_HTML[$sheet] = \"$table_output[$sheet]\";\n"; | |
170 | +} | |
171 | +echo " | |
172 | +function change_tabs(sheet) | |
173 | +{ | |
174 | + //alert('sheet_tab_' + sheet); | |
175 | + for(i=0;i<" , count($data->sheets) , ";i++) | |
176 | + { | |
177 | + document.getElementById('sheet_tab_' + i).className = 'tab_base'; | |
178 | + } | |
179 | + document.getElementById('table_loader_div').innerHTML=sheet_HTML[sheet]; | |
180 | + document.getElementById('sheet_tab_' + sheet).className = 'tab_loaded'; | |
181 | + | |
182 | +} | |
183 | +</SCRIPT>"; | |
184 | +echo " | |
185 | +<TABLE CLASS='table_body' NAME='tab_table'> | |
186 | +<TR>"; | |
187 | +for($sheet=0;$sheet<count($data->sheets);$sheet++) | |
188 | +{ | |
189 | + echo "<TD CLASS='tab_base' ID='sheet_tab_$sheet' ALIGN=CENTER | |
190 | + ONMOUSEDOWN=\"change_tabs($sheet);\">", $data->boundsheets[$sheet]['name'] , "</TD>"; | |
191 | +} | |
192 | + | |
193 | +echo | |
194 | +"<TR>"; | |
195 | +echo "</TABLE> | |
196 | +<DIV ID=table_loader_div></DIV> | |
197 | +<SCRIPT LANGUAGE='JavaScript'> | |
198 | +change_tabs(0); | |
199 | +</SCRIPT>"; | |
200 | +//echo "<IFRAME NAME=table_loader_iframe SRC='about:blank' WIDTH=100 HEIGHT=100></IFRAME>"; | |
201 | +/* | |
202 | +echo "<PRE>"; | |
203 | +print_r($data); | |
204 | +echo "</PRE>"; | |
205 | +*/ | |
206 | +?> | |
0 | 207 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | +define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c); | |
3 | +define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c); | |
4 | +define('ROOT_START_BLOCK_POS', 0x30); | |
5 | +define('BIG_BLOCK_SIZE', 0x200); | |
6 | +define('SMALL_BLOCK_SIZE', 0x40); | |
7 | +define('EXTENSION_BLOCK_POS', 0x44); | |
8 | +define('NUM_EXTENSION_BLOCK_POS', 0x48); | |
9 | +define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80); | |
10 | +define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c); | |
11 | +define('SMALL_BLOCK_THRESHOLD', 0x1000); | |
12 | +// property storage offsets | |
13 | +define('SIZE_OF_NAME_POS', 0x40); | |
14 | +define('TYPE_POS', 0x42); | |
15 | +define('START_BLOCK_POS', 0x74); | |
16 | +define('SIZE_POS', 0x78); | |
17 | +define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1)); | |
18 | + | |
19 | +//echo 'ROOT_START_BLOCK_POS = '.ROOT_START_BLOCK_POS."\n"; | |
20 | + | |
21 | +//echo bin2hex($data[ROOT_START_BLOCK_POS])."\n"; | |
22 | +//echo "a="; | |
23 | +//echo $data[ROOT_START_BLOCK_POS]; | |
24 | +//function log | |
25 | + | |
26 | +function GetInt4d($data, $pos) | |
27 | +{ | |
28 | + $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24); | |
29 | + if ($value>=4294967294) | |
30 | + { | |
31 | + $value=-2; | |
32 | + } | |
33 | + return $value; | |
34 | +} | |
35 | + | |
36 | + | |
37 | +class OLERead { | |
38 | + var $data = ''; | |
39 | + | |
40 | + | |
41 | + function OLERead(){ | |
42 | + | |
43 | + | |
44 | + } | |
45 | + | |
46 | + function read($sFileName){ | |
47 | + | |
48 | + // check if file exist and is readable (Darko Miljanovic) | |
49 | + if(!is_readable($sFileName)) { | |
50 | + $this->error = 1; | |
51 | + return false; | |
52 | + } | |
53 | + | |
54 | + $this->data = @file_get_contents($sFileName); | |
55 | + if (!$this->data) { | |
56 | + $this->error = 1; | |
57 | + return false; | |
58 | + } | |
59 | + //echo IDENTIFIER_OLE; | |
60 | + //echo 'start'; | |
61 | + if (substr($this->data, 0, 8) != IDENTIFIER_OLE) { | |
62 | + $this->error = 1; | |
63 | + return false; | |
64 | + } | |
65 | + $this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS); | |
66 | + $this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS); | |
67 | + $this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS); | |
68 | + $this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS); | |
69 | + $this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS); | |
70 | + | |
71 | + /* | |
72 | + echo $this->numBigBlockDepotBlocks." "; | |
73 | + echo $this->sbdStartBlock." "; | |
74 | + echo $this->rootStartBlock." "; | |
75 | + echo $this->extensionBlock." "; | |
76 | + echo $this->numExtensionBlocks." "; | |
77 | + */ | |
78 | + //echo "sbdStartBlock = $this->sbdStartBlock\n"; | |
79 | + $bigBlockDepotBlocks = array(); | |
80 | + $pos = BIG_BLOCK_DEPOT_BLOCKS_POS; | |
81 | + // echo "pos = $pos"; | |
82 | + $bbdBlocks = $this->numBigBlockDepotBlocks; | |
83 | + | |
84 | + if ($this->numExtensionBlocks != 0) { | |
85 | + $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4; | |
86 | + } | |
87 | + | |
88 | + for ($i = 0; $i < $bbdBlocks; $i++) { | |
89 | + $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos); | |
90 | + $pos += 4; | |
91 | + } | |
92 | + | |
93 | + | |
94 | + for ($j = 0; $j < $this->numExtensionBlocks; $j++) { | |
95 | + $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE; | |
96 | + $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1); | |
97 | + | |
98 | + for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) { | |
99 | + $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos); | |
100 | + $pos += 4; | |
101 | + } | |
102 | + | |
103 | + $bbdBlocks += $blocksToRead; | |
104 | + if ($bbdBlocks < $this->numBigBlockDepotBlocks) { | |
105 | + $this->extensionBlock = GetInt4d($this->data, $pos); | |
106 | + } | |
107 | + } | |
108 | + | |
109 | + // var_dump($bigBlockDepotBlocks); | |
110 | + | |
111 | + // readBigBlockDepot | |
112 | + $pos = 0; | |
113 | + $index = 0; | |
114 | + $this->bigBlockChain = array(); | |
115 | + | |
116 | + for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) { | |
117 | + $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE; | |
118 | + //echo "pos = $pos"; | |
119 | + for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) { | |
120 | + $this->bigBlockChain[$index] = GetInt4d($this->data, $pos); | |
121 | + $pos += 4 ; | |
122 | + $index++; | |
123 | + } | |
124 | + } | |
125 | + | |
126 | + //var_dump($this->bigBlockChain); | |
127 | + //echo '=====2'; | |
128 | + // readSmallBlockDepot(); | |
129 | + $pos = 0; | |
130 | + $index = 0; | |
131 | + $sbdBlock = $this->sbdStartBlock; | |
132 | + $this->smallBlockChain = array(); | |
133 | + | |
134 | + while ($sbdBlock != -2) { | |
135 | + | |
136 | + $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE; | |
137 | + | |
138 | + for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) { | |
139 | + $this->smallBlockChain[$index] = GetInt4d($this->data, $pos); | |
140 | + $pos += 4; | |
141 | + $index++; | |
142 | + } | |
143 | + | |
144 | + $sbdBlock = $this->bigBlockChain[$sbdBlock]; | |
145 | + } | |
146 | + | |
147 | + | |
148 | + // readData(rootStartBlock) | |
149 | + $block = $this->rootStartBlock; | |
150 | + $pos = 0; | |
151 | + $this->entry = $this->__readData($block); | |
152 | + | |
153 | + /* | |
154 | + while ($block != -2) { | |
155 | + $pos = ($block + 1) * BIG_BLOCK_SIZE; | |
156 | + $this->entry = $this->entry.substr($this->data, $pos, BIG_BLOCK_SIZE); | |
157 | + $block = $this->bigBlockChain[$block]; | |
158 | + } | |
159 | + */ | |
160 | + //echo '==='.$this->entry."==="; | |
161 | + $this->__readPropertySets(); | |
162 | + | |
163 | + } | |
164 | + | |
165 | + function __readData($bl) { | |
166 | + $block = $bl; | |
167 | + $pos = 0; | |
168 | + $data = ''; | |
169 | + | |
170 | + while ($block != -2) { | |
171 | + $pos = ($block + 1) * BIG_BLOCK_SIZE; | |
172 | + $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE); | |
173 | + //echo "pos = $pos data=$data\n"; | |
174 | + $block = $this->bigBlockChain[$block]; | |
175 | + } | |
176 | + return $data; | |
177 | + } | |
178 | + | |
179 | + function __readPropertySets(){ | |
180 | + $offset = 0; | |
181 | + //var_dump($this->entry); | |
182 | + while ($offset < strlen($this->entry)) { | |
183 | + $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE); | |
184 | + | |
185 | + $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8); | |
186 | + | |
187 | + $type = ord($d[TYPE_POS]); | |
188 | + //$maxBlock = strlen($d) / BIG_BLOCK_SIZE - 1; | |
189 | + | |
190 | + $startBlock = GetInt4d($d, START_BLOCK_POS); | |
191 | + $size = GetInt4d($d, SIZE_POS); | |
192 | + | |
193 | + $name = ''; | |
194 | + for ($i = 0; $i < $nameSize ; $i++) { | |
195 | + $name .= $d[$i]; | |
196 | + } | |
197 | + | |
198 | + $name = str_replace("\x00", "", $name); | |
199 | + | |
200 | + $this->props[] = array ( | |
201 | + 'name' => $name, | |
202 | + 'type' => $type, | |
203 | + 'startBlock' => $startBlock, | |
204 | + 'size' => $size); | |
205 | + | |
206 | + if (($name == "Workbook") || ($name == "Book")) { | |
207 | + $this->wrkbook = count($this->props) - 1; | |
208 | + } | |
209 | + | |
210 | + if ($name == "Root Entry") { | |
211 | + $this->rootentry = count($this->props) - 1; | |
212 | + } | |
213 | + | |
214 | + //echo "name ==$name=\n"; | |
215 | + | |
216 | + | |
217 | + $offset += PROPERTY_STORAGE_BLOCK_SIZE; | |
218 | + } | |
219 | + | |
220 | + } | |
221 | + | |
222 | + | |
223 | + function getWorkBook(){ | |
224 | + if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){ | |
225 | +// getSmallBlockStream(PropertyStorage ps) | |
226 | + | |
227 | + $rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']); | |
228 | + | |
229 | + $streamData = ''; | |
230 | + $block = $this->props[$this->wrkbook]['startBlock']; | |
231 | + //$count = 0; | |
232 | + $pos = 0; | |
233 | + while ($block != -2) { | |
234 | + $pos = $block * SMALL_BLOCK_SIZE; | |
235 | + $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE); | |
236 | + | |
237 | + $block = $this->smallBlockChain[$block]; | |
238 | + } | |
239 | + | |
240 | + return $streamData; | |
241 | + | |
242 | + | |
243 | + }else{ | |
244 | + | |
245 | + $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE; | |
246 | + if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) { | |
247 | + $numBlocks++; | |
248 | + } | |
249 | + | |
250 | + if ($numBlocks == 0) return ''; | |
251 | + | |
252 | + //echo "numBlocks = $numBlocks\n"; | |
253 | + //byte[] streamData = new byte[numBlocks * BIG_BLOCK_SIZE]; | |
254 | + //print_r($this->wrkbook); | |
255 | + $streamData = ''; | |
256 | + $block = $this->props[$this->wrkbook]['startBlock']; | |
257 | + //$count = 0; | |
258 | + $pos = 0; | |
259 | + //echo "block = $block"; | |
260 | + while ($block != -2) { | |
261 | + $pos = ($block + 1) * BIG_BLOCK_SIZE; | |
262 | + $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE); | |
263 | + $block = $this->bigBlockChain[$block]; | |
264 | + } | |
265 | + //echo 'stream'.$streamData; | |
266 | + return $streamData; | |
267 | + } | |
268 | + } | |
269 | + | |
270 | +} | |
271 | +?> | |
0 | 272 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ | |
3 | + | |
4 | +/** | |
5 | +* A class for reading Microsoft Excel Spreadsheets. | |
6 | +* | |
7 | +* Originally developed by Vadim Tkachenko under the name PHPExcelReader. | |
8 | +* (http://sourceforge.net/projects/phpexcelreader) | |
9 | +* Based on the Java version by Andy Khan (http://www.andykhan.com). Now | |
10 | +* maintained by David Sanders. Reads only Biff 7 and Biff 8 formats. | |
11 | +* | |
12 | +* PHP versions 4 and 5 | |
13 | +* | |
14 | +* LICENSE: This source file is subject to version 3.0 of the PHP license | |
15 | +* that is available through the world-wide-web at the following URI: | |
16 | +* http://www.php.net/license/3_0.txt. If you did not receive a copy of | |
17 | +* the PHP License and are unable to obtain it through the web, please | |
18 | +* send a note to license@php.net so we can mail you a copy immediately. | |
19 | +* | |
20 | +* @category Spreadsheet | |
21 | +* @package Spreadsheet_Excel_Reader | |
22 | +* @author Vadim Tkachenko <vt@apachephp.com> | |
23 | +* @license http://www.php.net/license/3_0.txt PHP License 3.0 | |
24 | +* @version CVS: $Id: reader.php 19 2007-03-13 12:42:41Z shangxiao $ | |
25 | +* @link http://pear.php.net/package/Spreadsheet_Excel_Reader | |
26 | +* @see OLE, Spreadsheet_Excel_Writer | |
27 | +*/ | |
28 | + | |
29 | + | |
30 | +//require_once 'PEAR.php'; | |
31 | +//require_once 'Spreadsheet/Excel/Reader/OLERead.php'; | |
32 | +require_once 'oleread.inc'; | |
33 | +//require_once 'OLE.php'; | |
34 | + | |
35 | +define('SPREADSHEET_EXCEL_READER_BIFF8', 0x600); | |
36 | +define('SPREADSHEET_EXCEL_READER_BIFF7', 0x500); | |
37 | +define('SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS', 0x5); | |
38 | +define('SPREADSHEET_EXCEL_READER_WORKSHEET', 0x10); | |
39 | + | |
40 | +define('SPREADSHEET_EXCEL_READER_TYPE_BOF', 0x809); | |
41 | +define('SPREADSHEET_EXCEL_READER_TYPE_EOF', 0x0a); | |
42 | +define('SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET', 0x85); | |
43 | +define('SPREADSHEET_EXCEL_READER_TYPE_DIMENSION', 0x200); | |
44 | +define('SPREADSHEET_EXCEL_READER_TYPE_ROW', 0x208); | |
45 | +define('SPREADSHEET_EXCEL_READER_TYPE_DBCELL', 0xd7); | |
46 | +define('SPREADSHEET_EXCEL_READER_TYPE_FILEPASS', 0x2f); | |
47 | +define('SPREADSHEET_EXCEL_READER_TYPE_NOTE', 0x1c); | |
48 | +define('SPREADSHEET_EXCEL_READER_TYPE_TXO', 0x1b6); | |
49 | +define('SPREADSHEET_EXCEL_READER_TYPE_RK', 0x7e); | |
50 | +define('SPREADSHEET_EXCEL_READER_TYPE_RK2', 0x27e); | |
51 | +define('SPREADSHEET_EXCEL_READER_TYPE_MULRK', 0xbd); | |
52 | +define('SPREADSHEET_EXCEL_READER_TYPE_MULBLANK', 0xbe); | |
53 | +define('SPREADSHEET_EXCEL_READER_TYPE_INDEX', 0x20b); | |
54 | +define('SPREADSHEET_EXCEL_READER_TYPE_SST', 0xfc); | |
55 | +define('SPREADSHEET_EXCEL_READER_TYPE_EXTSST', 0xff); | |
56 | +define('SPREADSHEET_EXCEL_READER_TYPE_CONTINUE', 0x3c); | |
57 | +define('SPREADSHEET_EXCEL_READER_TYPE_LABEL', 0x204); | |
58 | +define('SPREADSHEET_EXCEL_READER_TYPE_LABELSST', 0xfd); | |
59 | +define('SPREADSHEET_EXCEL_READER_TYPE_NUMBER', 0x203); | |
60 | +define('SPREADSHEET_EXCEL_READER_TYPE_NAME', 0x18); | |
61 | +define('SPREADSHEET_EXCEL_READER_TYPE_ARRAY', 0x221); | |
62 | +define('SPREADSHEET_EXCEL_READER_TYPE_STRING', 0x207); | |
63 | +define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA', 0x406); | |
64 | +define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA2', 0x6); | |
65 | +define('SPREADSHEET_EXCEL_READER_TYPE_FORMAT', 0x41e); | |
66 | +define('SPREADSHEET_EXCEL_READER_TYPE_XF', 0xe0); | |
67 | +define('SPREADSHEET_EXCEL_READER_TYPE_BOOLERR', 0x205); | |
68 | +define('SPREADSHEET_EXCEL_READER_TYPE_UNKNOWN', 0xffff); | |
69 | +define('SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR', 0x22); | |
70 | +define('SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS', 0xE5); | |
71 | + | |
72 | +define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS' , 25569); | |
73 | +define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904', 24107); | |
74 | +define('SPREADSHEET_EXCEL_READER_MSINADAY', 86400); | |
75 | +//define('SPREADSHEET_EXCEL_READER_MSINADAY', 24 * 60 * 60); | |
76 | + | |
77 | +//define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT', "%.2f"); | |
78 | +define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT', "%s"); | |
79 | + | |
80 | + | |
81 | +/* | |
82 | +* Place includes, constant defines and $_GLOBAL settings here. | |
83 | +* Make sure they have appropriate docblocks to avoid phpDocumentor | |
84 | +* construing they are documented by the page-level docblock. | |
85 | +*/ | |
86 | + | |
87 | +/** | |
88 | +* A class for reading Microsoft Excel Spreadsheets. | |
89 | +* | |
90 | +* Originally developed by Vadim Tkachenko under the name PHPExcelReader. | |
91 | +* (http://sourceforge.net/projects/phpexcelreader) | |
92 | +* Based on the Java version by Andy Khan (http://www.andykhan.com). Now | |
93 | +* maintained by David Sanders. Reads only Biff 7 and Biff 8 formats. | |
94 | +* | |
95 | +* @category Spreadsheet | |
96 | +* @package Spreadsheet_Excel_Reader | |
97 | +* @author Vadim Tkachenko <vt@phpapache.com> | |
98 | +* @copyright 1997-2005 The PHP Group | |
99 | +* @license http://www.php.net/license/3_0.txt PHP License 3.0 | |
100 | +* @version Release: @package_version@ | |
101 | +* @link http://pear.php.net/package/PackageName | |
102 | +* @see OLE, Spreadsheet_Excel_Writer | |
103 | +*/ | |
104 | +class Spreadsheet_Excel_Reader | |
105 | +{ | |
106 | + /** | |
107 | + * Array of worksheets found | |
108 | + * | |
109 | + * @var array | |
110 | + * @access public | |
111 | + */ | |
112 | + var $boundsheets = array(); | |
113 | + | |
114 | + /** | |
115 | + * Array of format records found | |
116 | + * | |
117 | + * @var array | |
118 | + * @access public | |
119 | + */ | |
120 | + var $formatRecords = array(); | |
121 | + | |
122 | + /** | |
123 | + * todo | |
124 | + * | |
125 | + * @var array | |
126 | + * @access public | |
127 | + */ | |
128 | + var $sst = array(); | |
129 | + | |
130 | + /** | |
131 | + * Array of worksheets | |
132 | + * | |
133 | + * The data is stored in 'cells' and the meta-data is stored in an array | |
134 | + * called 'cellsInfo' | |
135 | + * | |
136 | + * Example: | |
137 | + * | |
138 | + * $sheets --> 'cells' --> row --> column --> Interpreted value | |
139 | + * --> 'cellsInfo' --> row --> column --> 'type' - Can be 'date', 'number', or 'unknown' | |
140 | + * --> 'raw' - The raw data that Excel stores for that data cell | |
141 | + * | |
142 | + * @var array | |
143 | + * @access public | |
144 | + */ | |
145 | + var $sheets = array(); | |
146 | + | |
147 | + /** | |
148 | + * The data returned by OLE | |
149 | + * | |
150 | + * @var string | |
151 | + * @access public | |
152 | + */ | |
153 | + var $data; | |
154 | + | |
155 | + /** | |
156 | + * OLE object for reading the file | |
157 | + * | |
158 | + * @var OLE object | |
159 | + * @access private | |
160 | + */ | |
161 | + var $_ole; | |
162 | + | |
163 | + /** | |
164 | + * Default encoding | |
165 | + * | |
166 | + * @var string | |
167 | + * @access private | |
168 | + */ | |
169 | + var $_defaultEncoding; | |
170 | + | |
171 | + /** | |
172 | + * Default number format | |
173 | + * | |
174 | + * @var integer | |
175 | + * @access private | |
176 | + */ | |
177 | + var $_defaultFormat = SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT; | |
178 | + | |
179 | + /** | |
180 | + * todo | |
181 | + * List of formats to use for each column | |
182 | + * | |
183 | + * @var array | |
184 | + * @access private | |
185 | + */ | |
186 | + var $_columnsFormat = array(); | |
187 | + | |
188 | + /** | |
189 | + * todo | |
190 | + * | |
191 | + * @var integer | |
192 | + * @access private | |
193 | + */ | |
194 | + var $_rowoffset = 1; | |
195 | + | |
196 | + /** | |
197 | + * todo | |
198 | + * | |
199 | + * @var integer | |
200 | + * @access private | |
201 | + */ | |
202 | + var $_coloffset = 1; | |
203 | + | |
204 | + /** | |
205 | + * List of default date formats used by Excel | |
206 | + * | |
207 | + * @var array | |
208 | + * @access public | |
209 | + */ | |
210 | + var $dateFormats = array ( | |
211 | + 0xe => "d/m/Y", | |
212 | + 0xf => "d-M-Y", | |
213 | + 0x10 => "d-M", | |
214 | + 0x11 => "M-Y", | |
215 | + 0x12 => "h:i a", | |
216 | + 0x13 => "h:i:s a", | |
217 | + 0x14 => "H:i", | |
218 | + 0x15 => "H:i:s", | |
219 | + 0x16 => "d/m/Y H:i", | |
220 | + 0x2d => "i:s", | |
221 | + 0x2e => "H:i:s", | |
222 | + 0x2f => "i:s.S"); | |
223 | + | |
224 | + /** | |
225 | + * Default number formats used by Excel | |
226 | + * | |
227 | + * @var array | |
228 | + * @access public | |
229 | + */ | |
230 | + var $numberFormats = array( | |
231 | + 0x1 => "%1.0f", // "0" | |
232 | + 0x2 => "%1.2f", // "0.00", | |
233 | + 0x3 => "%1.0f", //"#,##0", | |
234 | + 0x4 => "%1.2f", //"#,##0.00", | |
235 | + 0x5 => "%1.0f", /*"$#,##0;($#,##0)",*/ | |
236 | + 0x6 => '$%1.0f', /*"$#,##0;($#,##0)",*/ | |
237 | + 0x7 => '$%1.2f', //"$#,##0.00;($#,##0.00)", | |
238 | + 0x8 => '$%1.2f', //"$#,##0.00;($#,##0.00)", | |
239 | + 0x9 => '%1.0f%%', // "0%" | |
240 | + 0xa => '%1.2f%%', // "0.00%" | |
241 | + 0xb => '%1.2f', // 0.00E00", | |
242 | + 0x25 => '%1.0f', // "#,##0;(#,##0)", | |
243 | + 0x26 => '%1.0f', //"#,##0;(#,##0)", | |
244 | + 0x27 => '%1.2f', //"#,##0.00;(#,##0.00)", | |
245 | + 0x28 => '%1.2f', //"#,##0.00;(#,##0.00)", | |
246 | + 0x29 => '%1.0f', //"#,##0;(#,##0)", | |
247 | + 0x2a => '$%1.0f', //"$#,##0;($#,##0)", | |
248 | + 0x2b => '%1.2f', //"#,##0.00;(#,##0.00)", | |
249 | + 0x2c => '$%1.2f', //"$#,##0.00;($#,##0.00)", | |
250 | + 0x30 => '%1.0f'); //"##0.0E0"; | |
251 | + | |
252 | + // }}} | |
253 | + // {{{ Spreadsheet_Excel_Reader() | |
254 | + | |
255 | + /** | |
256 | + * Constructor | |
257 | + * | |
258 | + * Some basic initialisation | |
259 | + */ | |
260 | + function Spreadsheet_Excel_Reader() | |
261 | + { | |
262 | + $this->_ole =& new OLERead(); | |
263 | + $this->setUTFEncoder('iconv'); | |
264 | + } | |
265 | + | |
266 | + // }}} | |
267 | + // {{{ setOutputEncoding() | |
268 | + | |
269 | + /** | |
270 | + * Set the encoding method | |
271 | + * | |
272 | + * @param string Encoding to use | |
273 | + * @access public | |
274 | + */ | |
275 | + function setOutputEncoding($encoding) | |
276 | + { | |
277 | + $this->_defaultEncoding = $encoding; | |
278 | + } | |
279 | + | |
280 | + // }}} | |
281 | + // {{{ setUTFEncoder() | |
282 | + | |
283 | + /** | |
284 | + * $encoder = 'iconv' or 'mb' | |
285 | + * set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding | |
286 | + * set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding | |
287 | + * | |
288 | + * @access public | |
289 | + * @param string Encoding type to use. Either 'iconv' or 'mb' | |
290 | + */ | |
291 | + function setUTFEncoder($encoder = 'iconv') | |
292 | + { | |
293 | + $this->_encoderFunction = ''; | |
294 | + | |
295 | + if ($encoder == 'iconv') { | |
296 | + $this->_encoderFunction = function_exists('iconv') ? 'iconv' : ''; | |
297 | + } elseif ($encoder == 'mb') { | |
298 | + $this->_encoderFunction = function_exists('mb_convert_encoding') ? | |
299 | + 'mb_convert_encoding' : | |
300 | + ''; | |
301 | + } | |
302 | + } | |
303 | + | |
304 | + // }}} | |
305 | + // {{{ setRowColOffset() | |
306 | + | |
307 | + /** | |
308 | + * todo | |
309 | + * | |
310 | + * @access public | |
311 | + * @param offset | |
312 | + */ | |
313 | + function setRowColOffset($iOffset) | |
314 | + { | |
315 | + $this->_rowoffset = $iOffset; | |
316 | + $this->_coloffset = $iOffset; | |
317 | + } | |
318 | + | |
319 | + // }}} | |
320 | + // {{{ setDefaultFormat() | |
321 | + | |
322 | + /** | |
323 | + * Set the default number format | |
324 | + * | |
325 | + * @access public | |
326 | + * @param Default format | |
327 | + */ | |
328 | + function setDefaultFormat($sFormat) | |
329 | + { | |
330 | + $this->_defaultFormat = $sFormat; | |
331 | + } | |
332 | + | |
333 | + // }}} | |
334 | + // {{{ setColumnFormat() | |
335 | + | |
336 | + /** | |
337 | + * Force a column to use a certain format | |
338 | + * | |
339 | + * @access public | |
340 | + * @param integer Column number | |
341 | + * @param string Format | |
342 | + */ | |
343 | + function setColumnFormat($column, $sFormat) | |
344 | + { | |
345 | + $this->_columnsFormat[$column] = $sFormat; | |
346 | + } | |
347 | + | |
348 | + | |
349 | + // }}} | |
350 | + // {{{ read() | |
351 | + | |
352 | + /** | |
353 | + * Read the spreadsheet file using OLE, then parse | |
354 | + * | |
355 | + * @access public | |
356 | + * @param filename | |
357 | + * @todo return a valid value | |
358 | + */ | |
359 | + function read($sFileName) | |
360 | + { | |
361 | + /* | |
362 | + require_once 'OLE.php'; | |
363 | + $ole = new OLE(); | |
364 | + $ole->read($sFileName); | |
365 | + | |
366 | + foreach ($ole->_list as $i => $pps) { | |
367 | + if (($pps->Name == 'Workbook' || $pps->Name == 'Book') && | |
368 | + $pps->Size >= SMALL_BLOCK_THRESHOLD) { | |
369 | + | |
370 | + $this->data = $ole->getData($i, 0, $ole->getDataLength($i)); | |
371 | + } elseif ($pps->Name == 'Root Entry') { | |
372 | + $this->data = $ole->getData($i, 0, $ole->getDataLength($i)); | |
373 | + } | |
374 | + //var_dump(strlen($ole->getData($i, 0, $ole->getDataLength($i))), $pps->Name, md5($this->data), $ole->getDataLength($i)); | |
375 | + } | |
376 | +//exit; | |
377 | + $this->_parse(); | |
378 | + | |
379 | + return sizeof($this->sheets) > 0; | |
380 | + */ | |
381 | + | |
382 | + $res = $this->_ole->read($sFileName); | |
383 | + | |
384 | + // oops, something goes wrong (Darko Miljanovic) | |
385 | + if($res === false) { | |
386 | + // check error code | |
387 | + if($this->_ole->error == 1) { | |
388 | + // bad file | |
389 | + die('The filename ' . $sFileName . ' is not readable'); | |
390 | + } | |
391 | + // check other error codes here (eg bad fileformat, etc...) | |
392 | + } | |
393 | + | |
394 | + $this->data = $this->_ole->getWorkBook(); | |
395 | + | |
396 | + | |
397 | + /* | |
398 | + $res = $this->_ole->read($sFileName); | |
399 | + | |
400 | + if ($this->isError($res)) { | |
401 | +// var_dump($res); | |
402 | + return $this->raiseError($res); | |
403 | + } | |
404 | + | |
405 | + $total = $this->_ole->ppsTotal(); | |
406 | + for ($i = 0; $i < $total; $i++) { | |
407 | + if ($this->_ole->isFile($i)) { | |
408 | + $type = unpack("v", $this->_ole->getData($i, 0, 2)); | |
409 | + if ($type[''] == 0x0809) { // check if it's a BIFF stream | |
410 | + $this->_index = $i; | |
411 | + $this->data = $this->_ole->getData($i, 0, $this->_ole->getDataLength($i)); | |
412 | + break; | |
413 | + } | |
414 | + } | |
415 | + } | |
416 | + | |
417 | + if ($this->_index === null) { | |
418 | + return $this->raiseError("$file doesn't seem to be an Excel file"); | |
419 | + } | |
420 | + | |
421 | + */ | |
422 | + | |
423 | + //echo "data =".$this->data; | |
424 | + //$this->readRecords(); | |
425 | + $this->_parse(); | |
426 | + } | |
427 | + | |
428 | + | |
429 | + // }}} | |
430 | + // {{{ _parse() | |
431 | + | |
432 | + /** | |
433 | + * Parse a workbook | |
434 | + * | |
435 | + * @access private | |
436 | + * @return bool | |
437 | + */ | |
438 | + function _parse() | |
439 | + { | |
440 | + $pos = 0; | |
441 | + | |
442 | + $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8; | |
443 | + $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8; | |
444 | + | |
445 | + $version = ord($this->data[$pos + 4]) | ord($this->data[$pos + 5])<<8; | |
446 | + $substreamType = ord($this->data[$pos + 6]) | ord($this->data[$pos + 7])<<8; | |
447 | + //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n"; | |
448 | + | |
449 | + if (($version != SPREADSHEET_EXCEL_READER_BIFF8) && | |
450 | + ($version != SPREADSHEET_EXCEL_READER_BIFF7)) { | |
451 | + return false; | |
452 | + } | |
453 | + | |
454 | + if ($substreamType != SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS){ | |
455 | + return false; | |
456 | + } | |
457 | + | |
458 | + //print_r($rec); | |
459 | + $pos += $length + 4; | |
460 | + | |
461 | + $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8; | |
462 | + $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8; | |
463 | + | |
464 | + while ($code != SPREADSHEET_EXCEL_READER_TYPE_EOF) { | |
465 | + switch ($code) { | |
466 | + case SPREADSHEET_EXCEL_READER_TYPE_SST: | |
467 | + //echo "Type_SST\n"; | |
468 | + $spos = $pos + 4; | |
469 | + $limitpos = $spos + $length; | |
470 | + $uniqueStrings = $this->_GetInt4d($this->data, $spos+4); | |
471 | + $spos += 8; | |
472 | + for ($i = 0; $i < $uniqueStrings; $i++) { | |
473 | + // Read in the number of characters | |
474 | + if ($spos == $limitpos) { | |
475 | + $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
476 | + $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
477 | + if ($opcode != 0x3c) { | |
478 | + return -1; | |
479 | + } | |
480 | + $spos += 4; | |
481 | + $limitpos = $spos + $conlength; | |
482 | + } | |
483 | + $numChars = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8); | |
484 | + //echo "i = $i pos = $pos numChars = $numChars "; | |
485 | + $spos += 2; | |
486 | + $optionFlags = ord($this->data[$spos]); | |
487 | + $spos++; | |
488 | + $asciiEncoding = (($optionFlags & 0x01) == 0) ; | |
489 | + $extendedString = ( ($optionFlags & 0x04) != 0); | |
490 | + | |
491 | + // See if string contains formatting information | |
492 | + $richString = ( ($optionFlags & 0x08) != 0); | |
493 | + | |
494 | + if ($richString) { | |
495 | + // Read in the crun | |
496 | + $formattingRuns = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8); | |
497 | + $spos += 2; | |
498 | + } | |
499 | + | |
500 | + if ($extendedString) { | |
501 | + // Read in cchExtRst | |
502 | + $extendedRunLength = $this->_GetInt4d($this->data, $spos); | |
503 | + $spos += 4; | |
504 | + } | |
505 | + | |
506 | + $len = ($asciiEncoding)? $numChars : $numChars*2; | |
507 | + if ($spos + $len < $limitpos) { | |
508 | + $retstr = substr($this->data, $spos, $len); | |
509 | + $spos += $len; | |
510 | + }else{ | |
511 | + // found countinue | |
512 | + $retstr = substr($this->data, $spos, $limitpos - $spos); | |
513 | + $bytesRead = $limitpos - $spos; | |
514 | + $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2)); | |
515 | + $spos = $limitpos; | |
516 | + | |
517 | + while ($charsLeft > 0){ | |
518 | + $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
519 | + $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
520 | + if ($opcode != 0x3c) { | |
521 | + return -1; | |
522 | + } | |
523 | + $spos += 4; | |
524 | + $limitpos = $spos + $conlength; | |
525 | + $option = ord($this->data[$spos]); | |
526 | + $spos += 1; | |
527 | + if ($asciiEncoding && ($option == 0)) { | |
528 | + $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength); | |
529 | + $retstr .= substr($this->data, $spos, $len); | |
530 | + $charsLeft -= $len; | |
531 | + $asciiEncoding = true; | |
532 | + }elseif (!$asciiEncoding && ($option != 0)){ | |
533 | + $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength); | |
534 | + $retstr .= substr($this->data, $spos, $len); | |
535 | + $charsLeft -= $len/2; | |
536 | + $asciiEncoding = false; | |
537 | + }elseif (!$asciiEncoding && ($option == 0)) { | |
538 | + // Bummer - the string starts off as Unicode, but after the | |
539 | + // continuation it is in straightforward ASCII encoding | |
540 | + $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength); | |
541 | + for ($j = 0; $j < $len; $j++) { | |
542 | + $retstr .= $this->data[$spos + $j].chr(0); | |
543 | + } | |
544 | + $charsLeft -= $len; | |
545 | + $asciiEncoding = false; | |
546 | + }else{ | |
547 | + $newstr = ''; | |
548 | + for ($j = 0; $j < strlen($retstr); $j++) { | |
549 | + $newstr = $retstr[$j].chr(0); | |
550 | + } | |
551 | + $retstr = $newstr; | |
552 | + $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength); | |
553 | + $retstr .= substr($this->data, $spos, $len); | |
554 | + $charsLeft -= $len/2; | |
555 | + $asciiEncoding = false; | |
556 | + //echo "Izavrat\n"; | |
557 | + } | |
558 | + $spos += $len; | |
559 | + | |
560 | + } | |
561 | + } | |
562 | + $retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr); | |
563 | +// echo "Str $i = $retstr\n"; | |
564 | + if ($richString){ | |
565 | + $spos += 4 * $formattingRuns; | |
566 | + } | |
567 | + | |
568 | + // For extended strings, skip over the extended string data | |
569 | + if ($extendedString) { | |
570 | + $spos += $extendedRunLength; | |
571 | + } | |
572 | + //if ($retstr == 'Derby'){ | |
573 | + // echo "bb\n"; | |
574 | + //} | |
575 | + $this->sst[]=$retstr; | |
576 | + } | |
577 | + /*$continueRecords = array(); | |
578 | + while ($this->getNextCode() == Type_CONTINUE) { | |
579 | + $continueRecords[] = &$this->nextRecord(); | |
580 | + } | |
581 | + //echo " 1 Type_SST\n"; | |
582 | + $this->shareStrings = new SSTRecord($r, $continueRecords); | |
583 | + //print_r($this->shareStrings->strings); | |
584 | + */ | |
585 | + // echo 'SST read: '.($time_end-$time_start)."\n"; | |
586 | + break; | |
587 | + | |
588 | + case SPREADSHEET_EXCEL_READER_TYPE_FILEPASS: | |
589 | + return false; | |
590 | + break; | |
591 | + case SPREADSHEET_EXCEL_READER_TYPE_NAME: | |
592 | + //echo "Type_NAME\n"; | |
593 | + break; | |
594 | + case SPREADSHEET_EXCEL_READER_TYPE_FORMAT: | |
595 | + $indexCode = ord($this->data[$pos+4]) | ord($this->data[$pos+5]) << 8; | |
596 | + | |
597 | + if ($version == SPREADSHEET_EXCEL_READER_BIFF8) { | |
598 | + $numchars = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8; | |
599 | + if (ord($this->data[$pos+8]) == 0){ | |
600 | + $formatString = substr($this->data, $pos+9, $numchars); | |
601 | + } else { | |
602 | + $formatString = substr($this->data, $pos+9, $numchars*2); | |
603 | + } | |
604 | + } else { | |
605 | + $numchars = ord($this->data[$pos+6]); | |
606 | + $formatString = substr($this->data, $pos+7, $numchars*2); | |
607 | + } | |
608 | + | |
609 | + $this->formatRecords[$indexCode] = $formatString; | |
610 | + // echo "Type.FORMAT\n"; | |
611 | + break; | |
612 | + case SPREADSHEET_EXCEL_READER_TYPE_XF: | |
613 | + //global $dateFormats, $numberFormats; | |
614 | + $indexCode = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8; | |
615 | + //echo "\nType.XF ".count($this->formatRecords['xfrecords'])." $indexCode "; | |
616 | + if (array_key_exists($indexCode, $this->dateFormats)) { | |
617 | + //echo "isdate ".$dateFormats[$indexCode]; | |
618 | + $this->formatRecords['xfrecords'][] = array( | |
619 | + 'type' => 'date', | |
620 | + 'format' => $this->dateFormats[$indexCode] | |
621 | + ); | |
622 | + }elseif (array_key_exists($indexCode, $this->numberFormats)) { | |
623 | + //echo "isnumber ".$this->numberFormats[$indexCode]; | |
624 | + $this->formatRecords['xfrecords'][] = array( | |
625 | + 'type' => 'number', | |
626 | + 'format' => $this->numberFormats[$indexCode] | |
627 | + ); | |
628 | + }else{ | |
629 | + $isdate = FALSE; | |
630 | + if ($indexCode > 0){ | |
631 | + if (isset($this->formatRecords[$indexCode])) | |
632 | + $formatstr = $this->formatRecords[$indexCode]; | |
633 | + //echo '.other.'; | |
634 | + //echo "\ndate-time=$formatstr=\n"; | |
635 | + if ($formatstr) | |
636 | + if (preg_match("/[^hmsday\/\-:\s]/i", $formatstr) == 0) { // found day and time format | |
637 | + $isdate = TRUE; | |
638 | + $formatstr = str_replace('mm', 'i', $formatstr); | |
639 | + $formatstr = str_replace('h', 'H', $formatstr); | |
640 | + //echo "\ndate-time $formatstr \n"; | |
641 | + } | |
642 | + } | |
643 | + | |
644 | + if ($isdate){ | |
645 | + $this->formatRecords['xfrecords'][] = array( | |
646 | + 'type' => 'date', | |
647 | + 'format' => $formatstr, | |
648 | + ); | |
649 | + }else{ | |
650 | + $this->formatRecords['xfrecords'][] = array( | |
651 | + 'type' => 'other', | |
652 | + 'format' => '', | |
653 | + 'code' => $indexCode | |
654 | + ); | |
655 | + } | |
656 | + } | |
657 | + //echo "\n"; | |
658 | + break; | |
659 | + case SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR: | |
660 | + //echo "Type.NINETEENFOUR\n"; | |
661 | + $this->nineteenFour = (ord($this->data[$pos+4]) == 1); | |
662 | + break; | |
663 | + case SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET: | |
664 | + //echo "Type.BOUNDSHEET\n"; | |
665 | + $rec_offset = $this->_GetInt4d($this->data, $pos+4); | |
666 | + $rec_typeFlag = ord($this->data[$pos+8]); | |
667 | + $rec_visibilityFlag = ord($this->data[$pos+9]); | |
668 | + $rec_length = ord($this->data[$pos+10]); | |
669 | + | |
670 | + if ($version == SPREADSHEET_EXCEL_READER_BIFF8){ | |
671 | + $chartype = ord($this->data[$pos+11]); | |
672 | + if ($chartype == 0){ | |
673 | + $rec_name = substr($this->data, $pos+12, $rec_length); | |
674 | + } else { | |
675 | + $rec_name = $this->_encodeUTF16(substr($this->data, $pos+12, $rec_length*2)); | |
676 | + } | |
677 | + }elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){ | |
678 | + $rec_name = substr($this->data, $pos+11, $rec_length); | |
679 | + } | |
680 | + $this->boundsheets[] = array('name'=>$rec_name, | |
681 | + 'offset'=>$rec_offset); | |
682 | + | |
683 | + break; | |
684 | + | |
685 | + } | |
686 | + | |
687 | + //echo "Code = ".base_convert($r['code'],10,16)."\n"; | |
688 | + $pos += $length + 4; | |
689 | + $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8; | |
690 | + $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8; | |
691 | + | |
692 | + //$r = &$this->nextRecord(); | |
693 | + //echo "1 Code = ".base_convert($r['code'],10,16)."\n"; | |
694 | + } | |
695 | + | |
696 | + foreach ($this->boundsheets as $key=>$val){ | |
697 | + $this->sn = $key; | |
698 | + $this->_parsesheet($val['offset']); | |
699 | + } | |
700 | + return true; | |
701 | + | |
702 | + } | |
703 | + | |
704 | + /** | |
705 | + * Parse a worksheet | |
706 | + * | |
707 | + * @access private | |
708 | + * @param todo | |
709 | + * @todo fix return codes | |
710 | + */ | |
711 | + function _parsesheet($spos) | |
712 | + { | |
713 | + $cont = true; | |
714 | + // read BOF | |
715 | + $code = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
716 | + $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
717 | + | |
718 | + $version = ord($this->data[$spos + 4]) | ord($this->data[$spos + 5])<<8; | |
719 | + $substreamType = ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8; | |
720 | + | |
721 | + if (($version != SPREADSHEET_EXCEL_READER_BIFF8) && ($version != SPREADSHEET_EXCEL_READER_BIFF7)) { | |
722 | + return -1; | |
723 | + } | |
724 | + | |
725 | + if ($substreamType != SPREADSHEET_EXCEL_READER_WORKSHEET){ | |
726 | + return -2; | |
727 | + } | |
728 | + //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n"; | |
729 | + $spos += $length + 4; | |
730 | + //var_dump($this->formatRecords); | |
731 | + //echo "code $code $length"; | |
732 | + while($cont) { | |
733 | + //echo "mem= ".memory_get_usage()."\n"; | |
734 | +// $r = &$this->file->nextRecord(); | |
735 | + $lowcode = ord($this->data[$spos]); | |
736 | + if ($lowcode == SPREADSHEET_EXCEL_READER_TYPE_EOF) break; | |
737 | + $code = $lowcode | ord($this->data[$spos+1])<<8; | |
738 | + $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
739 | + $spos += 4; | |
740 | + $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1; | |
741 | + $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1; | |
742 | + //echo "Code=".base_convert($code,10,16)." $code\n"; | |
743 | + unset($this->rectype); | |
744 | + $this->multiplier = 1; // need for format with % | |
745 | + switch ($code) { | |
746 | + case SPREADSHEET_EXCEL_READER_TYPE_DIMENSION: | |
747 | + //echo 'Type_DIMENSION '; | |
748 | + if (!isset($this->numRows)) { | |
749 | + if (($length == 10) || ($version == SPREADSHEET_EXCEL_READER_BIFF7)){ | |
750 | + $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+2]) | ord($this->data[$spos+3]) << 8; | |
751 | + $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+6]) | ord($this->data[$spos+7]) << 8; | |
752 | + } else { | |
753 | + $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8; | |
754 | + $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+10]) | ord($this->data[$spos+11]) << 8; | |
755 | + } | |
756 | + } | |
757 | + //echo 'numRows '.$this->numRows.' '.$this->numCols."\n"; | |
758 | + break; | |
759 | + case SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS: | |
760 | + $cellRanges = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
761 | + for ($i = 0; $i < $cellRanges; $i++) { | |
762 | + $fr = ord($this->data[$spos + 8*$i + 2]) | ord($this->data[$spos + 8*$i + 3])<<8; | |
763 | + $lr = ord($this->data[$spos + 8*$i + 4]) | ord($this->data[$spos + 8*$i + 5])<<8; | |
764 | + $fc = ord($this->data[$spos + 8*$i + 6]) | ord($this->data[$spos + 8*$i + 7])<<8; | |
765 | + $lc = ord($this->data[$spos + 8*$i + 8]) | ord($this->data[$spos + 8*$i + 9])<<8; | |
766 | + //$this->sheets[$this->sn]['mergedCells'][] = array($fr + 1, $fc + 1, $lr + 1, $lc + 1); | |
767 | + if ($lr - $fr > 0) { | |
768 | + $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1; | |
769 | + } | |
770 | + if ($lc - $fc > 0) { | |
771 | + $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1; | |
772 | + } | |
773 | + } | |
774 | + //echo "Merged Cells $cellRanges $lr $fr $lc $fc\n"; | |
775 | + break; | |
776 | + case SPREADSHEET_EXCEL_READER_TYPE_RK: | |
777 | + case SPREADSHEET_EXCEL_READER_TYPE_RK2: | |
778 | + //echo 'SPREADSHEET_EXCEL_READER_TYPE_RK'."\n"; | |
779 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
780 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
781 | + $rknum = $this->_GetInt4d($this->data, $spos + 6); | |
782 | + $numValue = $this->_GetIEEE754($rknum); | |
783 | + //echo $numValue." "; | |
784 | + if ($this->isDate($spos)) { | |
785 | + list($string, $raw) = $this->createDate($numValue); | |
786 | + }else{ | |
787 | + $raw = $numValue; | |
788 | + if (isset($this->_columnsFormat[$column + 1])){ | |
789 | + $this->curformat = $this->_columnsFormat[$column + 1]; | |
790 | + } | |
791 | + $string = sprintf($this->curformat, $numValue * $this->multiplier); | |
792 | + //$this->addcell(RKRecord($r)); | |
793 | + } | |
794 | + $this->addcell($row, $column, $string, $raw); | |
795 | + //echo "Type_RK $row $column $string $raw {$this->curformat}\n"; | |
796 | + break; | |
797 | + case SPREADSHEET_EXCEL_READER_TYPE_LABELSST: | |
798 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
799 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
800 | + $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8; | |
801 | + $index = $this->_GetInt4d($this->data, $spos + 6); | |
802 | + //var_dump($this->sst); | |
803 | + $this->addcell($row, $column, $this->sst[$index]); | |
804 | + //echo "LabelSST $row $column $string\n"; | |
805 | + break; | |
806 | + case SPREADSHEET_EXCEL_READER_TYPE_MULRK: | |
807 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
808 | + $colFirst = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
809 | + $colLast = ord($this->data[$spos + $length - 2]) | ord($this->data[$spos + $length - 1])<<8; | |
810 | + $columns = $colLast - $colFirst + 1; | |
811 | + $tmppos = $spos+4; | |
812 | + for ($i = 0; $i < $columns; $i++) { | |
813 | + $numValue = $this->_GetIEEE754($this->_GetInt4d($this->data, $tmppos + 2)); | |
814 | + if ($this->isDate($tmppos-4)) { | |
815 | + list($string, $raw) = $this->createDate($numValue); | |
816 | + }else{ | |
817 | + $raw = $numValue; | |
818 | + if (isset($this->_columnsFormat[$colFirst + $i + 1])){ | |
819 | + $this->curformat = $this->_columnsFormat[$colFirst + $i + 1]; | |
820 | + } | |
821 | + $string = sprintf($this->curformat, $numValue * $this->multiplier); | |
822 | + } | |
823 | + //$rec['rknumbers'][$i]['xfindex'] = ord($rec['data'][$pos]) | ord($rec['data'][$pos+1]) << 8; | |
824 | + $tmppos += 6; | |
825 | + $this->addcell($row, $colFirst + $i, $string, $raw); | |
826 | + //echo "MULRK $row ".($colFirst + $i)." $string\n"; | |
827 | + } | |
828 | + //MulRKRecord($r); | |
829 | + // Get the individual cell records from the multiple record | |
830 | + //$num = ; | |
831 | + | |
832 | + break; | |
833 | + case SPREADSHEET_EXCEL_READER_TYPE_NUMBER: | |
834 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
835 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
836 | + $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent | |
837 | + if ($this->isDate($spos)) { | |
838 | + list($string, $raw) = $this->createDate($tmp['double']); | |
839 | + // $this->addcell(DateRecord($r, 1)); | |
840 | + }else{ | |
841 | + //$raw = $tmp['']; | |
842 | + if (isset($this->_columnsFormat[$column + 1])){ | |
843 | + $this->curformat = $this->_columnsFormat[$column + 1]; | |
844 | + } | |
845 | + $raw = $this->createNumber($spos); | |
846 | + $string = sprintf($this->curformat, $raw * $this->multiplier); | |
847 | + | |
848 | + // $this->addcell(NumberRecord($r)); | |
849 | + } | |
850 | + $this->addcell($row, $column, $string, $raw); | |
851 | + //echo "Number $row $column $string\n"; | |
852 | + break; | |
853 | + case SPREADSHEET_EXCEL_READER_TYPE_FORMULA: | |
854 | + case SPREADSHEET_EXCEL_READER_TYPE_FORMULA2: | |
855 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
856 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
857 | + if ((ord($this->data[$spos+6])==0) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
858 | + //String formula. Result follows in a STRING record | |
859 | + //echo "FORMULA $row $column Formula with a string<br>\n"; | |
860 | + } elseif ((ord($this->data[$spos+6])==1) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
861 | + //Boolean formula. Result is in +2; 0=false,1=true | |
862 | + } elseif ((ord($this->data[$spos+6])==2) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
863 | + //Error formula. Error code is in +2; | |
864 | + } elseif ((ord($this->data[$spos+6])==3) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
865 | + //Formula result is a null string. | |
866 | + } else { | |
867 | + // result is a number, so first 14 bytes are just like a _NUMBER record | |
868 | + $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent | |
869 | + if ($this->isDate($spos)) { | |
870 | + list($string, $raw) = $this->createDate($tmp['double']); | |
871 | + // $this->addcell(DateRecord($r, 1)); | |
872 | + }else{ | |
873 | + //$raw = $tmp['']; | |
874 | + if (isset($this->_columnsFormat[$column + 1])){ | |
875 | + $this->curformat = $this->_columnsFormat[$column + 1]; | |
876 | + } | |
877 | + $raw = $this->createNumber($spos); | |
878 | + $string = sprintf($this->curformat, $raw * $this->multiplier); | |
879 | + | |
880 | + // $this->addcell(NumberRecord($r)); | |
881 | + } | |
882 | + $this->addcell($row, $column, $string, $raw); | |
883 | + //echo "Number $row $column $string\n"; | |
884 | + } | |
885 | + break; | |
886 | + case SPREADSHEET_EXCEL_READER_TYPE_BOOLERR: | |
887 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
888 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
889 | + $string = ord($this->data[$spos+6]); | |
890 | + $this->addcell($row, $column, $string); | |
891 | + //echo 'Type_BOOLERR '."\n"; | |
892 | + break; | |
893 | + case SPREADSHEET_EXCEL_READER_TYPE_ROW: | |
894 | + case SPREADSHEET_EXCEL_READER_TYPE_DBCELL: | |
895 | + case SPREADSHEET_EXCEL_READER_TYPE_MULBLANK: | |
896 | + break; | |
897 | + case SPREADSHEET_EXCEL_READER_TYPE_LABEL: | |
898 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
899 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
900 | + $this->addcell($row, $column, substr($this->data, $spos + 8, ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8)); | |
901 | + | |
902 | + // $this->addcell(LabelRecord($r)); | |
903 | + break; | |
904 | + | |
905 | + case SPREADSHEET_EXCEL_READER_TYPE_EOF: | |
906 | + $cont = false; | |
907 | + break; | |
908 | + default: | |
909 | + //echo ' unknown :'.base_convert($r['code'],10,16)."\n"; | |
910 | + break; | |
911 | + | |
912 | + } | |
913 | + $spos += $length; | |
914 | + } | |
915 | + | |
916 | + if (!isset($this->sheets[$this->sn]['numRows'])) | |
917 | + $this->sheets[$this->sn]['numRows'] = $this->sheets[$this->sn]['maxrow']; | |
918 | + if (!isset($this->sheets[$this->sn]['numCols'])) | |
919 | + $this->sheets[$this->sn]['numCols'] = $this->sheets[$this->sn]['maxcol']; | |
920 | + | |
921 | + } | |
922 | + | |
923 | + /** | |
924 | + * Check whether the current record read is a date | |
925 | + * | |
926 | + * @param todo | |
927 | + * @return boolean True if date, false otherwise | |
928 | + */ | |
929 | + function isDate($spos) | |
930 | + { | |
931 | + //$xfindex = GetInt2d(, 4); | |
932 | + $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8; | |
933 | + //echo 'check is date '.$xfindex.' '.$this->formatRecords['xfrecords'][$xfindex]['type']."\n"; | |
934 | + //var_dump($this->formatRecords['xfrecords'][$xfindex]); | |
935 | + if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'date') { | |
936 | + $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format']; | |
937 | + $this->rectype = 'date'; | |
938 | + return true; | |
939 | + } else { | |
940 | + if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'number') { | |
941 | + $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format']; | |
942 | + $this->rectype = 'number'; | |
943 | + if (($xfindex == 0x9) || ($xfindex == 0xa)){ | |
944 | + $this->multiplier = 100; | |
945 | + } | |
946 | + }else{ | |
947 | + $this->curformat = $this->_defaultFormat; | |
948 | + $this->rectype = 'unknown'; | |
949 | + } | |
950 | + return false; | |
951 | + } | |
952 | + } | |
953 | + | |
954 | + //}}} | |
955 | + //{{{ createDate() | |
956 | + | |
957 | + /** | |
958 | + * Convert the raw Excel date into a human readable format | |
959 | + * | |
960 | + * Dates in Excel are stored as number of seconds from an epoch. On | |
961 | + * Windows, the epoch is 30/12/1899 and on Mac it's 01/01/1904 | |
962 | + * | |
963 | + * @access private | |
964 | + * @param integer The raw Excel value to convert | |
965 | + * @return array First element is the converted date, the second element is number a unix timestamp | |
966 | + */ | |
967 | + function createDate($numValue) | |
968 | + { | |
969 | + if ($numValue > 1) { | |
970 | + $utcDays = $numValue - ($this->nineteenFour ? SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904 : SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS); | |
971 | + $utcValue = round(($utcDays+1) * SPREADSHEET_EXCEL_READER_MSINADAY); | |
972 | + $string = date ($this->curformat, $utcValue); | |
973 | + $raw = $utcValue; | |
974 | + } else { | |
975 | + $raw = $numValue; | |
976 | + $hours = floor($numValue * 24); | |
977 | + $mins = floor($numValue * 24 * 60) - $hours * 60; | |
978 | + $secs = floor($numValue * SPREADSHEET_EXCEL_READER_MSINADAY) - $hours * 60 * 60 - $mins * 60; | |
979 | + $string = date ($this->curformat, mktime($hours, $mins, $secs)); | |
980 | + } | |
981 | + | |
982 | + return array($string, $raw); | |
983 | + } | |
984 | + | |
985 | + function createNumber($spos) | |
986 | + { | |
987 | + $rknumhigh = $this->_GetInt4d($this->data, $spos + 10); | |
988 | + $rknumlow = $this->_GetInt4d($this->data, $spos + 6); | |
989 | + //for ($i=0; $i<8; $i++) { echo ord($this->data[$i+$spos+6]) . " "; } echo "<br>"; | |
990 | + $sign = ($rknumhigh & 0x80000000) >> 31; | |
991 | + $exp = ($rknumhigh & 0x7ff00000) >> 20; | |
992 | + $mantissa = (0x100000 | ($rknumhigh & 0x000fffff)); | |
993 | + $mantissalow1 = ($rknumlow & 0x80000000) >> 31; | |
994 | + $mantissalow2 = ($rknumlow & 0x7fffffff); | |
995 | + $value = $mantissa / pow( 2 , (20- ($exp - 1023))); | |
996 | + if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023))); | |
997 | + $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023))); | |
998 | + //echo "Sign = $sign, Exp = $exp, mantissahighx = $mantissa, mantissalow1 = $mantissalow1, mantissalow2 = $mantissalow2<br>\n"; | |
999 | + if ($sign) {$value = -1 * $value;} | |
1000 | + return $value; | |
1001 | + } | |
1002 | + | |
1003 | + function addcell($row, $col, $string, $raw = '') | |
1004 | + { | |
1005 | + //echo "ADD cel $row-$col $string\n"; | |
1006 | + $this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset); | |
1007 | + $this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset); | |
1008 | + $this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string; | |
1009 | + if ($raw) | |
1010 | + $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['raw'] = $raw; | |
1011 | + if (isset($this->rectype)) | |
1012 | + $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['type'] = $this->rectype; | |
1013 | + | |
1014 | + } | |
1015 | + | |
1016 | + | |
1017 | + function _GetIEEE754($rknum) | |
1018 | + { | |
1019 | + if (($rknum & 0x02) != 0) { | |
1020 | + $value = $rknum >> 2; | |
1021 | + } else { | |
1022 | +//mmp | |
1023 | +// first comment out the previously existing 7 lines of code here | |
1024 | +// $tmp = unpack("d", pack("VV", 0, ($rknum & 0xfffffffc))); | |
1025 | +// //$value = $tmp['']; | |
1026 | +// if (array_key_exists(1, $tmp)) { | |
1027 | +// $value = $tmp[1]; | |
1028 | +// } else { | |
1029 | +// $value = $tmp['']; | |
1030 | +// } | |
1031 | +// I got my info on IEEE754 encoding from | |
1032 | +// http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html | |
1033 | +// The RK format calls for using only the most significant 30 bits of the | |
1034 | +// 64 bit floating point value. The other 34 bits are assumed to be 0 | |
1035 | +// So, we use the upper 30 bits of $rknum as follows... | |
1036 | + $sign = ($rknum & 0x80000000) >> 31; | |
1037 | + $exp = ($rknum & 0x7ff00000) >> 20; | |
1038 | + $mantissa = (0x100000 | ($rknum & 0x000ffffc)); | |
1039 | + $value = $mantissa / pow( 2 , (20- ($exp - 1023))); | |
1040 | + if ($sign) {$value = -1 * $value;} | |
1041 | +//end of changes by mmp | |
1042 | + | |
1043 | + } | |
1044 | + | |
1045 | + if (($rknum & 0x01) != 0) { | |
1046 | + $value /= 100; | |
1047 | + } | |
1048 | + return $value; | |
1049 | + } | |
1050 | + | |
1051 | + function _encodeUTF16($string) | |
1052 | + { | |
1053 | + $result = $string; | |
1054 | + if ($this->_defaultEncoding){ | |
1055 | + switch ($this->_encoderFunction){ | |
1056 | + case 'iconv' : $result = iconv('UTF-16LE', $this->_defaultEncoding, $string); | |
1057 | + break; | |
1058 | + case 'mb_convert_encoding' : $result = mb_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE' ); | |
1059 | + break; | |
1060 | + } | |
1061 | + } | |
1062 | + return $result; | |
1063 | + } | |
1064 | + | |
1065 | + function _GetInt4d($data, $pos) | |
1066 | + { | |
1067 | + $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24); | |
1068 | + if ($value>=4294967294) | |
1069 | + { | |
1070 | + $value=-2; | |
1071 | + } | |
1072 | + return $value; | |
1073 | + } | |
1074 | + | |
1075 | +} | |
1076 | + | |
1077 | +/* | |
1078 | + * Local variables: | |
1079 | + * tab-width: 4 | |
1080 | + * c-basic-offset: 4 | |
1081 | + * c-hanging-comment-ender-p: nil | |
1082 | + * End: | |
1083 | + */ | |
1084 | + | |
1085 | +?> | ... | ... |
common/components/parsers/Parser.php
common/components/parsers/TableParser.php
... | ... | @@ -71,8 +71,8 @@ abstract class TableParser extends Parser |
71 | 71 | |
72 | 72 | if ($this->isEmptyRow()) { |
73 | 73 | //счетчик пустых строк |
74 | - //CustomVarDamp::dump($this->current_row_number); | |
75 | 74 | $empty_lines++; |
75 | + $this->current_row_number++; | |
76 | 76 | continue; |
77 | 77 | } |
78 | 78 | |
... | ... | @@ -101,7 +101,6 @@ abstract class TableParser extends Parser |
101 | 101 | $empty_lines = 0; |
102 | 102 | |
103 | 103 | } |
104 | - | |
105 | 104 | } |
106 | 105 | |
107 | 106 | /** | ... | ... |
1 | +<?php | |
2 | +/** | |
3 | + | |
4 | + */ | |
5 | +namespace common\components\parsers; | |
6 | + | |
7 | +/** | |
8 | + * Class XlsParser | |
9 | + * @package yii\multiparser | |
10 | + * @todo - перевести на анг. яз. | |
11 | + */ | |
12 | + | |
13 | + | |
14 | +class XlsParser extends TableParser | |
15 | +{ | |
16 | + // экземпляр класса Spreadsheet_Excel_Reader - для чтения листов Excel | |
17 | + protected $reader; | |
18 | + // строка кодировки | |
19 | + protected $encoding = 'CP1251'; | |
20 | + | |
21 | + /** | |
22 | + * @var int - номер листа с которого будет происходить чтение | |
23 | + */ | |
24 | + public $active_sheet = 0; | |
25 | + | |
26 | + /** | |
27 | + * метод устанавливает настройки конвертера и ридера | |
28 | + */ | |
29 | + public function setup() | |
30 | + { | |
31 | + parent::setup(); | |
32 | + $this->setupReader(); | |
33 | + } | |
34 | + | |
35 | + | |
36 | + /** | |
37 | + * устанавливает ридер и его параметры | |
38 | + */ | |
39 | + protected function setupReader() | |
40 | + { | |
41 | + require_once 'ExcelReader/reader.php'; | |
42 | + $this->reader = new \Spreadsheet_Excel_Reader(); | |
43 | + $this->reader->setOutputEncoding( $this->encoding ); | |
44 | + $this->reader->read( $this->file_path ); | |
45 | + | |
46 | + } | |
47 | + | |
48 | + public function read() | |
49 | + { | |
50 | + parent::read(); | |
51 | + | |
52 | + $this->cleanUp(); | |
53 | + | |
54 | + return $this->result; | |
55 | + } | |
56 | + | |
57 | + | |
58 | + protected function readRow( ) | |
59 | + { | |
60 | + $this->row = []; | |
61 | + $current_sheet = $this->reader->sheets[ $this->active_sheet ]; | |
62 | + | |
63 | + for ( $j = 1; $j <= $current_sheet['numCols']; $j++ ) { | |
64 | + if ( isset( $current_sheet['cells'][ $this->current_row_number ][$j]) ) | |
65 | + $this->row[] = $current_sheet['cells'][ $this->current_row_number ][$j]; | |
66 | + } | |
67 | + } | |
68 | + | |
69 | + protected function isEmptyRow(){ | |
70 | + | |
71 | + $is_empty = false; | |
72 | + | |
73 | + if ( !$this->row ) { | |
74 | + return true; | |
75 | + } | |
76 | + if ( count( $this->row ) < $this->min_column_quantity ) { | |
77 | + return true; | |
78 | + } | |
79 | + | |
80 | + $j = 0; | |
81 | + for ($i = 1; $i <= count( $this->row ); $i++) { | |
82 | + | |
83 | + if ( !isset( $this->row[ $i - 1 ] ) ) { | |
84 | + continue; | |
85 | + } | |
86 | + | |
87 | + if ( $this->isEmptyColumn( $this->row[$i - 1] ) ) { | |
88 | + $j++; | |
89 | + } | |
90 | + | |
91 | + if ( $j >= $this->min_column_quantity ) { | |
92 | + $is_empty = true; | |
93 | + break; | |
94 | + } | |
95 | + } | |
96 | + | |
97 | + return $is_empty; | |
98 | + } | |
99 | + | |
100 | + protected function isEmptyColumn( $val ){ | |
101 | + return $val == ''; | |
102 | + } | |
103 | + | |
104 | + protected function setResult( ){ | |
105 | + $this->result[] = $this->row; | |
106 | + } | |
107 | +} | |
0 | 108 | \ No newline at end of file | ... | ... |
common/components/parsers/XlsxParser.php
... | ... | @@ -208,7 +208,7 @@ class XlsxParser extends TableParser |
208 | 208 | |
209 | 209 | $is_empty = false; |
210 | 210 | |
211 | - if (!count($this->row) || !$this->current_node->valid()) { | |
211 | + if (!count($this->row)) { | |
212 | 212 | return true; |
213 | 213 | } |
214 | 214 | |
... | ... | @@ -230,7 +230,7 @@ class XlsxParser extends TableParser |
230 | 230 | |
231 | 231 | protected function isEmptyColumn($val) |
232 | 232 | { |
233 | - return $val == ''; | |
233 | + return $val == '' || $val === null; | |
234 | 234 | } |
235 | 235 | |
236 | 236 | protected function setResult() | ... | ... |
common/components/parsers/config.php
1 | 1 | <?php |
2 | - return [ | |
3 | - 'csv' => | |
4 | - ['web' => | |
5 | - ['class' => 'common\components\parsers\CustomCsvParser', | |
6 | - 'auto_detect_first_line' => true, | |
7 | - 'converter_conf' => [ | |
8 | - 'class' => 'common\components\parsers\CustomConverter', | |
9 | - 'configuration' => ["encode" => 'DESCR'], | |
10 | - ] | |
11 | - ], | |
12 | - 'console' => | |
13 | - ['class' => 'common\components\parsers\CustomCsvParser', | |
14 | - 'auto_detect_first_line' => true, | |
15 | - 'converter_conf' => [ | |
16 | - 'class' => ' common\components\parsers\CustomConverter', | |
17 | - 'configuration' => ["encode" => 'DESCR', | |
18 | - "string" => 'DESCR', | |
19 | - "float" => 'PRICE', | |
20 | - "brand" => 'BRAND', | |
21 | - "integer" => ['BOX','ADD_BOX'], | |
22 | - "multiply" => [], | |
23 | - "article" => [], | |
24 | - "details" => [] | |
2 | +return [ | |
3 | + 'csv' => | |
4 | + ['web' => | |
5 | + ['class' => 'common\components\parsers\CustomCsvParser', | |
6 | + 'auto_detect_first_line' => true, | |
7 | + 'converter_conf' => [ | |
8 | + 'class' => 'common\components\parsers\CustomConverter', | |
9 | + 'configuration' => ["encode" => 'DESCR'], | |
10 | + ] | |
11 | + ], | |
12 | + 'console' => | |
13 | + ['class' => 'common\components\parsers\CustomCsvParser', | |
14 | + 'auto_detect_first_line' => true, | |
15 | + 'converter_conf' => [ | |
16 | + 'class' => ' common\components\parsers\CustomConverter', | |
17 | + 'configuration' => ["encode" => 'DESCR', | |
18 | + "string" => 'DESCR', | |
19 | + "float" => 'PRICE', | |
20 | + "brand" => 'BRAND', | |
21 | + "integer" => ['BOX', 'ADD_BOX'], | |
22 | + "multiply" => [], | |
23 | + "article" => [], | |
24 | + "details" => [] | |
25 | 25 | |
26 | - ] | |
27 | - ],], | |
26 | + ] | |
27 | + ],], | |
28 | 28 | |
29 | - 'basic_column' => [ | |
30 | - Null => 'Пусто', | |
31 | - "BRAND" => 'Бренд', | |
32 | - "ARTICLE"=> 'Артикул', | |
33 | - "PRICE" => 'Цена', | |
34 | - "DESCR" => 'Наименование', | |
35 | - "BOX" => 'Колво', | |
36 | - "ADD_BOX"=> 'В пути', | |
37 | - "GROUP" => 'Группа RG' | |
38 | - ], | |
29 | + 'basic_column' => [ | |
30 | + Null => 'Пусто', | |
31 | + "BRAND" => 'Бренд', | |
32 | + "ARTICLE" => 'Артикул', | |
33 | + "PRICE" => 'Цена', | |
34 | + "DESCR" => 'Наименование', | |
35 | + "BOX" => 'Колво', | |
36 | + "ADD_BOX" => 'В пути', | |
37 | + "GROUP" => 'Группа RG' | |
38 | + ], | |
39 | 39 | |
40 | - 'crosses' => ['class' => 'common\components\parsers\CustomCsvParser', | |
41 | - 'auto_detect_first_line' => true, | |
42 | - 'min_column_quantity' => 4, | |
43 | - 'converter_conf' => [ | |
44 | - 'class' => ' common\components\parsers\CustomConverter', | |
45 | - 'hasKey' => 1, | |
46 | - 'configuration' => [ | |
47 | - "brand" => ['BRAND', 'CROSS_BRAND'], | |
48 | - ] | |
49 | - ], | |
50 | - 'basic_column' => [ | |
51 | - Null => 'Пусто', | |
52 | - "ARTICLE"=> 'Артикул', | |
53 | - "CROSS_ARTICLE" => 'Кросс артикул', | |
54 | - "BRAND" => 'Бренд', | |
55 | - "CROSS_BRAND" => 'Кросс бренд' | |
56 | - ], | |
57 | - ], | |
58 | - ], | |
59 | - 'xml' => | |
60 | - ['console' => | |
61 | - ['class' => 'common\components\parsers\XmlParser', | |
62 | - 'node' => 'Товар', | |
63 | - 'has_header_row' => true, | |
64 | - 'keys' => [ | |
65 | - "BRAND" => 'Производитель', | |
66 | - "ARTICLE"=> 'Код', | |
67 | - "PRICE" => 'Розница', | |
68 | - "DESCR" => 'Наименование', | |
69 | - "BOX" => 'Колво', | |
70 | - "ADD_BOX"=> 'Ожидаемое', | |
71 | - "GROUP" => 'Группа' | |
72 | - ], | |
73 | - 'converter_conf' => [ | |
74 | - 'class' => 'common\components\parsers\CustomConverter', | |
75 | - 'configuration' => ["details" => [] | |
76 | - ],], | |
77 | - ], | |
78 | - ], | |
79 | - 'xlsx' => | |
80 | - ['web' => | |
81 | - ['class' => 'common\components\parsers\XlsxParser', | |
82 | - 'path_for_extract_files' => \Yii::getAlias('@temp_upload') . '/xlsx/', | |
83 | - //'auto_detect_first_line' => true, | |
84 | - //'has_header_row' => true, | |
85 | - 'active_sheet' => 1, | |
86 | - 'converter_conf' => [ | |
87 | - 'class' => 'common\components\parsers\CustomConverter', | |
88 | - 'configuration' => ["string" => []], | |
89 | - ] | |
90 | - ], | |
91 | - 'console' => | |
92 | - ['class' => 'common\components\parsers\XlsxParser', | |
93 | - 'path_for_extract_files' => \Yii::getAlias('@temp_upload') . '/xlsx/', | |
94 | - 'auto_detect_first_line' => true, | |
95 | - 'active_sheet' => 1, | |
96 | - 'converter_conf' => [ | |
97 | - 'class' => ' common\components\parsers\CustomConverter', | |
98 | - 'configuration' => ["encode" => 'DESCR', | |
99 | - "string" => 'DESCR', | |
100 | - "float" => 'PRICE', | |
101 | - "brand" => 'BRAND', | |
102 | - "integer" => ['BOX','ADD_BOX'], | |
103 | - "multiply" => [], | |
104 | - "article" => [], | |
105 | - "details" => [] | |
106 | - ] | |
107 | - ],], | |
108 | - ] | |
109 | - ]; | |
40 | + 'crosses' => ['class' => 'common\components\parsers\CustomCsvParser', | |
41 | + 'auto_detect_first_line' => true, | |
42 | + 'min_column_quantity' => 4, | |
43 | + 'converter_conf' => [ | |
44 | + 'class' => ' common\components\parsers\CustomConverter', | |
45 | + 'hasKey' => 1, | |
46 | + 'configuration' => [ | |
47 | + "brand" => ['BRAND', 'CROSS_BRAND'], | |
48 | + ] | |
49 | + ], | |
50 | + 'basic_column' => [ | |
51 | + Null => 'Пусто', | |
52 | + "ARTICLE" => 'Артикул', | |
53 | + "CROSS_ARTICLE" => 'Кросс артикул', | |
54 | + "BRAND" => 'Бренд', | |
55 | + "CROSS_BRAND" => 'Кросс бренд' | |
56 | + ], | |
57 | + ], | |
58 | + ], | |
59 | + 'xml' => | |
60 | + ['console' => | |
61 | + ['class' => 'common\components\parsers\XmlParser', | |
62 | + 'node' => 'Товар', | |
63 | + 'has_header_row' => true, | |
64 | + 'keys' => [ | |
65 | + "BRAND" => 'Производитель', | |
66 | + "ARTICLE" => 'Код', | |
67 | + "PRICE" => 'Розница', | |
68 | + "DESCR" => 'Наименование', | |
69 | + "BOX" => 'Колво', | |
70 | + "ADD_BOX" => 'Ожидаемое', | |
71 | + "GROUP" => 'Группа' | |
72 | + ], | |
73 | + 'converter_conf' => [ | |
74 | + 'class' => 'common\components\parsers\CustomConverter', | |
75 | + 'configuration' => ["details" => [] | |
76 | + ],], | |
77 | + ], | |
78 | + ], | |
79 | + 'xlsx' => | |
80 | + ['web' => | |
81 | + ['class' => 'common\components\parsers\XlsxParser', | |
82 | + 'path_for_extract_files' => \Yii::getAlias('@temp_upload') . '/xlsx/', | |
83 | + //'auto_detect_first_line' => true, | |
84 | + //'has_header_row' => true, | |
85 | + 'active_sheet' => 1, | |
86 | + 'converter_conf' => [ | |
87 | + 'class' => 'common\components\parsers\CustomConverter', | |
88 | + 'configuration' => ["string" => []], | |
89 | + ] | |
90 | + ], | |
91 | + 'console' => | |
92 | + ['class' => 'common\components\parsers\XlsxParser', | |
93 | + 'path_for_extract_files' => \Yii::getAlias('@temp_upload') . '/xlsx/', | |
94 | + 'auto_detect_first_line' => true, | |
95 | + 'active_sheet' => 1, | |
96 | + 'min_column_quantity' => 3, | |
97 | + 'converter_conf' => [ | |
98 | + 'class' => ' common\components\parsers\CustomConverter', | |
99 | + 'configuration' => ["encode" => 'DESCR', | |
100 | + "string" => 'DESCR', | |
101 | + "float" => 'PRICE', | |
102 | + "brand" => 'BRAND', | |
103 | + "integer" => ['BOX', 'ADD_BOX'], | |
104 | + "multiply" => [], | |
105 | + "article" => [], | |
106 | + "details" => [] | |
107 | + ] | |
108 | + ],], | |
109 | + ], | |
110 | + 'txt' => | |
111 | + [ 'web' => | |
112 | + ['class' => 'common\components\parsers\CustomCsvParser', | |
113 | + 'auto_detect_first_line' => true, | |
114 | + 'delimiter' => "\t", | |
115 | + 'converter_conf' => [ | |
116 | + 'class' => 'common\components\parsers\CustomConverter', | |
117 | + 'configuration' => ["encode" => 'DESCR'], | |
118 | + ] | |
119 | + ], | |
120 | + 'console' => | |
121 | + ['class' => 'common\components\parsers\CustomCsvParser', | |
122 | + 'auto_detect_first_line' => true, | |
123 | + 'delimiter' => "\t", | |
124 | + 'converter_conf' => [ | |
125 | + 'class' => ' common\components\parsers\CustomConverter', | |
126 | + 'configuration' => ["encode" => 'DESCR', | |
127 | + "string" => 'DESCR', | |
128 | + "float" => 'PRICE', | |
129 | + "brand" => 'BRAND', | |
130 | + "integer" => ['BOX', 'ADD_BOX'], | |
131 | + "multiply" => [], | |
132 | + "article" => [], | |
133 | + "details" => [] | |
134 | + ] | |
135 | + ], | |
136 | + ], | |
137 | + 'basic_column' => [ | |
138 | + Null => 'Пусто', | |
139 | + "BRAND" => 'Бренд', | |
140 | + "ARTICLE" => 'Артикул', | |
141 | + "PRICE" => 'Цена', | |
142 | + "DESCR" => 'Наименование', | |
143 | + "BOX" => 'Колво', | |
144 | + "ADD_BOX" => 'В пути', | |
145 | + "GROUP" => 'Группа RG' | |
146 | + ], | |
147 | + ], | |
148 | + 'xls' => | |
149 | + ['web' => | |
150 | + ['class' => 'common\components\parsers\XlsParser', | |
151 | + 'auto_detect_first_line' => true, | |
152 | + 'converter_conf' => [ | |
153 | + 'class' => 'common\components\parsers\CustomConverter', | |
154 | + 'configuration' => ["encode" => 'DESCR'], | |
155 | + ] | |
156 | + ], | |
157 | + 'console' => | |
158 | + ['class' => 'common\components\parsers\XlsParser', | |
159 | + 'auto_detect_first_line' => true, | |
160 | + 'converter_conf' => [ | |
161 | + 'class' => ' common\components\parsers\CustomConverter', | |
162 | + 'configuration' => ["encode" => 'DESCR', | |
163 | + "string" => 'DESCR', | |
164 | + "float" => 'PRICE', | |
165 | + "brand" => 'BRAND', | |
166 | + "integer" => ['BOX', 'ADD_BOX'], | |
167 | + "multiply" => [], | |
168 | + "article" => [], | |
169 | + "details" => [] | |
170 | + | |
171 | + ] | |
172 | + ],], | |
173 | + | |
174 | + 'basic_column' => [ | |
175 | + Null => 'Пусто', | |
176 | + "BRAND" => 'Бренд', | |
177 | + "ARTICLE" => 'Артикул', | |
178 | + "PRICE" => 'Цена', | |
179 | + "DESCR" => 'Наименование', | |
180 | + "BOX" => 'Колво', | |
181 | + "ADD_BOX" => 'В пути', | |
182 | + "GROUP" => 'Группа RG' | |
183 | + ], | |
184 | + ], | |
185 | +]; | |
110 | 186 | ... | ... |
common/models/Brands.php
... | ... | @@ -22,6 +22,8 @@ class Brands extends BaseActiveRecord |
22 | 22 | { |
23 | 23 | // для валидации выбранного пользователем файла |
24 | 24 | public $file; |
25 | + // флаг, нужно ли удалять изображение | |
26 | + public $delete_img; | |
25 | 27 | /** |
26 | 28 | * @inheritdoc |
27 | 29 | */ |
... | ... | @@ -36,15 +38,17 @@ class Brands extends BaseActiveRecord |
36 | 38 | public function rules() |
37 | 39 | { |
38 | 40 | return [ |
39 | - [['BRAND', 'CONTENT'], 'required'], | |
41 | + [['BRAND'], 'required'], | |
40 | 42 | [['if_tecdoc', 'if_oem', 'if_checked'], 'integer'], |
41 | 43 | [['CONTENT'], 'string'], |
42 | 44 | [['timestamp'], 'safe'], |
45 | + [['delete_img'], 'safe'], | |
43 | 46 | [['BRAND'], 'string', 'max' => 100], |
44 | 47 | [['tecdoc_logo'], 'string', 'max' => 50], |
45 | 48 | ['IMG', 'string', 'max' => 255], |
46 | - ['IMG', 'match', 'pattern' => '/[a-zA-Z0-9]+\.[a-zA-Z]+/', 'message' => 'имя файла изображения должно иметь только латинские символы и цифры'], | |
47 | - [['file'], 'image' ] | |
49 | + ['IMG', 'match', 'pattern' => '/[a-zA-Z0-9]+\.[a-zA-Z]+/', 'message' => 'Имя файла изображения должно иметь только латинские символы и цифры'], | |
50 | + // [['file'], 'image' ] | |
51 | + [['file'], 'file', 'extensions' => ['jpg','png','bmp'], 'checkExtensionByMimeType'=>false ] | |
48 | 52 | ]; |
49 | 53 | } |
50 | 54 | |
... | ... | @@ -54,15 +58,16 @@ class Brands extends BaseActiveRecord |
54 | 58 | public function attributeLabels() |
55 | 59 | { |
56 | 60 | return [ |
57 | - 'ID' => 'Номер', | |
58 | - 'BRAND' => 'Название', | |
61 | + 'ID' => 'НОМЕР', | |
62 | + 'BRAND' => 'БРЕНД', | |
59 | 63 | 'if_tecdoc' => 'ТЕКДОК?', |
60 | 64 | 'tecdoc_logo' => 'Tecdoc Logo', |
61 | - 'if_oem' => 'Оргигинал', | |
65 | + 'if_oem' => 'Оригинал', | |
62 | 66 | 'if_checked' => 'If Checked', |
63 | 67 | 'CONTENT' => 'Описание', |
64 | 68 | 'IMG' => 'Изображение', |
65 | 69 | 'timestamp' => 'Timestamp', |
70 | + 'delete_img' => 'Удалить', | |
66 | 71 | ]; |
67 | 72 | } |
68 | 73 | } | ... | ... |
common/models/BrandsSearch.php
console/controllers/ParserController.php
... | ... | @@ -73,8 +73,6 @@ class ParserController extends Controller |
73 | 73 | $new_destination = \Yii::getAlias('@mail_upload') . '/'; |
74 | 74 | |
75 | 75 | $this->registerAndReplaceFiles($files, $new_destination); |
76 | - | |
77 | - | |
78 | 76 | } |
79 | 77 | |
80 | 78 | protected function parseFilesByExtension( $ext, $path_arr ) |
... | ... | @@ -282,6 +280,10 @@ class ParserController extends Controller |
282 | 280 | $importer_id = ImportersFiles::findOne(['id' => $file_name])->importer_id; |
283 | 281 | $current_importer = Importers::findOne(['id' => $importer_id]); |
284 | 282 | $keys = $current_importer->keys; |
283 | + // если меньше 5 ключей, то чего-то нехватает - обнуляем | |
284 | + if ( count( $keys ) < 5 ) { | |
285 | + $keys = NULL; | |
286 | + } | |
285 | 287 | $mult_array = $current_importer->multiply; |
286 | 288 | |
287 | 289 | // получим настройки ценообразования и передадим их отдельно в конвертер | ... | ... |