FEATURE: added ability to include userdefined form nested in Content field by using $UserDefinedForm token

This commit is contained in:
Will Rossiter 2009-04-14 23:50:48 +00:00
parent 643d6e35f6
commit 0dfded7444
2 changed files with 56 additions and 37 deletions

View File

@ -9,32 +9,22 @@
class UserDefinedForm extends Page {
/**
* Add Action in the CMS
*
* @var String
* @var String Add Action in the CMS
*/
static $add_action = "A User Form";
/**
* Icon for the User Defined Form in the CMS. Without the extension
* or the -file
*
* @var String
* @var String Icon for the User Defined Form in the CMS. Without the extension
*/
static $icon = "cms/images/treeicons/task";
/**
* What level permission is needed to edit / add
* this page type
*
* @var String
* @var String What level permission is needed to edit / add
*/
static $need_permission = 'ADMIN';
/**
* Fields on the user defined form page.
*
* @var Array
* @var Array Fields on the user defined form page.
*/
static $db = array(
"EmailOnSubmit" => "Boolean",
@ -45,10 +35,7 @@ class UserDefinedForm extends Page {
);
/**
* Default values of variables when this page is created
* in the CMS
*
* @var Array
* @var Array Default values of variables when this page is created
*/
static $defaults = array(
'Content' => '$UserDefinedForm',
@ -69,7 +56,7 @@ class UserDefinedForm extends Page {
*
* @return FieldSet
*/
function getCMSFields() {
public function getCMSFields() {
$fields = parent::getCMSFields();
// field editor
@ -105,45 +92,49 @@ class UserDefinedForm extends Page {
return $fields;
}
function FilterForm() {
// Build fields
/**
* Filter the Submissions page form
*
* @return Form
*/
public function FilterForm() {
$fields = new FieldSet();
$required = array();
foreach( $this->Fields() as $field ) {
$fields->push( $field->getFilterField() );
foreach($this->Fields() as $field) {
$fields->push($field->getFilterField());
}
// Build actions
$actions = new FieldSet(
new FormAction( "filter", _t('UserDefinedForm.SUBMIT', 'Submit') )
new FormAction("filter", _t('UserDefinedForm.SUBMIT', 'Submit'))
);
// set the name of the form
return new Form( $this, "Form", $fields, $actions );
}
/**
* Filter the submissions by the given criteria
*
* @param Array the filter data
* @param Form the form used
* @return Array|String
*/
function filter( $data, $form ) {
public function filter( $data, $form ) {
$filterClause = array( "`SubmittedForm`.`ParentID` = '{$this->ID}'" );
$keywords = preg_split( '/\s+/', $data['FilterKeyword'] );
$keywordClauses = array();
// combine all keywords into one clause
foreach( $keywords as $keyword ) {
foreach($keywords as $keyword) {
// escape %, \ and _ in the keyword. These have special meanings in a LIKE string
$keyword = preg_replace( '/([%_])/', '\\\\1', addslashes( $keyword ) );
$keywordClauses[] = "`Value` LIKE '%$keyword%'";
}
if( count( $keywordClauses ) > 0 ) {
if(count($keywordClauses) > 0) {
$filterClause[] = "( " . implode( ' OR ', $keywordClauses ) . ")";
$searchQuery = 'keywords \'' . implode( "', '", $keywords ) . '\' ';
}
@ -242,6 +233,30 @@ class UserDefinedForm_Controller extends Page_Controller {
parent::init();
}
/**
* 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() {
if($this->Content && $this->Form()) {
$hasLocation = stristr($this->Content, '$UserDefinedForm');
if($hasLocation) {
$content = str_ireplace('$UserDefinedForm', $this->Form()->forTemplate(), $this->Content);
return array(
'Content' => $content,
'Form' => ""
);
}
}
return array(
'Content' => $this->Content,
'Form' => $this->Form
);
}
/**
* Export each of the form submissions for this UserDefinedForm
* instance into a CSV file.
@ -251,7 +266,7 @@ class UserDefinedForm_Controller extends Page_Controller {
*
* @return HTTPResponse / bool
*/
function export() {
public function export() {
if(!$this->canEdit()) return false;
$now = Date("Y-m-d_h.i.s");

View File

@ -4,12 +4,16 @@
<div class="reports" id="FormSubmissions">
<% if ExportLink %>
<a href="$ExportLink" title="Export CSV file"><strong><% _t('EXPORTSUBMISSIONS', 'Export submissions to CSV') %></strong></a>
<a href="$ExportLink" title="<% _t('EXPORTCSVFILE', 'Export CSV file') %>"><strong><% _t('EXPORTSUBMISSIONS', 'Export submissions to CSV') %></strong></a>
<% end_if %>
<% if DeleteLink %>
<a href="$DeleteLink" title="<% _t('DELETEALLSUBMISSIONS', 'Delete Submissions') %>"><strong><% _t('DELETEALLSUBMISSIONS', 'Delete Submissions') %></strong></a>
<% end_if %>
<% control Submissions %>
<div class="report">
<span class="submitted"><% _t('SUBMITTED', 'Submitted at') %> $Created.Nice <% if Recipient %>to $Recipient<% end_if %></span>
<span class="submitted"><% _t('SUBMITTED', 'Submitted at') %> $Created.Nice. <a href="$DeleteLink"><% _t('DELETESUBMISSION', 'Delete Submission') %></a></span>
<table>
<% control FieldValues %>
<tr>