Archive for the ‘Repository’ Category

MODx class v1.0

Thursday, June 19th, 2008

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.

Menu PHP block

Thursday, June 12th, 2008

This is a simple PHP block taken from the main Template Blocks website. A good feature is that is automatically defines the selected tab based on the URL the visitor is viewing:

<?php
 
$links = array( 'Home' => '/',
                'Features' => '/features.html',
                'Demo' => '/demo.html',
                'Download' => '/download.html',
                'Support' => '/support/',
                'About' => '/about.html'
               );
 
echo '  <ul id="toplinks">';
 
foreach( $links as $key => $value ){
  $selected = ($_SERVER['REQUEST_URI'] == $value) ? ' class="selected"' : '';
  echo '<li><a href="' . $value . '"' . $selected . '><span>' . $key . '</span></a></li>';
}
 
echo '  </ul>';
 
?>

Just replace the array at the top the with your URL list and menu titles. You might also need to make some modifications in the markup that is generated…

Section Menu v1.2

Sunday, June 8th, 2008

This is the “topbar” block available in the dummy content of the latest release. It contains links to all sections, placed in a hierarchy list:

<?php
 
$items = $db->select( '*', $config['Sections'], false, 'id');
if( is_array($items) ){
  $children = array();
  foreach( $items as $k => $v ){
    preg_match('/(.*)\((.*)\)/', $v['position'], $matches);
 
    if($matches[2] == 'x') {
      array_push($children[$matches[1]], $v['id']);
    } else {
      $children[$matches[1]][$matches[2]] = $v['id'];
    }
  }
  $output = createOrder( $items, $children );
}
echo '<div id="topbar">';
echo $output;
echo '</div>';
 
function createOrder( $items, $order, $parent=0, $path='' ){
 
  ksort($order[$parent]); 
 
  $listings .= '<ul>';
 
  $middle = '</span></a>';
  $close = '</li>';
  foreach( $order[$parent] as $k => $v ){
    foreach( $items as $l => $w ){
      // now we can create the listings
      if( $w['id'] == $v ){
        $open = '<li><a href="/'. $path . $w['slug'] .'.html"><span>';
        // check for children in this branch
        if (array_key_exists($w['id'], $order)) {
		  $new_path = $path . $w['slug'] . '/';
	      $children = createOrder( $items, $order, $w['id'], $new_path );
	    } else {
	      $children = '';
	    }
	  $listings .= $open . $w['title'] . $middle .  $children . $close;
      }
    }
  }
  $listings .= '</ul>';
 
  return $listings;
}
 
?>

Instructions:
- Copy/paste the following code and save it as a PHP block
- Edit the $open , $close variables if needed for a custom layout…

Change log:
- v1.2 : Better linking of children. Proper order restored.
- v1.1 : Updated the ‘parent’ field to the new ‘position’ field. Additional tweaks.
- v1.0 : Initial release

You are currently browsing the archives for the Repository category.