| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
class User_m extends Winter_MVC_Model { |
| 9 |
|
| 10 |
public $_table_name = ''; |
| 11 |
public $_table_name_meta = ''; |
| 12 |
public $_order_by = 'user_nicename'; |
| 13 |
public $_primary_key = 'ID'; |
| 14 |
public $_own_columns = array(); |
| 15 |
public $_timestamps = TRUE; |
| 16 |
protected $_primary_filter = 'intval'; |
| 17 |
public $form_admin = array(); |
| 18 |
public $fields_list = NULL; |
| 19 |
|
| 20 |
public function __construct(){ |
| 21 |
parent::__construct(); |
| 22 |
|
| 23 |
global $wpdb; |
| 24 |
|
| 25 |
$this->_table_name = $wpdb->users; |
| 26 |
$this->_table_name_meta = $wpdb->usermeta; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_agents_names($where = array(), $like = "meta_value LIKE '%wdk_agent%'") |
| 30 |
{ |
| 31 |
$wp_usermeta_table = $this->_table_name_meta; |
| 32 |
|
| 33 |
$this->db->select('*'); |
| 34 |
$this->db->from($this->_table_name); |
| 35 |
$this->db->join($wp_usermeta_table.' ON '.$this->_table_name.'.ID = '.$wp_usermeta_table.'.user_id'); |
| 36 |
$this->db->where('meta_key', $this->db->prefix.'capabilities'); |
| 37 |
$this->db->where($where); |
| 38 |
$this->db->like($like); |
| 39 |
|
| 40 |
$query = $this->get(); |
| 41 |
|
| 42 |
$list_with_keys = array(); |
| 43 |
if ($this->db->num_rows() > 0) |
| 44 |
{ |
| 45 |
$results = $this->db->results(); |
| 46 |
|
| 47 |
foreach($results as $result) |
| 48 |
{ |
| 49 |
$list_with_keys[$result->user_id] = '#'.$result->ID.', '.$result->display_name; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $list_with_keys; |
| 54 |
} |
| 55 |
|
| 56 |
public function get_agents($where = array(), $like = "meta_value LIKE '%wdk_agent%'") |
| 57 |
{ |
| 58 |
$wp_usermeta_table = $this->_table_name_meta; |
| 59 |
|
| 60 |
$this->db->select('*'); |
| 61 |
$this->db->from($this->_table_name); |
| 62 |
$this->db->join($wp_usermeta_table.' ON '.$this->_table_name.'.ID = '.$wp_usermeta_table.'.user_id'); |
| 63 |
$this->db->where('meta_key', $this->db->prefix.'capabilities'); |
| 64 |
$this->db->where($where); |
| 65 |
$this->db->like($like); |
| 66 |
|
| 67 |
/* |
| 68 |
if(!empty($or_like) && is_array($or_like)){ |
| 69 |
foreach($or_like as $key=>$value) |
| 70 |
{ |
| 71 |
if(empty($value)) continue; |
| 72 |
$this->db->or_like('meta_value', "%$value%"); |
| 73 |
} |
| 74 |
}*/ |
| 75 |
|
| 76 |
$query = $this->get(); |
| 77 |
|
| 78 |
//$list_with_keys = array(0 => __('Root', 'wpdirectorykit')); |
| 79 |
$list_with_keys = array(); |
| 80 |
if ($this->db->num_rows() > 0) |
| 81 |
{ |
| 82 |
$results = $this->db->results(); |
| 83 |
|
| 84 |
foreach($results as $result) |
| 85 |
{ |
| 86 |
$list_with_keys[$result->user_id] = $result->user_email.', '.$result->display_name; |
| 87 |
} |
| 88 |
} |
| 89 |
//wmvc_dump($list_with_keys); |
| 90 |
|
| 91 |
return $list_with_keys; |
| 92 |
} |
| 93 |
|
| 94 |
public function get_pagination($limit = NULL, $offset = NULL, $where = array(), $order_by = NULL, $like = "meta_value LIKE '%wdk_agent%'", $show_other_agents_litings = FALSE, $only_listings_assigned = FALSE) |
| 95 |
{ |
| 96 |
$wp_usermeta_table = $this->_table_name_meta; |
| 97 |
$this->load->model('listing_m'); |
| 98 |
$alt_agents_table = $this->db->prefix.'wdk_listings_users'; |
| 99 |
|
| 100 |
$this->db->select('ID,user_login,user_nicename,user_email,display_name,user_status,user_url, COUNT('.$this->db->prefix.'wdk_listings.user_id_editor) AS listings_counter'); |
| 101 |
$this->db->from($this->_table_name); |
| 102 |
|
| 103 |
$this->db->join($wp_usermeta_table.' ON '.$this->_table_name.'.ID = '.$wp_usermeta_table.'.user_id'); |
| 104 |
$this->db->join($this->listing_m->_table_name.' ON '.$this->listing_m->_table_name.'.user_id_editor = '.$this->_table_name.'.ID', TRUE, 'LEFT'); |
| 105 |
|
| 106 |
$this->db->join($alt_agents_table.' ON '.$alt_agents_table.'.user_id = '.$this->_table_name.'.ID', TRUE, 'LEFT'); |
| 107 |
|
| 108 |
$this->db->where($where); |
| 109 |
$this->db->where('meta_key', $this->db->prefix.'capabilities'); |
| 110 |
|
| 111 |
|
| 112 |
if($only_listings_assigned) { |
| 113 |
$this->db->where(array($this->db->prefix.'wdk_listings.user_id_editor != 0'=>NULL)); |
| 114 |
} |
| 115 |
|
| 116 |
if(!empty($like)) |
| 117 |
$this->db->like($like); |
| 118 |
|
| 119 |
$this->db->group_by('ID'); |
| 120 |
|
| 121 |
$this->db->limit($limit); |
| 122 |
$this->db->offset($offset); |
| 123 |
|
| 124 |
if(!empty($order_by)){ |
| 125 |
$this->db->order_by($order_by); |
| 126 |
} else { |
| 127 |
$this->db->order_by($this->_order_by); |
| 128 |
} |
| 129 |
|
| 130 |
$query = $this->db->get(); |
| 131 |
|
| 132 |
if ($this->db->num_rows() > 0) |
| 133 |
return $this->db->results(); |
| 134 |
|
| 135 |
return array(); |
| 136 |
} |
| 137 |
|
| 138 |
public function total($where = array(), $like = "meta_value LIKE '%wdk_agent%'", $only_listings_assigned = FALSE) |
| 139 |
{ |
| 140 |
$wp_usermeta_table = $this->_table_name_meta; |
| 141 |
$this->load->model('listing_m'); |
| 142 |
$this->db->select('COUNT(DISTINCT ID) as total_count, COUNT('.$this->db->prefix.'wdk_listings.user_id_editor) AS listings_counter'); |
| 143 |
$this->db->from($this->_table_name); |
| 144 |
|
| 145 |
$this->db->join($wp_usermeta_table.' ON '.$this->_table_name.'.ID = '.$wp_usermeta_table.'.user_id'); |
| 146 |
$this->db->join($this->listing_m->_table_name.' ON '.$this->listing_m->_table_name.'.user_id_editor = '.$this->_table_name.'.ID', TRUE, 'LEFT'); |
| 147 |
|
| 148 |
$this->db->where($where); |
| 149 |
$this->db->where('meta_key', $this->db->prefix.'capabilities'); |
| 150 |
|
| 151 |
if($only_listings_assigned) { |
| 152 |
$this->db->where(array($this->db->prefix.'wdk_listings.user_id_editor != 0'=>NULL)); |
| 153 |
} |
| 154 |
|
| 155 |
if(!empty($like)) |
| 156 |
$this->db->like($like); |
| 157 |
|
| 158 |
$this->db->where($where); |
| 159 |
$this->db->order_by($this->_order_by); |
| 160 |
$query = $this->db->get(); |
| 161 |
|
| 162 |
$res = $this->db->results(); |
| 163 |
|
| 164 |
if(isset($res[0]->total_count)) |
| 165 |
return $res[0]->total_count; |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
public function get_available_fields() |
| 171 |
{ |
| 172 |
$fields = $this->db->list_fields($this->_table_name); |
| 173 |
|
| 174 |
return $fields; |
| 175 |
} |
| 176 |
} |
| 177 |
?> |