From 45b0fe42a979d444d547a5248eb2e9e915aaf16a Mon Sep 17 00:00:00 2001
From: wei <>
Date: Sun, 14 Jan 2007 02:10:24 +0000
Subject: Add "block-content" to allow user comments on block level elements in
quickstart docs.
---
.../protected/pages/Tutorial/AjaxChat.page | 264 ++++++++--------
.../pages/Tutorial/CurrencyConverter.page | 343 +++++++++++----------
2 files changed, 317 insertions(+), 290 deletions(-)
(limited to 'demos/quickstart/protected/pages/Tutorial')
diff --git a/demos/quickstart/protected/pages/Tutorial/AjaxChat.page b/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
index 2d49c9f6..ec6fc149 100644
--- a/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
+++ b/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
@@ -1,67 +1,67 @@
This tutorial introduces the Prado web application framework's
+ This tutorial introduces the Prado web application framework's
ActiveRecord
- and Active Controls to build a Chat
- web application. It is assumed that you
+ and Active Controls to build a Chat
+ web application. It is assumed that you
are familiar with PHP and you have access to a web server that is able to serve PHP5 scripts.
This basic chat application will utilize the following ideas/components in Prado.
- Building an AJAX Chat Application
- Building an AJAX Chat Application
+
+
In this tutorial you will build an AJAX Chat web application that allows - multiple users to communicate through their web browser. +
In this tutorial you will build an AJAX Chat web application that allows
+ multiple users to communicate through their web browser.
The application consists of two pages: a login page
that asks the user to enter their nickname and the main application chat
page.
- You can try the application locally or at
+ You can try the application locally or at
Pradosoft.com.
- The main application chat page is shown bellow.
+ The main application chat page is shown bellow.
class="figure" />
The download and installation steps are similar to those in +
The download and installation steps are similar to those in
the Currency converter tutorial.
To create the application, we run from the command line the following.
- See the Command Line Tool
+ See the Command Line Tool
for more details.
-
The above command creates the necessary directory structure and minimal +
The above command creates the necessary directory structure and minimal files (including "index.php" and "Home.page") to run a Prado web application. Now you can point your browser's URL to the web server to serve up the index.php script in the chat directory. - You should see the message "Welcome to Prado!" + You should see the message "Welcome to Prado!"
-The first task for this application is to ensure that each user - of the chat application is assigned with a unique (chosen by the user) +
The first task for this application is to ensure that each user + of the chat application is assigned with a unique (chosen by the user) username. To achieve this, we can secure the main chat application page to deny access to anonymous users. First, let us create the Login page with the following code. We save the Login.php and Login.page in the chat/protected/pages/ directory (there should be a Home.page file created by the command line tool).
-The login page contains +
The login page contains
a
Now we wish that if the user is trying to access the main application +
Now we wish that if the user is trying to access the main application
page, Home.page, before they have logged in, the user is presented with
the Login.page first. We add a chat/protected/application.xml configuration
file to import some classes that we shall use later.
-
If you now try to access the Home page by pointing your browser +
If you now try to access the Home page by pointing your browser to the index.php you will be redirected to the Login page.
-The
The
To implement a custom user manager module class we just need +
To implement a custom user manager module class we just need to extends the TModule class and implement the IUserManager interface. The getGuestName(), getUser() and validateUser() methods are required by the IUserManager interface. We save the custom user manager class as App_Code/ChatUserManager.php. -
-+
The getGuestName() method simply returns the name for a guest user and is not used in our application. The getUser() method returns a TUser object if the username @@ -257,25 +257,25 @@ exists in the database, the TUser object is set with role of "normal" that corresponds to the <authorization> rules defined in our config.xml file.
-The addNewUser() and usernameExists() -method uses the ActiveRecord corresponding to the chat_users table to +
The addNewUser() and usernameExists() +method uses the ActiveRecord corresponding to the chat_users table to add a new user and to check if a username already exists, respectively.
-The next thing to do is change the config.xml configuration to use +
The next thing to do is change the config.xml configuration to use our new custom user manager class. We simply change the <module> configuration with id="users".
-To perform authentication, we just want the user to enter a unique -username. We add a +
To perform authentication, we just want the user to enter a unique
+username. We add a
-In the createNewUser method, when the validation passes (that is, +
+In the createNewUser method, when the validation passes (that is, when the user name is not taken) we add a new user. Afterward we perform -a manual login process: -
Finally, we redirect the client to the default Home page.
-If you try to perform a login now, you will receive an error message like
-"Property 'ChatUserRecord::$last_activity' must not be null as defined
+Default Values for ActiveRecord
+
If you try to perform a login now, you will receive an error message like
+"Property 'ChatUserRecord::$last_activity' must not be null as defined
by column 'last_activity' in table 'chat_users'.". This means that the $last_activity
property value was null when we tried to insert a new record. We need to either
define a default value in the corresponding column in the table and allow null values or set the default
-value in the ChatUserRecord class. We shall demonstrate the later by
+value in the ChatUserRecord class. We shall demonstrate the later by
altering the ChatUserRecord with the addition of a set getter/setter
methods for the last_activity property.
-
Now we are ready to build the main chat application. We use a simple +
Now we are ready to build the main chat application. We use a simple
layout that consist of one panel holding the chat messages, one panel
to hold the users list, a textarea for the user to enter the text message
and a button to send the message.
-Prado Chat Demo
+Prado Chat Demo
@@ -422,9 +424,9 @@ and a button to send the message.
<com:TPlaceHolder ID="userList" />
</com:TForm>
@@ -436,23 +438,23 @@ We added two Active Control components: a
We should have some fun before we proceeding with setting up the chat buffering. We want +
We should have some fun before we proceeding with setting up the chat buffering. We want
to see how we can update the current page when we receive a message. First, we add
an OnClick event handler for the Send button.
-
To append or add some content to the message list panel, we need to use -some methods in the +
To append or add some content to the message list panel, we need to use
+some methods in the
To send a message to all the connected users we need to buffer or store +
To send a message to all the connected users we need to buffer or store
the message for each user. We can use the database to buffer the messages. The
chat_buffer table is defined as follows.
-
We finally arrive at the guts of the chat application logic. First, we +
We finally arrive at the guts of the chat application logic. First, we
need to save a received message into the chat buffer for all the
current users. We add this logic in the ChatBufferRecord class.
-
The next piece of the logic is to retrieve the users' messages from the buffer. +
The next piece of the logic is to retrieve the users' messages from the buffer. We simply load all the messages for a particular username and format that message appropriately (remember to escape the output to prevent Cross-Site Scripting attacks). After we load the messages, we delete those loaded messages and any older -messages that may have been left in the buffer. +messages that may have been left in the buffer.
-Now comes to put the application flow together. In the Home.php we update +
Now comes to put the application flow together. In the Home.php we update
the Send buttons OnClick event handler to use the application
logic we just implemented.
-
At this point the application is actually already functional, just not very +
At this point the application is actually already functional, just not very user friendly. If you open two different browsers, you should be able to communicate between the two users whenever the Send button is clicked.
-The next part is perhaps the more tricker and fiddly than the other tasks. We +
The next part is perhaps the more tricker and fiddly than the other tasks. We
need to improve the user experience. First, we want a list of current users
as well. So we add the following method to Home.php, we can call
this method when ever some callback event is raised, e.g. when the Send
button is clicked.
-
Actually, we want to periodically update the messages and user list as new +
Actually, we want to periodically update the messages and user list as new users join in and new message may arrive from other users. So we need to refresh the message list as well.
-Next, we need to redirect the user back to the login page if the user has +
Next, we need to redirect the user back to the login page if the user has
been inactive for some time, say about 5 mins, we can add this check to any stage
of the page life-cycle. Lets add it to the onLoad() stage.
-
The last few details are to periodically check for new messages and +
The last few details are to periodically check for new messages and
refresh the user list. We can accomplish this by polling the server using a
The final piece requires us to use some javascript. We want that when the +
The final piece requires us to use some javascript. We want that when the
user type some text in the textarea and press the Enter key, we want it
to send the message without clicking on the Send button. We add to the
Home.page some javascript.
-
This completes the tutorial on making a basic chat web application using +
This completes the tutorial on making a basic chat web application using the Prado framework. Hope you have enjoyed it.
diff --git a/demos/quickstart/protected/pages/Tutorial/CurrencyConverter.page b/demos/quickstart/protected/pages/Tutorial/CurrencyConverter.page index c11c1b94..43666560 100644 --- a/demos/quickstart/protected/pages/Tutorial/CurrencyConverter.page +++ b/demos/quickstart/protected/pages/Tutorial/CurrencyConverter.page @@ -1,59 +1,60 @@This tutorial introduces the Prado web application framework and teaches +
This tutorial introduces the Prado web application framework and teaches you how to build a simple web application in a few simple steps. This tutorial assumes that you are familiar with PHP and you have access to a web server that is able to serve PHP5 scripts. -
- -In this tutorial you will build a simple web application that converts - a dollar amount to an other currency, given the rate of that currency +
+ +In this tutorial you will build a simple web application that converts
+ a dollar amount to an other currency, given the rate of that currency
relative to the dollar. The completed application is shown bellow.
class="figure" />
- You can try the application locally or at
+ You can try the application locally or at
Pradosoft.com.
Notice that the application still functions exactly the same if javascript
is not available on the user's browser.
To install Prado, simply download the latest version of Prado from +
To install Prado, simply download the latest version of Prado from http://www.pradosoft.com - and unzip the file to a directory not accessible by your web server + and unzip the file to a directory not accessible by your web server (you may unzip it to a directory accessible by the web server if you wish - to see the demos and test). For further detailed installation, see the + to see the demos and test). For further detailed installation, see the Quickstart Installation guide.
- -The quickest and simplest way to create a new Prado web application is + +
The quickest and simplest way to create a new Prado web application is
to use the command tool prado-cli.php found in the framework
- directory of the Prado distribution. We create a new application by running
+ directory of the Prado distribution. We create a new application by running
the following command in your
- command prompt or console. The command creates a new directory named
- currency-converter in your current working directory.
+ command prompt or console. The command creates a new directory named
+ currency-converter in your current working directory.
You may need to change to the appropriate directory
first.
-
The above command creates the necessary directory structure and minimal +
The above command creates the necessary directory structure and minimal files (including "index.php" and "Home.page") to run a Prado web application. Now you can point your browser's url to the web server to serve up the index.php script in the currency-converter directory. - You should see the message "Welcome to Prado!" + You should see the message "Welcome to Prado!"
- -We start by editing the Home.page file found in the + +
We start by editing the Home.page file found in the
currency-converter/protected/pages/ directory. Files ending
- with ".page" are page templates that contains HTML and Prado controls.
- We simply add two textboxes, three labels and one button as follows.
-
If you refresh the page, you should see something similar to the following figure.
It may not look very pretty or orderly, but we shall change that later using CSS.
class="figure" />
- The first component we add is a + +
+ The first component we add is a
The next two pair of component we add is the + In Prado, only one TForm element is allowed per page. +
+ +The next two pair of component we add is the
The next pair of components are similar and defines the textbox + +
The next pair of components are similar and defines the textbox to hold the dollar value to be converted. The TLabel with ID value "total" defines a simple label. Notice that the ForControl property is absent. This means that this label is simply a simple label which we are going to use to display the converted total amount.
- -The final component is a + +
The final component is a
If you tried clicking on the "Convert" button then the page will refresh + +
If you tried clicking on the "Convert" button then the page will refresh
and does not do anything else. For the button to do some work, we need
to add a "Home.php" to where "Home.page" is. The Home class
should extends the
Prado uses PHP's __autoload method to load classes. The convention - is to use the class name with ".php" extension as filename. + is to use the class name with ".php" extension as filename.
- -So far there is nothing interesting about Prado, we just declared some + +
So far there is nothing interesting about Prado, we just declared some "web components" in some template file named Home.page and created a "Home.php" file with a Home class. The more interesting bits are in Prado's event-driven architecture as we shall see next.
- -We want that when the user click on the "Convert" button, we take the + +
We want that when the user click on the "Convert" button, we take the
values in the textbox, do some calculation and present the user with
the converted total. To handle the user clicking of the "Convert" button
we simply add an OnClick property to the "Convert" button in
- the "Home.page" template and add a corresponding event handler method
- in the "Home.php".
-
The value of the OnClick, "convert_clicked", will be the method
- name in the "Home.php" that will called when the user clicks on the
- "Convert" button.
-
If you run the application in your web browser, enter some values and click the "Convert" button then you should see that calculated value displayed next to the "Amount in Other Currency" label.
- -In the "convert_clicked" method the first parameter, $sender, - corresponds to the object that raised the event, in this case, + +
In the "convert_clicked" method the first parameter, $sender, + corresponds to the object that raised the event, in this case, the "Convert" button. The second parameter, $param contains any additional data that the $sender object may wish to have added.
- -We shall now examine, the three lines that implements the simply currency
- conversion in the "convert_clicked" method.
-
We shall now examine, the three lines that implements the simply currency + conversion in the "convert_clicked" method. +
+
The statement $this->currencyRate corresponds to the
TTextBox component with ID value "currencyRate" in the
"Home.page" template. The Text property of the TTextBox
contains the value that the user entered. So, we obtain this
value by $this->currencyRate->Text which we convert the
- value to a float value.
-
The next line does a similar things, it takes the user value from - the TTextBox with ID value "dollars and converts it to + the TTextBox with ID value "dollars and converts it to a float value.
- -The third line calculates the new amount and set this value in the + +
The third line calculates the new amount and set this value in the
Text property of the TLabel with ID="total".
- Thus, we display the new amount to the user in the label.
-
The way we convert the user entered value to float ensures that the - total amount is always a number. So the user is free to enter what +
The way we convert the user entered value to float ensures that the + total amount is always a number. So the user is free to enter what ever they like, they could even enter letters. The user's experience in using the application can be improved by adding validators - to inform the user of the allowed values in the currency rate and the + to inform the user of the allowed values in the currency rate and the amount to be calcuated.
- -For the currency rate, we should ensure that -
For the currency rate, we should ensure that
+
+ To ensure 1 we add one
+
For the amount to be calculated, we should ensure that -
For the amount to be calculated, we should ensure that
+
To ensure 1 we just add another TRequiredFieldValidator, for 2
we could use a
Now if you try to enter some invalid data in the application or left out + +
Now if you try to enter some invalid data in the application or left out
any of the fields the validators will be activated and present the user
with error messages. Notice that the error messages are presented
without reloading the page. Prado's validators by default validates
using both javascript and server side. The server side validation
is always performed. For the server side, we
- should skip the calculation if the validators are not satisfied. This can
- done as follows.
-
In this simple application we may further improve the user experience +} +
In this simple application we may further improve the user experience by decreasing the responsiveness of the application. One way to achieve a faster response is calculate and present the results without reloading the whole page.
- -We can replace the TButton with the Active Control counter part, + +
We can replace the TButton with the Active Control counter part,
The server side logic remains the same, we just need to import the
Active Controls name space as they are not included by default. We
- add the following line to the begin of "Home.php".
-
If you try the application now, you may notice that the page no longer
+
If you try the application now, you may notice that the page no longer needs to reload to calculate and display the converted total amount. However, since there is not page reload, there is no indication or not obvious that by clicking on the "Convert" button any has happened. @@ -317,59 +338,61 @@ Prado::using('System.Web.UI.ActiveControls.*'); to "calculating..." when the user clicks on the "Convert" button. The text of the "total" label will still be updated with the new calculate amount as before.
- -To indicate that the calculation is in progress, we can change the text + +
To indicate that the calculation is in progress, we can change the text
of the "total" label as follows. We add a ClientSide.OnLoading property
to the "Convert" button (since this button is responsible for requesting
- the calculation).
-
The ClientSide.OnLoading and various
-
The ClientSide.OnLoading and various
+
So far we have built a simple currency converter web application with + +
So far we have built a simple currency converter web application with little attention of the looks and feel. Now we can add a stylesheet to improve the overall appearance of the application. We can simply add the stylesheet inline with the template code or we may create a "theme".
- -To create and use a theme with Prado applications, we simply create a new + +
To create and use a theme with Prado applications, we simply create a new directory "themes/Basic" in the currency-converter directory. You may need to create the themes directory first. Any directory within the themes are considered as a theme with the - name of the theme being the directory name. See the + name of the theme being the directory name. See the Themes and Skins for further details.
- -We simply create a CSS file named "common.css" and save it in the + +
We simply create a CSS file named "common.css" and save it in the
themes/Basic directory. Then we add the following code
- to the beginning of "Home.page" (we add a little more HTML as well).
-
+ The first line <%@ Theme="Basic" %> defines the
+ theme to be used for this page. The