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