/** * Set OR element conditions * @param JBDatabaseQuery $select * @param string $elementId * @param string|array $value * @param int $i * @param bool $exact * @return array */ public function conditionOR(JBDatabaseQuery $select, $elementId, $value, $i = 0, $exact = false) { $select->leftJoin(ZOO_TABLE_CATEGORY_ITEM . ' AS tCategoryItem ON tCategoryItem.item_id = tItem.id'); return $this->_getWhere($value, $elementId); }
/** * Add order to query * @param JBDatabaseQuery $select * @param array $order * @param string $itemType */ protected function _addOrder(JBDatabaseQuery $select, $order, $itemType) { $select->order('tItem.priority DESC'); if (!empty($order) && is_array($order)) { $orders = $order; if (isset($order['field'])) { $orders = array($order); } foreach ($orders as $order) { $order = $this->app->data->create($order); $reverse = $order->get('reverse'); $orderParam = $order->get('order'); if (!empty($reverse)) { if (is_array($reverse) && isset($reverse[0])) { $dir = $reverse[0] == 1 ? 'DESC' : 'ASC'; } else { $dir = $reverse == 1 ? 'DESC' : 'ASC'; } } else { if (!empty($orderParam)) { $orderParam = trim(strtoupper($orderParam)); $dir = $orderParam == 'DESC' ? 'DESC' : 'ASC'; } else { $dir = 'ASC'; } } if (!in_array($order->get('mode'), array('s', 'n', 'd'), true)) { $order->set('mode', 's'); } $field = $order->get('field'); if ($field == 'corename') { $select->order('tItem.name ' . $dir); } else { if ($field == 'corealias') { $select->order('tItem.alias ' . $dir); } else { if ($field == 'corecreated') { $select->order('tItem.created ' . $dir); } else { if ($field == 'corehits') { $select->order('tItem.hits ' . $dir); } else { if ($field == 'coremodified') { $select->order('tItem.modified ' . $dir); } else { if ($field == 'corepublish_down') { $select->order('tItem.publish_down ' . $dir); } else { if ($field == 'corepublish_up') { $select->order('tItem.publish_up ' . $dir); } else { if ($field == 'coreauthor') { $select->leftJoin('#__users AS tJoomlaUsers ON tItem.created_by = tJoomlaUsers.id')->order('tJoomlaUsers.name ' . $dir); } else { if ($field == 'random') { $select->order('RAND()'); } elseif (strpos($field, '__')) { list($elementId, $id) = explode('__', $field); $select->leftJoin(ZOO_TABLE_JBZOO_SKU . ' AS tSku ON tSku.item_id = tItem.id')->where('tSku.element_id = ?', $elementId)->where('tSku.param_id = ?', $id)->where('tSku.variant = \'-1\'')->order('tSku.value_' . $order->get('mode') . ' ' . $dir); } else { $fieldName = $this->_jbtables->getFieldName($field, $order->get('mode')); $columns = $this->_jbtables->getFields($this->_jbtables->getIndexTable($itemType)); if (in_array($fieldName, $columns, true)) { $select->order('tIndex.' . $fieldName . ' ' . $dir)->where('tIndex.' . $fieldName . ' IS NOT NULL')->select($fieldName); } } } } } } } } } } } } }
/** * Render explain table * Function ported from Joomla debug plugin * @param JBDatabaseQuery $select * @return null|string */ protected function _explain(JBDatabaseQuery $select) { if (!(class_exists('jbdump') || JDEBUG)) { return null; } $table = $this->app->database->queryAssocList('EXPLAIN ' . $select->__toString()); if (!$table) { return null; } $html = array(); $html[] = '<table class="table" style="width:1600px"><tr>'; foreach (array_keys($table[0]) as $k) { $html[] = '<th>' . htmlspecialchars($k) . '</th>'; } $html[] = '</tr>'; foreach ($table as $tr) { $html[] = '<tr>'; foreach ($tr as $k => $td) { if ($td === null) { $td = 'NULL'; } if ($k == 'Error') { $html[] = '<td class="dbg-warning">' . htmlspecialchars($td); } elseif ($k == 'key') { if ($td === 'NULL') { $html[] = '<td><strong style="color:#f00;">NO_INDEX</strong>'; } else { $html[] = '<td><strong>' . htmlspecialchars($td) . '</strong>'; } } elseif ($k == 'Extra') { $htmlTd = htmlspecialchars($td); $htmlTd = preg_replace('/([^;]) /', '\\1 ', $htmlTd); $htmlTdWithWarnings = str_replace('Using filesort', '<strong style="color:#f00;">USE_FILESORT</strong>', $htmlTd); $html[] = '<td>' . $htmlTdWithWarnings; } elseif ($k == 'possible_keys') { $td = str_replace(',', ",\n", $td); $html[] = '<td>' . htmlspecialchars($td); } else { $html[] = '<td>' . htmlspecialchars($td); } $html[] = '</td>'; } $html[] = '</tr>'; } $html[] = '</table>'; $result = implode(PHP_EOL, $html); jbdump::sql($select); dump($result, 0, 'Explain::html'); }