]> git.mxchange.org Git - friendica.git/blob - object/Conversation.php
Merge branch 'threaded_items' of github.com:CyberDomovoy/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
95                 /*
96                  * Only add will be displayed
97                  */
98                 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
99                         logger('[WARN] Conversation::add_thread : Thread is a mail ('. $item->get_id() .').', LOGGER_DEBUG);
100                         return false;
101                 }
102                 if($item->get_data_value('verb') === ACTIVITY_LIKE || $item->get_data_value('verb') === ACTIVITY_DISLIKE) {
103                         logger('[WARN] Conversation::add_thread : Thread is a (dis)like ('. $item->get_id() .').', LOGGER_DEBUG);
104                         return false;
105                 }
106                 $item->set_conversation($this);
107                 $this->threads[] = $item;
108                 return end($this->threads);
109         }
110
111         /**
112          * Get data in a form usable by a conversation template
113          *
114          * We should find a way to avoid using those arguments (at least most of them)
115          *
116          * Returns:
117          *              _ The data requested on success
118          *              _ false on failure
119          */
120         public function get_template_data($alike, $dlike) {
121                 $result = array();
122
123                 foreach($this->threads as $item) {
124                         if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid'))
125                                 continue;
126                         $item_data = $item->get_template_data($alike, $dlike);
127                         if(!$item_data) {
128                                 logger('[ERROR] Conversation::get_template_data : Failed to get item template data ('. $item->get_id() .').', LOGGER_DEBUG);
129                                 return false;
130                         }
131                         $result[] = $item_data;
132                 }
133
134                 return $result;
135         }
136
137         /**
138          * Get a thread based on its item id
139          *
140          * Returns:
141          *              _ The found item on success
142          *              _ false on failure
143          */
144         private function get_thread($id) {
145                 foreach($this->threads as $item) {
146                         if($item->get_id() == $id)
147                                 return $item;
148                 }
149
150                 return false;
151         }
152 }
153 ?>