Completed
Branch FET-8476-powered-by-event-espr... (f0ec09)
by
unknown
105:01 queued 88:27
created

EE_Table_Base::is_global()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Base class for defining the tables that comprise models. This is used to store information
5
 * about the table\s alias, private key, etc.
6
 */
7
abstract class EE_Table_Base{
8
9
	/**
10
	 * This holds the table_name without the table prefix.
11
	 * @var string
12
	 */
13
	var $_table_name;
14
15
16
	/**
17
	 * This holds what is used as the alias for the table in queries.
18
	 * @var string
19
	 */
20
	var $_table_alias;
21
22
23
	/**
24
	 * Table's private key column
25
	 * @var string
26
	 */
27
	protected $_pk_column;
28
29
30
	/**
31
	 * Whether this table is a global table (in multisite) or specific to site.
32
	 * @var bool
33
	 */
34
	protected $_global;
35
36
	/**
37
	 *
38
	 * @global wpdb $wpdb
39
	 * @param string $table_name with or without wpdb prefix
40
	 * @param string $pk_column
41
	 * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install,
42
	 *					or whether each site on a multisite install has a copy of this table
43
	 */
44
	function __construct($table_name, $pk_column, $global = false ){
45
		$this->_global = $global;
46
		$prefix = $this->get_table_prefix();
47
		//if they added the prefix, let's remove it because we delay adding the prefix until right when its needed.
48
		if ( strpos( $table_name, $prefix ) === 0 ) {
49
			$table_name = ltrim( $table_name, $prefix );
50
		}
51
		$this->_table_name = $table_name;
52
		$this->_pk_column = $pk_column;
53
	}
54
55
56
57
58
	/**
59
	 * This returns the table prefix for the current model state.
60
	 *
61
	 * @global wpdb     $wpdb
62
	 * @return string
63
	 */
64
	public function get_table_prefix() {
65
		global $wpdb;
66
67
		if ( $this->_global ) {
68
			$prefix = $wpdb->base_prefix;
69
		} else {
70
			$prefix = $wpdb->get_blog_prefix( EEM_Base::get_model_query_blog_id() );
71
		}
72
		return $prefix;
73
	}
74
75
76
	/**
77
	 * Used to set the table_alias property
78
	 *
79
	 * @param string $table_alias
80
	 */
81
	function _construct_finalize_with_alias($table_alias){
82
		$this->_table_alias = $table_alias;
83
	}
84
85
86
	/**
87
	 * Returns the fully qualified table name for the database (includes the table prefix current for the blog).
88
	 * @return string
89
	 */
90
	function get_table_name(){
91
		return $this->get_table_prefix() . $this->_table_name;
92
	}
93
94
95
96
97
	/**
98
	 * Provides what is currently set as the alias for the table to be used in queries.
99
	 *
100
	 * @return string
101
	 * @throws EE_Error
102
	 */
103
	function get_table_alias(){
104
		if( ! $this->_table_alias){
105
			throw new EE_Error("You must call _construct_finalize_with_alias before using the EE_Table_Base. Did you forget to call parent::__construct at the end of your EEMerimental_Base child's __construct?");
106
		}
107
		return $this->_table_alias;
108
	}
109
110
111
112
	/**
113
	 *
114
	 * @return string name of column of PK
115
	 */
116
	function get_pk_column(){
117
		return $this->_pk_column;
118
	}
119
120
121
122
	/**
123
	 * returns a string with the table alias, a period, and the private key's column.
124
	 * @return string
125
	 */
126
	function get_fully_qualified_pk_column(){
127
		$sql =  $this->get_table_alias().".".$this->get_pk_column();
128
		return $sql;
129
	}
130
131
132
	/**
133
	 * returns the special sql for a inner select with a limit.
134
	 * @return string    SQL select
135
	 */
136
	public function get_select_join_limit( $limit ) {
137
		$limit = is_array( $limit ) ? 'LIMIT ' . implode(',', array_map( 'intval', $limit ) ) : 'LIMIT ' . (int) $limit;
138
		$SQL = SP . '(SELECT * FROM ' . $this->_table_name . SP . $limit . ') AS ' . $this->_table_alias;
139
		return $SQL;
140
	}
141
	
142
	/**
143
	 * Returns whether or not htis is a global table (ie, on multisite there's
144
	 * only one of these tables, on the main blog)
145
	 * @return boolean
146
	 */
147
	public function is_global() {
148
		return $this->_global;
149
	}
150
}
151