| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJTServicesMVCView { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
private $controller; |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
private $path; |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @var mixed |
| 29 |
*/ |
| 30 |
private $resourcePoints = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* put your comment there... |
| 34 |
* |
| 35 |
* @var mixed |
| 36 |
*/ |
| 37 |
private $scripts = array(); |
| 38 |
|
| 39 |
/** |
| 40 |
* put your comment there... |
| 41 |
* |
| 42 |
* @var mixed |
| 43 |
*/ |
| 44 |
private $styles = array(); |
| 45 |
|
| 46 |
/** |
| 47 |
* put your comment there... |
| 48 |
* |
| 49 |
* @var mixed |
| 50 |
*/ |
| 51 |
private $output; |
| 52 |
|
| 53 |
/** |
| 54 |
* put your comment there... |
| 55 |
* |
| 56 |
* @var mixed |
| 57 |
*/ |
| 58 |
private $url; |
| 59 |
|
| 60 |
/**. |
| 61 |
* put your comment there... |
| 62 |
* |
| 63 |
* @param CJTServicesMVCController $controller |
| 64 |
* @return {CJTServicesView|CJTServicesMVCController} |
| 65 |
*/ |
| 66 |
public function __construct(CJTServicesMVCController & $controller) |
| 67 |
{ |
| 68 |
|
| 69 |
$this->controller =& $controller; |
| 70 |
|
| 71 |
# Enqueue styles and script hooks |
| 72 |
if (is_admin()) |
| 73 |
{ |
| 74 |
add_action('admin_print_styles', array( $this, '_enqueueStyles')); |
| 75 |
add_action('admin_print_scripts', array( $this, '_enqueueScripts')); |
| 76 |
} |
| 77 |
|
| 78 |
# Cache View Path and Urls |
| 79 |
$config = $this->controller->getConfig(); |
| 80 |
$route = $this->controller->getRoute(); |
| 81 |
|
| 82 |
$viewConfig = $config['views'][$route['view']]; |
| 83 |
$this->path = $config['path'] . DIRECTORY_SEPARATOR . 'Views' . |
| 84 |
DIRECTORY_SEPARATOR . |
| 85 |
str_replace( '/', DIRECTORY_SEPARATOR, $viewConfig[ 'path' ] ); |
| 86 |
|
| 87 |
$this->url = "{$config[ 'url' ]}/Views/{$viewConfig[ 'path' ]}/"; |
| 88 |
|
| 89 |
# Push default resource location |
| 90 |
$this->resourcePoints[''] = $this->url; |
| 91 |
|
| 92 |
# Shared Resources Path |
| 93 |
$sharedResources =& $config['resources']['shared']; |
| 94 |
|
| 95 |
foreach ($sharedResources as $srname => $sr) |
| 96 |
{ |
| 97 |
$this->resourcePoints[$srname] = "{$config['url']}/{$sr['path']}"; |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* put your comment there... |
| 104 |
* |
| 105 |
*/ |
| 106 |
public function __toString() { |
| 107 |
# Render |
| 108 |
return $this->output; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* put your comment there... |
| 113 |
* |
| 114 |
*/ |
| 115 |
public function _enqueueScripts() |
| 116 |
{ |
| 117 |
|
| 118 |
foreach ( $this->scripts as $script ) |
| 119 |
{ |
| 120 |
// Enqueue script |
| 121 |
wp_enqueue_script( $script[ 'handle' ], |
| 122 |
$script[ 'src' ], |
| 123 |
$script[ 'dep' ], |
| 124 |
$script[ 'version' ], |
| 125 |
$script[ 'footer' ] |
| 126 |
); |
| 127 |
|
| 128 |
// Pluggable script queue |
| 129 |
foreach ( $script[ 'plugs' ] as $scriptPlug ) |
| 130 |
{ |
| 131 |
call_user_func( $scriptPlug, $script ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* put your comment there... |
| 139 |
* |
| 140 |
*/ |
| 141 |
public function _enqueueStyles() { |
| 142 |
foreach ( $this->styles as $style ) { |
| 143 |
wp_enqueue_style( $style[ 'handle' ], |
| 144 |
$style[ 'src' ], |
| 145 |
$style[ 'dep' ], |
| 146 |
$style[ 'version' ], |
| 147 |
$style[ 'media' ] |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* put your comment there... |
| 154 |
* |
| 155 |
* @param mixed $script |
| 156 |
*/ |
| 157 |
public function _localizeJS( $script ) |
| 158 |
{ |
| 159 |
|
| 160 |
$l10n =& $script[ 'l10n' ]; |
| 161 |
|
| 162 |
wp_localize_script( $script[ 'handle' ], $l10n[ 'jsName' ], $l10n[ 'strings' ] ); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
/*** |
| 167 |
* put your comment there... |
| 168 |
* |
| 169 |
* @param mixed $name |
| 170 |
* @param mixed $url |
| 171 |
* @param mixed $version |
| 172 |
* @param mixed $footer |
| 173 |
*/ |
| 174 |
public function enqueueScript( $handle, $src = null, $dep = null, $version = null, $footer = null ) |
| 175 |
{ |
| 176 |
|
| 177 |
$plugs = array(); |
| 178 |
|
| 179 |
$this->scripts[ $handle ] = compact( 'handle', 'src', 'dep', 'version', 'footer', 'plugs' ); |
| 180 |
|
| 181 |
return $this; |
| 182 |
} |
| 183 |
|
| 184 |
/*** |
| 185 |
* put your comment there... |
| 186 |
* |
| 187 |
* @param mixed $name |
| 188 |
* @param mixed $url |
| 189 |
* @param mixed $version |
| 190 |
* @param mixed $media |
| 191 |
*/ |
| 192 |
public function enqueueStyle($handle, $src = null, $dep = null, $version = null, $media = null) { |
| 193 |
# Add style |
| 194 |
$this->styles[] = compact( 'handle', 'src', 'dep', 'version', 'media' ); |
| 195 |
# Chain |
| 196 |
return $this; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* put your comment there... |
| 201 |
* |
| 202 |
*/ |
| 203 |
public function dispatch() |
| 204 |
{ |
| 205 |
# Get early output so its possible to use Wordpress hooks before they |
| 206 |
# are elapced |
| 207 |
$route = $this->controller->getRoute(); |
| 208 |
$this->output = $this->getTemplate( |
| 209 |
|
| 210 |
(isset($route[ 'template' ]) && $route['template']) ? |
| 211 |
$route[ 'template' ] : |
| 212 |
$route[ 'action' ] |
| 213 |
|
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* put your comment there... |
| 219 |
* |
| 220 |
*/ |
| 221 |
public function getPath() { |
| 222 |
return $this->path; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* |
| 227 |
* |
| 228 |
* @param mixed $name |
| 229 |
*/ |
| 230 |
public function getResName($name) |
| 231 |
{ |
| 232 |
|
| 233 |
$name = strtolower( $name ); |
| 234 |
|
| 235 |
$name = str_replace( array( '.' ), '-', $name ); |
| 236 |
|
| 237 |
$resName = strtolower( get_class( $this ) ) . "-{$name}"; |
| 238 |
|
| 239 |
return $resName; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* put your comment there... |
| 244 |
* |
| 245 |
* @param mixed $name |
| 246 |
* @param mixed $resPoint |
| 247 |
*/ |
| 248 |
public function getResUrl($name, $resPoint = '') |
| 249 |
{ |
| 250 |
return "{$this->resourcePoints[$resPoint]}/{$name}"; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* put your comment there... |
| 255 |
* |
| 256 |
* @param mixed $name |
| 257 |
*/ |
| 258 |
public function getScriptUrl($name, $resPoint = '') |
| 259 |
{ |
| 260 |
return $this->getResUrl("{$name}.js", $resPoint); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* put your comment there... |
| 265 |
* |
| 266 |
* @param mixed $name |
| 267 |
*/ |
| 268 |
public function getStyleUrl($name, $resPoint = '') |
| 269 |
{ |
| 270 |
return $this->getResUrl("{$name}.css", $resPoint); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* put your comment there... |
| 275 |
* |
| 276 |
* @param mixed $name |
| 277 |
*/ |
| 278 |
protected function getTemplate($name) |
| 279 |
{ |
| 280 |
# Get view path |
| 281 |
$config = $this->controller->getConfig(); |
| 282 |
$viewPath = $this->getPath(); |
| 283 |
|
| 284 |
# Template Extension/Format |
| 285 |
$extension = isset( $viewConfig[ 'format' ] ) ? $viewConfig[ 'format' ] : $config[ 'defaultFormat' ]; |
| 286 |
|
| 287 |
# Template file |
| 288 |
$templateFile = $viewPath . DIRECTORY_SEPARATOR . "{$name}.{$extension}.php"; |
| 289 |
|
| 290 |
# Execute tempate file |
| 291 |
ob_start(); |
| 292 |
|
| 293 |
require $templateFile; |
| 294 |
|
| 295 |
$result = ob_get_clean(); |
| 296 |
|
| 297 |
# Returns |
| 298 |
return $result; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* put your comment there... |
| 303 |
* |
| 304 |
* @param mixed $handle |
| 305 |
* @param mixed $jsName |
| 306 |
* @param mixed $strings |
| 307 |
*/ |
| 308 |
public function localizeJS( $handle, $jsName, $strings ) |
| 309 |
{ |
| 310 |
|
| 311 |
$script =& $this->scripts[ $handle ]; |
| 312 |
|
| 313 |
$script[ 'l10n' ] = compact( 'jsName', 'strings' ); |
| 314 |
|
| 315 |
$script[ 'plugs' ][ ] = array( $this, '_localizeJS' ); |
| 316 |
|
| 317 |
return $this; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* put your comment there... |
| 322 |
* |
| 323 |
*/ |
| 324 |
protected function messagesList() |
| 325 |
{ |
| 326 |
ob_start(); |
| 327 |
|
| 328 |
require __DIR__ . DIRECTORY_SEPARATOR . 'ViewTemplates' . DIRECTORY_SEPARATOR . 'MessagesList.html'; |
| 329 |
|
| 330 |
return ob_get_clean(); |
| 331 |
} |
| 332 |
|
| 333 |
} |