| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* No direct access. |
| 8 |
*/ |
| 9 |
defined('ABSPATH') or die("Access denied"); |
| 10 |
|
| 11 |
/** |
| 12 |
* Import libs. |
| 13 |
*/ |
| 14 |
require_once CJTOOLBOX_INCLUDE_PATH . '/db/mysql/sql-view.inc.php'; |
| 15 |
|
| 16 |
/** |
| 17 |
* |
| 18 |
*/ |
| 19 |
class CJTPinsBlockSQLView extends CJTSQLView |
| 20 |
{ |
| 21 |
|
| 22 |
/** |
| 23 |
* put your comment there... |
| 24 |
* |
| 25 |
* @var mixed |
| 26 |
*/ |
| 27 |
protected $blocksQuery; |
| 28 |
|
| 29 |
/** |
| 30 |
* put your comment there... |
| 31 |
* |
| 32 |
* @var mixed |
| 33 |
*/ |
| 34 |
protected $customPins; |
| 35 |
|
| 36 |
/** |
| 37 |
* put your comment there... |
| 38 |
* |
| 39 |
* @param mixed $dbDriver |
| 40 |
* @return CJTPinsBlockView |
| 41 |
*/ |
| 42 |
public function __construct( $driver ) |
| 43 |
{ |
| 44 |
// Initialize SQLView Parent. |
| 45 |
parent::__construct( $driver ); |
| 46 |
|
| 47 |
// Set default columns. |
| 48 |
$this->query->columns = array |
| 49 |
( |
| 50 |
'blocks.id', |
| 51 |
'blocks.owner', |
| 52 |
'blocks.name', |
| 53 |
'blocks.pinPoint', |
| 54 |
'blocks.location', |
| 55 |
'blocks.links', |
| 56 |
'blocks.expressions' |
| 57 |
); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* put your comment there... |
| 63 |
* |
| 64 |
*/ |
| 65 |
public function __toString() |
| 66 |
{ |
| 67 |
# In case of Error at $this->makeBlocksQuery dont processed |
| 68 |
if ( $this->blocksQuery === FALSE ) |
| 69 |
{ |
| 70 |
return ''; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
$filters = $this->query->filter; |
| 75 |
$customPins = $this->customPins; |
| 76 |
|
| 77 |
$customPins = implode( ' OR ', $customPins ); |
| 78 |
|
| 79 |
if ( ! empty( $customPins ) ) |
| 80 |
{ |
| 81 |
$customPins = " OR {$customPins}"; |
| 82 |
} |
| 83 |
|
| 84 |
// Exclude blocks ids. |
| 85 |
$excludes = implode( ',', $filters->excludes ); |
| 86 |
$excludes = ! empty( $excludes ) ? " AND `id` NOT IN ( {$excludes} )" : ''; |
| 87 |
|
| 88 |
# Build where clause |
| 89 |
$this->blocksQuery[ 'where' ] = "( ( ( `backupId` IS NULL ) AND ( `state` = '{$filters->state}' ){$excludes} ) AND ( ( blocks.`pinPoint` & {$filters->pinPoint} ){$customPins} ) )"; |
| 90 |
|
| 91 |
# Build final query |
| 92 |
$query = $this->buildQuery( $this->blocksQuery[ 'from' ], $this->blocksQuery[ 'where' ] ); |
| 93 |
|
| 94 |
return $query; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* put your comment there... |
| 99 |
* |
| 100 |
*/ |
| 101 |
public function exec() |
| 102 |
{ |
| 103 |
|
| 104 |
$blocks = array(); |
| 105 |
$query = ( string ) $this; |
| 106 |
|
| 107 |
# It will return empty query, in case of error |
| 108 |
if ( $query ) |
| 109 |
{ |
| 110 |
# Note: OBJECT_K get unique blocks as they will override each others |
| 111 |
$blocks = $this->driver->select( $query, OBJECT_K ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
return $blocks; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* put your comment there... |
| 120 |
* |
| 121 |
* @param mixed $pinPoint |
| 122 |
* @param mixed $customPins |
| 123 |
* @param mixed $state |
| 124 |
* @param mixed $excludes |
| 125 |
*/ |
| 126 |
public function & filters( $pinPoint, $customPins, $state = 'active', $excludes = array() ) |
| 127 |
{ |
| 128 |
|
| 129 |
$filters = array |
| 130 |
( |
| 131 |
'state' => $state, |
| 132 |
'pinPoint' => $pinPoint, |
| 133 |
'customPins' => $customPins, |
| 134 |
'excludes' => $excludes, |
| 135 |
); |
| 136 |
|
| 137 |
$this->query->filter = ( object ) $filters; |
| 138 |
|
| 139 |
# make query |
| 140 |
$this->makeBlocksQuery(); |
| 141 |
|
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* put your comment there... |
| 147 |
* |
| 148 |
*/ |
| 149 |
public function & getBlocksQuery() |
| 150 |
{ |
| 151 |
return $this->blocksQuery; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* put your comment there... |
| 156 |
* |
| 157 |
*/ |
| 158 |
public function & getCustomPins() |
| 159 |
{ |
| 160 |
return $this->customPins; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* put your comment there... |
| 165 |
* |
| 166 |
*/ |
| 167 |
private function & makeBlocksQuery() |
| 168 |
{ |
| 169 |
|
| 170 |
$blocksTable = $this->driver->getTableName( cssJSToolbox::$config->database->tables->blocks ); |
| 171 |
$pinsTable = $this->driver->getTableName( cssJSToolbox::$config->database->tables->blockPins ); |
| 172 |
$filters = $this->query->filter; |
| 173 |
|
| 174 |
// build custom pins filter. |
| 175 |
$customPins = array(); |
| 176 |
|
| 177 |
global $wp_query; |
| 178 |
|
| 179 |
foreach ( $filters->customPins as $pinFilter ) |
| 180 |
{ |
| 181 |
|
| 182 |
$pinFilter = (object) $pinFilter; |
| 183 |
$pins = implode( ',', $pinFilter->pins ); |
| 184 |
|
| 185 |
// Scan Pins, sometime if happened to be empty |
| 186 |
// The Wordpress request that carry $pins to be empty is not yet |
| 187 |
// known, we simply now need to discard that request |
| 188 |
// as user's server error log file filled with this error |
| 189 |
// and users complains about it all the time |
| 190 |
if ( ! $pins ) |
| 191 |
{ |
| 192 |
|
| 193 |
$this->blocksQuery = FALSE; |
| 194 |
|
| 195 |
return $this; |
| 196 |
} |
| 197 |
|
| 198 |
$customPins[ ] = "( ( blocks.`pinPoint` & {$pinFilter->flag} ) AND ( pins.pin = '{$pinFilter->pin}' ) AND ( pins.`value` IN ( {$pins} ) ) )"; |
| 199 |
} |
| 200 |
|
| 201 |
$this->customPins = $customPins; |
| 202 |
|
| 203 |
/** |
| 204 |
* @todo Allow backup ids to passed as filter. |
| 205 |
* @todo Allow block types to be passed as filter. |
| 206 |
*/ |
| 207 |
// Add moe re columns. |
| 208 |
$this->query->columns[ 'blocksGroup' ] = "(blocks.pinPoint & {$filters->pinPoint}) blocksGroup"; |
| 209 |
|
| 210 |
# This method allowed to run multiple time with diffeent parameters on the same instance |
| 211 |
$this->blocksQuery = array(); |
| 212 |
|
| 213 |
// Build final query. |
| 214 |
$this->blocksQuery[ 'from' ] = "`{$blocksTable}` blocks LEFT JOIN `{$pinsTable}` pins ON blocks.`id` = pins.`blockId`"; |
| 215 |
|
| 216 |
// WHERE CLAUSE WILL BE BUILD LATE IN __toString() method |
| 217 |
|
| 218 |
return $this; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* put your comment there... |
| 223 |
* |
| 224 |
* @param mixed $query |
| 225 |
*/ |
| 226 |
public function setBlocksQuery( $query ) |
| 227 |
{ |
| 228 |
|
| 229 |
$this->blocksQuery = $query; |
| 230 |
|
| 231 |
return $this; |
| 232 |
} |
| 233 |
|
| 234 |
} // End class. |