2008-09-29 05:18:23 +02:00
< ? php
2010-07-07 11:17:37 +02:00
2008-09-29 05:18:23 +02:00
/**
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 .
*
2010-07-07 11:17:37 +02:00
* @ todo Allow UserDefinedForm instances on Page subclasses ( eg via decorator )
*
2009-04-14 04:06:18 +02:00
* @ 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
*/
2010-09-03 07:06:13 +02:00
static $need_permission = array ( 'ADMIN' );
2008-09-29 05:18:23 +02:00
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' ));
2010-09-03 07:06:13 +02:00
$fields -> findOrMakeTab ( 'Root.Content.Options' , _t ( 'UserDefinedForm.OPTIONS' , 'Options' ));
2009-09-23 05:00:08 +02:00
$fields -> findOrMakeTab ( 'Root.Content.EmailRecipients' , _t ( 'UserDefinedForm.EMAILRECIPIENTS' , 'Email Recipients' ));
$fields -> findOrMakeTab ( 'Root.Content.OnComplete' , _t ( 'UserDefinedForm.ONCOMPLETE' , 'On Complete' ));
2009-11-17 01:39:24 +01:00
$fields -> findOrMakeTab ( 'Root.Content.Submissions' , _t ( 'UserDefinedForm.SUBMISSIONS' , 'Submissions' ));
2010-09-03 07:06:13 +02:00
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' ,
2010-04-08 23:18:01 +02:00
" \" 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 );
2010-09-03 07:06:13 +02:00
$fields -> addFieldsToTab ( " Root.Content.Options " , $this -> getFormOptions ());
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-12-07 01:52:00 +01:00
$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 ) {
2009-12-12 09:59:20 +01:00
$field -> doDeleteFromStage ( 'Live' );
2009-09-23 02:02:50 +02:00
}
}
2010-05-16 05:21:04 +02:00
2009-09-23 02:02:50 +02:00
// publish the draft pages
2009-09-23 00:36:53 +02:00
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
2009-12-12 09:59:20 +01:00
$field -> doPublish ( 'Stage' , 'Live' );
2009-09-23 00:36:53 +02:00
}
}
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 ) {
2009-12-12 09:59:20 +01:00
$field -> doDeleteFromStage ( 'Live' );
2009-09-23 00:36:53 +02:00
}
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
/**
2010-09-03 07:06:13 +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 ) {
2010-09-03 07:06:13 +02:00
parent :: doRollbackTo ( $version );
/*
Not implemented yet
// get the older version
$reverted = Versioned :: get_version ( $this -> ClassName , $this -> ID , $version );
if ( $reverted ) {
// using the lastedited date of the reverted object we can work out which
// form fields to revert back to
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
// query to see when the version of the page was pumped
$editedDate = DB :: query ( "
SELECT LastEdited
FROM \ " SiteTree_versions \"
WHERE \ " RecordID \" = ' $this->ID ' AND \" Version \" = $version
" )->value();
// find a the latest version which has been edited
$versionToGet = DB :: query ( "
SELECT *
FROM \ " EditableFormField_versions \"
WHERE \ " RecordID \" = ' $field->ID ' AND \" LastEdited \" <= ' $editedDate '
ORDER BY Version DESC
LIMIT 1
" )->record();
if ( $versionToGet ) {
Debug :: show ( 'publishing field' . $field -> Name );
Debug :: show ( $versionToGet );
$field -> publish ( $versionToGet , " Stage " , true );
$field -> writeWithoutVersion ();
}
else {
Debug :: show ( 'deleting field' . $field -> Name );
$this -> Fields () -> remove ( $field );
$field -> delete ();
$field -> destroy ();
}
}
2009-09-23 00:36:53 +02:00
}
2010-09-03 07:06:13 +02:00
// @todo Emails
2009-09-23 00:36:53 +02:00
}
2010-09-03 07:06:13 +02:00
*/
2009-09-23 00:36:53 +02:00
}
/**
* Revert the draft site to the current live site
*
* @ return void
*/
public function doRevertToLive () {
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
2010-09-03 07:06:13 +02:00
$field -> publish ( " Live " , " Stage " , false );
$field -> writeWithoutVersion ();
2009-09-23 00:36:53 +02:00
}
}
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 ();
2010-05-31 11:01:58 +02:00
// the form fields
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
$newField = $field -> duplicate ();
$newField -> ParentID = $page -> ID ;
$newField -> write ();
}
2008-09-29 05:18:23 +02:00
}
2010-05-31 11:01:58 +02:00
// the emails
if ( $this -> EmailRecipients ()) {
foreach ( $this -> EmailRecipients () as $email ) {
$newEmail = $email -> duplicate ();
$newEmail -> FormID = $page -> ID ;
$newEmail -> write ();
}
}
2008-09-29 05:18:23 +02:00
return $page ;
}
2009-09-23 00:36:53 +02:00
/**
2010-09-03 07:06:13 +02:00
* Custom options for the form . You can extend the built in options by
* using { @ link updateFormOptions ()}
2009-09-23 00:36:53 +02:00
*
* @ return FieldSet
*/
2010-09-03 07:06:13 +02:00
public function getFormOptions () {
$submit = ( $this -> SubmitButtonText ) ? $this -> SubmitButtonText : _t ( 'UserDefinedForm.SUBMITBUTTON' , 'Submit' );
$options = new FieldSet (
new TextField ( " SubmitButtonText " , _t ( 'UserDefinedForm.TEXTONSUBMIT' , 'Text on submit button:' ), $submit ),
2009-09-23 00:36:53 +02:00
new CheckboxField ( " ShowClearButton " , _t ( 'UserDefinedForm.SHOWCLEARFORM' , 'Show Clear Form Button' ), $this -> ShowClearButton )
);
2010-09-03 07:06:13 +02:00
$this -> extend ( 'updateFormOptions' , $options );
return $options ;
2009-09-23 00:36:53 +02:00
}
2010-05-16 05:21:04 +02:00
/**
* Return if this form has been modified on the stage site and not published .
* this is used on the workflow module and for a couple highlighting things
*
* @ todo make this a bit smarter - the issue with userforms is that it uses several
* relationships to form fields which has a undefined amount of options so
* for now just say its always modified
*/
public function getIsModifiedOnStage () {
return true ;
}
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 () {
2009-12-07 01:37:20 +01:00
parent :: init ();
2009-04-21 05:44:13 +02:00
// block prototype validation
Validator :: set_javascript_validation_handler ( 'none' );
// load the jquery
2009-12-07 01:37:20 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . '/thirdparty/jquery/jquery.js' );
Requirements :: javascript ( 'userforms/thirdparty/jquery-validate/jquery.validate.min.js' );
2008-09-29 05:18:23 +02:00
}
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 (
2009-12-09 23:28:09 +01:00
'Content' => DBField :: create ( 'HTMLText' , $content ),
2009-04-15 01:50:48 +02:00
'Form' => " "
);
}
}
2010-09-03 07:06:13 +02:00
2009-04-15 01:50:48 +02:00
return array (
2010-06-08 04:03:55 +02:00
'Content' => DBField :: create ( 'HTMLText' , $this -> Content ),
2010-09-03 07:06:13 +02:00
'Form' => $this -> Form ()
2009-04-15 01:50:48 +02:00
);
}
2009-04-27 08:00:05 +02:00
2009-04-14 04:06:18 +02:00
/**
2010-09-03 07:06:13 +02:00
* Get the form for the page . Form can be modified by calling { @ link updateForm ()}
* on a UserDefinedForm extension
2009-04-14 04:06:18 +02:00
*
* @ return Form
*/
2010-09-03 07:06:13 +02:00
function Form () {
$fields = $this -> getFormFields ();
$actions = $this -> getFormActions ();
2010-07-07 11:17:37 +02:00
2010-09-03 07:06:13 +02:00
// get the required fields including the validation
$required = $this -> getRequiredFields ();
// generate the conditional logic
$this -> generateConditionalJavascript ();
$form = new Form ( $this , " Form " , $fields , $actions , $required );
$this -> extend ( 'updateForm' , $form );
return $form ;
}
/**
* Get the form fields for the form on this page . Can modify this FieldSet
* by using { @ link updateFormFields ()} on an { @ link Extension } subclass which
* is applied to this controller
*
* @ return FieldSet
*/
function getFormFields () {
$fields = new FieldSet ();
2009-04-21 05:44:13 +02:00
if ( $this -> Fields ()) {
2010-09-03 07:06:13 +02:00
foreach ( $this -> Fields () as $editableField ) {
// get the raw form field from the editable version
$field = $editableField -> getFormField ();
if ( ! $field ) break ;
2009-04-27 08:17:02 +02:00
2010-09-03 07:06:13 +02:00
// set the error / formatting messages
$title = strip_tags ( " ' " . ( $editableField -> Title ? $editableField -> Title : $editableField -> Name ) . " ' " );
2009-04-27 08:17:02 +02:00
2010-09-03 07:06:13 +02:00
$errorMessage = sprintf ( _t ( 'Form.FIELDISREQUIRED' , '%s is required' ) . '.' , $title );
$errorMessage = ( $editableField -> CustomErrorMessage ) ? $editableField -> CustomErrorMessage : $errorMessage ;
2009-04-21 05:44:13 +02:00
2010-09-03 07:06:13 +02:00
$field -> setCustomValidationMessage ( $errorMessage );
2010-09-08 00:44:43 +02:00
2010-09-03 07:06:13 +02:00
// set the right title on this field
if ( $right = $editableField -> getSetting ( 'RightTitle' )) {
$field -> setRightTitle ( $right );
2010-07-07 11:17:37 +02:00
}
2010-09-03 07:06:13 +02:00
// if this field is required add some
if ( $editableField -> Required ) {
$field -> addExtraClass ( 'requiredField' );
if ( $identifier = UserDefinedForm :: $required_identifier ) {
$title = $field -> Title () . " <span class='required-identifier'> " . $identifier . " </span> " ;
$field -> setTitle ( $title );
2009-10-15 02:33:13 +02:00
}
2009-04-21 05:44:13 +02:00
}
2010-09-03 07:06:13 +02:00
$fields -> push ( $field );
}
}
$this -> extend ( 'updateFormFields' , $fields );
return $fields ;
}
/**
* Generate the form actions for the UserDefinedForm . You
* can manipulate these by using { @ link updateFormActions ()} on
* a decorator .
*
* @ todo Make form actions editable via their own field editor .
*
* @ return FieldSet
*/
function getFormActions () {
$submitText = ( $this -> SubmitButtonText ) ? $this -> SubmitButtonText : _t ( 'UserDefinedForm.SUBMITBUTTON' , 'Submit' );
$actions = new FieldSet (
new FormAction ( " process " , $submitText )
);
if ( $this -> ShowClearButton ) {
$actions -> push ( new ResetFormAction ( " clearForm " ));
}
$this -> extend ( 'updateFormActions' , $actions );
return $actions ;
}
/**
* Get the required form fields for this form . Includes building the jQuery
* validate structure
*
* @ return RequiredFields
*/
function getRequiredFields () {
$required = new RequiredFields ();
$rules = array ();
$validation = array ();
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
if ( $field -> Required ) {
$validation [ $field -> Name ] = $field -> getFormField () -> getCustomValidationMessage ();
$rules [ $field -> Name ] = array_merge ( array ( 'required' ), $field -> getValidation ());
$required -> addRequiredField ( $field -> Name );
2009-04-21 05:44:13 +02:00
}
2010-09-03 07:06:13 +02:00
}
}
// Set the Form Name
$rules = $this -> array2json ( $rules );
$messages = $this -> array2json ( $validation );
// set the custom script for this form
Requirements :: customScript ( <<< JS
( function ( $ ) {
$ ( document ) . ready ( function () {
$ ( " #Form_Form " ) . validate ({
errorClass : " required " ,
messages :
$messages
,
rules :
$rules
});
});
})( jQuery );
JS
, 'UserFormsValidation' );
$this -> extend ( 'updateRequiredFields' , $required );
return $required ;
}
/**
* Generate the javascript for the conditional field show / hiding logic .
* Allows complex rules to be created
* @ return void
*/
function generateConditionalJavascript () {
$default = " " ;
$rules = " " ;
if ( $this -> Fields ()) {
foreach ( $this -> Fields () as $field ) {
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
2010-09-03 07:06:13 +02:00
if ( ! $field -> getShowOnLoad ()) {
$default .= " $ ( \" # " . $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' ]);
2010-04-08 23:18:01 +02:00
$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 ;
2009-11-02 08:22:30 +01:00
case 'ValueLessThan' :
$expression = '$(this).val() < parseFloat("' . $dependency [ 'Value' ] . '")' ;
break ;
case 'ValueLessThanEqual' :
$expression = '$(this).val() <= parseFloat("' . $dependency [ 'Value' ] . '")' ;
break ;
case 'ValueGreaterThan' :
$expression = '$(this).val() > parseFloat("' . $dependency [ 'Value' ] . '")' ;
break ;
case 'ValueGreaterThanEqual' :
$expression = '$(this).val() >= parseFloat("' . $dependency [ 'Value' ] . '")' ;
break ;
2009-04-24 00:52:08 +02:00
default :
2009-05-06 05:34:40 +02:00
$expression = ( $checkboxField ) ? '!($(this).attr("checked"))' : '$(this).val() != "' . $dependency [ 'Value' ] . '"' ;
2009-11-02 08:22:30 +01:00
2009-04-24 00:52:08 +02:00
break ;
}
// put it all together
2010-09-03 07:06:13 +02:00
$rules .= $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
}
2009-04-21 05:44:13 +02:00
Requirements :: customScript ( <<< JS
( function ( $ ) {
$ ( document ) . ready ( function () {
2010-09-03 07:06:13 +02:00
$rules
$default
})
2009-04-21 05:44:13 +02:00
})( jQuery );
JS
2010-09-03 07:06:13 +02:00
, 'UserFormsConditional' );
2009-03-25 03:06:28 +01:00
2008-09-29 05:18:23 +02:00
}
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
*/
2010-09-03 07:06:13 +02:00
function array2json ( $array ) {
2009-04-21 05:44:13 +02:00
foreach ( $array as $key => $value )
if ( is_array ( $value )) {
$result [] = " $key : " . $this -> array2json ( $value );
} else {
$value = ( is_bool ( $value )) ? $value : " \" $value\ " " ;
2010-09-03 07:06:13 +02:00
$result [] = " $key : $value " ;
2009-04-21 05:44:13 +02:00
}
2010-09-03 07:06:13 +02:00
return ( isset ( $result )) ? " { \n " . implode ( ', ' , $result ) . " \n } \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 ) {
2010-09-03 07:06:13 +02:00
$submittedForm = Object :: create ( '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 ;
2010-09-03 07:06:13 +02:00
// if saving is not disabled save now to generate the ID
2009-07-22 06:02:21 +02:00
if ( ! $this -> DisableSaveSubmissions ) $submittedForm -> write ();
2008-09-29 05:18:23 +02:00
$values = array ();
$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 ) {
2010-09-03 07:06:13 +02:00
2009-05-12 08:10:13 +02:00
if ( ! $field -> showInReports ()) continue ;
2010-09-03 07:06:13 +02:00
// create a new submitted form field.
2010-02-14 08:53:16 +01:00
$submittedField = $field -> getSubmittedFormField ();
2008-09-29 05:18:23 +02:00
$submittedField -> ParentID = $submittedForm -> ID ;
$submittedField -> Name = $field -> Name ;
$submittedField -> Title = $field -> Title ;
2010-09-03 07:06:13 +02:00
// save the value from the data
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 ])){
2010-02-14 08:53:16 +01:00
if ( in_array ( " EditableFileField " , $field -> getClassAncestry ())) {
2009-04-28 04:03:13 +02:00
if ( isset ( $_FILES [ $field -> Name ])) {
// create the file from post data
$upload = new Upload ();
$file = new File ();
2010-09-03 07:06:13 +02:00
2009-04-28 04:03:13 +02:00
$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 ;
2010-09-03 07:06:13 +02:00
// attach a file only if lower than 1MB
2009-04-28 04:03:13 +02:00
if ( $file -> getAbsoluteSize () < 1024 * 1024 * 1 ){
$attachments [] = $file ;
}
}
2008-09-29 05:18:23 +02:00
}
}
2010-09-03 07:06:13 +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 );
2010-09-03 07:06:13 +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
2010-09-03 07:06:13 +02:00
// email users on submit.
2009-04-14 04:06:18 +02:00
if ( $this -> EmailRecipients ()) {
2010-09-03 07:06:13 +02:00
2009-04-14 04:06:18 +02:00
$email = new UserDefinedForm_SubmittedFormEmail ( $submittedFields );
2008-09-29 05:18:23 +02:00
$email -> populateTemplate ( $emailData );
2010-09-03 07:06:13 +02:00
2008-09-29 05:18:23 +02:00
if ( $attachments ){
foreach ( $attachments as $file ){
2009-04-20 01:22:22 +02:00
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 " ;
2010-04-26 00:36:09 +02:00
if ( isset ( $emailData [ 'Fields' ]) && ! $recipient -> HideFormData ) {
2009-05-12 08:10:13 +02:00
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
}
2010-09-03 07:06:13 +02:00
$referrer = ( isset ( $data [ 'Referrer' ])) ? '?referrer=' . urlencode ( $data [ 'Referrer' ]) : " " ;
return Director :: redirect ( $this -> Link () . 'finished' . $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' ),
2010-09-03 07:06:13 +02:00
'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)' )),
2010-09-08 00:48:46 +02:00
new TextareaField ( 'EmailBody' , _t ( 'UserDefinedForm.EMAILBODY' , 'Body' ))
2009-04-14 05:26:14 +02:00
);
if ( $this -> Form ()) {
2010-09-07 04:24:54 +02:00
$validEmailFields = DataObject :: get ( " EditableEmailField " , " \" ParentID \" = ' " . ( int ) $this -> FormID . " ' " );
$multiOptionFields = DataObject :: get ( " EditableMultipleOptionField " , " \" ParentID \" = ' " . ( int ) $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 ) {
2010-09-08 00:44:43 +02:00
2009-07-19 22:44:32 +02:00
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-12-04 01:39:56 +01:00
function canEdit () {
return $this -> Form () -> canEdit ();
}
function canDelete () {
return $this -> Form () -> canDelete ();
}
2009-04-14 04:06:18 +02:00
}
2009-12-07 03:04:20 +01:00
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
*/
2009-12-07 03:04:20 +01:00
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 ();
}
2009-12-07 03:04:20 +01:00
}