Integration with lifetype

Integration with lifetype

Postby ekwogefee on Sat Dec 30, 2006 5:19 pm

I am trying to create a contact form where users can post suggestions and I receive by mail.

I wish this contact form be on my summary page, as one of its menu items and it appears with the same header and footer.

and the url should be http://_myblogsite.com/contact.php
I have also created a contact.template file in the summary folder.

Now I am at crossroads as to the best approach to use.

I was hoping one could inform me if from my contact.php code i could make lifetype's smarty display my contact.template file and I attack all validation and form sending . The tutorial on integrating lifetype's smarty and php seems to be outdated.

OR

Create a controller , and a view where I will still display using contact.template , but in addition have email validation and other validations integrated with lifetype.

I prefer the second solution but I have difficulties implementing it. My main problem is creating a suitable action class for my contact.php. And a suitable view that will display my contacts.template file.
ekwogefee
 
Posts: 10
Joined: Fri Dec 08, 2006 2:25 am
Location: Yde

Postby phunkphorce on Sun Dec 31, 2006 7:28 am

Why don't you have a look at the other action classes in the class/summary/action/ folder? Some of them are pretty short and easy. And about the view, you probably don't need to bother as you can use SummaryView or SummaryCachedView with the name of your template as a parameter.
phunkphorce
Lifetype Expert
 
Posts: 9028
Joined: Mon Aug 25, 2003 6:34 am
Location: Suomessa

Postby ekwogefee on Sun Dec 31, 2006 1:46 pm

Can anyone then help me with the code of a simple action class that sends an email using ( I think) lifetype's integrated phpmail or email service action class?
The view is not a problem anymore.
Ekwoge Fritz Ekwoge
ekwogefee.akopo.com
ekwogefee
 
Posts: 10
Joined: Fri Dec 08, 2006 2:25 am
Location: Yde

Postby phunkphorce on Mon Jan 01, 2007 6:37 am

1) build your message with the EmailMessage class. It has methods for setting the subject (setSubject), the destination (setTo), the body (setBody) and so on. Full method list available here: http://lifetype.net/api/class_email_message.php

2) when ready, use the EmailService class to send the message. You need the EmailService::sendMessage() method. Full documentation available here: http://lifetype.net/api/class_email_message.php (scroll down for a full example also)
phunkphorce
Lifetype Expert
 
Posts: 9028
Joined: Mon Aug 25, 2003 6:34 am
Location: Suomessa

Data Validation

Postby ekwogefee on Tue Jan 02, 2007 12:21 pm

I have succeeded in creating my contact form , because i wanted it to work first , without data validation.
So I created a controller :
Code: Select all
<?php
//filename contact.php
 if (!defined( "PLOG_CLASS_PATH" )) {
        define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
    }

   include_once( PLOG_CLASS_PATH."class/controller/controller.class.php" );
   include_once( PLOG_CLASS_PATH."class/action/viewcontactformaction.class.php" );
   include_once( PLOG_CLASS_PATH."class/bootstrap.php" );
   include_once( PLOG_CLASS_PATH."class/misc/version.class.php" );          
   
   $actionMap["Default"] = "ViewContactFormAction";
   $actionMap["ViewContactForm"] = "ViewContactFormAction";
   $controller = new Controller( $actionMap, "op" );
 $controller->process( $_REQUEST );
 
 
 ?>


and an action class

Code: Select all

<?php
//filename viewcontactformaction.class.php
   include_once( PLOG_CLASS_PATH."class/summary/action/summaryaction.class.php" );
   include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
   include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
    include_once( PLOG_CLASS_PATH."class/mail/emailservice.class.php" );
   include_once( PLOG_CLASS_PATH."class/mail/emailmessage.class.php" );
   include_once( PLOG_CLASS_PATH."class/mail/phpmailer/class.phpmailer.php" );
   

class ViewContactFormAction extends Action
   {
   
   var $akopo_contact_message;
    var $userEmail;
    var $akopo_contact_subject;
   var $akopo_contact_message;
   
   var $_page;
   
function ViewContactFormAction( $actionInfo, $request )
    {
   $this->Action( $actionInfo, $request );
   $this->_page = "contacts";
}

function _sendEmail($userFullName,$userEmail,$akopo_contact_subject,$akopo_contact_message){
$message = new EmailMessage();
$message->addTo( "to@myhost.com.com" );
$message->setFrom( $userEmail);
$message->setFromName($userFullName);
$message->setBody($akopo_contact_message );
$message->setSubject($akopo_contact_subject);
$service = new EmailService();
if( $service->sendMessage( $message ))
{
;
//print( "message sent ok!" );
}
else
{
;
}
//print( "error sending message" );

}
   
function perform()
{
      $this->_page = "contacts";
      $this->_view = new SummaryView( $this->_page);
      $this->setCommonData();
$this->userFullName = Textfilter::filterAllHTML($this->_request->getValue( "userFullName" ));
$this->userEmail = Textfilter::filterAllHTML($this->_request->getValue( "userEmail" ));
$this->akopo_contact_subject = Textfilter::filterAllHTML($this->_request->getValue( "akopo_contact_subject" ));
$this->akopo_contact_message = Textfilter::filterAllHTML($this->_request->getValue( "akopo_contact_message" ));

$this->_sendEmail($this->userFullName,$this->userEmail,$this->akopo_contact_subject,$this->akopo_contact_message);

return( true );
      }
   }
?>





Now this works.

What is lacking now is data validation.

How do I get email validation and string validation. i have read the article on forms and data validation but it still does not seem to function.

Do i need to create an ErrorView ? How ?
How do I inform the user on the form that his email has been successfully sent ?
How do I stop the code from sending a void email ? I could do this in the _sendmail() but I believe data validation can help me here too ?
What classes do I need to include?

Though it would be preferable if someone just modified the code above to include data validation , any useful indications will be welcome.

An example on only email validation will do!!
Ekwoge Fritz Ekwoge
ekwogefee.akopo.com
ekwogefee
 
Posts: 10
Joined: Fri Dec 08, 2006 2:25 am
Location: Yde

Re: Data Validation

Postby phunkphorce on Tue Jan 02, 2007 5:39 pm

ekwogefee wrote:How do I get email validation and string validation. i have read the article on forms and data validation but it still does not seem to function.


Did you see this bit?

http://wiki.lifetype.net/index.php/Form ... ing_fields

This tells you that every field that you want validated, needs to be registered with a validator.

The Action::registerFieldValidator() method takes two parameters: the first one is the name of the field in the form and the second one is an instance of a Validator object. The Validator classes are described here and can be found in the class/data/validator/ folder, there are ready made ones for validating non-empty strings and email addresses.

If there is a field that doesn't need validation but needs to be passed back to the template in case of an error, register it with Action::registerField()

Do i need to create an ErrorView ? How ?


You need to create a basic view class that will extend SummaryView and that will get as a result the name of the template to be loaded. This template will include the form and all its fields. For each one of the fields, errors will be automatically displayed as long as you include this as part of the template:

Code: Select all
{include file="summary/validate.template" field=yourFieldName message="error message to be shown in case data is not valid"}


This needs to be done for each one of the fields from the form that you want to validate.

If a validation error occurs, the controller class will stop execution at that point and return to the view that was set as the "error" view via Action:: setValidationErrorView() This method takes as a parameter an instance of a View class, and don't forget to set the error message that should be shown by the view via View:: setErrorMessage()

How do I inform the user on the form that his email has been successfully sent ?


In the perform() method of your class, you can use the SummaryMessageView view class and pass the message you want to show as a parameter to the constructor. The class will take care of picking the right template, processing it and setting the message for you.

Code: Select all
How do I stop the code from sending a void email ? I could do this in the _sendmail() but I believe data validation can help me here too ?
What classes do I need to include?


Data validation will take care of this for you. Just make sure you validate your email body with a non-empty string validator class (StringValidator will help you)

[code]
Though it would be preferable if someone just modified the code above to include data validation , any useful indications will be welcome.[/quote]

Since you've come this far, you might as well finish the job yourself :), although I do understand that this may sound a bit difficult to follow... You can also have a look at some of the classes in the class/summary/action/ folder (speciall the ones related to the registration process) as most of them implement support for data validation. Then try to have a look at the registration templates under templates/summary/ on how to use data validation in your templates, hopefully that'll help.

And come back if you get stuck.
phunkphorce
Lifetype Expert
 
Posts: 9028
Joined: Mon Aug 25, 2003 6:34 am
Location: Suomessa

Solved

Postby ekwogefee on Thu Jan 04, 2007 5:05 pm

Thanks for all the tips.
I have successfully implemented my contacts page , and it is fully operational , with data validation.

I was doubting if I could post the code I wrote for the contact page for pLog novices like I , or for experts who may see something I am doing efficiently.

Is it against forum rules to do that ?

Once again thanks for your prompt responses.
Ekwoge Fritz Ekwoge
ekwogefee.akopo.com
ekwogefee
 
Posts: 10
Joined: Fri Dec 08, 2006 2:25 am
Location: Yde

Postby phunkphorce on Fri Jan 05, 2007 5:26 am

Please go ahead and post the code here, or link to a .zip file with all the needed files. I'm sure other people will appreciate it.
phunkphorce
Lifetype Expert
 
Posts: 9028
Joined: Mon Aug 25, 2003 6:34 am
Location: Suomessa

the contact form code

Postby ekwogefee on Sun Jan 14, 2007 3:24 am

The following bits of code are my implementation of a contact form for lifeType. The principle can easily help you add your own custom form in lifetype.
This code was tested for lifetype 1.1.3.
Written by a newbie( me :) ), for newbies to learn and experts to criticize, maybe for some security fixes.

After reading some documentation from Lifetype, this is what I did to get my contact form running.

All the contact form does is request the user's name, his email , the subject of his message and the content of the message. Then an email is sent to a dedicated email, with all these information. All this while integrating smoothly with lifetype.


So, like many newbies ( i guess ), I started with the front end . And this is done using templates or Smarty files in lifetype.
here is the code;

Code: Select all
//filename lifetypeDir/templates/summary/contacts.template
//By Ekwoge Fritz Ekwoge
//Date 04th January 2007

{include file="summary/header.template" selected="contacts" columns=1

section=$locale->tr("yoursite_contacts")}
  <script type="text/javascript" src="js/prototype/prototype.js"></script>
  <script type="text/javascript" src="js/ui/summary.js"></script>
 
  <form name="contactForm" action="contact.php" method="post">
    <fieldset class="inputField">
        <legend>{$locale->tr("yoursite_contact_us")}</legend>
        <p>
          {$locale->tr("yoursite_contact_us_help")}
        </p>
        {include file="summary/formvalidate.template" message=$locale->tr
("yoursite_contact_send_message_error")}
       
        <div class="field">
            <label for="yoursite_contact_userFullName">{$locale->tr

("yoursite_contact_userFullName")}</label>
         <span class="required">*</span>
            <div class="formHelp">{$locale->tr

("yoursite_contact_userFullName_help")}</div>
            <input type="text" name="yoursite_contact_userFullName"

value="{$yoursite_contact_userFullName}" id="yoursite_contact_userFullName" />
         {include file="summary/validate.template"

field=yoursite_contact_userFullName message=$locale->tr

("yoursite_contact_userFullName_error")}
        </div>
       
        <div class="field">
            <label for="yoursite_contact_userEmail">{$locale->tr

("yoursite_contact_userEmail")}</label>
         <span class="required">*</span>
         <div class="formHelp">{$locale->tr

("yoursite_contact_userEmail_help")}</div>
            <input type="text" name="yoursite_contact_userEmail"

id="yoursite_contact_userEmail" value="{$yoursite_contact_userEmail}" />
            {include file="summary/validate.template"

field=yoursite_contact_userEmail message=$locale->tr

("yoursite_contact_userEmail_error")}
        </div>
      
      
      
      <div class="field">
            <label for="yoursite_contact_subject">{$locale->tr

("yoursite_contact_subject")}</label>
         <span class="required">*</span>
            <div class="formHelp">{$locale->tr("yoursite_contact_subject_help")}

</div>
            <input type="text" name="yoursite_contact_subject"

value="{$yoursite_contact_subject}" id="yoursite_contact_subject" />
            {include file="summary/validate.template"

field=yoursite_contact_subject message=$locale->tr

("yoursite_contact_subject_error")}
                 </div>
       
      <div class="field">
            <label for="yoursite_contact_message">{$locale->tr

("yoursite_contact_message")}</label>
         <span class="required">*</span>
            <div class="formHelp">{$locale->tr("yoursite_contact_message_help")}

</div>
         
         <textarea name="yoursite_contact_message" class="contract"

style="width:99%" rows="10" value="{$yoursite_contact_message}" 

id="yoursite_contact_message">

    </textarea>
    {include file="summary/validate.template" field=yoursite_contact_message

message=$locale->tr("yoursite_contact_message_error")}
         
        </div>
       
      
    </fieldset>
    <div class="buttons">
      <input type="submit"  value="{$locale->tr("yoursite_contact_submit")}"

name="yoursite_contact_send"/>
    </div>
  </form>

{include file="summary/footer.template"}


Add this point , typing the URL http://yoursite.com/summary.php?op=disp ... e=contacts

will display a contact page, that follows lifetype's UI guideline.

We also need a template that informs the user that his message could not be sent .
This is done below :

Code: Select all

//filename lifetypeDir/templates/summary/contacterror.template
//By Ekwoge Fritz Ekwoge
//Date 04th January 2007

{include file="summary/header.template" selected="contacts" columns=1

section=$locale->tr("error")}
<div id="onecolumn">
 <p>
  {$locale->tr("yoursite_contact_sending_error")}
 </p>
 <p>
  <span style="color: red">{$locale->tr("yoursite_contact_sending_error_message")}

</span>
 </p>
 <p>
<a href="#" onclick="javascript:history.go(-1)">&laquo; {$locale->tr("back")}</a>
 </p>
</div>
{include file="summary/footer.template"}



The front end is up and looking good.But it doesn't do anything yet. Just a dummy form . Let's make it do something. Let's do the backend.

According to lifetype, we have to create a controller and some actions.

The controller for the contact form I implemented is shown below:
Code: Select all

<?php
//filename lifetypeDir/contact.php
//By Ekwoge Fritz Ekwoge, ekwogefee_@_gmail_dot_com
//Date 04th January 2007

 if (!defined( "PLOG_CLASS_PATH" )) {
        define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
    }
//normal includes needed for lifetype
   include_once( PLOG_CLASS_PATH."class/controller/controller.class.php" );
   include_once( PLOG_CLASS_PATH."class/bootstrap.php" );
   include_once( PLOG_CLASS_PATH."class/misc/version.class.php" );   
   include_once( PLOG_CLASS_PATH."class/net/http/session/sessionmanager.class.php"

);   

//Now I include the file where I implemented the ViewContactFormAction class.
include_once(PLOG_CLASS_PATH."class/action/viewcontactformaction.class.php" );



// initialiaze the session
   SessionManager::init();      
          

   $actionMap["Default"] = "ViewContactFormAction";

//This is not necessary. Just added it in case.
   $actionMap["ViewContactForm"] = "ViewContactFormAction";

   $controller = new Controller( $actionMap, "op" );
 $controller->process( $_REQUEST );
 
 
 ?>



and my action class is implemented below, with data validation which is basically non empty strings and an email of the right format:




Code: Select all

<?php

//filename lifetypeDir/class/action/viewcontactformaction.class.php
//By Ekwoge Fritz Ekwoge, ekwogefee_@_gmail_dot_com
//Date 04th January 2007

   include_once( PLOG_CLASS_PATH."class/summary/action/summaryaction.class.php"

);
   include_once(

PLOG_CLASS_PATH."class/summary/view/summarymessageview.class.php" );
   include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
   include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
    include_once( PLOG_CLASS_PATH."class/mail/emailservice.class.php" );
   include_once( PLOG_CLASS_PATH."class/mail/emailmessage.class.php" );
   include_once( PLOG_CLASS_PATH."class/mail/phpmailer/class.phpmailer.php" );
   include_once(

PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
    include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php"

);
    include_once( PLOG_CLASS_PATH."class/data/validator/emailvalidator.class.php" );
   
   
   

class ViewContactFormAction extends SummaryAction
   {
   
   var $yoursite_contact_userFullName;
    var $yoursite_contact_userEmail;
    var $yoursite_contact_subject;
   var $yoursite_contact_message;
   
   var $_page;
   
function ViewContactFormAction( $actionInfo, $request )
    {
   $this->SummaryAction( $actionInfo, $request );
   
   $this->registerFieldValidator( "yoursite_contact_userFullName", new

StringValidator());
   $this->registerFieldValidator( "yoursite_contact_userEmail", new

EmailValidator());
   $this->registerFieldValidator( "yoursite_contact_subject", new

StringValidator());
   $this->registerFieldValidator( "yoursite_contact_message", new

StringValidator());
   
   
   
   
   $this->_page = "contacts";
   
   
   
   
   $view = new SummaryView($this->_page);
   $view->setErrorMessage($this->_locale->tr

("yoursite_contact_send_message_error"));
   $this->setValidationErrorView( $view );   
   
}

function _sendEmail

($userFullName,$userEmail,$yoursite_contact_subject,$yoursite_contact_message){
$message = new EmailMessage();
$message->addTo( "info@yoursite.com" );
$message->setFrom( $userEmail);
$message->setFromName($userFullName);
$message->setBody($yoursite_contact_message );
$message->setSubject($yoursite_contact_subject);
$service = new EmailService();

if( $service->sendMessage( $message ))
{
//message sending went well. Congratulate the user.
   $this->_view = new SummaryMessageView ($this->_locale->tr

("yoursite_contact_success_sending_your_message"));
   $this->setCommonData();

;
//print( "message sent ok!" );
}
else
{
//There was an error sending the message.
$this->_view = new SummaryView("contacterror");
   $this->setCommonData();

;
}
//print( "error sending message" );

}
   
function perform()
{
      
$this->yoursite_contact_userFullName = Textfilter::filterAllHTML($this->_request-

>getValue( "yoursite_contact_userFullName" ));
$this->yoursite_contact_userEmail = Textfilter::filterAllHTML($this->_request-

>getValue( "yoursite_contact_userEmail" ));
$this->yoursite_contact_subject = Textfilter::filterAllHTML($this->_request-

>getValue( "yoursite_contact_subject" ));
$this->yoursite_contact_message = Textfilter::filterAllHTML($this->_request-

>getValue( "yoursite_contact_message" ));


$this->_sendEmail($this->yoursite_contact_userFullName,$this-

>yoursite_contact_userEmail,$this->yoursite_contact_subject,$this-

>yoursite_contact_message);

return( true );
      }
   }
?>




Hope this helps.
PS: Update your locales.
You can see it functioning it here:
http://www.akopo.com/summary.php?op=dis ... e=contacts
Ekwoge Fritz Ekwoge
ekwogefee.akopo.com
ekwogefee
 
Posts: 10
Joined: Fri Dec 08, 2006 2:25 am
Location: Yde

Postby phunkphorce on Sun Jan 14, 2007 7:38 am

Great tutorial, thank you! Hopefully other people will find it useful :)
phunkphorce
Lifetype Expert
 
Posts: 9028
Joined: Mon Aug 25, 2003 6:34 am
Location: Suomessa

Re: Integration with lifetype

Postby jondaley on Sat Apr 18, 2009 5:53 pm

Due to changing my own site from a separate installation to a shared installation, I've needed to redo my contact page (which used to simply use php), but I can't let random people run php code on my server, so I needed to come up with a plugin.
The "contact" plugin is now available via sourceforge for lifetype 1.2.
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch


Return to Other Problems

cron