Navigation:  "The Jet Engine" PHP Application Framework > Creating a Jet Engine Application > Required Files >

application.php

Previous pageReturn to chapter overviewNext page

application.php

 

This is your application object module.   The main Jet Application (also named "application.php", but in the root directory) will automatically include this file for you.

 

How you structure this file is completely up to you.   Essentially, this is the "brains" of your application's front end.  What the program (application.php) will do automatically is this:

 

1.It will "include" this file for you.

 

2.It will initialize your application's object by calling "new"

 

3.It will call methods in your application object that correspond to the "action" parameter sent in the URL parameters

       This means that all you need to do is create methods that match your links.  ie: admin.php?controller=legal&action=sue_me would look for an object method called "sue_me" in the "legal" application.php module.

 

       Whatever your method "returns" is what will be displayed as the main output ( <!-- PLUGIN:BODY --> ) in your page layout.

 

How you handle the rest is up to you.   Our recommendation is that you use the initialization process to perform some obvious functions:

 

Grab any data that you'll need, based on the users' request
Process anything (like saving a record to a database table) based on the users' request
Prepare other objects and datasets that your plugins and front end might want to reference.

 

An well commented example is given below.   Notice that there's no output specifically generated. Rather, these methods are storing data values, doing work, parsing templates, and setting flags.   Output is handled by the associated plugins.php module.

 

 


 

<?php

 

  class blog {

 

 

      var $blog_post;

      var $comments;

      var $articles;

 

 

      /*

         The initializer function.

 

         This function is called on every page view.  It brings in a list of all blog article titles,

         dates, and # of replies for the plugins to use.

 

         It also, if a blog article was specified, loads that article and all user replies into a

         class variable for later use.

      */

 

      function blog() {

 

          global $oSQL;

          global $jet;

          global $config;

 

          // List of all articles, for navigation

          $SQL = "

             SELECT

                *, (SELECT count(*) FROM blog_replies WHERE blog_replies.article_id = blog_articles.rowid ) AS num_replies

             FROM blog_articles

             ORDER BY post_date DESC LIMIT $config[num_articles_on_main_page]

          ";

          $this->articles = $oSQL->search( array("SQL"=>$SQL) );

 

          // If we have an article, get it, and any replies

          if ( ! empty($jet->CGI['article_id']) ) {

             

              $record = new Record( array("Parent"=>$oSQL, "Table"=>"blog_articles", "Schema"=>"blog_articles", "Lookup"=>1, "rowid"=>$jet->CGI['article_id']) );

             

              $this->blog_post = $record->data_as_hash();

             

              $this->get_comments($jet->CGI['article_id']);

 

          }

 

          return;

 

      }

 

 

 

      /*

         A function for internal use, this method returns all of the replies to an article

      */

 

 

      function get_comments($id) {

         

          global $oSQL;

 

          $SQL = "SELECT * FROM blog_replies WHERE article_id = $id";

          $this->comments = $oSQL->search( array("SQL"=>$SQL) );

 

      }

 

 

      /*

         This is the default action.  "index" is called when nothing is specified.  Basically,

         this method just draws a list of the last few articles written, without comments, and

         links to the full page view of the article.

      */

       function index() {

 

          global $config;

          global $jet;

          global $views;

 

 

          $articles = '';

 

          if ( $this->articles ) {

 

              $tpl = new SubTemplate (

                array(

                    "front" => '<<',

                    "back" => '>>',

                    "template_text" => $views['blog']['LAYOUT']['Blog Article']

                )

              );

 

              foreach ( $this->articles as $article ) {

 

                   // Format the date

                  $article['post_date'] = date("m/d/Y h:ia", $article['post_date']);

 

                  $id = $article['rowid'];

 

                  $url = $jet->dynamic_url(

                       array(

                         "controller"=>"blog",

                         "action"=>"article",

                         "article_id"=>$id

                       ),

                       $config['url_format']

                  );

 

                  $article['comments'] = "<a href='$url'>Comments ($article[num_replies])</a>";

 

                  $tpl->set_values($article);

 

                  $articles .= $tpl->merge();

 

              }

          }

 

          $this->title = 'My Blog';

 

          return $articles;

 

       }

 

      /*

         This saves a reply made by a website visitor.   Notice that it only returns back

         a success or fail, and it's own associated text (the error message on failure)

         The front end applications will take care of making these codes into meaningful

         HTML for the end user.

      */

 

 

      function post_reply() {

         

          global $iSQL;

          global $jet;

 

          // Set the date

 

          $jet->CGI['post_date'] = time();

 

 

          $record = new Record( array("Parent"=>$iSQL, "Table"=>"blog_replies", "Schema"=>"blog_replies", "Lookup"=>1, "rowid"=>$jet->CGI['rowid']) );

 

            if ( $record->update_values( $jet->CGI ) ) {

 

               if ( $record->save() ) {

                   

                    $message = "Reply SAVED!";

                    $ok = 1;

               }

 

               else {

                    $message = "An error occurred while updating this reply: " . $record->error_text;

                    $ok = 0;

               }

 

            }

            else {

               $message = "An error occurred while saving this reply: " . $record->error_text;

               $ok = 0 ;

            }

 

            return array($ok, $message);

 

 

      }

 

  }

 

 

?>


This help file was created with an unregistered evaluation copy of Help & Manual. © EC Software. All rights reserved. This message will not appear if you compile this help file with the registered version of Help & Manual.