@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class FileCache implements CacheInterface{ |
| 28 | 28 | |
@@ -44,15 +44,15 @@ discard block |
||
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | - * instance of the Log class |
|
| 48 | - */ |
|
| 49 | - if(is_object($logger)){ |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | - $this->logger->setLogger('Library::FileCache'); |
|
| 55 | - } |
|
| 47 | + * instance of the Log class |
|
| 48 | + */ |
|
| 49 | + if(is_object($logger)){ |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | + else{ |
|
| 53 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | + $this->logger->setLogger('Library::FileCache'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | 58 | if(! extension_loaded('zlib')){ |
@@ -80,26 +80,26 @@ discard block |
||
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
| 83 | - flock($handle, LOCK_SH); |
|
| 84 | - $data = file_get_contents($filePath); |
|
| 85 | - fclose($handle); |
|
| 86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | - unlink($filePath); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - if (time() > $data['expire']) { |
|
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | - // Unlinking when the file was expired |
|
| 96 | - unlink($filePath); |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | - return $data['data']; |
|
| 102 | - } |
|
| 83 | + flock($handle, LOCK_SH); |
|
| 84 | + $data = file_get_contents($filePath); |
|
| 85 | + fclose($handle); |
|
| 86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | + if (! $data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | + unlink($filePath); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + if (time() > $data['expire']) { |
|
| 94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | + // Unlinking when the file was expired |
|
| 96 | + unlink($filePath); |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | + return $data['data']; |
|
| 102 | + } |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | |
@@ -121,25 +121,25 @@ discard block |
||
| 121 | 121 | } |
| 122 | 122 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
| 123 | 123 | //Serializing along with the TTL |
| 124 | - $cacheData = serialize(array( |
|
| 124 | + $cacheData = serialize(array( |
|
| 125 | 125 | 'mtime' => time(), |
| 126 | 126 | 'expire' => $expire, |
| 127 | 127 | 'data' => $data, |
| 128 | 128 | 'ttl' => $ttl |
| 129 | 129 | ) |
| 130 | 130 | ); |
| 131 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | - fclose($handle); |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | - fclose($handle); |
|
| 131 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | + if(! $result){ |
|
| 133 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | + fclose($handle); |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + else{ |
|
| 138 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | + fclose($handle); |
|
| 140 | 140 | chmod($filePath, 0640); |
| 141 | 141 | return true; |
| 142 | - } |
|
| 142 | + } |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | } |
| 160 | 160 | else{ |
| 161 | 161 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 162 | - unlink($filePath); |
|
| 162 | + unlink($filePath); |
|
| 163 | 163 | return true; |
| 164 | 164 | } |
| 165 | 165 | } |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | else{ |
| 184 | 184 | $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
| 185 | - $data = file_get_contents($filePath); |
|
| 185 | + $data = file_get_contents($filePath); |
|
| 186 | 186 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 187 | 187 | if(! $data){ |
| 188 | 188 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
@@ -222,17 +222,17 @@ discard block |
||
| 222 | 222 | foreach ($list as $file) { |
| 223 | 223 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 224 | 224 | $data = file_get_contents($file); |
| 225 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 226 | - if(! $data){ |
|
| 227 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 228 | - } |
|
| 229 | - else if(time() > $data['expire']){ |
|
| 230 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 231 | - unlink($file); |
|
| 232 | - } |
|
| 233 | - else{ |
|
| 234 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 235 | - } |
|
| 225 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 226 | + if(! $data){ |
|
| 227 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 228 | + } |
|
| 229 | + else if(time() > $data['expire']){ |
|
| 230 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 231 | + unlink($file); |
|
| 232 | + } |
|
| 233 | + else{ |
|
| 234 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 235 | + } |
|
| 236 | 236 | } |
| 237 | 237 | } |
| 238 | 238 | } |
@@ -255,19 +255,19 @@ discard block |
||
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * @return boolean |
|
| 260 | - */ |
|
| 261 | - public function isCompressCacheData(){ |
|
| 262 | - return $this->compressCacheData; |
|
| 263 | - } |
|
| 258 | + /** |
|
| 259 | + * @return boolean |
|
| 260 | + */ |
|
| 261 | + public function isCompressCacheData(){ |
|
| 262 | + return $this->compressCacheData; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * @param boolean $compressCacheData |
|
| 267 | - * |
|
| 268 | - * @return self |
|
| 269 | - */ |
|
| 270 | - public function setCompressCacheData($status = true){ |
|
| 265 | + /** |
|
| 266 | + * @param boolean $compressCacheData |
|
| 267 | + * |
|
| 268 | + * @return self |
|
| 269 | + */ |
|
| 270 | + public function setCompressCacheData($status = true){ |
|
| 271 | 271 | //if Zlib extension is not loaded set compressCacheData to false |
| 272 | 272 | if($status === true && ! extension_loaded('zlib')){ |
| 273 | 273 | |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | $this->compressCacheData = $status; |
| 279 | 279 | } |
| 280 | 280 | return $this; |
| 281 | - } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | 283 | /** |
| 284 | 284 | * Check whether the cache feature for the handle is supported |
@@ -290,28 +290,28 @@ discard block |
||
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | /** |
| 293 | - * Return the Log instance |
|
| 294 | - * @return Log |
|
| 295 | - */ |
|
| 296 | - public function getLogger(){ |
|
| 297 | - return $this->logger; |
|
| 298 | - } |
|
| 293 | + * Return the Log instance |
|
| 294 | + * @return Log |
|
| 295 | + */ |
|
| 296 | + public function getLogger(){ |
|
| 297 | + return $this->logger; |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - /** |
|
| 301 | - * Set the log instance |
|
| 302 | - * @param Log $logger the log object |
|
| 303 | - */ |
|
| 304 | - public function setLogger(Log $logger){ |
|
| 305 | - $this->logger = $logger; |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 300 | + /** |
|
| 301 | + * Set the log instance |
|
| 302 | + * @param Log $logger the log object |
|
| 303 | + */ |
|
| 304 | + public function setLogger(Log $logger){ |
|
| 305 | + $this->logger = $logger; |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | 308 | |
| 309 | 309 | /** |
| 310 | - * Get the cache file full path for the given key |
|
| 311 | - * |
|
| 312 | - * @param $key the cache item key |
|
| 313 | - * @return string the full cache file path for this key |
|
| 314 | - */ |
|
| 310 | + * Get the cache file full path for the given key |
|
| 311 | + * |
|
| 312 | + * @param $key the cache item key |
|
| 313 | + * @return string the full cache file path for this key |
|
| 314 | + */ |
|
| 315 | 315 | private function getFilePath($key){ |
| 316 | 316 | return CACHE_PATH . md5($key) . '.cache'; |
| 317 | 317 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Whether to enable compression of the cache data file. |
@@ -39,23 +39,23 @@ discard block |
||
| 39 | 39 | private $logger; |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 42 | + public function __construct(Log $logger = null) { |
|
| 43 | + if (!$this->isSupported()) { |
|
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | 47 | * instance of the Log class |
| 48 | 48 | */ |
| 49 | - if(is_object($logger)){ |
|
| 49 | + if (is_object($logger)) { |
|
| 50 | 50 | $this->logger = $logger; |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 52 | + else { |
|
| 53 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 54 | 54 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | - if(! extension_loaded('zlib')){ |
|
| 58 | + if (!extension_loaded('zlib')) { |
|
| 59 | 59 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 60 | 60 | $this->compressCacheData = false; |
| 61 | 61 | } |
@@ -66,17 +66,17 @@ discard block |
||
| 66 | 66 | * @param string $key the key to identify the cache data |
| 67 | 67 | * @return mixed the cache data if exists else return false |
| 68 | 68 | */ |
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 69 | + public function get($key) { |
|
| 70 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 71 | 71 | $filePath = $this->getFilePath($key); |
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 72 | + if (!file_exists($filePath)) { |
|
| 73 | + $this->logger->info('No cache file found for the key [' . $key . '], return false'); |
|
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if(! is_resource($handle)){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 76 | + $this->logger->info('The cache file [' . $filePath . '] for the key [' . $key . '] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath, 'r'); |
|
| 78 | + if (!is_resource($handle)) { |
|
| 79 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
@@ -84,20 +84,20 @@ discard block |
||
| 84 | 84 | $data = file_get_contents($filePath); |
| 85 | 85 | fclose($handle); |
| 86 | 86 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 87 | + if (!$data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key [' . $key . '], return false'); |
|
| 89 | 89 | // If unserializing somehow didn't work out, we'll delete the file |
| 90 | 90 | unlink($filePath); |
| 91 | 91 | return false; |
| 92 | 92 | } |
| 93 | 93 | if (time() > $data['expire']) { |
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 94 | + $this->logger->info('The cache data for the key [' . $key . '] already expired delete the cache file [' . $filePath . ']'); |
|
| 95 | 95 | // Unlinking when the file was expired |
| 96 | 96 | unlink($filePath); |
| 97 | 97 | return false; |
| 98 | 98 | } |
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 99 | + else { |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key [' . $key . '], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | 101 | return $data['data']; |
| 102 | 102 | } |
| 103 | 103 | } |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | * @param integer $ttl the cache life time |
| 111 | 111 | * @return boolean true if success otherwise will return false |
| 112 | 112 | */ |
| 113 | - public function set($key, $data, $ttl = 0){ |
|
| 113 | + public function set($key, $data, $ttl = 0) { |
|
| 114 | 114 | $expire = time() + $ttl; |
| 115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 115 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | 116 | $filePath = $this->getFilePath($key); |
| 117 | - $handle = fopen($filePath,'w'); |
|
| 118 | - if(! is_resource($handle)){ |
|
| 119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 117 | + $handle = fopen($filePath, 'w'); |
|
| 118 | + if (!is_resource($handle)) { |
|
| 119 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | ) |
| 130 | 130 | ); |
| 131 | 131 | $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 132 | + if (!$result) { |
|
| 133 | + $this->logger->error('Can not write cache data into file [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 134 | 134 | fclose($handle); |
| 135 | 135 | return false; |
| 136 | 136 | } |
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 137 | + else { |
|
| 138 | + $this->logger->info('Cache data saved into file [' . $filePath . '] for the key [' . $key . ']'); |
|
| 139 | 139 | fclose($handle); |
| 140 | 140 | chmod($filePath, 0640); |
| 141 | 141 | return true; |
@@ -149,16 +149,16 @@ discard block |
||
| 149 | 149 | * @return boolean true if the cache is delete, false if can't delete |
| 150 | 150 | * the cache or the cache with the given key not exist |
| 151 | 151 | */ |
| 152 | - public function delete($key){ |
|
| 153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 152 | + public function delete($key) { |
|
| 153 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 154 | 154 | $filePath = $this->getFilePath($key); |
| 155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | - if(! file_exists($filePath)){ |
|
| 155 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 156 | + if (!file_exists($filePath)) { |
|
| 157 | 157 | $this->logger->info('This cache file does not exists skipping'); |
| 158 | 158 | return false; |
| 159 | 159 | } |
| 160 | - else{ |
|
| 161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 160 | + else { |
|
| 161 | + $this->logger->info('Found cache file [' . $filePath . '] remove it'); |
|
| 162 | 162 | unlink($filePath); |
| 163 | 163 | return true; |
| 164 | 164 | } |
@@ -172,25 +172,25 @@ discard block |
||
| 172 | 172 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 173 | 173 | * 'ttl' => the time to live of the cache in second |
| 174 | 174 | */ |
| 175 | - public function getInfo($key){ |
|
| 176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 175 | + public function getInfo($key) { |
|
| 176 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 177 | 177 | $filePath = $this->getFilePath($key); |
| 178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | - if(! file_exists($filePath)){ |
|
| 178 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 179 | + if (!file_exists($filePath)) { |
|
| 180 | 180 | $this->logger->info('This cache file does not exists skipping'); |
| 181 | 181 | return false; |
| 182 | 182 | } |
| 183 | - else{ |
|
| 184 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 183 | + else { |
|
| 184 | + $this->logger->info('Found cache file [' . $filePath . '] check the validity'); |
|
| 185 | 185 | $data = file_get_contents($filePath); |
| 186 | 186 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 187 | - if(! $data){ |
|
| 187 | + if (!$data) { |
|
| 188 | 188 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 189 | 189 | return false; |
| 190 | 190 | } |
| 191 | - else{ |
|
| 191 | + else { |
|
| 192 | 192 | $this->logger->info('This cache data is OK check for expire'); |
| 193 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 193 | + if (isset($data['expire']) && $data['expire'] > time()) { |
|
| 194 | 194 | $this->logger->info('This cache not yet expired return cache informations'); |
| 195 | 195 | $info = array( |
| 196 | 196 | 'mtime' => $data['mtime'], |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | ); |
| 200 | 200 | return $info; |
| 201 | 201 | } |
| 202 | - else{ |
|
| 202 | + else { |
|
| 203 | 203 | $this->logger->info('This cache already expired return false'); |
| 204 | 204 | return false; |
| 205 | 205 | } |
@@ -211,26 +211,26 @@ discard block |
||
| 211 | 211 | /** |
| 212 | 212 | * Used to delete expired cache data |
| 213 | 213 | */ |
| 214 | - public function deleteExpiredCache(){ |
|
| 214 | + public function deleteExpiredCache() { |
|
| 215 | 215 | $this->logger->debug('Deleting of expired cache files'); |
| 216 | 216 | $list = glob(CACHE_PATH . '*.cache'); |
| 217 | - if(! $list){ |
|
| 217 | + if (!$list) { |
|
| 218 | 218 | $this->logger->info('No cache files were found skipping'); |
| 219 | 219 | } |
| 220 | - else{ |
|
| 220 | + else { |
|
| 221 | 221 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 222 | 222 | foreach ($list as $file) { |
| 223 | 223 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 224 | 224 | $data = file_get_contents($file); |
| 225 | 225 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 226 | - if(! $data){ |
|
| 226 | + if (!$data) { |
|
| 227 | 227 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 228 | 228 | } |
| 229 | - else if(time() > $data['expire']){ |
|
| 229 | + else if (time() > $data['expire']) { |
|
| 230 | 230 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 231 | 231 | unlink($file); |
| 232 | 232 | } |
| 233 | - else{ |
|
| 233 | + else { |
|
| 234 | 234 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | /** |
| 241 | 241 | * Remove all file from cache folder |
| 242 | 242 | */ |
| 243 | - public function clean(){ |
|
| 243 | + public function clean() { |
|
| 244 | 244 | $this->logger->debug('Deleting of all cache files'); |
| 245 | 245 | $list = glob(CACHE_PATH . '*.cache'); |
| 246 | - if(! $list){ |
|
| 246 | + if (!$list) { |
|
| 247 | 247 | $this->logger->info('No cache files were found skipping'); |
| 248 | 248 | } |
| 249 | - else{ |
|
| 249 | + else { |
|
| 250 | 250 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 251 | 251 | foreach ($list as $file) { |
| 252 | 252 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | /** |
| 259 | 259 | * @return boolean |
| 260 | 260 | */ |
| 261 | - public function isCompressCacheData(){ |
|
| 261 | + public function isCompressCacheData() { |
|
| 262 | 262 | return $this->compressCacheData; |
| 263 | 263 | } |
| 264 | 264 | |
@@ -267,14 +267,14 @@ discard block |
||
| 267 | 267 | * |
| 268 | 268 | * @return self |
| 269 | 269 | */ |
| 270 | - public function setCompressCacheData($status = true){ |
|
| 270 | + public function setCompressCacheData($status = true) { |
|
| 271 | 271 | //if Zlib extension is not loaded set compressCacheData to false |
| 272 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 272 | + if ($status === true && !extension_loaded('zlib')) { |
|
| 273 | 273 | |
| 274 | 274 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 275 | 275 | $this->compressCacheData = false; |
| 276 | 276 | } |
| 277 | - else{ |
|
| 277 | + else { |
|
| 278 | 278 | $this->compressCacheData = $status; |
| 279 | 279 | } |
| 280 | 280 | return $this; |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * |
| 286 | 286 | * @return bool |
| 287 | 287 | */ |
| 288 | - public function isSupported(){ |
|
| 288 | + public function isSupported() { |
|
| 289 | 289 | return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
| 290 | 290 | } |
| 291 | 291 | |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | * Return the Log instance |
| 294 | 294 | * @return Log |
| 295 | 295 | */ |
| 296 | - public function getLogger(){ |
|
| 296 | + public function getLogger() { |
|
| 297 | 297 | return $this->logger; |
| 298 | 298 | } |
| 299 | 299 | |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * Set the log instance |
| 302 | 302 | * @param Log $logger the log object |
| 303 | 303 | */ |
| 304 | - public function setLogger(Log $logger){ |
|
| 304 | + public function setLogger(Log $logger) { |
|
| 305 | 305 | $this->logger = $logger; |
| 306 | 306 | return $this; |
| 307 | 307 | } |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | * @param $key the cache item key |
| 313 | 313 | * @return string the full cache file path for this key |
| 314 | 314 | */ |
| 315 | - private function getFilePath($key){ |
|
| 315 | + private function getFilePath($key) { |
|
| 316 | 316 | return CACHE_PATH . md5($key) . '.cache'; |
| 317 | 317 | } |
| 318 | 318 | } |
@@ -48,8 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | if(is_object($logger)){ |
| 50 | 50 | $this->logger = $logger; |
| 51 | - } |
|
| 52 | - else{ |
|
| 51 | + } else{ |
|
| 53 | 52 | $this->logger =& class_loader('Log', 'classes'); |
| 54 | 53 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 54 | } |
@@ -95,8 +94,7 @@ discard block |
||
| 95 | 94 | // Unlinking when the file was expired |
| 96 | 95 | unlink($filePath); |
| 97 | 96 | return false; |
| 98 | - } |
|
| 99 | - else{ |
|
| 97 | + } else{ |
|
| 100 | 98 | $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
| 101 | 99 | return $data['data']; |
| 102 | 100 | } |
@@ -133,8 +131,7 @@ discard block |
||
| 133 | 131 | $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
| 134 | 132 | fclose($handle); |
| 135 | 133 | return false; |
| 136 | - } |
|
| 137 | - else{ |
|
| 134 | + } else{ |
|
| 138 | 135 | $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
| 139 | 136 | fclose($handle); |
| 140 | 137 | chmod($filePath, 0640); |
@@ -156,8 +153,7 @@ discard block |
||
| 156 | 153 | if(! file_exists($filePath)){ |
| 157 | 154 | $this->logger->info('This cache file does not exists skipping'); |
| 158 | 155 | return false; |
| 159 | - } |
|
| 160 | - else{ |
|
| 156 | + } else{ |
|
| 161 | 157 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 162 | 158 | unlink($filePath); |
| 163 | 159 | return true; |
@@ -179,16 +175,14 @@ discard block |
||
| 179 | 175 | if(! file_exists($filePath)){ |
| 180 | 176 | $this->logger->info('This cache file does not exists skipping'); |
| 181 | 177 | return false; |
| 182 | - } |
|
| 183 | - else{ |
|
| 178 | + } else{ |
|
| 184 | 179 | $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
| 185 | 180 | $data = file_get_contents($filePath); |
| 186 | 181 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 187 | 182 | if(! $data){ |
| 188 | 183 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 189 | 184 | return false; |
| 190 | - } |
|
| 191 | - else{ |
|
| 185 | + } else{ |
|
| 192 | 186 | $this->logger->info('This cache data is OK check for expire'); |
| 193 | 187 | if(isset($data['expire']) && $data['expire'] > time()){ |
| 194 | 188 | $this->logger->info('This cache not yet expired return cache informations'); |
@@ -198,8 +192,7 @@ discard block |
||
| 198 | 192 | 'ttl' => $data['ttl'] |
| 199 | 193 | ); |
| 200 | 194 | return $info; |
| 201 | - } |
|
| 202 | - else{ |
|
| 195 | + } else{ |
|
| 203 | 196 | $this->logger->info('This cache already expired return false'); |
| 204 | 197 | return false; |
| 205 | 198 | } |
@@ -216,8 +209,7 @@ discard block |
||
| 216 | 209 | $list = glob(CACHE_PATH . '*.cache'); |
| 217 | 210 | if(! $list){ |
| 218 | 211 | $this->logger->info('No cache files were found skipping'); |
| 219 | - } |
|
| 220 | - else{ |
|
| 212 | + } else{ |
|
| 221 | 213 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 222 | 214 | foreach ($list as $file) { |
| 223 | 215 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -225,12 +217,10 @@ discard block |
||
| 225 | 217 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 226 | 218 | if(! $data){ |
| 227 | 219 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 228 | - } |
|
| 229 | - else if(time() > $data['expire']){ |
|
| 220 | + } else if(time() > $data['expire']){ |
|
| 230 | 221 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 231 | 222 | unlink($file); |
| 232 | - } |
|
| 233 | - else{ |
|
| 223 | + } else{ |
|
| 234 | 224 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 235 | 225 | } |
| 236 | 226 | } |
@@ -245,8 +235,7 @@ discard block |
||
| 245 | 235 | $list = glob(CACHE_PATH . '*.cache'); |
| 246 | 236 | if(! $list){ |
| 247 | 237 | $this->logger->info('No cache files were found skipping'); |
| 248 | - } |
|
| 249 | - else{ |
|
| 238 | + } else{ |
|
| 250 | 239 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 251 | 240 | foreach ($list as $file) { |
| 252 | 241 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -273,8 +262,7 @@ discard block |
||
| 273 | 262 | |
| 274 | 263 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 275 | 264 | $this->compressCacheData = false; |
| 276 | - } |
|
| 277 | - else{ |
|
| 265 | + } else{ |
|
| 278 | 266 | $this->compressCacheData = $status; |
| 279 | 267 | } |
| 280 | 268 | return $this; |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | private static $config = null; |
| 15 | 15 | |
| 16 | - public function __construct(){ |
|
| 16 | + public function __construct() { |
|
| 17 | 17 | $this->db = new Database(array( |
| 18 | 18 | 'driver' => 'sqlite', |
| 19 | 19 | 'database' => TESTS_PATH . 'assets/db_tests.db', |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | |
| 51 | 51 | |
| 52 | - public function testUsingSessionConfiguration(){ |
|
| 52 | + public function testUsingSessionConfiguration() { |
|
| 53 | 53 | //using value in the configuration |
| 54 | 54 | static::$config->set('session_save_path', 'DBSessionModel'); |
| 55 | 55 | static::$config->set('session_secret', $this->secret); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | $this->assertEquals($dbsh->decode($encoded), 'foo'); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - public function testWhenDataIsExpired(){ |
|
| 78 | + public function testWhenDataIsExpired() { |
|
| 79 | 79 | $dbsh = new DBSessionHandler($this->model); |
| 80 | 80 | $dbsh->setSessionSecret($this->secret); |
| 81 | 81 | |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | $this->assertNull($dbsh->read('foo')); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - public function testWhenDataAlreadyExistDoUpdate(){ |
|
| 91 | + public function testWhenDataAlreadyExistDoUpdate() { |
|
| 92 | 92 | $dbsh = new DBSessionHandler($this->model); |
| 93 | 93 | $dbsh->setSessionSecret($this->secret); |
| 94 | 94 | |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | $this->assertEquals($dbsh->read('foo'), '445'); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - public function testUsingCustomModelInstance(){ |
|
| 104 | + public function testUsingCustomModelInstance() { |
|
| 105 | 105 | $dbsh = new DBSessionHandler($this->model); |
| 106 | 106 | $dbsh->setSessionSecret($this->secret); |
| 107 | 107 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | |
| 132 | - public function testUsingCustomLogInstance(){ |
|
| 132 | + public function testUsingCustomLogInstance() { |
|
| 133 | 133 | $dbsh = new DBSessionHandler($this->model, new Log()); |
| 134 | 134 | $dbsh->setSessionSecret($this->secret); |
| 135 | 135 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | $this->assertEquals($dbsh->decode($encoded), 'foo'); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - public function testUsingCustomLoaderInstance(){ |
|
| 159 | + public function testUsingCustomLoaderInstance() { |
|
| 160 | 160 | $dbsh = new DBSessionHandler($this->model, null, new Loader()); |
| 161 | 161 | $dbsh->setSessionSecret($this->secret); |
| 162 | 162 | |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | |
| 187 | - public function testWhenModelInsanceIsNotSet(){ |
|
| 187 | + public function testWhenModelInsanceIsNotSet() { |
|
| 188 | 188 | $dbsh = new DBSessionHandler(null, null, new Loader()); |
| 189 | 189 | $dbsh->setSessionSecret($this->secret); |
| 190 | 190 | |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | $this->assertEquals($dbsh->decode($encoded), 'foo'); |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - public function testWhenModelTableColumnsIsNotSet(){ |
|
| 215 | + public function testWhenModelTableColumnsIsNotSet() { |
|
| 216 | 216 | //session table is empty |
| 217 | 217 | $this->model->setSessionTableColumns(array()); |
| 218 | 218 | $dbsh = new DBSessionHandler($this->model); |
@@ -1,595 +1,595 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | - * validation-in-model support, event callbacks and more. |
|
| 31 | - * |
|
| 32 | - * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | - * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | - */ |
|
| 35 | - |
|
| 36 | - class Model{ |
|
| 37 | - |
|
| 38 | - /* -------------------------------------------------------------- |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A base model with a series of CRUD functions (powered by CI's query builder), |
|
| 30 | + * validation-in-model support, event callbacks and more. |
|
| 31 | + * |
|
| 32 | + * @link http://github.com/jamierumbelow/codeigniter-base-model |
|
| 33 | + * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
|
| 34 | + */ |
|
| 35 | + |
|
| 36 | + class Model{ |
|
| 37 | + |
|
| 38 | + /* -------------------------------------------------------------- |
|
| 39 | 39 | * VARIABLES |
| 40 | 40 | * ------------------------------------------------------------ */ |
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * This model's default database table. Automatically |
|
| 44 | - * guessed by pluralising the model name. |
|
| 45 | - */ |
|
| 46 | - protected $_table; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The database connection object. Will be set to the default |
|
| 50 | - * connection. This allows individual models to use different DBs |
|
| 51 | - * without overwriting the global database connection. |
|
| 52 | - */ |
|
| 53 | - protected $_database; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * This model's default primary key or unique identifier. |
|
| 57 | - * Used by the get(), update() and delete() functions. |
|
| 58 | - */ |
|
| 59 | - protected $primary_key = 'id'; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Support for soft deletes and this model's 'deleted' key |
|
| 63 | - */ |
|
| 64 | - protected $soft_delete = false; |
|
| 65 | - protected $soft_delete_key = 'is_deleted'; |
|
| 66 | - protected $_temporary_with_deleted = FALSE; |
|
| 67 | - protected $_temporary_only_deleted = FALSE; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The various callbacks available to the model. Each are |
|
| 71 | - * simple lists of method names (methods will be run on $this). |
|
| 72 | - */ |
|
| 73 | - protected $before_create = array(); |
|
| 74 | - protected $after_create = array(); |
|
| 75 | - protected $before_update = array(); |
|
| 76 | - protected $after_update = array(); |
|
| 77 | - protected $before_get = array(); |
|
| 78 | - protected $after_get = array(); |
|
| 79 | - protected $before_delete = array(); |
|
| 80 | - protected $after_delete = array(); |
|
| 81 | - |
|
| 82 | - protected $callback_parameters = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Protected, non-modifiable attributes |
|
| 86 | - */ |
|
| 87 | - protected $protected_attributes = array(); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | - * => array to customise the class name and primary key |
|
| 92 | - */ |
|
| 93 | - protected $belongs_to = array(); |
|
| 94 | - protected $has_many = array(); |
|
| 95 | - |
|
| 96 | - protected $_with = array(); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * An array of validation rules. This needs to be the same format |
|
| 100 | - * as validation rules passed to the FormValidation library. |
|
| 101 | - */ |
|
| 102 | - protected $validate = array(); |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Optionally skip the validation. Used in conjunction with |
|
| 106 | - * skip_validation() to skip data validation for any future calls. |
|
| 107 | - */ |
|
| 108 | - protected $skip_validation = FALSE; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * By default we return our results as objects. If we need to override |
|
| 112 | - * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | - */ |
|
| 114 | - protected $return_type = 'object'; |
|
| 115 | - protected $_temporary_return_type = NULL; |
|
| 42 | + /** |
|
| 43 | + * This model's default database table. Automatically |
|
| 44 | + * guessed by pluralising the model name. |
|
| 45 | + */ |
|
| 46 | + protected $_table; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The database connection object. Will be set to the default |
|
| 50 | + * connection. This allows individual models to use different DBs |
|
| 51 | + * without overwriting the global database connection. |
|
| 52 | + */ |
|
| 53 | + protected $_database; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * This model's default primary key or unique identifier. |
|
| 57 | + * Used by the get(), update() and delete() functions. |
|
| 58 | + */ |
|
| 59 | + protected $primary_key = 'id'; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Support for soft deletes and this model's 'deleted' key |
|
| 63 | + */ |
|
| 64 | + protected $soft_delete = false; |
|
| 65 | + protected $soft_delete_key = 'is_deleted'; |
|
| 66 | + protected $_temporary_with_deleted = FALSE; |
|
| 67 | + protected $_temporary_only_deleted = FALSE; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The various callbacks available to the model. Each are |
|
| 71 | + * simple lists of method names (methods will be run on $this). |
|
| 72 | + */ |
|
| 73 | + protected $before_create = array(); |
|
| 74 | + protected $after_create = array(); |
|
| 75 | + protected $before_update = array(); |
|
| 76 | + protected $after_update = array(); |
|
| 77 | + protected $before_get = array(); |
|
| 78 | + protected $after_get = array(); |
|
| 79 | + protected $before_delete = array(); |
|
| 80 | + protected $after_delete = array(); |
|
| 81 | + |
|
| 82 | + protected $callback_parameters = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Protected, non-modifiable attributes |
|
| 86 | + */ |
|
| 87 | + protected $protected_attributes = array(); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Relationship arrays. Use flat strings for defaults or string |
|
| 91 | + * => array to customise the class name and primary key |
|
| 92 | + */ |
|
| 93 | + protected $belongs_to = array(); |
|
| 94 | + protected $has_many = array(); |
|
| 95 | + |
|
| 96 | + protected $_with = array(); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * An array of validation rules. This needs to be the same format |
|
| 100 | + * as validation rules passed to the FormValidation library. |
|
| 101 | + */ |
|
| 102 | + protected $validate = array(); |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Optionally skip the validation. Used in conjunction with |
|
| 106 | + * skip_validation() to skip data validation for any future calls. |
|
| 107 | + */ |
|
| 108 | + protected $skip_validation = FALSE; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * By default we return our results as objects. If we need to override |
|
| 112 | + * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
|
| 113 | + */ |
|
| 114 | + protected $return_type = 'object'; |
|
| 115 | + protected $_temporary_return_type = NULL; |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - /** |
|
| 118 | + /** |
|
| 119 | 119 | The database cache time |
| 120 | - */ |
|
| 121 | - protected $dbCacheTime = 0; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Instance of the Loader class |
|
| 125 | - * @var Loader |
|
| 126 | - */ |
|
| 127 | - protected $loaderInstance = null; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Instance of the FormValidation library |
|
| 131 | - * @var FormValidation |
|
| 132 | - */ |
|
| 133 | - protected $formValidationInstance = null; |
|
| 134 | - |
|
| 135 | - /* -------------------------------------------------------------- |
|
| 120 | + */ |
|
| 121 | + protected $dbCacheTime = 0; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Instance of the Loader class |
|
| 125 | + * @var Loader |
|
| 126 | + */ |
|
| 127 | + protected $loaderInstance = null; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Instance of the FormValidation library |
|
| 131 | + * @var FormValidation |
|
| 132 | + */ |
|
| 133 | + protected $formValidationInstance = null; |
|
| 134 | + |
|
| 135 | + /* -------------------------------------------------------------- |
|
| 136 | 136 | * GENERIC METHODS |
| 137 | 137 | * ------------------------------------------------------------ */ |
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | - * try our best to guess the table name. |
|
| 142 | - */ |
|
| 143 | - public function __construct(Database $db = null){ |
|
| 144 | - if(is_object($db)){ |
|
| 145 | - $this->setDatabaseInstance($db); |
|
| 146 | - } |
|
| 147 | - else{ |
|
| 148 | - $obj = & get_instance(); |
|
| 149 | - if(isset($obj->database) && is_object($obj->database)){ |
|
| 150 | - /** |
|
| 151 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | - * to prevent duplication |
|
| 153 | - */ |
|
| 154 | - $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | - array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | - $this->_temporary_return_type = $this->return_type; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /* -------------------------------------------------------------- |
|
| 139 | + /** |
|
| 140 | + * Initialise the model, tie into the CodeIgniter superobject and |
|
| 141 | + * try our best to guess the table name. |
|
| 142 | + */ |
|
| 143 | + public function __construct(Database $db = null){ |
|
| 144 | + if(is_object($db)){ |
|
| 145 | + $this->setDatabaseInstance($db); |
|
| 146 | + } |
|
| 147 | + else{ |
|
| 148 | + $obj = & get_instance(); |
|
| 149 | + if(isset($obj->database) && is_object($obj->database)){ |
|
| 150 | + /** |
|
| 151 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 152 | + * to prevent duplication |
|
| 153 | + */ |
|
| 154 | + $this->setDatabaseInstance(clone $obj->database); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + array_unshift($this->before_create, 'protect_attributes'); |
|
| 159 | + array_unshift($this->before_update, 'protect_attributes'); |
|
| 160 | + $this->_temporary_return_type = $this->return_type; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /* -------------------------------------------------------------- |
|
| 164 | 164 | * CRUD INTERFACE |
| 165 | 165 | * ------------------------------------------------------------ */ |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | - */ |
|
| 170 | - public function get($primary_value) |
|
| 171 | - { |
|
| 172 | - return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | - * any valid value to $this->_database->where(). |
|
| 178 | - */ |
|
| 179 | - public function get_by() |
|
| 180 | - { |
|
| 181 | - $where = func_get_args(); |
|
| 182 | - |
|
| 183 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | - { |
|
| 185 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $this->_set_where($where); |
|
| 189 | - |
|
| 190 | - $this->trigger('before_get'); |
|
| 167 | + /** |
|
| 168 | + * Fetch a single record based on the primary key. Returns an object. |
|
| 169 | + */ |
|
| 170 | + public function get($primary_value) |
|
| 171 | + { |
|
| 172 | + return $this->get_by($this->primary_key, $primary_value); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
| 177 | + * any valid value to $this->_database->where(). |
|
| 178 | + */ |
|
| 179 | + public function get_by() |
|
| 180 | + { |
|
| 181 | + $where = func_get_args(); |
|
| 182 | + |
|
| 183 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 184 | + { |
|
| 185 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $this->_set_where($where); |
|
| 189 | + |
|
| 190 | + $this->trigger('before_get'); |
|
| 191 | 191 | $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
| 192 | - $row = $this->_database->from($this->_table)->get($type); |
|
| 193 | - $this->_temporary_return_type = $this->return_type; |
|
| 194 | - $row = $this->trigger('after_get', $row); |
|
| 195 | - $this->_with = array(); |
|
| 196 | - return $row; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Fetch an array of records based on an array of primary values. |
|
| 201 | - */ |
|
| 202 | - public function get_many($values) |
|
| 203 | - { |
|
| 204 | - $this->_database->in($this->primary_key, $values); |
|
| 205 | - return $this->get_all(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | - */ |
|
| 211 | - public function get_many_by() |
|
| 212 | - { |
|
| 213 | - $where = func_get_args(); |
|
| 214 | - $this->_set_where($where); |
|
| 215 | - return $this->get_all(); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | - * to $this->_database->get() with scoped methods. |
|
| 221 | - */ |
|
| 222 | - public function get_all() |
|
| 223 | - { |
|
| 224 | - $this->trigger('before_get'); |
|
| 225 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | - { |
|
| 227 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | - } |
|
| 192 | + $row = $this->_database->from($this->_table)->get($type); |
|
| 193 | + $this->_temporary_return_type = $this->return_type; |
|
| 194 | + $row = $this->trigger('after_get', $row); |
|
| 195 | + $this->_with = array(); |
|
| 196 | + return $row; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Fetch an array of records based on an array of primary values. |
|
| 201 | + */ |
|
| 202 | + public function get_many($values) |
|
| 203 | + { |
|
| 204 | + $this->_database->in($this->primary_key, $values); |
|
| 205 | + return $this->get_all(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Fetch an array of records based on an arbitrary WHERE call. |
|
| 210 | + */ |
|
| 211 | + public function get_many_by() |
|
| 212 | + { |
|
| 213 | + $where = func_get_args(); |
|
| 214 | + $this->_set_where($where); |
|
| 215 | + return $this->get_all(); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Fetch all the records in the table. Can be used as a generic call |
|
| 220 | + * to $this->_database->get() with scoped methods. |
|
| 221 | + */ |
|
| 222 | + public function get_all() |
|
| 223 | + { |
|
| 224 | + $this->trigger('before_get'); |
|
| 225 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 226 | + { |
|
| 227 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 228 | + } |
|
| 229 | 229 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
| 230 | - $result = $this->_database->from($this->_table)->getAll($type); |
|
| 231 | - $this->_temporary_return_type = $this->return_type; |
|
| 232 | - |
|
| 233 | - foreach ($result as $key => &$row) |
|
| 234 | - { |
|
| 235 | - $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 236 | - } |
|
| 237 | - $this->_with = array(); |
|
| 238 | - return $result; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Insert a new row into the table. $data should be an associative array |
|
| 243 | - * of data to be inserted. Returns newly created ID. |
|
| 244 | - */ |
|
| 245 | - public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 246 | - { |
|
| 247 | - if ($skip_validation === FALSE) |
|
| 248 | - { |
|
| 249 | - $data = $this->validate($data); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if ($data !== FALSE) |
|
| 253 | - { |
|
| 254 | - $data = $this->trigger('before_create', $data); |
|
| 255 | - $this->_database->from($this->_table)->insert($data, $escape); |
|
| 256 | - $insert_id = $this->_database->insertId(); |
|
| 257 | - $this->trigger('after_create', $insert_id); |
|
| 258 | - return $insert_id; |
|
| 259 | - } |
|
| 260 | - else |
|
| 261 | - { |
|
| 262 | - return FALSE; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 268 | - */ |
|
| 269 | - public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 270 | - { |
|
| 271 | - $ids = array(); |
|
| 272 | - foreach ($data as $key => $row) |
|
| 273 | - { |
|
| 274 | - $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 275 | - } |
|
| 276 | - return $ids; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Updated a record based on the primary value. |
|
| 281 | - */ |
|
| 282 | - public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 283 | - { |
|
| 284 | - $data = $this->trigger('before_update', $data); |
|
| 285 | - if ($skip_validation === FALSE) |
|
| 286 | - { |
|
| 287 | - $data = $this->validate($data); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ($data !== FALSE) |
|
| 291 | - { |
|
| 292 | - $result = $this->_database->where($this->primary_key, $primary_value) |
|
| 293 | - ->from($this->_table) |
|
| 294 | - ->update($data, $escape); |
|
| 295 | - $this->trigger('after_update', array($data, $result)); |
|
| 296 | - return $result; |
|
| 297 | - } |
|
| 298 | - else |
|
| 299 | - { |
|
| 300 | - return FALSE; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Update many records, based on an array of primary values. |
|
| 306 | - */ |
|
| 307 | - public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 308 | - { |
|
| 309 | - $data = $this->trigger('before_update', $data); |
|
| 310 | - if ($skip_validation === FALSE) |
|
| 311 | - { |
|
| 312 | - $data = $this->validate($data); |
|
| 313 | - } |
|
| 314 | - if ($data !== FALSE) |
|
| 315 | - { |
|
| 316 | - $result = $this->_database->in($this->primary_key, $primary_values) |
|
| 317 | - ->from($this->_table) |
|
| 318 | - ->update($data, $escape); |
|
| 319 | - $this->trigger('after_update', array($data, $result)); |
|
| 320 | - return $result; |
|
| 321 | - } |
|
| 322 | - else |
|
| 323 | - { |
|
| 324 | - return FALSE; |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Updated a record based on an arbitrary WHERE clause. |
|
| 330 | - */ |
|
| 331 | - public function update_by() |
|
| 332 | - { |
|
| 333 | - $args = func_get_args(); |
|
| 334 | - $data = array(); |
|
| 335 | - if(count($args) == 2){ |
|
| 336 | - if(is_array($args[1])){ |
|
| 337 | - $data = array_pop($args); |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - else if(count($args) == 3){ |
|
| 341 | - if(is_array($args[2])){ |
|
| 342 | - $data = array_pop($args); |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - $data = $this->trigger('before_update', $data); |
|
| 346 | - if ($this->validate($data) !== FALSE) |
|
| 347 | - { |
|
| 348 | - $this->_set_where($args); |
|
| 349 | - $result = $this->_database->from($this->_table)->update($data); |
|
| 350 | - $this->trigger('after_update', array($data, $result)); |
|
| 351 | - return $result; |
|
| 352 | - } |
|
| 353 | - else |
|
| 354 | - { |
|
| 355 | - return FALSE; |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Update all records |
|
| 361 | - */ |
|
| 362 | - public function update_all($data = array(), $escape = true) |
|
| 363 | - { |
|
| 364 | - $data = $this->trigger('before_update', $data); |
|
| 365 | - $result = $this->_database->from($this->_table)->update($data, $escape); |
|
| 366 | - $this->trigger('after_update', array($data, $result)); |
|
| 367 | - return $result; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Delete a row from the table by the primary value |
|
| 372 | - */ |
|
| 373 | - public function delete($id) |
|
| 374 | - { |
|
| 375 | - $this->trigger('before_delete', $id); |
|
| 376 | - $this->_database->where($this->primary_key, $id); |
|
| 377 | - if ($this->soft_delete) |
|
| 378 | - { |
|
| 379 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 380 | - } |
|
| 381 | - else |
|
| 382 | - { |
|
| 383 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - $this->trigger('after_delete', $result); |
|
| 387 | - return $result; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Delete a row from the database table by an arbitrary WHERE clause |
|
| 392 | - */ |
|
| 393 | - public function delete_by() |
|
| 394 | - { |
|
| 395 | - $where = func_get_args(); |
|
| 396 | - $where = $this->trigger('before_delete', $where); |
|
| 397 | - $this->_set_where($where); |
|
| 398 | - if ($this->soft_delete) |
|
| 399 | - { |
|
| 400 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 401 | - } |
|
| 402 | - else |
|
| 403 | - { |
|
| 404 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 405 | - } |
|
| 406 | - $this->trigger('after_delete', $result); |
|
| 407 | - return $result; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Delete many rows from the database table by multiple primary values |
|
| 412 | - */ |
|
| 413 | - public function delete_many($primary_values) |
|
| 414 | - { |
|
| 415 | - $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 416 | - $this->_database->in($this->primary_key, $primary_values); |
|
| 417 | - if ($this->soft_delete) |
|
| 418 | - { |
|
| 419 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 420 | - } |
|
| 421 | - else |
|
| 422 | - { |
|
| 423 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 424 | - } |
|
| 425 | - $this->trigger('after_delete', $result); |
|
| 426 | - return $result; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Truncates the table |
|
| 432 | - */ |
|
| 433 | - public function truncate() |
|
| 434 | - { |
|
| 435 | - $result = $this->_database->from($this->_table)->delete(); |
|
| 436 | - return $result; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /* -------------------------------------------------------------- |
|
| 230 | + $result = $this->_database->from($this->_table)->getAll($type); |
|
| 231 | + $this->_temporary_return_type = $this->return_type; |
|
| 232 | + |
|
| 233 | + foreach ($result as $key => &$row) |
|
| 234 | + { |
|
| 235 | + $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
|
| 236 | + } |
|
| 237 | + $this->_with = array(); |
|
| 238 | + return $result; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Insert a new row into the table. $data should be an associative array |
|
| 243 | + * of data to be inserted. Returns newly created ID. |
|
| 244 | + */ |
|
| 245 | + public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 246 | + { |
|
| 247 | + if ($skip_validation === FALSE) |
|
| 248 | + { |
|
| 249 | + $data = $this->validate($data); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if ($data !== FALSE) |
|
| 253 | + { |
|
| 254 | + $data = $this->trigger('before_create', $data); |
|
| 255 | + $this->_database->from($this->_table)->insert($data, $escape); |
|
| 256 | + $insert_id = $this->_database->insertId(); |
|
| 257 | + $this->trigger('after_create', $insert_id); |
|
| 258 | + return $insert_id; |
|
| 259 | + } |
|
| 260 | + else |
|
| 261 | + { |
|
| 262 | + return FALSE; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Insert multiple rows into the table. Returns an array of multiple IDs. |
|
| 268 | + */ |
|
| 269 | + public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
|
| 270 | + { |
|
| 271 | + $ids = array(); |
|
| 272 | + foreach ($data as $key => $row) |
|
| 273 | + { |
|
| 274 | + $ids[] = $this->insert($row, $skip_validation, $escape); |
|
| 275 | + } |
|
| 276 | + return $ids; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Updated a record based on the primary value. |
|
| 281 | + */ |
|
| 282 | + public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 283 | + { |
|
| 284 | + $data = $this->trigger('before_update', $data); |
|
| 285 | + if ($skip_validation === FALSE) |
|
| 286 | + { |
|
| 287 | + $data = $this->validate($data); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ($data !== FALSE) |
|
| 291 | + { |
|
| 292 | + $result = $this->_database->where($this->primary_key, $primary_value) |
|
| 293 | + ->from($this->_table) |
|
| 294 | + ->update($data, $escape); |
|
| 295 | + $this->trigger('after_update', array($data, $result)); |
|
| 296 | + return $result; |
|
| 297 | + } |
|
| 298 | + else |
|
| 299 | + { |
|
| 300 | + return FALSE; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Update many records, based on an array of primary values. |
|
| 306 | + */ |
|
| 307 | + public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
|
| 308 | + { |
|
| 309 | + $data = $this->trigger('before_update', $data); |
|
| 310 | + if ($skip_validation === FALSE) |
|
| 311 | + { |
|
| 312 | + $data = $this->validate($data); |
|
| 313 | + } |
|
| 314 | + if ($data !== FALSE) |
|
| 315 | + { |
|
| 316 | + $result = $this->_database->in($this->primary_key, $primary_values) |
|
| 317 | + ->from($this->_table) |
|
| 318 | + ->update($data, $escape); |
|
| 319 | + $this->trigger('after_update', array($data, $result)); |
|
| 320 | + return $result; |
|
| 321 | + } |
|
| 322 | + else |
|
| 323 | + { |
|
| 324 | + return FALSE; |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Updated a record based on an arbitrary WHERE clause. |
|
| 330 | + */ |
|
| 331 | + public function update_by() |
|
| 332 | + { |
|
| 333 | + $args = func_get_args(); |
|
| 334 | + $data = array(); |
|
| 335 | + if(count($args) == 2){ |
|
| 336 | + if(is_array($args[1])){ |
|
| 337 | + $data = array_pop($args); |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + else if(count($args) == 3){ |
|
| 341 | + if(is_array($args[2])){ |
|
| 342 | + $data = array_pop($args); |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + $data = $this->trigger('before_update', $data); |
|
| 346 | + if ($this->validate($data) !== FALSE) |
|
| 347 | + { |
|
| 348 | + $this->_set_where($args); |
|
| 349 | + $result = $this->_database->from($this->_table)->update($data); |
|
| 350 | + $this->trigger('after_update', array($data, $result)); |
|
| 351 | + return $result; |
|
| 352 | + } |
|
| 353 | + else |
|
| 354 | + { |
|
| 355 | + return FALSE; |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Update all records |
|
| 361 | + */ |
|
| 362 | + public function update_all($data = array(), $escape = true) |
|
| 363 | + { |
|
| 364 | + $data = $this->trigger('before_update', $data); |
|
| 365 | + $result = $this->_database->from($this->_table)->update($data, $escape); |
|
| 366 | + $this->trigger('after_update', array($data, $result)); |
|
| 367 | + return $result; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Delete a row from the table by the primary value |
|
| 372 | + */ |
|
| 373 | + public function delete($id) |
|
| 374 | + { |
|
| 375 | + $this->trigger('before_delete', $id); |
|
| 376 | + $this->_database->where($this->primary_key, $id); |
|
| 377 | + if ($this->soft_delete) |
|
| 378 | + { |
|
| 379 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 380 | + } |
|
| 381 | + else |
|
| 382 | + { |
|
| 383 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + $this->trigger('after_delete', $result); |
|
| 387 | + return $result; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Delete a row from the database table by an arbitrary WHERE clause |
|
| 392 | + */ |
|
| 393 | + public function delete_by() |
|
| 394 | + { |
|
| 395 | + $where = func_get_args(); |
|
| 396 | + $where = $this->trigger('before_delete', $where); |
|
| 397 | + $this->_set_where($where); |
|
| 398 | + if ($this->soft_delete) |
|
| 399 | + { |
|
| 400 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 401 | + } |
|
| 402 | + else |
|
| 403 | + { |
|
| 404 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 405 | + } |
|
| 406 | + $this->trigger('after_delete', $result); |
|
| 407 | + return $result; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Delete many rows from the database table by multiple primary values |
|
| 412 | + */ |
|
| 413 | + public function delete_many($primary_values) |
|
| 414 | + { |
|
| 415 | + $primary_values = $this->trigger('before_delete', $primary_values); |
|
| 416 | + $this->_database->in($this->primary_key, $primary_values); |
|
| 417 | + if ($this->soft_delete) |
|
| 418 | + { |
|
| 419 | + $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 420 | + } |
|
| 421 | + else |
|
| 422 | + { |
|
| 423 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 424 | + } |
|
| 425 | + $this->trigger('after_delete', $result); |
|
| 426 | + return $result; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Truncates the table |
|
| 432 | + */ |
|
| 433 | + public function truncate() |
|
| 434 | + { |
|
| 435 | + $result = $this->_database->from($this->_table)->delete(); |
|
| 436 | + return $result; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /* -------------------------------------------------------------- |
|
| 440 | 440 | * RELATIONSHIPS |
| 441 | 441 | * ------------------------------------------------------------ */ |
| 442 | 442 | |
| 443 | - public function with($relationship) |
|
| 444 | - { |
|
| 445 | - $this->_with[] = $relationship; |
|
| 446 | - if (!in_array('relate', $this->after_get)) |
|
| 447 | - { |
|
| 448 | - $this->after_get[] = 'relate'; |
|
| 449 | - } |
|
| 450 | - return $this; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - public function relate($row) |
|
| 454 | - { |
|
| 455 | - if (empty($row)) |
|
| 456 | - { |
|
| 457 | - return $row; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - foreach ($this->belongs_to as $key => $value) |
|
| 461 | - { |
|
| 462 | - if (is_string($value)) |
|
| 463 | - { |
|
| 464 | - $relationship = $value; |
|
| 465 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 466 | - } |
|
| 467 | - else |
|
| 468 | - { |
|
| 469 | - $relationship = $key; |
|
| 470 | - $options = $value; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - if (in_array($relationship, $this->_with)) |
|
| 474 | - { |
|
| 475 | - if(is_object($this->loaderInstance)){ |
|
| 476 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 477 | - } |
|
| 478 | - else{ |
|
| 479 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 480 | - } |
|
| 481 | - if (is_object($row)) |
|
| 482 | - { |
|
| 483 | - $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 484 | - } |
|
| 485 | - else |
|
| 486 | - { |
|
| 487 | - $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - foreach ($this->has_many as $key => $value) |
|
| 493 | - { |
|
| 494 | - if (is_string($value)) |
|
| 495 | - { |
|
| 496 | - $relationship = $value; |
|
| 497 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 498 | - } |
|
| 499 | - else |
|
| 500 | - { |
|
| 501 | - $relationship = $key; |
|
| 502 | - $options = $value; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - if (in_array($relationship, $this->_with)) |
|
| 506 | - { |
|
| 507 | - if(is_object($this->loaderInstance)){ |
|
| 508 | - $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 509 | - } |
|
| 510 | - else{ |
|
| 511 | - Loader::model($options['model'], $relationship . '_model'); |
|
| 512 | - } |
|
| 513 | - if (is_object($row)) |
|
| 514 | - { |
|
| 515 | - $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 516 | - } |
|
| 517 | - else |
|
| 518 | - { |
|
| 519 | - $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - } |
|
| 523 | - return $row; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /* -------------------------------------------------------------- |
|
| 443 | + public function with($relationship) |
|
| 444 | + { |
|
| 445 | + $this->_with[] = $relationship; |
|
| 446 | + if (!in_array('relate', $this->after_get)) |
|
| 447 | + { |
|
| 448 | + $this->after_get[] = 'relate'; |
|
| 449 | + } |
|
| 450 | + return $this; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + public function relate($row) |
|
| 454 | + { |
|
| 455 | + if (empty($row)) |
|
| 456 | + { |
|
| 457 | + return $row; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + foreach ($this->belongs_to as $key => $value) |
|
| 461 | + { |
|
| 462 | + if (is_string($value)) |
|
| 463 | + { |
|
| 464 | + $relationship = $value; |
|
| 465 | + $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 466 | + } |
|
| 467 | + else |
|
| 468 | + { |
|
| 469 | + $relationship = $key; |
|
| 470 | + $options = $value; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + if (in_array($relationship, $this->_with)) |
|
| 474 | + { |
|
| 475 | + if(is_object($this->loaderInstance)){ |
|
| 476 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 477 | + } |
|
| 478 | + else{ |
|
| 479 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 480 | + } |
|
| 481 | + if (is_object($row)) |
|
| 482 | + { |
|
| 483 | + $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
|
| 484 | + } |
|
| 485 | + else |
|
| 486 | + { |
|
| 487 | + $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + foreach ($this->has_many as $key => $value) |
|
| 493 | + { |
|
| 494 | + if (is_string($value)) |
|
| 495 | + { |
|
| 496 | + $relationship = $value; |
|
| 497 | + $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 498 | + } |
|
| 499 | + else |
|
| 500 | + { |
|
| 501 | + $relationship = $key; |
|
| 502 | + $options = $value; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + if (in_array($relationship, $this->_with)) |
|
| 506 | + { |
|
| 507 | + if(is_object($this->loaderInstance)){ |
|
| 508 | + $this->loaderInstance->model($options['model'], $relationship . '_model'); |
|
| 509 | + } |
|
| 510 | + else{ |
|
| 511 | + Loader::model($options['model'], $relationship . '_model'); |
|
| 512 | + } |
|
| 513 | + if (is_object($row)) |
|
| 514 | + { |
|
| 515 | + $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
|
| 516 | + } |
|
| 517 | + else |
|
| 518 | + { |
|
| 519 | + $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + } |
|
| 523 | + return $row; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /* -------------------------------------------------------------- |
|
| 527 | 527 | * UTILITY METHODS |
| 528 | 528 | * ------------------------------------------------------------ */ |
| 529 | 529 | |
| 530 | - /** |
|
| 531 | - * Retrieve and generate a form_dropdown friendly array |
|
| 532 | - */ |
|
| 533 | - public function dropdown() |
|
| 534 | - { |
|
| 535 | - $args = func_get_args(); |
|
| 536 | - if(count($args) == 2) |
|
| 537 | - { |
|
| 538 | - list($key, $value) = $args; |
|
| 539 | - } |
|
| 540 | - else |
|
| 541 | - { |
|
| 542 | - $key = $this->primary_key; |
|
| 543 | - $value = $args[0]; |
|
| 544 | - } |
|
| 545 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 546 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 547 | - { |
|
| 548 | - $this->_database->where($this->soft_delete_key, FALSE); |
|
| 549 | - } |
|
| 550 | - $result = $this->_database->select(array($key, $value)) |
|
| 551 | - ->from($this->_table) |
|
| 552 | - ->getAll(); |
|
| 553 | - $options = array(); |
|
| 554 | - foreach ($result as $row) |
|
| 555 | - { |
|
| 556 | - $options[$row->{$key}] = $row->{$value}; |
|
| 557 | - } |
|
| 558 | - $options = $this->trigger('after_dropdown', $options); |
|
| 559 | - return $options; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 564 | - */ |
|
| 565 | - public function count_by() |
|
| 566 | - { |
|
| 567 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 568 | - { |
|
| 569 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 570 | - } |
|
| 571 | - $where = func_get_args(); |
|
| 572 | - $this->_set_where($where); |
|
| 573 | - $this->_database->from($this->_table)->getAll(); |
|
| 574 | - return $this->_database->numRows(); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Fetch a total count of rows, disregarding any previous conditions |
|
| 579 | - */ |
|
| 580 | - public function count_all() |
|
| 581 | - { |
|
| 582 | - if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 583 | - { |
|
| 584 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 585 | - } |
|
| 586 | - $this->_database->from($this->_table)->getAll(); |
|
| 587 | - return $this->_database->numRows(); |
|
| 588 | - } |
|
| 530 | + /** |
|
| 531 | + * Retrieve and generate a form_dropdown friendly array |
|
| 532 | + */ |
|
| 533 | + public function dropdown() |
|
| 534 | + { |
|
| 535 | + $args = func_get_args(); |
|
| 536 | + if(count($args) == 2) |
|
| 537 | + { |
|
| 538 | + list($key, $value) = $args; |
|
| 539 | + } |
|
| 540 | + else |
|
| 541 | + { |
|
| 542 | + $key = $this->primary_key; |
|
| 543 | + $value = $args[0]; |
|
| 544 | + } |
|
| 545 | + $this->trigger('before_dropdown', array( $key, $value )); |
|
| 546 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 547 | + { |
|
| 548 | + $this->_database->where($this->soft_delete_key, FALSE); |
|
| 549 | + } |
|
| 550 | + $result = $this->_database->select(array($key, $value)) |
|
| 551 | + ->from($this->_table) |
|
| 552 | + ->getAll(); |
|
| 553 | + $options = array(); |
|
| 554 | + foreach ($result as $row) |
|
| 555 | + { |
|
| 556 | + $options[$row->{$key}] = $row->{$value}; |
|
| 557 | + } |
|
| 558 | + $options = $this->trigger('after_dropdown', $options); |
|
| 559 | + return $options; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Fetch a count of rows based on an arbitrary WHERE call. |
|
| 564 | + */ |
|
| 565 | + public function count_by() |
|
| 566 | + { |
|
| 567 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 568 | + { |
|
| 569 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 570 | + } |
|
| 571 | + $where = func_get_args(); |
|
| 572 | + $this->_set_where($where); |
|
| 573 | + $this->_database->from($this->_table)->getAll(); |
|
| 574 | + return $this->_database->numRows(); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Fetch a total count of rows, disregarding any previous conditions |
|
| 579 | + */ |
|
| 580 | + public function count_all() |
|
| 581 | + { |
|
| 582 | + if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
|
| 583 | + { |
|
| 584 | + $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 585 | + } |
|
| 586 | + $this->_database->from($this->_table)->getAll(); |
|
| 587 | + return $this->_database->numRows(); |
|
| 588 | + } |
|
| 589 | 589 | |
| 590 | 590 | /** |
| 591 | - * Enabled cache temporary |
|
| 592 | - */ |
|
| 591 | + * Enabled cache temporary |
|
| 592 | + */ |
|
| 593 | 593 | public function cached($ttl = 0){ |
| 594 | 594 | if($ttl > 0){ |
| 595 | 595 | $this->_database = $this->_database->cached($ttl); |
@@ -597,385 +597,385 @@ discard block |
||
| 597 | 597 | return $this; |
| 598 | 598 | } |
| 599 | 599 | |
| 600 | - /** |
|
| 601 | - * Tell the class to skip the insert validation |
|
| 602 | - */ |
|
| 603 | - public function skip_validation() |
|
| 604 | - { |
|
| 605 | - $this->skip_validation = TRUE; |
|
| 606 | - return $this; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Get the skip validation status |
|
| 611 | - */ |
|
| 612 | - public function get_skip_validation() |
|
| 613 | - { |
|
| 614 | - return $this->skip_validation; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Return the next auto increment of the table. Only tested on MySQL. |
|
| 619 | - */ |
|
| 620 | - public function get_next_id() |
|
| 621 | - { |
|
| 622 | - return (int) $this->_database->select('AUTO_INCREMENT') |
|
| 623 | - ->from('information_schema.TABLES') |
|
| 624 | - ->where('TABLE_NAME', $this->_table) |
|
| 625 | - ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Getter for the table name |
|
| 630 | - */ |
|
| 631 | - public function table() |
|
| 632 | - { |
|
| 633 | - return $this->_table; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /* -------------------------------------------------------------- |
|
| 600 | + /** |
|
| 601 | + * Tell the class to skip the insert validation |
|
| 602 | + */ |
|
| 603 | + public function skip_validation() |
|
| 604 | + { |
|
| 605 | + $this->skip_validation = TRUE; |
|
| 606 | + return $this; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Get the skip validation status |
|
| 611 | + */ |
|
| 612 | + public function get_skip_validation() |
|
| 613 | + { |
|
| 614 | + return $this->skip_validation; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Return the next auto increment of the table. Only tested on MySQL. |
|
| 619 | + */ |
|
| 620 | + public function get_next_id() |
|
| 621 | + { |
|
| 622 | + return (int) $this->_database->select('AUTO_INCREMENT') |
|
| 623 | + ->from('information_schema.TABLES') |
|
| 624 | + ->where('TABLE_NAME', $this->_table) |
|
| 625 | + ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Getter for the table name |
|
| 630 | + */ |
|
| 631 | + public function table() |
|
| 632 | + { |
|
| 633 | + return $this->_table; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /* -------------------------------------------------------------- |
|
| 637 | 637 | * GLOBAL SCOPES |
| 638 | 638 | * ------------------------------------------------------------ */ |
| 639 | 639 | |
| 640 | - /** |
|
| 641 | - * Return the next call as an array rather than an object |
|
| 642 | - */ |
|
| 643 | - public function as_array() |
|
| 644 | - { |
|
| 645 | - $this->_temporary_return_type = 'array'; |
|
| 646 | - return $this; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * Return the next call as an object rather than an array |
|
| 651 | - */ |
|
| 652 | - public function as_object() |
|
| 653 | - { |
|
| 654 | - $this->_temporary_return_type = 'object'; |
|
| 655 | - return $this; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Don't care about soft deleted rows on the next call |
|
| 660 | - */ |
|
| 661 | - public function with_deleted() |
|
| 662 | - { |
|
| 663 | - $this->_temporary_with_deleted = TRUE; |
|
| 664 | - return $this; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - /** |
|
| 668 | - * Only get deleted rows on the next call |
|
| 669 | - */ |
|
| 670 | - public function only_deleted() |
|
| 671 | - { |
|
| 672 | - $this->_temporary_only_deleted = TRUE; |
|
| 673 | - return $this; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /* -------------------------------------------------------------- |
|
| 640 | + /** |
|
| 641 | + * Return the next call as an array rather than an object |
|
| 642 | + */ |
|
| 643 | + public function as_array() |
|
| 644 | + { |
|
| 645 | + $this->_temporary_return_type = 'array'; |
|
| 646 | + return $this; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * Return the next call as an object rather than an array |
|
| 651 | + */ |
|
| 652 | + public function as_object() |
|
| 653 | + { |
|
| 654 | + $this->_temporary_return_type = 'object'; |
|
| 655 | + return $this; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Don't care about soft deleted rows on the next call |
|
| 660 | + */ |
|
| 661 | + public function with_deleted() |
|
| 662 | + { |
|
| 663 | + $this->_temporary_with_deleted = TRUE; |
|
| 664 | + return $this; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + /** |
|
| 668 | + * Only get deleted rows on the next call |
|
| 669 | + */ |
|
| 670 | + public function only_deleted() |
|
| 671 | + { |
|
| 672 | + $this->_temporary_only_deleted = TRUE; |
|
| 673 | + return $this; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /* -------------------------------------------------------------- |
|
| 677 | 677 | * OBSERVERS |
| 678 | 678 | * ------------------------------------------------------------ */ |
| 679 | 679 | |
| 680 | - /** |
|
| 681 | - * MySQL DATETIME created_at and updated_at |
|
| 682 | - */ |
|
| 683 | - public function created_at($row) |
|
| 684 | - { |
|
| 685 | - if (is_object($row)) |
|
| 686 | - { |
|
| 687 | - $row->created_at = date('Y-m-d H:i:s'); |
|
| 688 | - } |
|
| 689 | - else |
|
| 690 | - { |
|
| 691 | - $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - return $row; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - public function updated_at($row) |
|
| 698 | - { |
|
| 699 | - if (is_object($row)) |
|
| 700 | - { |
|
| 701 | - $row->updated_at = date('Y-m-d H:i:s'); |
|
| 702 | - } |
|
| 703 | - else |
|
| 704 | - { |
|
| 705 | - $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 706 | - } |
|
| 707 | - return $row; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Serialises data for you automatically, allowing you to pass |
|
| 712 | - * through objects and let it handle the serialisation in the background |
|
| 713 | - */ |
|
| 714 | - public function serialize($row) |
|
| 715 | - { |
|
| 716 | - foreach ($this->callback_parameters as $column) |
|
| 717 | - { |
|
| 718 | - $row[$column] = serialize($row[$column]); |
|
| 719 | - } |
|
| 720 | - return $row; |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - public function unserialize($row) |
|
| 724 | - { |
|
| 725 | - foreach ($this->callback_parameters as $column) |
|
| 726 | - { |
|
| 727 | - if (is_array($row)) |
|
| 728 | - { |
|
| 729 | - $row[$column] = unserialize($row[$column]); |
|
| 730 | - } |
|
| 731 | - else |
|
| 732 | - { |
|
| 733 | - $row->$column = unserialize($row->$column); |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - return $row; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Protect attributes by removing them from $row array |
|
| 741 | - */ |
|
| 742 | - public function protect_attributes($row) |
|
| 743 | - { |
|
| 744 | - foreach ($this->protected_attributes as $attr) |
|
| 745 | - { |
|
| 746 | - if (is_object($row)) |
|
| 747 | - { |
|
| 680 | + /** |
|
| 681 | + * MySQL DATETIME created_at and updated_at |
|
| 682 | + */ |
|
| 683 | + public function created_at($row) |
|
| 684 | + { |
|
| 685 | + if (is_object($row)) |
|
| 686 | + { |
|
| 687 | + $row->created_at = date('Y-m-d H:i:s'); |
|
| 688 | + } |
|
| 689 | + else |
|
| 690 | + { |
|
| 691 | + $row['created_at'] = date('Y-m-d H:i:s'); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + return $row; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + public function updated_at($row) |
|
| 698 | + { |
|
| 699 | + if (is_object($row)) |
|
| 700 | + { |
|
| 701 | + $row->updated_at = date('Y-m-d H:i:s'); |
|
| 702 | + } |
|
| 703 | + else |
|
| 704 | + { |
|
| 705 | + $row['updated_at'] = date('Y-m-d H:i:s'); |
|
| 706 | + } |
|
| 707 | + return $row; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Serialises data for you automatically, allowing you to pass |
|
| 712 | + * through objects and let it handle the serialisation in the background |
|
| 713 | + */ |
|
| 714 | + public function serialize($row) |
|
| 715 | + { |
|
| 716 | + foreach ($this->callback_parameters as $column) |
|
| 717 | + { |
|
| 718 | + $row[$column] = serialize($row[$column]); |
|
| 719 | + } |
|
| 720 | + return $row; |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + public function unserialize($row) |
|
| 724 | + { |
|
| 725 | + foreach ($this->callback_parameters as $column) |
|
| 726 | + { |
|
| 727 | + if (is_array($row)) |
|
| 728 | + { |
|
| 729 | + $row[$column] = unserialize($row[$column]); |
|
| 730 | + } |
|
| 731 | + else |
|
| 732 | + { |
|
| 733 | + $row->$column = unserialize($row->$column); |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + return $row; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Protect attributes by removing them from $row array |
|
| 741 | + */ |
|
| 742 | + public function protect_attributes($row) |
|
| 743 | + { |
|
| 744 | + foreach ($this->protected_attributes as $attr) |
|
| 745 | + { |
|
| 746 | + if (is_object($row)) |
|
| 747 | + { |
|
| 748 | 748 | if(isset($row->$attr)){ |
| 749 | 749 | unset($row->$attr); |
| 750 | 750 | } |
| 751 | - } |
|
| 752 | - else |
|
| 753 | - { |
|
| 751 | + } |
|
| 752 | + else |
|
| 753 | + { |
|
| 754 | 754 | if(isset($row[$attr])){ |
| 755 | 755 | unset($row[$attr]); |
| 756 | 756 | } |
| 757 | - } |
|
| 758 | - } |
|
| 759 | - return $row; |
|
| 760 | - } |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + return $row; |
|
| 760 | + } |
|
| 761 | 761 | |
| 762 | 762 | /** |
| 763 | - * Return the database instance |
|
| 764 | - * @return Database the database instance |
|
| 765 | - */ |
|
| 766 | - public function getDatabaseInstance(){ |
|
| 767 | - return $this->_database; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - /** |
|
| 771 | - * set the Database instance for future use |
|
| 772 | - * @param Database $db the database object |
|
| 773 | - */ |
|
| 774 | - public function setDatabaseInstance($db){ |
|
| 775 | - $this->_database = $db; |
|
| 776 | - if($this->dbCacheTime > 0){ |
|
| 777 | - $this->_database->setCache($this->dbCacheTime); |
|
| 778 | - } |
|
| 779 | - return $this; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Return the loader instance |
|
| 784 | - * @return Loader the loader instance |
|
| 785 | - */ |
|
| 786 | - public function getLoader(){ |
|
| 787 | - return $this->loaderInstance; |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * set the loader instance for future use |
|
| 792 | - * @param Loader $loader the loader object |
|
| 793 | - */ |
|
| 794 | - public function setLoader($loader){ |
|
| 795 | - $this->loaderInstance = $loader; |
|
| 796 | - return $this; |
|
| 797 | - } |
|
| 798 | - |
|
| 799 | - /** |
|
| 800 | - * Return the FormValidation instance |
|
| 801 | - * @return FormValidation the form validation instance |
|
| 802 | - */ |
|
| 803 | - public function getFormValidation(){ |
|
| 804 | - return $this->formValidationInstance; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * set the form validation instance for future use |
|
| 809 | - * @param FormValidation $fv the form validation object |
|
| 810 | - */ |
|
| 811 | - public function setFormValidation($fv){ |
|
| 812 | - $this->formValidationInstance = $fv; |
|
| 813 | - return $this; |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - /* -------------------------------------------------------------- |
|
| 763 | + * Return the database instance |
|
| 764 | + * @return Database the database instance |
|
| 765 | + */ |
|
| 766 | + public function getDatabaseInstance(){ |
|
| 767 | + return $this->_database; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + /** |
|
| 771 | + * set the Database instance for future use |
|
| 772 | + * @param Database $db the database object |
|
| 773 | + */ |
|
| 774 | + public function setDatabaseInstance($db){ |
|
| 775 | + $this->_database = $db; |
|
| 776 | + if($this->dbCacheTime > 0){ |
|
| 777 | + $this->_database->setCache($this->dbCacheTime); |
|
| 778 | + } |
|
| 779 | + return $this; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Return the loader instance |
|
| 784 | + * @return Loader the loader instance |
|
| 785 | + */ |
|
| 786 | + public function getLoader(){ |
|
| 787 | + return $this->loaderInstance; |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * set the loader instance for future use |
|
| 792 | + * @param Loader $loader the loader object |
|
| 793 | + */ |
|
| 794 | + public function setLoader($loader){ |
|
| 795 | + $this->loaderInstance = $loader; |
|
| 796 | + return $this; |
|
| 797 | + } |
|
| 798 | + |
|
| 799 | + /** |
|
| 800 | + * Return the FormValidation instance |
|
| 801 | + * @return FormValidation the form validation instance |
|
| 802 | + */ |
|
| 803 | + public function getFormValidation(){ |
|
| 804 | + return $this->formValidationInstance; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * set the form validation instance for future use |
|
| 809 | + * @param FormValidation $fv the form validation object |
|
| 810 | + */ |
|
| 811 | + public function setFormValidation($fv){ |
|
| 812 | + $this->formValidationInstance = $fv; |
|
| 813 | + return $this; |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + /* -------------------------------------------------------------- |
|
| 817 | 817 | * QUERY BUILDER DIRECT ACCESS METHODS |
| 818 | 818 | * ------------------------------------------------------------ */ |
| 819 | 819 | |
| 820 | - /** |
|
| 821 | - * A wrapper to $this->_database->orderBy() |
|
| 822 | - */ |
|
| 823 | - public function order_by($criteria, $order = 'ASC') |
|
| 824 | - { |
|
| 825 | - if ( is_array($criteria) ) |
|
| 826 | - { |
|
| 827 | - foreach ($criteria as $key => $value) |
|
| 828 | - { |
|
| 829 | - $this->_database->orderBy($key, $value); |
|
| 830 | - } |
|
| 831 | - } |
|
| 832 | - else |
|
| 833 | - { |
|
| 834 | - $this->_database->orderBy($criteria, $order); |
|
| 835 | - } |
|
| 836 | - return $this; |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - /** |
|
| 840 | - * A wrapper to $this->_database->limit() |
|
| 841 | - */ |
|
| 842 | - public function limit($offset = 0, $limit = 10) |
|
| 843 | - { |
|
| 844 | - $this->_database->limit($offset, $limit); |
|
| 845 | - return $this; |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - /* -------------------------------------------------------------- |
|
| 820 | + /** |
|
| 821 | + * A wrapper to $this->_database->orderBy() |
|
| 822 | + */ |
|
| 823 | + public function order_by($criteria, $order = 'ASC') |
|
| 824 | + { |
|
| 825 | + if ( is_array($criteria) ) |
|
| 826 | + { |
|
| 827 | + foreach ($criteria as $key => $value) |
|
| 828 | + { |
|
| 829 | + $this->_database->orderBy($key, $value); |
|
| 830 | + } |
|
| 831 | + } |
|
| 832 | + else |
|
| 833 | + { |
|
| 834 | + $this->_database->orderBy($criteria, $order); |
|
| 835 | + } |
|
| 836 | + return $this; |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + /** |
|
| 840 | + * A wrapper to $this->_database->limit() |
|
| 841 | + */ |
|
| 842 | + public function limit($offset = 0, $limit = 10) |
|
| 843 | + { |
|
| 844 | + $this->_database->limit($offset, $limit); |
|
| 845 | + return $this; |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + /* -------------------------------------------------------------- |
|
| 849 | 849 | * INTERNAL METHODS |
| 850 | 850 | * ------------------------------------------------------------ */ |
| 851 | 851 | |
| 852 | - /** |
|
| 853 | - * Trigger an event and call its observers. Pass through the event name |
|
| 854 | - * (which looks for an instance variable $this->event_name), an array of |
|
| 855 | - * parameters to pass through and an optional 'last in interation' boolean |
|
| 856 | - */ |
|
| 857 | - protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 858 | - { |
|
| 859 | - if (isset($this->$event) && is_array($this->$event)) |
|
| 860 | - { |
|
| 861 | - foreach ($this->$event as $method) |
|
| 862 | - { |
|
| 863 | - if (strpos($method, '(')) |
|
| 864 | - { |
|
| 865 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 866 | - |
|
| 867 | - $method = $matches[1]; |
|
| 868 | - $this->callback_parameters = explode(',', $matches[3]); |
|
| 869 | - } |
|
| 870 | - $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 871 | - } |
|
| 872 | - } |
|
| 873 | - return $data; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - /** |
|
| 877 | - * Run validation on the passed data |
|
| 878 | - */ |
|
| 879 | - protected function validate(array $data) |
|
| 880 | - { |
|
| 881 | - if($this->skip_validation) |
|
| 882 | - { |
|
| 883 | - return $data; |
|
| 884 | - } |
|
| 885 | - if(!empty($this->validate)) |
|
| 886 | - { |
|
| 887 | - $fv = null; |
|
| 888 | - if(is_object($this->formValidationInstance)){ |
|
| 889 | - $fv = $this->formValidationInstance; |
|
| 890 | - } |
|
| 891 | - else{ |
|
| 892 | - Loader::library('FormValidation'); |
|
| 893 | - $fv = $this->formvalidation; |
|
| 894 | - $this->setFormValidation($fv); |
|
| 895 | - } |
|
| 852 | + /** |
|
| 853 | + * Trigger an event and call its observers. Pass through the event name |
|
| 854 | + * (which looks for an instance variable $this->event_name), an array of |
|
| 855 | + * parameters to pass through and an optional 'last in interation' boolean |
|
| 856 | + */ |
|
| 857 | + protected function trigger($event, $data = FALSE, $last = TRUE) |
|
| 858 | + { |
|
| 859 | + if (isset($this->$event) && is_array($this->$event)) |
|
| 860 | + { |
|
| 861 | + foreach ($this->$event as $method) |
|
| 862 | + { |
|
| 863 | + if (strpos($method, '(')) |
|
| 864 | + { |
|
| 865 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
| 866 | + |
|
| 867 | + $method = $matches[1]; |
|
| 868 | + $this->callback_parameters = explode(',', $matches[3]); |
|
| 869 | + } |
|
| 870 | + $data = call_user_func_array(array($this, $method), array($data, $last)); |
|
| 871 | + } |
|
| 872 | + } |
|
| 873 | + return $data; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + /** |
|
| 877 | + * Run validation on the passed data |
|
| 878 | + */ |
|
| 879 | + protected function validate(array $data) |
|
| 880 | + { |
|
| 881 | + if($this->skip_validation) |
|
| 882 | + { |
|
| 883 | + return $data; |
|
| 884 | + } |
|
| 885 | + if(!empty($this->validate)) |
|
| 886 | + { |
|
| 887 | + $fv = null; |
|
| 888 | + if(is_object($this->formValidationInstance)){ |
|
| 889 | + $fv = $this->formValidationInstance; |
|
| 890 | + } |
|
| 891 | + else{ |
|
| 892 | + Loader::library('FormValidation'); |
|
| 893 | + $fv = $this->formvalidation; |
|
| 894 | + $this->setFormValidation($fv); |
|
| 895 | + } |
|
| 896 | 896 | |
| 897 | - $fv->setData($data); |
|
| 898 | - $fv->setRules($this->validate); |
|
| 899 | - |
|
| 900 | - if ($fv->run()) |
|
| 901 | - { |
|
| 902 | - return $data; |
|
| 903 | - } |
|
| 904 | - else |
|
| 905 | - { |
|
| 906 | - return FALSE; |
|
| 907 | - } |
|
| 908 | - } |
|
| 909 | - else |
|
| 910 | - { |
|
| 911 | - return $data; |
|
| 912 | - } |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - |
|
| 916 | - /** |
|
| 917 | - * Set WHERE parameters, cleverly |
|
| 918 | - */ |
|
| 919 | - protected function _set_where($params) |
|
| 920 | - { |
|
| 921 | - if (count($params) == 1 && is_array($params[0])) |
|
| 922 | - { |
|
| 923 | - foreach ($params[0] as $field => $filter) |
|
| 924 | - { |
|
| 925 | - if (is_array($filter)) |
|
| 926 | - { |
|
| 927 | - $this->_database->in($field, $filter); |
|
| 928 | - } |
|
| 929 | - else |
|
| 930 | - { |
|
| 931 | - if (is_int($field)) |
|
| 932 | - { |
|
| 933 | - $this->_database->where($filter); |
|
| 934 | - } |
|
| 935 | - else |
|
| 936 | - { |
|
| 937 | - $this->_database->where($field, $filter); |
|
| 938 | - } |
|
| 939 | - } |
|
| 940 | - } |
|
| 941 | - } |
|
| 942 | - else if (count($params) == 1) |
|
| 943 | - { |
|
| 944 | - $this->_database->where($params[0]); |
|
| 945 | - } |
|
| 946 | - else if(count($params) == 2) |
|
| 947 | - { |
|
| 948 | - if (is_array($params[1])) |
|
| 949 | - { |
|
| 950 | - $this->_database->in($params[0], $params[1]); |
|
| 951 | - } |
|
| 952 | - else |
|
| 953 | - { |
|
| 954 | - $this->_database->where($params[0], $params[1]); |
|
| 955 | - } |
|
| 956 | - } |
|
| 957 | - else if(count($params) == 3) |
|
| 958 | - { |
|
| 959 | - $this->_database->where($params[0], $params[1], $params[2]); |
|
| 960 | - } |
|
| 961 | - else |
|
| 962 | - { |
|
| 963 | - if (is_array($params[1])) |
|
| 964 | - { |
|
| 965 | - $this->_database->in($params[0], $params[1]); |
|
| 966 | - } |
|
| 967 | - else |
|
| 968 | - { |
|
| 969 | - $this->_database->where($params[0], $params[1]); |
|
| 970 | - } |
|
| 971 | - } |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - /** |
|
| 897 | + $fv->setData($data); |
|
| 898 | + $fv->setRules($this->validate); |
|
| 899 | + |
|
| 900 | + if ($fv->run()) |
|
| 901 | + { |
|
| 902 | + return $data; |
|
| 903 | + } |
|
| 904 | + else |
|
| 905 | + { |
|
| 906 | + return FALSE; |
|
| 907 | + } |
|
| 908 | + } |
|
| 909 | + else |
|
| 910 | + { |
|
| 911 | + return $data; |
|
| 912 | + } |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + |
|
| 916 | + /** |
|
| 917 | + * Set WHERE parameters, cleverly |
|
| 918 | + */ |
|
| 919 | + protected function _set_where($params) |
|
| 920 | + { |
|
| 921 | + if (count($params) == 1 && is_array($params[0])) |
|
| 922 | + { |
|
| 923 | + foreach ($params[0] as $field => $filter) |
|
| 924 | + { |
|
| 925 | + if (is_array($filter)) |
|
| 926 | + { |
|
| 927 | + $this->_database->in($field, $filter); |
|
| 928 | + } |
|
| 929 | + else |
|
| 930 | + { |
|
| 931 | + if (is_int($field)) |
|
| 932 | + { |
|
| 933 | + $this->_database->where($filter); |
|
| 934 | + } |
|
| 935 | + else |
|
| 936 | + { |
|
| 937 | + $this->_database->where($field, $filter); |
|
| 938 | + } |
|
| 939 | + } |
|
| 940 | + } |
|
| 941 | + } |
|
| 942 | + else if (count($params) == 1) |
|
| 943 | + { |
|
| 944 | + $this->_database->where($params[0]); |
|
| 945 | + } |
|
| 946 | + else if(count($params) == 2) |
|
| 947 | + { |
|
| 948 | + if (is_array($params[1])) |
|
| 949 | + { |
|
| 950 | + $this->_database->in($params[0], $params[1]); |
|
| 951 | + } |
|
| 952 | + else |
|
| 953 | + { |
|
| 954 | + $this->_database->where($params[0], $params[1]); |
|
| 955 | + } |
|
| 956 | + } |
|
| 957 | + else if(count($params) == 3) |
|
| 958 | + { |
|
| 959 | + $this->_database->where($params[0], $params[1], $params[2]); |
|
| 960 | + } |
|
| 961 | + else |
|
| 962 | + { |
|
| 963 | + if (is_array($params[1])) |
|
| 964 | + { |
|
| 965 | + $this->_database->in($params[0], $params[1]); |
|
| 966 | + } |
|
| 967 | + else |
|
| 968 | + { |
|
| 969 | + $this->_database->where($params[0], $params[1]); |
|
| 970 | + } |
|
| 971 | + } |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + /** |
|
| 975 | 975 | Shortcut to controller |
| 976 | - */ |
|
| 977 | - public function __get($key){ |
|
| 978 | - return get_instance()->{$key}; |
|
| 979 | - } |
|
| 976 | + */ |
|
| 977 | + public function __get($key){ |
|
| 978 | + return get_instance()->{$key}; |
|
| 979 | + } |
|
| 980 | 980 | |
| 981 | - } |
|
| 981 | + } |
|
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
| 34 | 34 | */ |
| 35 | 35 | |
| 36 | - class Model{ |
|
| 36 | + class Model { |
|
| 37 | 37 | |
| 38 | 38 | /* -------------------------------------------------------------- |
| 39 | 39 | * VARIABLES |
@@ -140,13 +140,13 @@ discard block |
||
| 140 | 140 | * Initialise the model, tie into the CodeIgniter superobject and |
| 141 | 141 | * try our best to guess the table name. |
| 142 | 142 | */ |
| 143 | - public function __construct(Database $db = null){ |
|
| 144 | - if(is_object($db)){ |
|
| 143 | + public function __construct(Database $db = null) { |
|
| 144 | + if (is_object($db)) { |
|
| 145 | 145 | $this->setDatabaseInstance($db); |
| 146 | 146 | } |
| 147 | - else{ |
|
| 147 | + else { |
|
| 148 | 148 | $obj = & get_instance(); |
| 149 | - if(isset($obj->database) && is_object($obj->database)){ |
|
| 149 | + if (isset($obj->database) && is_object($obj->database)) { |
|
| 150 | 150 | /** |
| 151 | 151 | * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
| 152 | 152 | * to prevent duplication |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | |
| 183 | 183 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 184 | 184 | { |
| 185 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 185 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | $this->_set_where($where); |
@@ -224,9 +224,9 @@ discard block |
||
| 224 | 224 | $this->trigger('before_get'); |
| 225 | 225 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 226 | 226 | { |
| 227 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 227 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 228 | 228 | } |
| 229 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
| 229 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
| 230 | 230 | $result = $this->_database->from($this->_table)->getAll($type); |
| 231 | 231 | $this->_temporary_return_type = $this->return_type; |
| 232 | 232 | |
@@ -332,13 +332,13 @@ discard block |
||
| 332 | 332 | { |
| 333 | 333 | $args = func_get_args(); |
| 334 | 334 | $data = array(); |
| 335 | - if(count($args) == 2){ |
|
| 336 | - if(is_array($args[1])){ |
|
| 335 | + if (count($args) == 2) { |
|
| 336 | + if (is_array($args[1])) { |
|
| 337 | 337 | $data = array_pop($args); |
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | - else if(count($args) == 3){ |
|
| 341 | - if(is_array($args[2])){ |
|
| 340 | + else if (count($args) == 3) { |
|
| 341 | + if (is_array($args[2])) { |
|
| 342 | 342 | $data = array_pop($args); |
| 343 | 343 | } |
| 344 | 344 | } |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | $this->_database->where($this->primary_key, $id); |
| 377 | 377 | if ($this->soft_delete) |
| 378 | 378 | { |
| 379 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 379 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
| 380 | 380 | } |
| 381 | 381 | else |
| 382 | 382 | { |
@@ -397,7 +397,7 @@ discard block |
||
| 397 | 397 | $this->_set_where($where); |
| 398 | 398 | if ($this->soft_delete) |
| 399 | 399 | { |
| 400 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 400 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
| 401 | 401 | } |
| 402 | 402 | else |
| 403 | 403 | { |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | $this->_database->in($this->primary_key, $primary_values); |
| 417 | 417 | if ($this->soft_delete) |
| 418 | 418 | { |
| 419 | - $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
|
| 419 | + $result = $this->_database->from($this->_table)->update(array($this->soft_delete_key => TRUE)); |
|
| 420 | 420 | } |
| 421 | 421 | else |
| 422 | 422 | { |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | if (is_string($value)) |
| 463 | 463 | { |
| 464 | 464 | $relationship = $value; |
| 465 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
| 465 | + $options = array('primary_key' => $value . '_id', 'model' => $value . '_model'); |
|
| 466 | 466 | } |
| 467 | 467 | else |
| 468 | 468 | { |
@@ -472,10 +472,10 @@ discard block |
||
| 472 | 472 | |
| 473 | 473 | if (in_array($relationship, $this->_with)) |
| 474 | 474 | { |
| 475 | - if(is_object($this->loaderInstance)){ |
|
| 475 | + if (is_object($this->loaderInstance)) { |
|
| 476 | 476 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 477 | 477 | } |
| 478 | - else{ |
|
| 478 | + else { |
|
| 479 | 479 | Loader::model($options['model'], $relationship . '_model'); |
| 480 | 480 | } |
| 481 | 481 | if (is_object($row)) |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | if (is_string($value)) |
| 495 | 495 | { |
| 496 | 496 | $relationship = $value; |
| 497 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
| 497 | + $options = array('primary_key' => $this->_table . '_id', 'model' => $value . '_model'); |
|
| 498 | 498 | } |
| 499 | 499 | else |
| 500 | 500 | { |
@@ -504,10 +504,10 @@ discard block |
||
| 504 | 504 | |
| 505 | 505 | if (in_array($relationship, $this->_with)) |
| 506 | 506 | { |
| 507 | - if(is_object($this->loaderInstance)){ |
|
| 507 | + if (is_object($this->loaderInstance)) { |
|
| 508 | 508 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 509 | 509 | } |
| 510 | - else{ |
|
| 510 | + else { |
|
| 511 | 511 | Loader::model($options['model'], $relationship . '_model'); |
| 512 | 512 | } |
| 513 | 513 | if (is_object($row)) |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | public function dropdown() |
| 534 | 534 | { |
| 535 | 535 | $args = func_get_args(); |
| 536 | - if(count($args) == 2) |
|
| 536 | + if (count($args) == 2) |
|
| 537 | 537 | { |
| 538 | 538 | list($key, $value) = $args; |
| 539 | 539 | } |
@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | $key = $this->primary_key; |
| 543 | 543 | $value = $args[0]; |
| 544 | 544 | } |
| 545 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
| 545 | + $this->trigger('before_dropdown', array($key, $value)); |
|
| 546 | 546 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 547 | 547 | { |
| 548 | 548 | $this->_database->where($this->soft_delete_key, FALSE); |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | { |
| 567 | 567 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 568 | 568 | { |
| 569 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 569 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 570 | 570 | } |
| 571 | 571 | $where = func_get_args(); |
| 572 | 572 | $this->_set_where($where); |
@@ -581,7 +581,7 @@ discard block |
||
| 581 | 581 | { |
| 582 | 582 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
| 583 | 583 | { |
| 584 | - $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
| 584 | + $this->_database->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
| 585 | 585 | } |
| 586 | 586 | $this->_database->from($this->_table)->getAll(); |
| 587 | 587 | return $this->_database->numRows(); |
@@ -590,8 +590,8 @@ discard block |
||
| 590 | 590 | /** |
| 591 | 591 | * Enabled cache temporary |
| 592 | 592 | */ |
| 593 | - public function cached($ttl = 0){ |
|
| 594 | - if($ttl > 0){ |
|
| 593 | + public function cached($ttl = 0) { |
|
| 594 | + if ($ttl > 0) { |
|
| 595 | 595 | $this->_database = $this->_database->cached($ttl); |
| 596 | 596 | } |
| 597 | 597 | return $this; |
@@ -745,13 +745,13 @@ discard block |
||
| 745 | 745 | { |
| 746 | 746 | if (is_object($row)) |
| 747 | 747 | { |
| 748 | - if(isset($row->$attr)){ |
|
| 748 | + if (isset($row->$attr)) { |
|
| 749 | 749 | unset($row->$attr); |
| 750 | 750 | } |
| 751 | 751 | } |
| 752 | 752 | else |
| 753 | 753 | { |
| 754 | - if(isset($row[$attr])){ |
|
| 754 | + if (isset($row[$attr])) { |
|
| 755 | 755 | unset($row[$attr]); |
| 756 | 756 | } |
| 757 | 757 | } |
@@ -763,7 +763,7 @@ discard block |
||
| 763 | 763 | * Return the database instance |
| 764 | 764 | * @return Database the database instance |
| 765 | 765 | */ |
| 766 | - public function getDatabaseInstance(){ |
|
| 766 | + public function getDatabaseInstance() { |
|
| 767 | 767 | return $this->_database; |
| 768 | 768 | } |
| 769 | 769 | |
@@ -771,9 +771,9 @@ discard block |
||
| 771 | 771 | * set the Database instance for future use |
| 772 | 772 | * @param Database $db the database object |
| 773 | 773 | */ |
| 774 | - public function setDatabaseInstance($db){ |
|
| 774 | + public function setDatabaseInstance($db) { |
|
| 775 | 775 | $this->_database = $db; |
| 776 | - if($this->dbCacheTime > 0){ |
|
| 776 | + if ($this->dbCacheTime > 0) { |
|
| 777 | 777 | $this->_database->setCache($this->dbCacheTime); |
| 778 | 778 | } |
| 779 | 779 | return $this; |
@@ -783,7 +783,7 @@ discard block |
||
| 783 | 783 | * Return the loader instance |
| 784 | 784 | * @return Loader the loader instance |
| 785 | 785 | */ |
| 786 | - public function getLoader(){ |
|
| 786 | + public function getLoader() { |
|
| 787 | 787 | return $this->loaderInstance; |
| 788 | 788 | } |
| 789 | 789 | |
@@ -791,7 +791,7 @@ discard block |
||
| 791 | 791 | * set the loader instance for future use |
| 792 | 792 | * @param Loader $loader the loader object |
| 793 | 793 | */ |
| 794 | - public function setLoader($loader){ |
|
| 794 | + public function setLoader($loader) { |
|
| 795 | 795 | $this->loaderInstance = $loader; |
| 796 | 796 | return $this; |
| 797 | 797 | } |
@@ -800,7 +800,7 @@ discard block |
||
| 800 | 800 | * Return the FormValidation instance |
| 801 | 801 | * @return FormValidation the form validation instance |
| 802 | 802 | */ |
| 803 | - public function getFormValidation(){ |
|
| 803 | + public function getFormValidation() { |
|
| 804 | 804 | return $this->formValidationInstance; |
| 805 | 805 | } |
| 806 | 806 | |
@@ -808,7 +808,7 @@ discard block |
||
| 808 | 808 | * set the form validation instance for future use |
| 809 | 809 | * @param FormValidation $fv the form validation object |
| 810 | 810 | */ |
| 811 | - public function setFormValidation($fv){ |
|
| 811 | + public function setFormValidation($fv) { |
|
| 812 | 812 | $this->formValidationInstance = $fv; |
| 813 | 813 | return $this; |
| 814 | 814 | } |
@@ -822,7 +822,7 @@ discard block |
||
| 822 | 822 | */ |
| 823 | 823 | public function order_by($criteria, $order = 'ASC') |
| 824 | 824 | { |
| 825 | - if ( is_array($criteria) ) |
|
| 825 | + if (is_array($criteria)) |
|
| 826 | 826 | { |
| 827 | 827 | foreach ($criteria as $key => $value) |
| 828 | 828 | { |
@@ -878,17 +878,17 @@ discard block |
||
| 878 | 878 | */ |
| 879 | 879 | protected function validate(array $data) |
| 880 | 880 | { |
| 881 | - if($this->skip_validation) |
|
| 881 | + if ($this->skip_validation) |
|
| 882 | 882 | { |
| 883 | 883 | return $data; |
| 884 | 884 | } |
| 885 | - if(!empty($this->validate)) |
|
| 885 | + if (!empty($this->validate)) |
|
| 886 | 886 | { |
| 887 | 887 | $fv = null; |
| 888 | - if(is_object($this->formValidationInstance)){ |
|
| 888 | + if (is_object($this->formValidationInstance)) { |
|
| 889 | 889 | $fv = $this->formValidationInstance; |
| 890 | 890 | } |
| 891 | - else{ |
|
| 891 | + else { |
|
| 892 | 892 | Loader::library('FormValidation'); |
| 893 | 893 | $fv = $this->formvalidation; |
| 894 | 894 | $this->setFormValidation($fv); |
@@ -943,7 +943,7 @@ discard block |
||
| 943 | 943 | { |
| 944 | 944 | $this->_database->where($params[0]); |
| 945 | 945 | } |
| 946 | - else if(count($params) == 2) |
|
| 946 | + else if (count($params) == 2) |
|
| 947 | 947 | { |
| 948 | 948 | if (is_array($params[1])) |
| 949 | 949 | { |
@@ -954,7 +954,7 @@ discard block |
||
| 954 | 954 | $this->_database->where($params[0], $params[1]); |
| 955 | 955 | } |
| 956 | 956 | } |
| 957 | - else if(count($params) == 3) |
|
| 957 | + else if (count($params) == 3) |
|
| 958 | 958 | { |
| 959 | 959 | $this->_database->where($params[0], $params[1], $params[2]); |
| 960 | 960 | } |
@@ -974,7 +974,7 @@ discard block |
||
| 974 | 974 | /** |
| 975 | 975 | Shortcut to controller |
| 976 | 976 | */ |
| 977 | - public function __get($key){ |
|
| 977 | + public function __get($key) { |
|
| 978 | 978 | return get_instance()->{$key}; |
| 979 | 979 | } |
| 980 | 980 | |
@@ -143,8 +143,7 @@ discard block |
||
| 143 | 143 | public function __construct(Database $db = null){ |
| 144 | 144 | if(is_object($db)){ |
| 145 | 145 | $this->setDatabaseInstance($db); |
| 146 | - } |
|
| 147 | - else{ |
|
| 146 | + } else{ |
|
| 148 | 147 | $obj = & get_instance(); |
| 149 | 148 | if(isset($obj->database) && is_object($obj->database)){ |
| 150 | 149 | /** |
@@ -256,8 +255,7 @@ discard block |
||
| 256 | 255 | $insert_id = $this->_database->insertId(); |
| 257 | 256 | $this->trigger('after_create', $insert_id); |
| 258 | 257 | return $insert_id; |
| 259 | - } |
|
| 260 | - else |
|
| 258 | + } else |
|
| 261 | 259 | { |
| 262 | 260 | return FALSE; |
| 263 | 261 | } |
@@ -294,8 +292,7 @@ discard block |
||
| 294 | 292 | ->update($data, $escape); |
| 295 | 293 | $this->trigger('after_update', array($data, $result)); |
| 296 | 294 | return $result; |
| 297 | - } |
|
| 298 | - else |
|
| 295 | + } else |
|
| 299 | 296 | { |
| 300 | 297 | return FALSE; |
| 301 | 298 | } |
@@ -318,8 +315,7 @@ discard block |
||
| 318 | 315 | ->update($data, $escape); |
| 319 | 316 | $this->trigger('after_update', array($data, $result)); |
| 320 | 317 | return $result; |
| 321 | - } |
|
| 322 | - else |
|
| 318 | + } else |
|
| 323 | 319 | { |
| 324 | 320 | return FALSE; |
| 325 | 321 | } |
@@ -336,8 +332,7 @@ discard block |
||
| 336 | 332 | if(is_array($args[1])){ |
| 337 | 333 | $data = array_pop($args); |
| 338 | 334 | } |
| 339 | - } |
|
| 340 | - else if(count($args) == 3){ |
|
| 335 | + } else if(count($args) == 3){ |
|
| 341 | 336 | if(is_array($args[2])){ |
| 342 | 337 | $data = array_pop($args); |
| 343 | 338 | } |
@@ -349,8 +344,7 @@ discard block |
||
| 349 | 344 | $result = $this->_database->from($this->_table)->update($data); |
| 350 | 345 | $this->trigger('after_update', array($data, $result)); |
| 351 | 346 | return $result; |
| 352 | - } |
|
| 353 | - else |
|
| 347 | + } else |
|
| 354 | 348 | { |
| 355 | 349 | return FALSE; |
| 356 | 350 | } |
@@ -377,8 +371,7 @@ discard block |
||
| 377 | 371 | if ($this->soft_delete) |
| 378 | 372 | { |
| 379 | 373 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
| 380 | - } |
|
| 381 | - else |
|
| 374 | + } else |
|
| 382 | 375 | { |
| 383 | 376 | $result = $this->_database->from($this->_table)->delete(); |
| 384 | 377 | } |
@@ -398,8 +391,7 @@ discard block |
||
| 398 | 391 | if ($this->soft_delete) |
| 399 | 392 | { |
| 400 | 393 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
| 401 | - } |
|
| 402 | - else |
|
| 394 | + } else |
|
| 403 | 395 | { |
| 404 | 396 | $result = $this->_database->from($this->_table)->delete(); |
| 405 | 397 | } |
@@ -417,8 +409,7 @@ discard block |
||
| 417 | 409 | if ($this->soft_delete) |
| 418 | 410 | { |
| 419 | 411 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
| 420 | - } |
|
| 421 | - else |
|
| 412 | + } else |
|
| 422 | 413 | { |
| 423 | 414 | $result = $this->_database->from($this->_table)->delete(); |
| 424 | 415 | } |
@@ -463,8 +454,7 @@ discard block |
||
| 463 | 454 | { |
| 464 | 455 | $relationship = $value; |
| 465 | 456 | $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
| 466 | - } |
|
| 467 | - else |
|
| 457 | + } else |
|
| 468 | 458 | { |
| 469 | 459 | $relationship = $key; |
| 470 | 460 | $options = $value; |
@@ -474,15 +464,13 @@ discard block |
||
| 474 | 464 | { |
| 475 | 465 | if(is_object($this->loaderInstance)){ |
| 476 | 466 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 477 | - } |
|
| 478 | - else{ |
|
| 467 | + } else{ |
|
| 479 | 468 | Loader::model($options['model'], $relationship . '_model'); |
| 480 | 469 | } |
| 481 | 470 | if (is_object($row)) |
| 482 | 471 | { |
| 483 | 472 | $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
| 484 | - } |
|
| 485 | - else |
|
| 473 | + } else |
|
| 486 | 474 | { |
| 487 | 475 | $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
| 488 | 476 | } |
@@ -495,8 +483,7 @@ discard block |
||
| 495 | 483 | { |
| 496 | 484 | $relationship = $value; |
| 497 | 485 | $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
| 498 | - } |
|
| 499 | - else |
|
| 486 | + } else |
|
| 500 | 487 | { |
| 501 | 488 | $relationship = $key; |
| 502 | 489 | $options = $value; |
@@ -506,15 +493,13 @@ discard block |
||
| 506 | 493 | { |
| 507 | 494 | if(is_object($this->loaderInstance)){ |
| 508 | 495 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
| 509 | - } |
|
| 510 | - else{ |
|
| 496 | + } else{ |
|
| 511 | 497 | Loader::model($options['model'], $relationship . '_model'); |
| 512 | 498 | } |
| 513 | 499 | if (is_object($row)) |
| 514 | 500 | { |
| 515 | 501 | $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
| 516 | - } |
|
| 517 | - else |
|
| 502 | + } else |
|
| 518 | 503 | { |
| 519 | 504 | $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
| 520 | 505 | } |
@@ -536,8 +521,7 @@ discard block |
||
| 536 | 521 | if(count($args) == 2) |
| 537 | 522 | { |
| 538 | 523 | list($key, $value) = $args; |
| 539 | - } |
|
| 540 | - else |
|
| 524 | + } else |
|
| 541 | 525 | { |
| 542 | 526 | $key = $this->primary_key; |
| 543 | 527 | $value = $args[0]; |
@@ -685,8 +669,7 @@ discard block |
||
| 685 | 669 | if (is_object($row)) |
| 686 | 670 | { |
| 687 | 671 | $row->created_at = date('Y-m-d H:i:s'); |
| 688 | - } |
|
| 689 | - else |
|
| 672 | + } else |
|
| 690 | 673 | { |
| 691 | 674 | $row['created_at'] = date('Y-m-d H:i:s'); |
| 692 | 675 | } |
@@ -699,8 +682,7 @@ discard block |
||
| 699 | 682 | if (is_object($row)) |
| 700 | 683 | { |
| 701 | 684 | $row->updated_at = date('Y-m-d H:i:s'); |
| 702 | - } |
|
| 703 | - else |
|
| 685 | + } else |
|
| 704 | 686 | { |
| 705 | 687 | $row['updated_at'] = date('Y-m-d H:i:s'); |
| 706 | 688 | } |
@@ -727,8 +709,7 @@ discard block |
||
| 727 | 709 | if (is_array($row)) |
| 728 | 710 | { |
| 729 | 711 | $row[$column] = unserialize($row[$column]); |
| 730 | - } |
|
| 731 | - else |
|
| 712 | + } else |
|
| 732 | 713 | { |
| 733 | 714 | $row->$column = unserialize($row->$column); |
| 734 | 715 | } |
@@ -748,8 +729,7 @@ discard block |
||
| 748 | 729 | if(isset($row->$attr)){ |
| 749 | 730 | unset($row->$attr); |
| 750 | 731 | } |
| 751 | - } |
|
| 752 | - else |
|
| 732 | + } else |
|
| 753 | 733 | { |
| 754 | 734 | if(isset($row[$attr])){ |
| 755 | 735 | unset($row[$attr]); |
@@ -828,8 +808,7 @@ discard block |
||
| 828 | 808 | { |
| 829 | 809 | $this->_database->orderBy($key, $value); |
| 830 | 810 | } |
| 831 | - } |
|
| 832 | - else |
|
| 811 | + } else |
|
| 833 | 812 | { |
| 834 | 813 | $this->_database->orderBy($criteria, $order); |
| 835 | 814 | } |
@@ -887,8 +866,7 @@ discard block |
||
| 887 | 866 | $fv = null; |
| 888 | 867 | if(is_object($this->formValidationInstance)){ |
| 889 | 868 | $fv = $this->formValidationInstance; |
| 890 | - } |
|
| 891 | - else{ |
|
| 869 | + } else{ |
|
| 892 | 870 | Loader::library('FormValidation'); |
| 893 | 871 | $fv = $this->formvalidation; |
| 894 | 872 | $this->setFormValidation($fv); |
@@ -900,13 +878,11 @@ discard block |
||
| 900 | 878 | if ($fv->run()) |
| 901 | 879 | { |
| 902 | 880 | return $data; |
| 903 | - } |
|
| 904 | - else |
|
| 881 | + } else |
|
| 905 | 882 | { |
| 906 | 883 | return FALSE; |
| 907 | 884 | } |
| 908 | - } |
|
| 909 | - else |
|
| 885 | + } else |
|
| 910 | 886 | { |
| 911 | 887 | return $data; |
| 912 | 888 | } |
@@ -925,46 +901,38 @@ discard block |
||
| 925 | 901 | if (is_array($filter)) |
| 926 | 902 | { |
| 927 | 903 | $this->_database->in($field, $filter); |
| 928 | - } |
|
| 929 | - else |
|
| 904 | + } else |
|
| 930 | 905 | { |
| 931 | 906 | if (is_int($field)) |
| 932 | 907 | { |
| 933 | 908 | $this->_database->where($filter); |
| 934 | - } |
|
| 935 | - else |
|
| 909 | + } else |
|
| 936 | 910 | { |
| 937 | 911 | $this->_database->where($field, $filter); |
| 938 | 912 | } |
| 939 | 913 | } |
| 940 | 914 | } |
| 941 | - } |
|
| 942 | - else if (count($params) == 1) |
|
| 915 | + } else if (count($params) == 1) |
|
| 943 | 916 | { |
| 944 | 917 | $this->_database->where($params[0]); |
| 945 | - } |
|
| 946 | - else if(count($params) == 2) |
|
| 918 | + } else if(count($params) == 2) |
|
| 947 | 919 | { |
| 948 | 920 | if (is_array($params[1])) |
| 949 | 921 | { |
| 950 | 922 | $this->_database->in($params[0], $params[1]); |
| 951 | - } |
|
| 952 | - else |
|
| 923 | + } else |
|
| 953 | 924 | { |
| 954 | 925 | $this->_database->where($params[0], $params[1]); |
| 955 | 926 | } |
| 956 | - } |
|
| 957 | - else if(count($params) == 3) |
|
| 927 | + } else if(count($params) == 3) |
|
| 958 | 928 | { |
| 959 | 929 | $this->_database->where($params[0], $params[1], $params[2]); |
| 960 | - } |
|
| 961 | - else |
|
| 930 | + } else |
|
| 962 | 931 | { |
| 963 | 932 | if (is_array($params[1])) |
| 964 | 933 | { |
| 965 | 934 | $this->_database->in($params[0], $params[1]); |
| 966 | - } |
|
| 967 | - else |
|
| 935 | + } else |
|
| 968 | 936 | { |
| 969 | 937 | $this->_database->where($params[0], $params[1]); |
| 970 | 938 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class Response{ |
| 28 | 28 | |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | private $_currentUrlCacheKey = null; |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | - * Whether we can compress the output using Gzip |
|
| 61 | - * @var boolean |
|
| 62 | - */ |
|
| 60 | + * Whether we can compress the output using Gzip |
|
| 61 | + * @var boolean |
|
| 62 | + */ |
|
| 63 | 63 | private static $_canCompressOutput = false; |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -236,8 +236,8 @@ discard block |
||
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
| 239 | - * Send the final page output to user |
|
| 240 | - */ |
|
| 239 | + * Send the final page output to user |
|
| 240 | + */ |
|
| 241 | 241 | public function renderFinalPage(){ |
| 242 | 242 | $logger = self::getLogger(); |
| 243 | 243 | $obj = & get_instance(); |
@@ -301,8 +301,8 @@ discard block |
||
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | /** |
| 304 | - * Send the final page output to user if is cached |
|
| 305 | - */ |
|
| 304 | + * Send the final page output to user if is cached |
|
| 305 | + */ |
|
| 306 | 306 | public function renderFinalPageFromCache(&$cache){ |
| 307 | 307 | $logger = self::getLogger(); |
| 308 | 308 | $url = $this->_currentUrl; |
@@ -362,9 +362,9 @@ discard block |
||
| 362 | 362 | } |
| 363 | 363 | |
| 364 | 364 | /** |
| 365 | - * Get the final page to be rendered |
|
| 366 | - * @return string |
|
| 367 | - */ |
|
| 365 | + * Get the final page to be rendered |
|
| 366 | + * @return string |
|
| 367 | + */ |
|
| 368 | 368 | public function getFinalPageRendered(){ |
| 369 | 369 | return $this->_pageRender; |
| 370 | 370 | } |
@@ -152,8 +152,7 @@ discard block |
||
| 152 | 152 | if(! headers_sent()){ |
| 153 | 153 | header('Location: '.$url); |
| 154 | 154 | exit; |
| 155 | - } |
|
| 156 | - else{ |
|
| 155 | + } else{ |
|
| 157 | 156 | echo '<script> |
| 158 | 157 | location.href = "'.$url.'"; |
| 159 | 158 | </script>'; |
@@ -202,12 +201,10 @@ discard block |
||
| 202 | 201 | if($moduleViewPath){ |
| 203 | 202 | $path = $moduleViewPath; |
| 204 | 203 | $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
| 205 | - } |
|
| 206 | - else{ |
|
| 204 | + } else{ |
|
| 207 | 205 | $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
| 208 | 206 | } |
| 209 | - } |
|
| 210 | - else{ |
|
| 207 | + } else{ |
|
| 211 | 208 | $logger->info('The current request does not use module using the default location.'); |
| 212 | 209 | } |
| 213 | 210 | } |
@@ -291,8 +288,7 @@ discard block |
||
| 291 | 288 | //compress the output if is available |
| 292 | 289 | if (self::$_canCompressOutput){ |
| 293 | 290 | ob_start('ob_gzhandler'); |
| 294 | - } |
|
| 295 | - else{ |
|
| 291 | + } else{ |
|
| 296 | 292 | ob_start(); |
| 297 | 293 | } |
| 298 | 294 | self::sendHeaders(200); |
@@ -324,8 +320,7 @@ discard block |
||
| 324 | 320 | $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
| 325 | 321 | self::sendHeaders(304); |
| 326 | 322 | return; |
| 327 | - } |
|
| 328 | - else{ |
|
| 323 | + } else{ |
|
| 329 | 324 | $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
| 330 | 325 | self::sendHeaders(200); |
| 331 | 326 | //get the cache content |
@@ -345,15 +340,13 @@ discard block |
||
| 345 | 340 | //compress the output if is available |
| 346 | 341 | if (self::$_canCompressOutput){ |
| 347 | 342 | ob_start('ob_gzhandler'); |
| 348 | - } |
|
| 349 | - else{ |
|
| 343 | + } else{ |
|
| 350 | 344 | ob_start(); |
| 351 | 345 | } |
| 352 | 346 | echo $content; |
| 353 | 347 | ob_end_flush(); |
| 354 | 348 | return; |
| 355 | - } |
|
| 356 | - else{ |
|
| 349 | + } else{ |
|
| 357 | 350 | $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
| 358 | 351 | $cache->delete($pageCacheKey); |
| 359 | 352 | } |
@@ -394,16 +387,14 @@ discard block |
||
| 394 | 387 | //compress the output if is available |
| 395 | 388 | if (self::$_canCompressOutput){ |
| 396 | 389 | ob_start('ob_gzhandler'); |
| 397 | - } |
|
| 398 | - else{ |
|
| 390 | + } else{ |
|
| 399 | 391 | ob_start(); |
| 400 | 392 | } |
| 401 | 393 | require_once $path; |
| 402 | 394 | $output = ob_get_clean(); |
| 403 | 395 | self::sendHeaders(404); |
| 404 | 396 | echo $output; |
| 405 | - } |
|
| 406 | - else{ |
|
| 397 | + } else{ |
|
| 407 | 398 | show_error('The 404 view [' .$path. '] does not exist'); |
| 408 | 399 | } |
| 409 | 400 | } |
@@ -418,8 +409,7 @@ discard block |
||
| 418 | 409 | //compress the output if exists |
| 419 | 410 | if (self::$_canCompressOutput){ |
| 420 | 411 | ob_start('ob_gzhandler'); |
| 421 | - } |
|
| 422 | - else{ |
|
| 412 | + } else{ |
|
| 423 | 413 | ob_start(); |
| 424 | 414 | } |
| 425 | 415 | extract($data); |
@@ -427,8 +417,7 @@ discard block |
||
| 427 | 417 | $output = ob_get_clean(); |
| 428 | 418 | self::sendHeaders(503); |
| 429 | 419 | echo $output; |
| 430 | - } |
|
| 431 | - else{ |
|
| 420 | + } else{ |
|
| 432 | 421 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
| 433 | 422 | set_http_status_header(503); |
| 434 | 423 | echo 'The error view [' . $path . '] does not exist'; |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Response{ |
|
| 27 | + class Response { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The list of request header to send with response |
@@ -65,9 +65,9 @@ discard block |
||
| 65 | 65 | /** |
| 66 | 66 | * Construct new response instance |
| 67 | 67 | */ |
| 68 | - public function __construct(){ |
|
| 69 | - $this->_currentUrl = (! empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '' ) |
|
| 70 | - . (! empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : '' ); |
|
| 68 | + public function __construct() { |
|
| 69 | + $this->_currentUrl = (!empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '') |
|
| 70 | + . (!empty($_SERVER['QUERY_STRING']) ? ('?' . $_SERVER['QUERY_STRING']) : ''); |
|
| 71 | 71 | |
| 72 | 72 | $this->_currentUrlCacheKey = md5($this->_currentUrl); |
| 73 | 73 | |
@@ -82,9 +82,9 @@ discard block |
||
| 82 | 82 | * Get the logger singleton instance |
| 83 | 83 | * @return Log the logger instance |
| 84 | 84 | */ |
| 85 | - private static function getLogger(){ |
|
| 86 | - if(self::$logger == null){ |
|
| 87 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 85 | + private static function getLogger() { |
|
| 86 | + if (self::$logger == null) { |
|
| 87 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 88 | 88 | self::$logger[0]->setLogger('Library::Response'); |
| 89 | 89 | } |
| 90 | 90 | return self::$logger[0]; |
@@ -95,12 +95,12 @@ discard block |
||
| 95 | 95 | * @param integer $httpCode the HTTP status code |
| 96 | 96 | * @param array $headers the additional headers to add to the existing headers list |
| 97 | 97 | */ |
| 98 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
| 98 | + public static function sendHeaders($httpCode = 200, array $headers = array()) { |
|
| 99 | 99 | set_http_status_header($httpCode); |
| 100 | 100 | self::setHeaders($headers); |
| 101 | - if(! headers_sent()){ |
|
| 102 | - foreach(self::getHeaders() as $key => $value){ |
|
| 103 | - header($key .': '.$value); |
|
| 101 | + if (!headers_sent()) { |
|
| 102 | + foreach (self::getHeaders() as $key => $value) { |
|
| 103 | + header($key . ': ' . $value); |
|
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | } |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | * Get the list of the headers |
| 110 | 110 | * @return array the headers list |
| 111 | 111 | */ |
| 112 | - public static function getHeaders(){ |
|
| 112 | + public static function getHeaders() { |
|
| 113 | 113 | return self::$headers; |
| 114 | 114 | } |
| 115 | 115 | |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | * @param string $name the header name |
| 119 | 119 | * @return string the header value |
| 120 | 120 | */ |
| 121 | - public static function getHeader($name){ |
|
| 121 | + public static function getHeader($name) { |
|
| 122 | 122 | return array_key_exists($name, self::$headers) ? self::$headers[$name] : null; |
| 123 | 123 | } |
| 124 | 124 | |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | * @param string $name the header name |
| 129 | 129 | * @param string $value the header value to be set |
| 130 | 130 | */ |
| 131 | - public static function setHeader($name, $value){ |
|
| 131 | + public static function setHeader($name, $value) { |
|
| 132 | 132 | self::$headers[$name] = $value; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | * @param array $headers the list of the headers to set. |
| 138 | 138 | * Note: this will merge with the existing headers |
| 139 | 139 | */ |
| 140 | - public static function setHeaders(array $headers){ |
|
| 140 | + public static function setHeaders(array $headers) { |
|
| 141 | 141 | self::$headers = array_merge(self::getHeaders(), $headers); |
| 142 | 142 | } |
| 143 | 143 | |
@@ -145,17 +145,17 @@ discard block |
||
| 145 | 145 | * Redirect user in the specified page |
| 146 | 146 | * @param string $path the URL or URI to be redirect to |
| 147 | 147 | */ |
| 148 | - public static function redirect($path = ''){ |
|
| 148 | + public static function redirect($path = '') { |
|
| 149 | 149 | $logger = self::getLogger(); |
| 150 | 150 | $url = Url::site_url($path); |
| 151 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
| 152 | - if(! headers_sent()){ |
|
| 153 | - header('Location: '.$url); |
|
| 151 | + $logger->info('Redirect to URL [' . $url . ']'); |
|
| 152 | + if (!headers_sent()) { |
|
| 153 | + header('Location: ' . $url); |
|
| 154 | 154 | exit; |
| 155 | 155 | } |
| 156 | - else{ |
|
| 156 | + else { |
|
| 157 | 157 | echo '<script> |
| 158 | - location.href = "'.$url.'"; |
|
| 158 | + location.href = "'.$url . '"; |
|
| 159 | 159 | </script>'; |
| 160 | 160 | } |
| 161 | 161 | } |
@@ -168,10 +168,10 @@ discard block |
||
| 168 | 168 | * @return void|string if $return is true will return the view content otherwise |
| 169 | 169 | * will display the view content. |
| 170 | 170 | */ |
| 171 | - public function render($view, $data = null, $return = false){ |
|
| 171 | + public function render($view, $data = null, $return = false) { |
|
| 172 | 172 | $logger = self::getLogger(); |
| 173 | 173 | //convert data to an array |
| 174 | - $data = ! is_array($data) ? (array) $data : $data; |
|
| 174 | + $data = !is_array($data) ? (array) $data : $data; |
|
| 175 | 175 | $view = str_ireplace('.php', '', $view); |
| 176 | 176 | $view = trim($view, '/\\'); |
| 177 | 177 | $viewFile = $view . '.php'; |
@@ -180,42 +180,42 @@ discard block |
||
| 180 | 180 | //super instance |
| 181 | 181 | $obj = & get_instance(); |
| 182 | 182 | |
| 183 | - if(Module::hasModule()){ |
|
| 183 | + if (Module::hasModule()) { |
|
| 184 | 184 | //check in module first |
| 185 | 185 | $logger->debug('Checking the view [' . $view . '] from module list ...'); |
| 186 | 186 | $mod = null; |
| 187 | 187 | //check if the request class contains module name |
| 188 | - if(strpos($view, '/') !== false){ |
|
| 188 | + if (strpos($view, '/') !== false) { |
|
| 189 | 189 | $viewPath = explode('/', $view); |
| 190 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
| 190 | + if (isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())) { |
|
| 191 | 191 | $mod = $viewPath[0]; |
| 192 | 192 | array_shift($viewPath); |
| 193 | 193 | $view = implode('/', $viewPath); |
| 194 | 194 | $viewFile = $view . '.php'; |
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | - if(! $mod && !empty($obj->moduleName)){ |
|
| 197 | + if (!$mod && !empty($obj->moduleName)) { |
|
| 198 | 198 | $mod = $obj->moduleName; |
| 199 | 199 | } |
| 200 | - if($mod){ |
|
| 200 | + if ($mod) { |
|
| 201 | 201 | $moduleViewPath = Module::findViewFullPath($view, $mod); |
| 202 | - if($moduleViewPath){ |
|
| 202 | + if ($moduleViewPath) { |
|
| 203 | 203 | $path = $moduleViewPath; |
| 204 | - $logger->info('Found view [' . $view . '] in module [' .$mod. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
| 204 | + $logger->info('Found view [' . $view . '] in module [' . $mod . '], the file path is [' . $moduleViewPath . '] we will used it'); |
|
| 205 | 205 | } |
| 206 | - else{ |
|
| 207 | - $logger->info('Cannot find view [' . $view . '] in module [' .$mod. '] using the default location'); |
|
| 206 | + else { |
|
| 207 | + $logger->info('Cannot find view [' . $view . '] in module [' . $mod . '] using the default location'); |
|
| 208 | 208 | } |
| 209 | 209 | } |
| 210 | - else{ |
|
| 210 | + else { |
|
| 211 | 211 | $logger->info('The current request does not use module using the default location.'); |
| 212 | 212 | } |
| 213 | 213 | } |
| 214 | 214 | $logger->info('The view file path to be loaded is [' . $path . ']'); |
| 215 | 215 | $found = false; |
| 216 | - if(file_exists($path)){ |
|
| 217 | - foreach(get_object_vars($obj) as $key => $value){ |
|
| 218 | - if(! isset($this->{$key})){ |
|
| 216 | + if (file_exists($path)) { |
|
| 217 | + foreach (get_object_vars($obj) as $key => $value) { |
|
| 218 | + if (!isset($this->{$key})) { |
|
| 219 | 219 | $this->{$key} = & $obj->{$key}; |
| 220 | 220 | } |
| 221 | 221 | } |
@@ -224,39 +224,39 @@ discard block |
||
| 224 | 224 | //need use require() instead of require_once because can load this view many time |
| 225 | 225 | require $path; |
| 226 | 226 | $content = ob_get_clean(); |
| 227 | - if($return){ |
|
| 227 | + if ($return) { |
|
| 228 | 228 | return $content; |
| 229 | 229 | } |
| 230 | 230 | $this->_pageRender .= $content; |
| 231 | 231 | $found = true; |
| 232 | 232 | } |
| 233 | - if(! $found){ |
|
| 234 | - show_error('Unable to find view [' .$view . ']'); |
|
| 233 | + if (!$found) { |
|
| 234 | + show_error('Unable to find view [' . $view . ']'); |
|
| 235 | 235 | } |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
| 239 | 239 | * Send the final page output to user |
| 240 | 240 | */ |
| 241 | - public function renderFinalPage(){ |
|
| 241 | + public function renderFinalPage() { |
|
| 242 | 242 | $logger = self::getLogger(); |
| 243 | 243 | $obj = & get_instance(); |
| 244 | 244 | $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
| 245 | 245 | $dispatcher = $obj->eventdispatcher; |
| 246 | 246 | $content = $this->_pageRender; |
| 247 | - if(! $content){ |
|
| 247 | + if (!$content) { |
|
| 248 | 248 | $logger->warning('The final view content is empty.'); |
| 249 | 249 | return; |
| 250 | 250 | } |
| 251 | 251 | //dispatch |
| 252 | 252 | $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
| 253 | - $content = ! empty($event->payload) ? $event->payload : null; |
|
| 254 | - if(empty($content)){ |
|
| 253 | + $content = !empty($event->payload) ? $event->payload : null; |
|
| 254 | + if (empty($content)) { |
|
| 255 | 255 | $logger->warning('The view content is empty after dispatch to event listeners.'); |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | //check whether need save the page into cache. |
| 259 | - if($cachePageStatus){ |
|
| 259 | + if ($cachePageStatus) { |
|
| 260 | 260 | //current page URL |
| 261 | 261 | $url = $this->_currentUrl; |
| 262 | 262 | //Cache view Time to live in second |
@@ -271,14 +271,14 @@ discard block |
||
| 271 | 271 | |
| 272 | 272 | //get the cache information to prepare header to send to browser |
| 273 | 273 | $cacheInfo = $cacheInstance->getInfo($cacheKey); |
| 274 | - if($cacheInfo){ |
|
| 274 | + if ($cacheInfo) { |
|
| 275 | 275 | $lastModified = $cacheInfo['mtime']; |
| 276 | 276 | $expire = $cacheInfo['expire']; |
| 277 | 277 | $maxAge = $expire - time(); |
| 278 | 278 | self::setHeader('Pragma', 'public'); |
| 279 | 279 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
| 280 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 281 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 280 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
| 281 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
@@ -289,10 +289,10 @@ discard block |
||
| 289 | 289 | $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
| 290 | 290 | |
| 291 | 291 | //compress the output if is available |
| 292 | - if (self::$_canCompressOutput){ |
|
| 292 | + if (self::$_canCompressOutput) { |
|
| 293 | 293 | ob_start('ob_gzhandler'); |
| 294 | 294 | } |
| 295 | - else{ |
|
| 295 | + else { |
|
| 296 | 296 | ob_start(); |
| 297 | 297 | } |
| 298 | 298 | self::sendHeaders(200); |
@@ -303,7 +303,7 @@ discard block |
||
| 303 | 303 | /** |
| 304 | 304 | * Send the final page output to user if is cached |
| 305 | 305 | */ |
| 306 | - public function renderFinalPageFromCache(&$cache){ |
|
| 306 | + public function renderFinalPageFromCache(&$cache) { |
|
| 307 | 307 | $logger = self::getLogger(); |
| 308 | 308 | $url = $this->_currentUrl; |
| 309 | 309 | //the current page cache key for identification |
@@ -312,25 +312,25 @@ discard block |
||
| 312 | 312 | $logger->debug('Checking if the page content for the URL [' . $url . '] is cached ...'); |
| 313 | 313 | //get the cache information to prepare header to send to browser |
| 314 | 314 | $cacheInfo = $cache->getInfo($pageCacheKey); |
| 315 | - if($cacheInfo){ |
|
| 315 | + if ($cacheInfo) { |
|
| 316 | 316 | $lastModified = $cacheInfo['mtime']; |
| 317 | 317 | $expire = $cacheInfo['expire']; |
| 318 | 318 | $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
| 319 | 319 | self::setHeader('Pragma', 'public'); |
| 320 | 320 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
| 321 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
| 322 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
| 323 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
| 321 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
| 322 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
| 323 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { |
|
| 324 | 324 | $logger->info('The cache page content is not yet expire for the URL [' . $url . '] send 304 header to browser'); |
| 325 | 325 | self::sendHeaders(304); |
| 326 | 326 | return; |
| 327 | 327 | } |
| 328 | - else{ |
|
| 328 | + else { |
|
| 329 | 329 | $logger->info('The cache page content is expired or the browser don\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $url . '] send cache headers to tell the browser'); |
| 330 | 330 | self::sendHeaders(200); |
| 331 | 331 | //get the cache content |
| 332 | 332 | $content = $cache->get($pageCacheKey); |
| 333 | - if($content){ |
|
| 333 | + if ($content) { |
|
| 334 | 334 | $logger->info('The page content for the URL [' . $url . '] already cached just display it'); |
| 335 | 335 | //load benchmark class |
| 336 | 336 | $benchmark = & class_loader('Benchmark'); |
@@ -343,17 +343,17 @@ discard block |
||
| 343 | 343 | |
| 344 | 344 | ///display the final output |
| 345 | 345 | //compress the output if is available |
| 346 | - if (self::$_canCompressOutput){ |
|
| 346 | + if (self::$_canCompressOutput) { |
|
| 347 | 347 | ob_start('ob_gzhandler'); |
| 348 | 348 | } |
| 349 | - else{ |
|
| 349 | + else { |
|
| 350 | 350 | ob_start(); |
| 351 | 351 | } |
| 352 | 352 | echo $content; |
| 353 | 353 | ob_end_flush(); |
| 354 | 354 | return; |
| 355 | 355 | } |
| 356 | - else{ |
|
| 356 | + else { |
|
| 357 | 357 | $logger->info('The page cache content for the URL [' . $url . '] is not valid may be already expired'); |
| 358 | 358 | $cache->delete($pageCacheKey); |
| 359 | 359 | } |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | * Get the final page to be rendered |
| 366 | 366 | * @return string |
| 367 | 367 | */ |
| 368 | - public function getFinalPageRendered(){ |
|
| 368 | + public function getFinalPageRendered() { |
|
| 369 | 369 | return $this->_pageRender; |
| 370 | 370 | } |
| 371 | 371 | |
@@ -373,14 +373,14 @@ discard block |
||
| 373 | 373 | * Send the HTTP 404 error if can not found the |
| 374 | 374 | * routing information for the current request |
| 375 | 375 | */ |
| 376 | - public static function send404(){ |
|
| 376 | + public static function send404() { |
|
| 377 | 377 | /********* for logs **************/ |
| 378 | 378 | //can't use $obj = & get_instance() here because the global super object will be available until |
| 379 | 379 | //the main controller is loaded even for Loader::library('xxxx'); |
| 380 | 380 | $logger = self::getLogger(); |
| 381 | - $request =& class_loader('Request', 'classes'); |
|
| 382 | - $userAgent =& class_loader('Browser'); |
|
| 383 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
| 381 | + $request = & class_loader('Request', 'classes'); |
|
| 382 | + $userAgent = & class_loader('Browser'); |
|
| 383 | + $browser = $userAgent->getPlatform() . ', ' . $userAgent->getBrowser() . ' ' . $userAgent->getVersion(); |
|
| 384 | 384 | |
| 385 | 385 | //here can't use Loader::functions just include the helper manually |
| 386 | 386 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
@@ -390,12 +390,12 @@ discard block |
||
| 390 | 390 | $logger->error($str); |
| 391 | 391 | /***********************************/ |
| 392 | 392 | $path = CORE_VIEWS_PATH . '404.php'; |
| 393 | - if(file_exists($path)){ |
|
| 393 | + if (file_exists($path)) { |
|
| 394 | 394 | //compress the output if is available |
| 395 | - if (self::$_canCompressOutput){ |
|
| 395 | + if (self::$_canCompressOutput) { |
|
| 396 | 396 | ob_start('ob_gzhandler'); |
| 397 | 397 | } |
| 398 | - else{ |
|
| 398 | + else { |
|
| 399 | 399 | ob_start(); |
| 400 | 400 | } |
| 401 | 401 | require_once $path; |
@@ -403,8 +403,8 @@ discard block |
||
| 403 | 403 | self::sendHeaders(404); |
| 404 | 404 | echo $output; |
| 405 | 405 | } |
| 406 | - else{ |
|
| 407 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
| 406 | + else { |
|
| 407 | + show_error('The 404 view [' . $path . '] does not exist'); |
|
| 408 | 408 | } |
| 409 | 409 | } |
| 410 | 410 | |
@@ -412,14 +412,14 @@ discard block |
||
| 412 | 412 | * Display the error to user |
| 413 | 413 | * @param array $data the error information |
| 414 | 414 | */ |
| 415 | - public static function sendError(array $data = array()){ |
|
| 415 | + public static function sendError(array $data = array()) { |
|
| 416 | 416 | $path = CORE_VIEWS_PATH . 'errors.php'; |
| 417 | - if(file_exists($path)){ |
|
| 417 | + if (file_exists($path)) { |
|
| 418 | 418 | //compress the output if exists |
| 419 | - if (self::$_canCompressOutput){ |
|
| 419 | + if (self::$_canCompressOutput) { |
|
| 420 | 420 | ob_start('ob_gzhandler'); |
| 421 | 421 | } |
| 422 | - else{ |
|
| 422 | + else { |
|
| 423 | 423 | ob_start(); |
| 424 | 424 | } |
| 425 | 425 | extract($data); |
@@ -428,7 +428,7 @@ discard block |
||
| 428 | 428 | self::sendHeaders(503); |
| 429 | 429 | echo $output; |
| 430 | 430 | } |
| 431 | - else{ |
|
| 431 | + else { |
|
| 432 | 432 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
| 433 | 433 | set_http_status_header(503); |
| 434 | 434 | echo 'The error view [' . $path . '] does not exist'; |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * This class represent the event detail to dispatch to correspond listener |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * This class represent the event detail to dispatch to correspond listener |
| 29 | 29 | */ |
| 30 | - class EventInfo{ |
|
| 30 | + class EventInfo { |
|
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | 33 | * The event name |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public $stop; |
| 56 | 56 | |
| 57 | - public function __construct($name, $payload = array(), $returnBack = false, $stop = false){ |
|
| 57 | + public function __construct($name, $payload = array(), $returnBack = false, $stop = false) { |
|
| 58 | 58 | $this->name = $name; |
| 59 | 59 | $this->payload = $payload; |
| 60 | 60 | $this->returnBack = $returnBack; |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | 3 | /** |
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
@@ -22,1198 +22,1198 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | class Database{ |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | - */ |
|
| 32 | - private $pdo = null; |
|
| 31 | + */ |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | - */ |
|
| 37 | + */ |
|
| 38 | 38 | private $databaseName = null; |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The SQL SELECT statment |
| 42 | 42 | * @var string |
| 43 | - */ |
|
| 43 | + */ |
|
| 44 | 44 | private $select = '*'; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The SQL FROM statment |
| 48 | 48 | * @var string |
| 49 | - */ |
|
| 50 | - private $from = null; |
|
| 49 | + */ |
|
| 50 | + private $from = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The SQL WHERE statment |
| 54 | 54 | * @var string |
| 55 | - */ |
|
| 56 | - private $where = null; |
|
| 55 | + */ |
|
| 56 | + private $where = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The SQL LIMIT statment |
| 60 | 60 | * @var string |
| 61 | - */ |
|
| 62 | - private $limit = null; |
|
| 61 | + */ |
|
| 62 | + private $limit = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The SQL JOIN statment |
| 66 | 66 | * @var string |
| 67 | - */ |
|
| 68 | - private $join = null; |
|
| 67 | + */ |
|
| 68 | + private $join = null; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The SQL ORDER BY statment |
| 72 | 72 | * @var string |
| 73 | - */ |
|
| 74 | - private $orderBy = null; |
|
| 73 | + */ |
|
| 74 | + private $orderBy = null; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The SQL GROUP BY statment |
| 78 | 78 | * @var string |
| 79 | - */ |
|
| 80 | - private $groupBy = null; |
|
| 79 | + */ |
|
| 80 | + private $groupBy = null; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The SQL HAVING statment |
| 84 | 84 | * @var string |
| 85 | - */ |
|
| 86 | - private $having = null; |
|
| 85 | + */ |
|
| 86 | + private $having = null; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The number of rows returned by the last query |
| 90 | 90 | * @var int |
| 91 | - */ |
|
| 92 | - private $numRows = 0; |
|
| 91 | + */ |
|
| 92 | + private $numRows = 0; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The last insert id for the primary key column that have auto increment or sequence |
| 96 | 96 | * @var mixed |
| 97 | - */ |
|
| 98 | - private $insertId = null; |
|
| 97 | + */ |
|
| 98 | + private $insertId = null; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The full SQL query statment after build for each command |
| 102 | 102 | * @var string |
| 103 | - */ |
|
| 104 | - private $query = null; |
|
| 103 | + */ |
|
| 104 | + private $query = null; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * The error returned for the last query |
| 108 | 108 | * @var string |
| 109 | - */ |
|
| 110 | - private $error = null; |
|
| 109 | + */ |
|
| 110 | + private $error = null; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The result returned for the last query |
| 114 | 114 | * @var mixed |
| 115 | - */ |
|
| 116 | - private $result = array(); |
|
| 115 | + */ |
|
| 116 | + private $result = array(); |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * The prefix used in each database table |
| 120 | 120 | * @var string |
| 121 | - */ |
|
| 122 | - private $prefix = null; |
|
| 121 | + */ |
|
| 122 | + private $prefix = null; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * The list of SQL valid operators |
| 126 | 126 | * @var array |
| 127 | - */ |
|
| 128 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 127 | + */ |
|
| 128 | + private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | 131 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 132 | 132 | * @var int |
| 133 | - */ |
|
| 133 | + */ |
|
| 134 | 134 | private $cacheTtl = 0; |
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * The cache current time to live. 0 means no need to use the cache feature |
| 138 | 138 | * @var int |
| 139 | - */ |
|
| 140 | - private $temporaryCacheTtl = 0; |
|
| 139 | + */ |
|
| 140 | + private $temporaryCacheTtl = 0; |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * The number of executed query for the current request |
| 144 | 144 | * @var int |
| 145 | - */ |
|
| 146 | - private $queryCount = 0; |
|
| 145 | + */ |
|
| 146 | + private $queryCount = 0; |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * The default data to be used in the statments query INSERT, UPDATE |
| 150 | 150 | * @var array |
| 151 | - */ |
|
| 152 | - private $data = array(); |
|
| 151 | + */ |
|
| 152 | + private $data = array(); |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * The database configuration |
| 156 | 156 | * @var array |
| 157 | - */ |
|
| 158 | - private $config = array(); |
|
| 157 | + */ |
|
| 158 | + private $config = array(); |
|
| 159 | 159 | |
| 160 | 160 | /** |
| 161 | 161 | * The logger instance |
| 162 | 162 | * @var Log |
| 163 | 163 | */ |
| 164 | - private $logger = null; |
|
| 164 | + private $logger = null; |
|
| 165 | 165 | |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * The cache instance |
|
| 169 | - * @var CacheInterface |
|
| 170 | - */ |
|
| 171 | - private $cacheInstance = null; |
|
| 167 | + /** |
|
| 168 | + * The cache instance |
|
| 169 | + * @var CacheInterface |
|
| 170 | + */ |
|
| 171 | + private $cacheInstance = null; |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * The benchmark instance |
|
| 175 | - * @var Benchmark |
|
| 176 | - */ |
|
| 177 | - private $benchmarkInstance = null; |
|
| 173 | + /** |
|
| 174 | + * The benchmark instance |
|
| 175 | + * @var Benchmark |
|
| 176 | + */ |
|
| 177 | + private $benchmarkInstance = null; |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Construct new database |
|
| 182 | - * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 183 | - */ |
|
| 184 | - public function __construct($overwriteConfig = array()){ |
|
| 185 | - //Set Log instance to use |
|
| 186 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 180 | + /** |
|
| 181 | + * Construct new database |
|
| 182 | + * @param array $overwriteConfig the config to overwrite with the config set in database.php |
|
| 183 | + */ |
|
| 184 | + public function __construct($overwriteConfig = array()){ |
|
| 185 | + //Set Log instance to use |
|
| 186 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 187 | 187 | |
| 188 | - //Set global configuration using the config file |
|
| 189 | - $this->setDatabaseConfigurationFromConfigFile($overwriteConfig); |
|
| 188 | + //Set global configuration using the config file |
|
| 189 | + $this->setDatabaseConfigurationFromConfigFile($overwriteConfig); |
|
| 190 | 190 | |
| 191 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * This is used to connect to database |
|
| 196 | - * @return bool |
|
| 197 | - */ |
|
| 198 | - public function connect(){ |
|
| 199 | - $config = $this->getDatabaseConfiguration(); |
|
| 200 | - if(! empty($config)){ |
|
| 201 | - try{ |
|
| 202 | - $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 203 | - $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 204 | - $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 205 | - $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 206 | - return true; |
|
| 207 | - } |
|
| 208 | - catch (PDOException $e){ |
|
| 209 | - $this->logger->fatal($e->getMessage()); |
|
| 210 | - show_error('Cannot connect to Database.'); |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - else{ |
|
| 215 | - show_error('Database configuration is not set.'); |
|
| 216 | - return false; |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Set the SQL FROM statment |
|
| 222 | - * @param string|array $table the table name or array of table list |
|
| 223 | - * @return object the current Database instance |
|
| 224 | - */ |
|
| 225 | - public function from($table){ |
|
| 226 | - if(is_array($table)){ |
|
| 227 | - $froms = ''; |
|
| 228 | - foreach($table as $key){ |
|
| 229 | - $froms .= $this->prefix . $key . ', '; |
|
| 230 | - } |
|
| 231 | - $this->from = rtrim($froms, ', '); |
|
| 232 | - } |
|
| 233 | - else{ |
|
| 234 | - $this->from = $this->prefix . $table; |
|
| 235 | - } |
|
| 236 | - return $this; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Set the SQL SELECT statment |
|
| 241 | - * @param string|array $fields the field name or array of field list |
|
| 242 | - * @return object the current Database instance |
|
| 243 | - */ |
|
| 244 | - public function select($fields){ |
|
| 245 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 246 | - $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 247 | - return $this; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Set the SQL SELECT DISTINCT statment |
|
| 252 | - * @param string $field the field name to distinct |
|
| 253 | - * @return object the current Database instance |
|
| 254 | - */ |
|
| 255 | - public function distinct($field){ |
|
| 256 | - $distinct = ' DISTINCT ' . $field; |
|
| 257 | - $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 258 | - |
|
| 259 | - return $this; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Set the SQL function MAX in SELECT statment |
|
| 264 | - * @param string $field the field name |
|
| 265 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 266 | - * @return object the current Database instance |
|
| 267 | - */ |
|
| 268 | - public function max($field, $name = null){ |
|
| 269 | - $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 270 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 271 | - return $this; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Set the SQL function MIN in SELECT statment |
|
| 276 | - * @param string $field the field name |
|
| 277 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 278 | - * @return object the current Database instance |
|
| 279 | - */ |
|
| 280 | - public function min($field, $name = null){ |
|
| 281 | - $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 282 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Set the SQL function SUM in SELECT statment |
|
| 288 | - * @param string $field the field name |
|
| 289 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 290 | - * @return object the current Database instance |
|
| 291 | - */ |
|
| 292 | - public function sum($field, $name = null){ |
|
| 293 | - $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 294 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 295 | - return $this; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Set the SQL function COUNT in SELECT statment |
|
| 300 | - * @param string $field the field name |
|
| 301 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 302 | - * @return object the current Database instance |
|
| 303 | - */ |
|
| 304 | - public function count($field = '*', $name = null){ |
|
| 305 | - $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 306 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 307 | - return $this; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Set the SQL function AVG in SELECT statment |
|
| 312 | - * @param string $field the field name |
|
| 313 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 314 | - * @return object the current Database instance |
|
| 315 | - */ |
|
| 316 | - public function avg($field, $name = null){ |
|
| 317 | - $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 318 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 319 | - return $this; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Set the SQL JOIN statment |
|
| 324 | - * @param string $table the join table name |
|
| 325 | - * @param string $field1 the first field for join conditions |
|
| 326 | - * @param string $op the join condition operator. If is null the default will be "=" |
|
| 327 | - * @param string $field2 the second field for join conditions |
|
| 328 | - * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 329 | - * @return object the current Database instance |
|
| 330 | - */ |
|
| 331 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 332 | - $on = $field1; |
|
| 333 | - $table = $this->prefix . $table; |
|
| 334 | - if(! is_null($op)){ |
|
| 335 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 336 | - } |
|
| 337 | - if (empty($this->join)){ |
|
| 338 | - $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 339 | - } |
|
| 340 | - else{ |
|
| 341 | - $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 342 | - } |
|
| 343 | - return $this; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Set the SQL INNER JOIN statment |
|
| 348 | - * @see Database::join() |
|
| 349 | - * @return object the current Database instance |
|
| 350 | - */ |
|
| 351 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 352 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Set the SQL LEFT JOIN statment |
|
| 357 | - * @see Database::join() |
|
| 358 | - * @return object the current Database instance |
|
| 359 | - */ |
|
| 360 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 361 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Set the SQL RIGHT JOIN statment |
|
| 366 | - * @see Database::join() |
|
| 367 | - * @return object the current Database instance |
|
| 368 | - */ |
|
| 369 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 370 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Set the SQL FULL OUTER JOIN statment |
|
| 375 | - * @see Database::join() |
|
| 376 | - * @return object the current Database instance |
|
| 377 | - */ |
|
| 378 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 379 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Set the SQL LEFT OUTER JOIN statment |
|
| 384 | - * @see Database::join() |
|
| 385 | - * @return object the current Database instance |
|
| 386 | - */ |
|
| 387 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 388 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Set the SQL RIGHT OUTER JOIN statment |
|
| 393 | - * @see Database::join() |
|
| 394 | - * @return object the current Database instance |
|
| 395 | - */ |
|
| 396 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 397 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Set the SQL WHERE CLAUSE for IS NULL |
|
| 402 | - * @param string|array $field the field name or array of field list |
|
| 403 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 404 | - * @return object the current Database instance |
|
| 405 | - */ |
|
| 406 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 407 | - if(is_array($field)){ |
|
| 408 | - foreach($field as $f){ |
|
| 409 | - $this->whereIsNull($f, $andOr); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - else{ |
|
| 413 | - if (! $this->where){ |
|
| 414 | - $this->where = $field.' IS NULL '; |
|
| 415 | - } |
|
| 416 | - else{ |
|
| 417 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - return $this; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 425 | - * @param string|array $field the field name or array of field list |
|
| 426 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 427 | - * @return object the current Database instance |
|
| 428 | - */ |
|
| 429 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 430 | - if(is_array($field)){ |
|
| 431 | - foreach($field as $f){ |
|
| 432 | - $this->whereIsNotNull($f, $andOr); |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - else{ |
|
| 436 | - if (! $this->where){ |
|
| 437 | - $this->where = $field.' IS NOT NULL '; |
|
| 438 | - } |
|
| 439 | - else{ |
|
| 440 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - return $this; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * Get the SQL WHERE clause using array column => value |
|
| 448 | - * @see Database::where |
|
| 449 | - * |
|
| 450 | - * @return string |
|
| 451 | - */ |
|
| 452 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 453 | - $_where = array(); |
|
| 454 | - foreach ($where as $column => $data){ |
|
| 455 | - if(is_null($data)){ |
|
| 456 | - $data = ''; |
|
| 457 | - } |
|
| 458 | - $_where[] = $type . $column . ' = ' . ($escape ? $this->escape($data) : $data); |
|
| 459 | - } |
|
| 460 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 461 | - return $where; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 466 | - * @see Database::where |
|
| 467 | - * |
|
| 468 | - * @return string |
|
| 469 | - */ |
|
| 470 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 471 | - $x = explode('?', $where); |
|
| 472 | - $w = ''; |
|
| 473 | - foreach($x as $k => $v){ |
|
| 474 | - if(! empty($v)){ |
|
| 475 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 476 | - $op[$k] = ''; |
|
| 477 | - } |
|
| 478 | - $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - return $w; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 486 | - * @see Database::where |
|
| 487 | - * |
|
| 488 | - * @return string |
|
| 489 | - */ |
|
| 490 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 491 | - $w = ''; |
|
| 492 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 493 | - if(is_null($op)){ |
|
| 494 | - $op = ''; |
|
| 495 | - } |
|
| 496 | - $w = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 497 | - } |
|
| 498 | - else{ |
|
| 499 | - if(is_null($val)){ |
|
| 500 | - $val = ''; |
|
| 501 | - } |
|
| 502 | - $w = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 503 | - } |
|
| 504 | - return $w; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Set the $this->where property |
|
| 509 | - * @param string $whereStr the WHERE clause string |
|
| 510 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 511 | - */ |
|
| 512 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 513 | - if (empty($this->where)){ |
|
| 514 | - $this->where = $whereStr; |
|
| 515 | - } |
|
| 516 | - else{ |
|
| 517 | - if(substr($this->where, -1) == '('){ |
|
| 518 | - $this->where = $this->where . ' ' . $whereStr; |
|
| 519 | - } |
|
| 520 | - else{ |
|
| 521 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 522 | - } |
|
| 523 | - } |
|
| 524 | - } |
|
| 191 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * This is used to connect to database |
|
| 196 | + * @return bool |
|
| 197 | + */ |
|
| 198 | + public function connect(){ |
|
| 199 | + $config = $this->getDatabaseConfiguration(); |
|
| 200 | + if(! empty($config)){ |
|
| 201 | + try{ |
|
| 202 | + $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
|
| 203 | + $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
|
| 204 | + $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
|
| 205 | + $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
|
| 206 | + return true; |
|
| 207 | + } |
|
| 208 | + catch (PDOException $e){ |
|
| 209 | + $this->logger->fatal($e->getMessage()); |
|
| 210 | + show_error('Cannot connect to Database.'); |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + else{ |
|
| 215 | + show_error('Database configuration is not set.'); |
|
| 216 | + return false; |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Set the SQL FROM statment |
|
| 222 | + * @param string|array $table the table name or array of table list |
|
| 223 | + * @return object the current Database instance |
|
| 224 | + */ |
|
| 225 | + public function from($table){ |
|
| 226 | + if(is_array($table)){ |
|
| 227 | + $froms = ''; |
|
| 228 | + foreach($table as $key){ |
|
| 229 | + $froms .= $this->prefix . $key . ', '; |
|
| 230 | + } |
|
| 231 | + $this->from = rtrim($froms, ', '); |
|
| 232 | + } |
|
| 233 | + else{ |
|
| 234 | + $this->from = $this->prefix . $table; |
|
| 235 | + } |
|
| 236 | + return $this; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Set the SQL SELECT statment |
|
| 241 | + * @param string|array $fields the field name or array of field list |
|
| 242 | + * @return object the current Database instance |
|
| 243 | + */ |
|
| 244 | + public function select($fields){ |
|
| 245 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 246 | + $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
|
| 247 | + return $this; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Set the SQL SELECT DISTINCT statment |
|
| 252 | + * @param string $field the field name to distinct |
|
| 253 | + * @return object the current Database instance |
|
| 254 | + */ |
|
| 255 | + public function distinct($field){ |
|
| 256 | + $distinct = ' DISTINCT ' . $field; |
|
| 257 | + $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
|
| 258 | + |
|
| 259 | + return $this; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Set the SQL function MAX in SELECT statment |
|
| 264 | + * @param string $field the field name |
|
| 265 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 266 | + * @return object the current Database instance |
|
| 267 | + */ |
|
| 268 | + public function max($field, $name = null){ |
|
| 269 | + $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 270 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 271 | + return $this; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Set the SQL function MIN in SELECT statment |
|
| 276 | + * @param string $field the field name |
|
| 277 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 278 | + * @return object the current Database instance |
|
| 279 | + */ |
|
| 280 | + public function min($field, $name = null){ |
|
| 281 | + $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 282 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Set the SQL function SUM in SELECT statment |
|
| 288 | + * @param string $field the field name |
|
| 289 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 290 | + * @return object the current Database instance |
|
| 291 | + */ |
|
| 292 | + public function sum($field, $name = null){ |
|
| 293 | + $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 294 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 295 | + return $this; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Set the SQL function COUNT in SELECT statment |
|
| 300 | + * @param string $field the field name |
|
| 301 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 302 | + * @return object the current Database instance |
|
| 303 | + */ |
|
| 304 | + public function count($field = '*', $name = null){ |
|
| 305 | + $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 306 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 307 | + return $this; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Set the SQL function AVG in SELECT statment |
|
| 312 | + * @param string $field the field name |
|
| 313 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 314 | + * @return object the current Database instance |
|
| 315 | + */ |
|
| 316 | + public function avg($field, $name = null){ |
|
| 317 | + $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 318 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 319 | + return $this; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Set the SQL JOIN statment |
|
| 324 | + * @param string $table the join table name |
|
| 325 | + * @param string $field1 the first field for join conditions |
|
| 326 | + * @param string $op the join condition operator. If is null the default will be "=" |
|
| 327 | + * @param string $field2 the second field for join conditions |
|
| 328 | + * @param string $type the type of join (INNER, LEFT, RIGHT) |
|
| 329 | + * @return object the current Database instance |
|
| 330 | + */ |
|
| 331 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 332 | + $on = $field1; |
|
| 333 | + $table = $this->prefix . $table; |
|
| 334 | + if(! is_null($op)){ |
|
| 335 | + $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 336 | + } |
|
| 337 | + if (empty($this->join)){ |
|
| 338 | + $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 339 | + } |
|
| 340 | + else{ |
|
| 341 | + $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
|
| 342 | + } |
|
| 343 | + return $this; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Set the SQL INNER JOIN statment |
|
| 348 | + * @see Database::join() |
|
| 349 | + * @return object the current Database instance |
|
| 350 | + */ |
|
| 351 | + public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 352 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Set the SQL LEFT JOIN statment |
|
| 357 | + * @see Database::join() |
|
| 358 | + * @return object the current Database instance |
|
| 359 | + */ |
|
| 360 | + public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 361 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Set the SQL RIGHT JOIN statment |
|
| 366 | + * @see Database::join() |
|
| 367 | + * @return object the current Database instance |
|
| 368 | + */ |
|
| 369 | + public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 370 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Set the SQL FULL OUTER JOIN statment |
|
| 375 | + * @see Database::join() |
|
| 376 | + * @return object the current Database instance |
|
| 377 | + */ |
|
| 378 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 379 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Set the SQL LEFT OUTER JOIN statment |
|
| 384 | + * @see Database::join() |
|
| 385 | + * @return object the current Database instance |
|
| 386 | + */ |
|
| 387 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 388 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Set the SQL RIGHT OUTER JOIN statment |
|
| 393 | + * @see Database::join() |
|
| 394 | + * @return object the current Database instance |
|
| 395 | + */ |
|
| 396 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 397 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Set the SQL WHERE CLAUSE for IS NULL |
|
| 402 | + * @param string|array $field the field name or array of field list |
|
| 403 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 404 | + * @return object the current Database instance |
|
| 405 | + */ |
|
| 406 | + public function whereIsNull($field, $andOr = 'AND'){ |
|
| 407 | + if(is_array($field)){ |
|
| 408 | + foreach($field as $f){ |
|
| 409 | + $this->whereIsNull($f, $andOr); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + else{ |
|
| 413 | + if (! $this->where){ |
|
| 414 | + $this->where = $field.' IS NULL '; |
|
| 415 | + } |
|
| 416 | + else{ |
|
| 417 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + return $this; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Set the SQL WHERE CLAUSE for IS NOT NULL |
|
| 425 | + * @param string|array $field the field name or array of field list |
|
| 426 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 427 | + * @return object the current Database instance |
|
| 428 | + */ |
|
| 429 | + public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 430 | + if(is_array($field)){ |
|
| 431 | + foreach($field as $f){ |
|
| 432 | + $this->whereIsNotNull($f, $andOr); |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + else{ |
|
| 436 | + if (! $this->where){ |
|
| 437 | + $this->where = $field.' IS NOT NULL '; |
|
| 438 | + } |
|
| 439 | + else{ |
|
| 440 | + $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + return $this; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * Get the SQL WHERE clause using array column => value |
|
| 448 | + * @see Database::where |
|
| 449 | + * |
|
| 450 | + * @return string |
|
| 451 | + */ |
|
| 452 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 453 | + $_where = array(); |
|
| 454 | + foreach ($where as $column => $data){ |
|
| 455 | + if(is_null($data)){ |
|
| 456 | + $data = ''; |
|
| 457 | + } |
|
| 458 | + $_where[] = $type . $column . ' = ' . ($escape ? $this->escape($data) : $data); |
|
| 459 | + } |
|
| 460 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 461 | + return $where; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 466 | + * @see Database::where |
|
| 467 | + * |
|
| 468 | + * @return string |
|
| 469 | + */ |
|
| 470 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 471 | + $x = explode('?', $where); |
|
| 472 | + $w = ''; |
|
| 473 | + foreach($x as $k => $v){ |
|
| 474 | + if(! empty($v)){ |
|
| 475 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 476 | + $op[$k] = ''; |
|
| 477 | + } |
|
| 478 | + $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + return $w; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * Get the default SQL WHERE clause using operator = or the operator argument |
|
| 486 | + * @see Database::where |
|
| 487 | + * |
|
| 488 | + * @return string |
|
| 489 | + */ |
|
| 490 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 491 | + $w = ''; |
|
| 492 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 493 | + if(is_null($op)){ |
|
| 494 | + $op = ''; |
|
| 495 | + } |
|
| 496 | + $w = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
|
| 497 | + } |
|
| 498 | + else{ |
|
| 499 | + if(is_null($val)){ |
|
| 500 | + $val = ''; |
|
| 501 | + } |
|
| 502 | + $w = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
|
| 503 | + } |
|
| 504 | + return $w; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Set the $this->where property |
|
| 509 | + * @param string $whereStr the WHERE clause string |
|
| 510 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 511 | + */ |
|
| 512 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 513 | + if (empty($this->where)){ |
|
| 514 | + $this->where = $whereStr; |
|
| 515 | + } |
|
| 516 | + else{ |
|
| 517 | + if(substr($this->where, -1) == '('){ |
|
| 518 | + $this->where = $this->where . ' ' . $whereStr; |
|
| 519 | + } |
|
| 520 | + else{ |
|
| 521 | + $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 522 | + } |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | 525 | |
| 526 | - /** |
|
| 527 | - * Set the SQL WHERE CLAUSE statment |
|
| 528 | - * @param string|array $where the where field or array of field list |
|
| 529 | - * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 530 | - * @param mixed $val the where value |
|
| 531 | - * @param string $type the type used for this where clause (NOT, etc.) |
|
| 532 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 533 | - * @param boolean $escape whether to escape or not the $val |
|
| 534 | - * @return object the current Database instance |
|
| 535 | - */ |
|
| 536 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 537 | - $whereStr = ''; |
|
| 538 | - if (is_array($where)){ |
|
| 539 | - $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 540 | - } |
|
| 541 | - else{ |
|
| 542 | - if(is_array($op)){ |
|
| 543 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 544 | - } else { |
|
| 545 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - $this->setWhereStr($whereStr, $andOr); |
|
| 549 | - return $this; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * Set the SQL WHERE CLAUSE statment using OR |
|
| 554 | - * @see Database::where() |
|
| 555 | - * @return object the current Database instance |
|
| 556 | - */ |
|
| 557 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 558 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 564 | - * @see Database::where() |
|
| 565 | - * @return object the current Database instance |
|
| 566 | - */ |
|
| 567 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 568 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 573 | - * @see Database::where() |
|
| 574 | - * @return object the current Database instance |
|
| 575 | - */ |
|
| 576 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 577 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Set the opened parenthesis for the complex SQL query |
|
| 582 | - * @param string $type the type of this grouped (NOT, etc.) |
|
| 583 | - * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 584 | - * @return object the current Database instance |
|
| 585 | - */ |
|
| 586 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 587 | - if (empty($this->where)){ |
|
| 588 | - $this->where = $type . ' ('; |
|
| 589 | - } |
|
| 590 | - else{ |
|
| 591 | - if(substr($this->where, -1) == '('){ |
|
| 592 | - $this->where .= $type . ' ('; |
|
| 593 | - } |
|
| 594 | - else{ |
|
| 595 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - return $this; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 603 | - * @see Database::groupStart() |
|
| 604 | - * @return object the current Database instance |
|
| 605 | - */ |
|
| 606 | - public function notGroupStart(){ |
|
| 607 | - return $this->groupStart('NOT'); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 612 | - * @see Database::groupStart() |
|
| 613 | - * @return object the current Database instance |
|
| 614 | - */ |
|
| 615 | - public function orGroupStart(){ |
|
| 616 | - return $this->groupStart('', ' OR'); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 621 | - * @see Database::groupStart() |
|
| 622 | - * @return object the current Database instance |
|
| 623 | - */ |
|
| 624 | - public function orNotGroupStart(){ |
|
| 625 | - return $this->groupStart('NOT', ' OR'); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Close the parenthesis for the grouped SQL |
|
| 630 | - * @return object the current Database instance |
|
| 631 | - */ |
|
| 632 | - public function groupEnd(){ |
|
| 633 | - $this->where .= ')'; |
|
| 634 | - return $this; |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * Set the SQL WHERE CLAUSE statment for IN |
|
| 639 | - * @param string $field the field name for IN statment |
|
| 640 | - * @param array $keys the list of values used |
|
| 641 | - * @param string $type the condition separator type (NOT) |
|
| 642 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 643 | - * @param boolean $escape whether to escape or not the values |
|
| 644 | - * @return object the current Database instance |
|
| 645 | - */ |
|
| 646 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 647 | - $_keys = array(); |
|
| 648 | - foreach ($keys as $k => $v){ |
|
| 649 | - if(is_null($v)){ |
|
| 650 | - $v = ''; |
|
| 651 | - } |
|
| 652 | - $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 653 | - } |
|
| 654 | - $keys = implode(', ', $_keys); |
|
| 655 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 656 | - $this->setWhereStr($whereStr, $andOr); |
|
| 657 | - return $this; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - /** |
|
| 661 | - * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 662 | - * @see Database::in() |
|
| 663 | - * @return object the current Database instance |
|
| 664 | - */ |
|
| 665 | - public function notIn($field, array $keys, $escape = true){ |
|
| 666 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 671 | - * @see Database::in() |
|
| 672 | - * @return object the current Database instance |
|
| 673 | - */ |
|
| 674 | - public function orIn($field, array $keys, $escape = true){ |
|
| 675 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - /** |
|
| 679 | - * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 680 | - * @see Database::in() |
|
| 681 | - * @return object the current Database instance |
|
| 682 | - */ |
|
| 683 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 684 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 689 | - * @param string $field the field used for the BETWEEN statment |
|
| 690 | - * @param mixed $value1 the BETWEEN begin value |
|
| 691 | - * @param mixed $value2 the BETWEEN end value |
|
| 692 | - * @param string $type the condition separator type (NOT) |
|
| 693 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 694 | - * @param boolean $escape whether to escape or not the values |
|
| 695 | - * @return object the current Database instance |
|
| 696 | - */ |
|
| 697 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 698 | - if(is_null($value1)){ |
|
| 699 | - $value1 = ''; |
|
| 700 | - } |
|
| 701 | - if(is_null($value2)){ |
|
| 702 | - $value2 = ''; |
|
| 703 | - } |
|
| 704 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 705 | - $this->setWhereStr($whereStr, $andOr); |
|
| 706 | - return $this; |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 711 | - * @see Database::between() |
|
| 712 | - * @return object the current Database instance |
|
| 713 | - */ |
|
| 714 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 715 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 720 | - * @see Database::between() |
|
| 721 | - * @return object the current Database instance |
|
| 722 | - */ |
|
| 723 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 724 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 729 | - * @see Database::between() |
|
| 730 | - * @return object the current Database instance |
|
| 731 | - */ |
|
| 732 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 733 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 738 | - * @param string $field the field name used in LIKE statment |
|
| 739 | - * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 740 | - * @param string $type the condition separator type (NOT) |
|
| 741 | - * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 742 | - * @param boolean $escape whether to escape or not the values |
|
| 743 | - * @return object the current Database instance |
|
| 744 | - */ |
|
| 745 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 746 | - if(empty($data)){ |
|
| 747 | - $data = ''; |
|
| 748 | - } |
|
| 749 | - $like = $escape ? $this->escape($data) : $data; |
|
| 750 | - if (empty($this->where)){ |
|
| 751 | - $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 752 | - } |
|
| 753 | - else{ |
|
| 754 | - if(substr($this->where, -1) == '('){ |
|
| 755 | - $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 756 | - } |
|
| 757 | - else{ |
|
| 758 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 759 | - } |
|
| 760 | - } |
|
| 761 | - return $this; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 766 | - * @see Database::like() |
|
| 767 | - * @return object the current Database instance |
|
| 768 | - */ |
|
| 769 | - public function orLike($field, $data, $escape = true){ |
|
| 770 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 775 | - * @see Database::like() |
|
| 776 | - * @return object the current Database instance |
|
| 777 | - */ |
|
| 778 | - public function notLike($field, $data, $escape = true){ |
|
| 779 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 784 | - * @see Database::like() |
|
| 785 | - * @return object the current Database instance |
|
| 786 | - */ |
|
| 787 | - public function orNotLike($field, $data, $escape = true){ |
|
| 788 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Set the SQL LIMIT statment |
|
| 793 | - * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 794 | - * like LIMIT n; |
|
| 795 | - * @param int $limitEnd the limit count |
|
| 796 | - * @return object the current Database instance |
|
| 797 | - */ |
|
| 798 | - public function limit($limit, $limitEnd = null){ |
|
| 799 | - if(empty($limit)){ |
|
| 800 | - return; |
|
| 801 | - } |
|
| 802 | - if (! is_null($limitEnd)){ |
|
| 803 | - $this->limit = $limit . ', ' . $limitEnd; |
|
| 804 | - } |
|
| 805 | - else{ |
|
| 806 | - $this->limit = $limit; |
|
| 807 | - } |
|
| 808 | - return $this; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * Set the SQL ORDER BY CLAUSE statment |
|
| 813 | - * @param string $orderBy the field name used for order |
|
| 814 | - * @param string $orderDir the order direction (ASC or DESC) |
|
| 815 | - * @return object the current Database instance |
|
| 816 | - */ |
|
| 817 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 818 | - if (! empty($orderDir)){ |
|
| 819 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 820 | - } |
|
| 821 | - else{ |
|
| 822 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 823 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 824 | - } |
|
| 825 | - else{ |
|
| 826 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 827 | - } |
|
| 828 | - } |
|
| 829 | - return $this; |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - /** |
|
| 833 | - * Set the SQL GROUP BY CLAUSE statment |
|
| 834 | - * @param string|array $field the field name used or array of field list |
|
| 835 | - * @return object the current Database instance |
|
| 836 | - */ |
|
| 837 | - public function groupBy($field){ |
|
| 838 | - if(is_array($field)){ |
|
| 839 | - $this->groupBy = implode(', ', $field); |
|
| 840 | - } |
|
| 841 | - else{ |
|
| 842 | - $this->groupBy = $field; |
|
| 843 | - } |
|
| 844 | - return $this; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - /** |
|
| 848 | - * Set the SQL HAVING CLAUSE statment |
|
| 849 | - * @param string $field the field name used for HAVING statment |
|
| 850 | - * @param string|array $op the operator used or array |
|
| 851 | - * @param mixed $val the value for HAVING comparaison |
|
| 852 | - * @param boolean $escape whether to escape or not the values |
|
| 853 | - * @return object the current Database instance |
|
| 854 | - */ |
|
| 855 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 856 | - if(is_array($op)){ |
|
| 857 | - $x = explode('?', $field); |
|
| 858 | - $w = ''; |
|
| 859 | - foreach($x as $k => $v){ |
|
| 860 | - if(!empty($v)){ |
|
| 861 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 862 | - $op[$k] = ''; |
|
| 863 | - } |
|
| 864 | - $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 865 | - } |
|
| 866 | - } |
|
| 867 | - $this->having = $w; |
|
| 868 | - } |
|
| 869 | - else if (! in_array($op, $this->operatorList)){ |
|
| 870 | - if(is_null($op)){ |
|
| 871 | - $op = ''; |
|
| 872 | - } |
|
| 873 | - $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 874 | - } |
|
| 875 | - else{ |
|
| 876 | - if(is_null($val)){ |
|
| 877 | - $val = ''; |
|
| 878 | - } |
|
| 879 | - $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 880 | - } |
|
| 881 | - return $this; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - /** |
|
| 885 | - * Return the number of rows returned by the current query |
|
| 886 | - * @return int |
|
| 887 | - */ |
|
| 888 | - public function numRows(){ |
|
| 889 | - return $this->numRows; |
|
| 890 | - } |
|
| 891 | - |
|
| 892 | - /** |
|
| 893 | - * Return the last insert id value |
|
| 894 | - * @return mixed |
|
| 895 | - */ |
|
| 896 | - public function insertId(){ |
|
| 897 | - return $this->insertId; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - /** |
|
| 901 | - * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 902 | - */ |
|
| 903 | - public function error(){ |
|
| 526 | + /** |
|
| 527 | + * Set the SQL WHERE CLAUSE statment |
|
| 528 | + * @param string|array $where the where field or array of field list |
|
| 529 | + * @param array|string $op the condition operator. If is null the default will be "=" |
|
| 530 | + * @param mixed $val the where value |
|
| 531 | + * @param string $type the type used for this where clause (NOT, etc.) |
|
| 532 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 533 | + * @param boolean $escape whether to escape or not the $val |
|
| 534 | + * @return object the current Database instance |
|
| 535 | + */ |
|
| 536 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 537 | + $whereStr = ''; |
|
| 538 | + if (is_array($where)){ |
|
| 539 | + $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
|
| 540 | + } |
|
| 541 | + else{ |
|
| 542 | + if(is_array($op)){ |
|
| 543 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 544 | + } else { |
|
| 545 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + $this->setWhereStr($whereStr, $andOr); |
|
| 549 | + return $this; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * Set the SQL WHERE CLAUSE statment using OR |
|
| 554 | + * @see Database::where() |
|
| 555 | + * @return object the current Database instance |
|
| 556 | + */ |
|
| 557 | + public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 558 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Set the SQL WHERE CLAUSE statment using AND and NOT |
|
| 564 | + * @see Database::where() |
|
| 565 | + * @return object the current Database instance |
|
| 566 | + */ |
|
| 567 | + public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 568 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * Set the SQL WHERE CLAUSE statment using OR and NOT |
|
| 573 | + * @see Database::where() |
|
| 574 | + * @return object the current Database instance |
|
| 575 | + */ |
|
| 576 | + public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 577 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Set the opened parenthesis for the complex SQL query |
|
| 582 | + * @param string $type the type of this grouped (NOT, etc.) |
|
| 583 | + * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
|
| 584 | + * @return object the current Database instance |
|
| 585 | + */ |
|
| 586 | + public function groupStart($type = '', $andOr = ' AND'){ |
|
| 587 | + if (empty($this->where)){ |
|
| 588 | + $this->where = $type . ' ('; |
|
| 589 | + } |
|
| 590 | + else{ |
|
| 591 | + if(substr($this->where, -1) == '('){ |
|
| 592 | + $this->where .= $type . ' ('; |
|
| 593 | + } |
|
| 594 | + else{ |
|
| 595 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + return $this; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Set the opened parenthesis for the complex SQL query using NOT type |
|
| 603 | + * @see Database::groupStart() |
|
| 604 | + * @return object the current Database instance |
|
| 605 | + */ |
|
| 606 | + public function notGroupStart(){ |
|
| 607 | + return $this->groupStart('NOT'); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Set the opened parenthesis for the complex SQL query using OR for separator |
|
| 612 | + * @see Database::groupStart() |
|
| 613 | + * @return object the current Database instance |
|
| 614 | + */ |
|
| 615 | + public function orGroupStart(){ |
|
| 616 | + return $this->groupStart('', ' OR'); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 621 | + * @see Database::groupStart() |
|
| 622 | + * @return object the current Database instance |
|
| 623 | + */ |
|
| 624 | + public function orNotGroupStart(){ |
|
| 625 | + return $this->groupStart('NOT', ' OR'); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Close the parenthesis for the grouped SQL |
|
| 630 | + * @return object the current Database instance |
|
| 631 | + */ |
|
| 632 | + public function groupEnd(){ |
|
| 633 | + $this->where .= ')'; |
|
| 634 | + return $this; |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * Set the SQL WHERE CLAUSE statment for IN |
|
| 639 | + * @param string $field the field name for IN statment |
|
| 640 | + * @param array $keys the list of values used |
|
| 641 | + * @param string $type the condition separator type (NOT) |
|
| 642 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 643 | + * @param boolean $escape whether to escape or not the values |
|
| 644 | + * @return object the current Database instance |
|
| 645 | + */ |
|
| 646 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 647 | + $_keys = array(); |
|
| 648 | + foreach ($keys as $k => $v){ |
|
| 649 | + if(is_null($v)){ |
|
| 650 | + $v = ''; |
|
| 651 | + } |
|
| 652 | + $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
|
| 653 | + } |
|
| 654 | + $keys = implode(', ', $_keys); |
|
| 655 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 656 | + $this->setWhereStr($whereStr, $andOr); |
|
| 657 | + return $this; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + /** |
|
| 661 | + * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator |
|
| 662 | + * @see Database::in() |
|
| 663 | + * @return object the current Database instance |
|
| 664 | + */ |
|
| 665 | + public function notIn($field, array $keys, $escape = true){ |
|
| 666 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Set the SQL WHERE CLAUSE statment for IN with OR separator |
|
| 671 | + * @see Database::in() |
|
| 672 | + * @return object the current Database instance |
|
| 673 | + */ |
|
| 674 | + public function orIn($field, array $keys, $escape = true){ |
|
| 675 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + /** |
|
| 679 | + * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator |
|
| 680 | + * @see Database::in() |
|
| 681 | + * @return object the current Database instance |
|
| 682 | + */ |
|
| 683 | + public function orNotIn($field, array $keys, $escape = true){ |
|
| 684 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Set the SQL WHERE CLAUSE statment for BETWEEN |
|
| 689 | + * @param string $field the field used for the BETWEEN statment |
|
| 690 | + * @param mixed $value1 the BETWEEN begin value |
|
| 691 | + * @param mixed $value2 the BETWEEN end value |
|
| 692 | + * @param string $type the condition separator type (NOT) |
|
| 693 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 694 | + * @param boolean $escape whether to escape or not the values |
|
| 695 | + * @return object the current Database instance |
|
| 696 | + */ |
|
| 697 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 698 | + if(is_null($value1)){ |
|
| 699 | + $value1 = ''; |
|
| 700 | + } |
|
| 701 | + if(is_null($value2)){ |
|
| 702 | + $value2 = ''; |
|
| 703 | + } |
|
| 704 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
|
| 705 | + $this->setWhereStr($whereStr, $andOr); |
|
| 706 | + return $this; |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator |
|
| 711 | + * @see Database::between() |
|
| 712 | + * @return object the current Database instance |
|
| 713 | + */ |
|
| 714 | + public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 715 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator |
|
| 720 | + * @see Database::between() |
|
| 721 | + * @return object the current Database instance |
|
| 722 | + */ |
|
| 723 | + public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 724 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator |
|
| 729 | + * @see Database::between() |
|
| 730 | + * @return object the current Database instance |
|
| 731 | + */ |
|
| 732 | + public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 733 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Set the SQL WHERE CLAUSE statment for LIKE |
|
| 738 | + * @param string $field the field name used in LIKE statment |
|
| 739 | + * @param string $data the LIKE value for this field including the '%', and '_' part |
|
| 740 | + * @param string $type the condition separator type (NOT) |
|
| 741 | + * @param string $andOr the multiple conditions separator (OR, AND) |
|
| 742 | + * @param boolean $escape whether to escape or not the values |
|
| 743 | + * @return object the current Database instance |
|
| 744 | + */ |
|
| 745 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 746 | + if(empty($data)){ |
|
| 747 | + $data = ''; |
|
| 748 | + } |
|
| 749 | + $like = $escape ? $this->escape($data) : $data; |
|
| 750 | + if (empty($this->where)){ |
|
| 751 | + $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
|
| 752 | + } |
|
| 753 | + else{ |
|
| 754 | + if(substr($this->where, -1) == '('){ |
|
| 755 | + $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 756 | + } |
|
| 757 | + else{ |
|
| 758 | + $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 759 | + } |
|
| 760 | + } |
|
| 761 | + return $this; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * Set the SQL WHERE CLAUSE statment for LIKE with OR separator |
|
| 766 | + * @see Database::like() |
|
| 767 | + * @return object the current Database instance |
|
| 768 | + */ |
|
| 769 | + public function orLike($field, $data, $escape = true){ |
|
| 770 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator |
|
| 775 | + * @see Database::like() |
|
| 776 | + * @return object the current Database instance |
|
| 777 | + */ |
|
| 778 | + public function notLike($field, $data, $escape = true){ |
|
| 779 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator |
|
| 784 | + * @see Database::like() |
|
| 785 | + * @return object the current Database instance |
|
| 786 | + */ |
|
| 787 | + public function orNotLike($field, $data, $escape = true){ |
|
| 788 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + /** |
|
| 792 | + * Set the SQL LIMIT statment |
|
| 793 | + * @param int $limit the limit offset. If $limitEnd is null this will be the limit count |
|
| 794 | + * like LIMIT n; |
|
| 795 | + * @param int $limitEnd the limit count |
|
| 796 | + * @return object the current Database instance |
|
| 797 | + */ |
|
| 798 | + public function limit($limit, $limitEnd = null){ |
|
| 799 | + if(empty($limit)){ |
|
| 800 | + return; |
|
| 801 | + } |
|
| 802 | + if (! is_null($limitEnd)){ |
|
| 803 | + $this->limit = $limit . ', ' . $limitEnd; |
|
| 804 | + } |
|
| 805 | + else{ |
|
| 806 | + $this->limit = $limit; |
|
| 807 | + } |
|
| 808 | + return $this; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * Set the SQL ORDER BY CLAUSE statment |
|
| 813 | + * @param string $orderBy the field name used for order |
|
| 814 | + * @param string $orderDir the order direction (ASC or DESC) |
|
| 815 | + * @return object the current Database instance |
|
| 816 | + */ |
|
| 817 | + public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 818 | + if (! empty($orderDir)){ |
|
| 819 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 820 | + } |
|
| 821 | + else{ |
|
| 822 | + if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 823 | + $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 824 | + } |
|
| 825 | + else{ |
|
| 826 | + $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 827 | + } |
|
| 828 | + } |
|
| 829 | + return $this; |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + /** |
|
| 833 | + * Set the SQL GROUP BY CLAUSE statment |
|
| 834 | + * @param string|array $field the field name used or array of field list |
|
| 835 | + * @return object the current Database instance |
|
| 836 | + */ |
|
| 837 | + public function groupBy($field){ |
|
| 838 | + if(is_array($field)){ |
|
| 839 | + $this->groupBy = implode(', ', $field); |
|
| 840 | + } |
|
| 841 | + else{ |
|
| 842 | + $this->groupBy = $field; |
|
| 843 | + } |
|
| 844 | + return $this; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + /** |
|
| 848 | + * Set the SQL HAVING CLAUSE statment |
|
| 849 | + * @param string $field the field name used for HAVING statment |
|
| 850 | + * @param string|array $op the operator used or array |
|
| 851 | + * @param mixed $val the value for HAVING comparaison |
|
| 852 | + * @param boolean $escape whether to escape or not the values |
|
| 853 | + * @return object the current Database instance |
|
| 854 | + */ |
|
| 855 | + public function having($field, $op = null, $val = null, $escape = true){ |
|
| 856 | + if(is_array($op)){ |
|
| 857 | + $x = explode('?', $field); |
|
| 858 | + $w = ''; |
|
| 859 | + foreach($x as $k => $v){ |
|
| 860 | + if(!empty($v)){ |
|
| 861 | + if(isset($op[$k]) && is_null($op[$k])){ |
|
| 862 | + $op[$k] = ''; |
|
| 863 | + } |
|
| 864 | + $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | + $this->having = $w; |
|
| 868 | + } |
|
| 869 | + else if (! in_array($op, $this->operatorList)){ |
|
| 870 | + if(is_null($op)){ |
|
| 871 | + $op = ''; |
|
| 872 | + } |
|
| 873 | + $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
|
| 874 | + } |
|
| 875 | + else{ |
|
| 876 | + if(is_null($val)){ |
|
| 877 | + $val = ''; |
|
| 878 | + } |
|
| 879 | + $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
|
| 880 | + } |
|
| 881 | + return $this; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + /** |
|
| 885 | + * Return the number of rows returned by the current query |
|
| 886 | + * @return int |
|
| 887 | + */ |
|
| 888 | + public function numRows(){ |
|
| 889 | + return $this->numRows; |
|
| 890 | + } |
|
| 891 | + |
|
| 892 | + /** |
|
| 893 | + * Return the last insert id value |
|
| 894 | + * @return mixed |
|
| 895 | + */ |
|
| 896 | + public function insertId(){ |
|
| 897 | + return $this->insertId; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + /** |
|
| 901 | + * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
|
| 902 | + */ |
|
| 903 | + public function error(){ |
|
| 904 | 904 | if($this->error){ |
| 905 | 905 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 906 | 906 | } |
| 907 | - } |
|
| 908 | - |
|
| 909 | - /** |
|
| 910 | - * Get the result of one record rows returned by the current query |
|
| 911 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 912 | - * If is string will determine the result type "array" or "object" |
|
| 913 | - * @return mixed the query SQL string or the record result |
|
| 914 | - */ |
|
| 915 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 916 | - $this->limit = 1; |
|
| 917 | - $query = $this->getAll(true); |
|
| 918 | - if($returnSQLQueryOrResultType === true){ |
|
| 919 | - return $query; |
|
| 920 | - } |
|
| 921 | - else{ |
|
| 922 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Get the result of record rows list returned by the current query |
|
| 928 | - * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 929 | - * If is string will determine the result type "array" or "object" |
|
| 930 | - * @return mixed the query SQL string or the record result |
|
| 931 | - */ |
|
| 932 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 933 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 934 | - if (! empty($this->join)){ |
|
| 935 | - $query .= $this->join; |
|
| 936 | - } |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + /** |
|
| 910 | + * Get the result of one record rows returned by the current query |
|
| 911 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 912 | + * If is string will determine the result type "array" or "object" |
|
| 913 | + * @return mixed the query SQL string or the record result |
|
| 914 | + */ |
|
| 915 | + public function get($returnSQLQueryOrResultType = false){ |
|
| 916 | + $this->limit = 1; |
|
| 917 | + $query = $this->getAll(true); |
|
| 918 | + if($returnSQLQueryOrResultType === true){ |
|
| 919 | + return $query; |
|
| 920 | + } |
|
| 921 | + else{ |
|
| 922 | + return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Get the result of record rows list returned by the current query |
|
| 928 | + * @param boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string. |
|
| 929 | + * If is string will determine the result type "array" or "object" |
|
| 930 | + * @return mixed the query SQL string or the record result |
|
| 931 | + */ |
|
| 932 | + public function getAll($returnSQLQueryOrResultType = false){ |
|
| 933 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 934 | + if (! empty($this->join)){ |
|
| 935 | + $query .= $this->join; |
|
| 936 | + } |
|
| 937 | 937 | |
| 938 | - if (! empty($this->where)){ |
|
| 939 | - $query .= ' WHERE ' . $this->where; |
|
| 940 | - } |
|
| 938 | + if (! empty($this->where)){ |
|
| 939 | + $query .= ' WHERE ' . $this->where; |
|
| 940 | + } |
|
| 941 | 941 | |
| 942 | - if (! empty($this->groupBy)){ |
|
| 943 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 944 | - } |
|
| 942 | + if (! empty($this->groupBy)){ |
|
| 943 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 944 | + } |
|
| 945 | 945 | |
| 946 | - if (! empty($this->having)){ |
|
| 947 | - $query .= ' HAVING ' . $this->having; |
|
| 948 | - } |
|
| 946 | + if (! empty($this->having)){ |
|
| 947 | + $query .= ' HAVING ' . $this->having; |
|
| 948 | + } |
|
| 949 | 949 | |
| 950 | - if (! empty($this->orderBy)){ |
|
| 951 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 952 | - } |
|
| 950 | + if (! empty($this->orderBy)){ |
|
| 951 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 952 | + } |
|
| 953 | 953 | |
| 954 | - if(! empty($this->limit)){ |
|
| 955 | - $query .= ' LIMIT ' . $this->limit; |
|
| 956 | - } |
|
| 954 | + if(! empty($this->limit)){ |
|
| 955 | + $query .= ' LIMIT ' . $this->limit; |
|
| 956 | + } |
|
| 957 | 957 | |
| 958 | 958 | if($returnSQLQueryOrResultType === true){ |
| 959 | - return $query; |
|
| 960 | - } |
|
| 961 | - else{ |
|
| 962 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 963 | - } |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - /** |
|
| 967 | - * Insert new record in the database |
|
| 968 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 969 | - * @param boolean $escape whether to escape or not the values |
|
| 970 | - * @return mixed the insert id of the new record or null |
|
| 971 | - */ |
|
| 972 | - public function insert($data = array(), $escape = true){ |
|
| 973 | - $column = array(); |
|
| 974 | - $val = array(); |
|
| 975 | - if(empty($data) && $this->getData()){ |
|
| 976 | - $columns = array_keys($this->getData()); |
|
| 977 | - $column = implode(',', $columns); |
|
| 978 | - $val = implode(', ', $this->getData()); |
|
| 979 | - } |
|
| 980 | - else{ |
|
| 981 | - $columns = array_keys($data); |
|
| 982 | - $column = implode(',', $columns); |
|
| 983 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 984 | - } |
|
| 985 | - |
|
| 986 | - $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 987 | - $query = $this->query($query); |
|
| 988 | - |
|
| 989 | - if ($query){ |
|
| 990 | - if(! $this->pdo){ |
|
| 991 | - $this->connect(); |
|
| 992 | - } |
|
| 993 | - $this->insertId = $this->pdo->lastInsertId(); |
|
| 994 | - return $this->insertId(); |
|
| 995 | - } |
|
| 996 | - else{ |
|
| 959 | + return $query; |
|
| 960 | + } |
|
| 961 | + else{ |
|
| 962 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 963 | + } |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + /** |
|
| 967 | + * Insert new record in the database |
|
| 968 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 969 | + * @param boolean $escape whether to escape or not the values |
|
| 970 | + * @return mixed the insert id of the new record or null |
|
| 971 | + */ |
|
| 972 | + public function insert($data = array(), $escape = true){ |
|
| 973 | + $column = array(); |
|
| 974 | + $val = array(); |
|
| 975 | + if(empty($data) && $this->getData()){ |
|
| 976 | + $columns = array_keys($this->getData()); |
|
| 977 | + $column = implode(',', $columns); |
|
| 978 | + $val = implode(', ', $this->getData()); |
|
| 979 | + } |
|
| 980 | + else{ |
|
| 981 | + $columns = array_keys($data); |
|
| 982 | + $column = implode(',', $columns); |
|
| 983 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 984 | + } |
|
| 985 | + |
|
| 986 | + $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 987 | + $query = $this->query($query); |
|
| 988 | + |
|
| 989 | + if ($query){ |
|
| 990 | + if(! $this->pdo){ |
|
| 991 | + $this->connect(); |
|
| 992 | + } |
|
| 993 | + $this->insertId = $this->pdo->lastInsertId(); |
|
| 994 | + return $this->insertId(); |
|
| 995 | + } |
|
| 996 | + else{ |
|
| 997 | 997 | return false; |
| 998 | - } |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * Update record in the database |
|
| 1003 | - * @param array $data the record data if is empty will use the $this->data array. |
|
| 1004 | - * @param boolean $escape whether to escape or not the values |
|
| 1005 | - * @return mixed the update status |
|
| 1006 | - */ |
|
| 1007 | - public function update($data = array(), $escape = true){ |
|
| 1008 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1009 | - $values = array(); |
|
| 1010 | - if(empty($data) && $this->getData()){ |
|
| 1011 | - foreach ($this->getData() as $column => $val){ |
|
| 1012 | - $values[] = $column . ' = ' . $val; |
|
| 1013 | - } |
|
| 1014 | - } |
|
| 1015 | - else{ |
|
| 1016 | - foreach ($data as $column => $val){ |
|
| 1017 | - $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1018 | - } |
|
| 1019 | - } |
|
| 1020 | - $query .= implode(', ', $values); |
|
| 1021 | - if (! empty($this->where)){ |
|
| 1022 | - $query .= ' WHERE ' . $this->where; |
|
| 1023 | - } |
|
| 1024 | - |
|
| 1025 | - if (! empty($this->orderBy)){ |
|
| 1026 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - if (! empty($this->limit)){ |
|
| 1030 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1031 | - } |
|
| 1032 | - return $this->query($query); |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - /** |
|
| 1036 | - * Delete the record in database |
|
| 1037 | - * @return mixed the delete status |
|
| 1038 | - */ |
|
| 1039 | - public function delete(){ |
|
| 1040 | - $query = 'DELETE FROM ' . $this->from; |
|
| 1041 | - |
|
| 1042 | - if (! empty($this->where)){ |
|
| 1043 | - $query .= ' WHERE ' . $this->where; |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - if (! empty($this->orderBy)){ |
|
| 1047 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - if (! empty($this->limit)){ |
|
| 1051 | - $query .= ' LIMIT ' . $this->limit; |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1055 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1056 | - } |
|
| 1057 | - return $this->query($query); |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - /** |
|
| 1061 | - * Execute an SQL query |
|
| 1062 | - * @param string $query the query SQL string |
|
| 1063 | - * @param boolean $all whether to return all record or not |
|
| 1064 | - * @param boolean $array return the result as array |
|
| 1065 | - * @return mixed the query result |
|
| 1066 | - */ |
|
| 1067 | - public function query($query, $all = true, $array = false){ |
|
| 1068 | - $this->reset(); |
|
| 1069 | - if(is_array($all)){ |
|
| 1070 | - $x = explode('?', $query); |
|
| 1071 | - $q = ''; |
|
| 1072 | - foreach($x as $k => $v){ |
|
| 1073 | - if(! empty($v)){ |
|
| 1074 | - $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1075 | - } |
|
| 1076 | - } |
|
| 1077 | - $query = $q; |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1081 | - $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1082 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1083 | - //cache expire time |
|
| 998 | + } |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * Update record in the database |
|
| 1003 | + * @param array $data the record data if is empty will use the $this->data array. |
|
| 1004 | + * @param boolean $escape whether to escape or not the values |
|
| 1005 | + * @return mixed the update status |
|
| 1006 | + */ |
|
| 1007 | + public function update($data = array(), $escape = true){ |
|
| 1008 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 1009 | + $values = array(); |
|
| 1010 | + if(empty($data) && $this->getData()){ |
|
| 1011 | + foreach ($this->getData() as $column => $val){ |
|
| 1012 | + $values[] = $column . ' = ' . $val; |
|
| 1013 | + } |
|
| 1014 | + } |
|
| 1015 | + else{ |
|
| 1016 | + foreach ($data as $column => $val){ |
|
| 1017 | + $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
|
| 1018 | + } |
|
| 1019 | + } |
|
| 1020 | + $query .= implode(', ', $values); |
|
| 1021 | + if (! empty($this->where)){ |
|
| 1022 | + $query .= ' WHERE ' . $this->where; |
|
| 1023 | + } |
|
| 1024 | + |
|
| 1025 | + if (! empty($this->orderBy)){ |
|
| 1026 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + if (! empty($this->limit)){ |
|
| 1030 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1031 | + } |
|
| 1032 | + return $this->query($query); |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + /** |
|
| 1036 | + * Delete the record in database |
|
| 1037 | + * @return mixed the delete status |
|
| 1038 | + */ |
|
| 1039 | + public function delete(){ |
|
| 1040 | + $query = 'DELETE FROM ' . $this->from; |
|
| 1041 | + |
|
| 1042 | + if (! empty($this->where)){ |
|
| 1043 | + $query .= ' WHERE ' . $this->where; |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + if (! empty($this->orderBy)){ |
|
| 1047 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + if (! empty($this->limit)){ |
|
| 1051 | + $query .= ' LIMIT ' . $this->limit; |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1055 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 1056 | + } |
|
| 1057 | + return $this->query($query); |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + /** |
|
| 1061 | + * Execute an SQL query |
|
| 1062 | + * @param string $query the query SQL string |
|
| 1063 | + * @param boolean $all whether to return all record or not |
|
| 1064 | + * @param boolean $array return the result as array |
|
| 1065 | + * @return mixed the query result |
|
| 1066 | + */ |
|
| 1067 | + public function query($query, $all = true, $array = false){ |
|
| 1068 | + $this->reset(); |
|
| 1069 | + if(is_array($all)){ |
|
| 1070 | + $x = explode('?', $query); |
|
| 1071 | + $q = ''; |
|
| 1072 | + foreach($x as $k => $v){ |
|
| 1073 | + if(! empty($v)){ |
|
| 1074 | + $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
|
| 1075 | + } |
|
| 1076 | + } |
|
| 1077 | + $query = $q; |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 1081 | + $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 1082 | + $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1083 | + //cache expire time |
|
| 1084 | 1084 | $cacheExpire = $this->temporaryCacheTtl; |
| 1085 | 1085 | |
| 1086 | 1086 | //return to the initial cache time |
| 1087 | 1087 | $this->temporaryCacheTtl = $this->cacheTtl; |
| 1088 | 1088 | |
| 1089 | 1089 | //config for cache |
| 1090 | - $cacheEnable = get_config('cache_enable'); |
|
| 1090 | + $cacheEnable = get_config('cache_enable'); |
|
| 1091 | 1091 | |
| 1092 | 1092 | //the database cache content |
| 1093 | - $cacheContent = null; |
|
| 1093 | + $cacheContent = null; |
|
| 1094 | 1094 | |
| 1095 | 1095 | //this database query cache key |
| 1096 | - $cacheKey = null; |
|
| 1096 | + $cacheKey = null; |
|
| 1097 | 1097 | |
| 1098 | 1098 | //the cache manager instance |
| 1099 | - $cacheInstance = null; |
|
| 1099 | + $cacheInstance = null; |
|
| 1100 | 1100 | |
| 1101 | 1101 | //if can use cache feature for this query |
| 1102 | 1102 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 1103 | 1103 | |
| 1104 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1105 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1106 | - $cacheKey = md5($query . $all . $array); |
|
| 1107 | - if(is_object($this->cacheInstance)){ |
|
| 1108 | - $cacheInstance = $this->cacheInstance; |
|
| 1109 | - } |
|
| 1110 | - else{ |
|
| 1111 | - $obj = & get_instance(); |
|
| 1112 | - $cacheInstance = $obj->cache; |
|
| 1113 | - } |
|
| 1114 | - $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1115 | - } |
|
| 1116 | - else{ |
|
| 1104 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1105 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 1106 | + $cacheKey = md5($query . $all . $array); |
|
| 1107 | + if(is_object($this->cacheInstance)){ |
|
| 1108 | + $cacheInstance = $this->cacheInstance; |
|
| 1109 | + } |
|
| 1110 | + else{ |
|
| 1111 | + $obj = & get_instance(); |
|
| 1112 | + $cacheInstance = $obj->cache; |
|
| 1113 | + } |
|
| 1114 | + $cacheContent = $cacheInstance->get($cacheKey); |
|
| 1115 | + } |
|
| 1116 | + else{ |
|
| 1117 | 1117 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1118 | - } |
|
| 1118 | + } |
|
| 1119 | 1119 | |
| 1120 | - if(! $this->pdo){ |
|
| 1121 | - $this->connect(); |
|
| 1122 | - } |
|
| 1120 | + if(! $this->pdo){ |
|
| 1121 | + $this->connect(); |
|
| 1122 | + } |
|
| 1123 | 1123 | |
| 1124 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1125 | - //for database query execution time |
|
| 1126 | - $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1127 | - $bench = null; |
|
| 1128 | - if(is_object($this->benchmarkInstance)){ |
|
| 1129 | - $bench = $this->benchmarkInstance; |
|
| 1130 | - } |
|
| 1131 | - else{ |
|
| 1132 | - $obj = & get_instance(); |
|
| 1133 | - $bench = $obj->benchmark; |
|
| 1134 | - } |
|
| 1135 | - $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1136 | - //Now execute the query |
|
| 1137 | - $sqlQuery = $this->pdo->query($this->query); |
|
| 1124 | + if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1125 | + //for database query execution time |
|
| 1126 | + $benchmarkMarkerKey = md5($query . $all . $array); |
|
| 1127 | + $bench = null; |
|
| 1128 | + if(is_object($this->benchmarkInstance)){ |
|
| 1129 | + $bench = $this->benchmarkInstance; |
|
| 1130 | + } |
|
| 1131 | + else{ |
|
| 1132 | + $obj = & get_instance(); |
|
| 1133 | + $bench = $obj->benchmark; |
|
| 1134 | + } |
|
| 1135 | + $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
|
| 1136 | + //Now execute the query |
|
| 1137 | + $sqlQuery = $this->pdo->query($this->query); |
|
| 1138 | 1138 | |
| 1139 | - //get response time for this query |
|
| 1140 | - $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1141 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 1142 | - if($responseTime >= 1 ){ |
|
| 1143 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1144 | - } |
|
| 1145 | - if ($sqlQuery){ |
|
| 1146 | - //if need return all result like list of record |
|
| 1147 | - if ($all){ |
|
| 1148 | - $this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
| 1149 | - } |
|
| 1150 | - else{ |
|
| 1151 | - $this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
| 1152 | - } |
|
| 1153 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1154 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1155 | - $this->numRows = count($this->result); |
|
| 1156 | - } |
|
| 1157 | - else{ |
|
| 1158 | - $this->numRows = $sqlQuery->rowCount(); |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1162 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1163 | - $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1164 | - } |
|
| 1165 | - } |
|
| 1166 | - else{ |
|
| 1167 | - $error = $this->pdo->errorInfo(); |
|
| 1168 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1169 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1170 | - $this->error(); |
|
| 1171 | - } |
|
| 1172 | - } |
|
| 1173 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1174 | - $queryStr = $this->pdo->query($this->query); |
|
| 1175 | - if($queryStr){ |
|
| 1176 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1177 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1178 | - $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1179 | - $this->numRows = 1; |
|
| 1180 | - } |
|
| 1181 | - else{ |
|
| 1182 | - $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1183 | - $this->numRows = $queryStr->rowCount(); |
|
| 1184 | - } |
|
| 1185 | - } |
|
| 1186 | - if (! $this->result){ |
|
| 1187 | - $error = $this->pdo->errorInfo(); |
|
| 1188 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1189 | - $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1190 | - $this->error(); |
|
| 1191 | - } |
|
| 1192 | - } |
|
| 1193 | - else{ |
|
| 1194 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1195 | - $this->result = $cacheContent; |
|
| 1196 | - $this->numRows = count($this->result); |
|
| 1197 | - } |
|
| 1198 | - $this->queryCount++; |
|
| 1199 | - if(! $this->result){ |
|
| 1200 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1201 | - } |
|
| 1202 | - return $this->result; |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - /** |
|
| 1206 | - * Set database cache time to live |
|
| 1207 | - * @param integer $ttl the cache time to live in second |
|
| 1208 | - * @return object the current Database instance |
|
| 1209 | - */ |
|
| 1210 | - public function setCache($ttl = 0){ |
|
| 1211 | - if($ttl > 0){ |
|
| 1212 | - $this->cacheTtl = $ttl; |
|
| 1213 | - $this->temporaryCacheTtl = $ttl; |
|
| 1214 | - } |
|
| 1215 | - return $this; |
|
| 1216 | - } |
|
| 1139 | + //get response time for this query |
|
| 1140 | + $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
|
| 1141 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 1142 | + if($responseTime >= 1 ){ |
|
| 1143 | + $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1144 | + } |
|
| 1145 | + if ($sqlQuery){ |
|
| 1146 | + //if need return all result like list of record |
|
| 1147 | + if ($all){ |
|
| 1148 | + $this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
|
| 1149 | + } |
|
| 1150 | + else{ |
|
| 1151 | + $this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
|
| 1152 | + } |
|
| 1153 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1154 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1155 | + $this->numRows = count($this->result); |
|
| 1156 | + } |
|
| 1157 | + else{ |
|
| 1158 | + $this->numRows = $sqlQuery->rowCount(); |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1162 | + $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1163 | + $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
|
| 1164 | + } |
|
| 1165 | + } |
|
| 1166 | + else{ |
|
| 1167 | + $error = $this->pdo->errorInfo(); |
|
| 1168 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1169 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1170 | + $this->error(); |
|
| 1171 | + } |
|
| 1172 | + } |
|
| 1173 | + else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1174 | + $queryStr = $this->pdo->query($this->query); |
|
| 1175 | + if($queryStr){ |
|
| 1176 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 1177 | + if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1178 | + $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1179 | + $this->numRows = 1; |
|
| 1180 | + } |
|
| 1181 | + else{ |
|
| 1182 | + $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 1183 | + $this->numRows = $queryStr->rowCount(); |
|
| 1184 | + } |
|
| 1185 | + } |
|
| 1186 | + if (! $this->result){ |
|
| 1187 | + $error = $this->pdo->errorInfo(); |
|
| 1188 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 1189 | + $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
|
| 1190 | + $this->error(); |
|
| 1191 | + } |
|
| 1192 | + } |
|
| 1193 | + else{ |
|
| 1194 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1195 | + $this->result = $cacheContent; |
|
| 1196 | + $this->numRows = count($this->result); |
|
| 1197 | + } |
|
| 1198 | + $this->queryCount++; |
|
| 1199 | + if(! $this->result){ |
|
| 1200 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 1201 | + } |
|
| 1202 | + return $this->result; |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + /** |
|
| 1206 | + * Set database cache time to live |
|
| 1207 | + * @param integer $ttl the cache time to live in second |
|
| 1208 | + * @return object the current Database instance |
|
| 1209 | + */ |
|
| 1210 | + public function setCache($ttl = 0){ |
|
| 1211 | + if($ttl > 0){ |
|
| 1212 | + $this->cacheTtl = $ttl; |
|
| 1213 | + $this->temporaryCacheTtl = $ttl; |
|
| 1214 | + } |
|
| 1215 | + return $this; |
|
| 1216 | + } |
|
| 1217 | 1217 | |
| 1218 | 1218 | /** |
| 1219 | 1219 | * Enabled cache temporary for the current query not globally |
@@ -1221,258 +1221,258 @@ discard block |
||
| 1221 | 1221 | * @return object the current Database instance |
| 1222 | 1222 | */ |
| 1223 | 1223 | public function cached($ttl = 0){ |
| 1224 | - if($ttl > 0){ |
|
| 1225 | - $this->temporaryCacheTtl = $ttl; |
|
| 1226 | - } |
|
| 1224 | + if($ttl > 0){ |
|
| 1225 | + $this->temporaryCacheTtl = $ttl; |
|
| 1226 | + } |
|
| 1227 | + return $this; |
|
| 1228 | + } |
|
| 1229 | + |
|
| 1230 | + /** |
|
| 1231 | + * Escape the data before execute query useful for security. |
|
| 1232 | + * @param mixed $data the data to be escaped |
|
| 1233 | + * @return mixed the data after escaped |
|
| 1234 | + */ |
|
| 1235 | + public function escape($data){ |
|
| 1236 | + if(is_null($data)){ |
|
| 1237 | + return null; |
|
| 1238 | + } |
|
| 1239 | + if(! $this->pdo){ |
|
| 1240 | + $this->connect(); |
|
| 1241 | + } |
|
| 1242 | + return $this->pdo->quote(trim($data)); |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + /** |
|
| 1246 | + * Return the number query executed count for the current request |
|
| 1247 | + * @return int |
|
| 1248 | + */ |
|
| 1249 | + public function queryCount(){ |
|
| 1250 | + return $this->queryCount; |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + /** |
|
| 1254 | + * Return the current query SQL string |
|
| 1255 | + * @return string |
|
| 1256 | + */ |
|
| 1257 | + public function getQuery(){ |
|
| 1258 | + return $this->query; |
|
| 1259 | + } |
|
| 1260 | + |
|
| 1261 | + /** |
|
| 1262 | + * Return the application database name |
|
| 1263 | + * @return string |
|
| 1264 | + */ |
|
| 1265 | + public function getDatabaseName(){ |
|
| 1266 | + return $this->databaseName; |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + /** |
|
| 1270 | + * Return the database configuration |
|
| 1271 | + * @return array |
|
| 1272 | + */ |
|
| 1273 | + public function getDatabaseConfiguration(){ |
|
| 1274 | + return $this->config; |
|
| 1275 | + } |
|
| 1276 | + |
|
| 1277 | + /** |
|
| 1278 | + * set the database configuration |
|
| 1279 | + * @param array $config the configuration |
|
| 1280 | + */ |
|
| 1281 | + public function setDatabaseConfiguration(array $config){ |
|
| 1282 | + $this->config = array_merge($this->config, $config); |
|
| 1283 | + $this->prefix = $this->config['prefix']; |
|
| 1284 | + $this->databaseName = $this->config['database']; |
|
| 1285 | + $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1227 | 1286 | return $this; |
| 1228 | - } |
|
| 1229 | - |
|
| 1230 | - /** |
|
| 1231 | - * Escape the data before execute query useful for security. |
|
| 1232 | - * @param mixed $data the data to be escaped |
|
| 1233 | - * @return mixed the data after escaped |
|
| 1234 | - */ |
|
| 1235 | - public function escape($data){ |
|
| 1236 | - if(is_null($data)){ |
|
| 1237 | - return null; |
|
| 1238 | - } |
|
| 1239 | - if(! $this->pdo){ |
|
| 1240 | - $this->connect(); |
|
| 1241 | - } |
|
| 1242 | - return $this->pdo->quote(trim($data)); |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - /** |
|
| 1246 | - * Return the number query executed count for the current request |
|
| 1247 | - * @return int |
|
| 1248 | - */ |
|
| 1249 | - public function queryCount(){ |
|
| 1250 | - return $this->queryCount; |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - /** |
|
| 1254 | - * Return the current query SQL string |
|
| 1255 | - * @return string |
|
| 1256 | - */ |
|
| 1257 | - public function getQuery(){ |
|
| 1258 | - return $this->query; |
|
| 1259 | - } |
|
| 1260 | - |
|
| 1261 | - /** |
|
| 1262 | - * Return the application database name |
|
| 1263 | - * @return string |
|
| 1264 | - */ |
|
| 1265 | - public function getDatabaseName(){ |
|
| 1266 | - return $this->databaseName; |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - /** |
|
| 1270 | - * Return the database configuration |
|
| 1271 | - * @return array |
|
| 1272 | - */ |
|
| 1273 | - public function getDatabaseConfiguration(){ |
|
| 1274 | - return $this->config; |
|
| 1275 | - } |
|
| 1276 | - |
|
| 1277 | - /** |
|
| 1278 | - * set the database configuration |
|
| 1279 | - * @param array $config the configuration |
|
| 1280 | - */ |
|
| 1281 | - public function setDatabaseConfiguration(array $config){ |
|
| 1282 | - $this->config = array_merge($this->config, $config); |
|
| 1283 | - $this->prefix = $this->config['prefix']; |
|
| 1284 | - $this->databaseName = $this->config['database']; |
|
| 1285 | - $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password']))))); |
|
| 1286 | - return $this; |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - /** |
|
| 1290 | - * Return the PDO instance |
|
| 1291 | - * @return PDO |
|
| 1292 | - */ |
|
| 1293 | - public function getPdo(){ |
|
| 1294 | - return $this->pdo; |
|
| 1295 | - } |
|
| 1296 | - |
|
| 1297 | - /** |
|
| 1298 | - * Set the PDO instance |
|
| 1299 | - * @param PDO $pdo the pdo object |
|
| 1300 | - */ |
|
| 1301 | - public function setPdo(PDO $pdo){ |
|
| 1302 | - $this->pdo = $pdo; |
|
| 1303 | - return $this; |
|
| 1304 | - } |
|
| 1305 | - |
|
| 1306 | - |
|
| 1307 | - /** |
|
| 1308 | - * Return the Log instance |
|
| 1309 | - * @return Log |
|
| 1310 | - */ |
|
| 1311 | - public function getLogger(){ |
|
| 1312 | - return $this->logger; |
|
| 1313 | - } |
|
| 1314 | - |
|
| 1315 | - /** |
|
| 1316 | - * Set the log instance |
|
| 1317 | - * @param Log $logger the log object |
|
| 1318 | - */ |
|
| 1319 | - public function setLogger($logger){ |
|
| 1320 | - $this->logger = $logger; |
|
| 1321 | - return $this; |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - /** |
|
| 1325 | - * Return the cache instance |
|
| 1326 | - * @return CacheInterface |
|
| 1327 | - */ |
|
| 1328 | - public function getCacheInstance(){ |
|
| 1329 | - return $this->cacheInstance; |
|
| 1330 | - } |
|
| 1331 | - |
|
| 1332 | - /** |
|
| 1333 | - * Set the cache instance |
|
| 1334 | - * @param CacheInterface $cache the cache object |
|
| 1335 | - */ |
|
| 1336 | - public function setCacheInstance($cache){ |
|
| 1337 | - $this->cacheInstance = $cache; |
|
| 1338 | - return $this; |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - /** |
|
| 1342 | - * Return the benchmark instance |
|
| 1343 | - * @return Benchmark |
|
| 1344 | - */ |
|
| 1345 | - public function getBenchmark(){ |
|
| 1346 | - return $this->benchmarkInstance; |
|
| 1347 | - } |
|
| 1348 | - |
|
| 1349 | - /** |
|
| 1350 | - * Set the benchmark instance |
|
| 1351 | - * @param Benchmark $cache the cache object |
|
| 1352 | - */ |
|
| 1353 | - public function setBenchmark($benchmark){ |
|
| 1354 | - $this->benchmarkInstance = $benchmark; |
|
| 1355 | - return $this; |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - /** |
|
| 1359 | - * Return the data to be used for insert, update, etc. |
|
| 1360 | - * @return array |
|
| 1361 | - */ |
|
| 1362 | - public function getData(){ |
|
| 1363 | - return $this->data; |
|
| 1364 | - } |
|
| 1365 | - |
|
| 1366 | - /** |
|
| 1367 | - * Set the data to be used for insert, update, etc. |
|
| 1368 | - * @param string $key the data key identified |
|
| 1369 | - * @param mixed $value the data value |
|
| 1370 | - * @param boolean $escape whether to escape or not the $value |
|
| 1371 | - * @return object the current Database instance |
|
| 1372 | - */ |
|
| 1373 | - public function setData($key, $value, $escape = true){ |
|
| 1374 | - $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1375 | - return $this; |
|
| 1376 | - } |
|
| 1377 | - |
|
| 1378 | - /** |
|
| 1379 | - * Set the Log instance using argument or create new instance |
|
| 1380 | - * @param object $logger the Log instance if not null |
|
| 1381 | - */ |
|
| 1382 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1383 | - if($logger !== null){ |
|
| 1384 | - $this->logger = $logger; |
|
| 1385 | - } |
|
| 1386 | - else{ |
|
| 1387 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 1388 | - $this->logger->setLogger('Library::Database'); |
|
| 1389 | - } |
|
| 1390 | - } |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + /** |
|
| 1290 | + * Return the PDO instance |
|
| 1291 | + * @return PDO |
|
| 1292 | + */ |
|
| 1293 | + public function getPdo(){ |
|
| 1294 | + return $this->pdo; |
|
| 1295 | + } |
|
| 1296 | + |
|
| 1297 | + /** |
|
| 1298 | + * Set the PDO instance |
|
| 1299 | + * @param PDO $pdo the pdo object |
|
| 1300 | + */ |
|
| 1301 | + public function setPdo(PDO $pdo){ |
|
| 1302 | + $this->pdo = $pdo; |
|
| 1303 | + return $this; |
|
| 1304 | + } |
|
| 1305 | + |
|
| 1306 | + |
|
| 1307 | + /** |
|
| 1308 | + * Return the Log instance |
|
| 1309 | + * @return Log |
|
| 1310 | + */ |
|
| 1311 | + public function getLogger(){ |
|
| 1312 | + return $this->logger; |
|
| 1313 | + } |
|
| 1314 | + |
|
| 1315 | + /** |
|
| 1316 | + * Set the log instance |
|
| 1317 | + * @param Log $logger the log object |
|
| 1318 | + */ |
|
| 1319 | + public function setLogger($logger){ |
|
| 1320 | + $this->logger = $logger; |
|
| 1321 | + return $this; |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + /** |
|
| 1325 | + * Return the cache instance |
|
| 1326 | + * @return CacheInterface |
|
| 1327 | + */ |
|
| 1328 | + public function getCacheInstance(){ |
|
| 1329 | + return $this->cacheInstance; |
|
| 1330 | + } |
|
| 1331 | + |
|
| 1332 | + /** |
|
| 1333 | + * Set the cache instance |
|
| 1334 | + * @param CacheInterface $cache the cache object |
|
| 1335 | + */ |
|
| 1336 | + public function setCacheInstance($cache){ |
|
| 1337 | + $this->cacheInstance = $cache; |
|
| 1338 | + return $this; |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + /** |
|
| 1342 | + * Return the benchmark instance |
|
| 1343 | + * @return Benchmark |
|
| 1344 | + */ |
|
| 1345 | + public function getBenchmark(){ |
|
| 1346 | + return $this->benchmarkInstance; |
|
| 1347 | + } |
|
| 1348 | + |
|
| 1349 | + /** |
|
| 1350 | + * Set the benchmark instance |
|
| 1351 | + * @param Benchmark $cache the cache object |
|
| 1352 | + */ |
|
| 1353 | + public function setBenchmark($benchmark){ |
|
| 1354 | + $this->benchmarkInstance = $benchmark; |
|
| 1355 | + return $this; |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + /** |
|
| 1359 | + * Return the data to be used for insert, update, etc. |
|
| 1360 | + * @return array |
|
| 1361 | + */ |
|
| 1362 | + public function getData(){ |
|
| 1363 | + return $this->data; |
|
| 1364 | + } |
|
| 1365 | + |
|
| 1366 | + /** |
|
| 1367 | + * Set the data to be used for insert, update, etc. |
|
| 1368 | + * @param string $key the data key identified |
|
| 1369 | + * @param mixed $value the data value |
|
| 1370 | + * @param boolean $escape whether to escape or not the $value |
|
| 1371 | + * @return object the current Database instance |
|
| 1372 | + */ |
|
| 1373 | + public function setData($key, $value, $escape = true){ |
|
| 1374 | + $this->data[$key] = $escape ? $this->escape($value) : $value; |
|
| 1375 | + return $this; |
|
| 1376 | + } |
|
| 1377 | + |
|
| 1378 | + /** |
|
| 1379 | + * Set the Log instance using argument or create new instance |
|
| 1380 | + * @param object $logger the Log instance if not null |
|
| 1381 | + */ |
|
| 1382 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1383 | + if($logger !== null){ |
|
| 1384 | + $this->logger = $logger; |
|
| 1385 | + } |
|
| 1386 | + else{ |
|
| 1387 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 1388 | + $this->logger->setLogger('Library::Database'); |
|
| 1389 | + } |
|
| 1390 | + } |
|
| 1391 | 1391 | |
| 1392 | 1392 | /** |
| 1393 | 1393 | * Setting the database configuration using the configuration file |
| 1394 | 1394 | * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
| 1395 | 1395 | */ |
| 1396 | - protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1397 | - $db = array(); |
|
| 1398 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1399 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 1400 | - require CONFIG_PATH . 'database.php'; |
|
| 1401 | - } |
|
| 1396 | + protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1397 | + $db = array(); |
|
| 1398 | + if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1399 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 1400 | + require CONFIG_PATH . 'database.php'; |
|
| 1401 | + } |
|
| 1402 | 1402 | |
| 1403 | - if(! empty($overwriteConfig)){ |
|
| 1404 | - $db = array_merge($db, $overwriteConfig); |
|
| 1405 | - } |
|
| 1406 | - $config = array(); |
|
| 1407 | - $config['driver'] = isset($db['driver']) ? $db['driver'] : 'mysql'; |
|
| 1408 | - $config['username'] = isset($db['username']) ? $db['username'] : 'root'; |
|
| 1409 | - $config['password'] = isset($db['password']) ? $db['password'] : ''; |
|
| 1410 | - $config['database'] = isset($db['database']) ? $db['database'] : ''; |
|
| 1411 | - $config['hostname'] = isset($db['hostname']) ? $db['hostname'] : 'localhost'; |
|
| 1412 | - $config['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8'; |
|
| 1413 | - $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
|
| 1414 | - $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
|
| 1415 | - $port = ''; |
|
| 1416 | - if(strstr($config['hostname'], ':')){ |
|
| 1417 | - $p = explode(':', $config['hostname']); |
|
| 1418 | - $port = isset($p[1]) ? $p[1] : ''; |
|
| 1419 | - $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
| 1420 | - } |
|
| 1421 | - $config['port'] = $port; |
|
| 1422 | - $this->setDatabaseConfiguration($config); |
|
| 1423 | - } |
|
| 1424 | - |
|
| 1425 | - /** |
|
| 1426 | - * This method is used to get the PDO DSN string using th configured driver |
|
| 1427 | - * @return string the DSN string |
|
| 1428 | - */ |
|
| 1429 | - protected function getDsnFromDriver(){ |
|
| 1430 | - $config = $this->getDatabaseConfiguration(); |
|
| 1431 | - if(! empty($config)){ |
|
| 1432 | - $driverDsnMap = array( |
|
| 1433 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 1434 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1435 | - . 'dbname=' . $config['database'], |
|
| 1436 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 1437 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1438 | - . 'dbname=' . $config['database'], |
|
| 1439 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 1440 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 1441 | - . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 1442 | - . '/' . $config['database'] |
|
| 1443 | - ); |
|
| 1444 | - return isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 1445 | - } |
|
| 1403 | + if(! empty($overwriteConfig)){ |
|
| 1404 | + $db = array_merge($db, $overwriteConfig); |
|
| 1405 | + } |
|
| 1406 | + $config = array(); |
|
| 1407 | + $config['driver'] = isset($db['driver']) ? $db['driver'] : 'mysql'; |
|
| 1408 | + $config['username'] = isset($db['username']) ? $db['username'] : 'root'; |
|
| 1409 | + $config['password'] = isset($db['password']) ? $db['password'] : ''; |
|
| 1410 | + $config['database'] = isset($db['database']) ? $db['database'] : ''; |
|
| 1411 | + $config['hostname'] = isset($db['hostname']) ? $db['hostname'] : 'localhost'; |
|
| 1412 | + $config['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8'; |
|
| 1413 | + $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
|
| 1414 | + $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
|
| 1415 | + $port = ''; |
|
| 1416 | + if(strstr($config['hostname'], ':')){ |
|
| 1417 | + $p = explode(':', $config['hostname']); |
|
| 1418 | + $port = isset($p[1]) ? $p[1] : ''; |
|
| 1419 | + $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
|
| 1420 | + } |
|
| 1421 | + $config['port'] = $port; |
|
| 1422 | + $this->setDatabaseConfiguration($config); |
|
| 1423 | + } |
|
| 1424 | + |
|
| 1425 | + /** |
|
| 1426 | + * This method is used to get the PDO DSN string using th configured driver |
|
| 1427 | + * @return string the DSN string |
|
| 1428 | + */ |
|
| 1429 | + protected function getDsnFromDriver(){ |
|
| 1430 | + $config = $this->getDatabaseConfiguration(); |
|
| 1431 | + if(! empty($config)){ |
|
| 1432 | + $driverDsnMap = array( |
|
| 1433 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 1434 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1435 | + . 'dbname=' . $config['database'], |
|
| 1436 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 1437 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 1438 | + . 'dbname=' . $config['database'], |
|
| 1439 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 1440 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 1441 | + . (($config['port']) != '' ? ':' . $config['port'] : '') |
|
| 1442 | + . '/' . $config['database'] |
|
| 1443 | + ); |
|
| 1444 | + return isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : ''; |
|
| 1445 | + } |
|
| 1446 | 1446 | |
| 1447 | - return null; |
|
| 1448 | - } |
|
| 1447 | + return null; |
|
| 1448 | + } |
|
| 1449 | 1449 | |
| 1450 | 1450 | |
| 1451 | 1451 | /** |
| 1452 | 1452 | * Reset the database class attributs to the initail values before each query. |
| 1453 | 1453 | */ |
| 1454 | 1454 | private function reset(){ |
| 1455 | - $this->select = '*'; |
|
| 1456 | - $this->from = null; |
|
| 1457 | - $this->where = null; |
|
| 1458 | - $this->limit = null; |
|
| 1459 | - $this->orderBy = null; |
|
| 1460 | - $this->groupBy = null; |
|
| 1461 | - $this->having = null; |
|
| 1462 | - $this->join = null; |
|
| 1463 | - $this->numRows = 0; |
|
| 1464 | - $this->insertId = null; |
|
| 1465 | - $this->query = null; |
|
| 1466 | - $this->error = null; |
|
| 1467 | - $this->result = array(); |
|
| 1468 | - $this->data = array(); |
|
| 1455 | + $this->select = '*'; |
|
| 1456 | + $this->from = null; |
|
| 1457 | + $this->where = null; |
|
| 1458 | + $this->limit = null; |
|
| 1459 | + $this->orderBy = null; |
|
| 1460 | + $this->groupBy = null; |
|
| 1461 | + $this->having = null; |
|
| 1462 | + $this->join = null; |
|
| 1463 | + $this->numRows = 0; |
|
| 1464 | + $this->insertId = null; |
|
| 1465 | + $this->query = null; |
|
| 1466 | + $this->error = null; |
|
| 1467 | + $this->result = array(); |
|
| 1468 | + $this->data = array(); |
|
| 1469 | 1469 | } |
| 1470 | 1470 | |
| 1471 | 1471 | /** |
| 1472 | 1472 | * The class destructor |
| 1473 | 1473 | */ |
| 1474 | 1474 | public function __destruct(){ |
| 1475 | - $this->pdo = null; |
|
| 1475 | + $this->pdo = null; |
|
| 1476 | 1476 | } |
| 1477 | 1477 | |
| 1478 | 1478 | } |
@@ -23,165 +23,165 @@ discard block |
||
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | - class Database{ |
|
| 26 | + class Database { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | 31 | */ |
| 32 | - private $pdo = null; |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | 37 | */ |
| 38 | - private $databaseName = null; |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The SQL SELECT statment |
| 42 | 42 | * @var string |
| 43 | 43 | */ |
| 44 | - private $select = '*'; |
|
| 44 | + private $select = '*'; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The SQL FROM statment |
| 48 | 48 | * @var string |
| 49 | 49 | */ |
| 50 | - private $from = null; |
|
| 50 | + private $from = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The SQL WHERE statment |
| 54 | 54 | * @var string |
| 55 | 55 | */ |
| 56 | - private $where = null; |
|
| 56 | + private $where = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The SQL LIMIT statment |
| 60 | 60 | * @var string |
| 61 | 61 | */ |
| 62 | - private $limit = null; |
|
| 62 | + private $limit = null; |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The SQL JOIN statment |
| 66 | 66 | * @var string |
| 67 | 67 | */ |
| 68 | - private $join = null; |
|
| 68 | + private $join = null; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The SQL ORDER BY statment |
| 72 | 72 | * @var string |
| 73 | 73 | */ |
| 74 | - private $orderBy = null; |
|
| 74 | + private $orderBy = null; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The SQL GROUP BY statment |
| 78 | 78 | * @var string |
| 79 | 79 | */ |
| 80 | - private $groupBy = null; |
|
| 80 | + private $groupBy = null; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The SQL HAVING statment |
| 84 | 84 | * @var string |
| 85 | 85 | */ |
| 86 | - private $having = null; |
|
| 86 | + private $having = null; |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The number of rows returned by the last query |
| 90 | 90 | * @var int |
| 91 | 91 | */ |
| 92 | - private $numRows = 0; |
|
| 92 | + private $numRows = 0; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The last insert id for the primary key column that have auto increment or sequence |
| 96 | 96 | * @var mixed |
| 97 | 97 | */ |
| 98 | - private $insertId = null; |
|
| 98 | + private $insertId = null; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The full SQL query statment after build for each command |
| 102 | 102 | * @var string |
| 103 | 103 | */ |
| 104 | - private $query = null; |
|
| 104 | + private $query = null; |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * The error returned for the last query |
| 108 | 108 | * @var string |
| 109 | 109 | */ |
| 110 | - private $error = null; |
|
| 110 | + private $error = null; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The result returned for the last query |
| 114 | 114 | * @var mixed |
| 115 | 115 | */ |
| 116 | - private $result = array(); |
|
| 116 | + private $result = array(); |
|
| 117 | 117 | |
| 118 | 118 | /** |
| 119 | 119 | * The prefix used in each database table |
| 120 | 120 | * @var string |
| 121 | 121 | */ |
| 122 | - private $prefix = null; |
|
| 122 | + private $prefix = null; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * The list of SQL valid operators |
| 126 | 126 | * @var array |
| 127 | 127 | */ |
| 128 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
| 128 | + private $operatorList = array('=', '!=', '<', '>', '<=', '>=', '<>'); |
|
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | 131 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 132 | 132 | * @var int |
| 133 | 133 | */ |
| 134 | - private $cacheTtl = 0; |
|
| 134 | + private $cacheTtl = 0; |
|
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * The cache current time to live. 0 means no need to use the cache feature |
| 138 | 138 | * @var int |
| 139 | 139 | */ |
| 140 | - private $temporaryCacheTtl = 0; |
|
| 140 | + private $temporaryCacheTtl = 0; |
|
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * The number of executed query for the current request |
| 144 | 144 | * @var int |
| 145 | 145 | */ |
| 146 | - private $queryCount = 0; |
|
| 146 | + private $queryCount = 0; |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * The default data to be used in the statments query INSERT, UPDATE |
| 150 | 150 | * @var array |
| 151 | 151 | */ |
| 152 | - private $data = array(); |
|
| 152 | + private $data = array(); |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * The database configuration |
| 156 | 156 | * @var array |
| 157 | 157 | */ |
| 158 | - private $config = array(); |
|
| 158 | + private $config = array(); |
|
| 159 | 159 | |
| 160 | 160 | /** |
| 161 | 161 | * The logger instance |
| 162 | 162 | * @var Log |
| 163 | 163 | */ |
| 164 | - private $logger = null; |
|
| 164 | + private $logger = null; |
|
| 165 | 165 | |
| 166 | 166 | |
| 167 | 167 | /** |
| 168 | 168 | * The cache instance |
| 169 | 169 | * @var CacheInterface |
| 170 | 170 | */ |
| 171 | - private $cacheInstance = null; |
|
| 171 | + private $cacheInstance = null; |
|
| 172 | 172 | |
| 173 | 173 | /** |
| 174 | 174 | * The benchmark instance |
| 175 | 175 | * @var Benchmark |
| 176 | 176 | */ |
| 177 | - private $benchmarkInstance = null; |
|
| 177 | + private $benchmarkInstance = null; |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | 181 | * Construct new database |
| 182 | 182 | * @param array $overwriteConfig the config to overwrite with the config set in database.php |
| 183 | 183 | */ |
| 184 | - public function __construct($overwriteConfig = array()){ |
|
| 184 | + public function __construct($overwriteConfig = array()) { |
|
| 185 | 185 | //Set Log instance to use |
| 186 | 186 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 187 | 187 | |
@@ -195,23 +195,23 @@ discard block |
||
| 195 | 195 | * This is used to connect to database |
| 196 | 196 | * @return bool |
| 197 | 197 | */ |
| 198 | - public function connect(){ |
|
| 198 | + public function connect() { |
|
| 199 | 199 | $config = $this->getDatabaseConfiguration(); |
| 200 | - if(! empty($config)){ |
|
| 201 | - try{ |
|
| 200 | + if (!empty($config)) { |
|
| 201 | + try { |
|
| 202 | 202 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
| 203 | 203 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 204 | 204 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 205 | 205 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 206 | 206 | return true; |
| 207 | 207 | } |
| 208 | - catch (PDOException $e){ |
|
| 208 | + catch (PDOException $e) { |
|
| 209 | 209 | $this->logger->fatal($e->getMessage()); |
| 210 | 210 | show_error('Cannot connect to Database.'); |
| 211 | 211 | return false; |
| 212 | 212 | } |
| 213 | 213 | } |
| 214 | - else{ |
|
| 214 | + else { |
|
| 215 | 215 | show_error('Database configuration is not set.'); |
| 216 | 216 | return false; |
| 217 | 217 | } |
@@ -222,15 +222,15 @@ discard block |
||
| 222 | 222 | * @param string|array $table the table name or array of table list |
| 223 | 223 | * @return object the current Database instance |
| 224 | 224 | */ |
| 225 | - public function from($table){ |
|
| 226 | - if(is_array($table)){ |
|
| 225 | + public function from($table) { |
|
| 226 | + if (is_array($table)) { |
|
| 227 | 227 | $froms = ''; |
| 228 | - foreach($table as $key){ |
|
| 228 | + foreach ($table as $key) { |
|
| 229 | 229 | $froms .= $this->prefix . $key . ', '; |
| 230 | 230 | } |
| 231 | 231 | $this->from = rtrim($froms, ', '); |
| 232 | 232 | } |
| 233 | - else{ |
|
| 233 | + else { |
|
| 234 | 234 | $this->from = $this->prefix . $table; |
| 235 | 235 | } |
| 236 | 236 | return $this; |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | * @param string|array $fields the field name or array of field list |
| 242 | 242 | * @return object the current Database instance |
| 243 | 243 | */ |
| 244 | - public function select($fields){ |
|
| 244 | + public function select($fields) { |
|
| 245 | 245 | $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
| 246 | 246 | $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select); |
| 247 | 247 | return $this; |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | * @param string $field the field name to distinct |
| 253 | 253 | * @return object the current Database instance |
| 254 | 254 | */ |
| 255 | - public function distinct($field){ |
|
| 255 | + public function distinct($field) { |
|
| 256 | 256 | $distinct = ' DISTINCT ' . $field; |
| 257 | 257 | $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct); |
| 258 | 258 | |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | * @param string $name if is not null represent the alias used for this field in the result |
| 266 | 266 | * @return object the current Database instance |
| 267 | 267 | */ |
| 268 | - public function max($field, $name = null){ |
|
| 268 | + public function max($field, $name = null) { |
|
| 269 | 269 | $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 270 | 270 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 271 | 271 | return $this; |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | * @param string $name if is not null represent the alias used for this field in the result |
| 278 | 278 | * @return object the current Database instance |
| 279 | 279 | */ |
| 280 | - public function min($field, $name = null){ |
|
| 280 | + public function min($field, $name = null) { |
|
| 281 | 281 | $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 282 | 282 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 283 | 283 | return $this; |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | * @param string $name if is not null represent the alias used for this field in the result |
| 290 | 290 | * @return object the current Database instance |
| 291 | 291 | */ |
| 292 | - public function sum($field, $name = null){ |
|
| 292 | + public function sum($field, $name = null) { |
|
| 293 | 293 | $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 294 | 294 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 295 | 295 | return $this; |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * @param string $name if is not null represent the alias used for this field in the result |
| 302 | 302 | * @return object the current Database instance |
| 303 | 303 | */ |
| 304 | - public function count($field = '*', $name = null){ |
|
| 304 | + public function count($field = '*', $name = null) { |
|
| 305 | 305 | $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 306 | 306 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 307 | 307 | return $this; |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | * @param string $name if is not null represent the alias used for this field in the result |
| 314 | 314 | * @return object the current Database instance |
| 315 | 315 | */ |
| 316 | - public function avg($field, $name = null){ |
|
| 316 | + public function avg($field, $name = null) { |
|
| 317 | 317 | $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
| 318 | 318 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
| 319 | 319 | return $this; |
@@ -328,16 +328,16 @@ discard block |
||
| 328 | 328 | * @param string $type the type of join (INNER, LEFT, RIGHT) |
| 329 | 329 | * @return object the current Database instance |
| 330 | 330 | */ |
| 331 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
| 331 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = '') { |
|
| 332 | 332 | $on = $field1; |
| 333 | 333 | $table = $this->prefix . $table; |
| 334 | - if(! is_null($op)){ |
|
| 335 | - $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 334 | + if (!is_null($op)) { |
|
| 335 | + $on = (!in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2); |
|
| 336 | 336 | } |
| 337 | - if (empty($this->join)){ |
|
| 337 | + if (empty($this->join)) { |
|
| 338 | 338 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 339 | 339 | } |
| 340 | - else{ |
|
| 340 | + else { |
|
| 341 | 341 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 342 | 342 | } |
| 343 | 343 | return $this; |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | * @see Database::join() |
| 349 | 349 | * @return object the current Database instance |
| 350 | 350 | */ |
| 351 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 351 | + public function innerJoin($table, $field1, $op = null, $field2 = '') { |
|
| 352 | 352 | return $this->join($table, $field1, $op, $field2, 'INNER '); |
| 353 | 353 | } |
| 354 | 354 | |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | * @see Database::join() |
| 358 | 358 | * @return object the current Database instance |
| 359 | 359 | */ |
| 360 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 360 | + public function leftJoin($table, $field1, $op = null, $field2 = '') { |
|
| 361 | 361 | return $this->join($table, $field1, $op, $field2, 'LEFT '); |
| 362 | 362 | } |
| 363 | 363 | |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | * @see Database::join() |
| 367 | 367 | * @return object the current Database instance |
| 368 | 368 | */ |
| 369 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 369 | + public function rightJoin($table, $field1, $op = null, $field2 = '') { |
|
| 370 | 370 | return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
| 371 | 371 | } |
| 372 | 372 | |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | * @see Database::join() |
| 376 | 376 | * @return object the current Database instance |
| 377 | 377 | */ |
| 378 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 378 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 379 | 379 | return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
| 380 | 380 | } |
| 381 | 381 | |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | * @see Database::join() |
| 385 | 385 | * @return object the current Database instance |
| 386 | 386 | */ |
| 387 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 387 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 388 | 388 | return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
| 389 | 389 | } |
| 390 | 390 | |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | * @see Database::join() |
| 394 | 394 | * @return object the current Database instance |
| 395 | 395 | */ |
| 396 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
| 396 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
| 397 | 397 | return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
| 398 | 398 | } |
| 399 | 399 | |
@@ -403,18 +403,18 @@ discard block |
||
| 403 | 403 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 404 | 404 | * @return object the current Database instance |
| 405 | 405 | */ |
| 406 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
| 407 | - if(is_array($field)){ |
|
| 408 | - foreach($field as $f){ |
|
| 406 | + public function whereIsNull($field, $andOr = 'AND') { |
|
| 407 | + if (is_array($field)) { |
|
| 408 | + foreach ($field as $f) { |
|
| 409 | 409 | $this->whereIsNull($f, $andOr); |
| 410 | 410 | } |
| 411 | 411 | } |
| 412 | - else{ |
|
| 413 | - if (! $this->where){ |
|
| 414 | - $this->where = $field.' IS NULL '; |
|
| 412 | + else { |
|
| 413 | + if (!$this->where) { |
|
| 414 | + $this->where = $field . ' IS NULL '; |
|
| 415 | 415 | } |
| 416 | - else{ |
|
| 417 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
|
| 416 | + else { |
|
| 417 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' IS NULL '; |
|
| 418 | 418 | } |
| 419 | 419 | } |
| 420 | 420 | return $this; |
@@ -426,18 +426,18 @@ discard block |
||
| 426 | 426 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 427 | 427 | * @return object the current Database instance |
| 428 | 428 | */ |
| 429 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
| 430 | - if(is_array($field)){ |
|
| 431 | - foreach($field as $f){ |
|
| 429 | + public function whereIsNotNull($field, $andOr = 'AND') { |
|
| 430 | + if (is_array($field)) { |
|
| 431 | + foreach ($field as $f) { |
|
| 432 | 432 | $this->whereIsNotNull($f, $andOr); |
| 433 | 433 | } |
| 434 | 434 | } |
| 435 | - else{ |
|
| 436 | - if (! $this->where){ |
|
| 437 | - $this->where = $field.' IS NOT NULL '; |
|
| 435 | + else { |
|
| 436 | + if (!$this->where) { |
|
| 437 | + $this->where = $field . ' IS NOT NULL '; |
|
| 438 | 438 | } |
| 439 | - else{ |
|
| 440 | - $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
|
| 439 | + else { |
|
| 440 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' IS NOT NULL '; |
|
| 441 | 441 | } |
| 442 | 442 | } |
| 443 | 443 | return $this; |
@@ -449,15 +449,15 @@ discard block |
||
| 449 | 449 | * |
| 450 | 450 | * @return string |
| 451 | 451 | */ |
| 452 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
| 452 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true) { |
|
| 453 | 453 | $_where = array(); |
| 454 | - foreach ($where as $column => $data){ |
|
| 455 | - if(is_null($data)){ |
|
| 454 | + foreach ($where as $column => $data) { |
|
| 455 | + if (is_null($data)) { |
|
| 456 | 456 | $data = ''; |
| 457 | 457 | } |
| 458 | 458 | $_where[] = $type . $column . ' = ' . ($escape ? $this->escape($data) : $data); |
| 459 | 459 | } |
| 460 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 460 | + $where = implode(' ' . $andOr . ' ', $_where); |
|
| 461 | 461 | return $where; |
| 462 | 462 | } |
| 463 | 463 | |
@@ -467,12 +467,12 @@ discard block |
||
| 467 | 467 | * |
| 468 | 468 | * @return string |
| 469 | 469 | */ |
| 470 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
| 470 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true) { |
|
| 471 | 471 | $x = explode('?', $where); |
| 472 | 472 | $w = ''; |
| 473 | - foreach($x as $k => $v){ |
|
| 474 | - if(! empty($v)){ |
|
| 475 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 473 | + foreach ($x as $k => $v) { |
|
| 474 | + if (!empty($v)) { |
|
| 475 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 476 | 476 | $op[$k] = ''; |
| 477 | 477 | } |
| 478 | 478 | $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
@@ -487,16 +487,16 @@ discard block |
||
| 487 | 487 | * |
| 488 | 488 | * @return string |
| 489 | 489 | */ |
| 490 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
| 490 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true) { |
|
| 491 | 491 | $w = ''; |
| 492 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 493 | - if(is_null($op)){ |
|
| 492 | + if (!in_array((string) $op, $this->operatorList)) { |
|
| 493 | + if (is_null($op)) { |
|
| 494 | 494 | $op = ''; |
| 495 | 495 | } |
| 496 | 496 | $w = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
| 497 | 497 | } |
| 498 | - else{ |
|
| 499 | - if(is_null($val)){ |
|
| 498 | + else { |
|
| 499 | + if (is_null($val)) { |
|
| 500 | 500 | $val = ''; |
| 501 | 501 | } |
| 502 | 502 | $w = $type . $where . $op . ($escape ? $this->escape($val) : $val); |
@@ -509,16 +509,16 @@ discard block |
||
| 509 | 509 | * @param string $whereStr the WHERE clause string |
| 510 | 510 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
| 511 | 511 | */ |
| 512 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 513 | - if (empty($this->where)){ |
|
| 512 | + protected function setWhereStr($whereStr, $andOr = 'AND') { |
|
| 513 | + if (empty($this->where)) { |
|
| 514 | 514 | $this->where = $whereStr; |
| 515 | 515 | } |
| 516 | - else{ |
|
| 517 | - if(substr($this->where, -1) == '('){ |
|
| 516 | + else { |
|
| 517 | + if (substr($this->where, -1) == '(') { |
|
| 518 | 518 | $this->where = $this->where . ' ' . $whereStr; |
| 519 | 519 | } |
| 520 | - else{ |
|
| 521 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
| 520 | + else { |
|
| 521 | + $this->where = $this->where . ' ' . $andOr . ' ' . $whereStr; |
|
| 522 | 522 | } |
| 523 | 523 | } |
| 524 | 524 | } |
@@ -533,13 +533,13 @@ discard block |
||
| 533 | 533 | * @param boolean $escape whether to escape or not the $val |
| 534 | 534 | * @return object the current Database instance |
| 535 | 535 | */ |
| 536 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
| 536 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true) { |
|
| 537 | 537 | $whereStr = ''; |
| 538 | - if (is_array($where)){ |
|
| 538 | + if (is_array($where)) { |
|
| 539 | 539 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 540 | 540 | } |
| 541 | - else{ |
|
| 542 | - if(is_array($op)){ |
|
| 541 | + else { |
|
| 542 | + if (is_array($op)) { |
|
| 543 | 543 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
| 544 | 544 | } else { |
| 545 | 545 | $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
@@ -554,7 +554,7 @@ discard block |
||
| 554 | 554 | * @see Database::where() |
| 555 | 555 | * @return object the current Database instance |
| 556 | 556 | */ |
| 557 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
| 557 | + public function orWhere($where, $op = null, $val = null, $escape = true) { |
|
| 558 | 558 | return $this->where($where, $op, $val, '', 'OR', $escape); |
| 559 | 559 | } |
| 560 | 560 | |
@@ -564,7 +564,7 @@ discard block |
||
| 564 | 564 | * @see Database::where() |
| 565 | 565 | * @return object the current Database instance |
| 566 | 566 | */ |
| 567 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
| 567 | + public function notWhere($where, $op = null, $val = null, $escape = true) { |
|
| 568 | 568 | return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
| 569 | 569 | } |
| 570 | 570 | |
@@ -573,7 +573,7 @@ discard block |
||
| 573 | 573 | * @see Database::where() |
| 574 | 574 | * @return object the current Database instance |
| 575 | 575 | */ |
| 576 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
| 576 | + public function orNotWhere($where, $op = null, $val = null, $escape = true) { |
|
| 577 | 577 | return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
| 578 | 578 | } |
| 579 | 579 | |
@@ -583,15 +583,15 @@ discard block |
||
| 583 | 583 | * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
| 584 | 584 | * @return object the current Database instance |
| 585 | 585 | */ |
| 586 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
| 587 | - if (empty($this->where)){ |
|
| 586 | + public function groupStart($type = '', $andOr = ' AND') { |
|
| 587 | + if (empty($this->where)) { |
|
| 588 | 588 | $this->where = $type . ' ('; |
| 589 | 589 | } |
| 590 | - else{ |
|
| 591 | - if(substr($this->where, -1) == '('){ |
|
| 590 | + else { |
|
| 591 | + if (substr($this->where, -1) == '(') { |
|
| 592 | 592 | $this->where .= $type . ' ('; |
| 593 | 593 | } |
| 594 | - else{ |
|
| 594 | + else { |
|
| 595 | 595 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 596 | 596 | } |
| 597 | 597 | } |
@@ -603,7 +603,7 @@ discard block |
||
| 603 | 603 | * @see Database::groupStart() |
| 604 | 604 | * @return object the current Database instance |
| 605 | 605 | */ |
| 606 | - public function notGroupStart(){ |
|
| 606 | + public function notGroupStart() { |
|
| 607 | 607 | return $this->groupStart('NOT'); |
| 608 | 608 | } |
| 609 | 609 | |
@@ -612,7 +612,7 @@ discard block |
||
| 612 | 612 | * @see Database::groupStart() |
| 613 | 613 | * @return object the current Database instance |
| 614 | 614 | */ |
| 615 | - public function orGroupStart(){ |
|
| 615 | + public function orGroupStart() { |
|
| 616 | 616 | return $this->groupStart('', ' OR'); |
| 617 | 617 | } |
| 618 | 618 | |
@@ -621,7 +621,7 @@ discard block |
||
| 621 | 621 | * @see Database::groupStart() |
| 622 | 622 | * @return object the current Database instance |
| 623 | 623 | */ |
| 624 | - public function orNotGroupStart(){ |
|
| 624 | + public function orNotGroupStart() { |
|
| 625 | 625 | return $this->groupStart('NOT', ' OR'); |
| 626 | 626 | } |
| 627 | 627 | |
@@ -629,7 +629,7 @@ discard block |
||
| 629 | 629 | * Close the parenthesis for the grouped SQL |
| 630 | 630 | * @return object the current Database instance |
| 631 | 631 | */ |
| 632 | - public function groupEnd(){ |
|
| 632 | + public function groupEnd() { |
|
| 633 | 633 | $this->where .= ')'; |
| 634 | 634 | return $this; |
| 635 | 635 | } |
@@ -643,10 +643,10 @@ discard block |
||
| 643 | 643 | * @param boolean $escape whether to escape or not the values |
| 644 | 644 | * @return object the current Database instance |
| 645 | 645 | */ |
| 646 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
| 646 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true) { |
|
| 647 | 647 | $_keys = array(); |
| 648 | - foreach ($keys as $k => $v){ |
|
| 649 | - if(is_null($v)){ |
|
| 648 | + foreach ($keys as $k => $v) { |
|
| 649 | + if (is_null($v)) { |
|
| 650 | 650 | $v = ''; |
| 651 | 651 | } |
| 652 | 652 | $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v)); |
@@ -662,7 +662,7 @@ discard block |
||
| 662 | 662 | * @see Database::in() |
| 663 | 663 | * @return object the current Database instance |
| 664 | 664 | */ |
| 665 | - public function notIn($field, array $keys, $escape = true){ |
|
| 665 | + public function notIn($field, array $keys, $escape = true) { |
|
| 666 | 666 | return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
| 667 | 667 | } |
| 668 | 668 | |
@@ -671,7 +671,7 @@ discard block |
||
| 671 | 671 | * @see Database::in() |
| 672 | 672 | * @return object the current Database instance |
| 673 | 673 | */ |
| 674 | - public function orIn($field, array $keys, $escape = true){ |
|
| 674 | + public function orIn($field, array $keys, $escape = true) { |
|
| 675 | 675 | return $this->in($field, $keys, '', 'OR', $escape); |
| 676 | 676 | } |
| 677 | 677 | |
@@ -680,7 +680,7 @@ discard block |
||
| 680 | 680 | * @see Database::in() |
| 681 | 681 | * @return object the current Database instance |
| 682 | 682 | */ |
| 683 | - public function orNotIn($field, array $keys, $escape = true){ |
|
| 683 | + public function orNotIn($field, array $keys, $escape = true) { |
|
| 684 | 684 | return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
| 685 | 685 | } |
| 686 | 686 | |
@@ -694,11 +694,11 @@ discard block |
||
| 694 | 694 | * @param boolean $escape whether to escape or not the values |
| 695 | 695 | * @return object the current Database instance |
| 696 | 696 | */ |
| 697 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
| 698 | - if(is_null($value1)){ |
|
| 697 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true) { |
|
| 698 | + if (is_null($value1)) { |
|
| 699 | 699 | $value1 = ''; |
| 700 | 700 | } |
| 701 | - if(is_null($value2)){ |
|
| 701 | + if (is_null($value2)) { |
|
| 702 | 702 | $value2 = ''; |
| 703 | 703 | } |
| 704 | 704 | $whereStr = $field . ' ' . $type . ' BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2); |
@@ -711,7 +711,7 @@ discard block |
||
| 711 | 711 | * @see Database::between() |
| 712 | 712 | * @return object the current Database instance |
| 713 | 713 | */ |
| 714 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
| 714 | + public function notBetween($field, $value1, $value2, $escape = true) { |
|
| 715 | 715 | return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
| 716 | 716 | } |
| 717 | 717 | |
@@ -720,7 +720,7 @@ discard block |
||
| 720 | 720 | * @see Database::between() |
| 721 | 721 | * @return object the current Database instance |
| 722 | 722 | */ |
| 723 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
| 723 | + public function orBetween($field, $value1, $value2, $escape = true) { |
|
| 724 | 724 | return $this->between($field, $value1, $value2, '', 'OR', $escape); |
| 725 | 725 | } |
| 726 | 726 | |
@@ -729,7 +729,7 @@ discard block |
||
| 729 | 729 | * @see Database::between() |
| 730 | 730 | * @return object the current Database instance |
| 731 | 731 | */ |
| 732 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
| 732 | + public function orNotBetween($field, $value1, $value2, $escape = true) { |
|
| 733 | 733 | return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
| 734 | 734 | } |
| 735 | 735 | |
@@ -742,20 +742,20 @@ discard block |
||
| 742 | 742 | * @param boolean $escape whether to escape or not the values |
| 743 | 743 | * @return object the current Database instance |
| 744 | 744 | */ |
| 745 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
| 746 | - if(empty($data)){ |
|
| 745 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true) { |
|
| 746 | + if (empty($data)) { |
|
| 747 | 747 | $data = ''; |
| 748 | 748 | } |
| 749 | 749 | $like = $escape ? $this->escape($data) : $data; |
| 750 | - if (empty($this->where)){ |
|
| 750 | + if (empty($this->where)) { |
|
| 751 | 751 | $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
| 752 | 752 | } |
| 753 | - else{ |
|
| 754 | - if(substr($this->where, -1) == '('){ |
|
| 753 | + else { |
|
| 754 | + if (substr($this->where, -1) == '(') { |
|
| 755 | 755 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 756 | 756 | } |
| 757 | - else{ |
|
| 758 | - $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 757 | + else { |
|
| 758 | + $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
|
| 759 | 759 | } |
| 760 | 760 | } |
| 761 | 761 | return $this; |
@@ -766,7 +766,7 @@ discard block |
||
| 766 | 766 | * @see Database::like() |
| 767 | 767 | * @return object the current Database instance |
| 768 | 768 | */ |
| 769 | - public function orLike($field, $data, $escape = true){ |
|
| 769 | + public function orLike($field, $data, $escape = true) { |
|
| 770 | 770 | return $this->like($field, $data, '', 'OR', $escape); |
| 771 | 771 | } |
| 772 | 772 | |
@@ -775,7 +775,7 @@ discard block |
||
| 775 | 775 | * @see Database::like() |
| 776 | 776 | * @return object the current Database instance |
| 777 | 777 | */ |
| 778 | - public function notLike($field, $data, $escape = true){ |
|
| 778 | + public function notLike($field, $data, $escape = true) { |
|
| 779 | 779 | return $this->like($field, $data, 'NOT ', 'AND', $escape); |
| 780 | 780 | } |
| 781 | 781 | |
@@ -784,7 +784,7 @@ discard block |
||
| 784 | 784 | * @see Database::like() |
| 785 | 785 | * @return object the current Database instance |
| 786 | 786 | */ |
| 787 | - public function orNotLike($field, $data, $escape = true){ |
|
| 787 | + public function orNotLike($field, $data, $escape = true) { |
|
| 788 | 788 | return $this->like($field, $data, 'NOT ', 'OR', $escape); |
| 789 | 789 | } |
| 790 | 790 | |
@@ -795,14 +795,14 @@ discard block |
||
| 795 | 795 | * @param int $limitEnd the limit count |
| 796 | 796 | * @return object the current Database instance |
| 797 | 797 | */ |
| 798 | - public function limit($limit, $limitEnd = null){ |
|
| 799 | - if(empty($limit)){ |
|
| 798 | + public function limit($limit, $limitEnd = null) { |
|
| 799 | + if (empty($limit)) { |
|
| 800 | 800 | return; |
| 801 | 801 | } |
| 802 | - if (! is_null($limitEnd)){ |
|
| 802 | + if (!is_null($limitEnd)) { |
|
| 803 | 803 | $this->limit = $limit . ', ' . $limitEnd; |
| 804 | 804 | } |
| 805 | - else{ |
|
| 805 | + else { |
|
| 806 | 806 | $this->limit = $limit; |
| 807 | 807 | } |
| 808 | 808 | return $this; |
@@ -814,16 +814,16 @@ discard block |
||
| 814 | 814 | * @param string $orderDir the order direction (ASC or DESC) |
| 815 | 815 | * @return object the current Database instance |
| 816 | 816 | */ |
| 817 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
| 818 | - if (! empty($orderDir)){ |
|
| 819 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 817 | + public function orderBy($orderBy, $orderDir = ' ASC') { |
|
| 818 | + if (!empty($orderDir)) { |
|
| 819 | + $this->orderBy = !$this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 820 | 820 | } |
| 821 | - else{ |
|
| 822 | - if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 823 | - $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 821 | + else { |
|
| 822 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()') { |
|
| 823 | + $this->orderBy = !$this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
|
| 824 | 824 | } |
| 825 | - else{ |
|
| 826 | - $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 825 | + else { |
|
| 826 | + $this->orderBy = !$this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
|
| 827 | 827 | } |
| 828 | 828 | } |
| 829 | 829 | return $this; |
@@ -834,11 +834,11 @@ discard block |
||
| 834 | 834 | * @param string|array $field the field name used or array of field list |
| 835 | 835 | * @return object the current Database instance |
| 836 | 836 | */ |
| 837 | - public function groupBy($field){ |
|
| 838 | - if(is_array($field)){ |
|
| 837 | + public function groupBy($field) { |
|
| 838 | + if (is_array($field)) { |
|
| 839 | 839 | $this->groupBy = implode(', ', $field); |
| 840 | 840 | } |
| 841 | - else{ |
|
| 841 | + else { |
|
| 842 | 842 | $this->groupBy = $field; |
| 843 | 843 | } |
| 844 | 844 | return $this; |
@@ -852,13 +852,13 @@ discard block |
||
| 852 | 852 | * @param boolean $escape whether to escape or not the values |
| 853 | 853 | * @return object the current Database instance |
| 854 | 854 | */ |
| 855 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
| 856 | - if(is_array($op)){ |
|
| 855 | + public function having($field, $op = null, $val = null, $escape = true) { |
|
| 856 | + if (is_array($op)) { |
|
| 857 | 857 | $x = explode('?', $field); |
| 858 | 858 | $w = ''; |
| 859 | - foreach($x as $k => $v){ |
|
| 860 | - if(!empty($v)){ |
|
| 861 | - if(isset($op[$k]) && is_null($op[$k])){ |
|
| 859 | + foreach ($x as $k => $v) { |
|
| 860 | + if (!empty($v)) { |
|
| 861 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
| 862 | 862 | $op[$k] = ''; |
| 863 | 863 | } |
| 864 | 864 | $w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : ''); |
@@ -866,14 +866,14 @@ discard block |
||
| 866 | 866 | } |
| 867 | 867 | $this->having = $w; |
| 868 | 868 | } |
| 869 | - else if (! in_array($op, $this->operatorList)){ |
|
| 870 | - if(is_null($op)){ |
|
| 869 | + else if (!in_array($op, $this->operatorList)) { |
|
| 870 | + if (is_null($op)) { |
|
| 871 | 871 | $op = ''; |
| 872 | 872 | } |
| 873 | 873 | $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
| 874 | 874 | } |
| 875 | - else{ |
|
| 876 | - if(is_null($val)){ |
|
| 875 | + else { |
|
| 876 | + if (is_null($val)) { |
|
| 877 | 877 | $val = ''; |
| 878 | 878 | } |
| 879 | 879 | $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val); |
@@ -885,7 +885,7 @@ discard block |
||
| 885 | 885 | * Return the number of rows returned by the current query |
| 886 | 886 | * @return int |
| 887 | 887 | */ |
| 888 | - public function numRows(){ |
|
| 888 | + public function numRows() { |
|
| 889 | 889 | return $this->numRows; |
| 890 | 890 | } |
| 891 | 891 | |
@@ -893,15 +893,15 @@ discard block |
||
| 893 | 893 | * Return the last insert id value |
| 894 | 894 | * @return mixed |
| 895 | 895 | */ |
| 896 | - public function insertId(){ |
|
| 896 | + public function insertId() { |
|
| 897 | 897 | return $this->insertId; |
| 898 | 898 | } |
| 899 | 899 | |
| 900 | 900 | /** |
| 901 | 901 | * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.) |
| 902 | 902 | */ |
| 903 | - public function error(){ |
|
| 904 | - if($this->error){ |
|
| 903 | + public function error() { |
|
| 904 | + if ($this->error) { |
|
| 905 | 905 | show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
| 906 | 906 | } |
| 907 | 907 | } |
@@ -912,14 +912,14 @@ discard block |
||
| 912 | 912 | * If is string will determine the result type "array" or "object" |
| 913 | 913 | * @return mixed the query SQL string or the record result |
| 914 | 914 | */ |
| 915 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 915 | + public function get($returnSQLQueryOrResultType = false) { |
|
| 916 | 916 | $this->limit = 1; |
| 917 | 917 | $query = $this->getAll(true); |
| 918 | - if($returnSQLQueryOrResultType === true){ |
|
| 918 | + if ($returnSQLQueryOrResultType === true) { |
|
| 919 | 919 | return $query; |
| 920 | 920 | } |
| 921 | - else{ |
|
| 922 | - return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 921 | + else { |
|
| 922 | + return $this->query($query, false, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 923 | 923 | } |
| 924 | 924 | } |
| 925 | 925 | |
@@ -929,37 +929,37 @@ discard block |
||
| 929 | 929 | * If is string will determine the result type "array" or "object" |
| 930 | 930 | * @return mixed the query SQL string or the record result |
| 931 | 931 | */ |
| 932 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 932 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
| 933 | 933 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
| 934 | - if (! empty($this->join)){ |
|
| 934 | + if (!empty($this->join)) { |
|
| 935 | 935 | $query .= $this->join; |
| 936 | 936 | } |
| 937 | 937 | |
| 938 | - if (! empty($this->where)){ |
|
| 938 | + if (!empty($this->where)) { |
|
| 939 | 939 | $query .= ' WHERE ' . $this->where; |
| 940 | 940 | } |
| 941 | 941 | |
| 942 | - if (! empty($this->groupBy)){ |
|
| 942 | + if (!empty($this->groupBy)) { |
|
| 943 | 943 | $query .= ' GROUP BY ' . $this->groupBy; |
| 944 | 944 | } |
| 945 | 945 | |
| 946 | - if (! empty($this->having)){ |
|
| 946 | + if (!empty($this->having)) { |
|
| 947 | 947 | $query .= ' HAVING ' . $this->having; |
| 948 | 948 | } |
| 949 | 949 | |
| 950 | - if (! empty($this->orderBy)){ |
|
| 950 | + if (!empty($this->orderBy)) { |
|
| 951 | 951 | $query .= ' ORDER BY ' . $this->orderBy; |
| 952 | 952 | } |
| 953 | 953 | |
| 954 | - if(! empty($this->limit)){ |
|
| 954 | + if (!empty($this->limit)) { |
|
| 955 | 955 | $query .= ' LIMIT ' . $this->limit; |
| 956 | 956 | } |
| 957 | 957 | |
| 958 | - if($returnSQLQueryOrResultType === true){ |
|
| 958 | + if ($returnSQLQueryOrResultType === true) { |
|
| 959 | 959 | return $query; |
| 960 | 960 | } |
| 961 | - else{ |
|
| 962 | - return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
|
| 961 | + else { |
|
| 962 | + return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false)); |
|
| 963 | 963 | } |
| 964 | 964 | } |
| 965 | 965 | |
@@ -969,15 +969,15 @@ discard block |
||
| 969 | 969 | * @param boolean $escape whether to escape or not the values |
| 970 | 970 | * @return mixed the insert id of the new record or null |
| 971 | 971 | */ |
| 972 | - public function insert($data = array(), $escape = true){ |
|
| 972 | + public function insert($data = array(), $escape = true) { |
|
| 973 | 973 | $column = array(); |
| 974 | 974 | $val = array(); |
| 975 | - if(empty($data) && $this->getData()){ |
|
| 975 | + if (empty($data) && $this->getData()) { |
|
| 976 | 976 | $columns = array_keys($this->getData()); |
| 977 | 977 | $column = implode(',', $columns); |
| 978 | 978 | $val = implode(', ', $this->getData()); |
| 979 | 979 | } |
| 980 | - else{ |
|
| 980 | + else { |
|
| 981 | 981 | $columns = array_keys($data); |
| 982 | 982 | $column = implode(',', $columns); |
| 983 | 983 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -986,14 +986,14 @@ discard block |
||
| 986 | 986 | $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
| 987 | 987 | $query = $this->query($query); |
| 988 | 988 | |
| 989 | - if ($query){ |
|
| 990 | - if(! $this->pdo){ |
|
| 989 | + if ($query) { |
|
| 990 | + if (!$this->pdo) { |
|
| 991 | 991 | $this->connect(); |
| 992 | 992 | } |
| 993 | 993 | $this->insertId = $this->pdo->lastInsertId(); |
| 994 | 994 | return $this->insertId(); |
| 995 | 995 | } |
| 996 | - else{ |
|
| 996 | + else { |
|
| 997 | 997 | return false; |
| 998 | 998 | } |
| 999 | 999 | } |
@@ -1004,29 +1004,29 @@ discard block |
||
| 1004 | 1004 | * @param boolean $escape whether to escape or not the values |
| 1005 | 1005 | * @return mixed the update status |
| 1006 | 1006 | */ |
| 1007 | - public function update($data = array(), $escape = true){ |
|
| 1007 | + public function update($data = array(), $escape = true) { |
|
| 1008 | 1008 | $query = 'UPDATE ' . $this->from . ' SET '; |
| 1009 | 1009 | $values = array(); |
| 1010 | - if(empty($data) && $this->getData()){ |
|
| 1011 | - foreach ($this->getData() as $column => $val){ |
|
| 1010 | + if (empty($data) && $this->getData()) { |
|
| 1011 | + foreach ($this->getData() as $column => $val) { |
|
| 1012 | 1012 | $values[] = $column . ' = ' . $val; |
| 1013 | 1013 | } |
| 1014 | 1014 | } |
| 1015 | - else{ |
|
| 1016 | - foreach ($data as $column => $val){ |
|
| 1015 | + else { |
|
| 1016 | + foreach ($data as $column => $val) { |
|
| 1017 | 1017 | $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
| 1018 | 1018 | } |
| 1019 | 1019 | } |
| 1020 | 1020 | $query .= implode(', ', $values); |
| 1021 | - if (! empty($this->where)){ |
|
| 1021 | + if (!empty($this->where)) { |
|
| 1022 | 1022 | $query .= ' WHERE ' . $this->where; |
| 1023 | 1023 | } |
| 1024 | 1024 | |
| 1025 | - if (! empty($this->orderBy)){ |
|
| 1025 | + if (!empty($this->orderBy)) { |
|
| 1026 | 1026 | $query .= ' ORDER BY ' . $this->orderBy; |
| 1027 | 1027 | } |
| 1028 | 1028 | |
| 1029 | - if (! empty($this->limit)){ |
|
| 1029 | + if (!empty($this->limit)) { |
|
| 1030 | 1030 | $query .= ' LIMIT ' . $this->limit; |
| 1031 | 1031 | } |
| 1032 | 1032 | return $this->query($query); |
@@ -1036,22 +1036,22 @@ discard block |
||
| 1036 | 1036 | * Delete the record in database |
| 1037 | 1037 | * @return mixed the delete status |
| 1038 | 1038 | */ |
| 1039 | - public function delete(){ |
|
| 1039 | + public function delete() { |
|
| 1040 | 1040 | $query = 'DELETE FROM ' . $this->from; |
| 1041 | 1041 | |
| 1042 | - if (! empty($this->where)){ |
|
| 1042 | + if (!empty($this->where)) { |
|
| 1043 | 1043 | $query .= ' WHERE ' . $this->where; |
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | - if (! empty($this->orderBy)){ |
|
| 1046 | + if (!empty($this->orderBy)) { |
|
| 1047 | 1047 | $query .= ' ORDER BY ' . $this->orderBy; |
| 1048 | 1048 | } |
| 1049 | 1049 | |
| 1050 | - if (! empty($this->limit)){ |
|
| 1050 | + if (!empty($this->limit)) { |
|
| 1051 | 1051 | $query .= ' LIMIT ' . $this->limit; |
| 1052 | 1052 | } |
| 1053 | 1053 | |
| 1054 | - if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){ |
|
| 1054 | + if ($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite') { |
|
| 1055 | 1055 | $query = 'TRUNCATE TABLE ' . $this->from; |
| 1056 | 1056 | } |
| 1057 | 1057 | return $this->query($query); |
@@ -1064,13 +1064,13 @@ discard block |
||
| 1064 | 1064 | * @param boolean $array return the result as array |
| 1065 | 1065 | * @return mixed the query result |
| 1066 | 1066 | */ |
| 1067 | - public function query($query, $all = true, $array = false){ |
|
| 1067 | + public function query($query, $all = true, $array = false) { |
|
| 1068 | 1068 | $this->reset(); |
| 1069 | - if(is_array($all)){ |
|
| 1069 | + if (is_array($all)) { |
|
| 1070 | 1070 | $x = explode('?', $query); |
| 1071 | 1071 | $q = ''; |
| 1072 | - foreach($x as $k => $v){ |
|
| 1073 | - if(! empty($v)){ |
|
| 1072 | + foreach ($x as $k => $v) { |
|
| 1073 | + if (!empty($v)) { |
|
| 1074 | 1074 | $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : ''); |
| 1075 | 1075 | } |
| 1076 | 1076 | } |
@@ -1079,7 +1079,7 @@ discard block |
||
| 1079 | 1079 | |
| 1080 | 1080 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
| 1081 | 1081 | $sqlSELECTQuery = stristr($this->query, 'SELECT'); |
| 1082 | - $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO')); |
|
| 1082 | + $this->logger->info('Execute SQL query [' . $this->query . '], return type: ' . ($array ? 'ARRAY' : 'OBJECT') . ', return as list: ' . ($all ? 'YES' : 'NO')); |
|
| 1083 | 1083 | //cache expire time |
| 1084 | 1084 | $cacheExpire = $this->temporaryCacheTtl; |
| 1085 | 1085 | |
@@ -1101,34 +1101,34 @@ discard block |
||
| 1101 | 1101 | //if can use cache feature for this query |
| 1102 | 1102 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 1103 | 1103 | |
| 1104 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1104 | + if ($dbCacheStatus && $sqlSELECTQuery) { |
|
| 1105 | 1105 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
| 1106 | 1106 | $cacheKey = md5($query . $all . $array); |
| 1107 | - if(is_object($this->cacheInstance)){ |
|
| 1107 | + if (is_object($this->cacheInstance)) { |
|
| 1108 | 1108 | $cacheInstance = $this->cacheInstance; |
| 1109 | 1109 | } |
| 1110 | - else{ |
|
| 1110 | + else { |
|
| 1111 | 1111 | $obj = & get_instance(); |
| 1112 | 1112 | $cacheInstance = $obj->cache; |
| 1113 | 1113 | } |
| 1114 | 1114 | $cacheContent = $cacheInstance->get($cacheKey); |
| 1115 | 1115 | } |
| 1116 | - else{ |
|
| 1116 | + else { |
|
| 1117 | 1117 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1118 | 1118 | } |
| 1119 | 1119 | |
| 1120 | - if(! $this->pdo){ |
|
| 1120 | + if (!$this->pdo) { |
|
| 1121 | 1121 | $this->connect(); |
| 1122 | 1122 | } |
| 1123 | 1123 | |
| 1124 | - if (! $cacheContent && $sqlSELECTQuery){ |
|
| 1124 | + if (!$cacheContent && $sqlSELECTQuery) { |
|
| 1125 | 1125 | //for database query execution time |
| 1126 | 1126 | $benchmarkMarkerKey = md5($query . $all . $array); |
| 1127 | 1127 | $bench = null; |
| 1128 | - if(is_object($this->benchmarkInstance)){ |
|
| 1128 | + if (is_object($this->benchmarkInstance)) { |
|
| 1129 | 1129 | $bench = $this->benchmarkInstance; |
| 1130 | 1130 | } |
| 1131 | - else{ |
|
| 1131 | + else { |
|
| 1132 | 1132 | $obj = & get_instance(); |
| 1133 | 1133 | $bench = $obj->benchmark; |
| 1134 | 1134 | } |
@@ -1139,64 +1139,64 @@ discard block |
||
| 1139 | 1139 | //get response time for this query |
| 1140 | 1140 | $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')'); |
| 1141 | 1141 | //TODO use the configuration value for the high response time currently is 1 second |
| 1142 | - if($responseTime >= 1 ){ |
|
| 1143 | - $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.'); |
|
| 1142 | + if ($responseTime >= 1) { |
|
| 1143 | + $this->logger->warning('High response time while processing database query [' . $query . ']. The response time is [' . $responseTime . '] sec.'); |
|
| 1144 | 1144 | } |
| 1145 | - if ($sqlQuery){ |
|
| 1145 | + if ($sqlQuery) { |
|
| 1146 | 1146 | //if need return all result like list of record |
| 1147 | - if ($all){ |
|
| 1147 | + if ($all) { |
|
| 1148 | 1148 | $this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
| 1149 | 1149 | } |
| 1150 | - else{ |
|
| 1150 | + else { |
|
| 1151 | 1151 | $this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
| 1152 | 1152 | } |
| 1153 | 1153 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1154 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1154 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 1155 | 1155 | $this->numRows = count($this->result); |
| 1156 | 1156 | } |
| 1157 | - else{ |
|
| 1157 | + else { |
|
| 1158 | 1158 | $this->numRows = $sqlQuery->rowCount(); |
| 1159 | 1159 | } |
| 1160 | 1160 | |
| 1161 | - if ($dbCacheStatus && $sqlSELECTQuery){ |
|
| 1162 | - $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
|
| 1161 | + if ($dbCacheStatus && $sqlSELECTQuery) { |
|
| 1162 | + $this->logger->info('Save the result for query [' . $this->query . '] into cache for future use'); |
|
| 1163 | 1163 | $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
| 1164 | 1164 | } |
| 1165 | 1165 | } |
| 1166 | - else{ |
|
| 1166 | + else { |
|
| 1167 | 1167 | $error = $this->pdo->errorInfo(); |
| 1168 | 1168 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 1169 | 1169 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1170 | 1170 | $this->error(); |
| 1171 | 1171 | } |
| 1172 | 1172 | } |
| 1173 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1173 | + else if ((!$cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)) { |
|
| 1174 | 1174 | $queryStr = $this->pdo->query($this->query); |
| 1175 | - if($queryStr){ |
|
| 1175 | + if ($queryStr) { |
|
| 1176 | 1176 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1177 | - if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
|
| 1177 | + if (in_array($this->config['driver'], array('sqlite', 'pgsql'))) { |
|
| 1178 | 1178 | $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1179 | 1179 | $this->numRows = 1; |
| 1180 | 1180 | } |
| 1181 | - else{ |
|
| 1181 | + else { |
|
| 1182 | 1182 | $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1183 | 1183 | $this->numRows = $queryStr->rowCount(); |
| 1184 | 1184 | } |
| 1185 | 1185 | } |
| 1186 | - if (! $this->result){ |
|
| 1186 | + if (!$this->result) { |
|
| 1187 | 1187 | $error = $this->pdo->errorInfo(); |
| 1188 | 1188 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 1189 | 1189 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1190 | 1190 | $this->error(); |
| 1191 | 1191 | } |
| 1192 | 1192 | } |
| 1193 | - else{ |
|
| 1194 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 1193 | + else { |
|
| 1194 | + $this->logger->info('The result for query [' . $this->query . '] already cached use it'); |
|
| 1195 | 1195 | $this->result = $cacheContent; |
| 1196 | 1196 | $this->numRows = count($this->result); |
| 1197 | 1197 | } |
| 1198 | 1198 | $this->queryCount++; |
| 1199 | - if(! $this->result){ |
|
| 1199 | + if (!$this->result) { |
|
| 1200 | 1200 | $this->logger->info('No result where found for the query [' . $query . ']'); |
| 1201 | 1201 | } |
| 1202 | 1202 | return $this->result; |
@@ -1207,8 +1207,8 @@ discard block |
||
| 1207 | 1207 | * @param integer $ttl the cache time to live in second |
| 1208 | 1208 | * @return object the current Database instance |
| 1209 | 1209 | */ |
| 1210 | - public function setCache($ttl = 0){ |
|
| 1211 | - if($ttl > 0){ |
|
| 1210 | + public function setCache($ttl = 0) { |
|
| 1211 | + if ($ttl > 0) { |
|
| 1212 | 1212 | $this->cacheTtl = $ttl; |
| 1213 | 1213 | $this->temporaryCacheTtl = $ttl; |
| 1214 | 1214 | } |
@@ -1220,8 +1220,8 @@ discard block |
||
| 1220 | 1220 | * @param integer $ttl the cache time to live in second |
| 1221 | 1221 | * @return object the current Database instance |
| 1222 | 1222 | */ |
| 1223 | - public function cached($ttl = 0){ |
|
| 1224 | - if($ttl > 0){ |
|
| 1223 | + public function cached($ttl = 0) { |
|
| 1224 | + if ($ttl > 0) { |
|
| 1225 | 1225 | $this->temporaryCacheTtl = $ttl; |
| 1226 | 1226 | } |
| 1227 | 1227 | return $this; |
@@ -1232,11 +1232,11 @@ discard block |
||
| 1232 | 1232 | * @param mixed $data the data to be escaped |
| 1233 | 1233 | * @return mixed the data after escaped |
| 1234 | 1234 | */ |
| 1235 | - public function escape($data){ |
|
| 1236 | - if(is_null($data)){ |
|
| 1235 | + public function escape($data) { |
|
| 1236 | + if (is_null($data)) { |
|
| 1237 | 1237 | return null; |
| 1238 | 1238 | } |
| 1239 | - if(! $this->pdo){ |
|
| 1239 | + if (!$this->pdo) { |
|
| 1240 | 1240 | $this->connect(); |
| 1241 | 1241 | } |
| 1242 | 1242 | return $this->pdo->quote(trim($data)); |
@@ -1246,7 +1246,7 @@ discard block |
||
| 1246 | 1246 | * Return the number query executed count for the current request |
| 1247 | 1247 | * @return int |
| 1248 | 1248 | */ |
| 1249 | - public function queryCount(){ |
|
| 1249 | + public function queryCount() { |
|
| 1250 | 1250 | return $this->queryCount; |
| 1251 | 1251 | } |
| 1252 | 1252 | |
@@ -1254,7 +1254,7 @@ discard block |
||
| 1254 | 1254 | * Return the current query SQL string |
| 1255 | 1255 | * @return string |
| 1256 | 1256 | */ |
| 1257 | - public function getQuery(){ |
|
| 1257 | + public function getQuery() { |
|
| 1258 | 1258 | return $this->query; |
| 1259 | 1259 | } |
| 1260 | 1260 | |
@@ -1262,7 +1262,7 @@ discard block |
||
| 1262 | 1262 | * Return the application database name |
| 1263 | 1263 | * @return string |
| 1264 | 1264 | */ |
| 1265 | - public function getDatabaseName(){ |
|
| 1265 | + public function getDatabaseName() { |
|
| 1266 | 1266 | return $this->databaseName; |
| 1267 | 1267 | } |
| 1268 | 1268 | |
@@ -1270,7 +1270,7 @@ discard block |
||
| 1270 | 1270 | * Return the database configuration |
| 1271 | 1271 | * @return array |
| 1272 | 1272 | */ |
| 1273 | - public function getDatabaseConfiguration(){ |
|
| 1273 | + public function getDatabaseConfiguration() { |
|
| 1274 | 1274 | return $this->config; |
| 1275 | 1275 | } |
| 1276 | 1276 | |
@@ -1278,7 +1278,7 @@ discard block |
||
| 1278 | 1278 | * set the database configuration |
| 1279 | 1279 | * @param array $config the configuration |
| 1280 | 1280 | */ |
| 1281 | - public function setDatabaseConfiguration(array $config){ |
|
| 1281 | + public function setDatabaseConfiguration(array $config) { |
|
| 1282 | 1282 | $this->config = array_merge($this->config, $config); |
| 1283 | 1283 | $this->prefix = $this->config['prefix']; |
| 1284 | 1284 | $this->databaseName = $this->config['database']; |
@@ -1290,7 +1290,7 @@ discard block |
||
| 1290 | 1290 | * Return the PDO instance |
| 1291 | 1291 | * @return PDO |
| 1292 | 1292 | */ |
| 1293 | - public function getPdo(){ |
|
| 1293 | + public function getPdo() { |
|
| 1294 | 1294 | return $this->pdo; |
| 1295 | 1295 | } |
| 1296 | 1296 | |
@@ -1298,7 +1298,7 @@ discard block |
||
| 1298 | 1298 | * Set the PDO instance |
| 1299 | 1299 | * @param PDO $pdo the pdo object |
| 1300 | 1300 | */ |
| 1301 | - public function setPdo(PDO $pdo){ |
|
| 1301 | + public function setPdo(PDO $pdo) { |
|
| 1302 | 1302 | $this->pdo = $pdo; |
| 1303 | 1303 | return $this; |
| 1304 | 1304 | } |
@@ -1308,7 +1308,7 @@ discard block |
||
| 1308 | 1308 | * Return the Log instance |
| 1309 | 1309 | * @return Log |
| 1310 | 1310 | */ |
| 1311 | - public function getLogger(){ |
|
| 1311 | + public function getLogger() { |
|
| 1312 | 1312 | return $this->logger; |
| 1313 | 1313 | } |
| 1314 | 1314 | |
@@ -1316,7 +1316,7 @@ discard block |
||
| 1316 | 1316 | * Set the log instance |
| 1317 | 1317 | * @param Log $logger the log object |
| 1318 | 1318 | */ |
| 1319 | - public function setLogger($logger){ |
|
| 1319 | + public function setLogger($logger) { |
|
| 1320 | 1320 | $this->logger = $logger; |
| 1321 | 1321 | return $this; |
| 1322 | 1322 | } |
@@ -1325,7 +1325,7 @@ discard block |
||
| 1325 | 1325 | * Return the cache instance |
| 1326 | 1326 | * @return CacheInterface |
| 1327 | 1327 | */ |
| 1328 | - public function getCacheInstance(){ |
|
| 1328 | + public function getCacheInstance() { |
|
| 1329 | 1329 | return $this->cacheInstance; |
| 1330 | 1330 | } |
| 1331 | 1331 | |
@@ -1333,7 +1333,7 @@ discard block |
||
| 1333 | 1333 | * Set the cache instance |
| 1334 | 1334 | * @param CacheInterface $cache the cache object |
| 1335 | 1335 | */ |
| 1336 | - public function setCacheInstance($cache){ |
|
| 1336 | + public function setCacheInstance($cache) { |
|
| 1337 | 1337 | $this->cacheInstance = $cache; |
| 1338 | 1338 | return $this; |
| 1339 | 1339 | } |
@@ -1342,7 +1342,7 @@ discard block |
||
| 1342 | 1342 | * Return the benchmark instance |
| 1343 | 1343 | * @return Benchmark |
| 1344 | 1344 | */ |
| 1345 | - public function getBenchmark(){ |
|
| 1345 | + public function getBenchmark() { |
|
| 1346 | 1346 | return $this->benchmarkInstance; |
| 1347 | 1347 | } |
| 1348 | 1348 | |
@@ -1350,7 +1350,7 @@ discard block |
||
| 1350 | 1350 | * Set the benchmark instance |
| 1351 | 1351 | * @param Benchmark $cache the cache object |
| 1352 | 1352 | */ |
| 1353 | - public function setBenchmark($benchmark){ |
|
| 1353 | + public function setBenchmark($benchmark) { |
|
| 1354 | 1354 | $this->benchmarkInstance = $benchmark; |
| 1355 | 1355 | return $this; |
| 1356 | 1356 | } |
@@ -1359,7 +1359,7 @@ discard block |
||
| 1359 | 1359 | * Return the data to be used for insert, update, etc. |
| 1360 | 1360 | * @return array |
| 1361 | 1361 | */ |
| 1362 | - public function getData(){ |
|
| 1362 | + public function getData() { |
|
| 1363 | 1363 | return $this->data; |
| 1364 | 1364 | } |
| 1365 | 1365 | |
@@ -1370,7 +1370,7 @@ discard block |
||
| 1370 | 1370 | * @param boolean $escape whether to escape or not the $value |
| 1371 | 1371 | * @return object the current Database instance |
| 1372 | 1372 | */ |
| 1373 | - public function setData($key, $value, $escape = true){ |
|
| 1373 | + public function setData($key, $value, $escape = true) { |
|
| 1374 | 1374 | $this->data[$key] = $escape ? $this->escape($value) : $value; |
| 1375 | 1375 | return $this; |
| 1376 | 1376 | } |
@@ -1379,12 +1379,12 @@ discard block |
||
| 1379 | 1379 | * Set the Log instance using argument or create new instance |
| 1380 | 1380 | * @param object $logger the Log instance if not null |
| 1381 | 1381 | */ |
| 1382 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 1383 | - if($logger !== null){ |
|
| 1382 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
| 1383 | + if ($logger !== null) { |
|
| 1384 | 1384 | $this->logger = $logger; |
| 1385 | 1385 | } |
| 1386 | - else{ |
|
| 1387 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 1386 | + else { |
|
| 1387 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 1388 | 1388 | $this->logger->setLogger('Library::Database'); |
| 1389 | 1389 | } |
| 1390 | 1390 | } |
@@ -1393,14 +1393,14 @@ discard block |
||
| 1393 | 1393 | * Setting the database configuration using the configuration file |
| 1394 | 1394 | * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
| 1395 | 1395 | */ |
| 1396 | - protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()){ |
|
| 1396 | + protected function setDatabaseConfigurationFromConfigFile(array $overwriteConfig = array()) { |
|
| 1397 | 1397 | $db = array(); |
| 1398 | - if(file_exists(CONFIG_PATH . 'database.php')){ |
|
| 1398 | + if (file_exists(CONFIG_PATH . 'database.php')) { |
|
| 1399 | 1399 | //here don't use require_once because somewhere user can create database instance directly |
| 1400 | 1400 | require CONFIG_PATH . 'database.php'; |
| 1401 | 1401 | } |
| 1402 | 1402 | |
| 1403 | - if(! empty($overwriteConfig)){ |
|
| 1403 | + if (!empty($overwriteConfig)) { |
|
| 1404 | 1404 | $db = array_merge($db, $overwriteConfig); |
| 1405 | 1405 | } |
| 1406 | 1406 | $config = array(); |
@@ -1413,12 +1413,12 @@ discard block |
||
| 1413 | 1413 | $config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci'; |
| 1414 | 1414 | $config['prefix'] = isset($db['prefix']) ? $db['prefix'] : ''; |
| 1415 | 1415 | $port = ''; |
| 1416 | - if(strstr($config['hostname'], ':')){ |
|
| 1416 | + if (strstr($config['hostname'], ':')) { |
|
| 1417 | 1417 | $p = explode(':', $config['hostname']); |
| 1418 | 1418 | $port = isset($p[1]) ? $p[1] : ''; |
| 1419 | 1419 | $config['hostname'] = isset($p[0]) ? $p[0] : ''; |
| 1420 | 1420 | } |
| 1421 | - $config['port'] = $port; |
|
| 1421 | + $config['port'] = $port; |
|
| 1422 | 1422 | $this->setDatabaseConfiguration($config); |
| 1423 | 1423 | } |
| 1424 | 1424 | |
@@ -1426,9 +1426,9 @@ discard block |
||
| 1426 | 1426 | * This method is used to get the PDO DSN string using th configured driver |
| 1427 | 1427 | * @return string the DSN string |
| 1428 | 1428 | */ |
| 1429 | - protected function getDsnFromDriver(){ |
|
| 1429 | + protected function getDsnFromDriver() { |
|
| 1430 | 1430 | $config = $this->getDatabaseConfiguration(); |
| 1431 | - if(! empty($config)){ |
|
| 1431 | + if (!empty($config)) { |
|
| 1432 | 1432 | $driverDsnMap = array( |
| 1433 | 1433 | 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
| 1434 | 1434 | . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
@@ -1451,7 +1451,7 @@ discard block |
||
| 1451 | 1451 | /** |
| 1452 | 1452 | * Reset the database class attributs to the initail values before each query. |
| 1453 | 1453 | */ |
| 1454 | - private function reset(){ |
|
| 1454 | + private function reset() { |
|
| 1455 | 1455 | $this->select = '*'; |
| 1456 | 1456 | $this->from = null; |
| 1457 | 1457 | $this->where = null; |
@@ -1471,7 +1471,7 @@ discard block |
||
| 1471 | 1471 | /** |
| 1472 | 1472 | * The class destructor |
| 1473 | 1473 | */ |
| 1474 | - public function __destruct(){ |
|
| 1474 | + public function __destruct() { |
|
| 1475 | 1475 | $this->pdo = null; |
| 1476 | 1476 | } |
| 1477 | 1477 | |
@@ -204,14 +204,12 @@ discard block |
||
| 204 | 204 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 205 | 205 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 206 | 206 | return true; |
| 207 | - } |
|
| 208 | - catch (PDOException $e){ |
|
| 207 | + } catch (PDOException $e){ |
|
| 209 | 208 | $this->logger->fatal($e->getMessage()); |
| 210 | 209 | show_error('Cannot connect to Database.'); |
| 211 | 210 | return false; |
| 212 | 211 | } |
| 213 | - } |
|
| 214 | - else{ |
|
| 212 | + } else{ |
|
| 215 | 213 | show_error('Database configuration is not set.'); |
| 216 | 214 | return false; |
| 217 | 215 | } |
@@ -229,8 +227,7 @@ discard block |
||
| 229 | 227 | $froms .= $this->prefix . $key . ', '; |
| 230 | 228 | } |
| 231 | 229 | $this->from = rtrim($froms, ', '); |
| 232 | - } |
|
| 233 | - else{ |
|
| 230 | + } else{ |
|
| 234 | 231 | $this->from = $this->prefix . $table; |
| 235 | 232 | } |
| 236 | 233 | return $this; |
@@ -336,8 +333,7 @@ discard block |
||
| 336 | 333 | } |
| 337 | 334 | if (empty($this->join)){ |
| 338 | 335 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 339 | - } |
|
| 340 | - else{ |
|
| 336 | + } else{ |
|
| 341 | 337 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 342 | 338 | } |
| 343 | 339 | return $this; |
@@ -408,12 +404,10 @@ discard block |
||
| 408 | 404 | foreach($field as $f){ |
| 409 | 405 | $this->whereIsNull($f, $andOr); |
| 410 | 406 | } |
| 411 | - } |
|
| 412 | - else{ |
|
| 407 | + } else{ |
|
| 413 | 408 | if (! $this->where){ |
| 414 | 409 | $this->where = $field.' IS NULL '; |
| 415 | - } |
|
| 416 | - else{ |
|
| 410 | + } else{ |
|
| 417 | 411 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL '; |
| 418 | 412 | } |
| 419 | 413 | } |
@@ -431,12 +425,10 @@ discard block |
||
| 431 | 425 | foreach($field as $f){ |
| 432 | 426 | $this->whereIsNotNull($f, $andOr); |
| 433 | 427 | } |
| 434 | - } |
|
| 435 | - else{ |
|
| 428 | + } else{ |
|
| 436 | 429 | if (! $this->where){ |
| 437 | 430 | $this->where = $field.' IS NOT NULL '; |
| 438 | - } |
|
| 439 | - else{ |
|
| 431 | + } else{ |
|
| 440 | 432 | $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL '; |
| 441 | 433 | } |
| 442 | 434 | } |
@@ -494,8 +486,7 @@ discard block |
||
| 494 | 486 | $op = ''; |
| 495 | 487 | } |
| 496 | 488 | $w = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op); |
| 497 | - } |
|
| 498 | - else{ |
|
| 489 | + } else{ |
|
| 499 | 490 | if(is_null($val)){ |
| 500 | 491 | $val = ''; |
| 501 | 492 | } |
@@ -512,12 +503,10 @@ discard block |
||
| 512 | 503 | protected function setWhereStr($whereStr, $andOr = 'AND'){ |
| 513 | 504 | if (empty($this->where)){ |
| 514 | 505 | $this->where = $whereStr; |
| 515 | - } |
|
| 516 | - else{ |
|
| 506 | + } else{ |
|
| 517 | 507 | if(substr($this->where, -1) == '('){ |
| 518 | 508 | $this->where = $this->where . ' ' . $whereStr; |
| 519 | - } |
|
| 520 | - else{ |
|
| 509 | + } else{ |
|
| 521 | 510 | $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
| 522 | 511 | } |
| 523 | 512 | } |
@@ -537,8 +526,7 @@ discard block |
||
| 537 | 526 | $whereStr = ''; |
| 538 | 527 | if (is_array($where)){ |
| 539 | 528 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 540 | - } |
|
| 541 | - else{ |
|
| 529 | + } else{ |
|
| 542 | 530 | if(is_array($op)){ |
| 543 | 531 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
| 544 | 532 | } else { |
@@ -586,12 +574,10 @@ discard block |
||
| 586 | 574 | public function groupStart($type = '', $andOr = ' AND'){ |
| 587 | 575 | if (empty($this->where)){ |
| 588 | 576 | $this->where = $type . ' ('; |
| 589 | - } |
|
| 590 | - else{ |
|
| 577 | + } else{ |
|
| 591 | 578 | if(substr($this->where, -1) == '('){ |
| 592 | 579 | $this->where .= $type . ' ('; |
| 593 | - } |
|
| 594 | - else{ |
|
| 580 | + } else{ |
|
| 595 | 581 | $this->where .= $andOr . ' ' . $type . ' ('; |
| 596 | 582 | } |
| 597 | 583 | } |
@@ -749,12 +735,10 @@ discard block |
||
| 749 | 735 | $like = $escape ? $this->escape($data) : $data; |
| 750 | 736 | if (empty($this->where)){ |
| 751 | 737 | $this->where = $field . ' ' . $type . 'LIKE ' . $like; |
| 752 | - } |
|
| 753 | - else{ |
|
| 738 | + } else{ |
|
| 754 | 739 | if(substr($this->where, -1) == '('){ |
| 755 | 740 | $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 756 | - } |
|
| 757 | - else{ |
|
| 741 | + } else{ |
|
| 758 | 742 | $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like; |
| 759 | 743 | } |
| 760 | 744 | } |
@@ -801,8 +785,7 @@ discard block |
||
| 801 | 785 | } |
| 802 | 786 | if (! is_null($limitEnd)){ |
| 803 | 787 | $this->limit = $limit . ', ' . $limitEnd; |
| 804 | - } |
|
| 805 | - else{ |
|
| 788 | + } else{ |
|
| 806 | 789 | $this->limit = $limit; |
| 807 | 790 | } |
| 808 | 791 | return $this; |
@@ -817,12 +800,10 @@ discard block |
||
| 817 | 800 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 818 | 801 | if (! empty($orderDir)){ |
| 819 | 802 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
| 820 | - } |
|
| 821 | - else{ |
|
| 803 | + } else{ |
|
| 822 | 804 | if(stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
| 823 | 805 | $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 824 | - } |
|
| 825 | - else{ |
|
| 806 | + } else{ |
|
| 826 | 807 | $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC'); |
| 827 | 808 | } |
| 828 | 809 | } |
@@ -837,8 +818,7 @@ discard block |
||
| 837 | 818 | public function groupBy($field){ |
| 838 | 819 | if(is_array($field)){ |
| 839 | 820 | $this->groupBy = implode(', ', $field); |
| 840 | - } |
|
| 841 | - else{ |
|
| 821 | + } else{ |
|
| 842 | 822 | $this->groupBy = $field; |
| 843 | 823 | } |
| 844 | 824 | return $this; |
@@ -865,14 +845,12 @@ discard block |
||
| 865 | 845 | } |
| 866 | 846 | } |
| 867 | 847 | $this->having = $w; |
| 868 | - } |
|
| 869 | - else if (! in_array($op, $this->operatorList)){ |
|
| 848 | + } else if (! in_array($op, $this->operatorList)){ |
|
| 870 | 849 | if(is_null($op)){ |
| 871 | 850 | $op = ''; |
| 872 | 851 | } |
| 873 | 852 | $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op); |
| 874 | - } |
|
| 875 | - else{ |
|
| 853 | + } else{ |
|
| 876 | 854 | if(is_null($val)){ |
| 877 | 855 | $val = ''; |
| 878 | 856 | } |
@@ -917,8 +895,7 @@ discard block |
||
| 917 | 895 | $query = $this->getAll(true); |
| 918 | 896 | if($returnSQLQueryOrResultType === true){ |
| 919 | 897 | return $query; |
| 920 | - } |
|
| 921 | - else{ |
|
| 898 | + } else{ |
|
| 922 | 899 | return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 923 | 900 | } |
| 924 | 901 | } |
@@ -957,8 +934,7 @@ discard block |
||
| 957 | 934 | |
| 958 | 935 | if($returnSQLQueryOrResultType === true){ |
| 959 | 936 | return $query; |
| 960 | - } |
|
| 961 | - else{ |
|
| 937 | + } else{ |
|
| 962 | 938 | return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) ); |
| 963 | 939 | } |
| 964 | 940 | } |
@@ -976,8 +952,7 @@ discard block |
||
| 976 | 952 | $columns = array_keys($this->getData()); |
| 977 | 953 | $column = implode(',', $columns); |
| 978 | 954 | $val = implode(', ', $this->getData()); |
| 979 | - } |
|
| 980 | - else{ |
|
| 955 | + } else{ |
|
| 981 | 956 | $columns = array_keys($data); |
| 982 | 957 | $column = implode(',', $columns); |
| 983 | 958 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -992,8 +967,7 @@ discard block |
||
| 992 | 967 | } |
| 993 | 968 | $this->insertId = $this->pdo->lastInsertId(); |
| 994 | 969 | return $this->insertId(); |
| 995 | - } |
|
| 996 | - else{ |
|
| 970 | + } else{ |
|
| 997 | 971 | return false; |
| 998 | 972 | } |
| 999 | 973 | } |
@@ -1011,8 +985,7 @@ discard block |
||
| 1011 | 985 | foreach ($this->getData() as $column => $val){ |
| 1012 | 986 | $values[] = $column . ' = ' . $val; |
| 1013 | 987 | } |
| 1014 | - } |
|
| 1015 | - else{ |
|
| 988 | + } else{ |
|
| 1016 | 989 | foreach ($data as $column => $val){ |
| 1017 | 990 | $values[] = $column . '=' . ($escape ? $this->escape($val) : $val); |
| 1018 | 991 | } |
@@ -1106,14 +1079,12 @@ discard block |
||
| 1106 | 1079 | $cacheKey = md5($query . $all . $array); |
| 1107 | 1080 | if(is_object($this->cacheInstance)){ |
| 1108 | 1081 | $cacheInstance = $this->cacheInstance; |
| 1109 | - } |
|
| 1110 | - else{ |
|
| 1082 | + } else{ |
|
| 1111 | 1083 | $obj = & get_instance(); |
| 1112 | 1084 | $cacheInstance = $obj->cache; |
| 1113 | 1085 | } |
| 1114 | 1086 | $cacheContent = $cacheInstance->get($cacheKey); |
| 1115 | - } |
|
| 1116 | - else{ |
|
| 1087 | + } else{ |
|
| 1117 | 1088 | $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database'); |
| 1118 | 1089 | } |
| 1119 | 1090 | |
@@ -1127,8 +1098,7 @@ discard block |
||
| 1127 | 1098 | $bench = null; |
| 1128 | 1099 | if(is_object($this->benchmarkInstance)){ |
| 1129 | 1100 | $bench = $this->benchmarkInstance; |
| 1130 | - } |
|
| 1131 | - else{ |
|
| 1101 | + } else{ |
|
| 1132 | 1102 | $obj = & get_instance(); |
| 1133 | 1103 | $bench = $obj->benchmark; |
| 1134 | 1104 | } |
@@ -1146,15 +1116,13 @@ discard block |
||
| 1146 | 1116 | //if need return all result like list of record |
| 1147 | 1117 | if ($all){ |
| 1148 | 1118 | $this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC); |
| 1149 | - } |
|
| 1150 | - else{ |
|
| 1119 | + } else{ |
|
| 1151 | 1120 | $this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC); |
| 1152 | 1121 | } |
| 1153 | 1122 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1154 | 1123 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 1155 | 1124 | $this->numRows = count($this->result); |
| 1156 | - } |
|
| 1157 | - else{ |
|
| 1125 | + } else{ |
|
| 1158 | 1126 | $this->numRows = $sqlQuery->rowCount(); |
| 1159 | 1127 | } |
| 1160 | 1128 | |
@@ -1162,23 +1130,20 @@ discard block |
||
| 1162 | 1130 | $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use'); |
| 1163 | 1131 | $cacheInstance->set($cacheKey, $this->result, $cacheExpire); |
| 1164 | 1132 | } |
| 1165 | - } |
|
| 1166 | - else{ |
|
| 1133 | + } else{ |
|
| 1167 | 1134 | $error = $this->pdo->errorInfo(); |
| 1168 | 1135 | $this->error = isset($error[2]) ? $error[2] : ''; |
| 1169 | 1136 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1170 | 1137 | $this->error(); |
| 1171 | 1138 | } |
| 1172 | - } |
|
| 1173 | - else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1139 | + } else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){ |
|
| 1174 | 1140 | $queryStr = $this->pdo->query($this->query); |
| 1175 | 1141 | if($queryStr){ |
| 1176 | 1142 | //Sqlite and pgsql always return 0 when using rowCount() |
| 1177 | 1143 | if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){ |
| 1178 | 1144 | $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1179 | 1145 | $this->numRows = 1; |
| 1180 | - } |
|
| 1181 | - else{ |
|
| 1146 | + } else{ |
|
| 1182 | 1147 | $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE |
| 1183 | 1148 | $this->numRows = $queryStr->rowCount(); |
| 1184 | 1149 | } |
@@ -1189,8 +1154,7 @@ discard block |
||
| 1189 | 1154 | $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error)); |
| 1190 | 1155 | $this->error(); |
| 1191 | 1156 | } |
| 1192 | - } |
|
| 1193 | - else{ |
|
| 1157 | + } else{ |
|
| 1194 | 1158 | $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
| 1195 | 1159 | $this->result = $cacheContent; |
| 1196 | 1160 | $this->numRows = count($this->result); |
@@ -1382,8 +1346,7 @@ discard block |
||
| 1382 | 1346 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 1383 | 1347 | if($logger !== null){ |
| 1384 | 1348 | $this->logger = $logger; |
| 1385 | - } |
|
| 1386 | - else{ |
|
| 1349 | + } else{ |
|
| 1387 | 1350 | $this->logger =& class_loader('Log', 'classes'); |
| 1388 | 1351 | $this->logger->setLogger('Library::Database'); |
| 1389 | 1352 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | class Controller{ |
| 28 | 28 | |
@@ -114,12 +114,12 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 116 | 116 | if($logger !== null){ |
| 117 | - $this->logger = $logger; |
|
| 118 | - } |
|
| 119 | - else{ |
|
| 120 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 117 | + $this->logger = $logger; |
|
| 118 | + } |
|
| 119 | + else{ |
|
| 120 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 121 | 121 | $this->logger->setLogger('MainController'); |
| 122 | - } |
|
| 122 | + } |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Controller{ |
|
| 27 | + class Controller { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The name of the module if this controller belong to an module |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * Class constructor |
| 49 | 49 | * @param object $logger the Log instance to use if is null will create one |
| 50 | 50 | */ |
| 51 | - public function __construct(Log $logger = null){ |
|
| 51 | + public function __construct(Log $logger = null) { |
|
| 52 | 52 | //setting the Log instance |
| 53 | 53 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 54 | 54 | |
@@ -84,9 +84,9 @@ discard block |
||
| 84 | 84 | /** |
| 85 | 85 | * This method is used to set the module name |
| 86 | 86 | */ |
| 87 | - protected function setModuleNameFromRouter(){ |
|
| 87 | + protected function setModuleNameFromRouter() { |
|
| 88 | 88 | //determine the current module |
| 89 | - if(isset($this->router) && $this->router->getModule()){ |
|
| 89 | + if (isset($this->router) && $this->router->getModule()) { |
|
| 90 | 90 | $this->moduleName = $this->router->getModule(); |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -95,13 +95,13 @@ discard block |
||
| 95 | 95 | * Set the cache using the argument otherwise will use the configuration |
| 96 | 96 | * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
| 97 | 97 | */ |
| 98 | - protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 98 | + protected function setCacheFromParamOrConfig(CacheInterface $cache = null) { |
|
| 99 | 99 | $this->logger->debug('Setting the cache handler instance'); |
| 100 | 100 | //set cache handler instance |
| 101 | - if(get_config('cache_enable', false)){ |
|
| 102 | - if ($cache !== null){ |
|
| 101 | + if (get_config('cache_enable', false)) { |
|
| 102 | + if ($cache !== null) { |
|
| 103 | 103 | $this->cache = $cache; |
| 104 | - } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 104 | + } else if (isset($this->{strtolower(get_config('cache_handler'))})) { |
|
| 105 | 105 | $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
| 106 | 106 | unset($this->{strtolower(get_config('cache_handler'))}); |
| 107 | 107 | } |
@@ -112,12 +112,12 @@ discard block |
||
| 112 | 112 | * Set the Log instance using argument or create new instance |
| 113 | 113 | * @param object $logger the Log instance if not null |
| 114 | 114 | */ |
| 115 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 116 | - if($logger !== null){ |
|
| 115 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
| 116 | + if ($logger !== null) { |
|
| 117 | 117 | $this->logger = $logger; |
| 118 | 118 | } |
| 119 | - else{ |
|
| 120 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 119 | + else { |
|
| 120 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 121 | 121 | $this->logger->setLogger('MainController'); |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -126,20 +126,20 @@ discard block |
||
| 126 | 126 | * This method is used to load the required resources for framework to work |
| 127 | 127 | * @return void |
| 128 | 128 | */ |
| 129 | - private function loadRequiredResources(){ |
|
| 129 | + private function loadRequiredResources() { |
|
| 130 | 130 | $this->logger->debug('Adding the loaded classes to the super instance'); |
| 131 | - foreach (class_loaded() as $var => $class){ |
|
| 132 | - $this->$var =& class_loader($class); |
|
| 131 | + foreach (class_loaded() as $var => $class) { |
|
| 132 | + $this->$var = & class_loader($class); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | $this->logger->debug('Loading the required classes into super instance'); |
| 136 | - $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 137 | - $this->loader =& class_loader('Loader', 'classes'); |
|
| 138 | - $this->lang =& class_loader('Lang', 'classes'); |
|
| 139 | - $this->request =& class_loader('Request', 'classes'); |
|
| 136 | + $this->eventdispatcher = & class_loader('EventDispatcher', 'classes'); |
|
| 137 | + $this->loader = & class_loader('Loader', 'classes'); |
|
| 138 | + $this->lang = & class_loader('Lang', 'classes'); |
|
| 139 | + $this->request = & class_loader('Request', 'classes'); |
|
| 140 | 140 | //dispatch the request instance created event |
| 141 | 141 | $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
| 142 | - $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 142 | + $this->response = & class_loader('Response', 'classes', 'classes'); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | } |
@@ -115,8 +115,7 @@ |
||
| 115 | 115 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 116 | 116 | if($logger !== null){ |
| 117 | 117 | $this->logger = $logger; |
| 118 | - } |
|
| 119 | - else{ |
|
| 118 | + } else{ |
|
| 120 | 119 | $this->logger =& class_loader('Log', 'classes'); |
| 121 | 120 | $this->logger->setLogger('MainController'); |
| 122 | 121 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | |
| 27 | 27 | |
| 28 | - public function testDefaultValue(){ |
|
| 28 | + public function testDefaultValue() { |
|
| 29 | 29 | $e = new EventInfo('foo'); |
| 30 | 30 | $this->assertSame($e->name, 'foo'); |
| 31 | 31 | $this->assertSame($e->payload, array()); |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | $this->assertFalse($e->stop); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - public function testPayloadValueIsSet(){ |
|
| 36 | + public function testPayloadValueIsSet() { |
|
| 37 | 37 | $e = new EventInfo('foo', array('bar')); |
| 38 | 38 | $this->assertSame($e->name, 'foo'); |
| 39 | 39 | $this->assertSame($e->payload, array('bar')); |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | $this->assertFalse($e->stop); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public function testReturnBackValueIsSetToTrue(){ |
|
| 44 | + public function testReturnBackValueIsSetToTrue() { |
|
| 45 | 45 | $e = new EventInfo('foo', array('bar'), true); |
| 46 | 46 | $this->assertSame($e->name, 'foo'); |
| 47 | 47 | $this->assertSame($e->payload, array('bar')); |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | $this->assertFalse($e->stop); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - public function testStopValueIsSetToTue(){ |
|
| 52 | + public function testStopValueIsSetToTue() { |
|
| 53 | 53 | $e = new EventInfo('foo', array('bar'), true, true); |
| 54 | 54 | $this->assertSame($e->name, 'foo'); |
| 55 | 55 | $this->assertSame($e->payload, array('bar')); |