Forum core source dump (as of July 31)

lib/Modules.class.php

Note: I removed a lot of the regions I had here from the previous version.

/**
* Modules class.
* Extending the base forum functionality through pluggable modules.
*
* @author Eksith Rodrigo
* @version 0.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @access public
*/

final class Modules
{
	static private $instance;			// Singleton instance for this class
	protected $resources = array();		// Loaded modules
	protected $regions = array();		// Output render regions
	
	/**
	* Modules constructor. Initiates all given modules
	*
	* @param array $props Properties array global
	* @param object $db Database object
	* @param array $modules List of modules to be loaded
	*/
	private function __construct(&$p, &$db, $modules)
	{
		// Start the regions
		$this->initRegions();
		
		// Load the modules into the $resources array
		foreach($modules as $resource)
			$this->resources[$resource] = new $resource($p, $db);
	}
	
	/**
	* Singleton instance.
	*
	* @param array $p The presets in the core passed by reference.
	* @param object $d The database object (MySQL, SQLite et al) passed by reference.
	* @return object Core Singleton instance
	*/
	static function getInstance(&$props, &$db, $modules)
	{
		if(!isset(self::$instance))
			self::$instance = new Modules($props, $db, $modules);
		
		return self::$instance;
	}
	
	
	/**
	* Trigger module events and render the output for a specified region.
	* When called within the templates, this function iterates through all loaded modules 
	* and calls the appropriate event function.
	*
	* @param string $method Event name ("BeforeHeaderStart", "AfterHeaderEnd" etc...)
	* @param mixed $args Will depend on where the method is called. Generally, will be an array, string or integer.
	* @return string $regions[key] Output HTML from loaded modules for the specified event.
	*/
	public function __trigger($method, &$args, $html=true)
	{
		// onHeader, onBeforeBody onAfterBody etc... The same convention as the __exec function
		$call = "on" . $method;
		
		// Iterate through each module resource
		foreach($this->resources as $key => $module)
		{
			// If an "on" event function exists for this region...
			if(method_exists($module, $call))
			{
				// If we're rendering to the template
				if($html)
					// Call the function and load the module's output into the region to be injected
					$this->regions[$method] .= call_user_func(array($module, $call), $args);
				else
					// Simply call the event with no output
					call_user_func(array($module, $call), $args);
			}
		}
		
		return $this->regions[$method];
	}
	
	/**
	* Erases a specific render region (used for loops where fresh output is needed each time)
	*
	* @param string $method Region identifier
	*/
	public function __clean($method)
	{
		if(isset($this->regions[$method]))
			$this->regions[$method] = '';
	}
	
	/**
	* Template render injection regions.
	* Initiates $regions array into accessible empty keys. Note: Template designers MUST use these keys!
	*/
	private function initRegions()
	{
		
		/**
		* Generic regions
		*/
		$this->regions["Head"] = '';						// On head tag
		
		$this->regions["BeforeBody"] = '';					// After body tag
		$this->regions["Body"] = '';						// On body
		$this->regions["AfterBody"] = '';					// Before body end tag
		
		$this->regions["BeforeHeaderStart"] = '';			// Before header start
		$this->regions["BeforeHeader"] = '';				// After header start
		$this->regions["Header"] = '';						// On header
		$this->regions["AfterHeader"] = '';					// Before header end
		$this->regions["AfterHeaderEnd"] = '';				// After header end
		
		$this->regions["BeforeContentStart"] = '';			// Before content start
		$this->regions["BeforeContent"] = '';				// After content start
		$this->regions["Content"] = '';						// On content
		$this->regions["AfterContent"] = '';				// Before content end
		$this->regions["AfterContentEnd"] = '';				// After content end
		
		$this->regions["BeforeWelcomeStart"] = '';			// Before welcome banner start
		$this->regions["BeforeWelcome"] = '';				// After welcome banner start
		$this->regions["Welcome"] = '';						// On welcome
		$this->regions["AfterWelcome"] = '';				// Before welcome banner end
		$this->regions["AfterWelcomeEnd"] = '';				// After welcome banner end
		
		
		/**
		* List regions
		*/
		$this->regions["BeforeForumsStart"] = '';			// Before forums list start
		$this->regions["BeforeForums"] = '';				// Before first list item start
		$this->regions["Forums"] = '';						// On forum list as item
		$this->regions["AfterForums"] = '';					// After last forum list item end
		$this->regions["AfterForumsEnd"] = '';				// After forums list end
		
		$this->regions["BeforeTopicsStart"] = '';			// Before topics list start
		$this->regions["BeforeTopics"] = '';				// Before first topics list item start
		$this->regions["Topics"] = '';						// On topics list as item
		$this->regions["AfterTopics"] = '';					// After last topic list item end
		$this->regions["AfterTopicsEnd"] = '';				// After topics list end
		
		$this->regions["BeforeRecentStart"] = '';			// Before recent topics list start
		$this->regions["BeforeRecent"] = '';				// Before first topic list item start
		$this->regions["Recent"] = '';						// On topics list as item
		$this->regions["AfterRecent"] = '';					// Before recent topics list end
		$this->regions["AfterRecentEnd"] = '';				// After recent topics list end
		
		$this->regions["BeforeRepliesStart"] = '';			// Before replies start 
		$this->regions["BeforeReplies"] = '';				// After replies start 
		$this->regions["Replies"] = '';						// On replies 
		$this->regions["AfterReplies"] = '';				// Before replies end 
		$this->regions["AfterRepliesEnd"] = '';				// After replies end 
		
		$this->regions["BeforePagerStart"] = '';			// Before pager start 
		$this->regions["BeforePager"] = '';					// After pager start 
		$this->regions["Pager"] = '';						// On pager 
		$this->regions["AfterPager"] = '';					// Before pager end 
		$this->regions["AfterPagerEnd"] = '';				// After pager end 
		
		
		/**
		* Form data
		*/
		$this->regions["BeforeTopicFormStart"] = '';		// Before new topic form start
		$this->regions["BeforeTopicForm"] = '';				// After new topic form start
		$this->regions["TopicForm"] = '';					// On new topic form
		$this->regions["AfterTopicForm"] = '';				// Before new topic form end
		$this->regions["AfterTopicFormEnd"] = '';			// After new topic form end
		
		$this->regions["BeforeReplyFormStart"] = '';		// Before new reply form start
		$this->regions["BeforeReplyForm"] = '';				// After new reply form start
		$this->regions["ReplyForm"] = '';					// On new reply form
		$this->regions["AfterReplyForm"] = '';				// Before new reply form end
		$this->regions["AfterReplyFormEnd"] = '';			// After new reply form end
		
		$this->regions["BeforeLoginFormStart"] = '';		// Before login form start
		$this->regions["BeforeLoginForm"] = '';				// After login form start
		$this->regions["LoginForm"] = '';					// On login form
		$this->regions["AfterLoginForm"] = '';				// Before login form end
		$this->regions["AfterLoginFormEnd"] = '';			// After login form end
		
		$this->regions["BeforeReigsterFormStart"] = '';		// Before register form start
		$this->regions["BeforeReigsterForm"] = '';			// After register form start
		$this->regions["RegisterForm"] = '';				// On register form
		$this->regions["AfterRegisterForm"] = '';			// Before register form end
		$this->regions["AfterRegisterFormEnd"] = '';		// After register form end
		
		$this->regions["BeforeSearchStart"] = '';			// Before search form start
		$this->regions["BeforeSearch"] = '';				// After search form start
		$this->regions["Search"] = '';						// On search form
		$this->regions["AfterSearch"] = '';					// Before search form end
		$this->regions["AfterSearchEnd"] = '';				// After search form end
		
		
		/**
		* Individual form fields
		*/
		$this->regions["ForumSelectFieldStart"] = '';		// Forum select dropdown field start
		$this->regions["ForumSelectField"] = '';			// Select dropdown field items
		$this->regions["ForumSelectFieldEnd"] = '';			// Forum select dropdown field end
		
		$this->regions["SearchFieldStart"] = '';			// Before search input field start
		$this->regions["SearchFieldEnd"] = '';				// After search input field end
		
		$this->regions["UsernameFieldStart"] = '';			// Before login username input field start
		$this->regions["UsernameFieldEnd"] = '';			// After login username input field end
		
		$this->regions["PasswordFieldStart"] = '';			// Before login password input field start
		$this->regions["PasswordFieldEnd"] = '';			// After login password input field end
		
		$this->regions["TopicTitleFieldStart"] = '';		// Before topic title input field start
		$this->regions["TopicTitleFieldEnd"] = '';			// After topic title input field end
		
		$this->regions["TopicContentFieldStart"] = '';		// Before topic content textarea field start
		$this->regions["TopicContentFieldEnd"] = '';		// After topic content textarea field end
		
		$this->regions["TopicForumFieldStart"] = '';		// Before topic forum select field start
		$this->regions["TopicForumFieldEnd"] = '';			// After topic forum select field end
		
		$this->regions["ReplyContentFieldStart"] = '';		// Before reply content textarea start
		$this->regions["ReplyContentFieldEnd"] = '';		// After reply content textarea end
		
		/**
		* These fields will only be present if the user is anonymous.
		* Registered users will have the username called from their login information.
		*/
		$this->regions["TopicNameFieldStart"] = '';			// Before topic author name input field start
		$this->regions["TopicNameFieldEnd"] = '';			// After topic author name input field end
		
		$this->regions["TopicEmailFieldStart"] = '';		// Before topic author email input field start
		$this->regions["TopicEmailFieldEnd"] = '';			// After topic author email input field end
		
		$this->regions["ReplyNameFieldStart"] = '';			// Before reply author name input field start	
		$this->regions["ReplyNameFieldEnd"] = '';			// After reply author name input field end
		
		$this->regions["ReplyNameFieldStart"] = '';			// Before reply author email input field start	
		$this->regions["ReplyNameFieldEnd"] = '';			// After reply author email input field end
		
		
		/**
		* Navigation regions and items
		*/
		$this->regions["BeforeBreadcrumbStart"] = '';		// Before breadcrumb links start
		$this->regions["BeforeBreadcrumb"] = '';			// After breadcrumb links start
		$this->regions["Breadcrumb"] = '';					// On breadcrumb 
		$this->regions["AfterBreadcrumb"] = '';				// Before breadcrumb end
		$this->regions["AfterBreadcrumbEnd"] = '';			// After breadcrumb end
		
		$this->regions["BeforeNavigationStart"] = '';		// Before navigation list start
		$this->regions["BeforeNavigation"] = '';			// After navigation list start
		$this->regions["Navigation"] = '';					// On navigation list as list item
		$this->regions["AfterNavigation"] = '';				// After last navigation list item
		$this->regions["AfterNavigationEnd"] = '';			// After navigation list end
		
		
		/**
		* Ancillary regions
		* Sidebar, footer etc...
		*/
		$this->regions["BeforeSidebarStart"] = '';			// Before sidebar start
		$this->regions["BeforeSidebar"] = '';				// After sidebar start
		$this->regions["Sidebar"] = '';						// On sidebar 
		$this->regions["AfterSidebar"] = '';				// Before sidebar end
		$this->regions["AfterSidebarEnd"] = '';				// After sidebar end
		
		$this->regions["BeforeFooterStart"] = '';			// Before footer start
		$this->regions["BeforeFooter"] = '';				// After footer start
		$this->regions["Footer"] = '';						// On footer 
		$this->regions["AfterFooter"] = '';					// Before footer end
		$this->regions["AfterFooterEnd"] = '';				// After footer end
	}
}
Advertisements

6 thoughts on “Forum core source dump (as of July 31)

  1. Pingback: I have developer ADHD « This page intentionally left ugly

  2. This was a very interesting read for me!
    Although I have a question,
    I am a beginning php programmer, currently developing drupal modules on my internship, but looking to develop some sort of forum script/cms/framework/… . I’m not planning on actually making it public, it’s just a personal project to teach myself some things. So, my question…, I saw in your main.php you do:
    Core::getInstance and Modules::getInstance, is it the same as doing:
    $core = new Core();
    $core->getInstance(…); ? and did you have to define ‘Core’ somewhere or does php automatically recognise it as a class and searches for it (with the autoload function you wrote)?
    thanks in advance!

    You can contact me on my email if you want

    • Hi Jelle

      Yes, the basic idea is to let the autoload function load the class dynamically so you wouldn’t need to declare it elsewhere.

      Technically $core = new Core(); wouldn’t work in this case becuse the __construct function in that class is set to private.

      So you would always have to use the getInstance method when you want to create a new instance unless you’re calling new Core() inside the Core class itself.

  3. Pingback: (Snippet) Blocking IPs from a list file with PHP « This page intentionally left ugly

  4. Pingback: PHP Plugin/Module system | This page intentionally left ugly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s