/**
  * Update custom plugin fields
  *
  * @param $event
  * @param BugData $bug
  */
 function bugUpdateData($event, BugData $bug)
 {
     require_once SPECMANAGEMENT_CORE_URI . 'specmanagement_database_api.php';
     $specmanagement_database_api = new specmanagement_database_api();
     $version_id = null;
     $type_id = null;
     $p_version_id = null;
     $bug_id = $bug->id;
     $project_id = helper_get_current_project();
     $ptime = gpc_get_string('ptime', $specmanagement_database_api->get_ptime_row($bug_id)[2]);
     $type = gpc_get_string('types', '');
     if ($bug_id != null) {
         $target_version = bug_get_field($bug_id, 'target_version');
         if (!is_null($target_version)) {
             $version_id = version_get_id($target_version);
             $version_obj = $specmanagement_database_api->get_plugin_version_row_by_version_id($version_id);
             $p_version_id = $version_obj[0];
             $type_id = $specmanagement_database_api->get_type_id($type);
         }
     }
     $work_package = preg_replace('/\\/\\/+/', '/', gpc_get_string('work_package', ''));
     switch ($event) {
         case 'EVENT_REPORT_BUG':
             $specmanagement_database_api->insert_version_row($project_id, $version_id, $type_id);
             $specmanagement_database_api->insert_source_row($bug_id, $p_version_id, $work_package);
             $specmanagement_database_api->insert_ptime_row($bug_id, $ptime);
             break;
         case 'EVENT_UPDATE_BUG':
             if (strlen($work_package) == 0) {
                 $work_package = $specmanagement_database_api->get_workpackage_by_bug_id($bug_id);
             }
             $specmanagement_database_api->update_version_row($project_id, $version_id, $type_id);
             $specmanagement_database_api->update_source_row($bug_id, $p_version_id, $work_package);
             $specmanagement_database_api->update_ptime_row($bug_id, $ptime);
             break;
     }
 }