1<?php
2/**
3 * @author  Ben Chavet <ben@horde.org>
4 * @package Trean
5 */
6class Trean_Bookmark
7{
8    public $id = null;
9    public $userId = null;
10    public $url = null;
11    public $title = '';
12    public $description = '';
13    public $clicks = 0;
14    public $http_status = null;
15    public $favicon_url;
16    public $dt;
17    public $tags = array();
18
19    /**
20     */
21    public function __construct($bookmark = array())
22    {
23        if ($bookmark) {
24            $this->userId = $bookmark['user_id'];
25            $this->url = $bookmark['bookmark_url'];
26            $this->title = $bookmark['bookmark_title'];
27            $this->description = $bookmark['bookmark_description'];
28
29            if (!empty($bookmark['bookmark_id'])) {
30                $this->id = (int)$bookmark['bookmark_id'];
31            }
32            if (!empty($bookmark['bookmark_clicks'])) {
33                $this->clicks = (int)$bookmark['bookmark_clicks'];
34            }
35            if (!empty($bookmark['bookmark_http_status'])) {
36                $this->http_status = $bookmark['bookmark_http_status'];
37            }
38            if (!empty($bookmark['favicon_url'])) {
39                $this->favicon_url = $bookmark['favicon_url'];
40            }
41            if (!empty($bookmark['bookmark_dt'])) {
42                $this->dt = $bookmark['bookmark_dt'];
43            }
44
45            if (!empty($bookmark['bookmark_tags'])) {
46                $this->tags = $bookmark['bookmark_tags'];
47            }
48        }
49    }
50
51    /**
52     * Save bookmark.
53     */
54    public function save($crawl = true)
55    {
56        if (!strlen($this->url)) {
57            throw new Trean_Exception('Incomplete bookmark');
58        }
59
60        $charset = $GLOBALS['trean_db']->getOption('charset');
61        $c_url = Horde_String::convertCharset($this->url, 'UTF-8', $charset);
62        $c_title = Horde_String::convertCharset($this->title, 'UTF-8', $charset);
63        $c_description = Horde_String::convertCharset($this->description, 'UTF-8', $charset);
64        $c_favicon_url = Horde_String::convertCharset($this->favicon_url, 'UTF-8', $charset);
65
66        if ($this->id) {
67            // Update an existing bookmark.
68            $GLOBALS['trean_db']->update('
69                UPDATE trean_bookmarks
70                SET user_id = ?,
71                    bookmark_url = ?,
72                    bookmark_title = ?,
73                    bookmark_description = ?,
74                    bookmark_clicks = ?,
75                    bookmark_http_status = ?,
76                    favicon_url = ?
77                WHERE bookmark_id = ?',
78                array(
79                    $this->userId,
80                    $c_url,
81                    $c_title,
82                    $c_description,
83                    $this->clicks,
84                    $this->http_status,
85                    $c_favicon_url,
86                    $this->id,
87            ));
88
89            $GLOBALS['injector']->getInstance('Trean_Tagger')->replaceTags((string)$this->id, $this->tags, $GLOBALS['registry']->getAuth(), 'bookmark');
90        } else {
91            // Saving a new bookmark.
92            $bookmark_id = $GLOBALS['trean_db']->insert('
93                INSERT INTO trean_bookmarks (
94                    user_id,
95                    bookmark_url,
96                    bookmark_title,
97                    bookmark_description,
98                    bookmark_clicks,
99                    bookmark_http_status,
100                    favicon_url,
101                    bookmark_dt
102                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
103                array(
104                    $this->userId,
105                    $c_url,
106                    $c_title,
107                    $c_description,
108                    $this->clicks,
109                    $this->http_status,
110                    $c_favicon_url,
111                    $this->dt,
112            ));
113
114            $this->id = (int)$bookmark_id;
115            $GLOBALS['injector']->getInstance('Trean_Tagger')->tag((string)$this->id, $this->tags, $GLOBALS['registry']->getAuth(), 'bookmark');
116        }
117
118        if ($crawl) {
119            try {
120                $queue = $GLOBALS['injector']->getInstance('Horde_Queue_Storage');
121                $queue->add(new Trean_Queue_Task_Crawl(
122                    $this->url,
123                    $this->title,
124                    $this->description,
125                    $this->id,
126                    $this->userId
127                ));
128            } catch (Exception $e) {
129                Horde::log($e, 'INFO');
130            }
131        }
132
133        return $this->id;
134    }
135}
136