| @@ -15,41 +15,22 @@ | ||
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * |
| 18 | 18 | */ |
| 19 | -class CJTPinsBlockSQLView extends CJTSQLView | |
| 20 | -{ | |
| 19 | +class CJTPinsBlockSQLView extends CJTSQLView { | |
| 21 | 20 | |
| 22 | 21 | /** |
| 23 | 22 | * put your comment there... |
| 24 | 23 | * |
| 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 | 24 | * @param mixed $dbDriver |
| 40 | 25 | * @return CJTPinsBlockView |
| 41 | 26 | */ |
| 42 | - public function __construct( $driver ) | |
| 43 | - { | |
| 27 | + public function __construct($driver) { | |
| 44 | 28 | // Initialize SQLView Parent. |
| 45 | - parent::__construct( $driver ); | |
| 46 | - | |
| 29 | + parent::__construct($driver); | |
| 47 | 30 | // Set default columns. |
| 48 | - $this->query->columns = array | |
| 49 | - ( | |
| 31 | + $this->query->columns = array( | |
| 50 | 32 | 'blocks.id', |
| 51 | - 'blocks.owner', | |
| 52 | 33 | 'blocks.name', |
| 53 | 34 | 'blocks.pinPoint', |
| 54 | 35 | 'blocks.location', |
| 55 | 36 | 'blocks.links', |
| @@ -54,9 +35,8 @@ | ||
| 54 | 35 | 'blocks.location', |
| 55 | 36 | 'blocks.links', |
| 56 | 37 | 'blocks.expressions' |
| 57 | 38 | ); |
| 58 | - | |
| 59 | 39 | } |
| 60 | 40 | |
| 61 | 41 | /** |
| 62 | 42 | * put your comment there... |
| @@ -61,37 +41,41 @@ | ||
| 61 | 41 | /** |
| 62 | 42 | * put your comment there... |
| 63 | 43 | * |
| 64 | 44 | */ |
| 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 | - | |
| 45 | + public function __toString() { | |
| 46 | + // Prepare tables name. | |
| 47 | + $blocksTable = $this->driver->getTableName(cssJSToolbox::$config->database->tables->blocks); | |
| 48 | + $pinsTable = $this->driver->getTableName(cssJSToolbox::$config->database->tables->blockPins); | |
| 74 | 49 | $filters = $this->query->filter; |
| 75 | - $customPins = $this->customPins; | |
| 76 | - | |
| 77 | - $customPins = implode( ' OR ', $customPins ); | |
| 78 | - | |
| 79 | - if ( ! empty( $customPins ) ) | |
| 80 | - { | |
| 50 | + // build custom pins filter. | |
| 51 | + $customPins = array(); | |
| 52 | + foreach ($filters->customPins as $pinFilter) { | |
| 53 | + $pinFilter = (object) $pinFilter; | |
| 54 | + $pins = implode(',', $pinFilter->pins); | |
| 55 | + $customPins[] = "((blocks.`pinPoint` & {$pinFilter->flag}) AND (pins.pin = '{$pinFilter->pin}') AND (pins.`value` IN ({$pins})))"; | |
| 56 | + } | |
| 57 | + $customPins = implode(' OR ', $customPins); | |
| 58 | + if (!empty($customPins)) { | |
| 81 | 59 | $customPins = " OR {$customPins}"; |
| 82 | 60 | } |
| 83 | - | |
| 84 | 61 | // 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 | - | |
| 62 | + $excludes = implode(',', $filters->excludes); | |
| 63 | + $excludes = !empty($excludes) ? " AND `id` NOT IN ({$excludes})" : ''; | |
| 64 | + /** | |
| 65 | + * @todo Allow backup ids to passed as filter. | |
| 66 | + * @todo Allow block types to be passed as filter. | |
| 67 | + */ | |
| 68 | + // Add moe re columns. | |
| 69 | + $this->query->columns['blocksGroup'] = "(blocks.pinPoint & {$filters->pinPoint}) blocksGroup"; | |
| 70 | + // Build final query. | |
| 71 | + $query['from'] = "`{$blocksTable}` blocks | |
| 72 | + LEFT JOIN `{$pinsTable}` pins | |
| 73 | + ON blocks.`id` = pins.`blockId`"; | |
| 74 | + $query['where'] = "(((`backupId` IS NULL) AND (`state` = '{$filters->state}'){$excludes}) AND | |
| 75 | + ((blocks.`pinPoint` & {$filters->pinPoint}){$customPins}))"; | |
| 76 | + // Combine all into one statement. | |
| 77 | + $query = $this->buildQuery($query['from'], $query['where']); | |
| 94 | 78 | return $query; |
| 95 | 79 | } |
| 96 | 80 | |
| 97 | 81 | /** |
| @@ -97,138 +81,25 @@ | ||
| 97 | 81 | /** |
| 98 | 82 | * put your comment there... |
| 99 | 83 | * |
| 100 | 84 | */ |
| 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; | |
| 85 | + public function exec() { | |
| 86 | + return $this->driver->select($this, OBJECT_K); | |
| 116 | 87 | } |
| 117 | - | |
| 88 | + | |
| 118 | 89 | /** |
| 119 | 90 | * put your comment there... |
| 120 | 91 | * |
| 121 | 92 | * @param mixed $pinPoint |
| 122 | 93 | * @param mixed $customPins |
| 123 | - * @param mixed $state | |
| 124 | - * @param mixed $excludes | |
| 125 | 94 | */ |
| 126 | - public function & filters( $pinPoint, $customPins, $state = 'active', $excludes = array() ) | |
| 127 | - { | |
| 128 | - | |
| 129 | - $filters = array | |
| 130 | - ( | |
| 95 | + public function filters($pinPoint, $customPins, $state = 'active', $excludes = array()) { | |
| 96 | + $filters = array( | |
| 131 | 97 | 'state' => $state, |
| 132 | 98 | 'pinPoint' => $pinPoint, |
| 133 | 99 | 'customPins' => $customPins, |
| 134 | 100 | 'excludes' => $excludes, |
| 135 | 101 | ); |
| 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; | |
| 102 | + $this->query->filter = (object) $filters; | |
| 232 | 103 | } |
| 233 | 104 | |
| 234 | 105 | } // End class. |