Views
|
|
| Dennis Iversen |
| 14-Feb-2011 11:44:05 |
Index View File
As we stated in the model we would use two views. One for displaying a blog index page, and one for displaying a full entry. All other HTML is created with the form connected to our mysql table in the database. These views are just regular PHP files. Lets start with the index view. In our blog_simple folder we will create a views folder: Create blog_simple/views. In blog_simple/views we will create the file index.inc which is used for displaying our main listing of all entries. It looks like this:
<?php
/**
* view file for listing all entries by updated date.
*
* In the mdoel we have the method blogSimple::indexController()
* which will list all entries in our blog.
*
* We used the template function get_include_contents()
* in this manner:
*
* $str = get_include_contents(_COS_PATH . "/modules/blog_simple/views/index.inc", $rows);
*
* In this template all rows are transformed into the variable $vars
*
* We also use a couple of standard functions, which all can be found in
* lib/common.inc
*/
foreach ($vars as $key => $row) {
$link_url = "/blog_simple/view/$row[id]/";
// we use the following standard function for transforming our
// url into a SEO frindly URL. my latest blog entry becomes
// my_latest_blog_entry
$link_url.= create_seo_title($row['title']);
// create link is also a standard function
// the function just returns a <a href="url">title</a>
// html link from a URL and a title
$link = create_link($link_url, $row['title']);
// we print headline for our blog entry
echo "<h3>$link</h3>\n";
// we fetch some info about the user
$user = blogSimple::getUserInfo($row['user_id']);
// we create a written by string
$written_by = lang::translate('blog_simple_written_by') ;
$written_by.= "<strong>";
// if any profile system is specified we will get a profile
// link. If not: Then the standard get_profile_link will just
// return a clear text version of the user name.
$written_by.= " " . get_profile_link($user) . " ";
$written_by.= "</strong>";
// print to screen
echo $written_by;
// we create a easy to read string about time when entry
// was added or updated.
$datetime = date_create($row['updated']);
$date_formatted = date_format($datetime, register::$vars['coscms_main']['date_format_long']);
echo "<strong>$date_formatted</strong>\n";
// we fetch a module setting telling us how long our abstract
// should be
$teaser_length = get_module_ini('blog_simple_abstract_length');
// we use the standard function substr2 to get our teaser of the entry
$teaser = substr2($row['content'], $teaser_length, 3);
// print teaser to screen
echo "<p>$teaser</p>\n";
// we check if user has access right to view admin options of this
// entry. (as a standard all access control function can be found
// in the session class located in lib/session.php
$ret = session::checkAccessControl('blog_simple_allow', false);
// if access is ok we print some admin links.
if ($ret){
echo create_link("/blog_simple/edit/$row[id]", lang::translate('blog_simple_edit_entry'));
echo " | ";
echo create_link("/blog_simple/delete/$row[id]", lang::translate('blog_simple_delete_entry'));
}
}
Entry View File
The Next view file is the view that will display a single blog entry. In our blog_simple_views folder we create the file view.inc:
It looks like this:
<?php
// print title
echo "<h3>$vars[title]</h3>";
// start paragraph
echo "<p>\n";
// get user info for this blog entry
$user = blogSimple::getUserInfo($vars['user_id']);
// compose written by string
$written_by = lang::translate('blog_simple_written_by');
$written_by.= " <strong>" . get_profile_link($user) . "</strong> ";
// echo written by
echo $written_by;
// compose formatted date as string
$datetime = date_create($vars['updated']);
$date_formatted = date_format($datetime, register::$vars['coscms_main']['date_format_long']);
// echo formatted date
echo "<strong>$date_formatted</strong>";
// echo content
echo $vars['content'];
// check if it is an amdin user and add edit options
$ret = session::checkAccessControl('blog_simple_allow', false);
if ($ret) {
echo create_link("/blog_simple/edit/$vars[id]", lang::translate('blog_simple_edit_entry'));
echo " | ";
echo create_link("/blog_simple/delete/$vars[id]", lang::translate('blog_simple_delete_entry'));
}
Now we have everything except the controllers which ties it all together in some different files.
