1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class ClipitChat extends UBMessage {
19: 20: 21:
22: const SUBTYPE = "ClipitChat";
23: const REL_MESSAGE_DESTINATION = "ClipitChat-destination";
24: const REL_MESSAGE_FILE = "ClipitChat-ClipitFile";
25: const REL_MESSAGE_USER = "ClipitChat-ClipitUser";
26:
27: public $archived_array = array();
28:
29: 30: 31: 32: 33:
34: protected function copy_from_elgg($elgg_entity) {
35: parent::copy_from_elgg($elgg_entity);
36: $this->archived_array = (array)$elgg_entity->get("archived_array");
37: }
38:
39: 40: 41: 42: 43:
44: protected function copy_to_elgg($elgg_entity) {
45: parent::copy_to_elgg($elgg_entity);
46: $elgg_entity->set("archived_array", (array)$this->archived_array);
47: }
48:
49: static function get_inbox($user_id) {
50: $inbox_array = array();
51: $incoming_messages = static::get_by_destination(array($user_id));
52: $incoming_messages = $incoming_messages[$user_id];
53: if($incoming_messages !== null) {
54: $sender_array = array();
55: foreach($incoming_messages as $message) {
56: $archived_status = static::get_archived_status($message->id, array($user_id));
57: $archived_status = (bool)array_pop($archived_status);
58: if($archived_status === false) {
59: if(array_search($message->owner_id, $sender_array) === false) {
60: $sender_array[] = $message->owner_id;
61: $inbox_array[$message->owner_id] = array($message);
62: } else {
63: array_push($inbox_array[$message->owner_id], $message);
64: }
65: }
66: }
67: }
68: return $inbox_array;
69: }
70:
71: static function get_inbox_count($user_id) {
72: $count = 0;
73: $inbox_array = static::get_inbox($user_id);
74: foreach($inbox_array as $owner_messages) {
75: $count += count($owner_messages);
76: }
77: return $count;
78: }
79:
80: static function get_inbox_unread($user_id) {
81: $count = 0;
82: $inbox_array = static::get_inbox($user_id);
83: foreach($inbox_array as $owner_messages) {
84: foreach($owner_messages as $message) {
85: $read_status = static::get_read_status($message->id, array($user_id));
86: $read_status = (bool)array_pop($read_status);
87: if(!$read_status) {
88: $count ++;
89: }
90: }
91: }
92: return $count;
93: }
94:
95: static function get_sent($user_id) {
96: $sent_messages = static::get_by_sender(array($user_id));
97: $sent_messages = array_pop($sent_messages);
98: $sent_array = array();
99: foreach($sent_messages as $message) {
100: $archived_status = static::get_archived_status($message->id, array($user_id));
101: $archived_status = (bool)array_pop($archived_status);
102: if($archived_status === false) {
103: $sent_array[] = $message;
104: }
105: }
106: return $sent_array;
107: }
108:
109: static function get_sent_count($user_id) {
110: $count = 0;
111: $sent_messages = static::get_by_sender(array($user_id));
112: $sent_messages = array_pop($sent_messages);
113: foreach($sent_messages as $message) {
114: $archived_status = static::get_archived_status($message->id, array($user_id));
115: $archived_status = (bool)array_pop($archived_status);
116: if($archived_status === false) {
117: $count ++;
118: }
119: }
120: return $count;
121: }
122:
123: static function get_archived($user_id) {
124: $incoming_messages = static::get_by_destination(array($user_id));
125: $incoming_messages = array_pop($incoming_messages);
126: $sent_messages = static::get_by_sender(array($user_id));
127: $sent_messages = array_pop($sent_messages);
128: $archived_array = array();
129: foreach($incoming_messages as $message) {
130: $archived_status = static::get_archived_status($message->id, array($user_id));
131: $archived_status = (bool)array_pop($archived_status);
132: if($archived_status === true) {
133: array_push($archived_array, $message);
134: }
135: }
136: foreach($sent_messages as $message) {
137: $archived_status = static::get_archived_status($message->id, array($user_id));
138: $archived_status = (bool)array_pop($archived_status);
139: if($archived_status === true) {
140: array_push($archived_array, $message);
141: }
142: }
143: return $archived_array;
144: }
145:
146: static function get_archived_count($user_id) {
147: $count = 0;
148: $archived_array = static::get_archived($user_id);
149: foreach($archived_array as $owner_messages) {
150: $count += count($owner_messages);
151: }
152: return $count;
153: }
154:
155: static function get_conversation($user1_id, $user2_id) {
156: $conversation = array();
157:
158: $sender_messages = static::get_by_sender(array($user1_id));
159: $sender_messages = $sender_messages[$user1_id];
160: if(!empty($sender_messages)) {
161: foreach ($sender_messages as $message) {
162: if ($message->destination == (int)$user2_id) {
163: $archived = static::get_archived_status($message->id, array($user1_id));
164: if (!$archived[$user1_id]) {
165: $conversation[$message->id] = $message;
166: }
167: }
168: }
169: }
170:
171: $sender_messages = static::get_by_sender(array($user2_id));
172: $sender_messages = $sender_messages[$user2_id];
173: if(!empty($sender_messages)) {
174: foreach ($sender_messages as $message) {
175: if ($message->destination == (int)$user1_id) {
176: $archived = static::get_archived_status($message->id, array($user1_id));
177: if (!$archived[$user1_id]) {
178: $conversation[$message->id] = $message;
179: }
180: }
181: }
182: }
183: uasort($conversation, 'UBItem::sort_by_date');
184: return $conversation;
185: }
186:
187: static function get_conversation_count($user1_id, $user2_id) {
188: $count = 0;
189:
190: $sender_messages = static::get_by_sender(array($user1_id));
191: $sender_messages = $sender_messages[$user1_id];
192: foreach($sender_messages as $message) {
193: if($message->destination == (int)$user2_id) {
194: $archived = static::get_archived_status($message->id, array($user1_id));
195: if(!$archived[$user1_id]) {
196: $count ++;
197: }
198: }
199: }
200:
201: $sender_messages = static::get_by_sender(array($user2_id));
202: $sender_messages = $sender_messages[$user2_id];
203: foreach($sender_messages as $message) {
204: if($message->destination == (int)$user1_id) {
205: $archived = static::get_archived_status($message->id, array($user1_id));
206: if(!$archived[$user1_id]) {
207: $count ++;
208: }
209: }
210: }
211: return $count;
212: }
213:
214: static function get_conversation_unread($user1_id, $user2_id) {
215: $count = 0;
216:
217: $sender_messages = static::get_by_sender(array($user1_id));
218: $sender_messages = $sender_messages[$user1_id];
219: foreach($sender_messages as $message) {
220: if($message->destination == (int)$user2_id) {
221: $archived = static::get_archived_status($message->id, array($user1_id));
222: $read = static::get_read_status($message->id, array($user1_id));
223: if(!$archived[$user1_id] && !$read[$user1_id]) {
224: $count ++;
225: }
226: }
227: }
228:
229: $sender_messages = static::get_by_sender(array($user2_id));
230: $sender_messages = $sender_messages[$user2_id];
231: foreach($sender_messages as $message) {
232: if($message->destination == (int)$user1_id) {
233: $archived = static::get_archived_status($message->id, array($user1_id));
234: $read = static::get_read_status($message->id, array($user1_id));
235: if(!$archived[$user1_id] && !$read[$user1_id]) {
236: $count ++;
237: }
238: }
239: }
240: return $count;
241: }
242:
243: static function get_archived_status($id, $user_array = null) {
244: $archived_array = static::get_properties($id, array("archived_array"));
245: $archived_array = array_pop($archived_array);
246: if(!$user_array) {
247: return $archived_array;
248: } else {
249: $return_array = array();
250: foreach($user_array as $user_id) {
251: if(in_array($user_id, $archived_array)) {
252: $return_array[$user_id] = true;
253: } else {
254: $return_array[$user_id] = false;
255: }
256: }
257: return $return_array;
258: }
259: }
260:
261: static function set_archived_status($id, $archived_value, $user_array) {
262: $archived_array = static::get_properties($id, array("archived_array"));
263: $archived_array = array_pop($archived_array);
264: foreach($user_array as $user_id) {
265: if($archived_value == true) {
266: if(!in_array($user_id, $archived_array)) {
267: array_push($archived_array, $user_id);
268: }
269: } else if($archived_value == false) {
270: $index = array_search((int)$user_id, $archived_array);
271: if(isset($index) && $index !== false) {
272: array_splice($archived_array, $index, 1);
273: }
274: }
275: }
276: $prop_value_array["archived_array"] = $archived_array;
277: return static::set_properties($id, $prop_value_array);
278: }
279: }