Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class views_join {
- /**
- * Construct the views_join object.
- */
- function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT') {
- $this->extra_type = 'AND';
- if (!empty($table)) {
- $this->table = $table;
- $this->left_table = $left_table;
- $this->left_field = $left_field;
- $this->field = $field;
- $this->extra = $extra;
- $this->type = strtoupper($type);
- }
- elseif (!empty($this->definition)) {
- // if no arguments, construct from definition.
- // These four must exist or it will throw notices.
- $this->table = $this->definition['table'];
- $this->left_table = $this->definition['left_table'];
- $this->left_field = $this->definition['left_field'];
- $this->field = $this->definition['field'];
- if (!empty($this->definition['extra'])) {
- $this->extra = $this->definition['extra'];
- }
- if (!empty($this->definition['extra type'])) {
- $this->extra_type = strtoupper($this->definition['extra type']);
- }
- $this->type = !empty($this->definition['type']) ? strtoupper($this->definition['type']) : 'LEFT';
- }
- }
- /**
- * Build the SQL for the join this object represents.
- *
- * @param $select_query
- * An implementation of SelectQueryInterface.
- * @param $table
- * The base table to join.
- * @param $view_query
- * The source query, implementation of views_plugin_query.
- * @return
- *
- */
- function build_join($select_query, $table, $view_query) {
- if (empty($this->definition['table formula'])) {
- $right_table = "{" . $this->table . "}";
- }
- else {
- $right_table = $this->definition['table formula'];
- }
- if ($this->left_table) {
- $left = $view_query->get_table_info($this->left_table);
- $left_field = "$left[alias].$this->left_field";
- }
- else {
- // This can be used if left_field is a formula or something. It should be used only *very* rarely.
- $left_field = $this->left_field;
- }
- $condition = "$left_field = $table[alias].$this->field";
- $arguments = array();
- // Tack on the extra.
- if (isset($this->extra)) {
- if (is_array($this->extra)) {
- $extras = array();
- foreach ($this->extra as $info) {
- $extra = '';
- // Figure out the table name. Remember, only use aliases provided
- // if at all possible.
- $join_table = '';
- if (!array_key_exists('table', $info)) {
- $join_table = $table['alias'] . '.';
- }
- elseif (isset($info['table'])) {
- $join_table = $info['table'] . '.';
- }
- $operator = !empty($info['operator']) ? $info['operator'] : '=';
- if (!empty($info['left_field'])) {
- $extras[] = "$join_table$info[field] $operator $left[alias].$info[left_field]";
- }
- else if (!empty($info['value'])) {
- $placeholder = ':views_join_condition_' . $select_query->nextPlaceholder();
- if (is_array($info['value'])) {
- $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
- // Transform from IN() notation to = notation if just one value.
- if (count($info['value']) == 1) {
- $info['value'] = array_shift($info['value']);
- $operator = $operator == 'NOT IN' ? '!=' : '=';
- }
- }
- $extras[] = "$join_table$info[field] $operator $placeholder";
- $arguments[$placeholder] = $info['value'];
- }
- }
- if ($extras) {
- if (count($extras) == 1) {
- $condition .= ' AND ' . array_shift($extras);
- }
- else {
- $condition .= ' AND (' . implode(' ' . $this->extra_type . ' ', $extras) . ')';
- }
- }
- }
- elseif ($this->extra && is_string($this->extra)) {
- $condition .= " AND ($this->extra)";
- }
- }
- $select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment