@@ -31,6 +31,10 @@ class Author extends Model {
3131 this . additionalData = authorData . additionalData ;
3232 }
3333
34+ if ( authorData . imageConfigFields ) {
35+ this . imageConfigFields = authorData . imageConfigFields ;
36+ }
37+
3438 if ( authorData . name || authorData . name === '' ) {
3539 this . name = authorData . name ;
3640 this . username = authorData . username ;
@@ -89,11 +93,11 @@ class Author extends Model {
8993 }
9094
9195 if ( this . id !== 0 ) {
96+ this . checkAndCleanImages ( ) ;
9297 return this . updateAuthor ( ) ;
9398 }
9499
95100 this . checkAndCleanImages ( ) ;
96-
97101 return this . addAuthor ( ) ;
98102 }
99103
@@ -291,23 +295,26 @@ class Author extends Model {
291295 */
292296 cleanImages ( images , imagesDir , cancelEvent ) {
293297 let authorDir = this . id ;
294- let featuredImage = false ;
295-
298+ let featuredImage = '' ;
299+ let viewConfig = { } ;
300+
296301 if ( this . additionalData && this . additionalData . featuredImage ) {
297302 featuredImage = path . parse ( this . additionalData . featuredImage ) . base ;
298303 }
299304
305+ if ( this . additionalData && this . additionalData . viewConfig ) {
306+ viewConfig = this . additionalData . viewConfig ;
307+ }
308+
300309 // If author is cancelled - get the previous featured image
301310 if ( cancelEvent && this . id !== 0 ) {
302- let featuredImageSqlQuery = `SELECT additional_data FROM authors WHERE id = @id` ;
303-
304- let featuredImageResult = this . db . prepare ( featuredImageSqlQuery ) . all ( {
305- id : this . id
306- } ) ;
311+ let additionalDataQuery = `SELECT additional_data FROM authors WHERE id = @id` ;
312+ let additionalDataResult = this . db . prepare ( additionalDataQuery ) . all ( { id : this . id } ) ;
307313
308- if ( featuredImageResult && featuredImageResult [ 0 ] ) {
314+ if ( additionalDataResult ) {
309315 try {
310- featuredImage = JSON . parse ( featuredImageResult [ 0 ] . additional_data ) . featuredImage ;
316+ featuredImage = JSON . parse ( additionalDataResult [ 0 ] . additional_data ) . featuredImage ;
317+ viewConfig = JSON . parse ( additionalDataResult [ 0 ] . additional_data ) . viewConfig ;
311318 } catch ( e ) {
312319 console . log ( '(!) An issue occurred during parsing author additional data' , this . id ) ;
313320 }
@@ -318,11 +325,13 @@ class Author extends Model {
318325 authorDir = 'temp' ;
319326 }
320327
321- let imagesInAuthorViewSettings = [ ] ;
328+ let imagesInViewSettings = [ ] ;
322329
323- if ( this . additionalData && this . additionalData . viewConfig ) {
324- imagesInAuthorViewSettings = Object . values ( this . additionalData . viewConfig ) . filter ( item => item . type === "image" ) . map ( item => item . value ) ;
325- }
330+ imagesInViewSettings = Object . keys ( viewConfig ) . filter ( ( fieldName ) => {
331+ return this . imageConfigFields . indexOf ( fieldName ) !== - 1 && viewConfig [ fieldName ] !== '' ;
332+ } ) . map ( ( fieldName ) => {
333+ return viewConfig [ fieldName ] ;
334+ } ) ;
326335
327336 // Iterate through images
328337 for ( let i in images ) {
@@ -335,10 +344,10 @@ class Author extends Model {
335344 }
336345
337346 // Remove files which does not exist as featured image and authorViewSettings
338- if (
347+ if (
339348 ( cancelEvent && authorDir === 'temp' ) ||
340349 (
341- imagesInAuthorViewSettings . indexOf ( imagePath ) === - 1 &&
350+ imagesInViewSettings . indexOf ( imagePath ) === - 1 &&
342351 featuredImage !== imagePath
343352 )
344353 ) {
0 commit comments