2008-09-29 05:18:23 +02:00
< ? php
/**
2009-04-14 04:06:18 +02:00
* User Defined Form Page type that lets users build a form in the CMS
* using the FieldEditor Field .
*
* @ package userforms
2008-09-29 05:18:23 +02:00
*/
2009-04-14 04:06:18 +02:00
2008-09-29 05:18:23 +02:00
class UserDefinedForm extends Page {
2008-09-29 07:33:43 +02:00
2009-04-14 04:06:18 +02:00
/**
2009-04-15 01:50:48 +02:00
* @ var String Icon for the User Defined Form in the CMS . Without the extension
2009-04-14 04:06:18 +02:00
*/
2008-09-29 05:18:23 +02:00
static $icon = " cms/images/treeicons/task " ;
2009-04-14 04:06:18 +02:00
/**
2009-04-15 01:50:48 +02:00
* @ var String What level permission is needed to edit / add
2009-04-14 04:06:18 +02:00
*/
2008-09-29 05:18:23 +02:00
static $need_permission = 'ADMIN' ;
2009-10-15 02:33:13 +02:00
/**
* @ var String Required Identifier
*/
static $required_identifier = null ;
2009-04-14 04:06:18 +02:00
/**
2009-04-15 01:50:48 +02:00
* @ var Array Fields on the user defined form page .
2009-04-14 04:06:18 +02:00
*/
2008-09-29 05:18:23 +02:00
static $db = array (
" SubmitButtonText " => " Varchar " ,
2009-02-11 03:00:20 +01:00
" OnCompleteMessage " => " HTMLText " ,
2009-07-22 06:02:21 +02:00
" ShowClearButton " => " Boolean " ,
'DisableSaveSubmissions' => 'Boolean'
2008-09-29 05:18:23 +02:00
);
2009-04-14 04:06:18 +02:00
/**
2009-04-15 01:50:48 +02:00
* @ var Array Default values of variables when this page is created
2009-04-14 04:06:18 +02:00
*/
2008-09-29 05:18:23 +02:00
static $defaults = array (
2009-04-14 04:06:18 +02:00
'Content' => '$UserDefinedForm' ,
2009-07-22 06:02:21 +02:00
'DisableSaveSubmissions' => 0 ,
2009-04-14 04:06:18 +02:00
'OnCompleteMessage' => '<p>Thanks, we\'ve received your submission.</p>'
2008-09-29 05:18:23 +02:00
);
2009-09-23 00:36:53 +02:00
static $extensions = array (
" Versioned('Stage', 'Live') "
);
2009-04-14 04:06:18 +02:00
/**
* @ var Array
*/
2008-09-29 05:18:23 +02:00
static $has_many = array (
" Fields " => " EditableFormField " ,
2009-04-14 04:06:18 +02:00
" Submissions " => " SubmittedForm " ,
" EmailRecipients " => " UserDefinedForm_EmailRecipient "
2008-09-29 05:18:23 +02:00
);
2009-04-14 04:06:18 +02:00
/**
* Setup the CMS Fields for the User Defined Form
*
* @ return FieldSet
*/
2009-04-15 01:50:48 +02:00
public function getCMSFields () {
2009-04-14 04:06:18 +02:00
$fields = parent :: getCMSFields ();
2008-09-29 05:18:23 +02:00
2009-09-23 05:00:08 +02:00
// define tabs
$fields -> findOrMakeTab ( 'Root.Content.Form' , _t ( 'UserDefinedForm.FORM' , 'Form' ));
$fields -> findOrMakeTab ( 'Root.Content.Submissions' , _t ( 'UserDefinedForm.SUBMISSIONS' , 'Submissions' ));
$fields -> findOrMakeTab ( 'Root.Content.EmailRecipients' , _t ( 'UserDefinedForm.EMAILRECIPIENTS' , 'Email Recipients' ));
$fields -> findOrMakeTab ( 'Root.Content.OnComplete' , _t ( 'UserDefinedForm.ONCOMPLETE' , 'On Complete' ));
2009-04-14 04:06:18 +02:00
// field editor
2009-09-23 05:00:08 +02:00
$fields -> addFieldToTab ( " Root.Content.Form " , new FieldEditor ( " Fields " , 'Fields' , " " , $this ));
2009-04-14 04:06:18 +02:00
// view the submissions
2009-09-23 05:00:08 +02:00
$fields -> addFieldToTab ( " Root.Content.Submissions " , new CheckboxField ( 'DisableSaveSubmissions' , _t ( 'UserDefinedForm.SAVESUBMISSIONS' , " Disable Saving Submissions to Server " )));
$fields -> addFieldToTab ( " Root.Content.Submissions " , new SubmittedFormReportField ( " Reports " , _t ( 'UserDefinedForm.RECEIVED' , 'Received Submissions' ), " " , $this ) );
2009-04-15 01:21:52 +02:00
2009-04-14 04:06:18 +02:00
// who do we email on submission
2009-06-12 04:17:35 +02:00
$emailRecipients = new ComplexTableField (
$this ,
2009-04-14 04:06:18 +02:00
'EmailRecipients' ,
'UserDefinedForm_EmailRecipient' ,
array (
2009-09-23 05:00:08 +02:00
'EmailAddress' => _t ( 'UserDefinedForm.EMAILADDRESS' , 'Email' ),
'EmailSubject' => _t ( 'UserDefinedForm.EMAILSUBJECT' , 'Subject' ),
'EmailFrom' => _t ( 'UserDefinedForm.EMAILFROM' , 'From' )
2009-04-14 04:06:18 +02:00
),
2009-04-15 01:21:52 +02:00
'getCMSFields_forPopup' ,
" FormID = ' $this->ID ' "
2009-06-12 04:17:35 +02:00
);
2009-04-14 04:06:18 +02:00
$emailRecipients -> setAddTitle ( _t ( 'UserDefinedForm.AEMAILRECIPIENT' , 'A Email Recipient' ));
2009-06-12 04:17:35 +02:00
2009-09-23 05:00:08 +02:00
$fields -> addFieldToTab ( " Root.Content.EmailRecipients " , $emailRecipients );
2009-04-14 04:06:18 +02:00
// text to show on complete
2009-02-11 03:00:20 +01:00
$onCompleteFieldSet = new FieldSet (
2009-04-28 01:40:33 +02:00
new HtmlEditorField ( " OnCompleteMessage " , _t ( 'UserDefinedForm.ONCOMPLETELABEL' , 'Show on completion' ), 3 , " " , _t ( 'UserDefinedForm.ONCOMPLETEMESSAGE' , $this -> OnCompleteMessage ), $this )
2009-02-11 03:00:20 +01:00
);
2009-09-23 05:00:08 +02:00
$fields -> addFieldsToTab ( " Root.Content.OnComplete " , $onCompleteFieldSet );
2008-09-29 05:18:23 +02:00
return $fields ;
}
2009-09-23 00:36:53 +02:00
2009-09-23 05:00:08 +02:00
2009-09-23 00:36:53 +02:00
/**
* Publishing Versioning support .
*
* When publishing copy the editable form fields to the live database
* Not going to version emails and submissions as they are likely to
* persist over multiple versions
*
* @ return void
*/
public function doPublish () {
2009-09-23 02:02:50 +02:00
// remove fields on the live table which could have been orphaned.
2009-10-26 23:04:11 +01:00
if ( defined ( 'DB::USE_ANSI_SQL' )) {
2009-09-30 01:04:52 +02:00
$live = Versioned :: get_by_stage ( " EditableFormField " , " Live " , " \" EditableFormField \" . \" ParentID \" = $this->ID " );
} else {
$live = Versioned :: get_by_stage ( " EditableFormField " , " Live " , " `EditableFormField`.ParentID = $this->ID " );
}
2009-09-23 02:02:50 +02:00
if ( $live ) {
foreach ( $live as $field ) {
$field -> deleteFromStage ( 'Live' );
}
}
// publish the draft pages
2009-09-23 00:36:53 +02:00
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
$field -> publish ( 'Stage' , 'Live' );
}
}
2009-04-15 01:50:48 +02:00
2009-09-23 00:36:53 +02:00
parent :: doPublish ();
}
2009-04-15 00:59:46 +02:00
/**
2009-09-23 00:36:53 +02:00
* Unpublishing Versioning support
*
* When unpublishing the page it has to remove all the fields from
* the live database table
*
* @ return void
2009-04-15 00:59:46 +02:00
*/
2009-09-23 00:36:53 +02:00
public function doUnpublish () {
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
$field -> deleteFromStage ( 'Live' );
}
2009-04-15 00:59:46 +02:00
}
2009-09-23 00:36:53 +02:00
parent :: doUnpublish ();
2009-04-15 00:59:46 +02:00
}
2009-09-23 00:36:53 +02:00
2009-04-15 00:59:46 +02:00
/**
2009-09-23 00:36:53 +02:00
* Roll back a form to a previous version
2009-04-15 00:59:46 +02:00
*
2009-09-23 00:36:53 +02:00
* @ param String | int Version to roll back to
2009-04-15 00:59:46 +02:00
*/
2009-09-23 00:36:53 +02:00
public function doRollbackTo ( $version ) {
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
$field -> publish ( $version , " Stage " , true );
$field -> writeWithoutVersion ();
}
}
parent :: doRollbackTo ( $version );
}
/**
* Revert the draft site to the current live site
*
* @ return void
*/
public function doRevertToLive () {
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
$field -> writeToStage ( 'Live' , 'Stage' );
}
}
parent :: doRevertToLive ();
2008-09-29 05:18:23 +02:00
}
2008-09-29 07:33:43 +02:00
2008-09-29 05:18:23 +02:00
/**
* Duplicate this UserDefinedForm page , and its form fields .
* Submissions , on the other hand , won ' t be duplicated .
2009-04-15 00:59:46 +02:00
*
* @ return Page
2008-09-29 05:18:23 +02:00
*/
public function duplicate () {
$page = parent :: duplicate ();
foreach ( $this -> Fields () as $field ) {
$newField = $field -> duplicate ();
$newField -> ParentID = $page -> ID ;
$newField -> write ();
}
return $page ;
}
2009-09-23 00:36:53 +02:00
/**
* Custom Form Actions for the form
*
* @ param bool Is the Form readonly
* @ return FieldSet
*/
public function customFormActions ( $isReadonly = false ) {
return new FieldSet (
new TextField ( " SubmitButtonText " , _t ( 'UserDefinedForm.TEXTONSUBMIT' , 'Text on submit button:' ), $this -> SubmitButtonText ),
new CheckboxField ( " ShowClearButton " , _t ( 'UserDefinedForm.SHOWCLEARFORM' , 'Show Clear Form Button' ), $this -> ShowClearButton )
);
}
2008-09-29 05:18:23 +02:00
}
/**
* Controller for the { @ link UserDefinedForm } page type .
2009-04-14 04:06:18 +02:00
*
* @ package userform
2008-09-29 05:18:23 +02:00
* @ subpackage pagetypes
*/
2009-04-14 04:06:18 +02:00
2008-09-29 05:18:23 +02:00
class UserDefinedForm_Controller extends Page_Controller {
2009-04-21 05:44:13 +02:00
/**
* Load all the custom jquery needed to run the custom
* validation
*/
public function init () {
// block prototype validation
Validator :: set_javascript_validation_handler ( 'none' );
// load the jquery
Requirements :: javascript ( THIRDPARTY_DIR . '/jquery/jquery.js' );
Requirements :: javascript ( THIRDPARTY_DIR . '/jquery/plugins/validate/jquery.validate.min.js' );
2008-09-29 05:18:23 +02:00
parent :: init ();
}
2009-04-15 01:50:48 +02:00
/**
* Using $UserDefinedForm in the Content area of the page shows
* where the form should be rendered into . If it does not exist
* then default back to $Form
*
* @ return Array
*/
public function index () {
2009-04-21 05:44:13 +02:00
if ( $this -> Content && $form = $this -> Form ()) {
2009-04-15 01:50:48 +02:00
$hasLocation = stristr ( $this -> Content , '$UserDefinedForm' );
if ( $hasLocation ) {
2009-04-21 05:44:13 +02:00
$content = str_ireplace ( '$UserDefinedForm' , $form -> forTemplate (), $this -> Content );
2009-04-15 01:50:48 +02:00
return array (
'Content' => $content ,
'Form' => " "
);
}
}
return array (
'Content' => $this -> Content ,
'Form' => $this -> Form
);
}
2009-04-27 08:00:05 +02:00
2009-04-14 04:06:18 +02:00
/**
* User Defined Form . Feature of the user defined form is if you want the
* form to appear in a custom location on the page you can use $UserDefinedForm
* in the content area to describe where you want the form
*
* @ return Form
*/
2009-04-21 05:44:13 +02:00
public function Form () {
2008-09-29 05:18:23 +02:00
$fields = new FieldSet ();
2009-04-21 05:44:13 +02:00
$fieldValidation = array ();
$fieldValidationRules = array ();
2009-04-24 00:52:08 +02:00
$CustomDisplayRules = " " ;
$defaults = " " ;
2009-04-21 05:44:13 +02:00
$this -> SubmitButtonText = ( $this -> SubmitButtonText ) ? $this -> SubmitButtonText : _t ( 'UserDefinedForm.SUBMITBUTTON' , 'Submit' );
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
2009-04-24 00:52:08 +02:00
2009-04-21 05:44:13 +02:00
$fieldToAdd = $field -> getFormField ();
2009-04-27 08:17:02 +02:00
if ( ! $fieldToAdd ) break ;
2009-04-21 05:44:13 +02:00
$fieldValidationOptions = array ();
// Set the Error Messages
$errorMessage = sprintf ( _t ( 'Form.FIELDISREQUIRED' ) . '.' , strip_tags ( " ' " . ( $field -> Title ? $field -> Title : $field -> Name ) . " ' " ));
$errorMessage = ( $field -> CustomErrorMessage ) ? $field -> CustomErrorMessage : $errorMessage ;
$fieldToAdd -> setCustomValidationMessage ( $errorMessage );
// Is this field required
if ( $field -> Required ) {
$fieldValidation [ $field -> Name ] = $errorMessage ;
$fieldValidationOptions [ 'required' ] = true ;
2009-07-16 06:31:46 +02:00
$fieldToAdd -> addExtraClass ( 'requiredField' );
2009-10-15 04:36:43 +02:00
if ( UserDefinedForm :: $required_identifier ) {
$title = $fieldToAdd -> Title () . " <span class='requiredIdentifier'> " . UserDefinedForm :: $required_identifier . " </span> " ;
$fieldToAdd -> setTitle ( $title );
2009-10-15 02:33:13 +02:00
}
2009-04-21 05:44:13 +02:00
}
// Add field to the form
$fields -> push ( $fieldToAdd );
// Ask our form field for some more information on hour it should be validated
$fieldValidationOptions = array_merge ( $fieldValidationOptions , $field -> getValidation ());
// Check if we have need to update the global validation
if ( $fieldValidationOptions ) {
$fieldValidationRules [ $field -> Name ] = $fieldValidationOptions ;
}
2009-05-08 06:27:49 +02:00
$fieldId = $field -> Name ;
if ( $field -> ClassName == 'EditableFormHeading' ) {
$fieldId = 'Form_Form_' . $field -> Name ;
}
2009-04-24 00:52:08 +02:00
// Is this Field Show by Default
2009-05-06 05:34:40 +02:00
if ( ! $field -> ShowOnLoad ()) {
2009-05-08 06:27:49 +02:00
$defaults .= " $ ( \" # " . $fieldId . " \" ).hide(); \n " ;
2009-04-24 00:52:08 +02:00
}
// Check for field dependencies / default
if ( $field -> Dependencies ()) {
foreach ( $field -> Dependencies () as $dependency ) {
if ( is_array ( $dependency ) && isset ( $dependency [ 'ConditionField' ]) && $dependency [ 'ConditionField' ] != " " ) {
// get the field which is effected
$formName = Convert :: raw2sql ( $dependency [ 'ConditionField' ]);
$formFieldWatch = DataObject :: get_one ( " EditableFormField " , " Name = ' $formName ' " );
2009-05-01 06:11:31 +02:00
2009-04-24 00:52:08 +02:00
if ( ! $formFieldWatch ) break ;
2009-05-01 06:11:31 +02:00
// watch out for multiselect options - radios and check boxes
2009-05-07 05:55:04 +02:00
if ( is_a ( $formFieldWatch , 'EditableDropdown' )) {
2009-05-15 06:01:19 +02:00
$fieldToWatch = " $ ( \" select[name=' " . $dependency [ 'ConditionField' ] . " '] \" ) " ;
2009-05-01 06:11:31 +02:00
}
// watch out for checkboxs as the inputs don't have values but are 'checked
else if ( is_a ( $formFieldWatch , 'EditableCheckboxGroupField' )) {
$fieldToWatch = " $ ( \" input[name=' " . $dependency [ 'ConditionField' ] . " [ " . $dependency [ 'Value' ] . " ]'] \" ) " ;
}
else {
$fieldToWatch = " $ ( \" input[name=' " . $dependency [ 'ConditionField' ] . " '] \" ) " ;
}
2009-04-24 00:52:08 +02:00
// show or hide?
2009-05-06 05:34:40 +02:00
$view = ( isset ( $dependency [ 'Display' ]) && $dependency [ 'Display' ] == " Hide " ) ? " hide " : " show " ;
2009-04-24 00:52:08 +02:00
$opposite = ( $view == " show " ) ? " hide " : " show " ;
2009-05-01 06:11:31 +02:00
2009-05-08 06:27:49 +02:00
// what action do we need to keep track of. Something nicer here maybe?
// @todo encapulsation
$action = " change " ;
if ( $formFieldWatch -> ClassName == " EditableTextField " || $formFieldWatch -> ClassName == " EditableDateField " ) {
$action = " keyup " ;
}
2009-04-24 00:52:08 +02:00
2009-05-06 05:34:40 +02:00
// is this field a special option field
$checkboxField = false ;
if ( in_array ( $formFieldWatch -> ClassName , array ( 'EditableCheckboxGroupField' , 'EditableCheckbox' ))) {
2009-09-16 23:36:02 +02:00
$action = " click " ;
2009-05-06 05:34:40 +02:00
$checkboxField = true ;
}
2009-05-01 06:11:31 +02:00
// and what should we evaluate
2009-04-24 00:52:08 +02:00
switch ( $dependency [ 'ConditionOption' ]) {
case 'IsNotBlank' :
2009-05-06 05:34:40 +02:00
$expression = ( $checkboxField ) ? '$(this).attr("checked")' : '$(this).val() != ""' ;
2009-04-24 00:52:08 +02:00
break ;
case 'IsBlank' :
2009-05-06 05:34:40 +02:00
$expression = ( $checkboxField ) ? '!($(this).attr("checked"))' : '$(this).val() == ""' ;
2009-04-24 00:52:08 +02:00
break ;
case 'HasValue' :
2009-05-06 05:34:40 +02:00
$expression = ( $checkboxField ) ? '$(this).attr("checked")' : '$(this).val() == "' . $dependency [ 'Value' ] . '"' ;
2009-04-24 00:52:08 +02:00
break ;
default :
2009-05-06 05:34:40 +02:00
$expression = ( $checkboxField ) ? '!($(this).attr("checked"))' : '$(this).val() != "' . $dependency [ 'Value' ] . '"' ;
2009-04-24 00:52:08 +02:00
break ;
}
// put it all together
2009-05-08 06:27:49 +02:00
$CustomDisplayRules .= $fieldToWatch . " . $action (function() {
2009-05-01 06:11:31 +02:00
if ( " . $expression . " ) {
2009-05-08 06:27:49 +02:00
$ ( \ " # " . $fieldId . " \" ). " . $view . " ();
2009-04-24 00:52:08 +02:00
}
else {
2009-05-08 06:27:49 +02:00
$ ( \ " # " . $fieldId . " \" ). " . $opposite . " ();
2009-04-24 00:52:08 +02:00
}
}); " ;
}
}
}
2009-04-21 05:44:13 +02:00
}
2008-09-29 05:18:23 +02:00
}
$referer = isset ( $_SERVER [ 'HTTP_REFERER' ]) ? $_SERVER [ 'HTTP_REFERER' ] : '' ;
2009-04-21 05:44:13 +02:00
// Keep track of the referer
2008-09-29 05:18:23 +02:00
$fields -> push ( new HiddenField ( " Referrer " , " " , $referer ) );
// Build actions
2009-04-15 00:59:46 +02:00
$actions = new FieldSet (
new FormAction ( " process " , $this -> SubmitButtonText )
2008-09-29 05:18:23 +02:00
);
2009-04-20 05:07:57 +02:00
// Do we want to add a clear form.
2009-04-15 00:59:46 +02:00
if ( $this -> ShowClearButton ) {
$actions -> push ( new ResetFormAction ( " clearForm " ));
}
2009-10-27 10:23:42 +01:00
2009-04-21 05:44:13 +02:00
// return the form
2009-10-27 10:23:42 +01:00
$form = new Form ( $this , " Form " , $fields , $actions , new RequiredFields ( array_keys ( $fieldValidation )));
2008-09-29 05:18:23 +02:00
$form -> loadDataFrom ( $this -> failover );
2009-04-21 05:44:13 +02:00
$FormName = $form -> FormName ();
2009-04-24 00:52:08 +02:00
// Set the Form Name
2009-04-21 05:44:13 +02:00
$rules = $this -> array2json ( $fieldValidationRules );
$messages = $this -> array2json ( $fieldValidation );
2009-04-24 00:52:08 +02:00
2009-04-21 05:44:13 +02:00
// set the custom script for this form
Requirements :: customScript ( <<< JS
( function ( $ ) {
$ ( document ) . ready ( function () {
2009-04-24 00:52:08 +02:00
$defaults
2009-04-21 05:44:13 +02:00
$ ( " # $FormName " ) . validate ({
errorClass : " required " ,
messages :
$messages
,
rules :
$rules
});
2009-04-24 00:52:08 +02:00
$CustomDisplayRules
2009-04-21 05:44:13 +02:00
});
})( jQuery );
JS
);
2009-03-25 03:06:28 +01:00
2008-09-29 05:18:23 +02:00
return $form ;
}
2009-04-21 05:44:13 +02:00
/**
* Convert a PHP array to a JSON string . We cannot use { @ link Convert :: array2json }
* as it escapes our values with " " which appears to break the validate plugin
*
* @ param Array array to convert
* @ return JSON
*/
protected function array2json ( $array ) {
foreach ( $array as $key => $value )
if ( is_array ( $value )) {
$result [] = " $key : " . $this -> array2json ( $value );
} else {
$value = ( is_bool ( $value )) ? $value : " \" $value\ " " ;
$result [] = " $key : $value \n " ;
}
2009-04-24 00:52:08 +02:00
return ( isset ( $result )) ? " { \n " . implode ( ', ' , $result ) . " } \n " : '{}' ;
2009-04-21 05:44:13 +02:00
}
2009-04-14 04:06:18 +02:00
/**
* Process the form that is submitted through the site
*
* @ param Array Data
* @ param Form Form
* @ return Redirection
*/
function process ( $data , $form ) {
// submitted form object
2008-09-29 05:18:23 +02:00
$submittedForm = new SubmittedForm ();
2009-10-27 10:23:42 +01:00
$submittedForm -> SubmittedByID = ( $id = Member :: currentUserID ()) ? $id : 0 ;
2008-09-29 05:18:23 +02:00
$submittedForm -> ParentID = $this -> ID ;
$submittedForm -> Recipient = $this -> EmailTo ;
2009-07-22 06:02:21 +02:00
if ( ! $this -> DisableSaveSubmissions ) $submittedForm -> write ();
2008-09-29 05:18:23 +02:00
2009-04-14 04:06:18 +02:00
// email values
2008-09-29 05:18:23 +02:00
$values = array ();
$recipientAddresses = array ();
$sendCopy = false ;
$attachments = array ();
2009-02-27 02:21:10 +01:00
2009-04-20 01:52:44 +02:00
$submittedFields = new DataObjectSet ();
2009-04-14 04:06:18 +02:00
foreach ( $this -> Fields () as $field ) {
2009-05-12 08:10:13 +02:00
// don't show fields that shouldn't be shown
if ( ! $field -> showInReports ()) continue ;
2008-09-29 05:18:23 +02:00
$submittedField = new SubmittedFormField ();
$submittedField -> ParentID = $submittedForm -> ID ;
$submittedField -> Name = $field -> Name ;
$submittedField -> Title = $field -> Title ;
2009-04-27 08:17:02 +02:00
if ( $field -> hasMethod ( 'getValueFromData' )) {
$submittedField -> Value = $field -> getValueFromData ( $data );
2009-04-14 04:06:18 +02:00
}
else {
2008-09-29 05:18:23 +02:00
if ( isset ( $data [ $field -> Name ])) $submittedField -> Value = $data [ $field -> Name ];
2009-04-14 04:06:18 +02:00
}
2009-04-20 01:22:22 +02:00
if ( ! empty ( $data [ $field -> Name ])){
2009-04-28 04:03:13 +02:00
/**
* @ todo this should be on the EditableFile class . Just need to sort out
* attachments array
*/
if ( $field -> ClassName == " EditableFileField " ){
if ( isset ( $_FILES [ $field -> Name ])) {
// create the file from post data
$upload = new Upload ();
$file = new File ();
$upload -> loadIntoFile ( $_FILES [ $field -> Name ], $file );
2009-04-20 01:22:22 +02:00
2009-04-28 04:03:13 +02:00
// write file to form field
$submittedField -> UploadedFileID = $file -> ID ;
// Attach the file if its less than 1MB, provide a link if its over.
if ( $file -> getAbsoluteSize () < 1024 * 1024 * 1 ){
$attachments [] = $file ;
}
2009-04-20 01:22:22 +02:00
2009-04-28 04:03:13 +02:00
// Always provide the link if present.
if ( $file -> ID ) {
$submittedField -> Value = " <a href= \" " . $file -> getFilename () . " \" title= \" " . $file -> getFilename () . " \" > " . $file -> Title . " </a> " ;
} else {
$submittedField -> Value = " " ;
}
}
2008-09-29 05:18:23 +02:00
}
}
2009-07-22 06:02:21 +02:00
if ( ! $this -> DisableSaveSubmissions ) $submittedField -> write ();
2009-04-28 04:03:13 +02:00
2009-07-22 06:02:21 +02:00
$submittedFields -> push ( $submittedField );
2008-09-29 05:18:23 +02:00
}
2009-02-11 03:27:55 +01:00
$emailData = array (
" Sender " => Member :: currentUser (),
2009-07-27 01:23:45 +02:00
" Fields " => $submittedFields
2009-02-11 03:27:55 +01:00
);
2009-04-20 01:22:22 +02:00
2009-04-14 05:26:14 +02:00
// email users on submit. All have their own custom options.
2009-04-14 04:06:18 +02:00
if ( $this -> EmailRecipients ()) {
$email = new UserDefinedForm_SubmittedFormEmail ( $submittedFields );
2008-09-29 05:18:23 +02:00
$email -> populateTemplate ( $emailData );
if ( $attachments ){
foreach ( $attachments as $file ){
2009-04-20 01:22:22 +02:00
// bug with double decorated fields, valid ones should have an ID.
if ( $file -> ID != 0 ) {
$email -> attachFile ( $file -> Filename , $file -> Filename , $file -> getFileType ());
}
2008-09-29 05:18:23 +02:00
}
}
2009-04-20 01:22:22 +02:00
2009-04-14 04:06:18 +02:00
foreach ( $this -> EmailRecipients () as $recipient ) {
2009-07-27 01:23:45 +02:00
$email -> populateTemplate ( $recipient );
2009-04-14 04:06:18 +02:00
$email -> populateTemplate ( $emailData );
$email -> setFrom ( $recipient -> EmailFrom );
$email -> setBody ( $recipient -> EmailBody );
$email -> setSubject ( $recipient -> EmailSubject );
$email -> setTo ( $recipient -> EmailAddress );
2009-07-05 09:14:03 +02:00
2009-10-27 10:23:42 +01:00
// check to see if they are a dynamic sender. eg based on a email field a user selected
2009-07-05 09:14:03 +02:00
if ( $recipient -> SendEmailFromField ()) {
2009-10-27 10:23:42 +01:00
$submittedFormField = $submittedFields -> find ( 'Name' , $recipient -> SendEmailFromField () -> Name );
2009-07-05 09:14:03 +02:00
if ( $submittedFormField ) {
$email -> setFrom ( $submittedFormField -> Value );
}
}
2009-10-27 10:23:42 +01:00
// check to see if they are a dynamic reciever eg based on a dropdown field a user selected
2009-07-05 09:14:03 +02:00
if ( $recipient -> SendEmailToField ()) {
2009-10-27 10:23:42 +01:00
$submittedFormField = $submittedFields -> find ( 'Name' , $recipient -> SendEmailToField () -> Name );
2009-07-05 09:14:03 +02:00
2009-05-12 03:48:04 +02:00
if ( $submittedFormField ) {
$email -> setTo ( $submittedFormField -> Value );
2009-04-14 05:26:14 +02:00
}
}
2009-07-05 09:14:03 +02:00
2009-05-12 08:10:13 +02:00
if ( $recipient -> SendPlain ) {
$body = strip_tags ( $recipient -> EmailBody ) . " \n " ;
if ( isset ( $emailData [ 'Fields' ])) {
foreach ( $emailData [ 'Fields' ] as $Field ) {
2009-09-16 23:36:02 +02:00
$body .= $Field -> Title . ' - ' . $Field -> Value . ' \n' ;
2009-05-12 08:10:13 +02:00
}
}
$email -> setBody ( $body );
$email -> sendPlain ();
}
else {
$email -> send ();
}
2009-04-14 04:06:18 +02:00
}
2009-02-11 03:27:55 +01:00
}
2009-07-22 06:02:21 +02:00
return Director :: redirect ( $this -> Link () . 'finished?referrer=' . urlencode ( $data [ 'Referrer' ]));
2008-10-30 04:53:35 +01:00
}
/**
2008-10-30 04:59:18 +01:00
* This action handles rendering the " finished " message ,
* which is customisable by editing the ReceivedFormSubmission . ss
* template .
2008-10-30 04:53:35 +01:00
*
* @ return ViewableData
*/
function finished () {
$referrer = isset ( $_GET [ 'referrer' ]) ? urldecode ( $_GET [ 'referrer' ]) : null ;
2009-07-05 09:39:28 +02:00
return $this -> customise ( array (
2008-10-30 04:53:35 +01:00
'Content' => $this -> customise (
array (
'Link' => $referrer
)) -> renderWith ( 'ReceivedFormSubmission' ),
'Form' => ' ' ,
2008-09-29 05:18:23 +02:00
));
}
}
/**
2009-04-14 04:06:18 +02:00
* A Form can have multiply members / emails to email the submission
* to and custom subjects
*
* @ package userforms
*/
class UserDefinedForm_EmailRecipient extends DataObject {
static $db = array (
'EmailAddress' => 'Varchar(200)' ,
'EmailSubject' => 'Varchar(200)' ,
'EmailFrom' => 'Varchar(200)' ,
2009-06-16 05:13:49 +02:00
'EmailBody' => 'Text' ,
2009-07-27 01:23:45 +02:00
'SendPlain' => 'Boolean' ,
'HideFormData' => 'Boolean'
2009-04-14 04:06:18 +02:00
);
static $has_one = array (
2009-04-14 05:26:14 +02:00
'Form' => 'UserDefinedForm' ,
2009-07-05 09:14:03 +02:00
'SendEmailFromField' => 'EditableFormField' ,
'SendEmailToField' => 'EditableFormField'
2009-04-14 04:06:18 +02:00
);
2009-04-14 05:26:14 +02:00
/**
* Return the fields to edit this email .
* @ return FieldSet
*/
public function getCMSFields_forPopup () {
2009-07-17 05:40:18 +02:00
2009-04-14 05:26:14 +02:00
$fields = new FieldSet (
new TextField ( 'EmailSubject' , _t ( 'UserDefinedForm.EMAILSUBJECT' , 'Email Subject' )),
2009-07-17 05:40:18 +02:00
new TextField ( 'EmailFrom' , _t ( 'UserDefinedForm.FROMADDRESS' , 'Send Email From' )),
2009-04-15 01:21:52 +02:00
new TextField ( 'EmailAddress' , _t ( 'UserDefinedForm.SENDEMAILTO' , 'Send Email To' )),
2009-07-27 01:23:45 +02:00
new CheckboxField ( 'HideFormData' , _t ( 'UserDefinedForm.HIDEFORMDATA' , 'Hide Form Data from Email' )),
2009-07-17 05:40:18 +02:00
new CheckboxField ( 'SendPlain' , _t ( 'UserDefinedForm.SENDPLAIN' , 'Send Email as Plain Text (HTML will be stripped)' )),
new TextareaField ( 'EmailBody' , 'Body' )
2009-04-14 05:26:14 +02:00
);
if ( $this -> Form ()) {
2009-05-06 05:34:40 +02:00
$validEmailFields = DataObject :: get ( " EditableEmailField " , " ParentID = ' $this->FormID ' " );
2009-07-17 05:40:18 +02:00
$multiOptionFields = DataObject :: get ( " EditableMultipleOptionField " , " ParentID = ' $this->FormID ' " );
2009-04-14 05:26:14 +02:00
2009-07-17 05:40:18 +02:00
// if they have email fields then we could send from it
2009-04-14 05:26:14 +02:00
if ( $validEmailFields ) {
2009-07-19 22:44:32 +02:00
$fields -> insertAfter ( new DropdownField ( 'SendEmailFromFieldID' , _t ( 'UserDefinedForm.ORSELECTAFIELDTOUSEASFROM' , '.. or Select a Form Field to use as the From Address' ), $validEmailFields -> toDropdownMap ( 'ID' , 'Title' ), '' , null , " " ), 'EmailFrom' );
2009-07-05 09:14:03 +02:00
}
2009-07-17 05:40:18 +02:00
// if they have multiple options
2009-07-19 22:44:32 +02:00
if ( $multiOptionFields || $validEmailFields ) {
if ( $multiOptionFields && $validEmailFields ) {
$multiOptionFields -> merge ( $validEmailFields );
2009-08-07 04:13:53 +02:00
2009-07-19 22:44:32 +02:00
}
2009-08-07 04:13:53 +02:00
elseif ( ! $multiOptionFields ) {
$multiOptionFields = $validEmailFields ;
2009-07-19 22:44:32 +02:00
}
2009-08-07 04:13:53 +02:00
2009-07-17 05:40:18 +02:00
$multiOptionFields = $multiOptionFields -> toDropdownMap ( 'ID' , 'Title' );
$fields -> insertAfter ( new DropdownField ( 'SendEmailToFieldID' , _t ( 'UserDefinedForm.ORSELECTAFIELDTOUSEASTO' , '.. or Select a Field to use as the To Address' ), $multiOptionFields , '' , null , " " ), 'EmailAddress' );
2009-04-14 05:26:14 +02:00
}
}
2009-07-17 05:40:18 +02:00
2009-04-14 05:26:14 +02:00
return $fields ;
}
2009-04-14 04:06:18 +02:00
}
/**
* Email that gets sent to the people listed in the Email Recipients
* when a submission is made
*
* @ package userforms
2008-09-29 05:18:23 +02:00
*/
class UserDefinedForm_SubmittedFormEmail extends Email {
protected $ss_template = " SubmittedFormEmail " ;
protected $data ;
2009-04-14 04:06:18 +02:00
function __construct () {
2008-09-29 05:18:23 +02:00
parent :: __construct ();
}
}
?>