| @@ -33,9 +33,9 @@ | ||
| 33 | 33 | ), |
| 34 | 34 | array( |
| 35 | 35 | 'field' => 'date', |
| 36 | 36 | 'field_label' => __('Date', 'wpdirectorykit'), |
| 37 | - 'field_type' => 'DATE_READONLY', | |
| 37 | + 'field_type' => 'DATETIME_READONLY', | |
| 38 | 38 | 'rules' => '' |
| 39 | 39 | ), |
| 40 | 40 | array( |
| 41 | 41 | 'field' => 'message', |
| @@ -190,8 +190,14 @@ | ||
| 190 | 190 | $this->db->where($where); |
| 191 | 191 | $this->db->limit($limit); |
| 192 | 192 | $this->db->offset($offset); |
| 193 | 193 | |
| 194 | + if(!empty($order_by)){ | |
| 195 | + $this->db->order_by($order_by); | |
| 196 | + } else { | |
| 197 | + $this->db->order_by($this->_order_by); | |
| 198 | + } | |
| 199 | + | |
| 194 | 200 | $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT'); |
| 195 | 201 | $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT'); |
| 196 | 202 | |
| 197 | 203 | $query = $this->db->get(); |
| @@ -214,13 +220,13 @@ | ||
| 214 | 220 | if( $user_check || (!is_null($user_id) && !empty($user_id) ) ) |
| 215 | 221 | { |
| 216 | 222 | if(!is_null($user_id) && !empty($user_id)) |
| 217 | 223 | { |
| 218 | - $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR user_id_sender = '.$user_id.' )'); | |
| 224 | + $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR user_id_sender = '.$user_id.' OR user_id_receiver = '.$user_id.' )'); | |
| 219 | 225 | } |
| 220 | 226 | elseif($this->current_user_id) |
| 221 | 227 | { |
| 222 | - $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '. $this->current_user_id.' OR user_id_sender = '. $this->current_user_id.' )'); | |
| 228 | + $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '. $this->current_user_id.' OR user_id_sender = '. $this->current_user_id.' OR user_id_receiver = '. $this->current_user_id.' )'); | |
| 223 | 229 | } |
| 224 | 230 | } |
| 225 | 231 | |
| 226 | 232 | $query = $this->db->get(); |
| @@ -270,13 +276,13 @@ | ||
| 270 | 276 | if( $user_check || (!is_null($user_id) && !empty($user_id) ) ) |
| 271 | 277 | { |
| 272 | 278 | if(!is_null($user_id) && !empty($user_id)) |
| 273 | 279 | { |
| 274 | - $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR user_id_sender = '.$user_id.' )'); | |
| 280 | + $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR user_id_sender = '.$user_id.' OR user_id_receiver = '.$user_id.' )'); | |
| 275 | 281 | } |
| 276 | 282 | elseif($this->current_user_id) |
| 277 | 283 | { |
| 278 | - $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '. $this->current_user_id.' OR user_id_sender = '. $this->current_user_id.' )'); | |
| 284 | + $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '. $this->current_user_id.' OR user_id_sender = '. $this->current_user_id.' OR user_id_receiver = '. $this->current_user_id.' )'); | |
| 279 | 285 | } |
| 280 | 286 | } |
| 281 | 287 | |
| 282 | 288 | if(!empty($order_by)){ |
| @@ -380,10 +386,14 @@ | ||
| 380 | 386 | |
| 381 | 387 | return array(); |
| 382 | 388 | } |
| 383 | 389 | |
| 384 | - public function check_deletable($item_id, $user_id) | |
| 390 | + public function check_deletable($item_id, $user_id=NULL) | |
| 385 | 391 | { |
| 392 | + | |
| 393 | + if(empty($user_id)) | |
| 394 | + $user_id = get_current_user_id(); | |
| 395 | + | |
| 386 | 396 | $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date'); |
| 387 | 397 | $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT'); |
| 388 | 398 | $this->db->from($this->_table_name); |
| 389 | 399 | $row = $this->get($item_id, TRUE); |
| @@ -390,10 +400,10 @@ | ||
| 390 | 400 | |
| 391 | 401 | if(wmvc_show_data('user_id_receiver', $row) == $user_id || wmvc_show_data('user_id_editor', $row) == $user_id ) |
| 392 | 402 | return true; |
| 393 | 403 | |
| 394 | - if(wmvc_user_in_role('administrator')) return true; | |
| 395 | - | |
| 404 | + if(wmvc_user_in_role('administrator') || current_user_can('wdk_listings_manage')) return true; | |
| 405 | + | |
| 396 | 406 | return false; |
| 397 | 407 | } |
| 398 | 408 | |
| 399 | 409 | |
| @@ -413,9 +423,9 @@ | ||
| 413 | 423 | if(wmvc_show_data('user_id_receiver', $row) == $user_id || wmvc_show_data('user_id_editor', $row) == $user_id ) |
| 414 | 424 | return true; |
| 415 | 425 | } |
| 416 | 426 | |
| 417 | - if(wmvc_user_in_role('administrator')) return true; | |
| 427 | + if(wmvc_user_in_role('administrator') || current_user_can('wdk_listings_manage')) return true; | |
| 418 | 428 | |
| 419 | 429 | return false; |
| 420 | 430 | } |
| 421 | 431 | |
| @@ -439,14 +449,14 @@ | ||
| 439 | 449 | return false; |
| 440 | 450 | } |
| 441 | 451 | |
| 442 | 452 | public function delete($id, $user_id=NULL) { |
| 443 | - | |
| 453 | + | |
| 444 | 454 | if($user_id === NULL) |
| 445 | 455 | $user_id = get_current_user_id(); |
| 446 | 456 | |
| 447 | 457 | if(!$this->check_deletable($id, $user_id)) return false; |
| 448 | - | |
| 458 | + | |
| 449 | 459 | /* remove */ |
| 450 | 460 | parent::delete($id); |
| 451 | 461 | |
| 452 | 462 | return true; |
| @@ -451,23 +461,40 @@ | ||
| 451 | 461 | |
| 452 | 462 | return true; |
| 453 | 463 | } |
| 454 | 464 | |
| 455 | -} | |
| 465 | + | |
| 466 | + public function total_unread($user_id=NULL) | |
| 467 | + { | |
| 468 | + $post_table = $this->db->prefix.'posts'; | |
| 456 | 469 | |
| 457 | -/* | |
| 470 | + $this->db->select('COUNT(*) as total_count'); | |
| 458 | 471 | |
| 459 | -SELECT wptest_wdk_messages.*,wptest_wdk_listings.*, wptest_wdk_messages.date AS message_date,wptest_posts.post_title | |
| 460 | -, COUNT(wptest_wdk_messages_chat.related_key) AS chat_counter | |
| 461 | -FROM wptest_wdk_messages | |
| 472 | + if(empty($user_id)) | |
| 473 | + { | |
| 474 | + $user_id = $this->current_user_id; | |
| 475 | + } | |
| 462 | 476 | |
| 463 | -LEFT JOIN wptest_wdk_listings ON wptest_wdk_listings.post_id = wptest_wdk_messages.post_id | |
| 464 | -LEFT JOIN wptest_posts ON wptest_wdk_messages.post_id = wptest_posts.ID | |
| 465 | -LEFT JOIN wptest_wdk_messages_chat ON (wptest_wdk_messages.idmessage = wptest_wdk_messages_chat.related_key AND chat_is_readed IS NULL ) | |
| 477 | + $this->db->join($this->db->prefix.'wdk_messages_chat ON ('.$this->_table_name.'.idmessage = '.$this->db->prefix.'wdk_messages_chat.related_key AND chat_is_readed IS NULL AND `outgoing_msg_user_id`!= '.$user_id.')', NULL, 'LEFT'); | |
| 478 | + | |
| 479 | + $this->db->from($this->_table_name); | |
| 466 | 480 | |
| 467 | -WHERE (`wptest_wdk_listings`.`user_id_editor` = 4 OR user_id_sender = 4 ) | |
| 468 | -GROUP BY idmessage | |
| 469 | -ORDER BY idmessage DESC | |
| 470 | -LIMIT 10 OFFSET 0; | |
| 481 | + $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT'); | |
| 482 | + $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT'); | |
| 471 | 483 | |
| 472 | -*/ | |
| 484 | + $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR user_id_sender = '.$user_id.' OR user_id_receiver = '.$user_id.' )'); | |
| 485 | + | |
| 486 | + $this->db->where('(('.$this->_table_name.'.is_readed IS NULL AND related_key IS NULL AND user_id_editor = '.$user_id.') OR ('.$this->_table_name.'.is_readed IS NOT NULL AND related_key IS NOT NULL))'); | |
| 487 | + | |
| 488 | + $query = $this->db->get(); | |
| 489 | + | |
| 490 | + $res = $this->db->results(); | |
| 491 | + | |
| 492 | + if(isset($res[0]->total_count)) | |
| 493 | + return $res[0]->total_count; | |
| 494 | + | |
| 495 | + return 0; | |
| 496 | + } | |
| 497 | + | |
| 498 | +} | |
| 499 | + | |
| 473 | 500 | ?> |