Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tutorial 2/3 and some howto tweaks

  • Loading branch information...
commit 0308cc20054c242a1e67e849f7fc96b225f638ce 1 parent 6d8976e
@chillu chillu authored
View
56 docs/en/howto/navigation-menu.md
@@ -1,11 +1,16 @@
-#How to create a navigation menu
+# How to create a navigation menu
-To create a navigation menu, we will create a new template file: Navigation.ss. Put this file inside the templates/Includes folder in your theme.
+In this how-to, we'll create a simple menu which
+you can use as the primary navigation for your website.
+
+Add the following code to your main template,
+most likely the "Page" template in your theme,
+located in `themes/<mytheme>/templates/Page.ss`.
:::ss
<ul>
<% loop Menu(1) %>
- <li>
+ <li>
<a href="$Link" title="Go to the $Title page" class="$LinkingMode">
<span>$MenuTitle</span>
</a>
@@ -13,47 +18,4 @@ To create a navigation menu, we will create a new template file: Navigation.ss.
<% end_loop %>
</ul>
-To include this file in your main template, use the 'include' control code. The include control code will insert a template from the Includes folder into your template. The code for including our navigation menu looks like this:
-
- :::ss
- <% include Navigation %>
-
-Add this to the templates/Page.ss file where you want the menu to render. The template code in Menu1.ss is rendered as an unordered list in HTML; let's break down this file to see how this works.
-
-The first and last lines of the file are HTML tags to open and close an unordered list.
-
- :::ss
- <ul>
- <% loop Menu(1) %>
- <li>
- <a href="$Link" title="Go to the $Title page" class="$LinkingMode">
- <span>$MenuTitle</span>
- </a>
- </li>
- <% end_loop %>
- </ul>
-
-Line 2 and 4 use a template code called a loop. A loop iterates over a DataObjectSet; for each DataObject inside the set, everything between the <% loop %> and <% end_loop %> tags are repeated. Here we iterate over the Menu(1) DataObjectSet and this returns the set of all pages at the top level. (For a list of other controls you can use in your templates, see the [templates page](../reference/templates) .)
-
- :::ss
- <ul>
- <% loop Menu(1) %>
- <li>
- <a href="$Link" title="Go to the $Title page" class="$LinkingMode">
- <span>$MenuTitle</span>
- </a>
- </li>
- <% end_loop %>
- </ul>
-
-Line 3 is where we insert the list item for each menu item. It is sandwiched by the list item opening and closing tags, <li> and </li>. Inside we have a link, using some template codes to fill in the information for each page:
-
-* $Link – the link to the page
-* $Title – the full title of the page (this is a field in the CMS)
-* $MenuTitle – the menu title of the page (this is a field in the CMS)
-* $LinkingMode – which returns one of three things used as a CSS class to style each scenario differently.
- * current – this is the page that is currently being rendered
- * section – this page is a child of the page currently being rendered
- * link – this page is neither current nor section
-
-
+More details on creating a menu are explained as part of ["Tutorial 1: Building a basic site"](/tutorials/1-building-a-basic-site), as well as ["Page type templates" topic](/topics/page-type-templates).
View
106 docs/en/howto/simple-contact-form.md
@@ -1,56 +1,51 @@
# How to make a simple contact form
-To make a contact form, begin by making a page type for the form to live on – we don't want to see the contact form on every page. Here we have the skeleton code for a ContactPage page type:
+In this how-to, we'll explain how to set up a specific page type
+holding a contact form, which submits a message via email.
+Let's start by defining a new `ContactPage` page type:
:::php
<?php
class ContactPage extends Page {
- static $db = array(
- );
}
class ContactPage_Controller extends Page_Controller {
-
+ function Form() {
+ $fields = new FieldList(
+ new TextField('Name'),
+ new EmailField('Email'),
+ new TextareaField('Message')
+ );
+ $actions = new FieldList(
+ new FormAction('submit', 'Submit')
+ );
+ return new Form($this, 'Form', $fields, $actions);
+ }
}
-To create a form, we create a Form object on a function on our page controller. We'll call this function 'Form()' on our ContactPage_Controller class. It doesn't matter what you call your function, but it's standard practice to name the function Form() if there's only a single form on the page. Below is the function to create our contact form:
-
- :::php
- function Form() {
- $fields = new FieldSet(
- new TextField('Name'),
- new EmailField('Email'),
- new TextareaField('Message')
- );
-
- $actions = new FieldSet(
- new FormAction('submit', 'Submit')
- );
-
- return new Form($this, 'Form', $fields, $actions);
- }
+To create a form, we instanciate a `Form` object on a function on our page controller. We'll call this function `Form()`. You're free to choose this name, but it's standard practice to name the function `Form()` if there's only a single form on the page.
There's quite a bit in this function, so we'll step through one piece at a time.
:::php
- $fields = new FieldSet(
+ $fields = new FieldList(
new TextField('Name'),
new EmailField('Email'),
new TextareaField('Message')
);
-First we create all the fields we want in the contact form, and put them inside a FieldSet. You can find a list of form fields available on the `[api:FormField]` page.
+First we create all the fields we want in the contact form, and put them inside a FieldList. You can find a list of form fields available on the `[api:FormField]` page.
:::php
- $actions = FieldSet(
+ $actions = FieldList(
new FormAction('submit', 'Submit')
);
-We then create a `[api:FieldSet]` of the form actions, or the buttons that submit the form. Here we add a single form action, with the name 'submit', and the label 'Submit'. We'll use the name of the form action later.
+We then create a `[api:FieldList]` of the form actions, or the buttons that submit the form. Here we add a single form action, with the name 'submit', and the label 'Submit'. We'll use the name of the form action later.
:::php
return new Form('Form', $this, $fields, $actions);
-Finally we create the Form object and return it. The first argument is the name of the form – this has to be the same as the name of the function that creates the form, so we've used 'Form'. The second argument is the controller that the form is on – this is almost always $this. The third and fourth arguments are the fields and actions we created earlier.
+Finally we create the `Form` object and return it. The first argument is the name of the form – this has to be the same as the name of the function that creates the form, so we've used 'Form'. The second argument is the controller that the form is on – this is almost always $this. The third and fourth arguments are the fields and actions we created earlier.
To show the form on the page, we need to render it in our template. We do this by appending $ to the name of the form – so for the form we just created we need to add $Form. Add $Form to the themes/currenttheme/Layout/Page.ss template, below $Content.
@@ -64,26 +59,38 @@ If you now create a ContactPage in the CMS (making sure you have rebuilt the dat
Now that we have a contact form, we need some way of collecting the data submitted. We do this by creating a function on the controller with the same name as the form action. In this case, we create the function 'submit' on the ContactPage_Controller class.
:::php
- function submit($data, $form) {
- $email = new Email();
-
- $email->setTo('siteowner@mysite.com');
- $email->setFrom($data['Email']);
- $email->setSubject("Contact Message from {$data["Name"]}");
-
- $messageBody = "
- <p><strong>Name:</strong> {$data['Name']}</p>
- <p><strong>Website:</strong> {$data['Website']}</p>
- <p><strong>Message:</strong> {$data['Message']}</p>
- ";
- $email->setBody($messageBody);
- $email->send();
- return array(
- 'Content' => '<p>Thank you for your feedback.</p>',
- 'Form' => ''
- );
+ class ContactPage_Controller extends Page_Controller {
+ function Form() {
+ // ...
+ }
+ function submit($data, $form) {
+ $email = new Email();
+
+ $email->setTo('siteowner@mysite.com');
+ $email->setFrom($data['Email']);
+ $email->setSubject("Contact Message from {$data["Name"]}");
+
+ $messageBody = "
+ <p><strong>Name:</strong> {$data['Name']}</p>
+ <p><strong>Website:</strong> {$data['Website']}</p>
+ <p><strong>Message:</strong> {$data['Message']}</p>
+ ";
+ $email->setBody($messageBody);
+ $email->send();
+ return array(
+ 'Content' => '<p>Thank you for your feedback.</p>',
+ 'Form' => ''
+ );
+ }
}
+<div class="hint" markdown="1">
+ Caution: This form is prone to abuse by spammers,
+ since it doesn't enforce a rate limitation, or checks for bots.
+ We recommend to use a validation service like the ["recaptcha" module](http://www.silverstripe.org/recaptcha-module/)
+ for better security.
+</div>
+
Any function that receives a form submission takes two arguments: the data passed to the form as an indexed array, and the form itself. In order to extract the data, you can either use functions on the form object to get the fields and query their values, or just use the raw data in the array. In the example above, we used the array, as it's the easiest way to get data without requiring the form fields to perform any special transformations.
This data is used to create an email, which you then send to the address you choose.
@@ -95,20 +102,11 @@ The final thing we do is return a 'thank you for your feedback' message to the u
All forms have some basic validation built in – email fields will only let the user enter email addresses, number fields will only accept numbers, and so on. Sometimes you need more complicated validation, so you can define your own validation by extending the Validator class.
-The Sapphire framework comes with a predefined validator called 'RequiredFields', which performs the common task of making sure particular fields are filled out. Below is the code to add validation to a contact form:
+The framework comes with a predefined validator called `[api:RequiredFields]`, which performs the common task of making sure particular fields are filled out. Below is the code to add validation to a contact form:
function Form() {
- $fields = new FieldSet(
- new TextField('Name'),
- new EmailField('Email'),
- new TextareaField('Message')
- );
-
- $actions = new FieldSet(
- new FormAction('submit', 'Submit')
- );
+ // ...
$validator = new RequiredFields('Name', 'Message');
-
return new Form($this, 'Form', $fields, $actions, $validator);
}
View
13 docs/en/reference/database-structure.md
@@ -111,15 +111,4 @@ The information documented in this page is reflected in a few places in the code
* augmentQuery() is responsible for altering the normal data selection queries to support versions.
* augmentDatabase() is responsible for specifying the altered database schema to support versions.
* `[api:MySQLDatabase]`: getNextID() is used when creating new objects; it also handles the mechanics of
-updating the database to have the required schema.
-
-
-## Future work
-
-* We realise that a fixed mapping between the database and object-model isn't appropriate in all cases. In particular,
-it could be beneficial to set up a SilverStripe data-object as an interface layer to the databases of other
-applications. This kind of configuration support is on the cards for development once we start looking more seriously
-at providing avenues for clean integration between systems.
-* Some developers have commented that the the database layer could be used to maintain the relational integrity of this
-database structure.
-* It could be desirable to implement a non-repeating auto-numbering system.
+updating the database to have the required schema.
View
6 docs/en/tutorials/2-extending-a-basic-site.md
@@ -236,7 +236,7 @@ By default the field name *'Date'* or *'Author'* is shown as the title, however
Because our new pages inherit their templates from *Page*, we can view anything entered in the content area when navigating to these pages on our site. However, as there is no reference to the date or author fields in the *Page* template this data is not being displayed.
-To fix this we will create a template for each of our new page types. We'll put these in *themes/tutorial/templates/Layout* so we only have to define the page specific parts: SilverStripe will use *themes/tutorial/templates/Page.ss* for the basic
+To fix this we will create a template for each of our new page types. We'll put these in *themes/simple/templates/Layout* so we only have to define the page specific parts: SilverStripe will use *themes/simple/templates/Page.ss* for the basic
page layout.
### ArticlePage Template
@@ -366,7 +366,7 @@ It would be nice to greet page visitors with a summary of the latest news when t
This function simply runs a database query that gets the latest news articles from the database. By default, this is five, but you can change it by passing a number to the function. See the [Data Model](../topics/datamodel) documentation for details. We can reference this function as a page control in our *HomePage* template:
-**themes/tutorial/templates/Layout/Homepage.ss**
+**themes/simple/templates/Layout/Homepage.ss**
:::ss
...
@@ -514,7 +514,7 @@ resize the image every time the page is viewed.
The *StaffPage* template is also very straight forward.
-**themes/tutorial/templates/Layout/StaffPage.ss**
+**themes/simple/templates/Layout/StaffPage.ss**
:::ss
<div class="content-container">
View
101 docs/en/tutorials/3-forms.md
@@ -4,7 +4,7 @@
This tutorial is intended to be a continuation of the first two tutorials ([first tutorial](1-building-a-basic-site), [second tutorial](2-extending-a-basic-site)). In this tutorial we will build on the site we developed in the earlier tutorials and explore forms in SilverStripe. We will look at custom coded forms: forms which need to be written in PHP.
-Instead of using a custom coded form, we could use the [userforms module](http://silverstripe.org/user-forms-module). This module allows users to construct forms via the CMS. A UserDefinedForm is much quicker to implement, but lacks the flexibility of a coded form.
+Instead of using a custom coded form, we could use the [userforms module](http://silverstripe.org/user-forms-module). This module allows users to construct forms via the CMS. A form created this way is much quicker to implement, but also lacks the flexibility of a coded form.
## What are we working towards?
@@ -55,27 +55,24 @@ Let's step through this code.
:::php
// Create fields
- $fields = new FieldList(
- new TextField('Name'),
- new OptionsetField('Browser', 'Your Favourite Browser', array(
- 'Firefox' => 'Firefox',
- 'Chrome' => 'Chrome',
- 'Internet Explorer' => 'Internet Explorer',
- 'Safari' => 'Safari',
- 'Opera' => 'Opera',
- 'Lynx' => 'Lynx'
- ))
- );
-
-
-First we create our form fields.
+ $fields = new FieldList(
+ new TextField('Name'),
+ new OptionsetField('Browser', 'Your Favourite Browser', array(
+ 'Firefox' => 'Firefox',
+ 'Chrome' => 'Chrome',
+ 'Internet Explorer' => 'Internet Explorer',
+ 'Safari' => 'Safari',
+ 'Opera' => 'Opera',
+ 'Lynx' => 'Lynx'
+ ))
+ );
-We do this by creating a `[api:FieldList]` and passing our fields as arguments. The first field is a
-`[api:TextField]` with the name 'Name'.
+First we create our form fields.
+We do this by creating a `[api:FieldList]` and passing our fields as arguments.
+The first field is a `[api:TextField]` with the name 'Name'.
There is a second argument when creating a field which specifies the text on the label of the field. If no second
argument is passed, as in this case, it is assumed the label is the same as the name of the field.
-
The second field we create is an `[api:OptionsetField]`. This is a dropdown, and takes a third argument - an
array mapping the values to the options listed in the dropdown.
@@ -85,13 +82,9 @@ array mapping the values to the options listed in the dropdown.
);
-After creating the fields, we create the form actions. Form actions appear as buttons at the bottom of the form.
-
-The first argument is the name of the function to call when the button is pressed, and the second is the label of the
-button.
-
+After creating the fields, we create the form actions. Form actions appear as buttons at the bottom of the form.
+The first argument is the name of the function to call when the button is pressed, and the second is the label of the button.
Here we create a 'Submit' button which calls the 'doBrowserPoll' method, which we will create later.
-
All the form actions (in this case only one) are collected into a `[api:FieldList]` object the same way we did with
the fields.
@@ -100,16 +93,14 @@ the fields.
Finally we create the `[api:Form]` object and return it.
-
The first argument is the controller that contains the form, in most cases '$this'. The second is the name of the method
that returns the form, which is 'BrowserPollForm' in our case. The third and fourth arguments are the
FieldLists containing the fields and form actions respectively.
After creating the form function, we need to add the form to our home page template.
+Add the following code to the top of your home page template, just before `<div class="Content">`:
-Add the following code to the top of your home page template, just before the first Content `<div>`:
-
-**themes/tutorial/templates/Layout/HomePage.ss**
+**themes/simple/templates/Layout/HomePage.ss**
:::ss
...
@@ -120,28 +111,11 @@ Add the following code to the top of your home page template, just before the fi
<div class="Content">
...
-your HomePage.ss file should now look like this:
- :::ss
- <div id="BrowserPoll">
- <h2>Browser Poll</h2>
- $BrowserPollForm
- </div>
+In order to make the graphs render correctly,
+we need to add some CSS styling.
+Add the following code to the existing `form.css` file:
- <div class="content-container typography">
- <article>
- <div id="Banner">
- <img src="http://www.silverstripe.org/themes/silverstripe/images/sslogo.png" alt="Homepage image" />
- </div>
-
- <div class="content">$Content</div>
- </article>
- $Form
- $PageComments
- </div>
-
-Add the following code to the form style sheet:
-
-**themes/tutorial/css/form.css**
+**themes/simple/css/form.css**
:::css
/* BROWSER POLL */
@@ -182,18 +156,17 @@ Add the following code to the form style sheet:
}
-This CSS code will ensure that the form is formatted and positioned correctly. All going according to plan, if you visit [http://localhost/your_site_name/home?flush=all](http://localhost/your_site_name/home?flush=all) it should look something like this:
+All going according to plan, if you visit [http://localhost/your_site_name/home?flush=all](http://localhost/your_site_name/home?flush=all) it should look something like this:
![](_images/tutorial3_pollform.jpg)
## Processing the form
-Great! We now have a browser poll form, but it doesn't actually do anything. In order to make the form work, we have to implement the 'doBrowserPoll' method that we told it about.
+Great! We now have a browser poll form, but it doesn't actually do anything. In order to make the form work, we have to implement the 'doBrowserPoll()' method that we told it about.
First, we need some way of saving the poll submissions to the database, so we can retrieve the results later. We can do this by creating a new object that extends from `[api:DataObject]`.
-
-If you recall, in the [second tutorial](2-extending-a-basic-site) we said that all objects that inherit from DataObject and have their own fields are stored in tables the database. Also recall that all pages extend DataObject indirectly through `[api:SiteTree]`. Here instead of extending SiteTree (or `[api:Page]`) to create a page type, we will extend DataObject directly:
+If you recall, in the [second tutorial](2-extending-a-basic-site) we said that all objects that inherit from DataObject and have their own fields are stored in tables the database. Also recall that all pages extend DataObject indirectly through `[api:SiteTree]`. Here instead of extending SiteTree (or `[api:Page]`) to create a page type, we will extend `[api:DataObject]` directly:
**mysite/code/BrowserPollSubmission.php**
@@ -206,7 +179,6 @@ If you recall, in the [second tutorial](2-extending-a-basic-site) we said that a
);
}
-
If we then rebuild the database ([http://localhost/your_site_name/dev/build?flush=all](http://localhost/your_site_name/dev/build?flush=all)), we will see that the *BrowserPollSubmission* table is created. Now we just need to define 'doBrowserPoll' on *HomePage_Controller*:
**mysite/code/HomePage.php**
@@ -224,9 +196,7 @@ If we then rebuild the database ([http://localhost/your_site_name/dev/build?flus
A function that processes a form submission takes two arguments - the first is the data in the form, the second is the `[api:Form]` object.
-
In our function we create a new *BrowserPollSubmission* object. Since the name of our form fields, and the name of the database fields, are the same we can save the form directly into the data object.
-
We call the 'write' method to write our data to the database, and '$this->redirectBack()' will redirect the user back to the home page.
## Form validation
@@ -255,7 +225,6 @@ If we then open the homepage and attempt to submit the form without filling in t
## Showing the poll results
Now that we have a working form, we need some way of showing the results.
-
The first thing to do is make it so a user can only vote once per session. If the user hasn't voted, show the form, otherwise show the results.
We can do this using a session variable. The `[api:Session]` class handles all session variables in SilverStripe. First modify the 'doBrowserPoll' to set the session variable 'BrowserPollVoted' when a user votes.
@@ -280,21 +249,23 @@ Then we simply need to check if the session variable has been set in 'BrowserPol
it is.
:::php
- public function BrowserPollForm() {
- if(Session::get('BrowserPollVoted')) {
- return false;
- }
+ // ...
+ class HomePage_Controller extends Page_Controller {
// ...
- }
+ public function BrowserPollForm() {
+ if(Session::get('BrowserPollVoted')) return false;
+ // ...
+ }
+ }
-If you visit the home page now you will see you can only vote once per session; after that the form won't be shown. You can start a new session by closing and reopening your browser (or if you're using Firefox and have installed the [Web Developer](http://chrispederick.com/work/web-developer/) extension, you can use its Clear Session Cookies command).
+If you visit the home page now you will see you can only vote once per session; after that the form won't be shown. You can start a new session by closing and reopening your browser,
+or clearing your browsing session through your browsers preferences.
Although the form is not shown, you'll still see the 'Browser Poll' heading. We'll leave this for now: after we've built the bar graph of the results, we'll modify the template to show the graph instead of the form if the user has already voted.
Now that we're collecting data, it would be nice to show the results on the website as well. We could simply output every vote, but that's boring. Let's group the results by browser, through the SilverStripe data model.
-
In the [second tutorial](/tutorials/2-extending-a-basic-site), we got a collection of news articles for the home page by using the 'ArticleHolder::get()' function, which returns a `[api:DataList]`. We can get all submissions in the same fashion, through `BrowserPollSubmission::get()`. This list will be the starting point for our result aggregation.
Create the function 'BrowserPollResults' on the *HomePage_Controller* class.
@@ -347,7 +318,7 @@ The `groupBy()` method splits our list by the 'Browser' field passed to it, crea
The final step is to create the template to display our data. Change the 'BrowserPoll' div to the below.
-**themes/tutorial/templates/Layout/HomePage.ss**
+**themes/simple/templates/Layout/HomePage.ss**
:::ss
<div id="BrowserPoll">
Please sign in to comment.
Something went wrong with that request. Please try again.