Location: PHPKode > projects > AModules3 > amodules-3.0.1/samples/04-apiadmin-customizing/main.php
<?
include 'amodules3/loader.php';
/*
 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 *    FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 *    FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 *    FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 *    FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME 
 *
 * FIXME  This sample is not good anymore. With Beta2 page defenition structure was
 * FIXME  completely changed. I'll write a new template soon, but please disregard
 * FIXME  this one.
 */

class ApiMy extends ApiAdmin {
    function init(){
        /*
         * We are redefining init function to set up our environment. First let's
         * call parent's init:
         */

        parent::init();


        /*
         * Ok, now let's construct our administration system from blocks. Please remember,
         * that all of those steps are optional, but some of them relate on others.
         */


        /*
         * when calling init() Api does it inside try/catch block. If any exception will
         * be thrown here, ApiAdmin will handle them and display information. For production
         * system you'll want to disable default error handling (which is quite useful when
         * developing).
         */


        /*
         * ApiAdmin supports loading data from simple configuration file.
         */

        $this->readConfig();

        /* 
         * you may specify config file name which is config.php by default. Inside that file
         * you will have lines like this:
         *  $config['something']='value';
         *
         * Here are some interesting lines you can use right away:
         *
         *  $config['table_prefix']='ademo_';        // define table prefix (define as DTP)
         *
         *  $config['dsn']='mysql://user:hide@address.com/mydb';
         *
         *  $config['layout']='am3';                // default set of templates (look)
         *
         */


        // We could just do connection and use default error handler:
        //$this->dbConnect();
        /*
         * If we would use that line, it would try to connect to database by using value
         * from config.php / $config['dsn']. However to make things look better, let's
         * customize
         */


        /*
         * First we want to avoid default error message for database connection and display
         * our own message
         */
        try{
            /*
             * Second, we want to specify 'default' DSN, which will be used unless you
             * specify one in config.php
             */
            $this->dbConnect('mysql://hide@address.com/mysql');
        }catch (DBlite_Exception $e){
            echo "<font color=red>In our example we need mysql connectivity to perform some read-only actions over database mysql<p>Please create file config.php containing line: <br><pre>".'$'."config['dsn']='mysql://user:hide@address.com/mysql';</pre><br>Reload this page when done</font>";
            exit;
        }


        /*
         * Another important item in administration system is Menu. ApiAdmin provides
         * a easy way to setting up your menu:
         */

        $this->addMenu('AModules3 ApiAdmin demonstration',
                array(
                    'First Page',
                    'Second Page',
                    'Page3'=>'Page #3',
                    'Page4'=>'Last one'
                    )
                );

        /*
         * 2 arguments are specified, first one contains menu title. Second
         * contains array of 'pages'.
         */

        /*
         * PAGES
         *
         * In ApiAdmin you have to define all the pages you gonna use. For example,
         * if you want to use page 'Preferences', you have to tell ApiAdmin about it
         * when you are initializing.
         *
         * This is done to keep things secure. If you are not mentioning page, it will
         * not be accessible.
         *
         * There are 2 basic ways to register pages. First, if you specify page on the
         * menu, it's defined automatically. 2nd argument to addMenu contains 
         *   $page=>$page_descr
         * pairs. You may omit $page. In this case description will be used as a page
         * name (after removing non a-zA-Z0-9 characters). In our call to addMenu it
         * defines the following pages: FirstPage, SecondPage, Page3 and Page4
         *
         * There is a way to define page without adding it to menu, here how it's used:
         */

        $this->addPage('HiddenPage1');

        /*
         * Now that the page is defined, if it's accessed the following happens:
         * 
         * - we'll check file page/HiddenPage1.php, which would contain class named
         *    page_HiddenPage1. 
         *  
         * else
         *
         * - we'll check if template HiddenPage1.html exist. If it does, it will be
         *    shown as a static filler for page_content.
         *
         *
         * else 
         *
         *  - we'll create a blank page which you'll have to fill out yourself
         */

        /*
         * At this moment we defined HiddenPage1, but we don't have anything to put on it.
         * Let's look into our hello world examples and re-use class HelloWorld from hw3.php
         *
         * In order to put the object on particular page, we use the following:
         */
        if($page=$this->isCurrent('HiddenPage1')){

            /*
             * This code will be only executed if current page is HiddenPage1.
             *
             * if user requested other page all this will be skipped.
             */
            $page->add('HelloWorld',null,'content');
        }


        /*
         * 1. You can actually combine addPage with isCurrent
         * 2. You can customize pages which were defined inside addMenu
         */

        if($page=$this->addPage('HiddenPage2')){
            /*
             * This time, let's custtomize our greetings
             */
            $greeting1 = $page->add('HelloWorld',null,'content');
            $greeting1->message='This is our first message!!';


            /*
             * And add another object
             */
            $greeting2 = $page->add('HelloWorld','hw2','content');

            /*
             * Now pay attention to the 2nd argument of the add() function.
             * 
             * We have used it 'null' for 1st greeting line, but we specified
             * 'hw2' for greeting2. The reason is - 2nd argument contains
             * object sub-name. If it's omited (null), then name of the class
             * is used. Since both greeting lines are of the same class, 2nd
             * will override 1st and only 1 object will be seen on the page.
             *
             * You can try changing 'hw2' into null.
             */

            /*
             * Object naming
             *
             * Each object have it's unique name. When you add sub object
             * into your object, it inherits it's name and adds something
             * additional. For example let's find out name of our 1st line
             * greeting:
             */
            $greeting2->message="greeting1 name is: $greeting1->name";

            /*
             * It says name is: sample04_HiddenPage2_HelloWorld
             *
             * Quite complex, but the name identifies object inside application.
             * That means if your object would set a cookie named as it's name,
             * then it will be unique and will not clash with other objects.
             * Keeping unique ID is important for many AJAX operations.
             */

            /* oh yeah, one more thing. Our pages needs title. If page is
             * defined in addMenu, it will have it's title properly set, but
             * for our hidden page we'll need a custom title 
             */
            $this->template->set('frame_this',"~~~ My frame title ~~~");
            /*
             * I see you have a question. Why 'frame_this'. To find out, read
             * doc/Page-frame_this.txt, but basicly if you have <?frame_this?>..<?/?>,
             * then you'll have a frame arround your 'content' on your page
             */
        }

        /*
         * Ok, we have 2 hidden pages, let's put them on some existing page:
         * Page4
         */
        if($page=$this->isCurrent('Page4')){
            $page->template->set('content',
                    'Visit our <a href="'.$this->getDestinationUrl('HiddenPage1').'">first</a>'.
                    ' or <a href="'.$this->getDestinationUrl('HiddenPage2').'">seccond</a> hidden page'
                    );
        }


        /*
         * Let's try something else. Let's make 'Second Page' redirect to 'Page3'
         */
        if($page=$this->isCurrent('SecondPage')){
            $this->redirect('Page3');
        }

        /*
         * Alright, we are done with the basics. You now know how to create pages in
         * ApiAdmin, how menu works, how to add objects, static content and page
         * redirects.
         */

    }
}

// See hw3.php for more details about this class.
class HelloWorld extends BaseObject {
    function init(){
        $this->message = 'Hello world';
    }
    function render(){
        $this->output('<p>'.$this->message.'</p>');
    }
}


$api = new ApiMy('sample04');
$api->main();
Return current item: AModules3