Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 76 additions & 70 deletions code/ContactFormController.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
class ContactForm_Controller extends Page_Controller {
class ContactForm_Controller extends Page_Controller
{

function init() {
parent::init();
Requirements::css("mysite/css/Form.css");
Requirements::javascript("mysite/javascript/thirdparty/jquery.js");
Requirements::javascript("mysite/javascript/thirdparty/jquery-validate/jquery.validate.pack.js");
Requirements::javascript("mysite/javascript/thirdparty/jquery-validate/localization/messages_es.js");

Requirements::customScript('
public function init()
{
parent::init();
Requirements::css("mysite/css/Form.css");
Requirements::javascript("mysite/javascript/thirdparty/jquery.js");
Requirements::javascript("mysite/javascript/thirdparty/jquery-validate/jquery.validate.pack.js");
Requirements::javascript("mysite/javascript/thirdparty/jquery-validate/localization/messages_es.js");

Requirements::customScript('
jQuery(document).ready(function() {
jQuery("#Form_ContactForm").validate({
rules: {
Expand All @@ -23,74 +25,78 @@ function init() {
}
},
messages: {
Name: "'._t("ContactPage.NAME","We need your name").'",
Email: "'._t("ContactPage.EMAIL","Without your real email address, we can't reach you").'",
Comments: "'._t("ContactPage.COMMENTS","What are you thinking? Tell me").'"
Name: "'._t("ContactPage.NAME", "We need your name").'",
Email: "'._t("ContactPage.EMAIL", "Without your real email address, we can't reach you").'",
Comments: "'._t("ContactPage.COMMENTS", "What are you thinking? Tell me").'"
}
});
});
');
}

function ContactForm() {
// Create fields
$fields = new FieldList(
TextField::create('Name')->setTitle(_t('ContactPage.NAMEINPUT',"Name <em>*</em>")),
TextField::create("Cellphone")->setTitle(_t('ContactPage.CELLPHONE',"Cellphone")),
EmailField::create("Email")->setTitle(_t('ContactPage.EMAIL',"Email address"))->setAttribute('type', 'email'),
TextareaField::create("Question")->setTitle(_t('ContactPage.QUESTION',"Question <em>*</em>"))
);
$this->extend('updateContactForm', $fields);
}

public function ContactForm()
{
// Create fields
$fields = new FieldList(
TextField::create('Name')->setTitle(_t('ContactPage.NAMEINPUT', "Name <em>*</em>")),
TextField::create("Cellphone")->setTitle(_t('ContactPage.CELLPHONE', "Cellphone")),
EmailField::create("Email")->setTitle(_t('ContactPage.EMAIL', "Email address"))->setAttribute('type', 'email'),
TextareaField::create("Question")->setTitle(_t('ContactPage.QUESTION', "Question <em>*</em>"))
);
$this->extend('updateContactForm', $fields);

// Create action
$send = new FormAction('SendContactForm', _t('ContactPage.SEND',"Send"));
$send->addExtraClass("success btn");
// Create action
$send = new FormAction('SendContactForm', _t('ContactPage.SEND', "Send"));
$send->addExtraClass("success btn");

$actions = new FieldList(
$send
);
// Create action
$validator = new RequiredFields('Name', 'Email', 'Question');
return new Form($this, 'ContactForm', $fields, $actions, $validator);
}
$actions = new FieldList(
$send
);
// Create action
$validator = new RequiredFields('Name', 'Email', 'Question');
return new Form($this, 'ContactForm', $fields, $actions, $validator);
}

function SendContactForm($data, $form) {
//saves the question in the database
$CustomerQuestion = new CustomerQuestion();
$form->saveInto($CustomerQuestion);
$CustomerQuestion->write();

$cp = DataObject::get_one("ContactPage");
public function SendContactForm($data, $form)
{
//saves the question in the database
$CustomerQuestion = new CustomerQuestion();
$form->saveInto($CustomerQuestion);
$CustomerQuestion->write();

$cp = DataObject::get_one("ContactPage");

//Sets data
$From = $data['Email'];
$To = $cp->Mailto;
$Subject = _t('ContactPage.WEBSITECONTACTMESSAGE',"Website Contact message");
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('ContactEmail');
//populate template
$email->populateTemplate(array(
"CustomerQuestionID" => $CustomerQuestion->ID,
"Name" => $data["Name"],
"Cellphone" => $data["Cellphone"],
"Email" => $data["Email"],
"Question" => $data["Question"]
));
//send mail
if ($email->send()) {
Controller::curr()->redirect(Director::baseURL(). $this->URLSegment . "/success");
}else{
Controller::curr()->redirect(Director::baseURL(). $this->URLSegment . "/error");
}
}
//Sets data
$From = $data['Email'];
$To = $cp->Mailto;
$Subject = _t('ContactPage.WEBSITECONTACTMESSAGE', "Website Contact message");
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('ContactEmail');
//populate template
$email->populateTemplate(array(
"CustomerQuestionID" => $CustomerQuestion->ID,
"Name" => $data["Name"],
"Cellphone" => $data["Cellphone"],
"Email" => $data["Email"],
"Question" => $data["Question"]
));
//send mail
if ($email->send()) {
Controller::curr()->redirect(Director::baseURL(). $this->URLSegment . "/success");
} else {
Controller::curr()->redirect(Director::baseURL(). $this->URLSegment . "/error");
}
}

public function error(){
return $this->httpError(500);
}
public function error()
{
return $this->httpError(500);
}

public function success(){
$renderedContent = $this->renderWith('Page', array('Content' => $this->SubmitText));
return $renderedContent;
}
}
public function success()
{
$renderedContent = $this->renderWith('Page', array('Content' => $this->SubmitText));
return $renderedContent;
}
}
60 changes: 31 additions & 29 deletions code/ContactPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,41 @@
class ContactPage extends Page
{

static $icon = "CustomerQuestions/images/email.png";
static $description = 'Contact form';
static $allow_children = false;
static $db = array(
'Mailto' => 'Varchar(100)', //Email address where submissions will go
'SubmitText' => 'HTMLText' //Text presented OnAfterSubmit
);
public static $icon = "CustomerQuestions/images/email.png";
public static $description = 'Contact form';
public static $allow_children = false;
public static $db = array(
'Mailto' => 'Varchar(100)', //Email address where submissions will go
'SubmitText' => 'HTMLText' //Text presented OnAfterSubmit
);

/**
* We only admit one
*/
function canCreate($member = null) {
if(ContactPage::get()->Count()>1) {
return false;
} else {
return true;
}
}

//CMS fields
function getCMSFields() {
$contactTab = _t('ContactPage.CONTACT',"Contact");
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.".$contactTab, new TextField('Mailto', _t('ContactPage.EMAILSUBMISSIONSTO',"Email submissions to")));
$fields->addFieldToTab("Root.".$contactTab, $submittext = new HTMLEditorField('SubmitText', _t('ContactPage.THANKYOUTEXT',"Thank you text"),10));
return $fields;
}
/**
* We only admit one
*/
public function canCreate($member = null)
{
if (ContactPage::get()->Count()>1) {
return false;
} else {
return true;
}
}

//CMS fields
public function getCMSFields()
{
$contactTab = _t('ContactPage.CONTACT', "Contact");
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.".$contactTab, new TextField('Mailto', _t('ContactPage.EMAILSUBMISSIONSTO', "Email submissions to")));
$fields->addFieldToTab("Root.".$contactTab, $submittext = new HTMLEditorField('SubmitText', _t('ContactPage.THANKYOUTEXT', "Thank you text"), 10));
return $fields;
}
}

/******************
* Controller
******************/

class ContactPage_Controller extends ContactForm_Controller{
}
class ContactPage_Controller extends ContactForm_Controller
{
}
123 changes: 62 additions & 61 deletions code/CustomerQuestion.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
<?php
class CustomerQuestion extends DataObject {
class CustomerQuestion extends DataObject
{

static $singular_name = "Customer Question";
static $plural_name = "Customer Questions";
public static $singular_name = "Customer Question";
public static $plural_name = "Customer Questions";

static $db = array(
"Name" => "Varchar(255)",
"Cellphone" => "Varchar",
"Email" => "Varchar(255)",
"Question" => "Text",
"Answer" => "Text",
"Answered" => "Boolean",
);
public static $db = array(
"Name" => "Varchar(255)",
"Cellphone" => "Varchar",
"Email" => "Varchar(255)",
"Question" => "Text",
"Answer" => "Text",
"Answered" => "Boolean",
);

//Fields to show in the DOM
static $summary_fields = array(
"Name" => "Name",
"Email" => "Email"
);

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Main","Answered");
$fields->addFieldToTab("Root.Main", TextField::create('Name',_t('CustomerQuestion.NAME',"Name"))->setAttribute('readonly', true));
$fields->addFieldToTab("Root.Main", TextField::create('Cellphone',_t('CustomerQuestion.CELLPHONE',"Cell Phone"))->setAttribute('readonly', true));
$fields->addFieldToTab("Root.Main", TextField::create('Email',_t('CustomerQuestion.EMAIL',"Email"))->setAttribute('readonly', true));
$fields->addFieldToTab("Root.Main", TextareaField::create('Question',_t('CustomerQuestion.QUESTION',"Question"))->setAttribute('readonly', true));
//Fields to show in the DOM
public static $summary_fields = array(
"Name" => "Name",
"Email" => "Email"
);

public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Main", "Answered");
$fields->addFieldToTab("Root.Main", TextField::create('Name', _t('CustomerQuestion.NAME', "Name"))->setAttribute('readonly', true));
$fields->addFieldToTab("Root.Main", TextField::create('Cellphone', _t('CustomerQuestion.CELLPHONE', "Cell Phone"))->setAttribute('readonly', true));
$fields->addFieldToTab("Root.Main", TextField::create('Email', _t('CustomerQuestion.EMAIL', "Email"))->setAttribute('readonly', true));
$fields->addFieldToTab("Root.Main", TextareaField::create('Question', _t('CustomerQuestion.QUESTION', "Question"))->setAttribute('readonly', true));

$answer = TextareaField::create('Answer',_t('CustomerQuestion.ANSWER',"Your Answer"));
$answer = TextareaField::create('Answer', _t('CustomerQuestion.ANSWER', "Your Answer"));


if ($this->Answered) {
$fields->addFieldToTab("Root.Main", $answer->setAttribute('readonly', true));
}
else{
$fields->addFieldToTab("Root.Main", $answer);
}

$this->extend('updateCMSFields', $fields);
return $fields;
}

public function onBeforeWrite(){

parent::onBeforeWrite();
// if this 2 conditions are met i know the user is inside the CMS and can answer the question
if (!$this->Answered && !empty($this->Answer)) {
//Set data
$From = $this->Email;
$cp = DataObject::get_one("ContactPage");
$To = $cp->Mailto;

$Subject = _t('CustomerQuestion.ANSWER',"Here is your Answer!");
$Body = $this->Answer;
$email = new Email($From, $To, $Subject, $Body);
//set template
$email->setTemplate('ContactAnswer');
//send mail
if ($email->send()) {
$this->Answered = 1;
return Injector::inst()->get("Dashboard")->Link();
}else{
Controller::curr()->redirect(Director::baseURL(). $this->URLSegment . "/error");
}
}
}
}
if ($this->Answered) {
$fields->addFieldToTab("Root.Main", $answer->setAttribute('readonly', true));
} else {
$fields->addFieldToTab("Root.Main", $answer);
}

$this->extend('updateCMSFields', $fields);
return $fields;
}

public function onBeforeWrite()
{
parent::onBeforeWrite();
// if this 2 conditions are met i know the user is inside the CMS and can answer the question
if (!$this->Answered && !empty($this->Answer)) {
//Set data
$From = $this->Email;
$cp = DataObject::get_one("ContactPage");
$To = $cp->Mailto;

$Subject = _t('CustomerQuestion.ANSWER', "Here is your Answer!");
$Body = $this->Answer;
$email = new Email($From, $To, $Subject, $Body);
//set template
$email->setTemplate('ContactAnswer');
//send mail
if ($email->send()) {
$this->Answered = 1;
return Injector::inst()->get("Dashboard")->Link();
} else {
Controller::curr()->redirect(Director::baseURL(). $this->URLSegment . "/error");
}
}
}
}
22 changes: 12 additions & 10 deletions code/CustomerQuestionsAdmin.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

class CustomerQuestionsAdmin extends ModelAdmin {
public static $managed_models = array('CustomerQuestion'); // Can manage multiple models
static $url_segment = 'customer-questions';
static $menu_title = 'Customer Questions';
public $showImportForm = false;

function BackLink() {
return Injector::inst()->get("Dashboard")->Link();
}
}
class CustomerQuestionsAdmin extends ModelAdmin
{
public static $managed_models = array('CustomerQuestion'); // Can manage multiple models
public static $url_segment = 'customer-questions';
public static $menu_title = 'Customer Questions';
public $showImportForm = false;

public function BackLink()
{
return Injector::inst()->get("Dashboard")->Link();
}
}
Loading