| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
abstract class CJTServicesModel { |
| 10 |
|
| 11 |
/** |
| 12 |
* |
| 13 |
*/ |
| 14 |
const MESSAGE_ERROR = 'error'; |
| 15 |
|
| 16 |
/** |
| 17 |
* |
| 18 |
*/ |
| 19 |
const MESSAGE_NOTICE = 'notice'; |
| 20 |
|
| 21 |
/** |
| 22 |
* |
| 23 |
*/ |
| 24 |
const MESSAGE_WARNING = 'warning'; |
| 25 |
|
| 26 |
/** |
| 27 |
* put your comment there... |
| 28 |
* |
| 29 |
* @var mixed |
| 30 |
*/ |
| 31 |
private $_storage; |
| 32 |
|
| 33 |
/** |
| 34 |
* put your comment there... |
| 35 |
* |
| 36 |
* @var mixed |
| 37 |
*/ |
| 38 |
private $messsages = array(); |
| 39 |
|
| 40 |
/** |
| 41 |
* put your comment there... |
| 42 |
* |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
# Create Model Storage |
| 46 |
$this->_storage = new CJTServicesStorage( get_class( $this ), array() ); |
| 47 |
# Load state |
| 48 |
$this->readState(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* put your comment there... |
| 53 |
* |
| 54 |
*/ |
| 55 |
public function clearMessages() { |
| 56 |
# Clear |
| 57 |
$this->messsages = array(); |
| 58 |
# CHain |
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* put your comment there... |
| 64 |
* |
| 65 |
*/ |
| 66 |
public function & getStorage() { |
| 67 |
return $this->_storage; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* put your comment there... |
| 72 |
* |
| 73 |
*/ |
| 74 |
public function pullMessages() { |
| 75 |
# Get messages copy |
| 76 |
$messages = $this->messsages; |
| 77 |
# CLean messages |
| 78 |
$this->clearMessages(); |
| 79 |
# Return copied messages |
| 80 |
return $messages; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* put your comment there... |
| 85 |
* |
| 86 |
*/ |
| 87 |
protected function readState() { |
| 88 |
# Read database state |
| 89 |
$values = $this->getStorage()->getValue(); |
| 90 |
# For every found first lever property set object variable |
| 91 |
foreach( $values as $name => $value ) { |
| 92 |
# Set object property value |
| 93 |
$this->$name = $value; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* put your comment there... |
| 99 |
* |
| 100 |
* @param mixed $message |
| 101 |
*/ |
| 102 |
public function & setInformation($message) { |
| 103 |
return $this->setMessage( self::MESSAGE_NOTICE, $message ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* put your comment there... |
| 108 |
* |
| 109 |
* @param mixed $message |
| 110 |
*/ |
| 111 |
public function & setError($message) { |
| 112 |
return $this->setMessage( self::MESSAGE_ERROR, $message ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* put your comment there... |
| 117 |
* |
| 118 |
* @param mixed $type |
| 119 |
* @param mixed $message |
| 120 |
*/ |
| 121 |
public function & setMessage($type, $message) { |
| 122 |
# Queue message |
| 123 |
$this->messsages[ $type ][] = $message; |
| 124 |
|
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* put your comment there... |
| 130 |
* |
| 131 |
* @param mixed $message |
| 132 |
*/ |
| 133 |
public function & setNotice($message) { |
| 134 |
return $this->setMessage( self::MESSAGE_NOTICE, $message ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* put your comment there... |
| 139 |
* |
| 140 |
* @param mixed $message |
| 141 |
*/ |
| 142 |
public function & setWarning($message) { |
| 143 |
return $this->setMessage( self::MESSAGE_WARNING, $message ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* put your comment there... |
| 148 |
* |
| 149 |
* @param mixed $location |
| 150 |
*/ |
| 151 |
public function redirect($location) { |
| 152 |
# Redirect |
| 153 |
wp_redirect( $location ); |
| 154 |
# Save state |
| 155 |
$this->saveState(); |
| 156 |
# Temrinate |
| 157 |
die(); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* put your comment there... |
| 162 |
* |
| 163 |
*/ |
| 164 |
public function & saveState() { |
| 165 |
# Initialize |
| 166 |
$values = array(); |
| 167 |
# Save all not underscored vars value to database storage |
| 168 |
$props = get_object_vars( $this ); |
| 169 |
foreach( $props as $name => $value ) { |
| 170 |
# Save variable if there is no _ at the begining of the string |
| 171 |
if ( strpos( $name, '_' ) !== 0 ) { |
| 172 |
$values[ $name ] = $value; |
| 173 |
} |
| 174 |
} |
| 175 |
# Set new value and save to database |
| 176 |
$this->getStorage()->setValue( $values )->update(); |
| 177 |
# Chaining |
| 178 |
return $this; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|