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