/
Message.php
213 lines (161 loc) · 6.06 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
class Push_Model_Message extends Core_Model_Default {
const DISPLAYED_PER_PAGE = 10;
protected $_is_cachable = false;
protected $_types = array(
'iphone' => 'Push_Model_Iphone_Message',
'android' => 'Push_Model_Android_Message'
);
protected $_instances;
protected $_messageType;
public function __construct($datas = array()) {
parent::__construct($datas);
$this->_db_table = 'Push_Model_Db_Table_Message';
$this->_initInstances();
$this->_initMessageType();
}
public function delete() {
$message_id = $this->getId();
parent::delete();
$this->getTable()->deleteLog($message_id);
}
public function deleteFeature($option_value) {
$app = $this->getApplication();
$this->setMessageTypeByOptionValue($option_value);
$this->getTable()->deleteAllLogs($app->getId(),$this->getMessageType());
$this->getTable()->deleteAllMessages($app->getId(),$this->getMessageType());
}
public function getInstance($type = null) {
if(!empty($this->_instances[$type])) return $this->_instances[$type];
else return null;
}
public function getInstances() {
return $this->_instances;
}
public function getMessageType() {
return $this->_messageType;
}
public function getMessages() {
return $this->getTable()->getMessages($this->_messageType);
}
public function getTitle() {
return mb_convert_encoding($this->getData('title'), 'UTF-8', 'UTF-8');
}
public function getText() {
return mb_convert_encoding($this->getData('text'), 'UTF-8', 'UTF-8');
}
public function markAsRead($device_uid,$message_id = null) {
return $this->getTable()->markAsRead($device_uid,$message_id);
}
public function markAsDisplayed($device_id, $message_id) {
return $this->getTable()->markAsDisplayed($device_id, $message_id);
}
public function findByDeviceId($device_id,$offset=0) {
$allowed_categories = null;
if($this->_messageType==2) {
$subscription = new Topic_Model_Subscription();
$allowed_categories = $subscription->findAllowedCategories($device_id);
}
return $this->getTable()->findByDeviceId($device_id,$this->_messageType,$offset,$allowed_categories);
}
public function countByDeviceId($device_id) {
return $this->getTable()->countByDeviceId($device_id,$this->_messageType);
}
public function findLastPushMessage($device_id) {
$row = $this->getTable()->findLastPushMessage($device_id);
$this->_prepareDatas($row);
return $this;
}
public function findLastInAppMessage($app_id,$device_id) {
$subscription = new Topic_Model_Subscription();
$allowed_categories = $subscription->findAllowedCategories($device_id);
$row = $this->getTable()->findLastInAppMessage($app_id,$device_id,$allowed_categories);
$this->_prepareDatas($row);
return $this;
}
public function markInAppAsRead($app_id, $device_id, $device_type) {
return $this->getTable()->markInAppAsRead($app_id, $device_id, $device_type);
}
public function push() {
$errors = array();
$this->updateStatus('sending');
foreach($this->_instances as $type => $instance) {
$instance->setMessage($this)
->push()
;
if($instance->getErrors()) {
$errors[$instance->getId()] = $instance->getErrors();
}
}
$this->updateStatus('delivered');
if(!empty($errors)) {
Zend_Registry::get("logger")->sendException(print_r($errors, true), "push_", false);
}
$this->setErrors($errors);
}
public function createLog($device, $status, $id = null) {
if(!$id) $id = $device->getDeviceUid();
$is_displayed = 0;
// $is_displayed = $device->getTypeId() == 1 ? 1 : 0;
// if(!$is_displayed) {
// $is_displayed = !$this->getLatitude() && !$this->getLongitude();
// }
$datas = array(
'device_id' => $device->getId(),
'device_uid' => $id,
'device_type' => $device->getTypeId(),
'is_displayed' => $is_displayed,
'message_id' => $this->getId(),
'status' => $status,
'delivered_at' => $this->formatDate(null, 'y-MM-dd HH:mm:ss')
);
$this->getTable()->createLog($datas);
return $this;
}
public function updateStatus($status) {
$this->setStatus($status);
if($status == 'delivered') {
$this->setDeliveredAt($this->formatDate(null, 'y-MM-dd HH:mm:ss'));
}
$this->save();
}
public function setMessageType($message_type) {
$this->_messageType = $message_type;
return $this;
}
public function setMessageTypeByOptionValue($optionValue) {
$inapp_option_id = $this->getTable()->getInAppCode();
switch($optionValue) {
case $inapp_option_id:
$this->_messageType = 2;
break;
default:
$this->_messageType = 1;
}
}
public function getCoverUrl() {
$cover_path = Application_Model_Application::getImagePath().$this->getCover();
$base_cover_path = Application_Model_Application::getBaseImagePath().$this->getCover();
if($this->getCover() AND file_exists($base_cover_path)) {
return $cover_path;
}
return '';
}
public function getInAppCode() {
return $this->getTable()->getInAppCode();
}
protected function _initInstances() {
if(is_null($this->_instances)) {
$this->_instances = array();
foreach($this->_types as $device => $type) {
$this->_instances[$device] = new $type();
}
}
return $this->_instances;
}
public function _initMessageType() {
if (is_null($this->_messageType)) {
$this->_messageType = 1;
}
}
}