Macros code

This code belongs to the macros plugin. You should copy and paste this code into a file called NP_Macros.php. Upload this file to you nucleus/plugins directory and install the plugin. Don't forget to als upload a copy of the admin page which is located overher

<?php
 
/**
  * Plugin for Nucleus CMS (http://plugins.nucleuscms.org/)
  * Copyright (C) 2003 The Nucleus Plugins Project
  *
  * 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.
  *
  * see license.txt for the full license
  */
 
/**
 * Usage:
 * 		
 *
 * Versions:
 *
 *  TODO
 *		- documentation
 *		- etc
 */
class NP_Macros extends NucleusPlugin {
 
	function NP_Macros() {
		$this->table_macros = sql_table('plug_macros');
	}
 
	function getName() 		{ return 'Macros'; }
	function getAuthor()  	{ return 'Anand'; }
	function getURL()  		{ return 'http://tamizhan.com/'; }
	function getVersion() 	{ return '0.3'; }
	function getDescription()
	{
		return 'Macro Stuff!';
	}
 
	function supportsFeature($what) {
		switch($what)
		{
			case 'SqlTablePrefix':
				return 1;
			default:
				return 0;
		}
	}
 
 
	function install() {
 
		// create the table that will keep track of notifications
		$query =  'CREATE TABLE '. $this->table_macros. '(';
		$query .= ' id int(11) NOT NULL auto_increment,';
		$query .= ' pattern_string varchar(80) default NULL,';		
  		$query .= ' replace_string varchar(80) default NULL,';
  		$query .= ' pattern_type int(1) default 0,';
 		$query .= ' PRIMARY KEY  (id)';
		$query .= ') TYPE=MyISAM;';
		sql_query($query);
 
	}
 
	function event_QuickMenu(&$data) {			
		array_push(
			$data['options'], 
			array(
				'title' => 'Macros',
				'url' => $this->getAdminURL(),
				'tooltip' => 'Use Macros to enhance your posts.'
			      )
			);
	}
 
 
	function unInstall() {
		sql_query('DROP TABLE ' .$this->table_macros);
	}
 
	function getTableList() {
		return array($this->table_macros);
	}
 
	function getEventList() {
		return array('PreItem', 'QuickMenu');
	}
 
	function init() {
	}
 
	function hasAdminArea() {
		return 1;
	}
 
 
	/*
	 * Adds an entry to the 'Quick Menu' on the Nucleus administration pages.
	 * The entry will link to the commentcontrol admin page
	 */
 
	function doMacros(&$data) { 
 
		$query = 'SELECT * FROM '.$this->table_macros; 
		$macros = sql_query($query); 
		while ($macro = mysql_fetch_object($macros)) {
 
		      if ($macro->pattern_type == 0)
			$data = ereg_replace($macro->pattern_string,$macro->replace_string,$data); 
		      else
		      	$data = preg_replace($macro->pattern_string,$macro->replace_string,$data); 
		}
	} 
 
	function event_PreItem(&$data) {
 
		$this->doMacros($data['item']->title); 
		$this->doMacros($data['item']->body); 
		$this->doMacros($data['item']->more); 
 
	}
 
	function doAction($actionType)
	{
		global $CONF, $member;
 
		if (!$member->isLoggedIn() || !$member->isAdmin())
			return 'Sorry. not allowed';
 
		if ($actionType == 'add') {
 
			$pattern_string = postVar('pattern_string');
			$replace_string = postVar('replace_string');
			$pattern_type   = postVar('pattern_type');
 
			$query = 'INSERT INTO ' .$this->table_macros. ' (pattern_string,replace_string,pattern_type) VALUES ("'.addslashes($pattern_string).'","'.addslashes($replace_string).'","'.intval($pattern_type).'")';
			$res = sql_query($query);
 
			$url = $CONF['AdminURL'] . 'plugins/macros/';
			Header('Location: ' . $url);			
 
		}
		else if ($actionType == 'edit')
		{
 
			$id = requestVar('id');
 
			// get data from pending table
 
			$url = $CONF['AdminURL'] . 'plugins/macros/index.php?action=edit&id='.$id;
			Header('Location: ' . $url);			
 
 
		}		
		else if ($actionType == 'update')
		{
 
			$id = requestVar('id');
			$pattern_string = postVar('pattern_string');
			$replace_string = postVar('replace_string');
			$pattern_type   = postVar('pattern_type');
 
			// get data from pending table
 
			$query = 'UPDATE '.$this->table_macros.' SET pattern_string="'.addslashes($pattern_string).'",replace_string="'.addslashes($replace_string).'",pattern_type="'.intval($pattern_type).'" WHERE id=' . intval($id); 
			sql_query($query);			
 
			$url = $CONF['AdminURL'] . 'plugins/macros/';
			Header('Location: ' . $url);			
 
 
		}		
 
		else if ($actionType == 'delete')
		{
			$this->what = 'delete';
 
			$id = requestVar('id');
 
			$query = 'DELETE FROM ' . $this->table_macros . ' WHERE id=' . intval($id);
			sql_query($query);		
 
			$url = $CONF['AdminURL'] . 'plugins/macros/';
			Header('Location: ' . $url);			
 
		}
	}
 
        function _getMacros() 
	{
    		$aResult = array();
 
	    	$query = 'SELECT id, pattern_string, replace_string, pattern_type FROM ' . $this->table_macros;
		$res = sql_query($query);
 
		while ($o = mysql_fetch_object($res))
		{
			array_push($aResult, array(
				'pattern_string' 	=> $o->pattern_string,
				'replace_string' 	=> $o->replace_string,
				'pattern_type' 		=> intVal($o->pattern_type),
				'id'			=> intval($o->id)
			));
		}
 
		return $aResult;
 
	    }
 
	  function _getMacro($id) 
	  {
 
	    	$query = 'SELECT pattern_string, replace_string, pattern_type FROM ' . $this->table_macros .' WHERE id='.$id;
		$res = sql_query($query);
 
		while ($o = mysql_fetch_object($res))		
		{			
			$aResult['pattern_string'] = $o->pattern_string;
			$aResult['replace_string'] = $o->replace_string;			
			$aResult['pattern_type'] = intval($o->pattern_type);
		}
 
		return $aResult;
 
	    }
 
}
?>
macroscode.txt · Last modified: 2006/07/05 13:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki