loadTemplatefile("page.html", true, true); $tpl->setVariable("TITLE", "Lye Calculator"); // $tpl->setVariable("TIP", getPageTip()); $tpl->setVariable("ACTIVECALCS", ' class="activelink"'); $tpl->setVariable("MAINCONTENT", getMainContent()); // $tpl->setVariable("LEFTNAVBOX", getLeftNavBox()); // $tpl->setVariable("RIGHTCONTENT", getShoppingCartAdd()); // $tpl->setVariable("LEFTSPECIAL", getLeftSpecial()); $tpl->setVariable("COPYRIGHT", getCopyRightNotice("..")); $tpl->setVariable("OFFICE_INFO", getOfficeInfo("..")); if ((isset($_POST['naohpercent']) && (is_numeric($_POST['naohpercent'])))) $tpl->setVariable('NAOHPERCENT', $_POST['naohpercent']); else $tpl->setVariable('NAOHPERCENT', 25); $tpl->parse(); $tpl->show(); function getMainContent() { global $dbHNDL; $content = ''; $_State = isset($_REQUEST["_State"]) ? $_REQUEST["_State"] : 'Default'; // more variable from the web $validScreens = array( "Default" => "EnterRecipe", "New Recipe" => "EnterRecipe", "Edit Recipe" => "EditRecipe", "Calculate Lye" => "CalculateLye", "Resize Recipe" => "Resize" ); /**** if ($dbHNDL == 0) { header("Location: /db_error.html"); exit; } ****/ if ('Calculate Lye' == $_State) { return CalculateLye($_POST['fat_amt'], isset($_POST['lye_solution']) ? true : false, $_POST['solutionPercent'], $_POST['title'], $_POST['author'], $_POST['liquid'], $_POST['notes'], $_POST['unitmeasure'], $_POST['lyetype'], isset($_POST['basic_soap']) ? true : false, isset($_POST['sodiumlactate']) ? true : false, is_numeric($_POST['kohpercent']) ? $_POST['kohpercent'] : 75 , is_numeric($_POST['naohpercent']) ? $_POST['naohpercent'] : 25); } if ('Resize Recipe' == $_State) { return Resize($_POST['fat_amt'], $_POST['lye_solution'], $_POST['solutionPercent'], $_POST['title'], $_POST['author'], $_POST['liquid'], $_POST['notes'], $_POST['unitmeasure'], $_POST['lyetype'], $_POST['basic_soap'], preg_replace('/,/', ".", Sanitize($_POST['resized'])), $_POST['total_fat'], $_POST['sodiumlactate'], is_numeric($_POST['kohpercent']) ? $_POST['kohpercent'] : 75 , is_numeric($_POST['naohpercent']) ? $_POST['naohpercent'] : 25); } if ('Edit Recipe' == $_State) { return EditRecipe($_POST['title'], $_POST['author'], $_POST['unitmeasure'], $_POST['lyetype'], $_POST['lye_solution'], $_POST['solutionPercent'], $_POST['basic_soap'], $_POST['liquid'], $_POST['notes'], $_POST['fat_amt'], $_POST['sodiumlactate'], $_POST['kohpercent'], $_POST['naohpercent']); } return EnterRecipe(); } function getLeftNavBox() { $content = << EOT; return $content; } function getLeftSpecial() { global $specials; $featuredProductsList = BuildFeaturedSet($specials, "Specials"); $content = buildContent($featuredProductsList, "Templates"); return $content; } function getRightContent() { global $features; $featuredProductsList = BuildFeaturedSet($features, "Featured"); $content = buildContent($featuredProductsList, "Templates"); return $content; } function EnterRecipe() { return EditRecipe("", "", "", "", false, "", true, "", "", 0, false, 75, 25); } function EditRecipe($title, $author, $unitmeasure, $lyetype, $lye_solution, $solutionPercent, $basic_soap, $liquid, $notes, $fat_amt, $sodiumlactate, $kohpercent, $naohpercent) { global $dbHNDL; $tpl = new HTML_Template_IT("Templates"); $tpl->loadTemplatefile("LyeCalc.html", true, false); if (isset($title)) { $tpl->setVariable('RECIPE_TITLE', $title); } if (isset($author)) { $tpl->setVariable('RECIPE_AUTHOR', $author); } if (('ounces' == $unitmeasure) || ('' == $unitmeasure)) { $tpl->setVariable('OUNCES_CHECKED', 'CHECKED'); } elseif ('lbs' == $unitmeasure) { $tpl->setVariable('LBS_CHECKED', 'CHECKED'); } elseif ('grams' == $unitmeasure) { $tpl->setVariable('GRAMS_CHECKED', 'CHECKED'); } if (('NaOH' == $lyetype) || ('' == $lyetype)) { $tpl->setVariable('NaOH_CHECKED', 'CHECKED'); $kohpercent = 0; $naohpercent = 100; } elseif ('KOH' == $lyetype) { $tpl->setVariable('KOH_CHECKED', 'CHECKED'); $kohpercent = 100; $naohpercent = 0; } elseif ('BLEND' == $lyetype) { $tpl->setVariable('BLEND_CHECK', 'CHECKED'); if (!is_numeric($kohpercent)) { $kohpercent = 75; $naohpercent = 25; } $tpl->setVariable('BLEND_CHECKED', 'CHECKED'); } $tpl->setVariable('KOHPERCENT', $kohpercent); $tpl->setVariable('NAOHPERCENT', $naohpercent); if ($lye_solution) { $tpl->setVariable('LYE_SOLUTION_CHECK', 'CHECKED'); } if (isset($solutionPercent)) { $tpl->setVariable('SOLUTION_PERCENT', $solutionPercent); } if ($sodiumlactate) { $tpl->setVariable('CHECK_LACTATE', 'CHECKED'); } if ($basic_soap) { $tpl->setVariable('CHECK_BASIC', 'CHECKED'); } if (isset($liquid)) { $tpl->setVariable('LIQUID', $liquid); } if (isset($notes)) { $tpl->setVariable('NOTES', $notes); } $query = "SELECT * FROM LyeCalcData WHERE source_type = 'veg'" . " ORDER BY oil_name;"; $ccode = pg_query($dbHNDL, $query); if (!$ccode) { return "A database error occured."; } $breakCount = (int)(pg_num_rows($ccode) / 2); $count = 1; $bank = 0; $vegList = array(0 => '', 1 => ''); while ($row = pg_fetch_array($ccode)) { if (isset($fat_amt[$row['short_tag']])) { $fatValue = " value=\"". $fat_amt[$row['short_tag']] . "\""; } else { $fatValue = ""; } $vegList[$bank] .= "= 48 && event.charCode <= 57)))\" size=\"3\" name=\"fat_amt[" . $row['short_tag'] . "]\"$fatValue> "; if ($row['cat_link'] != '') { $vegList[$bank] .= ""; } $vegList[$bank] .= $row['oil_name']; if ($row['cat_link'] != '') { $vegList[$bank] .= ""; } $vegList[$bank] .= "
\n"; if ($count == $breakCount) { $bank++; } $count++; } $animalList = ''; $query = "SELECT * FROM LyeCalcData WHERE source_type = 'animal'" . " ORDER BY oil_name;"; $ccode = pg_query($dbHNDL, $query); if (!$ccode) { return "A database error occured."; } while ($row = pg_fetch_array($ccode)) { if (isset($fat_amt[$row['short_tag']])) { $fatValue = " value=\"". $fat_amt[$row['short_tag']] . "\""; } else { $fatValue = ""; } $animalList .= "= 48 && event.charCode <= 57)))\" size=\"3\" name=\"fat_amt[" . $row['short_tag'] . "]\"$fatValue> "; if ($row['cat_link'] != '') { $animalList .= ""; } $animalList .= $row['oil_name']; if ($row['cat_link'] != '') { $animalList .= ""; } $animalList .= "
\n"; } $blendList = ''; $query = "SELECT * FROM LyeCalcData WHERE source_type = 'blend'" . " ORDER BY oil_name;"; $ccode = pg_query($dbHNDL, $query); if (!$ccode) { return "A database error occured."; } while ($row = pg_fetch_array($ccode)) { if (isset($fat_amt[$row['short_tag']])) { $fatValue = " value=\"". $fat_amt[$row['short_tag']] . "\""; } else { $fatValue = ""; } $blendList .= "= 48 && event.charCode <= 57)))\" size=\"3\" name=\"fat_amt[" . $row['short_tag'] . "]\"$fatValue> "; if ($row['cat_link'] != '') { $blendList .= ""; } $blendList .= $row['oil_name']; if ($row['cat_link'] != '') { $blendList .= ""; } $blendList .= "
\n"; } $tpl->setVariable('VEG_LIST_1', $vegList[0]); $tpl->setVariable('VEG_LIST_2', $vegList[1]); $tpl->setVariable('ANIMAL_LIST', $animalList); $tpl->setVariable('BLEND_LIST', $blendList); $tpl->parse(); $content = $tpl->get(); return $content; } function CalculateLye($fat_amt, $lye_solution, $solutionPercent, $title, $author, $liquid, $notes, $unitmeasure, $lyetype, $basic_soap, $sodiumlactate, $kohpercent, $naohpercent) { global $dbHNDL; foreach ($fat_amt as $k => $v) { $fat_amt[$k] = preg_replace('/,/', ".", Sanitize($v)); } $lye_solution = Sanitize($lye_solution); $solutionPercent = Sanitize($solutionPercent); $title = Sanitize($title); $author = Sanitize($author); $liquid = Sanitize($liquid); $notes = Sanitize($notes); $unitmeasure = Sanitize($unitmeasure); $lyetype = Sanitize($lyetype); $basic_soap = Sanitize($basic_soap); $sodiumlactate = Sanitize($sodiumlactate); $soap = new HTML_Template_IT("Templates"); $soap->loadTemplatefile('CalculatedSoap.html', true, true); $soap->setCurrentBlock("HIDDEN_OIL_BLOCK"); $total_fat = 0; while (list($key, $value) = each($fat_amt)) { if (($value) && is_numeric($value)) { $total_fat += $value; $soap->setVariable("OIL_TAG", $key); $soap->setVariable("OIL_AMOUNT", $value); $soap->parseCurrentBlock(); } else { unset($fat_amt[$key]); } } // Check for oil entries if (0 == $total_fat) { return "Please select at least one oil for your recipe."; } if (!is_numeric($solutionPercent)) { unset($solutionPercent); } if (($lye_solution) && ($solutionPercent < 30)) { // Return error message return file_get_contents('../Content/WeakLyeSolution.txt'); } // Read db for names and saponification values $query = "SELECT * FROM LyeCalcData;"; $ccode = @pg_query($dbHNDL, $query); if (!$ccode) { return "A database error occured during calculation"; } while ($row = pg_fetch_array($ccode)) { $fatName[$row['short_tag']] = $row['oil_name']; $sv[$row['short_tag']] = $row['sap_value']; } reset($fat_amt); $soap->setCurrentBlock("OIL_ROW"); $total_lye = 0; while (list($key, $value) = each($fat_amt)) { if ($value) { $total_lye += $value * ($sv[$key] / 1000); $soap->setVariable("OIL_NAME", $fatName[$key]); $soap->setVariable("OIL_AMOUNT", $value); $soap->setVariable("OIL_PERCENT", sprintf("%6.2f", (($value /$total_fat) * 100))); $soap->parseCurrentBlock(); } } if ($lyetype == "NaOH") { $total_lye *= 0.7129; // NaOH 39.997 g/mol : KOH 56.1056 g/mol 39.997/56.1056 } if (($lyetype == "NaOH") || ($lyetype == "KOH")) { $soap->setCurrentBlock("LYE_ROW"); for ($i=0; $i<=10; $i++) { $temp = $total_lye * (1 - ($i / 100)); if ($lye_solution) { $temp = $temp / ($solutionPercent / 100); } if ($i <= 4) { $soap->setVariable("COLOR", "#FFBBBB"); $soap->setVariable("SUPERFAT", $i); $soap->setVariable("LYEAMOUNT", sprintf("%6.2f", $temp)); $soap->setVariable("STYLE_ON", ''); $soap->setVariable("STYLE_OFF", ''); $soap->parseCurrentBlock(); } else if ($i >= 9) { $soap->setVariable("COLOR", "#CCFFFF"); $soap->setVariable("SUPERFAT", $i); $soap->setVariable("LYEAMOUNT", sprintf("%6.2f", $temp)); $soap->parseCurrentBlock(); } else { $soap->setVariable("COLOR", "#CCFFCC"); $soap->setVariable("SUPERFAT", $i); $soap->setVariable("LYEAMOUNT", sprintf("%6.2f", $temp)); $soap->setVariable("STYLE_ON", ''); $soap->setVariable("STYLE_OFF", ''); $soap->parseCurrentBlock(); } } } if ($lyetype == "BLEND") { $total_KOH = $total_lye * $kohpercent * 0.01; $total_NaOH = ($total_lye * $naohpercent * 0.01) * 0.7129; $soap->setCurrentBlock("LYE_ROW"); for ($i=-10; $i<=10; $i++) { $temp_KOH = $total_KOH * (1 - ($i / 100)); $temp_NaOH = $total_NaOH * (1 - ($i / 100)); if ($i <= 4) { $soap->setVariable("COLOR", "#FFBBBB"); $soap->setVariable("SUPERFAT", $i); $soap->setVariable("LYEAMOUNT", sprintf("%1.2f / %1.2f", $temp_NaOH, $temp_KOH)); $soap->setVariable("STYLE_ON", ''); $soap->setVariable("STYLE_OFF", ''); $soap->parseCurrentBlock(); } else if ($i >= 9) { $soap->setVariable("COLOR", "#CCFFFF"); $soap->setVariable("SUPERFAT", $i); $soap->setVariable("LYEAMOUNT", sprintf("%1.2f / %1.2f", $temp_NaOH, $temp_KOH)); $soap->parseCurrentBlock(); } else { $soap->setVariable("COLOR", "#CCFFCC"); $soap->setVariable("SUPERFAT", $i); $soap->setVariable("LYEAMOUNT", sprintf("%1.2f / %1.2f", $temp_NaOH, $temp_KOH)); $soap->setVariable("STYLE_ON", ''); $soap->setVariable("STYLE_OFF", ''); $soap->parseCurrentBlock(); } } } if (isset($notes) && ('' != $notes)) { $soap->setCurrentBlock("NOTES_BLOCK"); $soap->setVariable("NOTES", preg_replace('/\n/', '
', $notes)); $soap->parseCurrentBlock(); } if ($basic_soap) { $soap->touchBlock("BASIC_SOAP_BLOCK"); } if ($sodiumlactate) { $soap->setCurrentBlock("SL_BLOCK"); if ("ounces" == $unitmeasure) $soap->setVariable("SL_UOM", "(oz wt)"); else $soap->setVariable("SL_UOM", "($unitmeasure)"); $soap->setVariable("SL_1", sprintf("%6.2f", ($total_fat * 0.01))); $soap->setVariable("SL_2", sprintf("%6.2f", ($total_fat * 0.02))); $soap->setVariable("SL_3", sprintf("%6.2f", ($total_fat * 0.03))); $soap->parseCurrentBlock(); } if ($lye_solution) { $soap->touchBlock("LYE_SOLUTION_BLOCK"); } $minLiquidFactor = 4; // For bar soap $maxLiquidFactor = 6; if ('BLEND' == $lyetype) { // For Cream Soap $minLiquidFactor = 12; $maxLiquidFactor = 20; } $soap->setCurrentBlock(); if ("ounces" == $unitmeasure) { $soap->setVariable("SELECT_OUNCE", "SELECTED"); $minFluid = ($total_fat * $minLiquidFactor / 16); $fluid = ($total_fat * $maxLiquidFactor / 16); $liquidAmount = sprintf("%6.0f to %6.0f fluid ounces", $minFluid, $fluid); } elseif ("lbs" == $unitmeasure) { $soap->setVariable("SELECT_POUNDS", "SELECTED"); $minFluid = ($total_fat * $minLiquidFactor); $fluid = ($total_fat * $maxLiquidFactor); $liquidAmount = sprintf("%6.0f to %6.0f fluid ounces", $minFluid, $fluid); } elseif ("grams" == $unitmeasure) { $soap->setVariable("SELECT_GRAMS", "SELECTED"); $minFluid = ($total_fat * $minLiquidFactor / 16); $fluid = ($total_fat * $maxLiquidFactor / 16); $liquidAmount = sprintf("%6.0f to %6.0f milliliters", $minFluid, $fluid); } $soap->setVariable("RECIPE_TITLE", $title); $soap->setVariable("CREATEDBY_AUTHOR", "Created by " . $author); $soap->setVariable("RECIPE_AUTHOR", $author); $soap->setVariable("LIQUID", $liquid); $soap->setVariable("LIQUID_AMOUNT", $liquidAmount); $soap->setVariable("RAWNOTES", $notes); $soap->setVariable("UNIT_OF_MEASURE", $unitmeasure); if ($lyetype == "BLEND") { $soap->setVariable('BLENDLABEL', '
NaOH / KOH
' . $naohpercent . ' % / ' . $kohpercent . ' %'); $soap->setVariable('KOHPERCENT', $kohpercent); $soap->setVariable('NAOHPERCENT', $naohpercent); } $soap->setVariable("TOTAL_WEIGHT", $total_fat); $soap->setVariable("LYE_TYPE", $lyetype); $soap->setVariable("LYE_SOLUTION", $lye_solution); if ($lye_solution) { $soap->setVariable("LYE_SOLUTION_TEXT", sprintf("%6.2f", $solutionPercent) . "% w/w solutions"); $soap->setVariable("SOLUTION_PERCENT", $solutionPercent); } $soap->setVariable("BASIC_SOAP", $basic_soap); $soap->setVariable("SODIUMLACTATE", $sodiumlactate); return $soap->get(); } function Resize($fat_amt, $lye_solution, $solutionPercent, $title, $author, $liquid, $notes, $unitmeasure, $lyetype, $basic_soap, $resized, $total_fat, $sodiumlactate, $kohpercent, $naohpercent) { $ratio = $resized / $total_fat; while (list($key, $value) = each($fat_amt)) { $fat_amt[$key] = $value * $ratio; } reset($fat_amt); return CalculateLye($fat_amt, $lye_solution, $solutionPercent, $title, $author, $liquid, $notes, $unitmeasure, $lyetype, $basic_soap, $sodiumlactate, $kohpercent, $naohpercent); } ?>