webCOMAND

Packages

In this tutorial, we will create a basic PHP web application to view and manage the presidents database created in the Content Type Tutorial.

We will keep the PHP code very basic to only introduce COMAND PHP API features that query, retrieve and modify Presidents in the repository.  Subsequent tutorials will cover better coding practices and web application approaches, including MVC and webCOMAND Apps.

Create "Presidents Demo" Package

In webCOMAND, a web application can be created as a Package that contains all of the application's code and support files.  A package makes it easy to organize, access and share a collection of related files and objects.  So, the first thing we will do is create a new Package.

  1. Click the Tutorials folder in the folder tree on the left.
  2. Click the New Menu (New Menu) and select "Package" in the drop-down.
  3. Enter the Title "Presidents Demo"
  4. Click the Package tab and enter a unique Namespace for our app, such as "com_presidentsdemo_www".  Reverse domain name notation is not required, but is a best practice to ensure unique package folder names and namespaces.
  5. Click Approve.

Create "public/index.php" PHP File

Now that we have a package, we can add our first PHP web application file.

Files in a Package are mirrored in the file system.  So the following steps will create files/packages/com_presidentsdemo_www/public/index.php.
  1. Click the Contents tab in our new "Presidents Demo" Package.
  2. Click the New Menu (New Menu) in the Contents field and select "PHP File" in the drop-down.
  3. Enter the Filename "public/index.php".
  4. Enter the Text:
    <?php
    // connect to the default comand repository
    $repo = \comand::repo();
    
    // list presidents
    $presidents = $repo->get('FROM President ORDER BY Number');
    echo('<ol>');
    foreach($presidents as $p) {
        echo('<li value="' . $p->Number . '">' .
         '<a href="edit.php?oid=' . $p->OID . '">' . $p->Name . '</a>' .
         '</li>');
    }
    echo('</ol>');
  5. Click Approve and then Back.

Create "public/edit.php" PHP File

Now we can add the edit functionality referenced in the "public/index.php" file.

  1. Click the New Menu (New Menu) in the Contents field and select "PHP File" in the drop-down.
  2. Enter the Filename "public/edit/index.php"
  3. Enter the Text:
    <?php
    function get($key) {
        return isset($_REQUEST[$key]) ? $_REQUEST[$key] : NULL;
    }
    
    function get_file($key) {
        return (isset($_FILES[$key]) && $_FILES[$key]['tmp_name'])
            ? file_get_contents($_FILES[$key]['tmp_name'])
            : NULL;
    }
    
    function edit_president( $oid, $op ) {
        // connect to the default comand repository
        $repo = \comand::repo();
    
        // get and validate president from oid
        $p = $repo->get_object_by_oid($oid);
        if(!$p || $p->Type->Identifier != 'President') {
            return '<p>Invalid President OID.</p>';
        }
    
        // if this is an update, get and submit form values
        if($op == 'update') {
            try {
                $p->Name = get('name');
                $p->Number = get('number');
                if($photo = get_file('photo')) {
                    $p->Photo = $photo;
                }
                $p->approve();
            } catch( Exception $e ) {
                return '<p>Update Error: ' . $e->getMessage() . '</p>';
            }
        }
    
        $foid = $p->Type->get_field('Photo')->OID;
        $photo_url = "/com_webcomand/img/$oid/$foid/96x96/4";
        echo <<<FORM
    <form method="POST" enctype="multipart/form-data">
        <input type="hidden" name="op" value="update" />
        <input type="hidden" name="oid" value="$oid" />
        <label>Number</label>
        <input type="number" name="number" value="$p->Number" />
        <label>Name</label>
        <input type="text" name="name" value="$p->Name" />
        <label>Photo</label>
        <img src="$photo_url" /> 
        <input type="file" name="photo" />
        <input type="submit" value="Update" />
    </form>
    FORM;
    }
    ?>
    <html>
    <head>
        <title>Presidents Demo</title>
        <style type="text/css">
            label, input, img {display: block; margin: 4px 0;}
            input {margin-bottom 16px; padding: 4px;}
        </style>
    </head>
    <body>
    <?php edit_president( get('oid'), get('op') ); ?>
    </body>
    </html>
  4. Click Approve and then Back.

Conclusion

Files in the public subfolder of a package can be accessed on the web by appending the package namespace and file path within the package's public folder to the end of the webCOMAND base URL.  If the filename is not specified at the end of the path, index.php will be discovered and served automatically, and if index.php is not found, index.html will be discovered and served if it exists.

So, you can now view and edit Presidents in the web application at:
https://<account>.webcomand.com/com_presidentsdemo_www/