| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version controller.inc.php |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* No direct access. |
| 8 |
*/ |
| 9 |
defined('ABSPATH') or die("Access denied"); |
| 10 |
|
| 11 |
/** |
| 12 |
* CJT controller base class. |
| 13 |
*/ |
| 14 |
abstract class CJTController extends CJTHookableClass { |
| 15 |
|
| 16 |
/** */ |
| 17 |
const NONCE_ACTION = 'cjtoolbox'; |
| 18 |
|
| 19 |
/** |
| 20 |
* put your comment there... |
| 21 |
* |
| 22 |
* @var mixed |
| 23 |
*/ |
| 24 |
protected $action; |
| 25 |
|
| 26 |
/** |
| 27 |
* put your comment there... |
| 28 |
* |
| 29 |
* @var mixed |
| 30 |
*/ |
| 31 |
protected $controllerInfo = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* put your comment there... |
| 35 |
* |
| 36 |
* @var mixed |
| 37 |
*/ |
| 38 |
protected $defaultAction = 'index'; |
| 39 |
|
| 40 |
/** |
| 41 |
* put your comment there... |
| 42 |
* |
| 43 |
* @var mixed |
| 44 |
*/ |
| 45 |
protected $request; |
| 46 |
|
| 47 |
/** |
| 48 |
* put your comment there... |
| 49 |
* |
| 50 |
* @var mixed |
| 51 |
*/ |
| 52 |
protected $model = null; |
| 53 |
|
| 54 |
/** |
| 55 |
* put your comment there... |
| 56 |
* |
| 57 |
* @var mixed |
| 58 |
*/ |
| 59 |
protected $oncallback = array('parameters' => array('callback', 'action', 'args')); |
| 60 |
|
| 61 |
/** |
| 62 |
* put your comment there... |
| 63 |
* |
| 64 |
* @var mixed |
| 65 |
*/ |
| 66 |
protected $ongetactionname = array('parameters' => array('action')); |
| 67 |
|
| 68 |
/** |
| 69 |
* put your comment there... |
| 70 |
* |
| 71 |
* @var mixed |
| 72 |
*/ |
| 73 |
protected static $ongetclassname = array('parameters' => array('class', 'name', 'type')); |
| 74 |
|
| 75 |
/** |
| 76 |
* put your comment there... |
| 77 |
* |
| 78 |
* @var mixed |
| 79 |
*/ |
| 80 |
protected $ongetviewname = array('parameters' => array('view')); |
| 81 |
|
| 82 |
/** |
| 83 |
* put your comment there... |
| 84 |
* |
| 85 |
* @var mixed |
| 86 |
*/ |
| 87 |
protected static $onloadcontroller = array('parameters' => array('file', 'name')); |
| 88 |
|
| 89 |
/** |
| 90 |
* put your comment there... |
| 91 |
* |
| 92 |
* @var mixed |
| 93 |
*/ |
| 94 |
protected $view = null; |
| 95 |
|
| 96 |
/** |
| 97 |
* put your comment there... |
| 98 |
* |
| 99 |
* @param mixed $hasView |
| 100 |
* @param mixed $request |
| 101 |
* @param mixed $overrideControllerPath |
| 102 |
* @param mixed $overrideContollerPrefix |
| 103 |
* @return CJTController |
| 104 |
*/ |
| 105 |
public function __construct($hasView = null, |
| 106 |
$request = null, |
| 107 |
$overrideControllerPath = null, |
| 108 |
$overrideContollerPrefix = null) { |
| 109 |
// Initialize hookable! |
| 110 |
parent::__construct(); |
| 111 |
// Read request parameters. |
| 112 |
$this->request = array_merge(((array) $_REQUEST), ((array) $request)); |
| 113 |
// Create default model. |
| 114 |
if (isset($this->controllerInfo['model'])) { |
| 115 |
// E_ALL complain! |
| 116 |
if (!isset($this->controllerInfo['model_file'])) { |
| 117 |
$this->controllerInfo['model_file'] = null; |
| 118 |
} |
| 119 |
$this->model = CJTModel::create($this->controllerInfo['model'], |
| 120 |
$this->request, |
| 121 |
$this->controllerInfo['model_file'], |
| 122 |
dirname($overrideControllerPath), |
| 123 |
$overrideContollerPrefix); |
| 124 |
} |
| 125 |
// Create default view. |
| 126 |
if ($hasView === null) { // Default value for $hasView = true |
| 127 |
// Request/passed parameters has priority over controller default view! |
| 128 |
$view = $this->ongetviewname(isset($this->request['view']) ? $this->request['view'] : |
| 129 |
(isset($this->controllerInfo['view']) ? $this->controllerInfo['view'] : null) |
| 130 |
); |
| 131 |
if ($view) { |
| 132 |
$this->view = self::getView($view, |
| 133 |
null, |
| 134 |
dirname($overrideControllerPath), |
| 135 |
$overrideContollerPrefix) |
| 136 |
// Push data into view. |
| 137 |
->setModel($this->model) |
| 138 |
->setRequest($this->request); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* put your comment there... |
| 145 |
* |
| 146 |
*/ |
| 147 |
public function _doAction() { |
| 148 |
// Force use of internal action untless its empty |
| 149 |
// then look for submitted action or get the default! |
| 150 |
$action = $this->action ? $this->action : |
| 151 |
(isset($_GET['action']) ? $_GET['action'] : $this->defaultAction); |
| 152 |
// filter action name! |
| 153 |
$action = $this->ongetactionname($action); |
| 154 |
if ($action) { |
| 155 |
$actionHandler = "{$action}Action"; |
| 156 |
// filter callback |
| 157 |
$callback = $this->oncallback(array($this, $actionHandler), $action); |
| 158 |
// Callback! |
| 159 |
call_user_func($callback); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* put your comment there... |
| 165 |
* |
| 166 |
* @param mixed $name |
| 167 |
* @param mixed $hasView |
| 168 |
* @param mixed $request |
| 169 |
* @param mixed $overrideControllersPath |
| 170 |
* @param mixed $overrideControllersPrefix |
| 171 |
*/ |
| 172 |
public static function create($name, |
| 173 |
$hasView = null, |
| 174 |
$request = null, |
| 175 |
$overrideControllersPath = null, |
| 176 |
$overrideControllersPrefix = null) { |
| 177 |
// Import controller file. |
| 178 |
$pathToControllers = $overrideControllersPath ? $overrideControllersPath : CJTOOLBOX_CONTROLLERS_PATH; |
| 179 |
$controllerFile = "{$pathToControllers}/{$name}.php"; |
| 180 |
require_once self::trigger('CJTController.loadcontroller', $controllerFile, $name); |
| 181 |
// Get controller class name. |
| 182 |
$class = self::getClassName($name, 'Controller', $overrideControllersPrefix); |
| 183 |
// Instantiate controller class. |
| 184 |
return new $class($hasView, $request, $overrideControllersPath, $overrideControllersPrefix); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* put your comment there... |
| 189 |
* |
| 190 |
* @deprecated Use cssJSToolbox::createSecurityToken |
| 191 |
*/ |
| 192 |
public function createSecurityToken() { |
| 193 |
return wp_create_nonce(self::NONCE_ACTION); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* put your comment there... |
| 198 |
* |
| 199 |
*/ |
| 200 |
protected function displayAction() { |
| 201 |
// Get view layout! |
| 202 |
$layout = isset($this->request['layout']) ? $this->request['layout'] : 'default'; |
| 203 |
ob_start(); |
| 204 |
$this->view->display($layout); |
| 205 |
$content = ob_get_clean(); |
| 206 |
return $content; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* put your comment there... |
| 211 |
* |
| 212 |
* @param mixed $name |
| 213 |
* @param mixed $hasView |
| 214 |
* @param mixed $request |
| 215 |
* @param mixed $overrideControllersPath |
| 216 |
*/ |
| 217 |
public static function getInstance($name, |
| 218 |
$hasView = null, |
| 219 |
$request = null, |
| 220 |
$overrideControllersPath = null, |
| 221 |
$overrideControllersPrefix = null) { |
| 222 |
return self::create($name, $hasView, $request, $overrideControllersPath, $overrideControllersPrefix); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Use CJTModel::create instead. |
| 227 |
* |
| 228 |
* @deprecated No longer used. |
| 229 |
*/ |
| 230 |
public static function getModel($name, |
| 231 |
$params = array(), |
| 232 |
$file = null, |
| 233 |
$overrideModelsPath = null, |
| 234 |
$overrideModelsPrefix = null) { |
| 235 |
$model = null; |
| 236 |
$pathToModels = $overrideModelsPath ? ($overrideModelsPath . DIRECTORY_SEPARATOR . 'models') : CJTOOLBOX_MODELS_PATH; |
| 237 |
if (!$file) { |
| 238 |
$file = $name; |
| 239 |
} |
| 240 |
// Import model file. |
| 241 |
$modelFile = "{$pathToModels}/{$file}.php"; |
| 242 |
require_once $modelFile; |
| 243 |
// Create model object. |
| 244 |
$modelClass = self::getClassName($name, 'Model', $overrideModelsPrefix); |
| 245 |
if (!class_exists($modelClass)) { |
| 246 |
throw new Exception("Model class {$modelClass} doesn't exists!!!"); |
| 247 |
} |
| 248 |
$model = new $modelClass($params); |
| 249 |
return $model; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* @deprecated No longer used. |
| 254 |
*/ |
| 255 |
public static function getClassName($name, $type, $prefix = null) { |
| 256 |
// Init vars |
| 257 |
$className = ''; |
| 258 |
// Getting default prefix |
| 259 |
if (!$prefix) { |
| 260 |
$prefix = 'CJT'; |
| 261 |
} |
| 262 |
// Every word start with uppercase character. |
| 263 |
$sanitizedName = ucfirst(str_replace(array('-', '_'), ' ', "{$name} {$type}")); |
| 264 |
// Remove spaces. |
| 265 |
$sanitizedName = str_replace(' ', '', $sanitizedName); |
| 266 |
// Filter. |
| 267 |
$className = self::trigger('CJTController.getclassname', "{$prefix}{$sanitizedName}", $name, $type); |
| 268 |
return $className; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* put your comment there... |
| 273 |
* |
| 274 |
* @param mixed $name |
| 275 |
*/ |
| 276 |
public function getRequestParameter($name) { |
| 277 |
return isset($this->request[$name]) ? $this->request[$name] : null; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* |
| 282 |
* Use CJTView:create instrad. |
| 283 |
* |
| 284 |
* @deprecated |
| 285 |
*/ |
| 286 |
public static function getView($path, |
| 287 |
$params = null, |
| 288 |
$overrideViewsPath = null, |
| 289 |
$overrideViewsPrefix = null) { |
| 290 |
$view = null; |
| 291 |
// Import view file. |
| 292 |
$viewInfo = self::getViewInfo($path, $overrideViewsPath, $overrideViewsPrefix); |
| 293 |
require_once $viewInfo['viewFile']; |
| 294 |
// Create view object. |
| 295 |
$name = str_replace(' ', '', ucwords(str_replace('/', ' ',$path))); |
| 296 |
$viewClass = self::getClassName($name, 'view', $overrideViewsPrefix); |
| 297 |
$view = new $viewClass($viewInfo, $params); |
| 298 |
return $view; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* put your comment there... |
| 303 |
* |
| 304 |
*/ |
| 305 |
public static function getViewInfo($path, $overrideViewsPath = null, $overrideViewsPrefix = null) { |
| 306 |
// Plugin views Url |
| 307 |
$viewsUrl = $overrideViewsPath ? |
| 308 |
WP_PLUGIN_URL . '/' . basename(dirname($overrideViewsPath)) : |
| 309 |
CJTOOLBOX_VIEWS_URL; |
| 310 |
// Path to views dir. |
| 311 |
$pathToViews = $overrideViewsPath ? ($overrideViewsPath . DIRECTORY_SEPARATOR . 'views') : CJTOOLBOX_VIEWS_PATH; |
| 312 |
// Get view name. |
| 313 |
$name = basename($path); |
| 314 |
// View info struct. |
| 315 |
$viewInfo = array( |
| 316 |
'name' => $path, |
| 317 |
'url' => "{$viewsUrl}/{$path}", |
| 318 |
'path' => "{$pathToViews}/{$path}", |
| 319 |
'viewsPath' => $pathToViews, |
| 320 |
'viewsUrl' => $viewsUrl, |
| 321 |
'viewFile' => "{$pathToViews}/{$path}/view.php", |
| 322 |
); |
| 323 |
return $viewInfo; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* put your comment there... |
| 328 |
* |
| 329 |
* @param mixed $action |
| 330 |
*/ |
| 331 |
public function setAction($action) { |
| 332 |
$this->action = $action; |
| 333 |
return $this; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* put your comment there... |
| 338 |
* |
| 339 |
* @param mixed $name |
| 340 |
* @param mixed $value |
| 341 |
*/ |
| 342 |
public function setRequestParameter($name, $value) { |
| 343 |
$this->request[$name] = $value; |
| 344 |
return $this; |
| 345 |
} |
| 346 |
|
| 347 |
} // End class. |
| 348 |
|
| 349 |
// Hookable! |
| 350 |
CJTController::define('CJTController', array('hookType' => CJTWordpressEvents::HOOK_FILTER)); |