@@ -17,8 +17,8 @@ class Module extends FormToolsModule
1717 protected $ author = "Ben Keen " ;
1818 protected $ authorEmail = "ben.keen@gmail.com " ;
1919 protected $ authorLink = "https://formtools.org " ;
20- protected $ version = "2.0.3 " ;
21- protected $ date = "2018-01-28 " ;
20+ protected $ version = "2.0.4 " ;
21+ protected $ date = "2018-01-30 " ;
2222 protected $ originLanguage = "en_us " ;
2323
2424 protected $ jsFiles = array (
@@ -28,7 +28,6 @@ class Module extends FormToolsModule
2828
2929 protected $ nav = array (
3030 "module_name " => array ("index.php " , false ),
31- "phrase_reset_field_settings " => array ("reset.php " , false ),
3231 "word_help " => array ("help.php " , false )
3332 );
3433
@@ -151,12 +150,10 @@ class Module extends FormToolsModule
151150 "rsv_field_name " => "" ,
152151 "custom_function " => "cf_tinymce_settings.check_required " ,
153152 "custom_function_required " => "yes " ,
154- "default_error_message " => "{ \$LANG.validation_default_rule_required} " ,
155- "list_order " => 1
153+ "default_error_message " => "{ \$LANG.validation_default_rule_required} "
156154 );
157155
158156
159- // orders get applied by the order they appear in this data structure
160157 private static $ fieldTypeSettings = array (
161158 array (
162159 "field_label " => "Toolbar " ,
@@ -232,6 +229,7 @@ public function install($module_id)
232229
233230 // check it's not already installed
234231 $ field_type_info = FieldTypes::getFieldTypeByIdentifier ("tinymce " );
232+
235233 if (!empty ($ field_type_info )) {
236234 return array (false , $ LANG ["notify_module_already_installed " ]);
237235 }
@@ -283,10 +281,13 @@ public function uninstall($module_id)
283281 }
284282
285283
286- public function upgrade ($ module_id , $ old_module_version )
284+ public function upgrade ($ module_id , $ old_module_version )
287285 {
288286 $ this ->resetHooks ();
289- $ this ->resetFieldType ($ module_id );
287+
288+ if ($ this ->getVersion () == "2.0.4 " ) {
289+ $ this ->resetFieldType ($ module_id );
290+ }
290291 }
291292
292293
@@ -382,7 +383,7 @@ public function updateSettings($info)
382383 }
383384
384385 /**
385- * Called on upgrading to 2.0.4, and via the UI . This resets the module to its factory defaults retaining the original
386+ * Called on upgrading to 2.0.4. This resets the module to its factory defaults retaining the original
386387 * database integrity, i.e. the field type, field type settings, validation and options are all *updated* in place,
387388 * rather than wiping them out and reinserting.
388389 *
@@ -411,7 +412,7 @@ public static function resetFieldType($module_id)
411412 group_id = :group_id,
412413 is_file_field = :is_file_field,
413414 is_date_field = :is_date_field,
414- raw_field_type_map = :raw_field_type_map,
415+ raw_field_type_map = :raw_field_type_map,
415416 raw_field_type_map_multi_select_id = :raw_field_type_map_multi_select_id,
416417 list_order = :list_order,
417418 compatible_field_sizes = :compatible_field_sizes,
@@ -422,7 +423,7 @@ public static function resetFieldType($module_id)
422423 edit_field_smarty_markup = :edit_field_smarty_markup,
423424 php_processing = :php_processing,
424425 resources_css = :resources_css,
425- resources_js = resources_js
426+ resources_js = : resources_js
426427 WHERE field_type_id = :field_type_id
427428 " );
428429 $ db ->bindAll (array (
@@ -461,12 +462,16 @@ public static function resetFieldType($module_id)
461462 rsv_field_name = :rsv_field_name,
462463 custom_function = :custom_function,
463464 custom_function_required = :custom_function_required,
464- default_error_message = :default_error_message,
465- WHERE field_type_id = :field_type_id
465+ default_error_message = :default_error_message
466+ WHERE rule_id = :rule_id
466467 " );
468+
467469 $ db ->bindAll (self ::$ validationRecordMap );
468470 $ db ->bind ("rule_id " , $ rule ["rule_id " ]);
469471 $ db ->execute ();
472+
473+ // clean up any old field type settings that are no longer applicable
474+ self ::removeOldFieldTypeSettings ($ field_type_id );
470475 }
471476
472477
@@ -515,6 +520,7 @@ private static function addValidation ($field_type_id)
515520 " );
516521 $ db ->bindAll (self ::$ validationRecordMap );
517522 $ db ->bind ("field_type_id " , $ field_type_id );
523+ $ db ->bind ("list_order " , 1 );
518524 $ db ->execute ();
519525 }
520526
@@ -557,14 +563,42 @@ private static function addFieldSettingOptions($setting_id, $options)
557563 {
558564 $ db = Core::$ db ;
559565
560- $ cols = array ("setting_id " , "option_text " , "option_value " , "option_order " , "is_new_sort_group " );
561-
562- $ data = array ();
566+ $ order = 1 ;
563567 foreach ($ options as $ row ) {
564- $ row ["setting_id " ] = $ setting_id ;
565- $ data [] = $ row ;
568+ $ db ->query ("
569+ INSERT INTO {PREFIX}field_type_setting_options (setting_id, option_text, option_value, option_order, is_new_sort_group)
570+ VALUES (:setting_id, :option_text, :option_value, :option_order, :is_new_sort_group)
571+ " );
572+ $ db ->bindAll (array (
573+ "setting_id " => $ setting_id ,
574+ "option_text " => $ row ["option_text " ],
575+ "option_value " => $ row ["option_value " ],
576+ "option_order " => $ order ,
577+ "is_new_sort_group " => $ row ["is_new_sort_group " ]
578+ ));
579+ $ db ->execute ();
580+
581+ $ order ++;
566582 }
567- $ db ->insertQueryMultiple ("field_type_setting_options " , $ cols , $ data );
568583 }
569584
585+
586+ private static function removeOldFieldTypeSettings ($ field_type_id )
587+ {
588+ $ db = Core::$ db ;
589+ $ known_field_type_setting_identifiers = array_column (self ::$ fieldTypeSettings , "field_setting_identifier " );
590+
591+ $ identifiers = implode ("', ' " , $ known_field_type_setting_identifiers );
592+ $ db ->query ("
593+ SELECT setting_id
594+ FROM {PREFIX}field_type_settings
595+ WHERE field_type_id = :field_type_id AND
596+ field_setting_identifier NOT IN (' $ identifiers')
597+ " );
598+ $ db ->bind ("field_type_id " , $ field_type_id );
599+ $ db ->execute ();
600+
601+ $ old_field_settings = $ db ->fetchAll (PDO ::FETCH_COLUMN );
602+ FieldTypes::deleteFieldTypeSettings ($ old_field_settings );
603+ }
570604}
0 commit comments