Search code examples
phpcode-snippetsmodx

custom PHP in MODX with a snippet


Ok, so I made a calculator in PHP and am trying to port i it into MODX via a snippet. I tried just putting all this code into the snippet and it doesn't work. It keeps giving me an error message and I can't view the resource that the snippet is on. Does anyone have any ideas? Thanks.

<?php
function selected( $val = "")
{
$units = isset( $_POST['units'] ) ? $_POST['units'] : null;
echo ( $units == $val ) ? "CHECKED='CHECKED'" : "";
}
$units = isset( $_POST['units'] ) ? $_POST['units'] : null;
$dia = isset( $_POST['dia'] ) ? $_POST['dia'] : null;
$depth = isset( $_POST['depth'] ) ? $_POST['depth'] : null;
$L = isset( $_POST['L'] ) ? $_POST['L'] : null;
$num = isset($_POST['num']) ? $_POST['num'] : null;
$waste = isset($_POST['waste']) ? $_POST['waste'] : null;
$knownvolume = isset($_POST['knownvolume']) ? $_POST['knownvolume'] : null;
$hw = isset($_POST['hw']) ? $_POST['hw'] : null;
//$denom = ( ( $units == "US" ) ? 12 : 100 );
$tubecoverage = ( ( $units == "US" ) ? 18.59 : 304.65);
$onegalcoverage = ( ( $units == "US" ) ? 231 : 3785.41);
$twogalcoverage = ( ( $units == "US" ) ? 462 : 7470.82);
$fivegalcoverage = ( ( $units == "US" ) ? 1155 : 18927.06);

$measurements = 'in or cm';
    if( $units == 'US' ){
    $measurements = 'in';
}
    if($units == 'Metric')
{
    $measurements = 'cm';
}

if($units == 'US')
    $length = 'feet';
    else if($units == 'Metric')
    $length = 'meters';
    else 
    $length = 'feet or meters';

$tubes = ($tubes);
    if($knownvolume == TRUE)
    $tubes = (($knownvolume / $tubecoverage) * ($waste + 1) * 1.05);
    else 
    $tubes = (((($dia / 2) * ($dia / 2) * pi()) / $tubecoverage) * ($waste + 1) * 1.05);

$onegal = ($tubes);
    if($knownvolume == TRUE)
    $onegal = (($knownvolume / $onegalcoverage) * ($waste + 1) * 1.05);
    else 
    $onegal = (((($dia / 2) * ($dia / 2) * pi()) / $onegalcoverage) * ($waste + 1) *     1.05);

$twogal = ($tubes);
    if($knownvolume == TRUE)
    $twogal = (($knownvolume / $twogalcoverage) * ($waste + 1) * 1.05);
    else 
    $twogal = (((($dia / 2) * ($dia / 2) * pi()) / $twogalcoverage) * ($waste + 1) * 1.05);

$fivegal = ($tubes);
    if($knownvolume == TRUE)
    $fivegal = (($knownvolume / $fivegalcoverage) * ($waste + 1) * 1.05);
    else 
$fivegal = (((($dia / 2) * ($dia / 2) * pi()) / $fivegalcoverage) * ($waste + 1) * 1.05);

?>
<form method='post' action=''>
<table border='0' width='500px' cellpadding='2' cellspacing='1' class="calculator">
<tr class="calcheading"><td><label><input type="radio" name="units" 
<?php  selected("US")?> value="US" />S.A.E.</label>
<label><input type="radio" name="units" <?php selected("Metric")?> value="Metric"   />Metric</label> </td></tr>

<tr class="calcheading"><td colspan="2"><strong>Cicular Volume Area</strong></td></tr>

<tr class="calcrow"><td>Known Volume (<?php echo $measurements ?>):</td><td align="center"><input type='text' name='knownvolume' value="<?php echo $knownvolume ?>"/>     </td></tr>

<tr class="calcrow2"><td>Dia (<?php echo $measurements ?>):</td><td align="center"><input type='text' name='dia' value="<?php echo $dia ?>"/></td></tr>

<tr class="calcrow"><td>Length (<?php echo $measurements ?>):</td><td align="center"><input type='text' name='L' value="<?php echo $L ?>"/></td></tr>

<tr class="calcrow"><td>Waste Variance % (Please add in decimal form):</td><td     align="center"><input type='text' name='waste' value="<?php echo $waste ?>"/></td></tr>

<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>

<tr class="calcrow">
<td><i>10.3 oz Tube(s) (CS-1500):</td>
<td align="center"><input type="text" value="<?php echo round($tubes, 2)?>"/></td></i>
</tr>
<tr class="calcrow">
<td><i>1 Gallon Bucket(s) (CS-1000):</td>
<td align="center"><input type="text" value="<?php echo round($onegal, 2)?>"/></td></i>
</tr>
<tr class="calcrow">
<td><i>2 Gallon Bucket(s) (CS-1800):</td>
<td align="center"><input type="text" value="<?php echo round($twogal, 2)?>"/></td></i>
</tr>
<tr class="calcrow">
<td><i>5 Gallon Bucket(s) (CS-1000/CS-1500):</td>
<td align="center"><input type="text" value="<?php echo round($fivegal, 2)?>"/></td></i>
</tr>
</table>
</form>          

Solution

  • This will not work like you expect.

    Snippets in Modx are pure php, so you can't use multiple <?php and ?>'s to separate the code. In addition, because of the way snippets are cached, you have to wrap every function within a if (!function_exists('nameoffunction').

    Here I have rewritten some of the code. The idea is that all the info that you need in the html is put in an array. I've called this $parse_arr. At the bottom of the script, I use Modx's method to replace all the placeholders from the chunk with values from the array.

    <?php
    $parse_arr = array();
    
    
    if (!function_exists('selected')) {
        function selected( $val = "") {
            $units = isset( $_POST['units'] ) ? $_POST['units'] : null;
            return ( $units == $val ) ? "CHECKED='CHECKED'" : "";
        }
    }
    
    $units = isset( $_POST['units'] ) ? $_POST['units'] : null;
    $dia = isset( $_POST['dia'] ) ? $_POST['dia'] : null;
    $depth = isset( $_POST['depth'] ) ? $_POST['depth'] : null;
    $L = isset( $_POST['L'] ) ? $_POST['L'] : null;
    $num = isset($_POST['num']) ? $_POST['num'] : null;
    $waste = isset($_POST['waste']) ? $_POST['waste'] : null;
    $knownvolume = isset($_POST['knownvolume']) ? $_POST['knownvolume'] : null;
    $hw = isset($_POST['hw']) ? $_POST['hw'] : null;
    //$denom = ( ( $units == "US" ) ? 12 : 100 );
    $tubecoverage = ( ( $units == "US" ) ? 18.59 : 304.65);
    $onegalcoverage = ( ( $units == "US" ) ? 231 : 3785.41);
    $twogalcoverage = ( ( $units == "US" ) ? 462 : 7470.82);
    $fivegalcoverage = ( ( $units == "US" ) ? 1155 : 18927.06);
    
    $measurements = 'in or cm';
        if( $units == 'US' ){
        $measurements = 'in';
    }
        if($units == 'Metric')
    {
        $measurements = 'cm';
    }
    
    if($units == 'US')
        $length = 'feet';
        else if($units == 'Metric')
        $length = 'meters';
        else 
        $length = 'feet or meters';
    
    $tubes = ($tubes);
        if($knownvolume == TRUE)
        $tubes = (($knownvolume / $tubecoverage) * ($waste + 1) * 1.05);
        else 
        $tubes = (((($dia / 2) * ($dia / 2) * pi()) / $tubecoverage) * ($waste + 1) * 1.05);
    
    $onegal = ($tubes);
        if($knownvolume == TRUE)
        $onegal = (($knownvolume / $onegalcoverage) * ($waste + 1) * 1.05);
        else 
        $onegal = (((($dia / 2) * ($dia / 2) * pi()) / $onegalcoverage) * ($waste + 1) *     1.05);
    
    $twogal = ($tubes);
        if($knownvolume == TRUE)
        $twogal = (($knownvolume / $twogalcoverage) * ($waste + 1) * 1.05);
        else 
        $twogal = (((($dia / 2) * ($dia / 2) * pi()) / $twogalcoverage) * ($waste + 1) * 1.05);
    
    $fivegal = ($tubes);
        if($knownvolume == TRUE)
        $fivegal = (($knownvolume / $fivegalcoverage) * ($waste + 1) * 1.05);
        else 
    $fivegal = (((($dia / 2) * ($dia / 2) * pi()) / $fivegalcoverage) * ($waste + 1) * 1.05);
    
    
    $parse_arr['selected_us'] = selected("US");
    $parse_arr['selected_metric'] = selected("Metric");
    $parse_arr['measurements'] = $measurements;
    
    $parse_arr['knownvolume'] = $knownvolume;
    $parse_arr['dia'] = $dia;
    
    // And so on
    
    return $modx->getChunk('name-of-chunk', $parse_arr, '[[+', ']]');
    

    Here's a part of the html. Save it as a chunk. The name must be the same as the chunk-name at the bottom of the snippet. (In this case I've called it name-of-chunk in my example).

    <form method='post' action=''>
    <table border='0' width='500px' cellpadding='2' cellspacing='1' class="calculator">
    <tr class="calcheading"><td><label><input type="radio" name="units" [[+selected_us]] value="US" />S.A.E.</label>
    <label><input type="radio" name="units" [[+selected_metric]] value="Metric"   />Metric</label> </td></tr>
    
    <tr class="calcheading"><td colspan="2"><strong>Cicular Volume Area</strong></td></tr>
    
    <tr class="calcrow"><td>Known Volume [[+measurements]]</td><td align="center"><input type='text' name='knownvolume' value="[[+knownvolume]]"/>     </td></tr>
    
    <tr class="calcrow2"><td>Dia [[+measurements]]</td><td align="center"><input type='text' name='dia' value="[[+dia]]"/></td></tr>
    

    You get the idea. Instead of having <?php echo SOMETHING; ?>, you use a placeholder [[+name-that-correspond-with-parse-arr-key]].

    You also have to write a snippet for the rounding-functions you are calling near the bottom of this chunk.

    Have fun.