Trouble Creating A UDT

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
govicinity
Forum Members
Forum Members
Posts: 119
Joined: Tue Nov 22, 2011 2:22 pm

Trouble Creating A UDT

Post by govicinity »

CMS Made Simple™ 1.11.9 “Bartolome”

Interesting one here, I have some PHP code which works well, has been checked for errors etc (it's fine - hoorah!), and I'd like to make a UDT with it (I don't want to turn on the PHP function in config). But this PHP has breaks in it for <style>, which unfortunately means the UDT won't submit... it does if I take out the <style> tag and it's contents (three of them) however.

So my question, after many web searches and forum searches, is... how can I use this code? Any help appreciated.

Code: Select all

<?php
    
error_reporting(-1);

$errFlag = FALSE;

if (!isset($_GET['submit'])){ ?>

<style type="text/css">#results {display: none;} </style>

<?php
    }

    if (isset($_GET['submit'])){ 
        $hourlyRate = (isset($_GET['hourly_rate']) ? $_GET['hourly_rate'] : NULL);
        $hoursPerWeek = (isset($_GET['hours_per_week']) ? $_GET['hours_per_week'] : NULL);
        $weeksPerYear = (isset($_GET['weeks_worked_per_year']) ? $_GET['weeks_worked_per_year'] : NULL);
        $expensesPerWeek = (isset($_GET['weekly_expenses']) ? $_GET['weekly_expenses'] : NULL);

        if($hourlyRate == ''){
            $errArr['hourlyRate'] = TRUE;
            $errFlag = TRUE;
        }
        if($hourlyRate !== '' and !is_numeric($hourlyRate)){
            $errArrNum['hourlyRate'] = TRUE;
            $errFlag = TRUE;
        }
        if($hoursPerWeek == ''){
            $errArr['hoursPerWeek'] = TRUE;
            $errFlag = TRUE;
        }
        if($hoursPerWeek !== '' and !is_numeric($hoursPerWeek)){
            $errArrNum['hoursPerWeek'] = TRUE;
            $errFlag = TRUE;
        }
        if($weeksPerYear == ''){
            $errArr['weeksPerYear'] = TRUE;
            $errFlag = TRUE;
        }
        if($weeksPerYear !== '' and !is_numeric($weeksPerYear)){
            $errArrNum['weeksPerYear'] = TRUE;
            $errFlag = TRUE;
        }
        if($expensesPerWeek == ''){
            $errArr['weeklyExpenses'] = TRUE;
            $errFlag = TRUE;
        }
        if($expensesPerWeek !== '' and !is_numeric($expensesPerWeek)){
            $errArrNum['weeklyExpenses'] = TRUE;
            $errFlag = TRUE;
        }

    if($errFlag){?>

<style type="text/css">#results {display: none;}</style>

<?php
    }

    if(!$errFlag){

                $WeeklyFee = 25;
                $claimableWeeklyFee = 16;
                $NIfreeAllowance = 148;

                $weeklyIncome = $hourlyRate * $hoursPerWeek;

                $annualIncome = $hourlyRate * $hoursPerWeek * $weeksPerYear;

                $weeklyExpenses = $expensesPerWeek + $claimableWeeklyFee;

                $annualExpenses = ($expensesPerWeek + $claimableWeeklyFee) * $weeksPerYear;

                if($annualIncome <= 100000){
                    $taxFreeAllowance = 9440;
                }
                if($annualIncome > 100000){
                    $amountOver = $annualIncome - 100000;
                    $taxFreeAllowanceReduction = $amountOver / 2;
                    if($taxFreeAllowanceReduction >= 9440){
                        $taxFreeAllowance = 0;
                    }else{
                        $taxFreeAllowance = 9440 - $taxFreeAllowanceReduction;
                    }
                }

                $taxableIncomeNumber = $annualIncome - $taxFreeAllowance;
                if($taxableIncomeNumber > 0){
                    $taxableIncome = $taxableIncomeNumber;
                }else{
                    $taxableIncome = 0;
                }

                if($taxableIncome >= 32010){
                    $taxableIncomeBand1 = 32010;
                }else{
                    $taxableIncomeBand1 = $taxableIncome;
                }
    
                $taxDueBand1 = $taxableIncomeBand1 * 0.2;

                if($taxableIncome - 32010 <= 0){
                    $taxableIncomeBand2 = 0;
                }
                if($taxableIncome - 32010 > 0 and $taxableIncome - 32010 <= 117989){
                    $taxableIncomeBand2 = $taxableIncome - 32010;
                }
                if($taxableIncome - 32010 > 117989){
                    $taxableIncomeBand2 = 117989;
                }

                $taxDueBand2 = $taxableIncomeBand2 * 0.4;

                if($taxableIncome - 150000 > 0){
                    $taxableIncomeBand3 = $taxableIncome - 150000;
                }else{
                    $taxableIncomeBand3 = 0;
                }

                $taxDueBand3 = $taxableIncomeBand3 * 0.45;

                $taxDue = $taxDueBand1 + $taxDueBand2 + $taxDueBand3;

                $NIliableIncomeNumber = $weeklyIncome - $NIfreeAllowance;
                if($NIliableIncomeNumber > 0){
                    $NIliableIncome = $NIliableIncomeNumber;
                }else{
                    $NIliableIncome = 0;
                }

                if($NIliableIncome >= 797){
                    $NIliableIncomeBand1 = 797;
                }else{
                    $NIliableIncomeBand1 = $NIliableIncome;
                }

                $NIdueBand1 = $NIliableIncomeBand1 * 0.12;

                if($NIliableIncome > 797){
                    $NIliableIncomeBand2 = $NIliableIncome - 797;
                }else{
                    $NIliableIncomeBand2 = 0;
                }

                $NIdueBand2 = $NIliableIncomeBand2 * 0.02;

                $NIduePerWeek = $NIdueBand1 + $NIdueBand2;

                $NIdue = $NIduePerWeek * $weeksPerYear;

                $netReturnPAYE = $annualIncome - $taxDue - $NIdue;

                $taxableIncomeNumberDBS = $annualIncome - $taxFreeAllowance - $annualExpenses;
                if($taxableIncomeNumberDBS > 0){
                    $taxableIncomeDBS = $taxableIncomeNumberDBS;
                }else{
                    $taxableIncomeDBS = 0;
                }

                if($taxableIncomeDBS >= 32010){
                    $taxableIncomeBand1DBS = 32010;
                }else{
                    $taxableIncomeBand1DBS = $taxableIncomeDBS;
                }
    
                $taxDueBand1DBS = $taxableIncomeBand1DBS * 0.2;

                if($taxableIncomeDBS - 32010 <= 0){
                    $taxableIncomeBand2DBS = 0;
                }
                if($taxableIncomeDBS - 32010 > 0 and $taxableIncomeDBS - 32010 <= 117989){
                    $taxableIncomeBand2DBS = $taxableIncomeDBS - 32010;
                }
                if($taxableIncomeDBS - 32010 > 117989){
                    $taxableIncomeBand2DBS = 117989;
                }

                $taxDueBand2DBS = $taxableIncomeBand2DBS * 0.4;

                if($taxableIncomeDBS - 150000 > 0){
                    $taxableIncomeBand3DBS = $taxableIncomeDBS - 150000;
                }else{
                    $taxableIncomeBand3DBS = 0;
                }

                $taxDueBand3DBS = $taxableIncomeBand3DBS * 0.45;

                $taxDueDBS = $taxDueBand1DBS + $taxDueBand2DBS + $taxDueBand3DBS;

                $NIliableIncomeNumberDBS = $weeklyIncome - $NIfreeAllowance - $weeklyExpenses;
                if($NIliableIncomeNumberDBS > 0){
                    $NIliableIncomeDBS = $NIliableIncomeNumberDBS;
                }else{
                    $NIliableIncomeDBS = 0;
                }

                if($NIliableIncomeDBS >= 797){
                    $NIliableIncomeBand1DBS = 797;
                }else{
                    $NIliableIncomeBand1DBS = $NIliableIncomeDBS;
                }

                $NIdueBand1DBS = $NIliableIncomeBand1DBS * 0.12;

                if($NIliableIncomeDBS > 797){
                    $NIliableIncomeBand2DBS = $NIliableIncomeDBS - 797;
                }else{
                    $NIliableIncomeBand2DBS = 0;
                }

                $NIdueBand2DBS = $NIliableIncomeBand2DBS * 0.02;

                $NIduePerWeekDBS = $NIdueBand1DBS + $NIdueBand2DBS;

                $NIdueDBS = $NIduePerWeekDBS * $weeksPerYear;

                if($weeklyIncome <= 149){
                    $ENIdue = 0;
                }else{
                    //calculate ENI liability band 1
                        if($weeklyIncome > 797){
                            $ENIliableIncomeBand1 = 648;
                        }else{
                            $ENIliableIncomeBand1 = $weeklyIncome - 149;
                        }        
                    //calculate ENI due band 1
                        $ENIdueBand1 = $ENIliableIncomeBand1 * 0.12;
                    //calculate ENI liable income band 2
                        if($weeklyIncome > 797){
                            $ENIliableIncomeBand2 = $weeklyIncome - 797;
                        }else{
                            $ENIliableIncomeBand2 = 0;
                        }
                    //calculate ENI due band 2
                        $ENIdueBand2 = $ENIliableIncomeBand2 * 0.02;
                    //calculate ENI due per week
                        $ENIduePerWeek = $ENIdueBand1 + $ENIdueBand2;
                    //calculate ENI due per annum
                        $ENIdue = $ENIduePerWeek * $weeksPerYear;
                }

                $netReturnDBS = $annualIncome - $taxDueDBS - $NIdueDBS - $ENIdue;
     
    ?>

<style type="text/css">#taxCalculator {display: none;} </style>

<?php
    }
    }
?>
Going up, woop, woop.
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: Trouble Creating A UDT

Post by spcherub »

There are a few problems with your code (I think) as far a UDT is concerned.

1. Remove all the <?php and ?> tags and references. The UDT already packages the PHP code as required by the core. Essentially everything in the UDT should be just plain PHP.

2. Replace all inline reference to <style> with PHP print statements like

Code: Select all

print "<style>...</style>";
3. You should probably remove all style references (which is not standards-compliant anyway) into an external stylesheet and use classes or ids to "attached" the styles to the desired elements. This will also keep the styling separate from the logic.

Hope that helps to get started.

-S
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Trouble Creating A UDT

Post by calguy1000 »

also, it's bad form to output design/display level elements from php and should be avoided as much as possible. You should use php for business logic, and supply the data to smarty to do display level logic.

One way of doing this is assigning all of the output from PHP to a smarty variable, or a smarty array.

i.e:

Code: Select all

// my udt
$mydata = array();
$mydata['val1'] = 123;
$mydata['val2'] = 345;
$mydata['val3'] = 456;
$smarty->assign('mydata',$mydata);
in your smarty code you could then do:

Code: Select all

{myudt}
{if isset($mydata.val1) && $mydata.val1 > 0}
<p>Val1 is: {$mydata.val1}</p>
{/if}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
govicinity
Forum Members
Forum Members
Posts: 119
Joined: Tue Nov 22, 2011 2:22 pm

Re: Trouble Creating A UDT

Post by govicinity »

Thanks guys, this is inherited PHP, not something I have written, I should have said I stripped the PHP tags out of it before I inserted it into the new UDT creation area, there isn't the budget to rewrite this one unfortunately.

I have been scratching my head for ages this afternoon now, but now I'm going to pop the code supplied by the client in an iFrame, not an elegant solution, but it will give them what they want, without me spending loads of time unpaid working this one out.

Thanks for the pointers though, certainly will help for future projects.
Going up, woop, woop.
Post Reply

Return to “Modules/Add-Ons”