function _ops_update_score()
{
    $OID = max(0, intval($_POST['OID']));
    $CID = max(0, intval($_POST['CID']));
    $msg = "";
    loginRequireMgmt();
    if (!loginCheckPermission(USER::MGMT_TEAM)) {
        redirect("errors/401");
    }
    $itemName = "Team";
    $urlPrefix = "mgmt_team";
    $object = new Team();
    if ($OID) {
        $object->retrieve($OID, $CID);
        if (!$object->exists()) {
            $msg = "{$itemName} not found!";
        } else {
            transactionBegin();
            if ($object->updateTotalScore()) {
                Event::createEvent(EVENT::TYPE_EDIT, $object, Station::getRegistrationStation(), 0, $_POST);
                // just put the post data into the event
                transactionCommit();
                $msg = "{$itemName} updated!";
            } else {
                transactionRollback();
                $msg = "{$itemName} update failed";
            }
        }
    } else {
        $msg = "attempting to create team from ops_update_score which is not supported";
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_delete($OID = 0, $CID = 0)
{
    $OID = max(0, intval($OID));
    $CID = max(0, intval($CID));
    $msg = '';
    loginRequireMgmt();
    if (!loginCheckPermission(USER::MGMT_STATIONTYPE)) {
        redirect("errors/401");
    }
    $itemName = "Station Type";
    $urlPrefix = "mgmt_stationtype";
    $object = new StationType($OID, $CID);
    if (!$object->exists()) {
        $msg = "{$itemName} not found!";
    } else {
        transactionBegin();
        if ($object->delete()) {
            transactionCommit();
            $msg = "{$itemName} deleted!";
        } else {
            TransactionRollback();
            $msg = "{$itemName} delete failed!";
        }
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_delete($OID = 0, $CID = 0)
{
    $OID = max(0, intval($OID));
    $CID = max(0, intval($CID));
    $msg = '';
    loginRequireMgmt();
    if (!loginCheckPermission(USER::TEST_EVENT)) {
        redirect("errors/401");
    }
    $itemName = "Event";
    $urlPrefix = "test_event";
    $object = new Event($OID, $CID);
    if (!$object->exists()) {
        $msg = "{$itemName} not found!";
    } else {
        transactionBegin();
        if ($object->delete()) {
            transactionCommit();
            $msg = "{$itemName} deleted!";
        } else {
            TransactionRollback();
            $msg = "{$itemName} delete failed!";
        }
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_loaddb()
{
    $urlPrefix = "mgmt_fsl_data";
    $item = "FSLData";
    if (isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
        // open the csv file for reading
        $file_path = $_FILES['csv_file']['tmp_name'];
        $handle = fopen($file_path, 'r');
        try {
            transactionBegin();
            $o = new FSLData();
            if ($o->truncateTable() === false) {
                throw new Exception(" truncate {$item}");
            }
            while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                if (count($data) != 15) {
                    throw new Exception("wrong number of value for {$item} object");
                }
                $o = new FSLData();
                $o->set('a_tag', $data[0]);
                $o->set('a_lat', $data[1]);
                $o->set('a_lng', $data[2]);
                $o->set('b_tag', $data[3]);
                $o->set('b_lat', $data[4]);
                $o->set('b_lng', $data[5]);
                $o->set('c_tag', $data[6]);
                $o->set('c_lat', $data[7]);
                $o->set('c_lng', $data[8]);
                $o->set('l_tag', $data[9]);
                $o->set('l_lat', $data[10]);
                $o->set('l_lng', $data[11]);
                $o->set('a_rad', $data[12]);
                $o->set('b_rad', $data[13]);
                $o->set('c_rad', $data[14]);
                if ($o->create() === false) {
                    throw new Execption("Can't create {$item} object");
                }
            }
            transactionCommit();
            $msg = "Load {$item} completed";
        } catch (Exception $e) {
            transactionRollback();
            $msg = "caught exception " . $e->getMessage();
        }
        // delete csv file
        unlink($file_path);
    } else {
        $msg = "error no file uploaded";
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_update()
{
    $OID = max(0, intval($_POST['OID']));
    $CID = max(0, intval($_POST['CID']));
    $msg = "";
    loginRequireMgmt();
    if (!loginCheckPermission(USER::MGMT_TEAM)) {
        redirect("errors/401");
    }
    $itemName = "Team";
    $urlPrefix = "mgmt_team";
    $object = new Team();
    if ($OID) {
        $object->retrieve($OID, $CID);
        if (!$object->exists()) {
            $msg = "{$itemName} not found!";
        } else {
            transactionBegin();
            $object->merge($_POST);
            if ($object->update()) {
                transactionCommit();
                $msg = "{$itemName} updated!";
            } else {
                transactionRollback();
                $msg = "{$itemName} update failed";
            }
        }
    } else {
        $object->merge($_POST);
        for ($retry = 0; $retry < PIN_RETRY_MAX; $retry++) {
            $pin = Team::generatePIN();
            if (Team::getFromPin($pin) === false) {
                // not a duplicate
                $object->set('pin', $pin);
                transactionBegin();
                if ($object->create() !== false) {
                    transactionCommit();
                    $msg = "{$itemName} created!";
                    break;
                }
            }
        }
        if ($retry >= PIN_RETRY_MAX) {
            transactionRollback();
            $msg = "{$itemName} Create failed";
        }
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_loaddb()
{
    $urlPrefix = "mgmt_team";
    $item = "Team";
    if (isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
        // open the csv file for reading
        $file_path = $_FILES['csv_file']['tmp_name'];
        $handle = fopen($file_path, 'r');
        try {
            transactionBegin();
            $o = new Team();
            if ($o->deleteAll() === false) {
                throw new Exception("delete all {$item}");
            }
            while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                if (count($data) != 2) {
                    throw new Exception("wrong number of value of {$item}");
                }
                $s = School::getFromName($data[1]);
                if ($s === false) {
                    throw new Exception("Can't find shool {$data['1']}");
                }
                $pin = generatePin();
                if ($pin === false) {
                    throw new Exception("Couldn't create unique pin");
                }
                $o = new Team();
                $o->set('name', $data[0]);
                $o->set('pin', $pin);
                $o->set('schoolId', $s->get('OID'));
                if ($o->create() === false) {
                    throw new Exception("Can't create {$item} object");
                }
            }
            transactionCommit();
            $msg = "Load {$item} completed";
        } catch (Exception $e) {
            transactionRollback();
            $msg = "caught exception " . $e->getMessage();
        }
        // delete csv file
        unlink($file_path);
    } else {
        $msg = "error no file uploaded";
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_loaddb()
{
    $urlPrefix = "mgmt_cts_data";
    $item = "CTSData";
    if (isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
        // open the csv file for reading
        $file_path = $_FILES['csv_file']['tmp_name'];
        $handle = fopen($file_path, 'r');
        try {
            transactionBegin();
            $o = new CTSData();
            if ($o->truncateTable() === false) {
                throw new Exception("error on truncate of {$item}");
            }
            while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                if (count($data) != 8) {
                    throw new Exception("wrong number of value in {$item} record");
                }
                $o = new CTSData();
                $station = Station::getFromTag($data[0]);
                if ($station === false) {
                    throw new Exception("could not find station " . $data[0]);
                }
                $o->set('stationId', $station->get('OID'));
                $o->set('_1st', $data[1]);
                $o->set('_2nd', $data[2]);
                $o->set('_3rd', $data[3]);
                $o->set('_4th', $data[4]);
                $o->set('_5th', $data[5]);
                $o->set('tolerance', $data[6]);
                if ($o->create() === false) {
                    throw new Execption("Can't create {$item} object");
                }
            }
            transactionCommit();
            $msg = "Load {$item} completed";
        } catch (Exception $e) {
            transactionRollback();
            $msg = "caught exception " . $e->getMessage();
        }
        // delete csv file
        unlink($file_path);
    } else {
        $msg = "error no file uploaded";
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_update()
{
    $OID = max(0, intval($_POST['OID']));
    $CID = max(0, intval($_POST['CID']));
    $encode = isset($_POST["encode"]);
    $msg = "";
    loginRequireMgmt();
    if (!loginCheckPermission(USER::MGMT_EXT_DATA)) {
        redirect("errors/401");
    }
    $itemName = "EXT Data";
    $urlPrefix = "mgmt_ext_data";
    $object = new EXTData();
    if ($OID) {
        $object->retrieve($OID, $CID);
        if (!$object->exists()) {
            $msg = "{$itemName} not found!";
        } else {
            transactionBegin();
            $object->merge($_POST);
            $object->set("encode", $encode);
            if ($object->update()) {
                transactionCommit();
                $msg = "{$itemName} updated!";
            } else {
                transactionRollback();
                $msg = "{$itemName} update failed";
            }
        }
    } else {
        $object->merge($_POST);
        $object->set("encode", $encode);
        transactionBegin();
        if ($object->create()) {
            transactionCommit();
            $msg = "{$itemName} created!";
        } else {
            transactionRollback();
            $msg = "{$itemName} Create failed";
        }
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_update()
{
    $OID = max(0, intval($_POST['OID']));
    $CID = max(0, intval($_POST['CID']));
    $msg = "";
    loginRequireMgmt();
    if (!loginCheckPermission(USER::MGMT_STATIONTYPE)) {
        redirect("errors/401");
    }
    $itemName = "Station Type";
    $urlPrefix = "mgmt_stationtype";
    $object = new StationType();
    if ($OID) {
        $object->retrieve($OID, $CID);
        if (!$object->exists()) {
            $msg = "{$itemName} not found!";
        } else {
            transactionBegin();
            $object->merge($_POST);
            var_dump($object);
            if ($object->update()) {
                transactionCommit();
                $msg = "{$itemName} updated!";
            } else {
                transactionRollback();
                $msg = "{$itemName} update failed";
            }
        }
    } else {
        $object->merge($_POST);
        transactionBegin();
        if ($object->create()) {
            transactionCommit();
            $msg = "{$itemName} created!";
        } else {
            transactionRollback();
            $msg = "{$itemName} Create failed";
        }
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_update()
{
    $OID = max(0, intval($_POST['OID']));
    $CID = max(0, intval($_POST['CID']));
    $msg = '';
    loginRequireMgmt();
    if (!loginCheckPermission(USER::TEST_EVENT)) {
        redirect("errors/401");
    }
    $itemName = "Event";
    $urlPrefix = "test_event";
    $object = new Event();
    if ($OID) {
        $object->retrieve($OID, $CID);
        if (!$object->exists()) {
            $msg = "{$itemName} not found!";
        } else {
            transactionBegin();
            $object->merge($_POST);
            if ($object->update()) {
                transactionCommit();
                $msg = "{$itemName} updated!";
            } else {
                transactionRollback();
                $msg = "{$itemName} update failed";
            }
        }
    } else {
        $object->merge($_POST);
        transactionBegin();
        if ($object->create()) {
            transactionCommit();
            $msg = "{$itemName} created!";
        } else {
            transactionRollback();
            $msg = "{$itemName} Create failed";
        }
    }
    redirect("{$urlPrefix}/manage", $msg);
}
function _ops_loaddb()
{
    $urlPrefix = "mgmt_cpa_data";
    $item = "CPAData";
    if (isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
        // open the csv file for reading
        $file_path = $_FILES['csv_file']['tmp_name'];
        $handle = fopen($file_path, 'r');
        try {
            transactionBegin();
            $o = new CPAData();
            if ($o->truncateTable() === false) {
                throw new Exception("CTSData");
            }
            while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                if (count($data) != 4) {
                    throw new Exception("wrong number of values for {$item} record");
                }
                $o = new CPAData();
                $o->set('label', $data[0]);
                $o->set('fence', $data[1]);
                $o->set('building', $data[2]);
                $o->set('sum', $data[3]);
                if ($o->create() === false) {
                    throw new Execption("Can't create {$item} object");
                }
            }
            transactionCommit();
            $msg = "Load {$item} completed";
        } catch (Exception $e) {
            transactionRollback();
            $msg = "caught exception " . $e->getMessage();
        }
        // delete csv file
        unlink($file_path);
    } else {
        $msg = "error no file uploaded";
    }
    redirect("{$urlPrefix}/manage", $msg);
}