| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly; |
| 3 |
|
| 4 |
class Wdk_listing extends Winter_MVC_Controller { |
| 5 |
|
| 6 |
public function __construct(){ |
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
// Edit listing method |
| 11 |
public function index() |
| 12 |
{ |
| 13 |
$this->load->model('field_m'); |
| 14 |
$this->load->model('listing_m'); |
| 15 |
$this->load->model('listingfield_m'); |
| 16 |
$this->load->model('listingusers_m'); |
| 17 |
$this->load->model('category_m'); |
| 18 |
$this->load->model('location_m'); |
| 19 |
$this->load->model('user_m'); |
| 20 |
|
| 21 |
$listing_post_id = $this->input->post_get('id'); |
| 22 |
|
| 23 |
wdk_access_check('listing_m', $listing_post_id, NULL, 'edit'); |
| 24 |
|
| 25 |
$this->data['db_data'] = NULL; |
| 26 |
|
| 27 |
$this->data['form'] = &$this->form; |
| 28 |
|
| 29 |
$this->data['categories'] = $this->category_m->get_parents(); |
| 30 |
$this->data['locations'] = $this->location_m->get_parents(); |
| 31 |
|
| 32 |
$or_like = "(meta_value LIKE '%administrator%' |
| 33 |
OR meta_value LIKE '%wdk_agency%' |
| 34 |
OR meta_value LIKE '%wdk_agent%' |
| 35 |
OR meta_value LIKE '%wdk_owner%')"; |
| 36 |
|
| 37 |
$this->data['agents'] = $this->user_m->get_agents(array(), $or_like); |
| 38 |
|
| 39 |
if(!empty($listing_post_id)) |
| 40 |
{ |
| 41 |
$listing_post = get_post( $listing_post_id ); |
| 42 |
|
| 43 |
$listing_db_data = $this->listing_m->get($listing_post_id, TRUE); |
| 44 |
|
| 45 |
$listingfield_db_data = $this->listingfield_m->get($listing_post_id, TRUE); |
| 46 |
|
| 47 |
$listingusers_db_data = $this->listingusers_m->get($listing_post_id, TRUE); |
| 48 |
|
| 49 |
$this->data['db_data'] = array_merge((array) $listing_post, |
| 50 |
(array) $listing_db_data, |
| 51 |
(array) $listingfield_db_data, |
| 52 |
(array) $listingusers_db_data); |
| 53 |
|
| 54 |
|
| 55 |
if(function_exists('run_wdk_payments')) { |
| 56 |
$this->data['packages'] = array(); |
| 57 |
|
| 58 |
global $Winter_MVC_wdk_payments; |
| 59 |
$Winter_MVC_wdk_payments->model('package_m'); |
| 60 |
|
| 61 |
$packages = $Winter_MVC_wdk_payments->package_m->get_by(array('(date_from IS NULL OR date_from < \''.current_time( 'mysql' ).'\')'=>NULL, |
| 62 |
'(date_to IS NULL OR date_to > \''.current_time( 'mysql' ).'\')'=>NULL, |
| 63 |
'(location_id IS NULL OR location_id = '.wdk_show_data('location_id', $listing_db_data, '0', TRUE, TRUE).')'=>NULL, |
| 64 |
'(category_id IS NULL OR category_id = '.wdk_show_data('category_id', $listing_db_data, '0', TRUE, TRUE).')'=>NULL)); |
| 65 |
if (count($packages) > 0) { |
| 66 |
foreach($packages as $package) { |
| 67 |
$this->data['packages'][wdk_show_data('idpackage', $package, TRUE, TRUE)] = wdk_show_data('idpackage', $package,'', TRUE, TRUE) |
| 68 |
.', '.wdk_show_data('package_name', $package,'', TRUE, TRUE) |
| 69 |
.'('.wdk_get_date(wdk_show_data('date_to', $package,'', TRUE, TRUE)).')'; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
$this->data['subscriptions'] = array(); |
| 76 |
$this->data['subscriptions_data'] = array(); |
| 77 |
if(wdk_get_option('wdk_membership_is_enable_subscriptions')){ |
| 78 |
global $Winter_MVC_wdk_membership; |
| 79 |
$Winter_MVC_wdk_membership->model('subscription_user_m'); |
| 80 |
$Winter_MVC_wdk_membership->model('subscription_m'); |
| 81 |
|
| 82 |
$this->db->select('*'); |
| 83 |
$this->db->join($Winter_MVC_wdk_membership->subscription_m->_table_name.' ON '.$Winter_MVC_wdk_membership->subscription_m->_table_name.'.idsubscription = '.$Winter_MVC_wdk_membership->subscription_user_m->_table_name.'.subscription_id'); |
| 84 |
$this->db->join($this->db->prefix.'wdk_categories ON '.$this->db->prefix.'wdk_categories.idcategory = '.$Winter_MVC_wdk_membership->subscription_m->_table_name.'.category_id', TRUE, 'LEFT'); |
| 85 |
$this->db->join($this->db->prefix.'wdk_locations ON '.$this->db->prefix.'wdk_locations.idlocation = '.$Winter_MVC_wdk_membership->subscription_m->_table_name.'.location_id', TRUE, 'LEFT'); |
| 86 |
$this->db->where(array( |
| 87 |
'(date_expire > \''.current_time( 'mysql' ).'\')'=>NULL, |
| 88 |
'(user_id = \''.wmvc_show_data('user_id', $this->data['db_data'], false).'\')'=>NULL, |
| 89 |
'(status = \'ACTIVE\')'=>NULL, |
| 90 |
) |
| 91 |
); |
| 92 |
|
| 93 |
$subscriptions = $Winter_MVC_wdk_membership->subscription_user_m->get(); |
| 94 |
|
| 95 |
if (count($subscriptions) > 0) { |
| 96 |
foreach($subscriptions as $subscription) { |
| 97 |
$this->data['subscriptions'][wdk_show_data('subscription_id',$subscription,'', TRUE, TRUE)] = wdk_show_data('subscription_id', $subscription,'', TRUE, TRUE) |
| 98 |
.', '.wdk_show_data('subscription_name', $subscription,'', TRUE, TRUE) |
| 99 |
.', '.wdk_show_data('location_title',$subscription, esc_html__('Any', 'wdk-membership'), TRUE, TRUE).' '. esc_html__('Location', 'wdk-membership') |
| 100 |
.', '.wdk_show_data('category_title',$subscription, esc_html__('Any', 'wdk-membership'), TRUE, TRUE).' '. esc_html__('Category', 'wdk-membership') |
| 101 |
.'('.wdk_get_date(wdk_show_data('date_expire', $subscription,'', TRUE, TRUE)).')'; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
$this->db->where(array('field_type !='=> 'SECTION')); |
| 108 |
$this->data['listing_fields'] = $this->field_m->get(); |
| 109 |
|
| 110 |
$this->data['fields'] = $this->field_m->get(); |
| 111 |
|
| 112 |
$rules = array( |
| 113 |
array( |
| 114 |
'field' => 'post_title', |
| 115 |
'label' => __('Title', 'wpdirectorykit'), |
| 116 |
'rules' => 'required' |
| 117 |
), |
| 118 |
array( |
| 119 |
'field' => 'post_content', |
| 120 |
'label' => __('Content', 'wpdirectorykit'), |
| 121 |
'rules' => 'required' |
| 122 |
), |
| 123 |
array( |
| 124 |
'field' => 'category_id', |
| 125 |
'label' => __('Category', 'wpdirectorykit'), |
| 126 |
'rules' => '' |
| 127 |
), |
| 128 |
array( |
| 129 |
'field' => 'location_id', |
| 130 |
'label' => __('Location', 'wpdirectorykit'), |
| 131 |
'rules' => '' |
| 132 |
), |
| 133 |
array( |
| 134 |
'field' => 'address', |
| 135 |
'label' => __('Address', 'wpdirectorykit'), |
| 136 |
'rules' => '' |
| 137 |
), |
| 138 |
array( |
| 139 |
'field' => 'lat', |
| 140 |
'label' => __('lat', 'wpdirectorykit'), |
| 141 |
'rules' => '' |
| 142 |
), |
| 143 |
array( |
| 144 |
'field' => 'lng', |
| 145 |
'label' => __('lng', 'wpdirectorykit'), |
| 146 |
'rules' => '' |
| 147 |
), |
| 148 |
array( |
| 149 |
'field' => 'listing_images', |
| 150 |
'label' => __('Listing images', 'wpdirectorykit'), |
| 151 |
'rules' => '' |
| 152 |
), |
| 153 |
array( |
| 154 |
'field' => 'is_featured', |
| 155 |
'label' => __('Is Featured', 'wpdirectorykit'), |
| 156 |
'rules' => '' |
| 157 |
), |
| 158 |
array( |
| 159 |
'field' => 'is_activated', |
| 160 |
'label' => __('Is Activated', 'wpdirectorykit'), |
| 161 |
'rules' => '' |
| 162 |
), |
| 163 |
array( |
| 164 |
'field' => 'is_approved', |
| 165 |
'label' => __('Is Approved', 'wpdirectorykit'), |
| 166 |
'rules' => '' |
| 167 |
), |
| 168 |
array( |
| 169 |
'field' => 'user_id', |
| 170 |
'label' => __('Agent', 'wpdirectorykit'), |
| 171 |
'rules' => '' |
| 172 |
), |
| 173 |
array( |
| 174 |
'field' => 'package_id', |
| 175 |
'label' => __('Package', 'wpdirectorykit'), |
| 176 |
'rules' => '' |
| 177 |
), |
| 178 |
array( |
| 179 |
'field' => 'subscription_id', |
| 180 |
'label' => __('Subscription id', 'wpdirectorykit'), |
| 181 |
'rules' => '' |
| 182 |
), |
| 183 |
); |
| 184 |
|
| 185 |
$rules[] = array( |
| 186 |
'field' => 'rank', |
| 187 |
'label' => __('Rank', 'wpdirectorykit'), |
| 188 |
'rules' => 'numeric' |
| 189 |
); |
| 190 |
|
| 191 |
|
| 192 |
foreach($this->data['fields'] as $field) |
| 193 |
{ |
| 194 |
$rules[] = |
| 195 |
array( |
| 196 |
'field' => 'field_'.$field->idfield, |
| 197 |
'label' => $field->field_label, |
| 198 |
'rules' => (wmvc_show_data('is_required', $field) == 1) ? 'required' : '' |
| 199 |
); |
| 200 |
|
| 201 |
if(isset($this->data['db_data']['field_'.$field->idfield.'_'.$field->field_type])) |
| 202 |
$this->data['db_data']['field_'.$field->idfield] = |
| 203 |
$this->data['db_data']['field_'.$field->idfield.'_'.$field->field_type]; |
| 204 |
} |
| 205 |
|
| 206 |
if($this->form->run($rules)) |
| 207 |
{ |
| 208 |
// Save procedure for basic data |
| 209 |
|
| 210 |
$data = $this->listing_m->prepare_data(wdk_get_post(), $rules, FALSE); |
| 211 |
// Save standard wp post |
| 212 |
|
| 213 |
// Create post object |
| 214 |
$listing_post = array( |
| 215 |
'ID' => $listing_post_id, |
| 216 |
'post_type' => 'wdk-listing', |
| 217 |
'post_title' => wp_strip_all_tags( $data['post_title'] ), |
| 218 |
'post_content' => $data['post_content'], |
| 219 |
'post_status' => 'publish', |
| 220 |
'post_author' => get_current_user_id() |
| 221 |
); |
| 222 |
|
| 223 |
// Insert the post into the database |
| 224 |
$id = wp_insert_post( $listing_post ); |
| 225 |
|
| 226 |
// Save our main listing data |
| 227 |
|
| 228 |
$listing_data = array('post_id' => $id); |
| 229 |
|
| 230 |
$listing_data_fields = array('category_id', 'location_id', 'address', 'lat', 'lng', 'listing_images', 'is_featured', 'is_activated','is_approved', 'package_id','rank','subscription_id'); |
| 231 |
foreach($listing_data_fields as $field_name) |
| 232 |
{ |
| 233 |
$listing_data[$field_name] = $data[$field_name]; |
| 234 |
} |
| 235 |
|
| 236 |
$image_ids = explode(',', $data['listing_images']); |
| 237 |
|
| 238 |
$listing_data['listing_images_path'] = ''; |
| 239 |
if(is_array($image_ids)) { |
| 240 |
foreach ($image_ids as $image_id) { |
| 241 |
if(is_numeric($image_id)) |
| 242 |
{ |
| 243 |
$image_path = wp_get_original_image_path( $image_id); |
| 244 |
if(!$image_path) continue; |
| 245 |
|
| 246 |
/* path of image */ |
| 247 |
$next_path = str_replace(WP_CONTENT_DIR . '/uploads/','', $image_path); |
| 248 |
|
| 249 |
/* check length listing_images_path + next image + comma, should be less 200*/ |
| 250 |
if(strlen($listing_data['listing_images_path'].$next_path)>195) break; |
| 251 |
|
| 252 |
if(!empty($listing_data['listing_images_path'])) |
| 253 |
$listing_data['listing_images_path'] .= ','; |
| 254 |
|
| 255 |
$listing_data['listing_images_path'] .= $next_path; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if(!function_exists('run_wdk_membership')) { |
| 261 |
if($listing_data['is_activated'] == 1) { |
| 262 |
$listing_data['is_approved'] = 1; |
| 263 |
} else { |
| 264 |
$listing_data['is_approved'] = 0; |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
if(!wmvc_user_in_role('administrator')) { |
| 270 |
unset($listing_data['rank']); |
| 271 |
} |
| 272 |
|
| 273 |
if(function_exists('run_wdk_payments') && wmvc_show_data('package_id', $listing_data_field, false)) { |
| 274 |
|
| 275 |
global $Winter_MVC_wdk_payments; |
| 276 |
$Winter_MVC_wdk_payments->model('package_m'); |
| 277 |
$package = $Winter_MVC_wdk_payments->package_m->get(wmvc_show_data('package_id', $listing_data_field, false), TRUE); |
| 278 |
if ($packages) { |
| 279 |
if(!isset($listing_data['rank']) || empty($listing_data['rank'])) |
| 280 |
$listing_data['rank'] = wdk_show_data('featured_rank',$package, 0, TRUE, TRUE); |
| 281 |
$listing_data['date_package_expire'] = date('Y-m-d H:i:s', strtotime('+'.wdk_show_data('days_limit',$package, 0, TRUE, TRUE).'days')); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/* dates set */ |
| 286 |
if(isset($this->data['db_data']['date'])) |
| 287 |
$listing_data['date'] = $this->data['db_data']['date']; |
| 288 |
|
| 289 |
$listing_data['date_modified'] = date('Y-m-d H:i:s'); |
| 290 |
|
| 291 |
if(empty($listing_db_data)) |
| 292 |
{ |
| 293 |
$this->listing_m->insert($listing_data, NULL); |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
$this->listing_m->insert($listing_data, $id); |
| 298 |
} |
| 299 |
|
| 300 |
// insert users/agents |
| 301 |
|
| 302 |
$this->listingusers_m->delete_where(array('post_id' => $id)); |
| 303 |
$this->listingusers_m->insert(array('post_id' => $id, 'user_id' => $this->input->post('user_id')), NULL); |
| 304 |
|
| 305 |
//exit($this->db->last_query()); |
| 306 |
|
| 307 |
// Save dynamic fields data |
| 308 |
|
| 309 |
$data['post_id'] = $id; |
| 310 |
|
| 311 |
if(empty($listingfield_db_data)) |
| 312 |
{ |
| 313 |
$this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, NULL); |
| 314 |
} |
| 315 |
else |
| 316 |
{ |
| 317 |
$this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, $id); |
| 318 |
} |
| 319 |
|
| 320 |
//exit($this->db->last_query()); |
| 321 |
|
| 322 |
//exit($this->db->last_error()); |
| 323 |
|
| 324 |
// redirect |
| 325 |
if(empty($listing_post_id) && !empty($id)) |
| 326 |
{ |
| 327 |
wp_redirect(admin_url("admin.php?page=wdk_listing&id=$id&is_updated=true")); |
| 328 |
exit; |
| 329 |
} |
| 330 |
|
| 331 |
/* fix checkbox after submit */ |
| 332 |
if(!empty($id)) |
| 333 |
{ |
| 334 |
$is_approved_before_save = $this->data['db_data']['is_approved']; |
| 335 |
|
| 336 |
$listing_post = get_post( $id ); |
| 337 |
$listing_db_data = $this->listing_m->get($id, TRUE); |
| 338 |
$listingfield_db_data = $this->listingfield_m->get($id, TRUE); |
| 339 |
$listingusers_db_data = $this->listingusers_m->get($id, TRUE); |
| 340 |
$this->data['db_data'] = array_merge((array) $listing_post, |
| 341 |
(array) $listing_db_data, |
| 342 |
(array) $listingfield_db_data, |
| 343 |
(array) $listingusers_db_data); |
| 344 |
|
| 345 |
/* if approved message */ |
| 346 |
if (!empty($listing_post_id) && wmvc_show_data('user_id', $data, false)) { |
| 347 |
if( $is_approved_before_save != 1 && wmvc_show_data('is_approved', $data) == 1 && wmvc_show_data('is_activated', $data) == 1) { |
| 348 |
|
| 349 |
$user = get_userdata( wmvc_show_data('user_id', $data) ); |
| 350 |
|
| 351 |
$data_message = array(); |
| 352 |
$data_message['user'] = $user; |
| 353 |
$data_message['post_id'] = $id; |
| 354 |
$data_message['post'] = $this->data['db_data']; |
| 355 |
$ret = wdk_mail($user->user_email, __('Your Listing Approved', 'wpdirectorykit'), $data_message, 'new_listing_approved'); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
$this->load->view('wdk_listing/index', $this->data); |
| 363 |
} |
| 364 |
|
| 365 |
} |
| 366 |
|