]> git.mxchange.org Git - friendica.git/blob - object/Conversation.php
Merge branch 'master' of https://github.com/friendica/friendica into threaded_items
[friendica.git] / object / Conversation.php
1 <?php
2 if(class_exists('Conversation'))
3         return;
4
5 require_once('boot.php');
6 require_once('object/BaseObject.php');
7 require_once('object/Item.php');
8 require_once('include/text.php');
9
10 /**
11  * A list of threads
12  *
13  * We should think about making this a SPL Iterator
14  */
15 class Conversation extends BaseObject {
16         private $threads = array();
17         private $mode = null;
18         private $writeable = false;
19         private $profile_owner = 0;
20
21         public function __construct($mode) {
22                 $this->set_mode($mode);
23         }
24
25         /**
26          * Set the mode we'll be displayed on
27          */
28         private function set_mode($mode) {
29                 if($this->get_mode() == $mode)
30                         return;
31
32                 $a = $this->get_app();
33
34                 switch($mode) {
35                         case 'network':
36                         case 'notes':
37                                 $this->profile_owner = local_user();
38                                 $this->writeable = true;
39                                 break;
40                         case 'profile':
41                                 $this->profile_owner = $a->profile['profile_uid'];
42                                 $this->writeable = can_write_wall($a,$this->profile_owner);
43                                 break;
44                         case 'display':
45                                 $this->profile_owner = $a->profile['uid'];
46                                 $this->writeable = can_write_wall($a,$this->profile_owner);
47                                 break;
48                         default:
49                                 logger('[ERROR] Conversation::set_mode : Unhandled mode ('. $mode .').', LOGGER_DEBUG);
50                                 return false;
51                                 break;
52                 }
53                 $this->mode = $mode;
54         }
55
56         /**
57          * Get mode
58          */
59         public function get_mode() {
60                 return $this->mode;
61         }
62
63         /**
64          * Check if page is writeable
65          */
66         public function is_writeable() {
67                 return $this->writeable;
68         }
69
70         /**
71          * Get profile owner
72          */
73         public function get_profile_owner() {
74                 return $this->profile_owner;
75         }
76
77         /**
78          * Add a thread to the conversation
79          *
80          * Returns:
81          *              _ The inserted item on success
82          *              _ false on failure
83          */
84         public function add_thread($item) {
85                 $item_id = $item->get_id();
86                 if(!$item_id) {
87                         logger('[ERROR] Conversation::add_thread : Item has no ID!!', LOGGER_DEBUG);
88                         return false;
89                 }
90                 if($this->get_thread($item->get_id())) {
91                         logger('[WARN] Conversation::add_thread : Thread already exists ('. $item->get_id() .').', LOGGER_DEBUG);
92                         return false;
93                 }
94                 $item->set_conversation($this);
95                 $this->threads[] = $item;
96                 return end($this->threads);
97         }
98
99         /**
100          * Get data in a form usable by a conversation template
101          *
102          * We should find a way to avoid using those arguments (at least most of them)
103          *
104          * Returns:
105          *              _ The data requested on success
106          *              _ false on failure
107          */
108         public function get_template_data($alike, $dlike) {
109                 $result = array();
110
111                 foreach($this->threads as $item) {
112                         if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid'))
113                                 continue;
114                         $item_data = $item->get_template_data($alike, $dlike);
115                         if(!$item_data) {
116                                 logger('[ERROR] Conversation::get_template_data : Failed to get item template data ('. $item->get_id() .').', LOGGER_DEBUG);
117                                 return false;
118                         }
119                         $result[] = $item_data;
120                 }
121
122                 return $result;
123         }
124
125         /**
126          * Get a thread based on its item id
127          *
128          * Returns:
129          *              _ The found item on success
130          *              _ false on failure
131          */
132         private function get_thread($id) {
133                 foreach($this->threads as $item) {
134                         if($item->get_id() == $id)
135                                 return $item;
136                 }
137
138                 return false;
139         }
140 }
141 ?>