Joomla Register Event plugin

From LeonWiki!

Jump to: navigation, search

Contents

Joomla Register Event

Objective
  • Allow a joomla forms user to embed a keyword phrase into their event descriptions and have that phrase replaced by a link to the appropriate form, or in the event the class/event is full or has expired, then replace the phrase with the text for class expired or class full
Strategy
  • Process the appropriate event trigger
  • Have the plugin code replace the keyword phrase with the menu link
  • If the class has expired have the plugin replace the keyword phrase with the class expired text
  • If the class is full (by testing to see if the event contact info field contains text = Full (case-insensitive)) then the plugin should replace the keyword phrase with the class full text
  • The keyword phrase is in the form: {registerevent menu_link=*<link>* class_full_text=*<text>* class_expired_text=*<text>*}
  • Plugin parameters supply the CSS attributes to style the expired and full text
  • The menu link uses the editor's CSS button to style the link
Implementation
First the xml file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install PUBLIC "-//Joomla! 1.5//DTD plugin 1.0//EN" "http://www.joomla.org/xml/dtd/1.5/plugin-install.dtd">

<install version="1.5" type="plugin" group="registerevent" method="upgrade">
	<name>Link Enhancer plugin</name>
	<author>Ron Siewert</author>
	<creationDate>2010-05-20</creationDate>
	<copyright>2010 Alchemy Software, Inc.</copyright>
	<license>http://www.gnu.org/copyleft/gpl.html GNU/GPL</license>
	<authorEmail>ron.siewert@alchemysoft.com</authorEmail>
	<authorUrl>www.alchemysoft.com</authorUrl>
	<version>0.1</version>
	<description>Replaces a token with a user-defined string as a hyperlink with event parameters to a registration form</description>

	<files>
		<filename plugin="linkenhancer">linkenhancer.php</filename>
	</files>

	<params>
	  <param name="font-size" type="text" size="18" default="12pt" label="Link Size" description="Font size for register link" />
	  <param name="font-color" type="text" size="18" default="blue" label="Link Color" description="Font Color for register link" />
	  <param name="font-style" type="text" size="18" default="normal" label="Link Style" description="Font Style for register link, can be italic or bold" />
	  <param name="font-weight" type="text" size="18" default="bold" label="Link Weight" description="Font Weight for register link" />
	  <param name="font-family" type="text" size="18" default="verdana" label="Link Family" description="Font Family for register link" />
	</params>
</install>

Next the php file
<?php
/*
 **********************************************
 LinkEnhancer Plugin
 Copyright (c) 2010 www.alchemysoft.com
 **********************************************
 LinkEnhancer replaces a phrase embedded in a
 JcalPro event description with a link to an 
 input form of the user's choosing. Passes
 event name, start date, and end date as
 url parameters for the form to process.
 **********************************************

 * LinkEnhancer Plugin
 *
 * $Id: linkenhancer.php 1017 2010-05-25 00:49:34Z rsie $:
 * 
 *
 * Plugin for displaying a registration link
 * in a jcalpro event description.
 * 
 */

defined( '_JEXEC' ) or die( 'Restricted Access' );

define('LB','<br/>');

jimport( 'joomla.event.plugin');
jimport ('joomla.plugin.plugin');

class plgRegistereventLinkenhancer extends JPlugin
{
  //Contructor
  function plgRegistereventLinkenhancer(&$subject,$config)
  {
    parent::__construct($subject,$config);
  }
  
  function onJCalEventDetail($event)
  {
    file_put_contents("log.txt","");
    
    //file_put_contents("log.txt",$event->description,FILE_APPEND);
    //file_put_contents("log.txt","Start Date = ".$event->start_date,FILE_APPEND);
    $matches = array();
    
    //get the params
    $fontcolor  = $this->params->get('font-color');
    $fontweight = $this->params->get('font-weight');
    $fontstyle  = $this->params->get('font-style');
    $fontsize   = $this->params->get('font-size');
    $fontfamily = $this->params->get('font-family','verdana');
    
    //start and end times are stored in UTC in the database, so we have to get the offset to display them correctly.    
    $this_tz_str = date_default_timezone_get();
    $this_tz     = new DateTimeZone($this_tz_str);
    $now         = new DateTime("now", $this_tz);
    $offset      = $this_tz->getOffset($now);

    $local_start_date = strtotime($event->start_date) + $offset;
    $local_end_date = strtotime($event->end_date) + $offset;
    $local_start_date_printable = strftime("%B %d %G %I:%M %p",$local_start_date);
    $local_end_date_printable = strftime("%B %d %G %I:%M %p",$local_end_date);
    
    //echo "start date = ".strftime("%B %e %G %I %M %p",$local_start_date).LB;
    //echo "end date   = ".strftime("%B %e %G %I %M %p",$local_end_date).LB;
    
    // regexp to catch plugin requests
    $regExp = "#{registerevent\s*(.*)}#Us";
    
    // string to replace end of url to contain parameters for the form to process
    $replace = "&event_name=".$event->title."&event_start_date=".$local_start_date_printable
      ."&event_end_date=".$local_end_date_printable."\">";
    
    //array to store replacement result
    $html = array();
    
    if (preg_match_all($regExp, $event->description, $matches, PREG_SET_ORDER) > 0) {
      foreach( $matches as $match) {
	
	// extract parameters passed as attributes
	$attribs = $this->getPluginAttributes($match[1]);
	
	$linkstyling = "<a style=\"color:".$fontcolor.";font-size:".$fontsize.";font-weight:".$fontweight.";font-style:".$fontstyle."\" ";
	$textstyling = "<p align='center' style='font-family:".$fontfamily.";font-size:".$fontsize.";color:".$fontcolor.";font-weight:".$fontweight.";font-style:".$fontstyle."'>";

	$status = $this->canRegister($event);
	if($status['canRegister']) {
	  //***Note: the following replace allows the styling that the user applied via the editor to be displayed
	  $html = str_replace("\">",$replace,$attribs['menu_link']);
	} else {
	  if($status['isExpired']) {
	    $thestyle = $textstyling . $attribs['event_expired_text'] . "</p>";
	    $html = str_replace($attribs['event_expired_text'],$thestyle,$attribs['event_expired_text']);
	  } else {
	    $thestyle = $textstyling . $attribs['event_full_text'] . "</p>";
	    $html = str_replace($attribs['event_full_text'],$thestyle,$attribs['event_full_text']);
	  }
	}
      }
      $event->description = str_replace($match[0],$html,$event->description);
    } else {
      file_put_contents("log.txt","Not matching");
    }
    return true;
  }
  
  /**
   * Extract parameters from content text {registerevent} entry
   *
   * @param $params the raw parameters string, as extracted by regexp
   * @return array individual parameters as $key => $value
   */
  function getPluginAttributes($params) {
    
    // attributes allowed as param of plugin call
    $paramList = array( 'menu_link', 'event_expired_text', 'event_full_text');
    
    // output array
    $attribs = array();
    
    //this regex grabs just the text between two '*' when you supply the key in the form: xxxx=
    foreach($paramList as $keyword) { 
      $mykey = '#('.$keyword.')=\*(((?:\\.|[^\\*])*))\*#';
      if (preg_match_all($mykey, $params, $matches, PREG_SET_ORDER) > 0) 
	{
	  foreach($matches as $match)
	    $attribs[$keyword] = $match[2];
	}
    }
    if(count($attribs) != count($paramList))
      echo "<font color='red'>Something wrong with register link syntax</font>";
    return $attribs;
  }
  
  /***
   * We first get the current date - separately - in GMT format.
   * Then we pass the values on to the mktime() function. This function
   * tries to work out if the date given is in DST or not.
   * Since jcal stores dates in gmt we now can compare the timestamps of
   * current date and event start date.
   ***/
  function canRegister($event) {
    $hour   = gmdate("H");
    $minute = gmdate("i");
    $second = gmdate("s");
    $day    = gmdate("d");
    $month  = gmdate("m");
    $year   = gmdate("Y");
    
    $localtime_in_gmt = mktime($hour,$minute,$second,$month, $day, $year, -1); 
    
    $now = $localtime_in_gmt;
    $evt_start_date = strtotime($event->start_date);
    
    $class_full = false;
    $canRegister = true;
    $class_expired = false;
    
    if($now > $evt_start_date ) {
      $canRegister = false;
      $class_expired = true;
    }
    if(strcasecmp($event->contact,"FULL")==0) {
      //echo "Can't register for past event OR if it's full";
      $class_full = true;
      $canRegister = false;
    }
    
    return array('canRegister'=>$canRegister, 'isFull'=>$class_full, 'isExpired'=>$class_expired);
  }
}


Gotchas
  • After defining parameters and their defaults and installing the plugin, the coder must save the defaults otherwise there are no parameter records in the database.
Personal tools
Alchemy Software, Inc.
Alchemy Software Website