Jamroom Logo Jamroom 5 Core
is now Open Source!
Follow Jamroom on Twitter!


Adding a menu item to an existing Jamroom Control Panel Menu Index Creating a Module Control Panel in the Jamroom Config: config.php

Creating the Module Form Controller

The Module "Form Controller" is the main PHP script that creates the Forms that the User will see in the Jamroom interface.  It is the only Module script NOT located in the module directory, and must be located directly in the Jamroom directory (this is due to pathing requirements in the master Jamroom Includer).

The Module Form Controller script handles the logic of entering, modifying and deleting items from your module database, or otherwise provides an interface into the module function.

For our jrYouTube module, we have created a Module Form Controller file called "jrYouTube.php", which contains the following:

<?php
/**
 * Jamroom jrYouTube Module Form Controller
 * @copyright 2009 by Talldude Networks LLC
 * @author Brian Johnson - bigguy@talldude.net
 */
require('include/jamroom-include.inc.php');

// Our script
$GLOBALS['JR_SCRIPT_NAME'] = 'jrYouTube.php';

$_post = getPostVars();
// Login required - get $_user
$_user = sessionVerify();
// get the correct language pack included..
$language = getLanguage($_user['user_language']);

// Show correct form based on "mode" received
switch ($_post['mode']) {

    // Create
    case 'create':

        // retrieve saved data from session
        $_rep = getForm('jrYouTube');

        jmHtmlBegin($language['jrYouTube'][1]);
        jmBodyBegin();
        jmSpanCell($language['jrYouTube'][1],'',30,'html_modify.png');
        jrGetFormNotice();
        jmBeginForm('jrYouTube.php?mode=save');
        jmInput($language['jrYouTube'][4],'youtube_video_id','text',$_rep['youtube_video_id']);
        jmInput($language['jrYouTube'][5],'youtube_video_category','text',
          $_rep['youtube_video_category']);
        jrFormSubmit($language['jrYouTube'][2],false,false,'admin.php?s=section_jrYouTubeMenu');
        jmEndForm();
        jmBodyEnd();
        jmHtmlEnd();
        exit;
        break;

    // Save
    case 'save':

        // Save posted values to session
        saveForm('jrYouTube');

        // Error Checking - YouTube ID is required
        if (strlen($_post['youtube_video_id']) === 0) {
            setFormHighlight('youtube_video_id');
            jrSetFormNotice('error',$language['jrYouTube'][6]);
            jrLocation('jrYouTube.php?mode=create');
        } 
     
        // No error - prep database values
        $_post['youtube_video_id']       = dbEscapeString($_post['youtube_video_id']);
        $_post['youtube_video_category'] = dbEscapeString($_post['youtube_video_category']);

        // see if we are doing a new video or an
        // existing video (we'll receive a youtube_id)
        if (checkType($_post['youtube_id'],'number_nz')) {
            $req = "UPDATE {$jamroom_db['jrYouTubeVideos']} SET
                      youtube_video_id       = '{$_post['youtube_video_id']}',
                      youtube_video_time     = '". time() ."',
                      youtube_video_category = '{$_post['youtube_video_category']}'
                     WHERE youtube_id = '{$_post['youtube_id']}'
                       AND youtube_band_id = '{$_user['user_band_id']}'
                     LIMIT 1";
            $url = "jrYouTube.php?mode=update&youtube_id={$_post['youtube_id']}";
        }
        else {
            $req = "INSERT INTO {$jamroom_db['jrYouTubeVideos']} (
                      youtube_band_id,
                      youtube_video_id,
                      youtube_video_time,
                      youtube_video_category
                    ) VALUES (
                      '{$_user['user_band_id']}',
                      '{$_post['youtube_video_id']}',
                      '". time() ."',
                      '{$_post['youtube_video_category']}'
                    )";
            // Set our refresh URL
            $url = 'jrYouTube.php?mode=create';
        }
        $cnt = dbQuery($req,'INSERT_ID');
        if (!checkType($cnt,'number_nz')) {
            jrSetFormNotice('error','Error saving entry - check connection');
            jrLocation('jrYouTube.php?mode=create');
        }
        // Looks good - reset, rebuild and return
        resetForm('jrYouTube');
        setLock($_user['user_band_id'],'on'); 
        jrSetFormNotice('success',$language['jrYouTube'][7]);
        jrLocation($url);
        break;

    // Modify
    case 'modify':

        // Get existing videos for this profile
        $req = "SELECT youtube_id, youtube_video_id, youtube_video_category
                  FROM {$jamroom_db['jrYouTubeVideos']}
                 WHERE youtube_band_id = '{$_user['user_band_id']}'
                 ORDER BY youtube_video_id ASC";
        $_rt = dbQuery($req,'NUMERIC');
        if (isset($_rt[0]) && is_array($_rt[0])) {
            foreach ($_rt as $_video) {
                $_sel["{$_video['youtube_id']}"] = "{$_video['youtube_video_id']} 
                  ({$_video['youtube_video_category']})";
            }
        }
        jmHtmlBegin($language['jrYouTube'][11]);
        jmBodyBegin();
        jmSpanCell($language['jrYouTube'][11],'',30,'html_modify.png');
        jrGetFormNotice();
        jmBeginForm('jrYouTube.php?mode=update');
        jmSelect($language['jrYouTube'][11],'youtube_id',$_sel);
        jrFormSubmit($language['jrYouTube'][11],false,false,'admin.php?s=section_jrYouTubeMenu');
        jmEndForm();
        jmBodyEnd();
        jmHtmlEnd();
        exit;
        break;

    // Update
    case 'update':

        if (!checkType($_post['youtube_id'],'number_nz')) {
            setFormHighlight('youtube_id');
            jrSetFormNotice('error',$language['jrYouTube'][6]);
            jrLocation('jrYouTube.php?mode=modify');
        }
        // Get the request youtube video info from the DB
        $req = "SELECT *
                  FROM {$jamroom_db['jrYouTubeVideos']}
                 WHERE youtube_id = '{$_post['youtube_id']}'
                 LIMIT 1";
        $_rt = dbQuery($req,'SINGLE');
        if (!isset($_rt) || !is_array($_rt)) {
            // Cannot find entry in database
            setFormHighlight('youtube_id');
            jrSetFormNotice('error',$language['jrYouTube'][6]);
            jrLocation('jrYouTube.php?mode=modify');
        }
        jmHtmlBegin($language['jrYouTube'][11]);
        jmBodyBegin();
        jmSpanCell($language['jrYouTube'][11],'',30,'html_modify.png');
        jrGetFormNotice();
        jmBeginForm("jrYouTube.php?mode=save&amp;youtube_id={$_post['youtube_id']}");
        jmInput($language['jrYouTube'][4],'youtube_video_id','text',$_rt['youtube_video_id']);
        jmInput($language['jrYouTube'][5],'youtube_video_category','text',
          $_rt['youtube_video_category']);
        jrFormSubmit($language['jrYouTube'][2],false,false,'admin.php?s=section_jrYouTubeMenu');
        jmEndForm();
        jmBodyEnd();
        jmHtmlEnd();
        exit;
        break;

    // Delete
    case 'delete':

        jmHtmlBegin($language['jrYouTube'][1]);
        jmBodyBegin();
        jmSpanCell($language['jrYouTube'][1],'',30,'html_modify.png');
        jmBeginForm('jrYouTube.php?mode=remove');
        jrGetFormNotice();
     
        // Get our current videos 
        $req = "SELECT *
                  FROM {$jamroom_db['jrYouTubeVideos']}
                 WHERE youtube_band_id = '{$_user['user_band_id']}'";
        $_rt = dbQuery($req,'youtube_video_id',null,false,'youtube_video_id');

        if (is_array($_rt)) {
            jmSelect($language['jrYouTube'][3],'youtube_video_id',$_rt);
            jrFormSubmit($language['jrYouTube'][3],false,false,false,$language['jrYouTube'][9]);
        }
        else {
            jmShowLine($language['jrYouTube'][8]);
            jrCancel('admin.php?s=section_jrYouTubeMenu');
        }
        jmEndForm();
        jmBodyEnd();
        jmHtmlEnd();
        exit;
        break;

    // Remove
    case 'remove':

        // Error Checking - YouTube ID is required
        if (strlen($_post['youtube_video_id']) > 0) {
            $req = "DELETE FROM {$jamroom_db['jrYouTubeVideos']}
                     WHERE youtube_video_id = '". dbEscapeString($_post['youtube_video_id']) ."'
                       AND youtube_band_id = '{$_user['user_band_id']}'
                     LIMIT 1";
            $cnt = dbQuery($req,'COUNT');
            if (isset($cnt) && $cnt == '1') {
                setLock($_user['user_band_id'],'on');
                jrSetFormNotice('success',$language['jrYouTube'][10]);
                jrLocation('jrYouTube.php?mode=delete');
            }
        }
        jrSetFormNotice('error',$language['jrYouTube'][6]);
        jrLocation('jrYouTube.php?mode=delete');
        break;

    // Video Browse - admin only
    case 'video_browse':

        jrAdminOnly();

        jmHtmlBegin('Browse YouTube Videos');
        jmBodyBegin();
        jmSpanCell('Browse YouTube Videos','',30,'html_select.png');
        // Start our header
        $dat[1]['title'] = 'YouTube URL';
        $dat[1]['style'] = 'width:75%;';
        $dat[2]['title'] = 'Category';
        $dat[2]['style'] = 'width:25%;';
        htmlPageSelect('header',$dat);
        unset($dat);

        // Get all of the YouTube Videos entered
        $req = "SELECT *
                  FROM {$jamroom_db['jrYouTubeVideos']}
                 ORDER BY youtube_id DESC";
        $_rt = dbQuery($req,'NUMERIC');
        if (isset($_rt) && is_array($_rt)) {
           foreach ($_rt as $_video) {
               $dat[1]['title'] = "<a href="{$config['jryoutube_base_url']}{$_video['youtube_video_id']}" target="_blank">{$_video['youtube_video_id']}</a>";
               $dat[2]['title'] = $_video['youtube_video_category'];
               htmlPageSelect('row',$dat);
           }
        }
        htmlPageSelect('footer');
        jmBodyEnd();
        jmHtmlEnd();
        exit;
        break;

    // Default - invalid mode
    default:
        jrInvalidOption();
        break;
}
?>

Looking at the Form Controller script, we can see that it provides a code section for each "mode" that we can operate in:

  • "create" - This is used to present the form for Creating a new YouTube Video entry.
  • "save" - This is the ACTION of the create form - this contains the logic to save the posted data from the Create form in the database.
  • "modify" - This section is used to Modify an existing entry.
  • "update" - this section does the actual work of "saving" the changes from the Modify form to the database.
  • "delete" - This creates a small form for the user to select the YouTube video they want to delete.
  • "remove" - This section is the ACTION for the "delete" mode ,and does the actual removal of the video from the database.
  • "video_browse" - this section is used by our Master Admin only to browse the YouTube videos that have been entered by all users.

This is how the script becomes the "Controller" for the module.  Note that your module may have more then one controller script - there's no requirement that there only be one, but it is recommended to try and keep your module to a single Form Controller PHP script so it's easier to know which script is handling which forms.

Adding a menu item to an existing Jamroom Control Panel Menu Adding a menu item to an existing Jamroom Control Panel Menu page 11 of 23 Creating a Module Control Panel in the Jamroom Config: config.php Creating a Module Control Panel in the Jamroom Config: config.php
Solutions Products Support Community Company
Social Media Platform
Social Networking Software
Musician Website Manager
Community Builder
Jamroom 5
Jamroom 5 Modules
Jamroom Marketplace
Support Forum
Documentation
Support Center
Contact Support
Community Forum
Member Sites
Developers
About Us
Contact Us
Privacy Policy
©2003 - 2024 The Jamroom Network