| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
class Listing_m extends Winter_MVC_Model { |
| 9 |
|
| 10 |
public $_table_name = 'wdk_listings'; |
| 11 |
public $_order_by = 'post_id DESC'; |
| 12 |
public $_primary_key = 'post_id'; |
| 13 |
public $_own_columns = array(); |
| 14 |
public $_timestamps = TRUE; |
| 15 |
protected $_primary_filter = 'intval'; |
| 16 |
public $form_admin = array(); |
| 17 |
public $fields_list = NULL; |
| 18 |
|
| 19 |
public function __construct(){ |
| 20 |
parent::__construct(); |
| 21 |
} |
| 22 |
|
| 23 |
/* [START] For dynamic data table */ |
| 24 |
|
| 25 |
public function get_available_fields() |
| 26 |
{ |
| 27 |
$fields = $this->db->list_fields($this->_table_name); |
| 28 |
|
| 29 |
return $fields; |
| 30 |
} |
| 31 |
|
| 32 |
public function total($where = array(), $user_check=FALSE, $user_id=NULL) |
| 33 |
{ |
| 34 |
|
| 35 |
$this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id'; |
| 36 |
$this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC'; |
| 37 |
|
| 38 |
$post_table = $this->db->prefix.'posts'; |
| 39 |
$fields_table = $this->db->prefix.'wdk_listings_fields'; |
| 40 |
|
| 41 |
$this->db->select('COUNT(DISTINCT '.$this->db->prefix.'wdk_listings.post_id) as total_count'); |
| 42 |
$this->db->from($this->_table_name); |
| 43 |
|
| 44 |
$this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID'); |
| 45 |
$this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id'); |
| 46 |
|
| 47 |
/* locations */ |
| 48 |
$this->db->join($this->db->prefix.'wdk_locations AS location_1 ON '.$this->_table_name.'.location_id = location_1.idlocation', NULL, 'LEFT'); |
| 49 |
/* for parents search*/ |
| 50 |
for($i = 2 ; $i <= 2; $i++) { |
| 51 |
$this->db->join($this->db->prefix.'wdk_locations AS location_'.$i.' ON location_'.($i-1).'.parent_id = location_'.$i.'.idlocation', NULL, 'LEFT'); |
| 52 |
} |
| 53 |
|
| 54 |
if(isset($where['is_activated'])){ |
| 55 |
$where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated']; |
| 56 |
unset($where['is_activated']); |
| 57 |
} |
| 58 |
|
| 59 |
if(isset($where['is_approved'])){ |
| 60 |
$where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved']; |
| 61 |
unset($where['is_approved']); |
| 62 |
} |
| 63 |
|
| 64 |
if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) ) ) |
| 65 |
{ |
| 66 |
$this->db->join($this->db->prefix.'wdk_listings_users ON '.$this->_table_name.'.post_id = '.$this->db->prefix.'wdk_listings_users.post_id', 'left'); |
| 67 |
|
| 68 |
if(!is_null($user_id) && !empty($user_id)) |
| 69 |
{ |
| 70 |
$this->db->where($this->db->prefix.'wdk_listings_users`.`user_id', $user_id); |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
$this->db->where($this->db->prefix.'wdk_listings_users`.`user_id', get_current_user_id()); |
| 75 |
} |
| 76 |
$this->db->where($where); |
| 77 |
} else { |
| 78 |
$this->db->where($where); |
| 79 |
} |
| 80 |
|
| 81 |
$this->db->where($where); |
| 82 |
$this->db->order_by($this->_order_by); |
| 83 |
|
| 84 |
$query = $this->db->get(); |
| 85 |
|
| 86 |
$res = $this->db->results(); |
| 87 |
|
| 88 |
$this->_primary_key = 'post_id'; |
| 89 |
$this->_order_by = 'post_id DESC'; |
| 90 |
|
| 91 |
if(isset($res[0]->total_count)) |
| 92 |
return $res[0]->total_count; |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
public function get_pagination($limit, $offset, $where = array(), $user_check = FALSE, $user_id=NULL) |
| 98 |
{ |
| 99 |
$this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id'; |
| 100 |
$this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC'; |
| 101 |
|
| 102 |
$post_table = $this->db->prefix.'posts'; |
| 103 |
$fields_table = $this->db->prefix.'wdk_listings_fields'; |
| 104 |
|
| 105 |
|
| 106 |
$this->db->select($this->_table_name.'.*, '.$post_table.'.*,'.$fields_table.'.*,'.$fields_table.'.post_id AS fields_post_id'); |
| 107 |
$this->db->from($this->_table_name); |
| 108 |
$this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID'); |
| 109 |
$this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id'); |
| 110 |
|
| 111 |
/* locations */ |
| 112 |
$this->db->join($this->db->prefix.'wdk_locations AS location_1 ON '.$this->_table_name.'.location_id = location_1.idlocation', NULL, 'LEFT'); |
| 113 |
/* for parents search*/ |
| 114 |
for($i = 2 ; $i <= 2; $i++) { |
| 115 |
$this->db->join($this->db->prefix.'wdk_locations AS location_'.$i.' ON location_'.($i-1).'.parent_id = location_'.$i.'.idlocation', NULL, 'LEFT'); |
| 116 |
} |
| 117 |
|
| 118 |
if(isset($where['is_activated'])){ |
| 119 |
$where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated']; |
| 120 |
unset($where['is_activated']); |
| 121 |
} |
| 122 |
|
| 123 |
if(isset($where['is_approved'])){ |
| 124 |
$where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved']; |
| 125 |
unset($where['is_approved']); |
| 126 |
} |
| 127 |
|
| 128 |
if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) ) ) |
| 129 |
{ |
| 130 |
$this->db->join($this->db->prefix.'wdk_listings_users ON '.$this->_table_name.'.post_id = '.$this->db->prefix.'wdk_listings_users.post_id', 'left'); |
| 131 |
|
| 132 |
if(!is_null($user_id) && !empty($user_id)) |
| 133 |
{ |
| 134 |
$this->db->where($this->db->prefix.'wdk_listings_users`.`user_id', $user_id); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
$this->db->where($this->db->prefix.'wdk_listings_users`.`user_id', get_current_user_id()); |
| 139 |
} |
| 140 |
$this->db->where($where); |
| 141 |
} else { |
| 142 |
$this->db->where($where); |
| 143 |
} |
| 144 |
|
| 145 |
$this->db->limit($limit); |
| 146 |
$this->db->offset($offset); |
| 147 |
|
| 148 |
$this->db->order_by($this->_order_by); |
| 149 |
|
| 150 |
$query = $this->db->get(); |
| 151 |
|
| 152 |
$this->_primary_key = 'post_id'; |
| 153 |
$this->_order_by = 'post_id DESC'; |
| 154 |
|
| 155 |
if ($this->db->num_rows() > 0) |
| 156 |
return $this->db->results(); |
| 157 |
|
| 158 |
return array(); |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
public function get($id = NULL, $single = FALSE) |
| 163 |
{ |
| 164 |
$this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id'; |
| 165 |
$this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC'; |
| 166 |
|
| 167 |
$post_table = $this->db->prefix.'posts'; |
| 168 |
$fields_table = $this->db->prefix.'wdk_listings_fields'; |
| 169 |
|
| 170 |
$this->db->select($this->_table_name.'.*, '.$post_table.'.*,'.$fields_table.'.*,'.$fields_table.'.post_id AS fields_post_id'); |
| 171 |
$this->db->from($this->_table_name); |
| 172 |
$this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID'); |
| 173 |
$this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id'); |
| 174 |
|
| 175 |
/*$query = $this->db->get(); |
| 176 |
|
| 177 |
if ($this->db->num_rows() > 0) |
| 178 |
return $this->db->row(); |
| 179 |
|
| 180 |
return array();*/ |
| 181 |
|
| 182 |
$return = parent::get($id, $single); |
| 183 |
|
| 184 |
$this->_primary_key = 'post_id'; |
| 185 |
$this->_order_by = 'post_id DESC'; |
| 186 |
|
| 187 |
return $return; |
| 188 |
} |
| 189 |
|
| 190 |
public function check_deletable($post_id) |
| 191 |
{ |
| 192 |
if(wmvc_user_in_role('administrator')) return true; |
| 193 |
|
| 194 |
$this->load->model('listingusers_m'); |
| 195 |
$user = $this->listingusers_m->get($post_id, TRUE); |
| 196 |
if(wmvc_show_data('user_id', $user) == get_current_user_id()) |
| 197 |
return true; |
| 198 |
|
| 199 |
return false; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
public function delete($post_id) { |
| 204 |
|
| 205 |
if(!$this->check_deletable($post_id)) return false; |
| 206 |
|
| 207 |
$this->load->model('listingfield_m'); |
| 208 |
|
| 209 |
/* remove listing */ |
| 210 |
parent::delete($post_id); |
| 211 |
$this->listingfield_m->delete($post_id); |
| 212 |
|
| 213 |
/* remove post */ |
| 214 |
wp_delete_post($post_id, true); |
| 215 |
|
| 216 |
do_action('wpdirectorykit/model/listing/delete', $post_id); |
| 217 |
|
| 218 |
return true; |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
/* [END] For dynamic data table */ |
| 223 |
|
| 224 |
public function is_related($item_id, $user_id, $method = 'edit') |
| 225 |
{ |
| 226 |
$this->load->model('listingusers_m'); |
| 227 |
$user = $this->listingusers_m->get($item_id, TRUE); |
| 228 |
if(wmvc_show_data('user_id', $user) == $user_id) |
| 229 |
return true; |
| 230 |
|
| 231 |
return false; |
| 232 |
} |
| 233 |
} |
| 234 |
?> |