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