PluginProbe
WP Directory Kit / 1.4.8
WP Directory Kit v1.4.8
1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 1.4.1 All 35 releases
wpdirectorykit / application / models / Messages_m.php

Messages_m.php in WP Directory Kit 1.4.8, at application/models/Messages_m.php

500 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8 class Messages_m extends Winter_MVC_Model {
9
10 public $_table_name = 'wdk_messages';
11 public $_order_by = 'idmessage DESC';
12 public $_primary_key = 'idmessage';
13 public $_own_columns = array();
14 public $_timestamps = TRUE;
15 protected $_primary_filter = 'intval';
16 public $form_admin = array();
17 public $fields_list_dash = NULL;
18 public $fields_list_dash_view = NULL;
19 public $fields_list = NULL;
20 public $current_user_id = NULL;
21
22 public function __construct(){
23 parent::__construct();
24 $this->current_user_id = get_current_user_id();
25
26 $this->fields_list_dash = array(
27 array(
28 'field' => 'post_id',
29 'field_label' => __('Listing id', 'wpdirectorykit'),
30 'hint' => '',
31 'field_type' => 'LISTING_READONLY',
32 'rules' => ''
33 ),
34 array(
35 'field' => 'date',
36 'field_label' => __('Date', 'wpdirectorykit'),
37 'field_type' => 'DATETIME_READONLY',
38 'rules' => ''
39 ),
40 array(
41 'field' => 'message',
42 'field_label' => __('Message', 'wpdirectorykit'),
43 'hint' => '',
44 'field_type' => 'TEXTAREA_READONLY',
45 'rules' => ''
46 ),
47 array(
48 'field' => 'is_readed',
49 'field_label' => __('Is Readed', 'wpdirectorykit'),
50 'hint' => '',
51 'field_type' => 'CHECKBOX_READONLY',
52 'rules' => ''
53 ),
54 array(
55 'field' => 'json_object',
56 'field_label' => __('Full Mail Data', 'wpdirectorykit'),
57 'hint' => '',
58 'field_type' => 'JSON_READONLY',
59 'rules' => ''
60 ),
61 );
62
63 foreach($this->fields_list_dash as $key=>$field)
64 {
65 $this->fields_list_dash[$key]['label'] = $field['field_label'];
66 }
67
68 $this->fields_list_dash_view = array(
69 array(
70 'field' => 'post_id',
71 'field_label' => __('Listing id', 'wpdirectorykit'),
72 'hint' => '',
73 'field_type' => 'LISTING_READONLY',
74 'rules' => ''
75 ),
76 array(
77 'field' => 'date',
78 'field_label' => __('Date', 'wpdirectorykit'),
79 'field_type' => 'DATE_READONLY',
80 'rules' => ''
81 ),
82 array(
83 'field' => 'message',
84 'field_label' => __('Message', 'wpdirectorykit'),
85 'hint' => '',
86 'field_type' => 'TEXTAREA_READONLY',
87 'rules' => ''
88 ),
89 array(
90 'field' => 'is_readed',
91 'field_label' => __('Is Readed', 'wpdirectorykit'),
92 'hint' => '',
93 'field_type' => 'CHECKBOX_READONLY',
94 'rules' => ''
95 ),
96 array(
97 'field' => 'json_object',
98 'field_label' => __('Full Mail Data', 'wpdirectorykit'),
99 'hint' => '',
100 'field_type' => 'JSON_READONLY',
101 'rules' => ''
102 ),
103 );
104
105 foreach($this->fields_list_dash_view as $key=>$field)
106 {
107 $this->fields_list_dash_view[$key]['label'] = $field['field_label'];
108 }
109 }
110
111 /* [START] For dynamic data table */
112
113 public function get_available_fields()
114 {
115 $fields = $this->db->list_fields($this->_table_name);
116
117 return $fields;
118 }
119
120 public function total($where = array(), $user_check = FALSE, $user_id=NULL)
121 {
122 $this->db->select('COUNT(*) as total_count');
123 $this->db->from($this->_table_name);
124 $this->db->where($where);
125 $this->db->order_by($this->_order_by);
126
127 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
128 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
129 {
130 if(!is_null($user_id) && !empty($user_id))
131 {
132 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id);
133 }
134 elseif($this->current_user_id)
135 {
136 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id);
137 }
138
139 }
140
141 $query = $this->db->get();
142
143 $res = $this->db->results();
144
145 if(isset($res[0]->total_count))
146 return $res[0]->total_count;
147
148 return 0;
149 }
150
151 /*
152 $chat_unread_count set TRUE only if activated Live Chat Addon
153 */
154 public function get_pagination($limit, $offset, $where = array(), $order_by = NULL, $user_check = FALSE, $user_id=NULL, $chat_unread_count = FALSE)
155 {
156 $post_table = $this->db->prefix.'posts';
157
158 if($chat_unread_count) {
159 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title,
160 COUNT('.$this->db->prefix.'wdk_messages_chat.related_key) AS chat_unread_counter');
161
162 if(!is_null($user_id) && !empty($user_id))
163 {
164 $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');
165 }
166 elseif($this->current_user_id)
167 {
168 $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`!= '.$this->current_user_id.')', NULL, 'LEFT');
169 }
170 $this->db->group_by('idmessage');
171 } else {
172 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title');
173 }
174
175 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
176 {
177 if(!is_null($user_id) && !empty($user_id))
178 {
179 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id);
180 }
181 elseif($this->current_user_id)
182 {
183 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id);
184 }
185 }
186
187 $this->db->group_by('idmessage');
188
189 $this->db->from($this->_table_name);
190 $this->db->where($where);
191 $this->db->limit($limit);
192 $this->db->offset($offset);
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
200 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
201 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT');
202
203 $query = $this->db->get();
204
205 if ($this->db->num_rows() > 0)
206 return $this->db->results();
207
208 return array();
209 }
210
211 public function total_merge($where = array(), $user_check = FALSE, $user_id=NULL)
212 {
213 $this->db->select('COUNT(*) as total_count');
214 $this->db->from($this->_table_name);
215 $this->db->where($where);
216 $this->db->order_by($this->_order_by);
217
218 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
219
220 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
221 {
222 if(!is_null($user_id) && !empty($user_id))
223 {
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.' )');
225 }
226 elseif($this->current_user_id)
227 {
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.' )');
229 }
230 }
231
232 $query = $this->db->get();
233
234 $res = $this->db->results();
235
236 if(isset($res[0]->total_count))
237 return $res[0]->total_count;
238
239 return 0;
240 }
241
242
243 /*
244 $chat_unread_count set TRUE only if activated Live Chat Addon
245 */
246
247 public function get_pagination_merge($limit, $offset, $where = array(), $order_by = NULL, $user_check = FALSE, $user_id=NULL, $chat_unread_count = FALSE)
248 {
249 $post_table = $this->db->prefix.'posts';
250
251 if($chat_unread_count) {
252 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title,
253 COUNT('.$this->db->prefix.'wdk_messages_chat.related_key) AS chat_unread_counter');
254
255 if(!is_null($user_id) && !empty($user_id))
256 {
257 $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');
258 }
259 elseif($this->current_user_id)
260 {
261 $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`!= '.$this->current_user_id.')', NULL, 'LEFT');
262 }
263 $this->db->group_by('idmessage');
264 } else {
265 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title');
266 }
267
268 $this->db->from($this->_table_name);
269 $this->db->where($where);
270 $this->db->limit($limit);
271 $this->db->offset($offset);
272
273 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
274 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT');
275
276 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
277 {
278 if(!is_null($user_id) && !empty($user_id))
279 {
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.' )');
281 }
282 elseif($this->current_user_id)
283 {
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.' )');
285 }
286 }
287
288 if(!empty($order_by)){
289 $this->db->order_by($order_by);
290 } else {
291 $this->db->order_by($this->_order_by);
292 }
293
294 $query = $this->db->get();
295
296 if ($this->db->num_rows() > 0)
297 return $this->db->results();
298
299 return array();
300 }
301
302 public function total_outbox($where = array(), $user_check = FALSE, $user_id=NULL)
303 {
304 $this->db->select('COUNT(*) as total_count');
305 $this->db->from($this->_table_name);
306 $this->db->where($where);
307 $this->db->order_by($this->_order_by);
308
309 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
310 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
311 {
312 if(!is_null($user_id) && !empty($user_id))
313 {
314 $this->db->where('user_id_sender', $user_id);
315 }
316 elseif($this->current_user_id)
317 {
318 $this->db->where('user_id_sender', $this->current_user_id);
319 }
320
321 }
322
323 $query = $this->db->get();
324
325 $res = $this->db->results();
326
327 if(isset($res[0]->total_count))
328 return $res[0]->total_count;
329
330 return 0;
331 }
332
333 /*
334 $chat_unread_count set TRUE only if activated Live Chat Addon
335 */
336 public function get_pagination_outbox($limit, $offset, $where = array(), $order_by = NULL, $user_check = FALSE, $user_id=NULL, $chat_unread_count = FALSE)
337 {
338 $post_table = $this->db->prefix.'posts';
339 if($chat_unread_count) {
340 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title,
341 COUNT('.$this->db->prefix.'wdk_messages_chat.related_key) AS chat_unread_counter');
342 if(!is_null($user_id) && !empty($user_id))
343 {
344 $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');
345 }
346 elseif($this->current_user_id)
347 {
348 $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`!= '.$this->current_user_id.')', NULL, 'LEFT');
349 }
350 $this->db->group_by('idmessage');
351 } else {
352 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title');
353 }
354
355 $this->db->from($this->_table_name);
356 $this->db->where($where);
357 $this->db->limit($limit);
358 $this->db->offset($offset);
359
360 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
361 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT');
362
363 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
364 {
365 if(!is_null($user_id) && !empty($user_id))
366 {
367 $this->db->where('user_id_sender', $user_id);
368 }
369 elseif($this->current_user_id)
370 {
371 $this->db->where('user_id_sender', $this->current_user_id);
372 }
373
374 }
375
376 if(!empty($order_by)){
377 $this->db->order_by($order_by);
378 } else {
379 $this->db->order_by($this->_order_by);
380 }
381
382 $query = $this->db->get();
383
384 if ($this->db->num_rows() > 0)
385 return $this->db->results();
386
387 return array();
388 }
389
390 public function check_deletable($item_id, $user_id=NULL)
391 {
392
393 if(empty($user_id))
394 $user_id = get_current_user_id();
395
396 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date');
397 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
398 $this->db->from($this->_table_name);
399 $row = $this->get($item_id, TRUE);
400
401 if(wmvc_show_data('user_id_receiver', $row) == $user_id || wmvc_show_data('user_id_editor', $row) == $user_id )
402 return true;
403
404 if(wmvc_user_in_role('administrator') || current_user_can('wdk_listings_manage')) return true;
405
406 return false;
407 }
408
409
410 /* [END] For dynamic data table */
411
412 public function is_related($item_id, $user_id, $method = 'edit')
413 {
414 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date');
415 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
416 $this->db->from($this->_table_name);
417 $row = $this->get($item_id, TRUE);
418
419 if($method == 'view') {
420 if(wmvc_show_data('user_id_receiver', $row) == $user_id || wmvc_show_data('user_id_editor', $row) == $user_id || wmvc_show_data('user_id_sender', $row) == $user_id )
421 return true;
422 } else {
423 if(wmvc_show_data('user_id_receiver', $row) == $user_id || wmvc_show_data('user_id_editor', $row) == $user_id )
424 return true;
425 }
426
427 if(wmvc_user_in_role('administrator') || current_user_can('wdk_listings_manage')) return true;
428
429 return false;
430 }
431
432 public function do_notified_by_user($user_id = NULL)
433 {
434 if(empty($user_id))
435 $user_id = get_current_user_id();
436
437 global $wpdb;
438 if($user_id){
439 $wpdb->query("UPDATE `{$this->_table_name}`
440 LEFT JOIN `{$this->db->prefix}wdk_listings` ON `{$this->db->prefix}wdk_listings`.post_id = `{$this->_table_name}`.post_id
441 SET `is_notified` = 1
442 WHERE
443 (`user_id_editor` = {$user_id}
444 OR `user_id_sender` = {$user_id})
445 ");
446 return true;
447 }
448
449 return false;
450 }
451
452 public function delete($id, $user_id=NULL) {
453
454 if($user_id === NULL)
455 $user_id = get_current_user_id();
456
457 if(!$this->check_deletable($id, $user_id)) return false;
458
459 /* remove */
460 parent::delete($id);
461
462 return true;
463 }
464
465
466 public function total_unread($user_id=NULL)
467 {
468 $post_table = $this->db->prefix.'posts';
469
470 $this->db->select('COUNT(*) as total_count');
471
472 if(empty($user_id))
473 {
474 $user_id = $this->current_user_id;
475 }
476
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);
480
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');
483
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
500 ?>