1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class UBMessage extends UBItem {
19: 20: 21:
22: const SUBTYPE = "UBMessage";
23: const REL_MESSAGE_DESTINATION = "UBMessage-destination";
24: const REL_MESSAGE_FILE = "UBMessage-UBFile";
25: const REL_MESSAGE_USER = "UBMessage-UBUser";
26:
27: public $destination = 0;
28: public $file_array = array();
29: public $read_array = array();
30:
31: 32: 33: 34: 35:
36: protected function copy_from_elgg($elgg_entity) {
37: parent::copy_from_elgg($elgg_entity);
38:
39: $this->read_array = (array)static::get_read_array($this->id);
40: $this->destination = (int)static::get_destination($this->id);
41: $this->file_array = (array)static::get_files($this->id);
42: }
43:
44: 45: 46: 47: 48:
49: protected function copy_to_elgg($elgg_entity) {
50: parent::copy_to_elgg($elgg_entity);
51:
52: }
53:
54: 55: 56: 57: 58: 59: 60:
61: protected function save($double_save = false) {
62: parent::save($double_save);
63: static::set_destination($this->id, $this->destination);
64: static::add_files($this->id, $this->file_array);
65: static::set_read_array($this->id, $this->read_array);
66: return $this->id;
67: }
68:
69:
70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81:
82: static function get_by_destination($destination_array, $limit = 0, $offset = 0, $count_only = false, $order_by = "", $ascending = true) {
83: $message_array = array();
84: foreach ($destination_array as $destination_id) {
85: $item_array = UBCollection::get_items($destination_id, static::REL_MESSAGE_DESTINATION, true);
86: if($count_only) {
87: $message_array[$destination_id] = count($item_array);
88: continue;
89: }
90: $temp_array = array();
91: foreach ($item_array as $item_id) {
92: $temp_array[$item_id] = new static((int)$item_id);
93: }
94: if (empty($temp_array)) {
95: $message_array[$destination_id] = null;
96: } else {
97: if(!empty($order_by)) {
98: $args = array("order_by" => $order_by, "ascending" => $ascending);
99: uasort($temp_array, function ($i1, $i2) use ($args) {
100: if (!$i1 && !$i2) {
101: return 0;
102: }
103: if ($i1->$args["order_by"] == $i2->$args["order_by"]) {
104: return 0;
105: }
106: if ((bool)$args["ascending"]) {
107: if (!$i1) {
108: return 1;
109: }
110: if (!$i2) {
111: return -1;
112: }
113: return (strtolower($i1->$args["order_by"]) < strtolower($i2->$args["order_by"]) ? -1 : 1);
114: } else {
115: if (!$i1) {
116: return -1;
117: }
118: if (!$i2) {
119: return 1;
120: }
121: return (strtolower($i2->$args["order_by"]) < strtolower($i1->$args["order_by"]) ? -1 : 1);
122: }
123: });
124: }
125: $message_array[$destination_id] = $temp_array;
126: if (!empty($limit)) {
127: $message_array[$destination_id] = array_slice($message_array[$destination_id], $offset, $limit, true);
128: } else {
129: $message_array[$destination_id] = array_slice($message_array[$destination_id], $offset, null, true);
130: }
131: }
132: }
133: return $message_array;
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: static function get_by_sender($sender_array) {
144: return static::get_by_owner($sender_array);
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154: static function get_destination($id) {
155: $item_array = UBCollection::get_items($id, static::REL_MESSAGE_DESTINATION);
156: if (empty($item_array)) {
157: return 0;
158: }
159: return (int)array_pop($item_array);
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169:
170: static function set_destination($id, $destination_id) {
171: if ($destination_id > 0) {
172: UBCollection::remove_all_items($id, static::REL_MESSAGE_DESTINATION);
173: return UBCollection::add_items($id, array($destination_id), static::REL_MESSAGE_DESTINATION);
174: }
175: return false;
176: }
177:
178: 179: 180: 181: 182: 183: 184:
185: static function get_sender($id) {
186: $message = new static($id);
187: return $message->owner_id;
188: }
189:
190: 191: 192: 193: 194: 195: 196: 197:
198: static function add_files($id, $file_array) {
199: return UBCollection::add_items($id, $file_array, static::REL_MESSAGE_FILE, true);
200: }
201:
202: 203: 204: 205: 206: 207: 208: 209:
210: static function set_files($id, $file_array) {
211: return UBCollection::set_items($id, $file_array, static::REL_MESSAGE_FILE, true);
212: }
213:
214: 215: 216: 217: 218: 219: 220: 221:
222: static function remove_files($id, $file_array) {
223: return UBCollection::remove_items($id, $file_array, static::REL_MESSAGE_FILE);
224: }
225:
226: 227: 228: 229: 230: 231: 232:
233: static function get_files($id) {
234: return UBCollection::get_items($id, static::REL_MESSAGE_FILE);
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244:
245: static function add_read_array($id, $read_array) {
246: return UBCollection::add_items($id, $read_array, static::REL_MESSAGE_USER);
247: }
248:
249: 250: 251: 252: 253: 254: 255: 256:
257: static function set_read_array($id, $read_array) {
258: return UBCollection::set_items($id, $read_array, static::REL_MESSAGE_USER);
259: }
260:
261: 262: 263: 264: 265: 266: 267: 268:
269: static function remove_read_array($id, $read_array) {
270: return UBCollection::remove_items($id, $read_array, static::REL_MESSAGE_USER);
271: }
272:
273: 274: 275: 276: 277: 278: 279:
280: static function get_read_array($id) {
281: return UBCollection::get_items($id, static::REL_MESSAGE_USER);
282: }
283:
284: 285: 286: 287: 288: 289: 290: 291:
292: static function get_read_status($id, $user_array = null) {
293: $props = static::get_properties($id, array("read_array", "owner_id"));
294: $read_array = $props["read_array"];
295: $owner_id = $props["owner_id"];
296: if (!$user_array) {
297: return $read_array;
298: } else {
299: $return_array = array();
300: foreach ($user_array as $user_id) {
301: if ((int)$user_id == (int)$owner_id || in_array($user_id, $read_array)) {
302: $return_array[$user_id] = true;
303: } else {
304: $return_array[$user_id] = false;
305: }
306: }
307: return $return_array;
308: }
309: }
310:
311: 312: 313: 314: 315: 316: 317: 318: 319: 320:
321: static function set_read_status($id, $read_value, $user_array) {
322: $read_array = static::get_read_array($id);
323: $update_flag = false;
324: foreach ($user_array as $user_id) {
325: $index = array_search((int)$user_id, $read_array);
326: if ($read_value === true && $index === false) {
327: array_push($read_array, $user_id);
328: $update_flag = true;
329: } elseif ($read_value === false && $index !== false) {
330: array_splice($read_array, $index, 1);
331: $update_flag = true;
332: }
333: }
334: if ($update_flag) {
335: return static::set_read_array($id, $read_array);
336: } else {
337: return $id;
338: }
339: }
340:
341: 342: 343: 344: 345: 346: 347: 348:
349: static function count_by_destination($destination_array, $recursive = false) {
350: $count_array = array();
351: foreach ($destination_array as $dest_id) {
352: $count_array[$dest_id] = UBCollection::count_items($dest_id,
353: static::REL_MESSAGE_DESTINATION,
354: true,
355: $recursive);
356: }
357: return $count_array;
358: }
359:
360: 361: 362: 363: 364: 365: 366:
367: static function count_by_sender($sender_array) {
368: $message_array = static::get_by_sender($sender_array);
369: $count_array = array();
370: foreach ($sender_array as $sender_id) {
371: $count_array[$sender_id] = count((array)$message_array[$sender_id]);
372: }
373: return $count_array;
374: }
375:
376: static function unread_by_destination($destination_array, $user_id, $recursive = false) {
377: $destination_messages = static::get_by_destination($destination_array);
378: $count_array = array();
379: foreach ($destination_array as $destination_id) {
380: $message_array = $destination_messages[$destination_id];
381: if (!$message_array) {
382: $count_array[$destination_id] = 0;
383: continue;
384: }
385: $count = 0;
386: foreach ($message_array as $message) {
387: if (array_search((int)$user_id, $message->read_array) === false) {
388: $count++;
389: }
390: if ($recursive) {
391: $temp_count = static::unread_by_destination(array((int)$message->id), $user_id, $recursive);
392: $count += $temp_count[$message->id];
393: }
394: }
395: $count_array[$destination_id] = $count;
396: }
397: return $count_array;
398: }
399:
400: static function unread_by_sender($sender_array, $user_id) {
401: $sender_messages = static::get_by_sender($sender_array);
402: $count_array = array();
403: foreach ($sender_array as $sender_id) {
404: $count = 0;
405: if (!empty($sender_messages[$sender_id])) {
406: $message_array = $sender_messages[$sender_id];
407: foreach ($message_array as $message) {
408: if (array_search((int)$user_id, $message->read_array) === false) {
409: $count++;
410: }
411: }
412: }
413: $count_array[$sender_id] = $count;
414: }
415: return $count_array;
416: }
417: }