Amazing: this is automated output. got converting of %% code blocks running. werner seems like php highlighter is not installed on this machine?

FIXME: Look over new file...

FIXME: code examples really up to date (0.4.0)? WernerKrauss /21.01.2005 10:33/

{{include page="!/FormGeneration" nomark="1"}}

{{include page="!/InputValidation" nomark="1"}}

{{include page="!/I18N" nomark="1"}}

Sending emails, HTML formatted with plain text attachments (PEAR::Mail)

<?php
    $options = array(
            'toEmail'       => $conf['email']['info'],
            'toRealName'    => 'Admin',
            'fromEmail'     => $oContact->email,
            'fromRealName'  => $contacterName,
            'replyTo'       => $oContact->email,
            'subject'       => SGL_Output::translate('Contact Enquiry from ') . $conf['site']['name'],
            'type'          => $oContact->type,
            'body'          => $oContact->comment,
            'template'      => SGL_MOD_DIR . '/' . $this->module . '/templates/' .
                $_SESSION['prefs']['theme'] . '/email_contact_us.php',
    );
    $message = & new SGL_Emailer($options);
    $message->prepare();
    return $message->send();
?>

{{include page="!/DB" nomark="1"}}

Simple template syntax with Flexy, this construct iterates through a collection of user objects

            <tr class="{switchRowClass()}" flexy:foreach="results,key,valueObj">
                <td align="center"><input type="checkbox" name="frmDelete[]" value="{valueObj.id}"></td>
                <td>{valueObj.id}</td>
                <td>{valueObj.username}</td>
                <td>{valueObj.link_gid.name}</td>
                <td>{valueObj.email}</td>
            </tr>

Searching a folder for files with a certain extension, attempting to delete them, redirecting to success page or logging an error on failure (with PEAR's System class)

<?php
    $dir = SGL_PATH . '/tmpl/basic';
    $aFiles = System::find("$dir -name *.php -name *.serial");
    if (!@System::rm($aFiles] {
        SGL::raiseError('There was a problem deleting the files', 
            SGL_ERROR_FILEUNWRITABLE);
    } else {
        SGL_Output::msgSet('Template cache successfullly deleted');
        SGL_HTTP::redirect('translationMgr.php');
    }
?>

Easy redirects (with SGL_HTTP class)

Redirects to standard method of the current module

<?php
SGL_HTTP::redirect();
?>

Redirects to other method of the current module

<?php
SGL_HTTP::redirect(null, array('action'=>myaction'];
?>

Redirects to other page

<?php
SGL_HTTP::redirect('pagetogo.html', array('myparam'=>myvalue'];
?>

Writing an ini file with form input (with PEAR's Config class)

<?php
        switch ($input->action) {
        case 'insert':
            $c = new Config();
            ''  read configuration data and get reference to root
            $root = & $c->parseConfig($input->conf, 'phparray');
            ''  write configuration to file
            $result = $c->writeConfig(SGL_PATH . '/etc/' . SGL_SERVER_NAME . '.default.conf.ini', 'inifile');
            if (!PEAR::isError($result] {

                SGL_Output::msgSet('config info successfully updated');
                SGL_HTTP::redirect('configMgr.php');
            } else {
                SGL::raiseError('There was a problem saving your configuration', SGL_ERROR_FILEUNWRITABLE);
            }
            break;
?>

App controller method, all page processing is controlled by this simple workflow

<?php
    function go(& $request)
    {
        SGL::logMessage(null, PEAR_LOG_DEBUG);
        ''  do general session & language initialisation and access-control check
        $this->initPage($request);
        ''  setup output object
        $output = & new SGL_Output();
        ''  validate the information posted from the browser
        $validatedInput = $this->page->validate($request, $output);
        ''  if input is valid, copy data members from input to output
        ''  and send to be processed
        if ($this->page->validated) {
            SGL::inherit($validatedInput, $output);
            ''  process the input, do workflow and add properties to output object
            $processedInput = $this->page->process($validatedInput, $output);
            $this->_displayPage($processedInput);
        } else {
            ''  otherwise, input validation failed, so input object 

            ''  contains page data for try-again
            $this->_displayPage($validatedInput);
        }
    }
?>

Forcing values for REQUEST variables

Let's suppose that we want to force a categoryID for article.php (equivalent of /article.php?frmCatID=5 ) We do it like this in /www/article.php :

<?php
''    initialise      
    require_once '../init.php';
    require_once SGL_CORE_DIR . '/Controller.php';
    require_once SGL_MOD_DIR . '/publisher/classes/ArticleViewMgr.php';
    error_log('```````#    NEW PAGE RUN - REG - ARTICLE BROWSER    ```````#');
    $process = & new SGL_Controller();
    $process->page = & new ArticleViewMgr();
    $request = new SGL_HTTP_Request();
    $request->set('frmCatID','5');               '' This is the trick
    $process->go(& $request);
?>

Raising an error

  • All raised errors are PEAR error objects.
  • Output is sent to logs and screen.
  • Admin is notified by email if error is greater than preset threshold.
  • Output to screen is suppressed if 'production' key in config is set to 'true'.
  • Errors can be logged to a variety of targets thanks to PEAR::Log, use the Configuation screen to choose between file or DB logging.
  • Format is SGL::raiseError(String $errorMsg, Constant SGL_ERROR_CONST [, Constant PEAR_ERROR_DIE ]).
<?php
    SGL::raiseError('There was a problem inserting the record', SGL_ERROR_NOAFFECTEDROWS);
?>

  • Example of available error constants:
<?php
    define('SGL_ERROR_DBFAILURE',           -111);
    define('SGL_ERROR_DBTRANSACTIONFAILURE',-112);
    define('SGL_ERROR_BANNEDUSER',          -113);
    define('SGL_ERROR_NOFILE',              -114);
    define('SGL_ERROR_INVALIDFILEPERMS',    -115);
    define('SGL_ERROR_INVALIDSESSION',      -116);
    define('SGL_ERROR_INVALIDPOST',         -117);
    define('SGL_ERROR_INVALIDTRANSLATION',  -118);
    define('SGL_ERROR_FILEUNWRITABLE',      -119);
?>

  • To raise a fatal error that will halt program execution, use the following format:
<?php
    SGL::raiseError('Cannot connect to DB, check your credentials, exiting ...',
        SGL_ERROR_DBFAILURE, PEAR_ERROR_DIE);
?>

Logging messages

  • Use SGL::logMessage for detailed logging, or just PHP's error_log() for simple messages.
  • You can optionally pass file and line args (see below)
  • You can also set the priority (defaults to PEAR_LOG_INFO), so PEAR_LOG_DEBUG messages will only be logged when logging is in debug mode.
  • Format is SGL::logMessage(String $msg[, String $file, Int $line, Constant $priority]);.
  • Examples:
<?php
    SGL::logMessage('There was a problem with ' . __CLASS__ . '::' . __FUNCTION__, __FILE__, __LINE__, PEAR_LOG_DEBUG);
    SGL::logMessage('I went here', null, null, PEAR_LOG_DEBUG);
    SGL::logMessage('I went here too', PEAR_LOG_DEBUG); '' Same as above
    SGL::logMessage(null, PEAR_LOG_DEBUG); '' Just record date/level/class/function
    SGL::logMessage('Parameter foo is set to  ' . foo); '' Record at INFO level
?>

Note that CLASS and FUNCTION are actually automatically set and output by the logMessage function.

Sample output: Aug 26 14:50:47 Seagull [debug] sgl_controller->_displaypage:

See DebuggingAndErrorHandling? for more informations.

{{Include page="!/Continue" nomark="1" }}