1 <?php
2
3 namespace wataridori\ChatworkSDK;
4
5 use wataridori\ChatworkSDK\Exception\ChatworkSDKException;
6
7 class ChatworkRoom extends ChatworkBase
8 {
9 public $room_id = '';
10 public $name = '';
11 public $type = '';
12 public $role = '';
13 public $sticky = '';
14 public $unread_num = '';
15 public $mention_num = '';
16 public $mytask_num = '';
17 public $message_num = '';
18 public $file_num = '';
19 public $task_num = '';
20 public $icon_path = '';
21 public $description = '';
22
23 protected $listMembers = [];
24
25 26 27 28 29
30 public function __construct($room)
31 {
32 $this->init($room);
33
34 parent::__construct($this->room_id);
35 $this->chatworkApi = new ChatworkApi();
36 }
37
38 39 40
41 public function init($room)
42 {
43 if (is_array($room)) {
44 foreach ($room as $key => $value) {
45 if (property_exists($this, $key)) {
46 $this->$key = $value;
47 }
48 }
49 } elseif (is_numeric($room)) {
50 $this->room_id = $room;
51 }
52 }
53
54 55 56
57 public function toArray()
58 {
59 return [
60 'room_id' => $this->room_id,
61 'name' => $this->name,
62 'type' => $this->type,
63 'role' => $this->role,
64 'sticky' => $this->sticky,
65 'unread_num' => $this->unread_num,
66 'mention_num' => $this->mention_num,
67 'mytask_num' => $this->mytask_num,
68 'message_num' => $this->message_num,
69 'file_num' => $this->file_num,
70 'task_num' => $this->task_num,
71 'icon_path' => $this->icon_path,
72 'description' => $this->description,
73 ];
74 }
75
76 77 78 79 80
81 public function get()
82 {
83 $room = $this->chatworkApi->getRoomById($this->room_id);
84 $this->init($room);
85 return $room;
86 }
87
88 89 90 91 92 93 94
95 public function updateInfo($params = [])
96 {
97 return $this->chatworkApi->updateRoomInfo($this->room_id, $params);
98 }
99
100 101 102 103 104
105 public function getMembers()
106 {
107 $members = [];
108 $results = $this->chatworkApi->getRoomMembersById($this->room_id);
109 foreach ($results as $result) {
110 $members[] = new ChatworkUser($result);
111 }
112
113 $this->listMembers = $members;
114
115 return $members;
116 }
117
118 119 120 121 122 123 124 125
126 public function updateMembers($members_admin_ids = [], $params = [])
127 {
128 return $this->chatworkApi->updateRoomMembers($this->room_id, $members_admin_ids, $params);
129 }
130
131 132 133 134 135 136 137
138 public function getMessages($force = false)
139 {
140 $messages = [];
141 $results = $this->chatworkApi->getRoomMessages($this->room_id, $force);
142 if ($results) {
143 foreach ($results as $result) {
144 $messages[] = new ChatworkMessage($result);
145 }
146 }
147
148 return $messages;
149 }
150
151 152 153 154 155
156 public function sendMessage($newMessage = null)
157 {
158 $message = $newMessage ? $newMessage : $this->message;
159 $this->chatworkApi->createRoomMessage($this->room_id, $message);
160 }
161
162 163 164 165 166 167 168 169 170 171 172
173 public function sendMessageToList($members, $sendMessage, $withName = true, $newLine = true, $usePicon = false)
174 {
175 $this->resetMessage();
176 foreach ($members as $member) {
177 if (!($member instanceof wataridori\ChatworkSDK\ChatworkUser)) {
178 $this->appendTo($member, $withName, $newLine, $usePicon);
179 } else {
180 throw new ChatworkSDKException('Invalid Members list');
181 }
182 }
183 $this->appendMessage($sendMessage);
184 $this->sendMessage();
185 }
186
187 188 189 190 191 192 193 194
195 public function sendMessageToAll($sendMessage, $withName = true, $newLine = true, $usePicon = false)
196 {
197 if (!$this->listMembers) {
198 $this->getMembers();
199 }
200
201 if ($this->listMembers) {
202 $this->sendMessageToList($this->listMembers, $sendMessage, $withName, $newLine, $usePicon);
203 }
204 }
205
206 207 208 209 210 211 212 213
214 public function buildReplyInRoom($chatworkMessage, $newLine = true)
215 {
216 return $this->buildReply($this->room_id, $chatworkMessage, $newLine);
217 }
218
219 220 221 222 223 224 225 226 227 228
229 public function appendReplyInRoom($chatworkMessage, $newLine = true)
230 {
231 return $this->appendReply($this->room_id, $chatworkMessage, $newLine);
232 }
233
234 235 236 237 238 239 240 241
242 public function reply($chatworkMessages, $msg, $newLine = true, $resetMessage = true)
243 {
244 if ($resetMessage) {
245 $this->resetMessage();
246 }
247 if ($chatworkMessages instanceof ChatworkMessage) {
248 $this->appendReplyInRoom($chatworkMessages, $newLine);
249 } else {
250 foreach ($chatworkMessages as $chatworkMessage) {
251 $this->appendReplyInRoom($chatworkMessage, $newLine);
252 }
253 }
254 $this->appendMessage($msg);
255 $this->sendMessage();
256 }
257 }
258