/**
  * Deletes tabs and private fields of plugin id
  *
  * @param int $id   id of plugin
  */
 function deleteTabAndFieldsOfPlugin($id)
 {
     global $_CB_database;
     //Find all tabs related to this plugin
     $_CB_database->setQuery("SELECT `tabid`, `fields` FROM #__comprofiler_tabs WHERE pluginid=" . (int) $id);
     $tabs = $_CB_database->loadObjectList();
     if (count($tabs) > 0) {
         $rowTab = new TabTable();
         foreach ($tabs as $tab) {
             //Find all fields related to the tab
             $_CB_database->setQuery("SELECT `fieldid`, `name` FROM #__comprofiler_fields WHERE `tabid`=" . (int) $tab->tabid . " AND `pluginid`=" . (int) $id);
             $fields = $_CB_database->loadObjectList();
             $rowField = new FieldTable();
             //Delete fields and fieldValues, but not data content itself in the comprofilier table so they stay on reinstall
             if (count($fields) > 0) {
                 //delete each field related to a tab and all field value related to a field, but not the content
                 foreach ($fields as $field) {
                     //Now delete the field itself without deleting the user data, preserving it for reinstall
                     //$rowField->deleteColumn('#__comprofiler',$field->name);	// this would delete the user data
                     $rowField->delete($field->fieldid);
                 }
             }
             if ($tab->fields) {
                 $_CB_database->setQuery("SELECT COUNT(*) FROM #__comprofiler_fields WHERE tabid=" . (int) $tab->tabid);
                 $fieldsCount = $_CB_database->loadResult();
                 if ($fieldsCount > 0) {
                     $_CB_database->setQuery("UPDATE #__comprofiler_tabs SET `pluginclass`=null, `pluginid`=null WHERE `tabid`=" . (int) $tab->tabid);
                     $_CB_database->query();
                 } else {
                     //delete each tab
                     $rowTab->delete($tab->tabid);
                 }
             } else {
                 //delete each tab
                 $rowTab->delete($tab->tabid);
             }
         }
     }
     //Find all fields related to this plugin which are in other tabs, are calculated and delete them as they are of no use anymore:
     $_CB_database->setQuery("SELECT `fieldid`, `name` FROM #__comprofiler_fields WHERE `calculated`=1 AND `sys`=0 AND `pluginid`=" . (int) $id);
     $fields = $_CB_database->loadObjectList();
     $rowField = new FieldTable();
     if (count($fields) > 0) {
         foreach ($fields as $field) {
             //Now delete the field itself:
             $rowField->delete($field->fieldid);
         }
     }
     //Find all fields related to this plugin and set to NULL the now uninstalled plugin.
     $_CB_database->setQuery("SELECT COUNT(*) FROM #__comprofiler_fields WHERE pluginid=" . (int) $id);
     $fieldsNumber = $_CB_database->loadResult();
     if ($fieldsNumber > 0) {
         $_CB_database->setQuery("UPDATE #__comprofiler_fields SET pluginid = NULL WHERE pluginid=" . (int) $id);
         $_CB_database->query();
     }
 }