@@ -109,43 +109,34 @@ public function load($filepath)
109109 Environment::increaseTimeLimitTo (3600 );
110110 Environment::increaseMemoryLimitTo ('512M ' );
111111
112- if ($ this ->useTransaction ) {
113- DB ::get_conn ()->transactionStart ();
114- }
115-
116- try {
117- //get all instances of the to be imported data object
118- if ($ this ->deleteExistingRecords ) {
119- if ($ this ->getCheckPermissions ()) {
120- // We need to check each record, in case there's some fancy conditional logic in the canDelete method.
121- // If we can't delete even a single record, we should bail because otherwise the result would not be
122- // what the user expects.
123- /** @var DataObject $record */
124- foreach (DataObject::get ($ this ->objectClass ) as $ record ) {
125- if (!$ record ->canDelete ()) {
126- $ type = $ record ->i18n_singular_name ();
127- throw new HTTPResponse_Exception (
128- _t (__CLASS__ . '.CANNOT_DELETE ' , "Not allowed to delete '{type}' records " , ["type " => $ type ]),
129- 403
130- );
131- }
112+ //get all instances of the to be imported data object
113+ if ($ this ->deleteExistingRecords ) {
114+ if ($ this ->getCheckPermissions ()) {
115+ // We need to check each record, in case there's some fancy conditional logic in the canDelete method.
116+ // If we can't delete even a single record, we should bail because otherwise the result would not be
117+ // what the user expects.
118+ /** @var DataObject $record */
119+ foreach (DataObject::get ($ this ->objectClass ) as $ record ) {
120+ if (!$ record ->canDelete ()) {
121+ $ type = $ record ->i18n_singular_name ();
122+ throw new HTTPResponse_Exception (
123+ _t (__CLASS__ . '.CANNOT_DELETE ' , "Not allowed to delete '{type}' records " , ["type " => $ type ]),
124+ 403
125+ );
132126 }
133127 }
134- DataObject::get ($ this ->objectClass )->removeAll ();
135128 }
136-
137- $ result = $ this ->processAll ($ filepath );
138-
139129 if ($ this ->useTransaction ) {
140- DB ::get_conn ()->transactionEnd ();
130+ DB ::get_conn ()->transactionStart ();
141131 }
142- } catch ( Exception $ e ) {
132+ DataObject:: get ( $ this -> objectClass )-> removeAll ();
143133 if ($ this ->useTransaction ) {
144- DB ::get_conn ()->transactionRollback ();
134+ DB ::get_conn ()->transactionEnd ();
145135 }
146- $ code = $ e ->getCode () ?: 500 ;
147- throw new HTTPResponse_Exception ($ e ->getMessage (), $ code );
148136 }
137+
138+ $ result = $ this ->processAll ($ filepath );
139+
149140 return $ result ;
150141 }
151142
@@ -186,13 +177,11 @@ protected function processAll($filepath, $preview = false)
186177 {
187178 $ this ->extend ('onBeforeProcessAll ' , $ filepath , $ preview );
188179
189- $ results = new BulkLoader_Result ();
190- $ ext = $ this ->getUploadFileExtension ();
191-
192180 if (!is_readable ($ filepath )) {
193181 throw new Exception ("Cannot read $ filepath " );
194182 }
195183
184+ $ ext = $ this ->getUploadFileExtension ();
196185 $ opts = [
197186 'separator ' => $ this ->delimiter ,
198187 'enclosure ' => $ this ->enclosure ,
@@ -204,22 +193,50 @@ protected function processAll($filepath, $preview = false)
204193
205194 $ data = SpreadCompat::read ($ filepath , ...$ opts );
206195
196+ $ results = $ this ->processData ($ data , $ preview );
197+
198+ $ this ->extend ('onAfterProcessAll ' , $ result , $ preview );
199+
200+ return $ results ;
201+ }
202+
203+ /**
204+ * @param iterable $data
205+ * @param bool $preview
206+ * @return BulkLoader_Result
207+ */
208+ public function processData ($ data , $ preview = false )
209+ {
210+ $ results = new BulkLoader_Result ();
211+
207212 $ objectClass = $ this ->objectClass ;
208213 $ objectConfig = $ objectClass ::config ();
209214 $ this ->db = $ objectConfig ->db ;
210215 $ this ->singleton = singleton ($ objectClass );
211216
212- foreach ($ data as $ row ) {
213- $ this ->processRecord (
214- $ row ,
215- $ this ->columnMap ,
216- $ results ,
217- $ preview
218- );
217+ try {
218+ if ($ this ->useTransaction ) {
219+ DB ::get_conn ()->transactionStart ();
220+ }
221+ foreach ($ data as $ row ) {
222+ $ this ->processRecord (
223+ $ row ,
224+ $ this ->columnMap ,
225+ $ results ,
226+ $ preview
227+ );
228+ }
229+ if ($ this ->useTransaction ) {
230+ DB ::get_conn ()->transactionEnd ();
231+ }
232+ } catch (Exception $ e ) {
233+ if ($ this ->useTransaction ) {
234+ DB ::get_conn ()->transactionRollback ();
235+ }
236+ $ code = $ e ->getCode () ?: 500 ;
237+ throw new HTTPResponse_Exception ($ e ->getMessage (), $ code );
219238 }
220239
221- $ this ->extend ('onAfterProcessAll ' , $ result , $ preview );
222-
223240 return $ results ;
224241 }
225242
0 commit comments