[345] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * @name Plenty Parser |
---|
| 5 | * @copyright 2011 |
---|
| 6 | * @author Dwayne Charrington |
---|
| 7 | * @license http://philsturgeon.co.uk/code/dbad-license |
---|
| 8 | * @version 1.0 |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | class Pp extends CI_Driver_Library { |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Codeigniter instance |
---|
| 15 | * |
---|
| 16 | * @var mixed |
---|
| 17 | */ |
---|
| 18 | protected $ci; |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * The driver to use for rendering |
---|
| 22 | * |
---|
| 23 | * @var mixed |
---|
| 24 | */ |
---|
| 25 | protected $_current_driver; |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Current theme in use |
---|
| 29 | * |
---|
| 30 | * @var mixed |
---|
| 31 | */ |
---|
| 32 | protected $_current_theme; |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Get default theme (if any) |
---|
| 36 | * |
---|
| 37 | * @var mixed |
---|
| 38 | */ |
---|
| 39 | protected $_default_theme; |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Theme locations |
---|
| 43 | * |
---|
| 44 | * @var mixed |
---|
| 45 | */ |
---|
| 46 | protected $_theme_locations = array(); |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Are theming capabilities enabled or disabled? |
---|
| 50 | * |
---|
| 51 | * @var mixed |
---|
| 52 | */ |
---|
| 53 | protected $_theme_enabled; |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Valid drivers for rendering views |
---|
| 57 | * |
---|
| 58 | * @var mixed |
---|
| 59 | */ |
---|
| 60 | protected $valid_drivers = array( |
---|
| 61 | 'pp_smarty', |
---|
| 62 | 'pp_twig', |
---|
| 63 | ); |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Constructor |
---|
| 67 | * |
---|
| 68 | */ |
---|
| 69 | public function __construct() |
---|
| 70 | { |
---|
| 71 | $this->ci = get_instance(); |
---|
| 72 | $this->ci->config->load('plentyparser'); |
---|
| 73 | |
---|
| 74 | // Get our default driver |
---|
| 75 | $this->_current_driver = config_item('parser.driver'); |
---|
| 76 | |
---|
| 77 | // Get default theme (if one set) |
---|
| 78 | $this->_default_theme = config_item('parser.theme.default'); |
---|
| 79 | |
---|
| 80 | // Get whether or not themes are enabled or disabled |
---|
| 81 | $this->_theme_enabled = config_item('parser.theme.enabled'); |
---|
| 82 | |
---|
| 83 | // Call the init function |
---|
| 84 | $this->_init(); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Init |
---|
| 89 | * Populates variables and other things |
---|
| 90 | * @returns void |
---|
| 91 | */ |
---|
| 92 | private function _init() |
---|
| 93 | { |
---|
| 94 | // Is theming capability enabled |
---|
| 95 | if ($this->_theme_enabled) |
---|
| 96 | { |
---|
| 97 | // Get all paths defined in the config file and add them to our array |
---|
| 98 | foreach (config_item('parser.theme.locations') AS $path) |
---|
| 99 | { |
---|
| 100 | // If path isn't already in our array of paths |
---|
| 101 | if ( !array_key_exists($path, $this->_theme_locations) ) |
---|
| 102 | { |
---|
| 103 | $this->_theme_locations[$path]; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // If no theme is set and we have a default, use that |
---|
| 108 | if ( empty($this->_current_theme) AND !empty($this->_default_theme) ) |
---|
| 109 | { |
---|
| 110 | $this->_current_theme = $this->_default_theme; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * Call |
---|
| 118 | * able to call native Smarty methods |
---|
| 119 | * @returns void |
---|
| 120 | */ |
---|
| 121 | public function __call($method, $params=array()) |
---|
| 122 | { |
---|
| 123 | |
---|
| 124 | if(!method_exists($this, $method)) |
---|
| 125 | { |
---|
| 126 | call_user_func_array(array($this->{$this->_current_driver}, $method), $params); |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Set Driver |
---|
| 133 | * Sets which driver to use for view rendering |
---|
| 134 | * |
---|
| 135 | * @param mixed $driver |
---|
| 136 | * @returns void |
---|
| 137 | */ |
---|
| 138 | public function set_driver($driver) |
---|
| 139 | { |
---|
| 140 | $driver = trim($driver); |
---|
| 141 | |
---|
| 142 | if ($driver == $this->_current_driver) |
---|
| 143 | { |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | else |
---|
| 147 | { |
---|
| 148 | $this->_current_driver = $driver; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * Set Theme |
---|
| 154 | * Set the theme name we're using |
---|
| 155 | * |
---|
| 156 | * @param mixed $theme |
---|
| 157 | * @returns void |
---|
| 158 | */ |
---|
| 159 | public function set_theme($theme) |
---|
| 160 | { |
---|
| 161 | $theme = trim($theme); |
---|
| 162 | |
---|
| 163 | if ($theme == $this->_current_theme) |
---|
| 164 | { |
---|
| 165 | return true; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | $this->_current_theme = $theme; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Add Theme Location |
---|
| 175 | * Add a new theme location |
---|
| 176 | * |
---|
| 177 | * @param mixed $location |
---|
| 178 | * @returns void |
---|
| 179 | */ |
---|
| 180 | public function add_theme_location($location) |
---|
| 181 | { |
---|
| 182 | // If path isn't already in our array of paths |
---|
| 183 | if ( !array_key_exists($location, $this->_theme_locations) ) |
---|
| 184 | { |
---|
| 185 | $this->_theme_locations[$location]; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | return true; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Assign Var |
---|
| 193 | * Set a variable name and value for a template |
---|
| 194 | * |
---|
| 195 | * @param mixed $name |
---|
| 196 | * @param mixed $value |
---|
| 197 | * @returns void |
---|
| 198 | */ |
---|
| 199 | public function assign_var($name, $value) |
---|
| 200 | { |
---|
| 201 | return $this->{$this->_current_driver}->assign_var($name, $value); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * Parse |
---|
| 206 | * Parse will return the contents instead of displaying |
---|
| 207 | * |
---|
| 208 | * @param mixed $template |
---|
| 209 | * @param mixed $data |
---|
| 210 | * @returns void |
---|
| 211 | */ |
---|
| 212 | public function parse($template, $data = array(), $return = false, $driver = '') |
---|
| 213 | { |
---|
| 214 | |
---|
| 215 | if (isset($GLOBALS['vskun'])) |
---|
| 216 | { |
---|
| 217 | foreach ($GLOBALS['vskun'] as $index=>$val) |
---|
| 218 | { |
---|
| 219 | $data[$index]=$val; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | // Are we setting a particular driver to render with? |
---|
| 224 | if ($driver !== '') |
---|
| 225 | { |
---|
| 226 | $this->_current_driver = trim($driver); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | // Add in the extension using the default if not supplied |
---|
| 230 | if (!stripos($template, ".")) |
---|
| 231 | { |
---|
| 232 | // If we have a parser template extension defined |
---|
| 233 | if (config_item('parser.'.$this->_current_driver.'.extension')) |
---|
| 234 | { |
---|
| 235 | $template = $template.config_item('parser.'.$this->_current_driver.'.extension'); |
---|
| 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
| 239 | show_error('No extension has been defined for the driver "'.$this->_current_driver.'". Please define one in the plentyparser.php config file.'); |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | // Call the driver parse function |
---|
| 244 | return $this->{$this->_current_driver}->parse($template, $data, $return); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * Parse String |
---|
| 249 | * Parse a string and return it as a string or display it |
---|
| 250 | * |
---|
| 251 | * @param mixed $string |
---|
| 252 | * @param mixed $data |
---|
| 253 | * @param mixed $return |
---|
| 254 | * @returns void |
---|
| 255 | */ |
---|
| 256 | public function parse_string($string, $data = array(), $return = false) |
---|
| 257 | { |
---|
| 258 | return $this->{$this->_current_driver}->parse_string($string, $data, $return); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | } |
---|