Guest User

Untitled

a guest
Mar 12th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.14 KB | None | 0 0
  1. class views_join {
  2.   /**
  3.    * Construct the views_join object.
  4.    */
  5.   function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT') {
  6.     $this->extra_type = 'AND';
  7.     if (!empty($table)) {
  8.       $this->table = $table;
  9.       $this->left_table = $left_table;
  10.       $this->left_field = $left_field;
  11.       $this->field = $field;
  12.       $this->extra = $extra;
  13.       $this->type = strtoupper($type);
  14.     }
  15.     elseif (!empty($this->definition)) {
  16.       // if no arguments, construct from definition.
  17.       // These four must exist or it will throw notices.
  18.       $this->table = $this->definition['table'];
  19.       $this->left_table = $this->definition['left_table'];
  20.       $this->left_field = $this->definition['left_field'];
  21.       $this->field = $this->definition['field'];
  22.       if (!empty($this->definition['extra'])) {
  23.         $this->extra = $this->definition['extra'];
  24.       }
  25.       if (!empty($this->definition['extra type'])) {
  26.         $this->extra_type = strtoupper($this->definition['extra type']);
  27.       }
  28.  
  29.       $this->type = !empty($this->definition['type']) ? strtoupper($this->definition['type']) : 'LEFT';
  30.     }
  31.   }
  32.  
  33.   /**
  34.    * Build the SQL for the join this object represents.
  35.    *
  36.    * @param $select_query
  37.    *   An implementation of SelectQueryInterface.
  38.    * @param $table
  39.    *   The base table to join.
  40.    * @param $view_query
  41.    *   The source query, implementation of views_plugin_query.
  42.    * @return
  43.    *
  44.    */
  45.   function build_join($select_query, $table, $view_query) {
  46.     if (empty($this->definition['table formula'])) {
  47.       $right_table = "{" . $this->table . "}";
  48.     }
  49.     else {
  50.       $right_table = $this->definition['table formula'];
  51.     }
  52.  
  53.     if ($this->left_table) {
  54.       $left = $view_query->get_table_info($this->left_table);
  55.       $left_field = "$left[alias].$this->left_field";
  56.     }
  57.     else {
  58.       // This can be used if left_field is a formula or something. It should be used only *very* rarely.
  59.       $left_field = $this->left_field;
  60.     }
  61.  
  62.     $condition = "$left_field = $table[alias].$this->field";
  63.     $arguments = array();
  64.  
  65.     // Tack on the extra.
  66.     if (isset($this->extra)) {
  67.       if (is_array($this->extra)) {
  68.         $extras = array();
  69.         foreach ($this->extra as $info) {
  70.           $extra = '';
  71.           // Figure out the table name. Remember, only use aliases provided
  72.           // if at all possible.
  73.           $join_table = '';
  74.           if (!array_key_exists('table', $info)) {
  75.             $join_table = $table['alias'] . '.';
  76.           }
  77.           elseif (isset($info['table'])) {
  78.             $join_table = $info['table'] . '.';
  79.           }
  80.  
  81.           $operator = !empty($info['operator']) ? $info['operator'] : '=';
  82.          
  83.           if (!empty($info['left_field'])) {
  84.             $extras[] = "$join_table$info[field] $operator $left[alias].$info[left_field]";
  85.           }
  86.           else if (!empty($info['value'])) {
  87.             $placeholder = ':views_join_condition_' . $select_query->nextPlaceholder();
  88.                    
  89.             if (is_array($info['value'])) {
  90.               $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
  91.               // Transform from IN() notation to = notation if just one value.
  92.               if (count($info['value']) == 1) {
  93.                 $info['value'] = array_shift($info['value']);
  94.                 $operator = $operator == 'NOT IN' ? '!=' : '=';
  95.               }
  96.             }
  97.            
  98.             $extras[] = "$join_table$info[field] $operator $placeholder";
  99.             $arguments[$placeholder] = $info['value'];
  100.           }
  101.         }
  102.  
  103.         if ($extras) {
  104.           if (count($extras) == 1) {
  105.             $condition .= ' AND ' . array_shift($extras);
  106.           }
  107.           else {
  108.             $condition .= ' AND (' . implode(' ' . $this->extra_type . ' ', $extras) . ')';
  109.           }
  110.         }
  111.       }
  112.       elseif ($this->extra && is_string($this->extra)) {
  113.         $condition .= " AND ($this->extra)";
  114.       }
  115.     }
  116.  
  117.     $select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment