1 <?php
2
3 4 5 6 7
8 class WebylonImportAdmin extends LeftAndMain {
9
10 static $url_segment = 'import';
11 static $url_rule = '/$Action/$ID';
12 static = 'Import';
13 static $tree_class = null;
14 private $filename = '/_import/import.xml';
15
16 17 18 19
20 function filename() {
21 return ASSETS_PATH . $this->filename;
22 }
23
24 25 26 27
28 public function ImportForm() {
29 if (file_exists($this->filename())) {
30 return false;
31 }
32 $fileField = new FileField('ImportFile', 'Файл для импорта');
33 $fileField->allowedExtensions = array('xml');
34 $fileField->setFolderName('Upload');
35 $uploadValidator = new Upload_Validator();
36 $uploadValidator->setAllowedExtensions(array('xml'));
37 $fileField->setValidator($uploadValidator);
38
39 $config = new ImportConfig();
40 $literalField = new LiteralField('Info', '');
41 if ($config->getStatus() == 'end') {
42 $literalField = new LiteralField('Info', 'Импорт окончен. ' . date("Y-m-d H:i:s", $config->endTime));
43 }
44
45 return new Form(
46 $this,
47 'ImportForm',
48 new FieldSet(
49 new HeaderField('ImportHead', 'Импортирование товаров'),
50 $literalField,
51 new HiddenField('ID', '', 'root'),
52 $fileField,
53 new CheckboxField('onlyOffers', "Импортируются только товары"),
54 new CheckboxField('clearProduct', 'Очистить товары'),
55 new CheckboxField('clearCatalog', 'Очистить рубрики')
56 ),
57 new FieldSet(
58 new FormAction('doImport', 'Импорт')
59 )
60 );
61 }
62
63 64 65 66
67 public function DeleteUnpublishedForm() {
68 return new Form(
69 $this,
70 'DeleteUnpublishedForm',
71 new FieldSet(
72 new HeaderField('ImportHead', 'Удаление неопубликованного')
73 ),
74 new FieldSet(
75 new FormAction('doClearUnpublish', 'Да, я хочу очистить все неопубликованное')
76 )
77 );
78 }
79
80 81 82 83
84 public function ImportInfo() {
85 $config = new ImportConfig();
86 $colors = array('gray', 'gray', 'gray', 'gray', 'gray');
87 $curr = 0;
88 $all = 0;
89 $error = '';
90 switch ($config->getStatus()) {
91 case 'error':
92 $error = '<p style="color:red">Ошибка при импорте. Импорт не может быть продолжен.<br/>' . $config->getMessage() . '</p>';
93 break;
94 case 'start':
95 $colors[0] = 'green';
96 break;
97
98 case 'clear':
99 $colors[1] = 'green';
100 break;
101 case 'write_catalog':
102 $colors[2] = 'green';
103 break;
104 case 'write_product':
105 $colors[3] = 'green';
106 $all = $config->allOffers;
107 $curr = $config->getCursor();
108 break;
109 case 'uupublish':
110 $color[4] = 'green';
111 default:
112 break;
113 }
114 return new Form(
115 $this,
116 'ImportInfo',
117 new FieldSet(
118 new HeaderField('ImportHead', 'Импортирование товаров'),
119 new LiteralField(
120 'ImportInfo',
121 '<h3>Осуществляется импорт...</h3>
122 ' . $error . '
123 <p style="color:' . $colors[0] . '">1) Импорт скоро начнется... начался: ' . date("Y-m-d H:i:s", $config->startTime) . '</p>
124 <p style="color:' . $colors[1] . '">2) Выполняю операции по чистке</p>
125 <p style="color:' . $colors[2] . '">3) Импортирую структуру каталога</p>
126 <p style="color:' . $colors[3] . '">4) Импортирую товары: ' . $curr . ' из ' . $all . '</p>
127 <p style="color:' . $colors[4] . '">5) Убираем товары/рубрики которые не обновлены с публикации</p>'
128 )
129 ),
130 new FieldSet(
131 new FormAction('doCancelImport', 'Остановить импортирование')
132 )
133 );
134 }
135
136 137 138 139 140 141
142 function doImport($data, $form) {
143 $this->startImport($data);
144 return Director::redirect('/admin/import');
145 }
146
147 148 149 150 151 152
153 function startImport($data) {
154 $valid = false;
155 $folderPath = dirname($this->filename());
156 $this->removeConfig();
157
158
159
160 if(!file_exists(ASSETS_PATH)){
161 mkdir(ASSETS_PATH, Filesystem::$folder_create_mask);
162 }
163 if(!file_exists( $folderPath)){
164 mkdir($folderPath, Filesystem::$folder_create_mask);
165 }
166
167 if (@$data['auto']) {
168 rename($data['importFile'], $this->filename());
169 }
170 else {
171 move_uploaded_file($data['ImportFile']['tmp_name'], $this->filename());
172 }
173
174 $import = false;
175 if (file_exists($this->filename())) {
176 $import = new BaseMediawebImportParser($this->filename(), 'Upload');
177 set_error_handler('validateError');
178 $valid= $import->validate();
179 restore_error_handler();
180 }
181
182
183 $config = new ImportConfig();
184 $config->clearDatabaseCatalog = (isset($data['clearCatalog'])) ? true : false;
185 $config->clearDatabaseProducts = (isset($data['clearProduct'])) ? true : false;
186 $config->onlyOffers = (isset($data['onlyOffers']) && !isset($data['clearCatalog'])) ? true : false;
187 if ($valid) {
188 $config->setStatus('start');
189 } else {
190 $config->setStatus('error');
191 $config->setMessage('Ошибка в формате файла: ' . (($import) ? $import->getMessage() : 'нет файла импорта'));
192 }
193
194 $config->startTime = time();
195 $config->write();
196 return $valid;
197 }
198
199 200 201 202 203 204
205 function doCancelImport($data, $form) {
206 $this->removeConfig();
207 return Director::redirect('/admin/import');
208 }
209
210 public function removeConfig() {
211 if (file_exists($this->filename())) {
212 unlink($this->filename());
213 }
214 ImportConfig::delete();
215 }
216
217 218 219 220 221 222
223 function doClearUnpublish($data, $form) {
224 $itemsForDelete = DataObject::get('Catalog', 'Status NOT LIKE \'Published\'');
225 if ($itemsForDelete)
226 foreach ($itemsForDelete as $page) {
227 $page->delete();
228 }
229 $itemsForDelete = DataObject::get('Product', 'Status NOT LIKE \'Published\'');
230 if ($itemsForDelete)
231 foreach ($itemsForDelete as $page) {
232 $page->delete();
233 }
234 return Director::redirect('/admin/import');
235 return true;
236 }
237
238 }
239
240 function validateError($errno, $errstr, $errfile, $errline) {
241 echo '<h1>Ошибка: ' . $errstr . '</h1>';
242 $adm = new WebylonImportAdmin();
243 $adm->removeConfig();
244 die();
245 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-