PluginProbe
WP Directory Kit / 1.1.8
WP Directory Kit v1.1.8
1.5.9 1.5.7 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 All 37 releases
wpdirectorykit / application / models / Messages_m.php

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

473 lines 18.4 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' => 'DATE_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 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
195 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT');
196
197 $query = $this->db->get();
198
199 if ($this->db->num_rows() > 0)
200 return $this->db->results();
201
202 return array();
203 }
204
205 public function total_merge($where = array(), $user_check = FALSE, $user_id=NULL)
206 {
207 $this->db->select('COUNT(*) as total_count');
208 $this->db->from($this->_table_name);
209 $this->db->where($where);
210 $this->db->order_by($this->_order_by);
211
212 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
213
214 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
215 {
216 if(!is_null($user_id) && !empty($user_id))
217 {
218 $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR user_id_sender = '.$user_id.' )');
219 }
220 elseif($this->current_user_id)
221 {
222 $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '. $this->current_user_id.' OR user_id_sender = '. $this->current_user_id.' )');
223 }
224 }
225
226 $query = $this->db->get();
227
228 $res = $this->db->results();
229
230 if(isset($res[0]->total_count))
231 return $res[0]->total_count;
232
233 return 0;
234 }
235
236
237 /*
238 $chat_unread_count set TRUE only if activated Live Chat Addon
239 */
240
241 public function get_pagination_merge($limit, $offset, $where = array(), $order_by = NULL, $user_check = FALSE, $user_id=NULL, $chat_unread_count = FALSE)
242 {
243 $post_table = $this->db->prefix.'posts';
244
245 if($chat_unread_count) {
246 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title,
247 COUNT('.$this->db->prefix.'wdk_messages_chat.related_key) AS chat_unread_counter');
248
249 if(!is_null($user_id) && !empty($user_id))
250 {
251 $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');
252 }
253 elseif($this->current_user_id)
254 {
255 $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');
256 }
257 $this->db->group_by('idmessage');
258 } else {
259 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title');
260 }
261
262 $this->db->from($this->_table_name);
263 $this->db->where($where);
264 $this->db->limit($limit);
265 $this->db->offset($offset);
266
267 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
268 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT');
269
270 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
271 {
272 if(!is_null($user_id) && !empty($user_id))
273 {
274 $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR user_id_sender = '.$user_id.' )');
275 }
276 elseif($this->current_user_id)
277 {
278 $this->db->where('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '. $this->current_user_id.' OR user_id_sender = '. $this->current_user_id.' )');
279 }
280 }
281
282 if(!empty($order_by)){
283 $this->db->order_by($order_by);
284 } else {
285 $this->db->order_by($this->_order_by);
286 }
287
288 $query = $this->db->get();
289
290 if ($this->db->num_rows() > 0)
291 return $this->db->results();
292
293 return array();
294 }
295
296 public function total_outbox($where = array(), $user_check = FALSE, $user_id=NULL)
297 {
298 $this->db->select('COUNT(*) as total_count');
299 $this->db->from($this->_table_name);
300 $this->db->where($where);
301 $this->db->order_by($this->_order_by);
302
303 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
304 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
305 {
306 if(!is_null($user_id) && !empty($user_id))
307 {
308 $this->db->where('user_id_sender', $user_id);
309 }
310 elseif($this->current_user_id)
311 {
312 $this->db->where('user_id_sender', $this->current_user_id);
313 }
314
315 }
316
317 $query = $this->db->get();
318
319 $res = $this->db->results();
320
321 if(isset($res[0]->total_count))
322 return $res[0]->total_count;
323
324 return 0;
325 }
326
327 /*
328 $chat_unread_count set TRUE only if activated Live Chat Addon
329 */
330 public function get_pagination_outbox($limit, $offset, $where = array(), $order_by = NULL, $user_check = FALSE, $user_id=NULL, $chat_unread_count = FALSE)
331 {
332 $post_table = $this->db->prefix.'posts';
333 if($chat_unread_count) {
334 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title,
335 COUNT('.$this->db->prefix.'wdk_messages_chat.related_key) AS chat_unread_counter');
336 if(!is_null($user_id) && !empty($user_id))
337 {
338 $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');
339 }
340 elseif($this->current_user_id)
341 {
342 $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');
343 }
344 $this->db->group_by('idmessage');
345 } else {
346 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date,'.$post_table.'.post_title');
347 }
348
349 $this->db->from($this->_table_name);
350 $this->db->where($where);
351 $this->db->limit($limit);
352 $this->db->offset($offset);
353
354 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
355 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT');
356
357 if( $user_check || (!is_null($user_id) && !empty($user_id) ) )
358 {
359 if(!is_null($user_id) && !empty($user_id))
360 {
361 $this->db->where('user_id_sender', $user_id);
362 }
363 elseif($this->current_user_id)
364 {
365 $this->db->where('user_id_sender', $this->current_user_id);
366 }
367
368 }
369
370 if(!empty($order_by)){
371 $this->db->order_by($order_by);
372 } else {
373 $this->db->order_by($this->_order_by);
374 }
375
376 $query = $this->db->get();
377
378 if ($this->db->num_rows() > 0)
379 return $this->db->results();
380
381 return array();
382 }
383
384 public function check_deletable($item_id, $user_id)
385 {
386 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date');
387 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
388 $this->db->from($this->_table_name);
389 $row = $this->get($item_id, TRUE);
390
391 if(wmvc_show_data('user_id_receiver', $row) == $user_id || wmvc_show_data('user_id_editor', $row) == $user_id )
392 return true;
393
394 if(wmvc_user_in_role('administrator')) return true;
395
396 return false;
397 }
398
399
400 /* [END] For dynamic data table */
401
402 public function is_related($item_id, $user_id, $method = 'edit')
403 {
404 $this->db->select($this->_table_name.'.*,'.$this->db->prefix.'wdk_listings.*, '.$this->_table_name.'.date AS message_date');
405 $this->db->join($this->db->prefix.'wdk_listings ON '.$this->db->prefix.'wdk_listings.post_id = '.$this->_table_name.'.post_id', NULL, 'LEFT');
406 $this->db->from($this->_table_name);
407 $row = $this->get($item_id, TRUE);
408
409 if($method == 'view') {
410 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 )
411 return true;
412 } else {
413 if(wmvc_show_data('user_id_receiver', $row) == $user_id || wmvc_show_data('user_id_editor', $row) == $user_id )
414 return true;
415 }
416
417 if(wmvc_user_in_role('administrator')) return true;
418
419 return false;
420 }
421
422 public function do_notified_by_user($user_id = NULL)
423 {
424 if(empty($user_id))
425 $user_id = get_current_user_id();
426
427 global $wpdb;
428 if($user_id){
429 $wpdb->query("UPDATE `{$this->_table_name}`
430 LEFT JOIN `{$this->db->prefix}wdk_listings` ON `{$this->db->prefix}wdk_listings`.post_id = `{$this->_table_name}`.post_id
431 SET `is_notified` = 1
432 WHERE
433 (`user_id_editor` = {$user_id}
434 OR `user_id_sender` = {$user_id})
435 ");
436 return true;
437 }
438
439 return false;
440 }
441
442 public function delete($id, $user_id=NULL) {
443
444 if($user_id === NULL)
445 $user_id = get_current_user_id();
446
447 if(!$this->check_deletable($id, $user_id)) return false;
448
449 /* remove */
450 parent::delete($id);
451
452 return true;
453 }
454
455 }
456
457 /*
458
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
462
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 )
466
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;
471
472 */
473 ?>