/**
  * does the actual validation for variables
  *
  * @param string $ifldname
  * @param string $fldopts
  * @return bool
  */
 private function validateField($ifldname, $fldopts)
 {
     $invalidated = false;
     $fldkey = $fldopts["dispkey"];
     $disp = $fldopts["disp"];
     if ($this->fldis("data", $ifldname)) {
         $fldname = base64_decode($ifldname);
         if (!ucfs::valid($fldname)) {
             $invalidated = ucfs::ferror($fldname);
         }
     } else {
         /* retrieve data from field options */
         if ($this->fields[$ifldname]["type"] == "date") {
             $value = "{$fldopts['year']}-{$fldopts['month']}-{$fldopts['day']}";
         } else {
             $value = $fldopts["value"];
         }
         $datatype = $fldopts["datatype"];
         if (!isset($fldopts["min"])) {
             $fldopts["min"] = "";
         }
         if (!isset($fldopts["max"])) {
             $fldopts["max"] = "";
         }
         $min = $fldopts["min"];
         $max = $fldopts["max"];
         /* mark field as being validated */
         $this->fields[$ifldname]["validated"] = true;
         $invalidated = cForm::validateValue($value, $datatype, $min, $max);
     }
     /* store error if any */
     if ($invalidated !== false) {
         if (!isset($this->errors[$fldkey])) {
             $this->errors[$fldkey] = array();
         }
         $this->errors[$fldkey][$ifldname] = "{$disp}. {$invalidated}";
     } else {
         return true;
     }
 }
function import($frm)
{
    /* @var $frm cForm */
    if ($frm->validate("import")) {
        return view($frm);
    }
    /* get field indexes */
    $stkcod = false;
    $price = false;
    foreach ($_REQUEST["fld"] as $fi => $ft) {
        if ($ft != "ignore") {
            ${$ft} = $fi;
        }
    }
    /* import file if all field types specified */
    if ($stkcod === false || $price === false) {
        $frm->setmsg("<li class='err'>Not all field types satisfied</li>");
    } else {
        $qry = new dbSelect("spricelist", "exten", grp(m("cols", "listid"), m("where", "suppid='{$_REQUEST['supid']}'")));
        $qry->run();
        if ($qry->num_rows() <= 0) {
            $suppinfo = qrySupplier($_REQUEST["supid"]);
            $cols = grp(m("suppid", $_REQUEST["supid"]), m("listname", $suppinfo["supname"]), m("div", USER_DIV));
            $upd = new dbUpdate("spricelist", "exten", $cols);
            $upd->run(DB_INSERT);
            $listid = $upd->lastid("listid");
        } else {
            $listid = $qry->fetch_result();
        }
        $upd = new dbDelete("splist_prices", "exten", "listid='{$listid}'");
        $upd->run();
        $upd = new dbUpdate("splist_prices", "exten");
        $invalid_fields = array();
        $nosuch_fields = array();
        $file = ucfs::file("supplist");
        foreach ($file as $rd) {
            $ri = explode(",", $rd);
            $ri[$stkcod] = trim($ri[$stkcod]);
            $ri[$price] = trim($ri[$price]);
            if (cForm::validateValue($ri[$stkcod], "string", 1, 250) || cForm::validateValue($ri[$price], "float", 1, 40)) {
                $invalid_fields[] = $ri[$stkcod];
                continue;
            }
            $stkid = suppStkid($_REQUEST["supid"], $ri[$stkcod]);
            if ($stkid === false) {
                $stkinfo = array("stkid" => "0", "catid" => "0", "prdcls" => "0");
            } else {
                $stkinfo = qryStock($stkid, "stkid, catid, prdcls");
            }
            if (!isset($_REQUEST["vatinc"])) {
                $ri[$price] += $ri[$price] * TAX_VAT / 100;
            }
            $cols = grp(m("listid", $listid), m("stkid", $stkinfo["stkid"]), m("catid", $stkinfo["catid"]), m("clasid", $stkinfo["prdcls"]), m("price", $ri[$price]), m("div", USER_DIV), m("supstkcod", $ri[$stkcod]));
            $upd->setCols($cols);
            $upd->run();
        }
        if (count($invalid_fields) > 0) {
            $msg = "<br />The following items weren't imported because they contain\n\t\t\t\tinvalid values for either the stock code or the price:<br />";
            foreach ($invalid_fields as $v) {
                $msg .= "&nbsp;&nbsp;&nbsp;&nbsp;- {$v}<br />";
            }
        } else {
            $msg = "";
        }
        $frm->setmsg("<li class='err'>Successfully imported new pricelist.{$msg}</li>");
    }
    return view($frm);
}