forked from SwiftDeal/eCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.message.php
More file actions
202 lines (174 loc) · 5.3 KB
/
Copy pathclass.message.php
File metadata and controls
202 lines (174 loc) · 5.3 KB
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
<?php
require_once($dir_model.'class.database.php');
/**
* Message
*/
class Message extends DatabaseObject
{
protected static $table_name = "messages";
public $id;
public $from_user_id;
public $to_user_id;
public $from_email;
public $to_email;
public $item_id;
public $message;
public $phone;
public $city;
public $created;
public $updated;
protected static $db_fields = array('id', 'from_user_id', 'to_user_id', 'from_email', 'to_email', 'item_id',
'message', 'phone', 'city', 'updated', 'created');
public static function make($post_id, $user_id="", $name="", $content="") {
if (!empty($post_id) && !empty($content)) {
$comment = new Comment();
$comment->post_id = (int)$post_id;
$comment->created = strftime("%Y-%m-%d %H:%M:%S", time()+1800);
$comment->user_id = $user_id;
$comment->name = $name;
$comment->content = $content;
return $comment;
} else {
return false;
}
}
public static function make_item($item_id, $user_id="", $name="", $content="") {
if (!empty($item_id) && !empty($content)) {
$comment = new Comment();
$comment->item_id = (int)$item_id;
$comment->created = strftime("%Y-%m-%d %H:%M:%S", time()+19800);
$comment->user_id = $user_id;
$comment->name = $name;
$comment->content = $content;
return $comment;
} else {
return false;
}
}
public static function find_messages_on($type="", $id=0) {
global $database;
$sql = "SELECT * FROM ". self::$table_name;
$sql .= " WHERE {$type}=".$database->escape_value($id);
$sql .= " ORDER BY created DESC";
return self::find_by_sql($sql);
}
public function send_notification() {
$item = Item::find_by_id('id', $this->item_id);
$item_title = str_replace(' ', '_', trim($item->title));
$user = User::find_by_id($this->to_user_id);
$to_name = $user->name;
$to = $user->email;
$subject = "Message on your Product at Swiftdeal";
$from = "Swiftdeal.in <info@swiftdeal.in>";
$message =<<<EMAILBODY
Hi, {$user->name}
A new Message has been received on your Item at Swiftdeal.in
Deal: https://swiftdeal.in/public/viewitem.php?item_id={$this->item_id}
At {$this->created} wrote:
{$this->message}
From :
{$this->from_email}
{$this->phone}
Sent Via,
SwiftDeal.in
Making Happy Deal
EMAILBODY;
$headers = "From: {$from}\n";
$headers .= "Reply-To: {$from}\n";
$headers .= "X-Mailer: PHP/".phpversion()."\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$result = mail($to, $subject, $message, $headers);
return $result;
}
//datbase object class file
public static function find_all() {
return self::find_by_sql("SELECT * FROM ".self::$table_name);
}
public static function find_by_id($id=0) {
$result_array = static::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id = {$id} LIMIT 1");
return !empty($result_array) ? array_shift($result_array) : false ;
}
public static function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set)) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}
private static function instantiate($record) {
$object = new self;
// $object->username = $record['username'];
// $object->firstname = $record['first_name'];
// $object->lastname = $record['last_name'];
foreach ($record as $attribute => $value) {
if ($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
private function has_attribute($attribute) {
$object_vars = $this->attributes();
return array_key_exists($attribute, $object_vars);
}
protected function attributes() {
$attributes = array();
foreach(self::$db_fields as $field) {
if(property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
protected function sanitized_attributes() {
global $database;
$clean_attributes = array();
foreach ($this->attributes() as $key => $value) {
$clean_attributes[$key] = $database->escape_value($value);
}
return $clean_attributes;
}
public function save() {
return isset($this->id) ? $this->update() : $this->create();
}
public function create() {
global $database;
$attributes = $this->sanitized_attributes();
$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
if ($database->query($sql)) {
$this->id = $database->insert_id();
return true;
} else {
return false;
}
}
public function update() {
global $database;
$attributes = $this->sanitized_attributes();
$attribute_pairs = array();
foreach ($attribute_pairs as $key => $value) {
$attribute_pairs[] = "{$key} = '{$value}'";
}
$sql = "UPDATE ".self::$table_name." SET ";
$sql .= join(", ", $attribute_pairs);
$sql .= " WHERE id = ".$database->escape_value($this->id);
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}
public function delete() {
global $database;
$sql = "DELETE FROM ".self::$table_name;
$sql .= " WHERE id = ".$database->escape_value($this->id);
$sql .= " LIMIT 1";
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}
}
?>