| 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, $count_agency_listings_false = TRUE) |
| 95 |
{ |
| 96 |
$wp_usermeta_table = $this->_table_name_meta; |
| 97 |
$this->load->model('listing_m'); |
| 98 |
$this->load->model('cachedusers_m'); |
| 99 |
|
| 100 |
$alt_agents_table = $this->db->prefix.'wdk_listings_users'; |
| 101 |
$select_list = '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'; |
| 102 |
|
| 103 |
if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) { |
| 104 |
$select_list .=','.$this->cachedusers_m->_table_name.'.*'; |
| 105 |
} |
| 106 |
|
| 107 |
if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php') && $count_agency_listings_false ) { |
| 108 |
$select_list .=', SUM('.$this->db->prefix.'wdk_membership_agency_agent.listings_count) AS agency_listings_counter'; |
| 109 |
} |
| 110 |
|
| 111 |
$this->db->select($select_list); |
| 112 |
$this->db->from($this->_table_name); |
| 113 |
|
| 114 |
$this->db->join($wp_usermeta_table.' ON '.$this->_table_name.'.ID = '.$wp_usermeta_table.'.user_id'); |
| 115 |
$this->db->join($this->listing_m->_table_name.' ON ('.$this->listing_m->_table_name.'.user_id_editor = '.$this->_table_name.'.ID AND ('.$this->listing_m->_table_name.'.is_activated = 1 AND '.$this->listing_m->_table_name.'.is_approved = 1))', TRUE, 'LEFT'); |
| 116 |
|
| 117 |
$this->db->join($alt_agents_table.' ON '.$alt_agents_table.'.user_id = '.$this->_table_name.'.ID', TRUE, 'LEFT'); |
| 118 |
|
| 119 |
|
| 120 |
if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php') && $count_agency_listings_false) { |
| 121 |
$this->db->join($this->db->prefix.'wdk_membership_agency_agent ON ('.$this->db->prefix.'wdk_membership_agency_agent.agency_id = '.$this->_table_name.'.ID AND '.$this->db->prefix.'wdk_membership_agency_agent.status = "CONFIRMED")', TRUE, 'LEFT'); |
| 122 |
} |
| 123 |
|
| 124 |
if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) { |
| 125 |
$this->db->join($this->cachedusers_m->_table_name.' ON '.$this->cachedusers_m->_table_name.'.cacheduser_user_id = '.$this->_table_name.'.ID', TRUE, 'LEFT'); |
| 126 |
} |
| 127 |
|
| 128 |
$this->db->where($where); |
| 129 |
$this->db->where('meta_key', $this->db->prefix.'capabilities'); |
| 130 |
|
| 131 |
|
| 132 |
if($only_listings_assigned) { |
| 133 |
$this->db->where(array($this->db->prefix.'wdk_listings.user_id_editor != 0'=>NULL)); |
| 134 |
} |
| 135 |
|
| 136 |
if(!empty($like)) |
| 137 |
$this->db->like($like); |
| 138 |
|
| 139 |
$this->db->group_by('ID'); |
| 140 |
|
| 141 |
$this->db->limit($limit); |
| 142 |
$this->db->offset($offset); |
| 143 |
|
| 144 |
if(!empty($order_by)){ |
| 145 |
$this->db->order_by($order_by); |
| 146 |
} else { |
| 147 |
$this->db->order_by($this->_order_by); |
| 148 |
} |
| 149 |
|
| 150 |
$query = $this->db->get(); |
| 151 |
|
| 152 |
if ($this->db->num_rows() > 0) |
| 153 |
return $this->db->results(); |
| 154 |
|
| 155 |
return array(); |
| 156 |
} |
| 157 |
|
| 158 |
public function total($where = array(), $like = "meta_value LIKE '%wdk_agent%'", $only_listings_assigned = FALSE) |
| 159 |
{ |
| 160 |
$wp_usermeta_table = $this->_table_name_meta; |
| 161 |
$this->load->model('listing_m'); |
| 162 |
$this->db->select('COUNT(DISTINCT ID) as total_count, COUNT('.$this->db->prefix.'wdk_listings.user_id_editor) AS listings_counter'); |
| 163 |
$this->db->from($this->_table_name); |
| 164 |
|
| 165 |
$this->db->join($wp_usermeta_table.' ON '.$this->_table_name.'.ID = '.$wp_usermeta_table.'.user_id'); |
| 166 |
$this->db->join($this->listing_m->_table_name.' ON ('.$this->listing_m->_table_name.'.user_id_editor = '.$this->_table_name.'.ID AND ('.$this->listing_m->_table_name.'.is_activated = 1 AND '.$this->listing_m->_table_name.'.is_approved = 1) )', TRUE, 'LEFT'); |
| 167 |
|
| 168 |
$this->db->where($where); |
| 169 |
$this->db->where('meta_key', $this->db->prefix.'capabilities'); |
| 170 |
|
| 171 |
if($only_listings_assigned) { |
| 172 |
$this->db->where(array($this->db->prefix.'wdk_listings.user_id_editor != 0'=>NULL)); |
| 173 |
} |
| 174 |
|
| 175 |
if(!empty($like)) |
| 176 |
$this->db->like($like); |
| 177 |
|
| 178 |
$this->db->where($where); |
| 179 |
$this->db->order_by($this->_order_by); |
| 180 |
$query = $this->db->get(); |
| 181 |
|
| 182 |
$res = $this->db->results(); |
| 183 |
|
| 184 |
if(isset($res[0]->total_count)) |
| 185 |
return $res[0]->total_count; |
| 186 |
|
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
public function get_available_fields() |
| 191 |
{ |
| 192 |
$fields = $this->db->list_fields($this->_table_name); |
| 193 |
|
| 194 |
return $fields; |
| 195 |
} |
| 196 |
|
| 197 |
public function remove_user_data($user_id = NULL) { |
| 198 |
if(empty($user_id)) return false; |
| 199 |
|
| 200 |
/* listings */ |
| 201 |
$this->_remove_data_from('this', 'listing_m', 'user_id_editor', 'post_id', $user_id); |
| 202 |
|
| 203 |
/* favorites */ |
| 204 |
$this->_remove_data_from('Winter_MVC_wdk_favorites', 'favorite_m', 'user_id', 'idfavorite', $user_id); |
| 205 |
|
| 206 |
/* reservation */ |
| 207 |
$this->_remove_data_from('Winter_MVC_wdk_bookings', 'reservation_m', 'user_id', 'idreservation', $user_id); |
| 208 |
|
| 209 |
/* membership subscription user */ |
| 210 |
$this->_remove_data_from('Winter_MVC_wdk_membership', 'subscription_user_m', 'user_id', 'idsubscription_user', $user_id); |
| 211 |
|
| 212 |
/* reviews */ |
| 213 |
$this->_remove_data_from('Winter_MVC_wdk_reviews', 'reviews_m', 'user_id', 'idreviews', $user_id); |
| 214 |
|
| 215 |
/* save search */ |
| 216 |
$this->_remove_data_from('Winter_MVC_wdk_save_search', 'save_search_m', 'user_id', 'idsave_search', $user_id); |
| 217 |
|
| 218 |
/* listing claim */ |
| 219 |
$this->_remove_data_from('Winter_MVC_wdk_listing_claim', 'listing_claim_m', 'user_id', 'idlistingclaim', $user_id); |
| 220 |
|
| 221 |
/* report abuse */ |
| 222 |
$this->_remove_data_from('Winter_MVC_wdk_report_abuse', 'report_abuse_m', 'user_id', 'idreportabuse', $user_id); |
| 223 |
|
| 224 |
/* messages */ |
| 225 |
$this->_remove_data_from('this', 'messages_m', 'user_id_sender', 'idmessage', $user_id); |
| 226 |
|
| 227 |
return TRUE; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
private function _remove_data_from($addon_object_name, $model_name, $user_column, $row_id, $user_id) { |
| 232 |
|
| 233 |
if($addon_object_name != 'this') |
| 234 |
global ${$addon_object_name}; |
| 235 |
|
| 236 |
if($addon_object_name == 'this' || isset(${$addon_object_name})) { |
| 237 |
if($addon_object_name != 'this') { |
| 238 |
${$addon_object_name}->model($model_name); |
| 239 |
} else { |
| 240 |
${$addon_object_name}->load->model($model_name); |
| 241 |
} |
| 242 |
|
| 243 |
$this->db->select('*'); |
| 244 |
$this->db->where($user_column, $user_id); |
| 245 |
$this->db->from(${$addon_object_name}->{$model_name}->_table_name); |
| 246 |
$query = $this->db->get(); |
| 247 |
if ($this->db->num_rows() > 0){ |
| 248 |
foreach ($this->db->results() as $row) { |
| 249 |
${$addon_object_name}->{$model_name}->delete($row->{$row_id}, $user_id); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
?> |