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