| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
abstract class CJTServicesMVCController { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
private $config; |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
private $models = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @var mixed |
| 29 |
*/ |
| 30 |
private $redirect; |
| 31 |
|
| 32 |
/** |
| 33 |
* put your comment there... |
| 34 |
* |
| 35 |
* @var mixed |
| 36 |
*/ |
| 37 |
private $result; |
| 38 |
|
| 39 |
/** |
| 40 |
* put your comment there... |
| 41 |
* |
| 42 |
* @var mixed |
| 43 |
*/ |
| 44 |
private $route; |
| 45 |
|
| 46 |
/** |
| 47 |
* put your comment there... |
| 48 |
* |
| 49 |
* @var mixed |
| 50 |
*/ |
| 51 |
private $view; |
| 52 |
|
| 53 |
/** |
| 54 |
* put your comment there... |
| 55 |
* |
| 56 |
* @param mixed $config |
| 57 |
* @param mixed $route |
| 58 |
* @return CJTServicesMVCController |
| 59 |
*/ |
| 60 |
public function __construct($config, $route) { |
| 61 |
# Initialize |
| 62 |
$this->config = $config; |
| 63 |
$this->route = $route; |
| 64 |
# Create View |
| 65 |
$viewClass = isset( $this->route[ 'viewClass' ] ) ? $this->route[ 'viewClass' ] : $this->config[ 'defaultViewClass' ]; |
| 66 |
$this->view = new $viewClass( $this ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* put your comment there... |
| 71 |
* |
| 72 |
* @param mixed $action |
| 73 |
*/ |
| 74 |
public function & dispatch($action) { |
| 75 |
# Get action method |
| 76 |
$methodName = lcfirst( $action ) . "Action"; |
| 77 |
if ( method_exists( $this, $methodName ) ) { |
| 78 |
# Call method, hold result |
| 79 |
$this->result = $this->$methodName(); |
| 80 |
# Redirect if redirected |
| 81 |
if ( $this->redirect ) { |
| 82 |
# Redirect |
| 83 |
wp_redirect( $this->redirect[ 'location' ] ); |
| 84 |
# Save Models state |
| 85 |
$this->saveState(); |
| 86 |
# Terminate |
| 87 |
die(); |
| 88 |
} |
| 89 |
# Result expected to be Array-Like iterator from which |
| 90 |
# we will copy all members to the view class |
| 91 |
$result = $this->result ? $this->result : array(); |
| 92 |
foreach ( $result as $name => $value ) { |
| 93 |
$this->view->$name = $value; |
| 94 |
} |
| 95 |
# Signal/Notify view so it can start work on the action result |
| 96 |
$this->view->dispatch(); |
| 97 |
} |
| 98 |
else { |
| 99 |
throw new Exception( 'Bad Request!!' ); |
| 100 |
} |
| 101 |
return $this; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* put your comment there... |
| 106 |
* |
| 107 |
*/ |
| 108 |
public function getConfig() { |
| 109 |
return $this->config; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* put your comment there... |
| 114 |
* |
| 115 |
* @param mixed $name |
| 116 |
*/ |
| 117 |
public function & getForm($name) {} |
| 118 |
|
| 119 |
/** |
| 120 |
* put your comment there... |
| 121 |
* |
| 122 |
* @param mixed $config |
| 123 |
* @param mixed $route |
| 124 |
*/ |
| 125 |
public static function & getInstance($config, $route) { |
| 126 |
# Get controller name or use default controller if controller is not supplied |
| 127 |
$controllerName = isset( $route[ 'controller' ] ) ? $route[ 'controller' ] : $config[ 'defaultController' ]; |
| 128 |
$controllerName = ucwords( $controllerName ); |
| 129 |
$controllerClass = "{$config[ 'namespace' ]}{$controllerName}Controller"; |
| 130 |
# Create controller |
| 131 |
$controller = new $controllerClass( $config, $route ); |
| 132 |
# Return controller |
| 133 |
return $controller; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* put your comment there... |
| 138 |
* |
| 139 |
* @param mixed $name |
| 140 |
*/ |
| 141 |
public function & getModel($name = null) { |
| 142 |
# Use controller name if name is nit supplied |
| 143 |
if ( ! $name ) { |
| 144 |
preg_match( '/' . preg_quote( $this->config[ 'namespace' ] ) . '(.+)Controller/', |
| 145 |
get_class( $this ), |
| 146 |
$controllerClassInfo ); |
| 147 |
$name = $controllerClassInfo[ 1 ]; |
| 148 |
} |
| 149 |
# Create model if not yet created |
| 150 |
if ( ! isset( $this->models[ $name ] ) ) { |
| 151 |
# Get model class |
| 152 |
$modelClass = "{$this->config[ 'namespace' ]}{$name}Model"; |
| 153 |
# Instantiate model |
| 154 |
$this->models[ $name ] = new $modelClass(); |
| 155 |
} |
| 156 |
return $this->models[ $name ]; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* put your comment there... |
| 161 |
* |
| 162 |
*/ |
| 163 |
public function & getResponse() { |
| 164 |
return $this->view; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* put your comment there... |
| 169 |
* |
| 170 |
*/ |
| 171 |
public function & getResult() { |
| 172 |
return $this->result; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* put your comment there... |
| 177 |
* |
| 178 |
*/ |
| 179 |
public function getRoute() { |
| 180 |
return $this->route; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* put your comment there... |
| 185 |
* |
| 186 |
* @param mixed $name |
| 187 |
*/ |
| 188 |
public function getServiceParameter($name) { |
| 189 |
|
| 190 |
return isset( $this->config[ 'parameters' ][ $name ] ) ? $this->config[ 'parameters' ][ $name ] : null; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* put your comment there... |
| 195 |
* |
| 196 |
* @param mixed $name |
| 197 |
*/ |
| 198 |
public function & getTable($name) {} |
| 199 |
|
| 200 |
/** |
| 201 |
* put your comment there... |
| 202 |
* |
| 203 |
* @param mixed $location |
| 204 |
*/ |
| 205 |
protected function redirect($location) { |
| 206 |
# Save redirect |
| 207 |
$this->redirect = compact( 'location' ); |
| 208 |
# Chain |
| 209 |
return $this; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* put your comment there... |
| 214 |
* |
| 215 |
*/ |
| 216 |
public function & saveState() { |
| 217 |
# Saving model states |
| 218 |
foreach ( $this->models as $model ) { |
| 219 |
$model->saveState(); |
| 220 |
} |
| 221 |
# chain |
| 222 |
return $this; |
| 223 |
} |
| 224 |
} |