MODx class v1.0

This class connects a MODx installation with Template Blocks to draw useful information like the web page title or the bread crumb trail…

<?php
/**
 * TEMPLATE BLOCKS
 * Copyright (C) 2008 Makis Tracend
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * 
 * Class: MODx
 * 
*************************************************************************
	MODx Content Management System and PHP Application Framework 
	Managed and maintained by Raymond Irving, Ryan Thrash and the
	MODx community
*************************************************************************
* Known Limitations: 
* - This class only works on MODx setup with MySQL 
* - Only one MODx installation per website is supported 
* - You need to insert the database information to connect to MODx manually
*/
 
 
/* Variables (PLEASE CHANGE AS SUITED) */
$_TEMPLATE_VARS['modx_root'] = '/'; 
$_TEMPLATE_VARS['database_server'] = '';
$_TEMPLATE_VARS['database_name'] = '';
$_TEMPLATE_VARS['database_user'] = '';
$_TEMPLATE_VARS['database_password'] = '';
$_TEMPLATE_VARS['table_prefix'] = '';
 
 
class MODx{
  # Define class varriables
  private $dbhost;
  private $dbuser;
  private $dbpass;
  private $dbname;
  private $prefix;
  private $connection;
  private $settings;
 
  # Call constructor
  function __construct() {
    global $_TEMPLATE_VARS;
 
    $this->dbhost = $_TEMPLATE_VARS['database_server'];
    $this->dbname = $_TEMPLATE_VARS['database_name'];
    $this->dbuser = $_TEMPLATE_VARS['database_user'];
    $this->dbpass = $_TEMPLATE_VARS['database_password'];
    $this->prefix = $_TEMPLATE_VARS['table_prefix'];
 
	$this->openConnection();
  }
 
  function openConnection(){
 
    $this->connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die('Could not connect: ' . mysql_error());
	//Select the database connection for use
	mysql_select_db($this->dbname, $this->connection) or die('could not select database'); 
  }
 
  function getSettings(){
 
	//Run the query and store the results
	$data=mysql_query('SELECT * FROM ' . $this->prefix . 'system_settings')or die('query failed'. mysql_error());
 
	$numrows=mysql_num_rows($data);
 
	if( $numrows>0 ){
	  //Place all of the data into an array
	  for( $i=0; $i<$numrows; $i++ ){
	    $row = mysql_fetch_assoc($data);
	    $returndata[$row['setting_name']]=$row['setting_value'];
	  }
	  //Return the results
      $this->settings = $returndata;
    }
  }
 
  function findPage(){
    global $_TEMPLATE_VARS;
 
	// first parse the URL
	if ( $this->settings['friendly_urls'] ) {
	  // we have user friendly URLs
	  $urlparts = explode('/', $_SERVER['REQUEST_URI']);
	  $file = array_pop( $urlparts );
	  // for the case we are viewing a container of pages we need to go one level in
      if( $file=='' ){ $file = array_pop( $urlparts ); }
 
	  if ( $this->settings['use_alias_path'] ) {
	    $parent_alias = array_pop( $urlparts );
		if( $parent_alias ){
		  // now find the parent's id
	      $parent_data=mysql_query("SELECT id FROM " . $this->prefix . "site_content WHERE alias='" . $parent_alias . "'")or die('query failed'. mysql_error());
	      if( mysql_num_rows($parent_data) ){ $parent_array=mysql_fetch_assoc($parent_data); }
		  $parent_id = $parent_array['id'];
		}
	  }
	  $file_parts = array( $this->settings['friendly_url_prefix'], $this->settings['friendly_url_suffix'] );
	  if ( $this->settings['friendly_alias_urls'] ) {
		$page_alias = ( $file != '' ) ? str_replace( $file_parts, '', $file ) : 'index';
	  } else {
		$page_id = ( $file != '' ) ? str_replace( $file_parts, '', $file ) : '1';
	  }
	} else {
	  // we simple have an id variable in the URL let's parse that...
      import_request_variables("gp", "page_");
	  // a precaution so that the query doesn't return an error
	  if( !$page_id ){ $page_id = '1'; }
	}
 
	// compile the query 
	$query = "SELECT * FROM " . $this->prefix . "site_content WHERE ";
	if($page_alias){ $query .= "alias='" . $page_alias . "'"; }
	if($page_id){ $query .= "id=" . $page_id; }
	if($parent_id){ $query .= " AND parent=" . $parent_id; }
 
	$data=mysql_query( $query )or die('query failed'. mysql_error());
 
	if( mysql_num_rows($data) ){
	  // We only need one record so we won't loop through the $numrows
	  $returndata=mysql_fetch_assoc($data);
	  //Return the results
	  return $returndata;
	}
  }	
}
 
$modx = new MODx();
$modx->getSettings();
$modx_page = $modx->findPage();
 
$_TEMPLATE_VARS['page-title'] = ( $modx_page['longtitle'] ) ? $modx_page['longtitle'] : $modx_page['pagetitle'];
$page['title'] = $_TEMPLATE_VARS['page-title'];
 
unset($modx);
unset($modx_page);
 
?>

Instructions:
- Copy the above code and paste it in a text editor.
- Edit the top $_TEMPLATE_VARS variables (6 total). The information refers to your MODx installation.
- Save the text file as “MODx.php” and place it in the classes folder of your Template Blocks (default: ‘template/classes’)
- To use it, select it for a section, in the available classes list (edit screen) or refer to it from a PHP block with an include command.

Known Limitations:
- This class only works on MODx setup with MySQL.
- Only one MODx installation per website is supported.
- You need to insert the database information to connect to MODx manually.

Changelog:
v1.0 Initial Release - Web page title automatically embedded in the functionality.

Leave a Reply