PluginProbe
WP Directory Kit / 1.1.6
WP Directory Kit v1.1.6
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.1.6, at application/models/Messages_m.php

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