Skip to content

Commit

Permalink
PHPDOC, clean up exception catching/handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Jul 6, 2014
1 parent 633cfbf commit 187bd5c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 17 deletions.
2 changes: 1 addition & 1 deletion trean/app/controllers/DeleteBookmark.php
Expand Up @@ -12,7 +12,7 @@ public function processRequest(Horde_Controller_Request $request, Horde_Controll
$gateway->removeBookmark($bookmark);
$notification->push(_("Deleted bookmark: ") . $bookmark->title, 'horde.success');
$result = array('data' => 'deleted');
} catch (Trean_Exception $e) {
} catch (Horde_Exception $e) {
$notification->push(sprintf(_("There was a problem deleting the bookmark: %s"), $e->getMessage()), 'horde.error');
$result = array('error' => $e->getMessage());
}
Expand Down
3 changes: 1 addition & 2 deletions trean/app/controllers/SaveBookmark.php
Expand Up @@ -9,7 +9,6 @@ public function processRequest(Horde_Controller_Request $request, Horde_Controll

try {
$bookmark = $gateway->getBookmark($id);

$old_url = $bookmark->url;
$bookmark->url = Horde_Util::getFormData('bookmark_url');
$bookmark->title = Horde_Util::getFormData('bookmark_title');
Expand All @@ -22,7 +21,7 @@ public function processRequest(Horde_Controller_Request $request, Horde_Controll

$bookmark->save();
$result = array('data' => 'saved');
} catch (Trean_Exception $e) {
} catch (Horde_Exception $e) {
$notification->push(sprintf(_("There was an error saving the bookmark: %s"), $e->getMessage()), 'horde.error');
$result = array('error' => $e->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion trean/edit.php
Expand Up @@ -15,7 +15,7 @@
$bookmark_id = Horde_Util::getFormData('bookmark');
try {
$bookmark = $trean_gateway->getBookmark($bookmark_id);
} catch (Trean_Exception $e) {
} catch (Horde_Exception_NotFound $e) {
$notification->push(sprintf(_("Bookmark not found: %s."), $e->getMessage()), 'horde.message');
Horde::url('browse.php', true)->redirect();
}
Expand Down
6 changes: 5 additions & 1 deletion trean/favicon.php
Expand Up @@ -16,7 +16,11 @@
exit;
}

$bookmark = $trean_gateway->getBookmark($bookmark_id);
try {
$bookmark = $trean_gateway->getBookmark($bookmark_id);
} catch (Horde_Exception $e) {
exit;
}
if (!$bookmark || !$bookmark->favicon_url) {
exit;
}
Expand Down
80 changes: 69 additions & 11 deletions trean/lib/Bookmarks.php
Expand Up @@ -6,6 +6,18 @@
* did not receive this file, see http://www.horde.org/licenses/bsdl.php.
*
* @author Ben Chavet <ben@horde.org>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @license http://www.horde.org/licenses/bsdl.php
* @package Trean
*/
/**
* Trean_Bookmarks:: Handles basic management of bookmark storage.
*
* @author Ben Chavet <ben@horde.org>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @license http://www.horde.org/licenses/bsdl.php
* @package Trean
*/
class Trean_Bookmarks
Expand All @@ -22,6 +34,8 @@ class Trean_Bookmarks

/**
* Constructor.
*
* @param Content_Users_Manager A user manager object.
*/
public function __construct(Content_Users_Manager $userManager)
{
Expand All @@ -34,7 +48,10 @@ public function __construct(Content_Users_Manager $userManager)
}

/**
* Create a new bookmark for the current user
* Create a new bookmark for the current user.
*
* @param array $properties The bookmark property array.
* @param boolean $crawl If true (default) attempt to crawl the URL.
*
* @return Trean_Bookmark
*/
Expand All @@ -50,7 +67,14 @@ public function newBookmark(array $properties, $crawl = true)
}

/**
* Search bookmarks.
* List bookmarks, sorted and paged as specified.
*
* @param string $sortyby Field to sort by.
* @param integer $sortdir Direction of sort.
* @param integer $from Starting bookmark.
* @param integer $to Ending bookmark.
*
* @return array An array of Trean_Bookmark objects.
*/
public function listBookmarks($sortby = 'title', $sortdir = 0, $from = 0, $count = 0)
{
Expand All @@ -67,6 +91,11 @@ public function listBookmarks($sortby = 'title', $sortdir = 0, $from = 0, $count

/**
* Search bookmarks.
*
* @param string $q The search text.
*
* @return array An array of Trean_Bookmark objects that match the search.
* @throws Trean_Exception
*/
public function searchBookmarks($q)
{
Expand Down Expand Up @@ -96,15 +125,25 @@ public function searchBookmarks($q)
* Returns the number of bookmarks.
*
* @return integer The number of all bookmarks.
* @throws Trean_Exception
*/
public function countBookmarks()
{
$sql = 'SELECT COUNT(*) FROM trean_bookmarks WHERE user_id = ?';
return $GLOBALS['trean_db']->selectValue($sql, array($this->_userId));
try {
return $GLOBALS['trean_db']->selectValue($sql, array($this->_userId));
} catch (Horde_Db_Exception $e) {
throw new Trean_Exception($e);
}
}

/**
* Return counts on grouping bookmarks by a specific property.
*
* @param string $groupby The field to group on. (i.e., 'status').
*
* @return array A hash of results.
* @throws Trean_Exception
*/
public function groupBookmarks($groupby)
{
Expand All @@ -119,7 +158,11 @@ public function groupBookmarks($groupby)
return array();
}

return $GLOBALS['trean_db']->selectAll($sql);
try {
return $GLOBALS['trean_db']->selectAll($sql);
} catch (Horde_Db_Exception $e) {
throw new Trean_Exception($e);
}
}

/**
Expand All @@ -128,15 +171,20 @@ public function groupBookmarks($groupby)
* @param integer $id The ID of the bookmark to retrieve.
*
* @return Trean_Bookmark The bookmark object corresponding to the given name.
* @throws Horde_Exception_NotFound, Trean_Exception
*/
public function getBookmark($id)
{
$bookmark = $GLOBALS['trean_db']->selectOne('
SELECT bookmark_id, user_id, bookmark_url, bookmark_title, bookmark_description, bookmark_clicks, bookmark_http_status, favicon_url, bookmark_dt
FROM trean_bookmarks
WHERE bookmark_id = ' . (int)$id);
try {
$bookmark = $GLOBALS['trean_db']->selectOne('
SELECT bookmark_id, user_id, bookmark_url, bookmark_title, bookmark_description, bookmark_clicks, bookmark_http_status, favicon_url, bookmark_dt
FROM trean_bookmarks
WHERE bookmark_id = ' . (int)$id);
} catch (Horde_Db_Exception $e) {
throw new Trean_Exception($e);
}
if (is_null($bookmark)) {
throw new Trean_Exception('not found');
throw new Horde_Exception_NotFound();
}

$bookmark = $this->_resultSet(array($bookmark));
Expand All @@ -147,12 +195,13 @@ public function getBookmark($id)
* Removes a Trean_Bookmark from the backend.
*
* @param Trean_Bookmark $bookmark The bookmark to remove.
* @throws Horde_Exception_PermissionDenied, Trean_Exception
*/
public function removeBookmark(Trean_Bookmark $bookmark)
{
/* Check permissions. */
if ($bookmark->userId != $this->_userId) {
throw new Trean_Exception('permission denied');
throw new Horde_Exception_PermissionDenied();
}

/* Untag */
Expand All @@ -163,13 +212,21 @@ public function removeBookmark(Trean_Bookmark $bookmark)
//$indexer->index('horde-user-' . $this->_userId, 'trean-bookmark', $this->_bookmarkId, json_encode(array(

/* Delete from SQL. */
$GLOBALS['trean_db']->delete('DELETE FROM trean_bookmarks WHERE bookmark_id = ' . (int)$bookmark->id);
try {
$GLOBALS['trean_db']->delete('DELETE FROM trean_bookmarks WHERE bookmark_id = ' . (int)$bookmark->id);
} catch (Horde_Db_Exception $e) {
throw new Trean_Exception($e);
}

return true;
}

/**
* Creates Trean_Bookmark objects for each row in a SQL result.
*
* @param array $bookmarks An array of query results.
*
* @return array An array of Trean_Bookmark objects.
*/
protected function _resultSet($bookmarks)
{
Expand All @@ -194,4 +251,5 @@ protected function _resultSet($bookmarks)

return $objects;
}

}
2 changes: 1 addition & 1 deletion trean/lib/TagBrowser.php
Expand Up @@ -47,7 +47,7 @@ public function getSlice($page = 0, $perpage = null)
$bookmarks[] = $injector
->getInstance('Trean_Bookmarks')
->getBookmark($id);
} catch (Trean_Exception $e) {
} catch (Horde_Exception_NotFound $e) {
Horde::log('Bookmark not found: ' . $id, 'ERR');
}
}
Expand Down

0 comments on commit 187bd5c

Please sign in to comment.