*/ /** * Class syntax_plugin_data_cloud */ class syntax_plugin_data_cloud extends syntax_plugin_data_table { /** * will hold the data helper plugin * @var $dthlp helper_plugin_data */ public $dthlp; /** * Constructor. Load helper plugin */ public function __construct() { $this->dthlp = plugin_load('helper', 'data'); if (!$this->dthlp) { msg('Loading the data helper failed. Make sure the data plugin is installed.', -1); } } /** * What kind of syntax are we? */ public function getType() { return 'substition'; } /** * What about paragraphs? */ public function getPType() { return 'block'; } /** * Where to sort in? */ public function getSort() { return 155; } /** * Connect pattern to lexer * * @param $mode */ public function connectTo($mode) { $this->Lexer->addSpecialPattern( '----+ *datacloud(?: [ a-zA-Z0-9_]*)?-+\n.*?\n----+', $mode, 'plugin_data_cloud' ); } /** * Builds the SQL query from the given data * * @param array &$data instruction by handler * @return bool|string SQL query or false */ public function buildSQL(&$data) { $ckey = array_keys($data['cols']); $ckey = $ckey[0]; $from = ' '; $where = ' '; $pagesjoin = ''; $tables = []; $sqlite = $this->dthlp->getDB(); if (!$sqlite) return false; $fields = ['pageid' => 'page', 'class' => 'class', 'title' => 'title']; // prepare filters (no request filters - we set them ourselves) if (is_array($data['filter']) && count($data['filter'])) { $cnt = 0; foreach ($data['filter'] as $filter) { //Note: value is already escaped $col = $filter['key']; $closecompare = ($filter['compare'] == 'IN(' ? ')' : ''); if (preg_match('/^%(\w+)%$/', $col, $m) && isset($fields[$m[1]])) { $where .= " " . $filter['logic'] . " pages." . $fields[$m[1]] . " " . $filter['compare'] . " " . $filter['value'] . $closecompare; $pagesjoin = ' LEFT JOIN pages ON pages.pid = data.pid'; } else { // filter by hidden column? if (!$tables[$col]) { $tables[$col] = 'T' . (++$cnt); $from .= ' LEFT JOIN data AS ' . $tables[$col] . ' ON ' . $tables[$col] . '.pid = data.pid'; $from .= ' AND ' . $tables[$col] . ".key = " . $sqlite->getPdo()->quote($col); } $where .= ' ' . $filter['logic'] . ' ' . $tables[$col] . '.value ' . $filter['compare'] . " " . $filter['value'] . $closecompare; } } } // build query $sql = "SELECT data.value AS value, COUNT(data.pid) AS cnt FROM data $from $pagesjoin WHERE data.key = " . $sqlite->getPdo()->quote($ckey) . " $where GROUP BY data.value"; if (isset($data['min'])) { $sql .= ' HAVING cnt >= ' . $data['min']; } $sql .= ' ORDER BY cnt DESC'; if ($data['limit']) { $sql .= ' LIMIT ' . $data['limit']; } return $sql; } protected $before_item = '