Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: [MOD] Codetwists - Additional form - 1.7  (Read 2783 times)

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
[MOD] Codetwists - Additional form - 1.7
« on: September 12, 2009, 05:48:59 PM »

sNews ADDITIONAL FORM modification Version 1.0 suited for snews version 1.7

Codetwist ran to my aid when I needed an additional form in my sNews project 1.6... Now i needed this for 1.7 and I tried four times before I got it to work, hehe... I think it worked from start but I fecked things up... Now it does work however and I thought it would be visible as an 1.7 modification aswell... So without further a'do:

Codetwists - Additional form - 1.7

This MOD is based on his Contact form with custom fields (sNews 1.6), and it goes something like this:

Backity up

A "must do" before applying this MOD
1. For this MOD to function following functions should be added (either in snews.php or included from separate php file):
Code: [Select]
function get_contact_fields( $arrFields, &$code, $action ) {
   $strResult = '';
   $code = 0;
   switch ($action) {
      case 'output' :
         foreach ( $arrFields as $fieldDef ) {
            $fieldType    = $fieldDef[0];
            $fieldName    = $fieldDef[1];
            $fieldDefault = '';
            $fieldLabel   = ($fieldDef[3] == 'required') ? '* ' . $fieldDef[2] : $fieldDef[2];
            $fieldCSS     = $fieldType;
            $strResult .= html_input( $fieldType, $fieldName, $fieldName, $fieldDefault, $fieldLabel, $fieldCSS, '', '', '', '', '', '', '', '', '');
         }
         break;
      case 'submit' :
         foreach ( $arrFields as $fieldDef ) {
            $fieldName  = $fieldDef[1];
            $fieldValue = cleanText( $fieldName );
            $fieldLabel = $fieldDef[2];
            $minLength  = $fieldDef[4];
            if (!empty($fieldValue)) {
               if ( $minLength > 0 ) {
                  if ( strlen( $fieldValue ) < $minLength ) {
                     $strResult = '';
                     $code = 2;
                     break;
                  }
               }
               $strResult .= "$fieldLabel: $fieldValue\n\n";
            }
            else if ( $fieldDef[3] == 'required' ) {
               $strResult = '';
               $code = 1;
               break;
            }
         }
         break;
      default :
         break;
   }
   return $strResult;
}


function cleanText( $name ) {
   $str = trim($_POST[$name]);
   $str = clean( cleanXSS( $str ) );
   return $str;
}
Actual modification:
2.  Most modification are done by applying Codetwists new function contact() (somewhat more modified), so just copy this snippet below  which is that function although changed to fit this MOD:
Code: [Select]
// APPLICATION FORM
function applform() {
   //
   // Custom fields definition array (My custom fields also added in en.php in lang folder...)
   $arrFields[] = array('text', 'ftg', l('label_ftg'), 'required', 0);
   $arrFields[] = array('text', 'pers', l('label_pers'), 'required', 10);
   $arrFields[] = array('text', 'adress', l('label_adress'), 'required', 0);
   $arrFields[] = array('text', 'ponr', l('label_ponr'), 'required', 0);
   $arrFields[] = array('text', 'poort', l('label_poort'), 'required', 0);
   $arrFields[] = array('text', 'phone', l('label_phone'), 'required', 0);

   // Add custom fields using following definition:
   // $arrFields[] = array("<input type>", "<name>", "<label>", '<required/optional>', '<minlength/0>');
   //
   // Examples.
   // To add mandatory field with label Phone No without string length checking:
   //$arrFields[] = array('text', 'phone', 'Phone No', 'required', 0);
   // To add optional field with label City with checking for minimal string length of 4 symbols:
   //$arrFields[] = array('text', 'city', 'City', 'optional', 4);
   //
    if (!isset($_POST['applform'])) {
          //echo '<p class="required">'.l('required').'</p>';
        echo '<div class="commentsbox"><h2>'.l('applform').'</h2>';
        echo '<p class="app_text"><strong>L&#228;s detta f&#246;rst.</strong><br /><br />Detta &#228;r ett standard formul&#228;r LEED.NU anv&#228;nder sig av vid ans&#246kan av medlemskap. </p>';

        echo html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', db('website'), '');
        echo html_input('text', 'name', 'name', '', '*'.l('1name'), 'text', '', '', '', '', '', '', '', '', '');
        echo html_input('text', 'email', 'email', '', '*'.l('email'), 'text', '', '', '', '', '', '', '', '', '');
        echo html_input('text', 'weblink', 'weblink', '', l('url'), 'text', '', '', '', '', '', '', '', '', '');
      //
      // Get output for custom fields
      $strHTML = get_contact_fields( $arrFields, $code, 'output' );
      echo $strHTML;
      //
        echo html_input('textarea', 'message', 'message', '', ''.l('message'), '', '', '', '', '', '5', '5', '', '', '');
      //
        echo mathCaptcha();
        //
        echo '<p>';
        echo html_input('hidden', 'ip', 'ip', $_SERVER['REMOTE_ADDR'], '', '', '', '', '', '', '', '', '', '', '');
        echo html_input('hidden', 'time', 'time', time(), '', '', '', '', '', '', '', '', '', '', '');
        echo html_input('submit', 'applform', 'applform', l('submit'), '', 'sendbutton', '', '', '', '', '', '', '', '', '');
        echo '</p></form></div>';
        $_SESSION[db('website').'applform'] = 0;
    }
    else {
        // Fetch and clean input data from default contact form
// changed var name  and added email address
// was: $to = s('website_email');
        $contactemail = 'theemailadress@youwanttheapplicationtogotoooo.com';
        //$subject = s('contact_subject');
        $subject = s('contact_subject');
        $name = trim($_POST['name']);
        $name = strlen($name) > 1 ? clean(cleanXSS($name)) : null;
        $mail = trim($_POST['email']);
        $mail = trim($_POST['email']);
        $mail = (strlen($mail) > 7 && preg_match( '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' , $mail)) ? clean(cleanXSS($mail)) : null;
        $url = trim($_POST['weblink']);
        $url = (strlen($url) > 8 && strpos($url, '?') === false) ? clean(cleanXSS($url)) : null;
        $message = trim($_POST['message']);
        $message = strlen($message) > 9 ? stripslashes(cleanXSS($message)) : null;
        $message = strip_tags($message);
// decode html
$message = html_entity_decode($message);
// end
        $now = is_numeric($_POST['time']) ? $_POST['time'] : null;
        $ip = (strlen($_POST['ip']) < 16) ? clean(cleanXSS($_POST['ip'])) : null;
      //
      // Fetch and clean data from added custom input fields
      $validExtraFields = true;
      $code = 0;
      $strExtraFields = get_contact_fields( $arrFields, $code, 'submit' );
      if ( $code == 1 || $code == 2 ) { // Required fields values missing/invalid
         $validExtraFields = false;
      }
      //
        if($_SESSION[db('website').'applform'] == 0) {
         // Added $validExtraFields to if condition
            if ($ip == $_SERVER['REMOTE_ADDR'] && (time() - $now) > 4 && $name && mathCaptcha($_POST['calc'], $_POST['sum']) && $validExtraFields) {
                $header = "MIME-Version: 1.0\n";
                $header .= "Content-type: text/plain; charset=".s('charset')."\n";
                $header .= "From: $name <$mail>\r\nReply-To: $name <$mail>\r\nReturn-Path: <$mail>\r\n";
                $addUrl = isset($url) ? l('url').': '.$url."\n\n" : '';
                $body = "Meddelandet skickat av: ".$name." <$mail>\n".$addUrl.$strExtraFields.l('message').":\n".$message;
// don't forget the changed var name $to -> $contactemail
//was: mail($to, $subject, $body, $header);
                mail($contactemail, $subject, $body, $header);
# notify of success
                echo notification(0,l('applform_sent'),'home');
                $_SESSION[db('website').'applform'] = 1;
            }
            else {echo notification(1,l('applform_not_sent'),'applform');}
}}}
3.  Add lang variables (to en.php in lang folder):
Quote
$l['applform'] = 'Application';
$l['applform_sent'] = 'Application form sent';
$l['applform_not_sent'] = 'Application form not sent';

And this if you wanna use them upper custom fields or add new one's, specify them here...
    $l['label_name'] = 'Reason for contact';
    $l['label_pers'] = 'Social security number';
    $l['label_ftg'] = 'Company';
    $l['label_adress'] = 'Streetadress';
    $l['label_ponr'] = 'Zip code';
    $l['label_poort'] = 'City';
    $l['label_phone'] = 'Telephone';
    $l['1name'] = 'First and sur name';
4. Find this in lang variables:
Quote
$l['cat_listSEF'] =  'archive,contact,sitemap,login,administration,admin_category,admin_article,article_new,extra_new,page_new,snews_categories,
snews_articles,extra_contents,snews_pages,snews_settings,snews_files,logout,groupings,admin_groupings,events_list,event_list,
event_edit,event_new,event_update,event_delete,event_save'; //SEF links of the hardcoded categories
5. Add this right after the above:
Quote
$l['cat_listSEF'] .= ',applform'; - Yes, the dot before the = and the comma before applform is supposed to be there ;)
6. Find this in function center():
Quote
case isset($_POST['contactform']):
contact(); return; break;
7. Add this right after the above:
Quote
case isset($_POST['applform']):
applform(); return; break;
8. Find this in function center():
Quote
case 'contact':
contact(); break;
9. Add this right after the above:
Quote
case 'applform':
applform(); break;
10. And finally, in order to be able to have this as a page, add this in function pages() (If you, indeed, need it as a page):
Quote
$class = ($categorySEF == 'applform') ? ' class="current"': '';
echo '<li><a'.$class.' href="'.db('website').'applform/" title="'.l('applform').'">'.l('applform').'</a></li>';

Note:
If you do not want to use it as a page it's possible to use as a link http://yoursite.com/applform

Adding your own fields and stuff:
After applying changes as described above just add extra fields needed to the array $arrFields at the very beginning of modified function contact(). Examples are included in code comments.

And...:
And of course, it's ever so possible to use it as a function inside index.php aswell... Just make a div and place it inside it:
Quote
<div class="SOMEFORM">
<?php applform(); ?>
</div>
Thanks a bunch to Codetwist for his original MOD. All credit to him.
« Last Edit: September 12, 2009, 06:02:38 PM by Patric Ahlqvist »
Logged
"It's only dead fish that goes with the flow... "
Updated

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #1 on: September 12, 2009, 09:19:40 PM »

+1 karma to Patric for working through a successful PHP mod! :)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #2 on: September 13, 2009, 10:47:27 AM »

HA! Thanks, but it's Codies MOD... to begin with and possible others that have done things to it along the way... I mearly moved it here and posted my upgraded version of the function... ;)

I have yet to learn the simpliest PHP..
« Last Edit: September 13, 2009, 10:49:13 AM by Patric Ahlqvist »
Logged
"It's only dead fish that goes with the flow... "
Updated

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #3 on: September 13, 2009, 01:46:50 PM »

One thingy... I'm trying to use this with TOGGLE... But I can't get it to work.. any insight's ?

Ideally I would like to be able to post this function in any article and have it "toggled" (I can't use Fred's thingy it seems for some odd reason, and the toggle function... well, I don't really get it for some reason... )

the toggle bit I tried is:
Code: [Select]
<div class="applbox">
<h2><a onclick="toggle id="appsub" style="cursor: pointer;">Medlemsans&#246;kan</a></h2>

<div id="appsub" style="display:none;">
<p>CODE</p>
</div>
</div>
« Last Edit: September 13, 2009, 01:54:32 PM by Patric Ahlqvist »
Logged
"It's only dead fish that goes with the flow... "
Updated

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #4 on: September 13, 2009, 07:31:42 PM »

By default in sNews 1.7, the toggle function only loads (is only active) when you are logged in because it is within the admin.js file in the js folder in sNews 1.7.

If we want the toggle function available site-wide, for use everywhere, logged in or not, then we have to take it out of the admin.js file and move it into the index.php file just above the closing </head> tag.

1.  In js folder's admin.js file, search for // toggle dynamic divs and delete the complete function:
    (delete all of this - ignore the php tags... just used to show script in color))

Code: [Select]
<?php

 
// toggle dynamic divs
function toggle(div) {
var elem document.getElementById(div);
if (elem.style.display=='') {elem.style.display='none'; return;}
elem.style.display='';
}

?>


2.  Copy this revised script and paste it into your index.php fil just above the </head> tag:
     (excluding the php tags)

Code: [Select]
<?php

<script type="text/javascript">
function 
toggle(div) {
var elem document.getElementById(div);
if (elem.style.display=='') {elem.style.display='none'; return;}
elem.style.display='';
   }
</script>

?>


3.  Paste this variation of your test script into some function to get it working somewhere:
     (excluding the php tags)

Code: [Select]
<?php

//Patric's test script
echo '<div class="applbox"><h2><a onclick="toggle(\'appsub\')" style="cursor: pointer;">Medlemsans&#38;#246;kan</a></h2></div>';
echo 
'<div id="appsub" style="display:none;">';
echo 
'<p>CODE.</p>';
echo 
'</div>';

?>


4.  Here is a ready-to-go replacement function contact for snews.php (1.7) with the toggle built in, ready to go:

Code: [Select]
<?php

// CONTACT FORM
function contact() {
    echo 
'<div class="commentsbox">';
      echo 
'<h2><a title="Contact Us" onclick="toggle(\'formname-1\')" style="cursor: pointer;">Contact Form</a></h2>';
echo '<div id="formname-1" style="display: none;">';
 
if (!isset($_POST['contactform'])) {
echo '<p>'.l('required').'</p>';
echo html_input('form''''post''''''''''''''''''''post'_SITE'');
echo html_input('text''name''name''''* '.l('name'), 'text''''''''''''''''''');
echo html_input('text''email''email''''* '.l('email'), 'text''''''''''''''''''');
echo html_input('text''weblink''weblink'''l('url'), 'text''''''''''''''''''');
echo html_input('textarea''message''message''''* '.l('message'), '''''''''''5''5''''''');
echo mathCaptcha();
echo '<p>';
echo html_input('hidden''ip''ip'$_SERVER['REMOTE_ADDR'], '''''''''''''''''''''');
echo html_input('hidden''time''time'time(), '''''''''''''''''''''');
echo html_input('submit''contactform''contactform'l('submit'), '''button''''''''''''''''''');
echo '</p></form>';
$_SESSION[_SITE.'contact'] = 0;
} else {

$to s('website_email');
$subject s('contact_subject');
$name trim($_POST['name']);
$name strlen($name) > clean(cleanXSS($name)) : null;
$mail trim($_POST['email']);
$mail = (strlen($mail) > && preg_match
'/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' $mail)) ? clean(cleanXSS($mail)) : null;
$url trim($_POST['weblink']);
$url = (strlen($url) > && strpos($url'?') === false) ? clean(cleanXSS($url)) : null;
$message trim($_POST['message']);
$message strlen($message) > stripslashes(cleanXSS($message)) : null;
$message strip_tags($message);
$now is_numeric($_POST['time']) ? $_POST['time'] : null;
$ip = (strlen($_POST['ip']) < 16) ? clean(cleanXSS($_POST['ip'])) : null;
if($_SESSION[_SITE.'contact'] == 0){
  if ($ip == $_SERVER['REMOTE_ADDR'] && (time() - $now) > && $name && $mail && $message && checkMathCaptcha()) {
echo notification(0,l('contact_sent'),'home');
$_SESSION[_SITE.'contact'] = 1;
$send_array = array(
'to'=>$to,
'name'=>$name,
'email'=>$mail,
'message'=>$message,
'ip'=>$ip,
'url'=>$url,
'subject'=>$subject);
send_email($send_array);
  } else {
echo notification(1,l('contact_not_sent'),'contact');
}
}
}
echo '</div>'// closes hidden div id "formname-1"
    
echo '</div>'// closes div class "commentsbox"
}

?>


5.  And, a bit of styling to go into style.css for the commentsbox division (class) for demonstration purposes:
                            .commentsbox { border: 1px solid #ff0000; }
« Last Edit: September 13, 2009, 07:35:46 PM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #5 on: September 14, 2009, 09:54:47 AM »

DAMN, Dougie... I will try this out tonight :)
Logged
"It's only dead fish that goes with the flow... "
Updated

Rui Mendes

  • Development,Testing, Support
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1009
  • sNews1.7
    • Comunidade Portuguesa
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #6 on: September 14, 2009, 02:28:00 PM »

Hello Patric, sorry I arrived at home too late.

Doug has right about the place "toggle" in <head>

There is another way:  Put script on top function you'll use or before your code, like this
Code: [Select]
<?php

echo '<script type="text/javascript">
//<![CDATA[
function toggle(div) {
var elem = document.getElementById(div);
if (elem.style.display=='') {elem.style.display='
none'; return;}
elem.style.display='';
   }
//]]>
</script>'
;


//Patric's test script
echo '<div class="applbox"><h2><a onclick="toggle(\'appsub\')" style="cursor: pointer;">Medlemsans&#38;#38;#246;kan</a></h2></div>';
echo 
'<div id="appsub" style="display:none;">';
echo 
'<p>CODE.</p>';
echo 
'</div>';

?>

Patric did you solve float fields issue?
Logged
Need a Job on Europe. Linkdin - Facebook / Group

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #7 on: September 15, 2009, 12:06:29 PM »

Hi, Rui... and thanks for this - No, I didn't solve the float issue... I could only get those text field floated, and positioned as I wanted, but not the message field..
Logged
"It's only dead fish that goes with the flow... "
Updated

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #8 on: September 15, 2009, 05:13:57 PM »

Now, that is working... But if I somehow would like to add this form to an article with that snippet above:

Quote
<div class="applbox">
<h2><a onclick="toggle id="appsub" style="cursor: pointer;">Medlemsansökan</a></h2>

<div id="appsub" style="display:none;">
<p>APPLICATION FORM FUNCTION</p>
</div>
</div>


Ok, got it... Dummy, numbnuts, hehe... i did what you did to the contact form, but to my additional form and then, voila, I can add it as a function to any article... Goodie, thanks a bunch homies ;)

One thing though... of which I'm uncertain.

I have copied the function applform()  (which is renamed: applformclick() ) and named it and every reference to it to applform_norm() . This making me have two forms. One for implementation into articles (clickable): applformclick() AND one for linkto (not clickable) yaddiyadddayooo: applform_norm().

Now is there any danger for cockups here doing this ?
« Last Edit: September 15, 2009, 06:30:40 PM by Patric Ahlqvist »
Logged
"It's only dead fish that goes with the flow... "
Updated

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #9 on: March 17, 2012, 05:36:18 PM »

Hi
Is this correct for snews1.7
$_SESSION[db('website').'applform'] = 0; ?
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #10 on: March 18, 2012, 12:15:35 PM »

I think if you use this mod must change all the

$_SESSION[db('website').'applform']

to

$_SESSION[_SITE.'applform']
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [MOD] Codetwists - Additional form - 1.7
« Reply #11 on: March 18, 2012, 07:37:15 PM »

That is correct.
db('website') was used in snews 1.6 (and earlier) and was replaced in sNews 1.7 by _SITE which is defined under // CONSTANTS in snews.php.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU