summaryrefslogtreecommitdiff
path: root/htdocs/src/Post.inc.php
blob: 34230b244df72801217e497d38ef1421bd8dbd8e (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php

/* Foursquare Community Site
 * 
 * Copyright (C) 2011 Foursquare Church.
 * 
 * Developers: Jesse Morgan <jmorgan@foursquarestaff.com>
 *
 */

require_once "base.inc.php";

class Post {
    private $info;
    private $indatabase = false;


    public function __construct($info=null) {
        $this->info = is_null($info) ? array() : $info;

        if ($info !== null and isset($info['id'])) {
            $this->indatabase = true;

        } else {
            $this->indatabase = false;
        }
    }

    public static function getById($id) {
        $where = "id='$id'";

        return Post::getPost($where);
    }

    public static function getBySecretId($secretid) {
        $where = "secretid='$secretid'";

        return Post::getPost($where);
    }

    private static function getPost($where) {
        $query = "SELECT *, UNIX_TIMESTAMP(created) AS createdts FROM post WHERE $where";

        $db = getDatabase();
        
        $row = $db->fetchAssocRow($query);

        if ($row) {
            $post = new Post();
            $post->info = $row;
            $post->indatabase = true;

            return $post;

        } else {
            return false;
        }
    }

    public function save() {
        $db = getDatabase();

        // Cleanup Info
        foreach ($this->info as $key=>$value) $info[$key] = addslashes($value);

        // Remove artifical fields.
        unset($info['createdts']);

        // Save or create?
        if ($this->indatabase) {
            try {
                $db->update('post', $info, "WHERE `id`='". $this->getId() ."'");
                return true;

            } catch (Cif_Database_Exception $e) {
                return false;
            }

        } else {
            // Creating... set special fields.
            $info['stage'] = 'verification';
            $info['secretid'] = uniqid();
            $info['created'] = date('Y-m-d H:i:s');
            
            try {
                $ret = $db->insert('post', $info);

                if ($ret) {
                    $this->info['id'] = $ret;
                    $this->info['stage'] = 'verification';
                    $this->info['secretid'] = $info['secretid'];
                }

                return true;

            } catch (Cif_Database_Exception $e) {
                return false;
            }
        }
    }

    public function getId() {
        return $this->info['id'];
    }

    public function getSecretId() {
        return $this->info['secretid'];
    }

    public function getName() {
        return htmlspecialchars($this->info['name']);
    }

    public function setName($value) {
        $this->info['name'] = $value;
    }

    public function getDescription() {
        return htmlspecialchars($this->info['description']);
    }

    public function setDescription($value) {
        $this->info['description'] = $value;
    }

    public function getStage() {
        return $this->info['stage'];
    }

    public function approve() {
        if ($this->getStage() == 'moderation') {
            $this->info['stage'] = 'approved';
            $this->sendAcceptance();
        }
    }

    public function verify() {
        if ($this->getStage() == 'verification') {
            $this->info['stage'] = 'moderation';
        }
    }

    public function reject($message='') {
        $this->info['stage'] = 'rejected';

        $this->sendRejection($message);
    }

    public function getCreated() {
        return $this->info['created'];
    }

    public function getEmail() {
        return $this->info['email'];
    }

    public function setEmail($value) {
        $this->info['email'] = $value;
    }

    public function setCategory($value) {
        $this->info['category_id'] = $value;
    }

    public function getAge() {
        $diff = time() - $this->info['createdts'];

        if ($diff < 60) {
            return floor($diff) ." seconds ago";

        } else if ($diff < 3600) {
            return floor($diff / 60) ." minutes ago";

        } else if ($diff < 86400) {
            return floor($diff / 3600) ." hours ago";

        } else if ($diff < 604800) {
            return floor($diff / 86400) ." days ago";

        } else {
            return floor($diff / 604800) . " weeks ago";
        }
    }

    public function getLocation() {
        return $this->info['location'];
    }

    public function addImage($file) {
        // TODO: Verify file type

        // TODO: Unique name for file.
        $newfile = $GLOBALS['CONFIG']['uploads'];

        if (move_uploaded_file($file, $newfile)) {
            return true;

        } else {
            return false;
        }
    }

    public function sendValidation() {
        $email = new Email($this->getEmail());

        $email->setSubject($GLOBAL['CONFIG']['sitetitle'] . " Email Validation");

        $url = $GLOBALS['CONFIG']['urlroot'] . '/validate.php?id=' . $this->getSecretId();
        
        $email->appendMessage("Please click on the link below to verify your email address.\n\n");
        $email->appendMessage($url);

        $email->send();
    }

    public function sendAcceptance() {
        $email = new Email($this->getEmail());

        $email->setSubject($GLOBAL['CONFIG']['sitetitle'] . " Posting Approved");

        $email->appendMessage("Your posting titled ". $this->getName()
            ." has been approved by our moderation team.\n\n");

        $url = $GLOBALS['CONFIG']['urlroot'] . '/postings/'
            . $this->getId() .'.html';
        $email->appendMessage("You can view your post at $url.");

        $email->send();
    }

    public function sendRejection($message='') {
        $email = new Email($this->getEmail());

        $email->setSubject($GLOBAL['CONFIG']['sitetitle'] . " Posting Rejected");

        $email->appendMessage("Your posting titled ". $this->getName()
            ." has been rejected by our moderation team.\n\n");

        if ($message != '') {
            $email->appendMessage("The moderator left the following comment\n");
            $email->appendMessage($message);
        }

        $email->send();
    }
}

?>