aboutsummaryrefslogtreecommitdiff
path: root/h-source/admin/Library/BoxParser.php
blob: 90d2426fab5b82ac517e66ed6eb0acf9240f826f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

// All EasyGiant code 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 3 of the License, or (at your option) any later version.
// See COPYRIGHT.txt and LICENSE.txt.

if (!defined('EG')) die('Direct access not allowed!');

//class to parse an XML text in order to create the modules corresponding to the elements of the XML text.
//the <type>module name</type> tag defines the name of the object that has to be instantiate and saved in the
//$modules property (that is an array referencing different module objects) array(moduleObj1,moduleObj2, ...)
//if the module class corresponding ot the <type>module name</type> tag does not exists, than no module is created and the next <type>module name</type> is checked
class BoxParser {

	public $modules = array(); //array referencing different module classes --> array(moduleObj1,moduleObj2, ...) See files inside the Application/Modules folder
	
	//$simpleXMLText: it has to be an XML text
	//$type; it can be string or file.
	public function __construct($simpleXMLText, $type = 'string')
	{
		if ($type === 'string')
		{
			if (@simplexml_load_string($simpleXMLText))
			{
				$simpleXmlObj = simplexml_load_string($simpleXMLText);
				$this->populate($simpleXmlObj);
			}
		}
		else if ($type === 'file')
		{
			if (@simplexml_load_file($simpleXMLText))
			{
				$simpleXmlObj = simplexml_load_file($simpleXMLText);
				$this->populate($simpleXmlObj);
			}	
		}
	}

	//inistantiate the module objects and save them in the $this->modules property array
	private function populate($simpleXmlObj)
	{
		foreach ($simpleXmlObj as $mod)
		{
			$className = 'Mod'.ucwords((string)$mod->type);
			if (class_exists($className))
			{
				if (file_exists(ROOT . DS . APPLICATION_PATH . DS . 'Modules' . DS . $className . '.php'))
				{
					$newModule = new $className($mod);
					if ($newModule instanceof ModAbstract)
					{
						$this->modules[] = $newModule;
					}
				}
			}
		}
	}

	//create the HTML of the modules
	public function render()
	{
		$HTML = null;
		foreach ($this->modules as $module)
		{
			$HTML .= $module->render();
		}
		return $HTML;
	}

}