/**
  * 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 moscomprofilerTabs($_CB_database);
         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 moscomprofilerFields($_CB_database);
             //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);
                 }
             }
             $fcount = 0;
             if ($tab->fields) {
                 $_CB_database->setQuery("SELECT COUNT(*) FROM #__comprofiler_fields WHERE tabid=" . (int) $tab->tabid);
                 $fcount = $_CB_database->loadResult();
                 if ($fcount > 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 moscomprofilerFields($_CB_database);
     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();
     }
 }
function removeTabs($cid, $option)
{
    global $_CB_database, $_CB_framework;
    if (!is_array($cid) || count($cid) < 1) {
        echo "<script type=\"text/javascript\"> alert('" . addslashes(CBTxt::T('Select an item to delete')) . "'); window.history.go(-1);</script>\n";
        exit;
    }
    $msg = '';
    if (count($cid)) {
        $obj = new moscomprofilerTabs($_CB_database);
        foreach ($cid as $id) {
            $noDelete = 0;
            $obj->load((int) $id);
            if (!$_CB_framework->acl->amIaSuperAdmin()) {
                if (!in_array($obj->useraccessgroupid, $_CB_framework->acl->get_groups_below_me(null, true))) {
                    echo "<script type=\"text/javascript\"> alert('" . addslashes(CBTxt::T('Unauthorized Access')) . "'); window.history.go(-1);</script>\n";
                    exit;
                }
            }
            $_CB_database->setQuery("SELECT COUNT(*) FROM #__comprofiler_fields WHERE tabid=" . (int) $id);
            $onField = $_CB_database->loadResult();
            if ($obj->sys > 0) {
                $msg .= sprintf(CBTxt::T('%s cannot be deleted because it is a system tab.'), getLangDefinition($obj->title)) . " \n";
                $noDelete = 1;
            }
            if ($obj->pluginid) {
                $plugin = new moscomprofilerPlugin($_CB_database);
                if ($plugin->load($obj->pluginid)) {
                    $msg .= sprintf(CBTxt::T('%s cannot be deleted because it is a tab belonging to an installed plugin.'), getLangDefinition($obj->title)) . " \n";
                    $noDelete = 1;
                }
            }
            if ($onField > 0) {
                $msg .= sprintf(CBTxt::T('%s is being referenced by an existing field and cannot be deleted!'), getLangDefinition($obj->title));
                $noDelete = 1;
            }
            if ($noDelete == 0) {
                $obj->delete($id);
                $msg .= $obj->getError();
            }
        }
    }
    if ($msg) {
        echo "<script type=\"text/javascript\"> alert('" . str_replace("\n", '\\n', addslashes($msg)) . "'); window.history.go(-1);</script>\n";
        exit;
    }
    cbRedirect($_CB_framework->backendUrl("index.php?option={$option}&task=showTab"));
}