Snippet vs. helper vs. plugin - what is the difference?

Feed 5 posts, 2 voices

Jul 4, 2008 11:36
Avatar
131 posts

I'm a bit confused on the difference between a snippet, helper, and plugin? Could a file be potentially saved as any of the three? Is there different functionality available in a snippet, plugin,helper that is not available in the other two?

 
Jul 5, 2008 18:14
Avatar
458 posts

As I understand it:

Snippet - stored in the database. Is just a "snippet" of information, which could be anything really. Mostly used to "includeSnippet()" into a Layout. This makes your layout more readable.

Helper - a piece of functionality, stored on disk. Functions get loaded only if you specifically want it to with the aid of: <?php use_helper('Pagination'); ?> after loading the functions, you can actually make use of them in your pages etc.

Plugin - a piece of functionality, stored on disk. Can be turned on/off through the admin screen. When turned on, it gets loaded on each and every page.

Hope that helps!

 
Jul 5, 2008 20:50
Avatar
131 posts

Thanks for the explanation! I'm having problems converting PHP form processing code I've used on several websites for use on Frog CMS.

The form processing code includes validation, error, and suspect phrase checking. When invalid information is entered in a form field, the user is returned to the form, and will see an error message displayed next to any field with invalid information.

I've converted the form processing code to a snippet and can send a message using the code from the contact form, when all the form fields are entered with valid information.

However, the form is not processing my validation and error checking. When invalid information is entered in the Frog CMS form, the form is not sent, but error messages aren't displaying on the form.

The code is a bit lengthy, and I wasn't sure about posting all the code on the forum. Hopefully I've entered the correct Markdown code to get it to display below.

I only need the form processing code for one page, so I created it as a snippet. Since plugins are loaded on every page, it didn't seem that a plugin was the best option for this site.

<?php 
/* set variables */
$sent_page = 'sent/';

/* if send button has been pressed, process the email message */
if (array_key_exists('send', $_POST)) {
    $to = 'myname@mysite.com';
    $subject = 'Feedback from the website';

/* list expected fields */
    $expected = array('name', 'email', 'message');

/* set required fields */
    $required = array('name', 'email', 'message');

/* create empty array for any missing fields */
    $missing = array();

/* assume that there is nothing suspect */
    $suspect = false;

/* create a pattern to locate suspect phrases, such as bcc, cc, the i at the end makes the 
pattern case-insensitive */
    $pattern = '/Content-Type:|Bcc:Cc:/i';

/* check for suspect phrases in the content of each element in a subarray (such as subarrays for 
check boxes) */
    function isSuspect($val, $pattern, &$suspect) {

/* if the variable is an array, loop through each element and pass it recursively back to the same function */
        if (is_array($val)) {
            isSuspect($item, $pattern, $suspect);
        }   
        else {
/* if one of the suspect phrases is found, set Boolean to true */
            if (preg_match($pattern, $val)) {
                $suspect = true;
            } /* end if preg_match */
        } /* end if is_array */
    } /* end isSuspect function  */

    isSuspect($_POST, $pattern, $suspect);

/* if suspect phrases are found, set mailSent to false, delete missing array to prevent email from being sent, 
and prevent processing the _POST array */
    if ($suspect) {
        $mailSent = false;
        unset($missing);
    }
    else {

/* process $_POST variables  - loop through $_POST array, strip out whitespace from user input, assign 
contents to variable with the same name. 
If required fields are blank, add name attribute to $missing array */
    foreach ($_POST as $key => $value) {

/* assign to temporary variable and strip whitespace if not an array */
        $temp = is_array($value) ? $value :trim($value);

/* if empty and required, add to $missing array */
        if (empty($temp) && in_array($key, $required)) {
            array_push($missing, $key);
            }
/* otherwise, assign to variable of the same name as $key */
        elseif (in_array($key, $expected)) {
            ${$key} = $temp;
        } /* end if */
    } /* end foreach */ 
} /* end else of if $suspect */     

/* validate email address */
if (!empty($email)) {

/* check for illegal characters in email address */
    $checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';

/* reject the email address if it doesn't match */
    if (!preg_match($checkEmail, $email)) {
        array_push($missing, 'email');
    } /* end preg_match */      
} /* end if !empty */

/* if no suspected phrases & all required fields filled in, create message */
if (!$suspect && empty($missing)) { 

/* build  message */
$mail_message = "Feedback from the website on ". date("l j F Y H:i:s") . "\n\n";    
$mail_message .= "  Name: $name\n";
$mail_message .= "  Email: $email\n"; 
$mail_message .= "  Message: \n  $message\n";

/* limit message line length to 70 characters */
$mail_message = wordwrap($mail_message, 70);
$additionalHeaders = "From: Mysite<myname@mysite.com>\r\n";
if (!empty($email)) {
$additionalHeaders .= "Reply-To: $email";
}

/* send message */
$mailSent =  mail($to, $subject, $mail_message, $additionalHeaders); 
/* mail sent, redirect to confirmation page */      
if ($mailSent) {
    header("location: $sent_page");
exit;
} /* end if mailSent */
} /* end if empty($missing) */
} /* end if array_key_exists */
?>
 
Jul 5, 2008 20:52
Avatar
131 posts

Since I'm now reporting a problem with code, perhaps this message should be moved to the General Bug Reports/Problems forum?

 
Jul 6, 2008 21:04
Avatar
131 posts

Any suggestions? I've been working on this code for four days, and am not sure how to proceed. I contacted David a few days ago about the issue, but neither of us were able to find a solution. David suggested I change the code to a "helper" file, which I did, but I still couldn't get the page to display validation and other error messages.

I've used this code on the same server, as a regular PHP page, and it works well.

 
Jul 10, 2008 12:06
Avatar
131 posts

I ended up putting all the code, form processing, and the page layout in a Layout. After spending 2 hours troubleshooting with another PHP programmer, we came to the conclusion that Frog was stripping out variables that were being used in two different snippets. We tried using the Global variables as well, but nothing worked. I've posted a request on the forums asking about variables in Frog, but haven't heard back.