diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b20020e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +language: php +php: + - 5.3.3 + - 5.4 +branches: + only: + - 7.x +env: + - FEDORA_VERSION="3.5" + - FEDORA_VERSION="3.6.2" +before_install: + - cd $HOME + - git clone -b 7.x git://github.com/Islandora/islandora.git + - git clone -b 7.x git://github.com/Islandora/islandora_solr_search.git + - export ISLANDORA_DIR=$HOME/islandora + - $HOME/islandora/tests/scripts/travis_setup.sh + - cd $HOME/drupal-* + - ln -s $TRAVIS_BUILD_DIR sites/all/modules/islandora_bookmark + - ln -s $HOME/islandora_solr_search sites/all/modules/islandora_solr_search + - drush en --yes islandora_bookmark +script: + - ant -buildfile sites/all/modules/islandora_bookmark/build.xml lint + - $ISLANDORA_DIR/tests/scripts/line_endings.sh sites/all/modules/islandora_bookmark + - drush coder-review --reviews=production,security,style,i18n,potx,sniffer islandora_bookmark + - phpcpd --names *.module,*.inc,*.test sites/all/modules/islandora_bookmark + - drush test-run --uri=http://localhost:8081 "Islandora Bookmark" \ No newline at end of file diff --git a/README.txt b/README.md similarity index 88% rename from README.txt rename to README.md index 0b3888f..8615df0 100644 --- a/README.txt +++ b/README.md @@ -1,3 +1,11 @@ +BUILD STATUS +------------ +Current build status: +[![Build Status](https://travis-ci.org/Islandora/islandora_bookmark.png?branch=7.x)](https://travis-ci.org/Islandora/islandora_bookmark) + +CI Server: +http://jenkins.discoverygarden.ca + ISLANDORA BOOKMARK ================== diff --git a/css/islandora_bookmark.css b/css/islandora_bookmark.css index 74748d6..39832d9 100644 --- a/css/islandora_bookmark.css +++ b/css/islandora_bookmark.css @@ -3,46 +3,54 @@ * Styles for Islandora Bookmark. */ div.islandora-bookmark-users div.form-item { - width: 200px; - display: inline-block; - display: -moz-inline-block; - margin-right: 10px; - *display:inline; - *zoom:1; - *vertical-align: 2%; + width: 200px; + display: inline-block; + display: -moz-inline-block; + margin-right: 10px; + *display: inline; + *zoom: 1; + *vertical-align: 2%; } div.islandora-bookmark-listname div.form-item { - display: inline-block; - display: -moz-inline-block; - margin-right: 10px; - *display:inline; - *zoom:1; - *vertical-align: 2%; + display: inline-block; + display: -moz-inline-block; + margin-right: 10px; + *display: inline; + *zoom: 1; + *vertical-align: 2%; } div.islandora-bookmark-adduser div.form-item { display: inline-block; display: -moz-inline-block; margin-right: 10px; - *display:inline; - *zoom:1; + *display: inline; + *zoom: 1; *vertical-align: 2%; } div.islandora-bookmark-error-message { - background-image: linear-gradient(#EE5F5B, #C43C35); + background-image: linear-gradient(#ee5f5b, #c43c35); color: white; text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); } -#islandora-bookmark .form-item-add-bookmarks -{ +#islandora-bookmark .form-item-add-bookmarks { display: inline-block; margin-right: 10px; } -#islandora-bookmark-detailed-form table .operations -{ +#islandora-bookmark-detailed-form table .operations { width: 100px; -} \ No newline at end of file +} + +#islandora-bookmark-rss-format { + float: right; + display: block; + clear: both; +} + +#islandora-bookmark-detailed-list { + clear: both; +} diff --git a/images/rss.png b/images/rss.png new file mode 100644 index 0000000..6326a89 Binary files /dev/null and b/images/rss.png differ diff --git a/includes/admin.form.inc b/includes/admin.form.inc index 8cc68d4..f98b145 100644 --- a/includes/admin.form.inc +++ b/includes/admin.form.inc @@ -18,6 +18,11 @@ */ function islandora_bookmark_admin_settings(array $form, array &$form_state) { $form = array( + 'list_type' => array( + '#type' => 'textfield', + '#title' => t('The name of the type of list.'), + '#default_value' => variable_get('islandora_bookmark_type', 'bookmark'), + ), 'overview_page_elements' => array( '#type' => 'textfield', '#title' => t('Bookmarks overview table'), @@ -62,11 +67,13 @@ function islandora_bookmark_admin_settings_submit(array $form, array &$form_stat case 'edit-submit': variable_set('islandora_bookmark_overview_page_elements', $form_state['values']['overview_page_elements']); variable_set('islandora_bookmark_detailed_page_elements', $form_state['values']['detailed_page_elements']); + variable_set('islandora_bookmark_type', $form_state['values']['list_type']); break; case 'edit-reset': variable_del('islandora_bookmark_overview_page_elements'); variable_del('islandora_bookmark_detailed_page_elements'); + variable_del('islandora_bookmark_type'); break; } } diff --git a/includes/api.inc b/includes/api.inc index 18cb01b..de81896 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -16,64 +16,23 @@ function islandora_bookmark_get_user_owned_bookmarks() { global $user; - $listids = array(); - $bookmark_list = array(); - - $list_query = db_query('SELECT listid FROM {islandora_bookmark_list_names} WHERE listowner = :uid ORDER BY listid', array(':uid' => $user->uid)); - - foreach ($list_query as $record) { - if (!in_array($record->listid, $listids)) { - $listids[] = $record->listid; - } - } - - foreach ($listids as $key => $value) { - $listpids = array(); - $listusers = array(); - - $pid_query = db_query('SELECT pidid FROM {islandora_bookmark_list_pids} WHERE listid = :listid', array(':listid' => $value)); + $list_query = db_query('SELECT DISTINCT listid FROM {islandora_bookmark_list_names} WHERE listowner = :uid ORDER BY listid', array(':uid' => $user->uid)); + $listids = (array) $list_query->fetchAll(PDO::FETCH_COLUMN); - foreach ($pid_query as $record) { - $listpids[] = $record->pidid; - } - - $user_query = db_query('SELECT uid FROM {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $value)); - - foreach ($user_query as $record) { - $listusers[] = $record->uid; - } - - $list_record = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $value))->fetchObject(); - $listid = $value; - - $params = array( - 'retrieve' => TRUE, - 'user_id' => $user->uid, - 'bookmark_owner' => $list_record->listowner, - 'bookmark_users' => $listusers, - 'bookmark_pids' => $listpids, - 'bookmark_name' => $list_record->listname, - 'bookmark_id' => $listid, - ); - $templist = new Bookmark($params); + $bookmark_list = array(); + foreach ($listids as $list_id) { + $templist = BookmarkDatabase::getList($list_id); $bookmark_list[] = $templist; } // User is anon, use session as well. - if ($user->uid == 0 && !empty($_SESSION['islandora_bookmark'])) { - foreach ($_SESSION['islandora_bookmark'] as $value) { - if (isset($pid)) { - $templist = unserialize($value); - if (in_array($pid, $templist->pidList)) { - $bookmark_list[] = $templist; - } - } - else { - $bookmark_list[] = unserialize($value); - } + if (isset($_SESSION) && $user->uid == 0 && !empty($_SESSION['islandora_bookmark'])) { + foreach (array_keys($_SESSION['islandora_bookmark']) as $id) { + $bookmark_list[] = BookmarkSession::getList($id); } } + return $bookmark_list; } @@ -96,46 +55,18 @@ function islandora_bookmark_get_user_shared_bookmarks() { foreach ($owner_query as $record) { $owner_ids[] = $record->listid; } - $shared_query = db_query('SELECT listid FROM {islandora_bookmark_list_users} WHERE uid = :uid ORDER BY listid', array(':uid' => $user->uid)); + $shared_query = db_query('SELECT listid FROM {islandora_bookmark_list_users} WHERE uid = :uid OR uid = 0 ORDER BY listid', array(':uid' => $user->uid)); foreach ($shared_query as $record) { $shared_ids[] = $record->listid; } $shared_ids = array_diff($shared_ids, $owner_ids); - foreach ($shared_ids as $key => $value) { - - $listpids = array(); - $listusers = array(); - - $pid_query = db_query('SELECT pidid FROM {islandora_bookmark_list_pids} WHERE listid = :listid', array(':listid' => $value)); - - foreach ($pid_query as $record) { - $listpids[] = $record->pidid; - } - - $user_query = db_query('SELECT uid FROM {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $value)); - - foreach ($user_query as $record) { - $listusers[] = $record->uid; - } - - $list_record = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $value))->fetchObject(); - $listid = $value; - - $params = array( - 'retrieve' => TRUE, - 'user_id' => $user->uid, - 'bookmark_owner' => $list_record->listowner, - 'bookmark_users' => $listusers, - 'bookmark_pids' => $listpids, - 'bookmark_name' => $list_record->listname, - 'bookmark_id' => $listid, - ); - $templist = new Bookmark($params); + foreach ($shared_ids as $id) { + $templist = Bookmark::getList($id); $bookmark_list[] = $templist; } - return $bookmark_list; + return array_filter($bookmark_list); } /** @@ -146,46 +77,15 @@ function islandora_bookmark_get_user_shared_bookmarks() { * @param int $listid * List ID to be searched for. * - * @return PidList - * PidList object. + * @return bool|PidList + * PidList object or FALSE if the list does not exist. */ function islandora_bookmark_get_bookmark_by_number($listid) { - global $user; - - if (!preg_match('/session_/', $listid)) { - $listresult = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $listid))->fetchObject(); - - $userresult = db_query('SELECT uid from {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $listid)); - - $userlist = array(); - - foreach ($userresult as $record) { - array_push($userlist, $record->uid); - } - - $pidresult = db_query('SELECT pidid from {islandora_bookmark_list_pids} WHERE listid = :listid', array(':listid' => $listid)); - $pidlist = array(); - - foreach ($pidresult as $record) { - array_push($pidlist, $record->pidid); - } - - $params = array( - 'retrieve' => TRUE, - 'user_id' => $user->uid, - 'bookmark_owner' => $listresult->listowner, - 'bookmark_name' => $listresult->listname, - 'bookmark_id' => $listid, - 'bookmark_users' => $userlist, - 'bookmark_pids' => $pidlist, - ); - $templist = new Bookmark($params); - + $list = Bookmark::getList($listid); + if ($list === NULL) { + return FALSE; } - else { - $templist = unserialize($_SESSION['islandora_bookmark'][$listid]); - } - return $templist; + return $list; } /** @@ -204,18 +104,54 @@ function islandora_bookmark_get_bookmarks_visible_to_user($pid, $uid) { // SESSION FIRST. if ($uid == 0 && !empty($_SESSION['islandora_bookmark'])) { foreach ($_SESSION['islandora_bookmark'] as $value) { - $templist = unserialize($value); - if (in_array($pid, $templist->pidList)) { - $lists[$templist->bookmarkId] = $templist->bookmarkId; + $templist = $value; + if (in_array($pid, $templist->getPids())) { + $lists[$templist->getId()] = $templist->getId(); } } } else { // DB. - $list_result = db_query('SELECT u.listid from {islandora_bookmark_list_users} as u INNER JOIN {islandora_bookmark_list_pids} as p ON u.listid = p.listid WHERE uid = :uid AND pidid = :pid', array(':uid' => $uid, ':pid' => $pid)); - foreach ($list_result as $record) { - $lists[$record->listid] = $record->listid; - } + $q = << $uid, ':pid' => $pid)) + ->fetchAll(PDO::FETCH_COLUMN); + $lists += drupal_map_assoc($list_result); } return $lists; } + +/** + * Retrieves all the pids from the bookmark list id. + * + * @param string $value + * The listid. + * + * @return array + * An array containing listpids. + */ +function islandora_bookmark_list_pids_query($value) { + $list = Bookmark::getList($value); + return $list->getPids(); +} + +/** + * Retrieves all the uids from the bookmark listid, including owner. + * + * @param string $value + * The listid. + * + * @return array + * An array containing user ids. + */ +function islandora_bookmark_list_users_query($value) { + $list = Bookmark::getList($value); + $users = $list->getUsers(); + $users[] = $list->bookmarkOwner; + return $users; +} diff --git a/includes/bookmark.inc b/includes/bookmark.inc index ed9e87a..3af053f 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -6,409 +6,976 @@ */ /** - * Class that defines a Bookmark object. A Bookmark object needs only to be - * constructed such that it contains a listname and listid for uniqueness. + * Interface defining the base operations required to implement a backend. */ -class Bookmark { +interface BookmarkInterface { /** - * The list of users who belong to this object. + * Adds the specified PID to the given list. * - * @var array + * @param string $pid + * The PID of an object. */ - public $userList = array(); + public function addPid($pid); /** - * The list of pids that belong to this object. + * Get PIDs associated with this list. * - * @var array + * @return array + * An array of string, each representing an ID in a list. */ - public $pidList = array(); + public function getPids($offset = 0, $limit = -1); /** - * The name of the object. + * Removes the specified PID from the given list. * - * @var string + * @param string $pid + * The PID of an object. */ - public $bookmarkName; + public function removePid($pid); + + /** + * Shares a list with the given user. + * + * @param int $user + * A user ID. + */ + public function addUser($user); + + /** + * Get a list of all the users who a list is shared with. + * + * @return array + * An array of integers, representing user IDs. + */ + public function getUsers(); /** - * The unique ID of the object. This is generated through the database or - * explicity assigned when adding objects to the session. + * Removes the given user from the object. * - * @var integer + * @param int $user + * A user ID. */ - public $bookmarkId; + public function removeUser($user); + + /** + * Delete the given list. + * + * Removes the list from the backend; attempting to get the same list again + * should fail. + */ + public function delete(); +} + +/** + * Class that defines a Bookmark object. A Bookmark object needs only to be + * constructed such that it contains a listname and listid for uniqueness. + */ +abstract class Bookmark implements BookmarkInterface { + /** + * The name of the object. + * + * @var string + */ + public $bookmarkName; /** * The owner of the object. * - * @var integer + * @var int */ public $bookmarkOwner; /** - * The user id of the current user accessing the object. + * The ID of the given list. * - * @var integer + * @var mixed */ - public $userId; + protected $bookmarkId; /** - * Whether the object is created or retrieved through the session. + * Flag whether or not the given backend is persistent. * - * @var boolean + * @var bool */ - public $bookmarkSession = FALSE; + protected $isPersistent; /** * Constructor for the Bookmark object. */ - public function __construct($params = array()) { - $this->userId = $params['user_id']; - $this->userList = $params['bookmark_users']; + public function __construct($id, $name, $owner) { + $this->bookmarkId = $id; + $this->bookmarkName = $name; + $this->bookmarkOwner = $owner; + } - if (!empty($params['bookmark_session'])) { - $this->bookmarkSession = $params['bookmark_session']; + /** + * Somewhat more graceful deprecation of old properties. + */ + public function __get($property) { + if ($property == 'userList') { + module_load_include('inc', 'islandora', 'includes/utilities'); + $message = islandora_deprecated('7.x-1.3', 'Use the methods {get,add,remove}Users(), instead of accessing as a property.'); + trigger_error(filter_xss($message), E_USER_DEPRECATED); + return $this->getUsers(); } - - if (!empty($params['bookmark_pids'])) { - $this->pidList = $params['bookmark_pids']; + elseif ($property == 'pidList') { + module_load_include('inc', 'islandora', 'includes/utilities'); + $message = islandora_deprecated('7.x-1.3', 'Use the methods {get,add,remove}Pids(), instead of accessing as a property.'); + trigger_error(filter_xss($message), E_USER_DEPRECATED); + return $this->getPids(); } + } - if (!empty($params['bookmark_name'])) { - $this->bookmarkName = $params['bookmark_name']; + /** + * Somewhat more graceful deprecation of old properties. + */ + public function __set($property, $value) { + if ($property == 'userList') { + module_load_include('inc', 'islandora', 'includes/utilities'); + $message = islandora_deprecated('7.x-1.3', 'Use the methods {get,add,remove}Users(), instead of accessing as a property.'); + trigger_error(filter_xss($message), E_USER_DEPRECATED); + $this->removeUsers($this->getUsers()); + $this->addUsers($value); + return $value; } - - if (!empty($params['bookmark_id'])) { - $this->bookmarkId = $params['bookmark_id']; + elseif ($property == 'pidList') { + module_load_include('inc', 'islandora', 'includes/utilities'); + $message = islandora_deprecated('7.x-1.3', 'Use the methods {get,add,remove}Pids(), instead of accessing as a property.'); + trigger_error(filter_xss($message), E_USER_DEPRECATED); + $this->removeAllPids(); + $this->addPids($value); + return $value; } + } + + /** + * Accessor for the list ID. + * + * @return mixed + * The value of $this->bookmarkId. + */ + public function getId() { + return $this->bookmarkId; + } - if (!$params['retrieve']) { - $this->createList(); + /** + * Accessor for the persistence. + * + * @return mixed + * The value of $this->isPersistent. + */ + public function getIsPersistent() { + return $this->isPersistent; + } + + /** + * Get the given (existing) list. + * + * @param mixed $id + * The ID of the list to attempt to load. + * @param bool $force_session_attempt + * Force examination of the session, to handle the hook_user_login() stuff. + * + * @return Bookmark|NULL + * Returns either an instantiated Bookmark object if the list exists, or + * NULL if we could not get it. + */ + public static function getList($id, $force_session_attempt = FALSE) { + global $user; + + $bookmark = NULL; + if ($user->uid === 0 || $force_session_attempt) { + $bookmark = BookmarkSession::getList($id, TRUE); } - else { - $this->bookmarkOwner = $params['bookmark_owner']; + + if ($bookmark === NULL) { + // Pass off to BookmarkDatabase. + $bookmark = BookmarkDatabase::getList($id, FALSE); } + + return $bookmark; + } + + /** + * Get the number of PIDs in the current list. + * + * Individual backends could probably do this more efficiently, so feel free + * to override. + * + * @return int + * An integer representing the number of items in the current list. + */ + public function getPidCount() { + return count($this->getPids()); } /** - * Removes the given pids from the object and from the database or session. + * Remove multiple PIDs from the list. + * + * Individual backends could probably do this more efficiently, so feel free + * to override. * * @param array $pids * An array of pids. */ public function removePids($pids) { - $this->pidList = array_diff($this->pidList, $pids); + foreach ($pids as $pid) { + $this->removePid($pid); + } + } - if (!$this->bookmarkSession) { - if ($this->managementAccess()) { - foreach ($pids as $value) { - $this->removePidDB($value); - } - } - else { - throw new Exception(t("You do not have access to remove bookmarks from the list $this->bookmarkName")); - } + /** + * Remove all PIDs from the list. + * + * Individual backends could probably do this more efficiently, so feel free + * to override. + */ + public function removeAllPids() { + $this->removePids($this->getPids()); + } + + /** + * Remove multiple users from the list. + * + * Individual backends could probably do this more efficiently, so feel free + * to override. + * + * @param array $users + * An array of integers, each representing a user ID. + */ + public function removeUsers($users) { + foreach ($users as $user) { + $this->removeUser($user); } - else { - $superserial = unserialize($_SESSION['islandora_bookmark'][$this->bookmarkId]); + } - foreach ($superserial->pidList as $key => $value) { - if (in_array($value, $pids)) { - unset($superserial->pidList[$key]); - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($superserial); - } - } + /** + * Adds the specified pids to the object and either the database or session. + * + * Individual backends could probably do this more efficiently, so feel free + * to override. + * + * @param array $pids + * An array of strings, representing object PIDs. + */ + public function addPids($pids) { + $pids = array_filter($pids, 'islandora_object_load'); + foreach ($pids as $pid) { + $this->addPid($pid); + } + } + + /** + * Adds the specified users to the object and either the database or session. + * + * Individual backends could probably do this more efficiently, so feel free + * to override. + * + * @param array $users + * An array of integers, representings user IDs. + */ + public function addUsers($users) { + foreach ($users as $user) { + $this->addUser($user); } } /** - * Remove all pids from the object and either the database or session. + * Create a new list for the current user. + * + * @param string $name + * A name for the newly created list. */ - public function removeAllPids() { - if (!$this->bookmarkSession) { - if ($this->managementAccess()) { - foreach ($this->pidList as $value) { - $this->removePidDB($value); - } - $this->pidList = array(); - } - else { - throw new Exception(t("You do not have access to remove bookmarks from the list $this->bookmarkName")); - } + public static function createNewList($name) { + global $user; + + if ($user->uid === 0) { + return BookmarkSession::createNewList($name); } else { - $superserial = unserialize($_SESSION['islandora_bookmark'][$this->bookmarkId]); - $superserial->pidList = array(); - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($superserial); + return BookmarkDatabase::createNewList($name); } } /** - * Removes the given users from the object and either the database or session. + * Removes the list from the backend. + * + * @deprecated Just here to conform with the old API. + */ + public function removeList() { + $this->delete(); + } + + /** + * Changes the name of the object in the session or the database. * - * @global type $user + * @param string $name + * The new name of the object. + */ + public function changeListName($name) { + $this->bookmarkName = $name; + } + + /** + * Check if user can execute management functions on the current Bookmark. * - * @param array $users - * An array of users. + * @return bool + * TRUE if they can execute management functions, FALSE otherwise. */ - public function removeUsers($users) { + public function managementAccess() { global $user; - $this->userList = array_diff($this->userList, $users); - if (!$this->bookmarkSession) { - if ($this->managementAccess()) { - if (in_array($this->bookmarkOwner, $users)) { - $this->removeList(); - return; - } - foreach ($users as $value) { - $this->removeUserDB($value); - } - } - elseif (in_array($this->userId, $users)) { - $this->removeUserDB($this->userId); - } - else { - throw new Exception(t("You do not have access to remove users from the list $this->bookmarkName")); - } + if ($user->uid === $this->bookmarkOwner) { + return TRUE; } else { - $superserial = unserialize($_SESSION['islandora_bookmark'][$this->bookmarkId]); - - foreach ($superserial->userList as $key => $value) { - if (in_array($this->bookmarkOwner, $users)) { - $this->removeList(); - return; - } - if (in_array($value, $users)) { - unset($superserial->userList[$key]); - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($superserial); - } - } + return FALSE; } } /** - * Adds the specified pids to the object and either the database or session. + * Get the definition for the table. * * @param array $pids - * An array of pids. + * An array of PIDs, for which to build a tableselect. * - * @throws Exception + * @return array + * An associative array defining a tableselect element. */ - public function addPids($pids) { - foreach ($pids as $key => $value) { - if (islandora_object_load($value)) { - if (!in_array($value, $this->pidList)) { - array_push($this->pidList, $value); - - if (!$this->bookmarkSession) { - if ($this->managementAccess()) { - $this->addPidDB($value); - } - else { - throw new Exception(t("You do not have access to add bookmarks to the list $this->bookmarkName")); - } - } - else { - $superserial = unserialize($_SESSION['islandora_bookmark'][$this->bookmarkId]); - $superserial->pidList[] = $value; - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($superserial); - } - } - } - else { - throw new Exception(t("Could not bookmark '%value' to '%bookmark' as the object doesn't exist.", array( - '%value' => $value, '%bookmark' => $this->bookmarkName))); - } - } + protected function getTable($pids) { + return array( + '#type' => 'tableselect', + '#header' => $this->getTableHeader(), + '#options' => array_map(array($this, 'getTableRow'), drupal_map_assoc($pids)), + ); } /** - * Adds the specified users to the object and either the database or session. + * Build the header for the table. * - * @param array $users - * An array of users. + * @return array + * An array representing the header for a tableselect, as used by + * theme_tableselect(). */ - public function addUsers($users) { - foreach ($users as $key => $value) { - if (!in_array($value, $this->userList)) { - array_push($this->userList, $value); + protected function getTableHeader() { + $header = array('markup' => array('data' => t('Label')));; - if (!$this->bookmarkSession) { - if ($this->managementAccess()) { - $this->addUserDB($value); - } - else { - throw new Exception(t("You do not have access to add users to the list $this->bookmarkName")); - } - } - else { - $superserial = unserialize($_SESSION['islandora_bookmark'][$this->bookmarkId]); - $superserial->userList[] = $value; - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($superserial); - } - } + if ($this->managementAccess()) { + $header['remove'] = array('data' => t('Operations')); } + + return $header; } /** - * Inserts a record into the database or adds the object to the session. + * Build the row for the given PID. + * + * @param string $pid + * A PID for which to generate a row in the tableselect. + * + * @return array + * An associative array keyed according to what is provided in the header. */ - public function createList() { - $this->bookmarkOwner = $this->userId; + protected function getTableRow($pid) { + $row = array( + 'markup' => islandora_bookmark_generate_markup($pid), + ); - if (!$this->bookmarkSession) { - $fields = array( - 'listname' => $this->bookmarkName, - 'listowner' => $this->userId, + if ($this->managementAccess()) { + $remove_button = array( + '#type' => 'submit', + '#name' => 'remove_pid_' . $pid, + '#id' => 'remove_pid_' . $pid, + '#button_type' => 'submit', + '#pid' => $pid, + '#value' => t('Remove'), + + ); + $row['remove'] = array( + 'data' => drupal_render($remove_button), + 'class' => 'operations', ); - $list_id = db_insert('islandora_bookmark_list_names')->fields($fields)->execute(); + } - $this->bookmarkId = $list_id; + return $row; + } - foreach ($this->userList as $key => $value) { - $fields = array( - 'uid' => $value, - 'listid' => $this->bookmarkId, - ); - db_insert('islandora_bookmark_list_users')->fields($fields)->execute(); - } - foreach ($this->pidList as $key => $value) { - $fields = array( - 'listid' => $this->bookmarkId, - 'pidid' => $value, + /** + * Form building method. + */ + public function formGet($form, &$form_state) { + global $user; + if (module_exists('citation_exporter')) { + if (CitationExporter::ReadyToExport()) { + $citation_export_head = array( + '#type' => 'html_tag', + '#tag' => 'meta', + '#attributes' => array( + 'http-equiv' => 'refresh', + 'content' => '1.0;' . url(CITATION_EXPORTER_EXPORT_MENU, array('absolute' => TRUE)), + ), ); - db_insert('islandora_bookmark_list_pids')->fields($fields)->execute(); + drupal_add_html_head($citation_export_head, 'citation_exporter_head'); } } - else { - // Add to session. - if (isset($_SESSION['islandora_bookmark'])) { - $index = count($_SESSION['islandora_bookmark']); + + $form = array( + '#tree' => TRUE, + '#attached' => array( + 'css' => array( + drupal_get_path('module', 'islandora_bookmark') . '/css/islandora_bookmark.css', + ), + ), + '#prefix' => '
', + '#suffix' => '
', + 'bookmarks' => array( + '#weight' => 0, + 'fieldset' => array( + '#type' => 'fieldset', + '#title' => 'Detailed List', + '#prefix' => '
', + '#suffix' => '
', + ), + ), + ); + + // This is the RSS icon link. + if ($this->getIsPersistent() && $this->getPidCount() > 0) { + $id = $this->getId(); + $form['bookmark_rss'] = array( + '#weight' => -1, + '#markup' => l(theme('image', array( + 'path' => drupal_get_path('module', 'islandora_bookmark') . '/images/rss.png', + 'title' => t('Present Bookmark list in RSS form'), + 'attributes' => array(), + )), "islandora-bookmark/listid/$id/rss", array('html' => TRUE)), + '#prefix' => '
', + '#suffix' => '
', + ); + } + + $rows = array(); + + // Do pager calcuations to limit the number of objects displayed per page. + $num_eles = variable_get('islandora_bookmark_detailed_page_elements', 10); + $num_pids = $this->getPidCount(); + $form_state['islandora_bookmark_pager_element'] = $pager_element = 0; + + $page = pager_default_initialize($num_pids, $num_eles, $pager_element); + $offset = $page * $num_eles; + + $pids = $this->getPids($offset, $num_eles); + + $table = $this->getTable($pids); + $form['bookmarks']['fieldset'] += array( + 'table' => $table += array( + '#empty' => t('@type list is empty.', array( + '@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')), + )), + '#weight' => -2, + ), + 'pager' => array( + '#markup' => theme('pager', array( + 'element' => $pager_element, + )), + '#weight' => -1, + ), + ); + $row_count = count($form['bookmarks']['fieldset']['table']['#options']); + + if ($row_count > 0) { + $this->formAddActions($form['bookmarks']['fieldset'], $form_state); + } + + $this->formAddSecondaryActions($form, $form_state); + + return $form; + } + + /** + * Add actions to the given section of the form. + * + * @param array $container + * A reference to a part of the form, where the actions should be added. + */ + protected function formAddActions(&$container, &$form_state) { + $groups = array(); + + if ($this->managementAccess()) { + $groups['remove'] = array( + 'remove_selected' => array( + '#type' => 'submit', + '#value' => t('Remove selected'), + '#action_validate' => array( + 'formHasObjectsSelected', + ), + '#action_submit' => array( + 'formRemoveSelectedFromList', + ), + ), + 'remove_all' => array( + '#type' => 'submit', + '#value' => t('Remove all'), + '#action_submit' => array( + 'formRemoveAllFromList', + ), + ), + ); + } + + if (user_access('export islandora bookmarks')) { + $export_styles = array(); + $export = islandora_bookmark_get_export_options(); + $export_options['default'] = t('- Export type -'); + + foreach ($export as $module_name => $export_array) { + foreach ($export_array as $name => $export_func) { + $export_options[t("@module_name", array("@module_name" => $module_name))][$name] = $name; + $form_state['islandora_bookmark_export'][$name] = $export_func; + $export_styles[$name] = islandora_bookmark_get_export_format_options($name); + } } - else { - $index = 0; + + $style_elements = array(); + foreach ($export_styles as $key => $value) { + if (!empty($value)) { + $style_elements[$key] = array( + '#type' => 'select', + '#title' => t('Style'), + '#options' => $value, + '#weight' => 0, + '#name' => 'islandora_bookmark_export_styles_' . $key, + '#states' => array( + 'visible' => array( + ':input[name="bookmarks[fieldset][export_options]"]' => array('value' => $key), + ), + ), + ); + } } - $this->bookmarkId = 'session_' . $index; - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($this); + + $groups += array( + 'export_title' => array( + '#markup' => '

' . t('Export') . '

', + '#weight' => 3, + ), + 'export_options' => array( + '#type' => 'select', + '#title' => t('Format'), + '#options' => $export_options, + '#weight' => 4, + ), + 'format_opts' => array( + '#weight' => 5, + 'export_styles' => $style_elements, + 'export_selected' => array( + '#type' => 'submit', + '#value' => t('Export selected @type', array( + // XXX: Should be made better for translating... (Not all + // languages add an "s" to pluralize). + '@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's', + )), + '#weight' => 5, + '#action_validate' => array( + 'formHasExportTypeSelected', + 'formHasObjectsSelected', + ), + '#action_submit' => array( + 'formExportSelected', + ), + ), + 'export_all' => array( + '#type' => 'submit', + '#value' => t('Export all @type', array( + // XXX: Should be made better for translating... (Not all + // languages add an "s" to pluralize). + '@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's', + )), + '#weight' => 6, + '#action_validate' => array( + 'formHasExportTypeSelected', + ), + '#action_submit' => array( + 'formExportAll', + ), + ), + ), + ); + } + + if (!empty($groups)) { + $container += $groups; } } /** - * Removes the record from the database or the object from the session. + * Add secondary actions to the form. */ - public function removeList() { - if (!$this->bookmarkSession) { - if ($this->managementAccess()) { - db_delete('islandora_bookmark_list_names') - ->condition('listid', $this->bookmarkId) - ->execute(); - - foreach ($this->userList as $key => $value) { - db_delete('islandora_bookmark_list_users') - ->condition('listid', $this->bookmarkId) - ->execute(); + protected function formAddSecondaryActions(&$form, &$form_state) { + global $user; + // Share. + if ($this->managementAccess()) { + if (user_access('share islandora bookmarks')) { + // Triggered element (Add item or remove). + if (isset($form_state['triggering_element'])) { + // Add. + if ($form_state['triggering_element']['#button_id'] == 'islandora-bookmark-add-user') { + $add_user = $form_state['input']['bookmarks']['users']['listusers']; + if (drupal_strlen($add_user)) { + if ($add_user == 0) { + $output_user = t('Everyone'); + } + else { + $output_user = user_load($add_user)->name; + } + drupal_set_message(t('The user @adduser has been added to @listname', array( + '@listname' => $this->bookmarkName, + '@adduser' => $output_user, + ))); + $this->addUsers(array($add_user)); + } + } + // Remove. + if ($form_state['triggering_element']['#button_id'] == 'islandora-bookmark-remove-user') { + $row = $form_state['triggering_element']['#row']; + $remove_user = $form_state['values']['bookmarks']['users'][$row]['user']; + if ($remove_user == 0) { + $output_user = t('Everyone'); + } + else { + $output_user = user_load($remove_user)->name; + } + drupal_set_message(t('The user @username has been removed from the list @listname.', + array( + '@listname' => $this->bookmarkName, + '@username' => $output_user, + ) + )); + $this->removeUsers(array($remove_user)); + } } - foreach ($this->pidList as $key => $value) { - db_delete('islandora_bookmark_list_pids') - ->condition('listid', $this->bookmarkId) + $fku = array(); + if ($this->getIsPersistent()) { + + $users = $this->getUsers(); + if (count($users) === 0) { + $fku['empty'] = array( + '#type' => 'item', + '#markup' => t('@type list is not shared with other users.', array( + '@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')), + )), + ); + } + else { + $fku['title'] = array( + '#markup' => '

' . t('Shared with') . ':

', + ); + // Remove the user from the populated shared users. + $bookmark_users = $this->getUsers(); + $key = array_search($user->uid, $bookmark_users); + if ($key !== FALSE) { + unset($bookmark_users[$key]); + } + + foreach ($bookmark_users as $itr => $uid) { + $tempuser = user_load($uid); + if ($tempuser->uid == 0) { + $user_output = t('Everyone'); + } + else { + $user_output = $tempuser->name; + } + if ($tempuser->uid != $user->uid) { + $fku[$itr] = array( + '#prefix' => '
', + '#suffix' => '
', + 'label' => array( + '#type' => 'item', + '#markup' => $user_output, + ), + 'user' => array( + '#type' => 'value', + '#value' => $uid, + ), + 'list-id' => array( + '#type' => 'value', + '#value' => $this->getId(), + ), + 'remove' => array( + '#type' => 'button', + '#value' => t('Remove'), + '#name' => 'islandora_bookmark_remove_user_' . $itr, + '#row' => $itr, + '#button_id' => 'islandora-bookmark-remove-user', + '#ajax' => array( + 'event' => 'click', + 'callback' => 'islandora_bookmark_remove_user', + 'wrapper' => 'islandora-bookmark-detailed-overview-users', + 'method' => 'replace', + ), + ), + ); + } + } + } + + $selectoptions = array( + '' => t('- Select user -'), + ); + if ($user->uid != 0) { + $options['0'] = array( + 'uid' => 0, + 'name' => t('Everyone'), + ); + } + // Get all users for use in select for forms. + $result = db_select('users', 'u') + ->fields('u', array('uid', 'name')) + ->condition('STATUS', 0, '!=') + ->condition('uid', $user->uid, '!=') + ->orderBy('uid') ->execute(); + + $options += $result->fetchAllAssoc('uid', PDO::FETCH_ASSOC); + if (isset($options['0'])) { + $options[0]['name'] = t('Everyone'); + } + + $available = array_diff_key($options, drupal_map_assoc($this->getUsers())); + foreach ($available as $index => $item) { + $selectoptions[$index] = $item['uid'] . ': ' . $item['name']; + } + $fku['listusers'] = array( + '#type' => 'select', + '#default value' => t("- Select user -"), + '#options' => $selectoptions, + '#width' => 20, + '#prefix' => '
', + ); + $fku['listadd'] = array( + '#type' => 'button', + '#value' => t('Share'), + '#button_id' => 'islandora-bookmark-add-user', + '#suffix' => '
', + '#ajax' => array( + 'event' => 'click', + 'callback' => 'islandora_bookmark_add_user', + 'wrapper' => 'islandora-bookmark-detailed-overview-users', + 'method' => 'replace', + ), + ); + + $form['bookmarks']['users'] = array( + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => FALSE, + '#title' => t('Share'), + '#prefix' => '
', + '#suffix' => '
', + ) + $fku; } } - else { - throw new Exception(t("You do not have access to remove the list $this->bookmarkName")); - } } - else { - unset($_SESSION['islandora_bookmark'][$this->bookmarkId]); + elseif ($user->uid != 0 && $user->uid != $this->bookmarkOwner && !in_array(0, $this->getUsers())) { + $form['bookmarks']['remove_self'] = array( + '#type' => 'submit', + '#value' => t('Remove yourself from this list'), + '#submit' => array('islandora_bookmark_remove_self'), + ); } } /** - * Removes the specified user from the object's entry in the database. - * - * @param int $user_delete - * The user to be removed. + * Form validation callback; ensure at least one object is selected. */ - protected function removeUserDB($user_delete) { - $and = db_and()->condition('uid', $user_delete)->condition('listid', $this->bookmarkId); - db_delete('islandora_bookmark_list_users')->condition($and)->execute(); + protected function formHasObjectsSelected($form, $form_state) { + $pids = $this->formGetSelected($form_state); + if (empty($pids)) { + form_set_error('bookmarks][fieldset][table', t('You must select at least one object.')); + } } /** - * Adds the specified user to the object's entry in the database. + * One of our '#action_validate' callbacks. * - * @param int $user_add - * The user to be added. + * Checks that the export action has an export type selected. */ - protected function addUserDB($user_add) { - $fields = array( - 'uid' => $user_add, - 'listid' => $this->bookmarkId, - ); - db_insert('islandora_bookmark_list_users')->fields($fields)->execute(); + protected function formHasExportTypeSelected($form, $form_state) { + $el = $form['bookmarks']['fieldset']['export_options']; + $key = drupal_array_get_nested_value($form_state['values'], $el['#parents']); + if ($key == 'default') { + form_set_error(implode('][', $el['#parents']), t('Please select an export type.')); + } } /** - * Removes the specified pid from the object's entry in the database. - * - * @param string $pid - * The pid to be removed. + * Submission callback; export the selected PIDs. */ - protected function removePidDB($pid) { - $and = db_and()->condition('pidid', $pid)->condition('listid', $this->bookmarkId); - db_delete('islandora_bookmark_list_pids')->condition($and)->execute(); + protected function formExportSelected($form, $form_state) { + $pids = $this->formGetSelected($form_state); + $this->formExport($pids, $form_state); } /** - * Add the specified pid to the object's entry in the database. - * - * @param string $pid - * The user to be added. + * Submission callback; export all PIDs in the list. */ - protected function addPidDB($pid) { - $fields = array( - 'listid' => $this->bookmarkId, - 'pidid' => $pid, + protected function formExportAll($form, $form_state) { + $pids = $this->formGetAll($form_state); + $this->formExport($pids, $form_state); + } + + /** + * Submission helper; export the given PIDs. + */ + protected function formExport($pids, $form_state) { + $el = $form_state['complete form']['bookmarks']['fieldset']['export_options']; + $key = drupal_array_get_nested_value($form_state['values'], $el['#parents']); + + $params = array( + $pids, ); - db_insert('islandora_bookmark_list_pids')->fields($fields)->execute(); + + if (array_key_exists('islandora_bookmark_export_styles_' . $key, $form_state['input'])) { + $params[] = $form_state['input']['islandora_bookmark_export_styles_' . $key]; + } + + call_user_func_array($form_state['islandora_bookmark_export'][$key], $params); } /** - * Changes the name of the object in the session or the database. - * - * @param string $name - * The new name of the object. + * Submission callback; remove the selected PIDs from the list. */ - public function changeListName($name) { - $this->bookmarkName = $name; - if (!$this->bookmarkSession) { - db_update('islandora_bookmark_list_names') - ->fields(array( - 'listname' => $name, - )) - ->condition('listid', $this->bookmarkId) - ->execute(); + protected function formRemoveSelectedFromList($form_state) { + $pids = $this->formGetAll($form_state); + $this->formRemoveFromList($pids, $form_state); + } + + /** + * Submission callback; remove all PIDs from the list. + */ + protected function formRemoveAllFromList($form_state) { + $pids = $this->formGetAll($form_state); + $this->formRemoveFromList($pids, $form_state); + } + + /** + * Submission helper; remove the given PIDs from the list. + */ + protected function formRemoveFromList($pids, $form_state) { + $this->removePids($pids); + } + + /** + * Form validation handler. + */ + public function formValidate(&$form, &$form_state) { + $pids_to_remove = $this->formGetPidToRemove($form_state); + if ($pids_to_remove) { + // No-op. } - else { - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($this); + elseif (isset($form_state['clicked_button']['#action_validate'])) { + $params = array(&$form, &$form_state); + foreach ($form_state['clicked_button']['#action_validate'] as $validator) { + call_user_func_array(array($this, $validator), $params); + } + } + elseif (isset($form_state['triggering_element']['#button_id'])) { + if ($form_state['triggering_element']['#button_id'] == 'islandora-bookmark-add-user') { + $parents = array('bookmarks', 'users', 'listusers'); + $add_user = drupal_array_get_nested_value($form_state['values'], $parents); + if (drupal_strlen($add_user) === 0) { + form_set_error(implode('][', $parents), t('Please select a user to add to @listname.', array( + '@listname' => $this->bookmarkName, + ))); + } + } } } /** - * Check if user can execute management functions on the current Bookmark. + * Get the PIDs of any single remove button(s). * - * @return bool - * TRUE if they can execute management functions, FALSE otherwise. + * @return array + * An array of PIDs--may be empty if there are none present. */ - public function managementAccess() { - if ($this->userId === $this->bookmarkOwner) { - return TRUE; + protected function formGetPidToRemove($form_state) { + $to_remove = preg_grep('/remove_pid_.*/', array_keys($form_state['input'])); + return preg_replace('/remove_pid_(.*)/', '$1', $to_remove); + } + + /** + * Form submission handler. + */ + public function formSubmit(&$form, &$form_state) { + $pids_to_remove = $this->formGetPidToRemove($form_state); + if ($pids_to_remove) { + $this->removePids($pids_to_remove); + + foreach (array_map('islandora_object_load', $pids_to_remove) as $removed_object) { + drupal_set_message(t('The @type @objectname has been removed from @listname.', array( + '@type' => variable_get('islandora_bookmark_type', 'bookmark'), + '@listname' => $this->bookmarkName, + '@objectname' => $removed_object->label, + ))); + } } - else { - return FALSE; + elseif (isset($form_state['clicked_button']['#action_submit'])) { + $params = array(&$form, &$form_state); + foreach ($form_state['clicked_button']['#action_submit'] as $submit) { + call_user_func_array(array($this, $submit), $params); + } + } + + $errors = form_get_errors(); + if (count($errors) === 0) { + $page = pager_default_initialize( + $this->getPidCount(), + variable_get('islandora_bookmark_detailed_page_elements', 10), + $form_state['islandora_bookmark_pager_element'] + ); + + $form_state['redirect'] = array( + 'islandora-bookmark/listid/' . $this->getId(), + array(), + ); + + if ($page > 0) { + $form_state['redirect'][1]['query'] = array( + 'page' => $page, + ); + } } } + + /** + * Submission helper; gets all PIDs. + */ + protected function formGetAll($form_state) { + $selected = array_filter($this->getPids()); + $functional = array_filter($selected, 'islandora_object_load'); + $broken = array_diff($selected, $functional); + if (!empty($broken)) { + watchdog('islandora_bookmark', 'Broken PIDs encountered: @pids', array( + '@pids' => implode(', ', $broken), + ), WATCHDOG_WARNING); + } + + return $functional; + } + + /** + * Submission helper; gets the PIDs of the selected items. + */ + protected function formGetSelected($form_state) { + $selected = array_filter($form_state['values']['bookmarks']['fieldset']['table']); + $functional = array_filter($selected, 'islandora_object_load'); + $broken = array_diff($selected, $functional); + if (!empty($broken)) { + watchdog('islandora_bookmark', 'Broken PIDs encountered: @pids', array( + '@pids' => implode(', ', $broken), + ), WATCHDOG_WARNING); + } + + return $functional; + } } diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc new file mode 100644 index 0000000..c159d89 --- /dev/null +++ b/includes/bookmark_db.inc @@ -0,0 +1,262 @@ +getMapped(). + * + * @var array + */ + protected $magicMap = array( + 'bookmarkName' => 'listname', + 'bookmarkOwner' => 'listowner', + 'type' => 'type', + ); + + /** + * Constructor for the Bookmark object. + */ + public function __construct($list_id) { + parent::__construct($list_id, NULL, NULL); + $this->isPersistent = TRUE; + + foreach (array_keys($this->magicMap) as $for_magic) { + unset($this->$for_magic); + } + } + + /** + * Helper function to deal with magic property. + * + * The properties as stored in the database are different. Let's map 'em + * here. + * + * @param string $property + * A property on this object which we want to map to a column in the + * "islandora_bookmark_list_names" table. + * + * @return string + * A string representing the column name. + * + * @throws Exception + * If we do not have a mapping for the given exception, throw an exception. + */ + protected function getMapped($property) { + if (isset($this->magicMap[$property])) { + return $this->magicMap[$property]; + } + else { + trigger_error(format_string('Unknown property "@property".', array( + '@property' => $property, + ))); + return FALSE; + } + } + + /** + * Magic property... Map to our column name, and set it. + */ + public function __set($property, $value) { + $mapped = $this->getMapped($property); + if ($mapped) { + db_update('islandora_bookmark_list_names') + ->fields(array( + $mapped => $value, + )) + ->condition('listid', $this->bookmarkId) + ->execute(); + return $value; + } + } + + /** + * Magic property... Map to our column name, and get it. + */ + public function __get($property) { + $mapped = $this->getMapped($property); + if ($mapped) { + return db_select('islandora_bookmark_list_names', 'n') + ->fields('n', array($this->getMapped($property))) + ->condition('listid', $this->bookmarkId) + ->execute() + ->fetchField(); + } + } + + /** + * Refer to parent docs. + * + * Branch to different classes based on type. Return NULL if a mapping for + * the given type is not available. + */ + public static function getList($id, $force_session_attempt = FALSE) { + if ($force_session_attempt) { + // Call the parent, which should call this with FALSE if not a session... + return parent::getList($id, $force_session_attempt); + } + + $types = module_invoke_all('islandora_bookmark_database_types'); + $type = db_select('islandora_bookmark_list_names', 'n') + ->fields('n', array('type')) + ->condition('listid', $id) + ->execute() + ->fetchField(); + + if (isset($types[$type])) { + $class = $types[$type]; + return new $class($id); + } + else { + return NULL; + } + } + + /** + * Convenience; get the base query for PIDs. + * + * @return SelectQuery + * A select query for the PIDs table. + */ + protected function getPidQuery() { + return db_select('islandora_bookmark_list_pids', 'p') + ->fields('p', array('pidid')) + ->condition('listid', $this->bookmarkId) + ->distinct(); + } + + /** + * Inherits. + */ + public function getPids($offset = 0, $limit = -1) { + $s = $this->getPidQuery(); + + if ($limit > 0) { + $s->range($offset, $limit); + } + + return $s->execute()->fetchAll(PDO::FETCH_COLUMN); + } + + /** + * Inherits. + */ + public function getPidCount() { + $s = $this->getPidQuery()->countQuery(); + + return intval($s->execute()->fetchField()); + } + + /** + * Build a new list. + * + * @param string $name + * The name for the list. + * @param string $type + * A string indicating the type of list. Default is 'bookmark'. + * @param object $owner + * A user object from which we can grab the uid. If not provided, we'll + * grab the global "$user" variable. + * + * @return BookmarkDatabase + * A new Bookmark object. + */ + public static function createNewList($name, $type = 'bookmark', $owner = NULL) { + if ($owner === NULL) { + global $user; + $owner = $user; + } + + $fields = array( + 'listname' => $name, + 'listowner' => $owner->uid, + 'type' => $type, + ); + $list_id = db_insert('islandora_bookmark_list_names') + ->fields($fields) + ->execute(); + + return static::getList($list_id, FALSE); + } + + /** + * Inherits. + */ + public function delete() { + if ($this->managementAccess()) { + $this->removeUsers($this->getUsers()); + $this->removePids($this->getPids()); + db_delete('islandora_bookmark_list_names') + ->condition('listid', $this->bookmarkId) + ->execute(); + + // XXX: Only to deal with old lists, in which the owner was included in + // "users" table. + $this->removeUser($this->bookmarkOwner); + } + else { + throw new Exception(t("You do not have access to remove the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); + } + } + + /** + * Inherits. + */ + public function removeUser($user_delete) { + db_delete('islandora_bookmark_list_users') + ->condition('uid', $user_delete) + ->condition('listid', $this->bookmarkId) + ->execute(); + } + + /** + * Inherits. + */ + public function addUser($user_add) { + $fields = array( + 'uid' => $user_add, + 'listid' => $this->bookmarkId, + ); + db_insert('islandora_bookmark_list_users')->fields($fields)->execute(); + } + + /** + * Inherits. + */ + public function removePid($pid) { + db_delete('islandora_bookmark_list_pids') + ->condition('pidid', $pid) + ->condition('listid', $this->bookmarkId) + ->execute(); + } + + /** + * Inherits. + */ + public function addPid($pid) { + $fields = array( + 'listid' => $this->bookmarkId, + 'pidid' => $pid, + ); + db_insert('islandora_bookmark_list_pids')->fields($fields)->execute(); + } + + /** + * Inherits. + */ + public function getUsers() { + return db_select('islandora_bookmark_list_users', 'u') + ->fields('u', array('uid')) + ->condition('listid', $this->bookmarkId) + ->condition('uid', $this->bookmarkOwner, '!=') + ->execute() + ->fetchAll(PDO::FETCH_COLUMN); + } +} diff --git a/includes/bookmark_rss.inc b/includes/bookmark_rss.inc new file mode 100644 index 0000000..555c7de --- /dev/null +++ b/includes/bookmark_rss.inc @@ -0,0 +1,132 @@ +getPids(); + + // Loop through all the pids and build the formatted RSS items. + $items = NULL; + foreach ($all_pids as $pid) { + // Load fedora object from pid. + $fedora_object = islandora_object_load($pid); + if ($fedora_object) { + $items .= islandora_bookmark_create_rss_item($fedora_object); + } + } + + // Get the global base url. + global $base_url; + // Set the channel elements for the RSS feed. + // The title of the RSS channel to be the site name or Drupal by default. + $title = t('@site_name aggregator', array('@site_name' => variable_get('site_name', 'Drupal'))); + // The link to be the base url. + $link = $base_url; + // The description of the RSS channel. + $description = t('Aggregated results of: @bookmarkName', array('@bookmarkName' => $bookmark->bookmarkName)); + + // Build the xml string to be printed. + $output = "\n"; + $output .= "\n"; + $output .= format_rss_channel($title, $link, $description, $items); + $output .= "\n"; + + // Print the xml. + print $output; + exit; +} + +/** + * Creates an bookmark RSS item. + * + * @param AbstractObject $object + * The object to create RSS item. + * + * @return string + * The formated RSS item. + */ +function islandora_bookmark_create_rss_item(AbstractObject $object) { + + module_load_include('inc', 'islandora', 'includes/utilities'); + + // Create rss item array. + $rss_item = array(); + + // Invokes the islandora_bookmark_rss_item hook. + foreach (islandora_build_hook_list('islandora_bookmark_rss_item', $object->models) as $hook) { + $temp = module_invoke_all($hook, $object); + + // If something returns then merge it with the $rss_ite. + if (!empty($temp)) { + $rss_item = array_merge_recursive($rss_item, $temp); + } + } + + // If $rss_item is empty then call the default RSS item. + if (empty($rss_item)) { + $rss_item = islandora_bookmark_rss_item($object); + } + + // Return the formatted RSS item. + return format_rss_item($rss_item['title'], $rss_item['link'], $rss_item['description'], $rss_item['items']); +} + +/** + * Function for setting the values of the elements for the default RSS. + * + * @tutorial http://feed2.w3.org/docs/rss2.html#hrelementsOfLtitemgt + * + * @tutorial http://feed2.w3.org/docs/rss2.html#requiredChannelElements + * + * @param AbstractObject $object + * An abstract object + * + * @return array + * an associative array containing the rss item to be displayed + */ +function islandora_bookmark_rss_item(AbstractObject $object) { + + // Global variables. + global $base_url; + + // Create an associative array for the required elements + // for a valid bookmark RSS item. + $rss_item = array(); + // The default title of the item will be the objects label. + $rss_item['title'] = $object->label; + // The default link will be to the objects page. + $rss_item['link'] = url('islandora/object/' . htmlspecialchars($object->id, ENT_QUOTES, 'utf-8'), array('absolute' => TRUE)); + // The default description will be No description. + $rss_item['description'] = t('No description'); + + // Get the site name to be the value of the source. + $rss_source = variable_get('site_name', "Default site name"); + + // Set the default source attribute to be to be the base url. + $rss_item['items'] = array( + array( + 'key' => 'source', + 'value' => $rss_source, 'attributes' => array('url' => $base_url)), + ); + + // Returns the the rss default item. + return $rss_item; +} diff --git a/includes/bookmark_session.inc b/includes/bookmark_session.inc new file mode 100644 index 0000000..6363385 --- /dev/null +++ b/includes/bookmark_session.inc @@ -0,0 +1,135 @@ + hash('sha256', $name), + )); + } + + /** + * Constructor for the Bookmark object. + */ + public function __construct($name) { + global $user; + parent::__construct(static::getKey($name), $name, $user->uid); + $this->pidList = array(); + $this->isPersistent = FALSE; + } + + /** + * Inherits. + */ + public static function getList($id) { + if (isset($_SESSION['islandora_bookmark'][$id])) { + return $_SESSION['islandora_bookmark'][$id]; + } + return NULL; + } + + /** + * Inherits. + */ + public function getPids($offset = 0, $limit = -1) { + if ($limit > 0) { + return array_slice($this->pidList, $offset, $limit); + } + else { + return $this->pidList; + } + } + + /** + * Build a new list. + * + * @param string $name + * The name for the list. + * @param object $owner + * A user object from which we can grab the uid. If not provided, we'll + * grab the global "$user" variable. + * + * @return BookmarkDatabase + * A new Bookmark object. + */ + public static function createNewList($name, $owner = NULL) { + if ($owner === NULL) { + global $user; + $owner = $user; + } + + $list_id = static::getKey($name); + + $_SESSION['islandora_bookmark'][$list_id] = new static($name); + + return static::getList($list_id); + } + + /** + * Inherits. + */ + public function delete() { + unset($_SESSION['islandora_bookmark'][$this->bookmarkId]); + } + + /** + * Inherits. + */ + public function getUsers() { + return array( + $this->bookmarkOwner, + ); + } + + /** + * Inherits. + */ + public function removeUser($user_delete) { + // No-op. + } + + /** + * Inherits. + */ + public function addUser($user_add) { + // No-op. + } + + /** + * Inherits. + */ + public function removePid($pid) { + $this->pidList = array_diff($this->pidList, array($pid)); + } + + /** + * Inherits. + */ + public function addPid($pid) { + $this->pidList[] = $pid; + } +} diff --git a/includes/solr_results.inc b/includes/solr_results.inc index 3bd77b0..a8d30f6 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -34,56 +34,90 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { if (empty($results)) { return t('No results'); } - $results_form = drupal_get_form('islandora_bookmark_results_form', $results['response']['objects'], $this->islandoraSolrQueryProcessor); + $results_form = drupal_get_form('islandora_bookmark_results_form', $this, $results['response']['objects']); return drupal_render($results_form); } -} -/** - * Creates a form that will be rendered with the Solr search results. - * - * Rendered as a tableselect form. - * - * @param array $form - * The Drupal form definition. - * @param array $form_state - * The Drupal form state. - * @param array $object_results - * The object results from the Islandora Solr processor created in the - * printResults function. - * @param IslandoraSolrQueryProcessor $query_processor - * The Islandora Solr Query Processor used to modify the existing query - * to retrieve all results. - * - * @return array - * The Drupal form definition. - */ -function islandora_bookmark_results_form(array $form, array &$form_state, $object_results, $query_processor) { - module_load_include('inc', 'islandora_bookmark', 'includes/api'); - - form_load_include($form_state, 'inc', 'islandora_bookmark', 'IslandoraSolrResultsBookmark'); - - if (module_exists('citation_exporter')) { - if (CitationExporter::ReadyToExport()) { - $citation_export_head = array( - '#type' => 'html_tag', - '#tag' => 'meta', - '#attributes' => array( - 'http-equiv' => 'refresh', - 'content' => '1.0;' . url(MENU_CITATION_EXPORTER_EXPORT, array('absolute' => TRUE)), - ), - ); - drupal_add_html_head($citation_export_head, 'citation_exporter_head'); + /** + * Form building method. + * + * @param array $form + * The Drupal form definition. + * @param array $form_state + * The Drupal form state. + * @param array $object_results + * The object results from the Islandora Solr processor created in the + * printResults function. + * + * @return array + * An array containing form structure. + */ + public function formGet($form, &$form_state, $object_results) { + $form_state['islandora_solr_query_processor'] = $this->islandoraSolrQueryProcessor; + module_load_include('inc', 'islandora_bookmark', 'includes/api'); + + form_load_include($form_state, 'inc', 'islandora_bookmark', 'IslandoraSolrResultsBookmark'); + + if (module_exists('citation_exporter')) { + if (CitationExporter::ReadyToExport()) { + $citation_export_head = array( + '#type' => 'html_tag', + '#tag' => 'meta', + '#attributes' => array( + 'http-equiv' => 'refresh', + 'content' => '1.0;' . url(CITATION_EXPORTER_EXPORT_MENU, array('absolute' => TRUE)), + ), + ); + drupal_add_html_head($citation_export_head, 'citation_exporter_head'); + } } + + $header = $this->getTableHeader(); + $rows = array(); + foreach ($object_results as $object_result) { + $doc = $object_result['solr_doc']; + $rows[$doc['PID']] = $this->getTableRow($object_result); + } + + if (count($rows) > 0) { + $form = array_merge($form, $this->getActions($form_state)); + } + + $form['islandora_bookmark_table'] = array( + '#header' => $header, + '#type' => 'tableselect', + '#options' => $rows, + '#attributes' => array('class' => array('islandora-bookmark-solr-results')), + '#empty' => t('No results found.'), + ); + return $form; } - $header = array( - 'markup' => t('Label'), - ); - $rows = array(); - $form_state['islandora_solr_query_processor'] = $query_processor; - foreach ($object_results as $object_result) { + /** + * Get the header row to use in the tableselect. + * + * @return array + * An associative array as used as headers by theme_tableselect()/ + */ + protected function getTableHeader() { + return array( + 'markup' => t('Label'), + ); + } + + /** + * Build a row for inclusion in the tableselect. + * + * @param array $object_result + * A result from the query processor. + * + * @return array + * An associative array of cells, keyed according to the header provided in + * $this->getTableHeader(). + */ + protected function getTableRow($object_result) { $doc = $object_result['solr_doc']; + $markup = ''; if (isset($doc['object_label'])) { $markup = $doc['object_label']; @@ -91,395 +125,468 @@ function islandora_bookmark_results_form(array $form, array &$form_state, $objec else { $markup = $doc['PID']; } + $object_url_info = array( 'path' => $object_result['object_url'], 'params' => $object_result['object_url_params'], 'markup' => $markup, ); - $rows[$doc['PID']] = array( + + return array( 'markup' => islandora_bookmark_generate_markup($doc['PID'], $object_url_info), ); } - if (count($rows) > 0) { - $export = islandora_bookmark_get_export_options(); - $export_options['default'] = t('- Export type -'); - $export_styles = array(); - foreach ($export as $module_name => $export_array) { - foreach ($export_array as $name => $export_func) { - $export_options[$module_name][$name] = $name; - $form_state['islandora_bookmark_export'][$name] = $export_func; - $export_styles[$name] = islandora_bookmark_get_export_format_options($name); + + /** + * Get the "actions" section of the form. + * + * @return array + * A form fragment containing a number of actions which might be performed + * on objects from the table select. + * + * Submit buttons may have two additional properties: + * - action_validate: An array of validation methods to call, which should + * form_set_error() as necessary. + * - action_submit: An array of submission methods to call. + * as hinted, these are /methods/, which should exist on the given class + * (or one of its ancestors). + */ + protected function getActions(&$form_state) { + $groups = array(); + + if (user_access('export islandora bookmarks')) { + $export = islandora_bookmark_get_export_options(); + $export_options['default'] = t('- Export type -'); + $export_styles = array(); + foreach ($export as $module_name => $export_array) { + foreach ($export_array as $name => $export_func) { + $export_options[$module_name][$name] = $name; + $form_state['islandora_bookmark_export'][$name] = $export_func; + $export_styles[$name] = islandora_bookmark_get_export_format_options($name); + } } - } - $form['islandora_bookmark_export_fieldset'] = array( - 'fieldset' => array( + $group = array( '#type' => 'fieldset', '#title' => t('Export to file'), '#weight' => 0, - ), - ); + ); - $form['islandora_bookmark_export_fieldset']['fieldset']['export_styles'] = array( - '#weight' => 4, - ); - foreach ($export_styles as $key => $value) { - if (!empty($export_styles[$key])) { - $form['islandora_bookmark_export_fieldset']['fieldset']['export_styles'][$key] = array( - '#type' => 'select', - '#title' => t('Style'), - '#options' => $export_styles[$key], - '#weight' => 4, - '#name' => 'islandora_bookmark_export_styles_' . $key, - '#states' => array( - 'visible' => array( - ':input[name="islandora_bookmark_export_options_select"]' => array('value' => $key), + $group['export_styles'] = array( + '#weight' => 4, + ); + foreach ($export_styles as $key => $value) { + if (!empty($export_styles[$key])) { + $group['export_styles'][$key] = array( + '#type' => 'select', + '#title' => t('Style'), + '#options' => $export_styles[$key], + '#weight' => 4, + '#name' => 'islandora_bookmark_export_styles_' . $key, + '#states' => array( + 'visible' => array( + ':input[name="export_options"]' => array('value' => $key), + ), ), - ), - ); + ); + } } - } - $form['islandora_bookmark_export_fieldset']['fieldset']['export_options'] = array( - '#type' => 'select', - '#options' => $export_options, - '#weight' => 3, - '#title' => t('Format'), - '#name' => 'islandora_bookmark_export_options_select', - ); + $group['export_options'] = array( + '#type' => 'select', + '#options' => $export_options, + '#weight' => 3, + '#title' => t('Format'), + ); - $form['islandora_bookmark_export_fieldset']['fieldset']['export_selected_submit'] = array( - '#type' => 'submit', - '#value' => t('Export selected objects'), - '#name' => 'export_selected', - '#weight' => 5, - ); - $form['islandora_bookmark_export_fieldset']['fieldset']['export_all_submit'] = array( - '#type' => 'submit', - '#value' => t('Export all results'), - '#name' => 'export_all', - '#weight' => 5, - ); - $user_bookmarks = islandora_bookmark_get_user_owned_bookmarks(); + $group['export_selected_submit'] = array( + '#type' => 'submit', + '#value' => t('Export selected objects'), + '#name' => 'export_selected', + '#weight' => 5, + '#action_submit' => 'exportSelected', + '#action_validate' => array( + 'hasExportTypeSelected', + 'hasObjectsSelected', + ), + ); + $group['export_all_submit'] = array( + '#type' => 'submit', + '#value' => t('Export all results'), + '#name' => 'export_all', + '#weight' => 5, + '#action_submit' => 'exportAll', + '#action_validate' => array( + 'hasExportTypeSelected', + ), + ); + + $groups['islandora_bookmark_export'] = array( + 'fieldset' => $group, + ); + } + $user_bookmarks = islandora_bookmark_get_user_owned_bookmarks(); if (count($user_bookmarks) > 0) { $save_options = array(); - $save_options['default'] = t('- Select bookmark list -'); + $save_options['default'] = t('- Select @type list -', array( + '@type' => variable_get('islandora_bookmark_type', 'bookmark'), + )); foreach ($user_bookmarks as $bookmark) { - $save_options[$bookmark->bookmarkId] = $bookmark->bookmarkName; + $save_options[$bookmark->getId()] = $bookmark->bookmarkName; } - $form['islandora_bookmark_save_fieldset'] = array( - 'fieldset' => array( - '#type' => 'fieldset', - '#title' => t('Bookmark'), - '#weight' => 0, - ), + $group = array( + '#type' => 'fieldset', + '#title' => t('@type', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), + '#weight' => 0, ); - $form['islandora_bookmark_save_fieldset']['fieldset']['save_options'] = array( + $group['save_options'] = array( '#type' => 'select', '#options' => $save_options, '#weight' => 3, ); - $form['islandora_bookmark_save_fieldset']['fieldset']['save_selected_submit'] = array( + $group['save_selected_submit'] = array( '#type' => 'submit', '#value' => t('Bookmark selected objects'), - '#name' => 'save_selected', '#weight' => 4, + '#action_submit' => 'saveSelected', + '#action_validate' => array( + 'hasListSelected', + 'hasObjectsSelected', + ), ); - $form['islandora_bookmark_save_fieldset']['fieldset']['save_all_submit'] = array( + $group['save_all_submit'] = array( '#type' => 'submit', '#value' => t('Bookmark all results'), - '#name' => 'save_all', '#weight' => 5, + '#action_submit' => 'saveAll', + '#action_validate' => array( + 'hasListSelected', + ), ); - $form['islandora_bookmark_save_fieldset']['fieldset']['go_to_bookmark'] = array( + $group['go_to_bookmark'] = array( '#type' => 'submit', '#value' => t('View list'), '#name' => 'goto_bookmark', '#weight' => 6, + '#action_submit' => 'goToBookmark', + '#action_validate' => array( + 'hasListSelected', + ), ); - } - } - $form['islandora_bookmark_table'] = array( - '#header' => $header, - '#type' => 'tableselect', - '#options' => $rows, - '#attributes' => array('class' => array('islandora-bookmark-solr-results')), - '#empty' => t('No results found.'), - ); - return $form; -} + $groups['islandora_bookmark_save_fieldset'] = array( + 'fieldset' => $group, + ); + } -/** - * Submit handler for the islandora_bookmark_results form. - * - * @param array $form - * The Drupal form definition. - * @param array $form_state - * The Drupal form state. - */ -function islandora_bookmark_results_form_submit(array $form, array &$form_state) { - $queries = $_GET; - unset($queries['q']); - $form_state['redirect'] = array(request_path(), array('query' => $queries)); - module_load_include('inc', 'islandora_bookmark', 'includes/api'); - - // Grab and modify the query proc's limit to 100000 incase we need - // to retrieve all objects. - $query_proc = $form_state['islandora_solr_query_processor']; - $query_proc->solrLimit = 100000; - - if ($form_state['clicked_button']['#name'] === 'export_selected') { - $pids = array(); - $broke_pids = ''; - $broke_count = 0; + return $groups; + } - foreach ($form_state['values']['islandora_bookmark_table'] as $checkbox => $value) { - if ($value !== 0) { - if (islandora_object_load($value)) { - $pids[] = $value; - } - else { - $broke_pids .= $value . ' '; - $broke_count++; - } - } - } - $key = $form_state['input']['islandora_bookmark_export_options_select']; - if (count($pids) > 0 && $key !== 'default') { - if (array_key_exists('islandora_bookmark_export_styles_' . $key, $form_state['input'])) { - call_user_func($form_state['islandora_bookmark_export'][$key], $pids, $form_state['input']['islandora_bookmark_export_styles_' . $key]); - } - else { - call_user_func($form_state['islandora_bookmark_export'][$key], $pids); - } - } - elseif (count($pids) === 0 && $broke_count === 0) { - drupal_set_message(t('Please select at least one object.'), 'error'); + /** + * One of our '#action_validate' callbacks. + * + * Checks that the export action has an export type selected. + */ + protected function hasExportTypeSelected($form, &$form_state) { + $el = $form['islandora_bookmark_export']['fieldset']['export_options']; + $key = drupal_array_get_nested_value($form_state['values'], $el['#parents']); + if ($key == 'default') { + form_set_error(implode('][', $el['#parents']), t('Please select an export type.')); } - elseif ($key === 'default') { - drupal_set_message(t('Please select an export type.'), 'error'); + } + + /** + * One of our '#action_validate' callbacks. + * + * Checks that the bookmark save/view action has a list selected. + */ + protected function hasListSelected($form, &$form_state) { + $el = $form['islandora_bookmark_save_fieldset']['fieldset']['save_options']; + $key = drupal_array_get_nested_value($form_state['values'], $el['#parents']); + if ($key == 'default') { + form_set_error(implode('][', $el['#parents']), t('Please select a list.')); } - else { - if ($broke_count > 0) { - $broken_pids = format_plural($broke_count, 'The following object was unable to be exported as it does not exist: !broke_pids.', - 'The following objects were unable to be exported as they do not exist: !broke_pids.', - array('!broke_pids' => trim($broke_pids))); - drupal_set_message($broken_pids, 'error'); - } + } + + /** + * One of our '#action_validate' callbacks. + * + * Checks that some objects are selected. + */ + protected function hasObjectsSelected($form, &$form_state) { + $pids = $this->getSelected($form_state); + if (empty($pids)) { + form_set_error('islandora_bookmark_table', t('You must select at least one object.')); } } - elseif ($form_state['clicked_button']['#name'] === 'export_all') { - $query_proc->executeQuery(); - $solr_results = $query_proc->islandoraSolrResult; - $object_results = $solr_results['response']['objects']; - $all_pids = array(); - $broke_pids = ''; - $broke_count = 0; - foreach ($object_results as $object_result) { - $doc = $object_result['solr_doc']; - if (islandora_object_load($doc['PID'])) { - $all_pids[$doc['PID']] = $doc['PID']; - } - else { - $broke_pids .= $doc['PID'] . ' '; - $broke_count++; - } + /** + * One of our '#action_submit' callbacks. + * + * Export all PIDs the search yielded. + * + * @param array $form_state + * The array of form state. + */ + protected function exportAll(&$form_state) { + $pids = $this->getAll($form_state); + $this->export($pids, $form_state); + } + + /** + * One of our '#action_submit' callbacks. + * + * Export the selected PIDs the search yielded. + * + * @param array $form_state + * The array of form state. + */ + protected function exportSelected(&$form_state) { + $pids = $this->getSelected($form_state); + $this->export($pids, $form_state); + } + + /** + * Actually handle the export of objects. + * + * @param array $pids + * Multiple PIDs, as strings. + * @param array $form_state + * The array of form state. + */ + protected function export($pids, &$form_state) { + $el = $form_state['complete form']['islandora_bookmark_export']['fieldset']['export_options']; + $key = drupal_array_get_nested_value($form_state['values'], $el['#parents']); + + $params = array( + $pids, + ); + + if (array_key_exists('islandora_bookmark_export_styles_' . $key, $form_state['input'])) { + $params[] = $form_state['input']['islandora_bookmark_export_styles_' . $key]; } - $key = $form_state['input']['islandora_bookmark_export_options_select']; - if ($key !== 'default' && count($all_pids > 0)) { - if (array_key_exists('islandora_bookmark_export_styles_' . $key, $form_state['input'])) { - call_user_func($form_state['islandora_bookmark_export'][$key], $all_pids, $form_state['input']['islandora_bookmark_export_styles_' . $key]); + call_user_func_array($form_state['islandora_bookmark_export'][$key], $params); + } + + /** + * One of our '#action_submit' callbacks. + * + * Redirect to the given list. + * + * @param array $form_state + * The array of form state. + */ + protected function goToBookmark(&$form_state) { + $el = $form_state['complete form']['islandora_bookmark_save_fieldset']['fieldset']['save_options']; + $key = drupal_array_get_nested_value($form_state['values'], $el['#parents']); + drupal_goto('islandora-bookmark/listid/' . $key); + } + + /** + * One of our '#action_submit' callbacks. + * + * Save all PIDs the search yielded to the selected list. + * + * @param array $form_state + * The array of form state. + */ + protected function saveAll(&$form_state) { + $pids = $this->getAll($form_state); + $this->save($pids, $form_state); + } + + /** + * One of our '#action_submit' callbacks. + * + * Save the selected PIDs the search yielded to the selected list. + * + * @param array $form_state + * The array of form state. + */ + protected function saveSelected(&$form_state) { + $pids = $this->getSelected($form_state); + $this->save($pids, $form_state); + } + + /** + * Actually handle saving PIDs to a list. + * + * @param array $pids + * An array of PIDs to save to the given list. + * @param array $form_state + * The array of form state. + */ + protected function save($pids, &$form_state) { + $el = $form_state['complete form']['islandora_bookmark_save_fieldset']['fieldset']['save_options']; + $key = drupal_array_get_nested_value($form_state['values'], $el['#parents']); + + // Instantiate a Bookmark so we can do some comparisons before we add + // the selected. + module_load_include('inc', 'islandora_bookmark', 'includes/api'); + $bookmark_object = islandora_bookmark_get_bookmark_by_number($key); + + $pids_to_add = array_diff($pids, $bookmark_object->getPids()); + $skipped_pids = array_intersect($pids, $bookmark_object->getPids()); + + if (count($pids_to_add) > 0) { + try { + $bookmark_object->addPids($pids_to_add); + + drupal_set_message(format_plural( + count($pids_to_add), + 'The selected object has been added to "@listname".', + 'The selected objects have been added to "@listname".', + array( + '@listname' => $bookmark_object->bookmarkName, + ) + )); } - else { - call_user_func($form_state['islandora_bookmark_export'][$key], $all_pids); + catch (Exception $e) { + drupal_set_message($e->getMessage(), 'error'); } } - elseif ($key === 'default') { - drupal_set_message(t('Please select an option to export as!'), 'error'); - } - else { - if ($broke_count > 0) { - $broken_pids = format_plural($broke_count, 'The following object was unable to be exported as it does not exist: !broke_pids.', - 'The following objects were unable to be exported as they do not exist: !broke_pids.', - array('!broke_pids' => trim($broke_pids))); - drupal_set_message($broken_pids, 'error'); - } + if (count($skipped_pids) > 0) { + drupal_set_message(format_plural( + count($skipped_pids), + 'The selected object (!skipped_pids) was not added as it already exists in the bookmark list "@listname".', + 'The selected objects (!skipped_pids) were not added as they already exist in the bookmark list "@listname".', + array( + '@skipped_pids' => implode(' ', $skipped_pids), + '@listname' => $bookmark_object->bookmarkName, + ) + )); } } - elseif ($form_state['clicked_button']['#name'] === 'save_selected') { - $pids = array(); - $broke_pids = ''; - $broke_count = 0; - foreach ($form_state['values']['islandora_bookmark_table'] as $checkbox => $value) { - if ($value !== 0) { - if (islandora_object_load($value)) { - $pids[] = $value; - } - else { - $broke_pids .= $value . ' '; - $broke_count++; - } + + /** + * Go over any '#action_validate' functions on the clicked button. + */ + public function formValidate(&$form, &$form_state) { + $clicked_button = $form_state['clicked_button']; + if (isset($clicked_button['#action_validate'])) { + foreach ((array) $clicked_button['#action_validate'] as $validator) { + call_user_func(array($this, $validator), $form, $form_state); } } - if ($broke_count > 0) { - $broken_pids = format_plural($broke_count, 'The following object was unable to be bookmarked as it does not exist: !broke_pids.', - 'The following objects were unable to be bookmarked as they do not exist: !broke_pids.', - array('!broke_pids' => trim($broke_pids))); - drupal_set_message($broken_pids, 'error'); - } - $key = $form_state['values']['save_options']; - if ($key !== 'default') { - // Instantiate a Bookmark so we can do some comparisons before we add - // the selected. - $bookmark_object = islandora_bookmark_get_bookmark_by_number($key); - - $pids_to_add = array(); - $skipped_pids = ''; - $skipped_count = 0; - foreach ($pids as $pid) { - if (!in_array($pid, $bookmark_object->pidList)) { - $pids_to_add[] = $pid; - } - else { - $skipped_pids .= $pid . ' '; - $skipped_count++; - } - } + } - if (count($pids_to_add) > 0) { - try { - $bookmark_object->addPids($pids_to_add); - $plural_add = format_plural(count($pids), 'selected object has', 'selected objects have'); - - $plural_skipped = ''; - - if ($skipped_count > 0) { - $plural_skipped = format_plural($skipped_count, 'The selected object (!skipped_pids) was not added as it already exists in the bookmark list.', - 'The selected objects (!skipped_pids) were not added as they already exist in the bookmark list.', - array('@skipped_pids' => trim($skipped_pids))); - } - drupal_set_message(t('The @plural_add been added to @listname. @plural_skipped', - array( - '@plural_add' => $plural_add, - '@listname' => $bookmark_object->bookmarkName, - '@plural_skipped' => $plural_skipped, - ) - )); - } - catch (Exception $e) { - drupal_set_message($e->getMessage(), 'error'); - } - } - else { - $plural_skipped = format_plural($skipped_count, 'The selected object (!skipped_pids) was not added as it already exists in !listname.', - 'The selected objects (!skipped_pids) were not added as they already exist in !listname.', - array('!skipped_pids' => trim($skipped_pids), '!listname' => $bookmark_object->bookmarkName) - ); - drupal_set_message($plural_skipped, 'warning'); + /** + * Submit handler for the islandora_bookmark_results form. + * + * Go over any '#action_submit' functions on the clicked button. + * + * @param array $form + * The Drupal form definition. + * @param array $form_state + * The Drupal form state. + */ + public function formSubmit(&$form, &$form_state) { + $queries = drupal_get_query_parameters(); + + $form_state['redirect'] = array(request_path(), array('query' => $queries)); + + $clicked_button = $form_state['clicked_button']; + if (isset($clicked_button['#action_submit'])) { + $params = array(&$form_state); + foreach ((array) $clicked_button['#action_submit'] as $submit_function) { + call_user_func_array(array($this, $submit_function), $params); } } - elseif (count($pids) === 0) { - drupal_set_message(t('Please select at least one object.'), 'error'); - } - else { - drupal_set_message(t('Please select a bookmark list.'), 'error'); - } } - elseif ($form_state['clicked_button']['#name'] === 'save_all') { - $query_proc->executeQuery(); - $solr_results = $query_proc->islandoraSolrResult; + + /** + * Submission helper; gets the PIDs all results. + */ + protected function getAll(&$form_state) { + $pids = array(); + + // Helper anonymous function... Just get the PID. + $get_pid = function($result) { + $doc = $result['solr_doc']; + return $doc['PID']; + }; + + $qp = $this->islandoraSolrQueryProcessor; + $qp->solrLimit = 10000; + + // Handle the first set separately, so we can get the total number for our + // loop. + $qp->executeQuery(); + $solr_results = $qp->islandoraSolrResult; + $result_count = $solr_results['response']['numFound']; $object_results = $solr_results['response']['objects']; + $pids = array_map($get_pid, $object_results); - $all_pids = array(); - $broke_pids = ''; - $broke_count = 0; + for ($i = $qp->solrLimit; $i < $result_count; $i += $qp->solrLimit) { + $qp->executeQuery(); + $solr_results = $qp->islandoraSolrResult; + $object_results = $solr_results['response']['objects']; - foreach ($object_results as $object_result) { - $doc = $object_result['solr_doc']; - if (islandora_object_load($doc['PID'])) { - $all_pids[$doc['PID']] = $doc['PID']; - } - else { - $broke_pids .= $doc['PID'] . ' '; - $broke_count++; - } + $pids = array_merge( + $pids, + array_map($get_pid, $object_results) + ); } - if ($broke_count > 0) { - $broken_pids = format_plural($broke_count, 'The following object was unable to be added as it does not exist: !broke_pids.', - 'The following objects were unable to be added as they do not exist: !broke_pids', - array('!broke_pids' => trim($broke_pids))); - drupal_set_message($broken_pids, 'error'); - } - $key = $form_state['values']['save_options']; - - if ($key !== 'default') { - // Instantiate a Bookmark so we can do some comparisons before we add - // the selected. - $bookmark_object = islandora_bookmark_get_bookmark_by_number($key); - - $pids_to_add = array(); - $skipped_pids = ''; - $skipped_count = 0; - foreach ($all_pids as $pid) { - if (!in_array($pid, $bookmark_object->pidList)) { - $pids_to_add[] = $pid; - } - else { - $skipped_pids .= $pid . ' '; - $skipped_count++; - } - } - if (count($pids_to_add) > 0) { - try { - $bookmark_object->addPids($pids_to_add); - $plural_add = format_plural(count($pids_to_add), 'result has', 'results have'); - - $plural_skipped = ''; - - if ($skipped_count > 0) { - $plural_skipped = format_plural($skipped_count, 'The following object was not added as it already exists in the bookmark list: !skipped_pids.', - 'The following objects were not added as they already exist in the bookmark list: !skipped_pids.', - array('!skipped_pids' => trim($skipped_pids))); - } - drupal_set_message(t('The @plural_add been added to @listname. @plural_skipped', - array( - '@plural_add' => $plural_add, - '@listname' => $bookmark_object->bookmarkName, - '@plural_skipped' => $plural_skipped, - ) - )); - } - catch (Exception $e) { - drupal_set_message($e->getMessage(), 'error'); - } - } - else { - $plural_skipped = format_plural($skipped_count, 'The following PID was not added as it already exists in !listname: !skipped_pids.', - 'The following PIDs (!skipped_pids) were not added as they already exist in !listname: !skipped_pids.', - array('!skipped_pids' => trim($skipped_pids), '!listname' => $bookmark_object->bookmarkName) - ); - drupal_set_message($plural_skipped, 'warning'); - } - } - else { - drupal_set_message(t('Please select a bookmark list.'), 'error'); - } + return $pids; } - else { - $key = $form_state['values']['save_options']; - if ($key !== 'default') { - drupal_goto('islandora-bookmark/listid/' . $key); - } - else { - drupal_set_message(t('Please select a bookmark list.'), 'error'); + /** + * Submission helper; gets the PIDs of the selected results. + */ + protected function getSelected(&$form_state) { + $selected = array_filter($form_state['values']['islandora_bookmark_table']); + $functional = array_filter($selected, 'islandora_object_load'); + $broken = array_diff($selected, $functional); + if (!empty($broken)) { + watchdog('islandora_bookmark', 'Broken PIDs encountered: @pids', array( + '@pids' => implode(', ', $broken), + ), WATCHDOG_WARNING); } + + return $functional; } } + +/** + * Form building function. + * + * @param array $form + * The Drupal form definition. + * @param array $form_state + * The Drupal form state. + * @param IslandoraSolrResultsBookmark $results_class + * An object on which we can call buildForm() to get our form structure. + * @param array $object_results + * The object results from the Islandora Solr processor created in the + * printResults function. + * + * @return array + * An array containing form structure. + */ +function islandora_bookmark_results_form($form, &$form_state, $results_class, $object_results) { + return $results_class->formGet($form, $form_state, $object_results); +} + +/** + * Form validation handler. + */ +function islandora_bookmark_results_form_validate(array &$form, array &$form_state) { + $form_state['build_info']['args'][0]->formValidate($form, $form_state); +} + +/** + * Submit handler for the islandora_bookmark_results form. + * + * @param array $form + * The Drupal form definition. + * @param array $form_state + * The Drupal form state. + */ +function islandora_bookmark_results_form_submit(array &$form, array &$form_state) { + $form_state['build_info']['args'][0]->formSubmit($form, $form_state); +} diff --git a/islandora_bookmark.api.php b/islandora_bookmark.api.php index 5058748..464311e 100644 --- a/islandora_bookmark.api.php +++ b/islandora_bookmark.api.php @@ -15,6 +15,16 @@ function hook_islandora_bookmark_export_handler() { } +/** + * Hook to alter the options returned for exportation. + * + * @return array + * Returns an array with the name of the module and the function name to call + * to handle exportation. In the form of 'module name' => 'export function'. + */ +function hook_islandora_bookmark_export_handler_alter(&$output) { +} + /** * Hook to generate markup for displaying a Fedora object within a table. * @@ -43,3 +53,55 @@ function hook_CMODEL_PID_islandora_bookmark_object_markup($fedora_object, $objec */ function hook_islandora_bookmark_export_styles($option) { } + +/** + * Hook to change or add values to RSS fields. + * + * Sometimes you might want to alter fields for an rss item. + * + * @param AbstractObject $object + * The bookmarked object + * + * @return array + * Returns an array containing the additional changes to the rss item. + */ +function hook_islandora_bookmark_rss_item(AbstractObject $object) { + + // Create an associative array for the required elements + // for a valid bookmark RSS item. + $rss_item = array(); + // The title of the item. + $rss_item['title'] = 'Altered Title';; + // The link of the item. + $rss_item['link'] = 'Altered Link'; + // The description of the item. + $rss_item['description'] = 'Altered description'; + + // Set the source attribute. + $rss_item['items'] = array( + array( + 'key' => 'source', + 'value' => 'source value', 'attributes' => array('url' => 'url')), + ); + + // Return the RSS item. + return $rss_item; +} + +/** + * Get the mapping of types so we can instantiate different classes. + * + * The "bookmark" class just saves to the DB... We may want to add in other + * things to occur on different actions, like changing something on an object + * when we add an object to a list. + * + * @return array + * An associative array mapping the "type" column in the + * "islandora_bookmark_list_names" to a class to use to interact with the + * given list. + */ +function hook_islandora_bookmark_database_types() { + return array( + 'my_type' => 'my_awesome_bookmark_class', + ); +} diff --git a/islandora_bookmark.info b/islandora_bookmark.info index a077750..4cffb24 100644 --- a/islandora_bookmark.info +++ b/islandora_bookmark.info @@ -6,5 +6,7 @@ configure = admin/islandora/islandora-bookmark version = 7.x-dev core = 7.x files[] = includes/bookmark.inc +files[] = includes/bookmark_db.inc +files[] = includes/bookmark_session.inc files[] = includes/solr_results.inc stylesheets[all][] = css/islandora_bookmark.css diff --git a/islandora_bookmark.install b/islandora_bookmark.install index d91dd09..0238045 100644 --- a/islandora_bookmark.install +++ b/islandora_bookmark.install @@ -65,9 +65,27 @@ function islandora_bookmark_schema() { 'type' => 'int', 'length' => 11, ), + 'type' => array( + 'description' => 'The type of list which we are adding.', + 'type' => 'varchar', + 'length' => 255, + 'default' => 'bookmark', + ), ), 'primary key' => array('listid'), ); return $schema; } + +/** + * Adds a new field to bookmarks so we can define different types of lists. + */ +function islandora_bookmark_update_7100(&$sandbox) { + db_add_field('islandora_bookmark_list_names', 'type', array( + 'description' => 'The type of list which we are adding.', + 'type' => 'varchar', + 'length' => 255, + 'default' => 'bookmark', + )); +} diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 760bab6..8c385a2 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -26,12 +26,22 @@ function islandora_bookmark_menu() { 'weight' => 0, ); $items['islandora-bookmark/listid/%'] = array( - 'title' => 'Detailed list', + 'title callback' => 'islandora_bookmark_list_title', + 'title arguments' => array(2), 'page callback' => 'islandora_bookmark_detailed_forms', 'page arguments' => array(2), 'access callback' => 'islandora_bookmark_access', 'access arguments' => array(2), ); + $items['islandora-bookmark/listid/%/rss'] = array( + 'title' => 'RSS Feed', + 'type' => MENU_CALLBACK, + 'page callback' => 'islandora_bookmark_rss', + 'page arguments' => array(2), + 'access callback' => 'islandora_bookmark_access', + 'access arguments' => array(2), + 'file' => 'includes/bookmark_rss.inc', + ); $items['islandora-bookmark/add'] = array( 'title' => 'Add', 'type' => MENU_LOCAL_TASK, @@ -90,6 +100,7 @@ function islandora_bookmark_access($list_id) { } else { $bookmark_access = FALSE; + $owner_query = db_query('SELECT listowner FROM {islandora_bookmark_list_names} WHERE listid = :listid ORDER BY listid', array(':listid' => $list_id))->fetchObject(); if ($owner_query && $owner_query->listowner === $user->uid) { @@ -102,6 +113,10 @@ function islandora_bookmark_access($list_id) { $bookmark_access = TRUE; break; } + elseif ($record->uid == 0) { + $bookmark_access = TRUE; + break; + } } } } @@ -160,7 +175,15 @@ function islandora_bookmark_block_view($delta = '') { switch ($delta) { case 'islandora_bookmark': + // Make sure we're on the islandora/object route. if (arg(0) == 'islandora' AND arg(1) == 'object') { + // Don't do anything if we're beyond islandora/object/PID. + // That is, only show the bookmark block if we're on the standard view. + if (arg(3)) { + break; + } + + // Snag the PID and hook up the block. if (arg(2)) { $pid = arg(2); } @@ -193,6 +216,10 @@ function islandora_bookmark_permission() { 'title' => t('Share bookmarks'), 'description' => t('Share created bookmarks with other users.'), ), + 'export islandora bookmarks' => array( + 'title' => t('Export bookmarks'), + 'description' => t('Export bookmark lists.'), + ), ); } @@ -215,20 +242,13 @@ function islandora_bookmark_theme() { * they logged in. Iterates through the session and adds these records to the * database. */ -function islandora_bookmark_user_login(&$edit, &$account) { - global $user; - +function islandora_bookmark_user_login(&$edit, $account) { if (!empty($_SESSION['islandora_bookmark'])) { - foreach ($_SESSION['islandora_bookmark'] as $key => $value) { - $bookmark_session = unserialize($value); - $anon_index = array_search(0, $bookmark_session->userList, TRUE); - // Replace the anon who's logged in with their associated uid. - $bookmark_session->userId = $user->uid; - $bookmark_session->userList[$anon_index] = $user->uid; - $bookmark_session->bookmarkSession = FALSE; - $bookmark_session->createList(); - unset($_SESSION['islandora_bookmark'][$key]); + foreach ($_SESSION['islandora_bookmark'] as $bookmark_session) { + $persistent_bookmark = BookmarkDatabase::createNewList($bookmark_session->bookmarkName, 'bookmark', $account); + $persistent_bookmark->addPids($bookmark_session->getPids()); } + unset($_SESSION['islandora_bookmark']); } } @@ -236,7 +256,7 @@ function islandora_bookmark_user_login(&$edit, &$account) { * Callback function for removing a Bookmark confirm form. */ function islandora_bookmark_delete_bookmark(array $form, array &$form_state) { - drupal_goto('islandora-bookmark/delete-bookmark/' . $form_state['bookmark_object']->bookmarkId); + drupal_goto('islandora-bookmark/delete-bookmark/' . $form_state['bookmark_object']->getId()); } /** @@ -254,7 +274,9 @@ function islandora_bookmark_delete_bookmark_confirm(array $form, array &$form_st module_load_include('inc', 'islandora_bookmark', 'includes/api'); $bookmark_object = islandora_bookmark_get_bookmark_by_number($list_id); $form_state['bookmark_object'] = $bookmark_object; - return confirm_form($form, t('Are you sure you want to delete the bookmark list %bookmark_name?', array('%bookmark_name' => $bookmark_object->bookmarkName)), 'islandora-bookmark/listid/' . $list_id, t('This action cannot be undone.'), t('Delete'), t('Cancel')); + return confirm_form($form, t('Are you sure you want to delete the @type list %bookmark_name?', array( + '%bookmark_name' => $bookmark_object->bookmarkName, + '@type' => variable_get('islandora_bookmark_type', 'bookmark'))), 'islandora-bookmark/listid/' . $list_id, t('This action cannot be undone.'), t('Delete'), t('Cancel')); } /** @@ -286,7 +308,7 @@ function islandora_bookmark_delete_bookmark_confirm_submit(array $form, array &$ * Callback function for removing self confirm form. */ function islandora_bookmark_remove_self(array $form, array &$form_state) { - drupal_goto('islandora-bookmark/remove-self/' . $form_state['bookmark_object']->bookmarkId); + drupal_goto('islandora-bookmark/remove-self/' . $form_state['build_info']['args'][0]); } /** @@ -305,7 +327,9 @@ function islandora_bookmark_remove_self_confirm(array $form, array &$form_state, $bookmark_object = islandora_bookmark_get_bookmark_by_number($list_id); $form_state['bookmark_object'] = $bookmark_object; - return confirm_form($form, t('Are you sure you want to remove yourself from the bookmark %bookmark_name ?', array('%bookmark_name' => $bookmark_object->bookmarkName)), 'islandora-bookmark/listid/' . $list_id, t('This action cannot be undone.'), + return confirm_form($form, t('Are you sure you want to remove yourself from the @type %bookmark_name ?', array( + '%bookmark_name' => $bookmark_object->bookmarkName, + '@type' => variable_get('islandora_bookmark_type', 'bookmark'))), 'islandora-bookmark/listid/' . $list_id, t('This action cannot be undone.'), t('Delete'), t('Cancel')); } @@ -351,7 +375,7 @@ function islandora_bookmark_update_bookmark(array $form, array &$form_state) { $newlist_name = $form_state['input']['bookmarks']['management']['listname']; $bookmark_object->changeListName(trim($newlist_name)); - drupal_set_message(t('Bookmark list settings updated.')); + drupal_set_message(t('List settings updated.')); } /** @@ -373,7 +397,7 @@ function islandora_bookmark_generate_markup($pid, $object_url_info = NULL) { // Set URL info if non provided. if (is_null($object_url_info)) { $object_url_info = array( - 'path' => "islandora/$pid", + 'path' => "islandora/object/$pid", 'params' => array(), 'markup' => $fedora_object->label ? $fedora_object->label : $fedora_object->pid, ); @@ -399,7 +423,7 @@ function islandora_bookmark_generate_markup($pid, $object_url_info = NULL) { } else { // Handle when we can't access a Fedora object. - watchdog('islandora_bookmark', "Tried to render markup for $pid, but was unable to load it."); + watchdog('islandora_bookmark', "Tried to render markup for @pid, but was unable to load it.", array("@pid" => $pid)); $output = '
' . t('Unable to load object @pid.', array('@pid' => $pid)) . '
'; } @@ -407,16 +431,15 @@ function islandora_bookmark_generate_markup($pid, $object_url_info = NULL) { return $output; } + /** * Themes a Fedora object. * * @param array $object_url_info - * Contains information to build an anchor tag ie. - * array( - * 'path' => "islandora/$pid", - * 'params' => array(), - * 'markup' => 'a label', - * ); + * An associative array contains information to build an anchor tag. + * - path: A string containing the path/ + * - params: An array of paramaters to be passed. + * - markup: A string containing the label for the anchor tag. * * @return array * an associative array containing the output from theme @@ -459,368 +482,23 @@ function islandora_bookmark_detailed_forms($list_id) { * The Drupal form definition. * @param array $form_state * The Drupal form state. + * @param mixed $list_id + * The identifier for the current list. * * @return array * The Drupal form definition. */ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list_id) { - global $user; - module_load_include('inc', 'islandora_bookmark', 'includes/api'); - module_load_include('inc', 'islandora', 'includes/utilities'); - drupal_add_css(drupal_get_path('module', 'islandora_bookmark') . '/css/islandora_bookmark.css'); - $bookmark = islandora_bookmark_get_bookmark_by_number($list_id); - - if (module_exists('citation_exporter')) { - if (CitationExporter::ReadyToExport()) { - $citation_export_head = array( - '#type' => 'html_tag', - '#tag' => 'meta', - '#attributes' => array( - 'http-equiv' => 'refresh', - 'content' => '1.0;' . url(MENU_CITATION_EXPORTER_EXPORT, array('absolute' => TRUE)), - ), - ); - drupal_add_html_head($citation_export_head, 'citation_exporter_head'); - } - } - - $form = array( - '#tree' => TRUE, - '#prefix' => '
', - '#suffix' => '
', - ); - - $form['bookmarks'] = array( - 'fieldset' => array( - '#type' => 'fieldset', - '#title' => check_plain($bookmark->bookmarkName), - ), - ); - - $rows = array(); - - // Do pager calcuations to limit the number of objects displayed per page. - $num_eles = variable_get('islandora_bookmark_detailed_page_elements', 10); - $all_pids = $bookmark->pidList; - $num_pids = count($all_pids); - - // Incase we get a page request > than the pages, Drupal will - // show the last page automatically, need to reflect the pids populated - // in this fashion. - $calc_pages = ceil($num_pids / $num_eles) - 1; - if (!empty($_GET['page'])) { - $page = $_GET['page']; - } - if (empty($page)) { - $page = 0; - $offset = 0; - } - else { - if ($page > $calc_pages) { - $page = $calc_pages; - } - $offset = $page * $num_eles; - } - - $pids = array_slice($all_pids, $offset, $num_eles); - - // Loop the pids and determine how to display the markup. - foreach ($pids as $pid) { - $rows[$pid] = array( - 'markup' => islandora_bookmark_generate_markup($pid), - ); - } - pager_default_initialize(count($all_pids), $num_eles); - $form['bookmarks']['fieldset']['pager'] = array( - '#markup' => theme('pager'), - '#weight' => -1, - ); - - $header = array('markup' => array('data' => t('Label'))); - - $buttons = array(); - $form_state['islandora_bookmark_current_page'] = $page; - $form_state['islandora_bookmark_pids_on_page'] = count($pids); - if ($bookmark->bookmarkOwner === $user->uid) { - $header['remove'] = array('data' => t('Operations')); - foreach ($pids as $key => $pid) { - $remove_button = array( - '#type' => 'submit', - '#name' => 'remove_pid_' . $pid, - '#id' => 'remove_pid_' . $pid, - '#button_type' => 'submit', - '#pid' => $pid, - '#value' => t('Remove'), - - ); - $rows[$pid]['remove'] = array( - 'data' => drupal_render($remove_button), - 'class' => 'operations', - ); - } - if (count($pids) != 0) { - $form['bookmarks']['fieldset']['remove_select'] = array( - '#type' => 'submit', - '#value' => t('Remove selected'), - '#name' => 'islandora_bookmark_remove_selected', - ); - $form['bookmarks']['fieldset']['remove_all'] = array( - '#type' => 'submit', - '#value' => t('Remove all'), - '#name' => 'islandora_bookmark_remove_all', - ); - } - } - if (count($rows)) { - $form['bookmarks']['fieldset']['table'] = array( - '#header' => $header, - '#type' => 'tableselect', - '#options' => $rows, - '#empty' => t('Bookmark list is empty.'), - '#weight' => -2, - ); - } - else { - $form['bookmarks']['fieldset']['table'] = array( - '#markup' => t('Bookmark list is empty.'), - ); - } - - if (count($pids) != 0) { - $form['bookmarks']['fieldset']['format_opts'] = array( - '#weight' => 4, - ); - $export_styles = array(); - $export = islandora_bookmark_get_export_options(); - $export_options['default'] = t('- Export type -'); - - foreach ($export as $module_name => $export_array) { - foreach ($export_array as $name => $export_func) { - $export_options[t($module_name)][$name] = $name; - $form_state['islandora_bookmark_export'][$name] = $export_func; - $export_styles[$name] = islandora_bookmark_get_export_format_options($name); - } - } - foreach ($export_styles as $key => $value) { - if (count($export_styles[$key])) { - $form['bookmarks']['fieldset']['format_opts']['export_styles'][$key] = array( - '#type' => 'select', - '#title' => t('Style'), - '#options' => $export_styles[$key], - '#weight' => 4, - '#name' => 'islandora_bookmark_export_styles_' . $key, - '#states' => array( - 'visible' => array( - ':input[name="islandora_bookmark_export_options_select"]' => array('value' => $key), - ), - ), - ); - } - } - - $form['bookmarks']['fieldset']['export_title'] = array( - '#markup' => '

' . t('Export') . '

', - '#weight' => 3, - ); - $form['bookmarks']['fieldset']['export_options'] = array( - '#type' => 'select', - '#title' => t('Format'), - '#options' => $export_options, - '#weight' => 4, - '#name' => 'islandora_bookmark_export_options_select', - ); - $form['bookmarks']['fieldset']['format_opts']['export_selected_submit'] = array( - '#type' => 'submit', - '#value' => t('Export selected bookmarks'), - '#name' => 'export_selected', - '#weight' => 5, - ); - $form['bookmarks']['fieldset']['format_opts']['export_all_submit'] = array( - '#type' => 'submit', - '#value' => t('Export all bookmarks'), - '#name' => 'export_all', - '#weight' => 6, - ); - } - - // Share. - if ($bookmark->bookmarkOwner === $user->uid) { - if (user_access('share islandora bookmarks')) { - // Triggered element (Add item or remove). - if (isset($form_state['triggering_element'])) { - // Add. - if ($form_state['triggering_element']['#button_id'] == 'islandora-bookmark-add-user') { - $add_user = $form_state['input']['bookmarks']['users']['listusers']; - if (drupal_strlen($add_user)) { - if ($add_user == 0) { - $output_user = variable_get('anonymous', t('Anonymous')); - } - else { - $output_user = user_load($add_user)->name; - } - drupal_set_message(t('The user @adduser has been added to @listname', - array( - '@listname' => $bookmark->bookmarkName, - '@adduser' => $output_user, - ) - )); - $bookmark->addUsers(array($add_user)); - } - else { - drupal_set_message(t("Please select a user to add to @listname.", - array( - '@listname' => $bookmark->bookmarkName, - ) - ), 'error'); - } - } - // Remove. - if ($form_state['triggering_element']['#button_id'] == 'islandora-bookmark-remove-user') { - $row = $form_state['triggering_element']['#row']; - $remove_user = $form_state['values']['bookmarks']['users'][$row]['user']; - if ($remove_user == 0) { - $output_user = variable_get('anonymous', t('Anonymous')); - } - else { - $output_user = user_load($remove_user)->name; - } - drupal_set_message(t('The user @username has been removed from the list @listname.', - array( - '@listname' => $bookmark->bookmarkName, - '@username' => $output_user, - ) - )); - $bookmark->removeUsers(array($remove_user)); - } - } - - $fku = array(); - $rids = array_keys($user->roles); - // Anonymous users can't share lists with other users. - if (!in_array('1', $rids)) { - $form['bookmarks']['users'] = array( - '#type' => 'fieldset', - '#collapsible' => TRUE, - '#collapsed' => FALSE, - '#title' => t('Share'), - '#prefix' => '
', - '#suffix' => '
', - ); - - if (count($bookmark->userList) == 1) { - $fku['empty'] = array( - '#type' => 'item', - '#markup' => t('Bookmark list is not shared with other users.'), - ); - } - else { - $fku['title'] = array( - '#markup' => '

' . t('Shared with') . ':

', - ); - // Remove the user from the populated shared users. - $key = array_search($user->uid, $bookmark->userList); - $bookmark_users = $bookmark->userList; - unset($bookmark_users[$key]); - - foreach ($bookmark_users as $itr => $uid) { - $tempuser = user_load($uid); - if ($tempuser->uid == 0) { - $user_output = variable_get('anonymous', t('Anonymous')); - } - else { - $user_output = $tempuser->name; - } - if ($tempuser->uid != $user->uid) { - $fku[$itr] = array( - '#prefix' => '
', - '#suffix' => '
', - 'label' => array( - '#type' => 'item', - '#markup' => $user_output, - ), - 'user' => array( - '#type' => 'value', - '#value' => $uid, - ), - 'list-id' => array( - '#type' => 'value', - '#value' => $bookmark->bookmarkId, - ), - 'remove' => array( - '#type' => 'button', - '#value' => t('Remove'), - '#name' => 'islandora_bookmark_remove_user_' . $itr, - '#row' => $itr, - '#button_id' => 'islandora-bookmark-remove-user', - '#ajax' => array( - 'event' => 'click', - 'callback' => 'islandora_bookmark_remove_user', - 'wrapper' => 'islandora-bookmark-detailed-overview-users', - 'method' => 'replace', - ), - ), - ); - } - } - } - - $selectoptions = array("" => t("- Select user -")); - - // Get all users for use in select for forms. - $result = db_query("SELECT uid, name FROM {users} ORDER BY uid"); - - foreach ($result as $record) { - if ($record->uid != $user->uid) { - $resultname = $record->name; - if ($record->uid == 0) { - $resultname = variable_get('anonymous', t('Anonymous')); - } - $options[$record->uid] = array( - 'uid' => $record->uid, - 'name' => $resultname, - ); - } - } - foreach ($options as $index => $item) { - if (!in_array($index, $bookmark->userList)) { - $selectoptions[$index] = $item['uid'] . ': ' . $item['name']; - } - } - $fku['listusers'] = array( - '#type' => 'select', - '#default value' => t("- Select user -"), - '#options' => $selectoptions, - '#width' => 20, - '#prefix' => '
', - ); - $fku['listadd'] = array( - '#type' => 'button', - '#value' => t('Share'), - '#button_id' => 'islandora-bookmark-add-user', - '#suffix' => '
', - '#ajax' => array( - 'event' => 'click', - 'callback' => 'islandora_bookmark_add_user', - 'wrapper' => 'islandora-bookmark-detailed-overview-users', - 'method' => 'replace', - ), - ); - $form['bookmarks']['users'] += $fku; - } - } - } - else { - if ($user->uid != 0) { - $form['bookmarks']['remove_self'] = array( - '#type' => 'submit', - '#value' => t('Remove yourself from this list'), - '#submit' => array('islandora_bookmark_remove_self'), - ); - } - } - $form_state['bookmark_object'] = $bookmark; + $bookmark = Bookmark::getList($list_id); + return $bookmark->formGet($form, $form_state); +} - return $form; +/** + * Form validation handler. + */ +function islandora_bookmark_detailed_form_validate(&$form, &$form_state) { + $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); + $bookmark->formValidate($form, $form_state); } /** @@ -835,193 +513,15 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list * The Drupal form state. */ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state) { - module_load_include('inc', 'islandora_bookmark', 'includes/api'); - $queries = $_GET; - unset($queries['q']); - $form_state['redirect'] = array(request_path(), array('query' => $queries)); - - foreach ($_POST as $key => $value) { - preg_match('/remove_pid_(.*)/', $key, $match); - if ($match) { - break; - } - } - - if ($match) { - $bookmark = $form_state['bookmark_object']; - $bookmark->removePids(array($match[1])); - $removed_object = islandora_object_load($match[1]); - - drupal_set_message(t('The bookmark @objectname has been removed from @listname.', - array( - '@listname' => $bookmark->bookmarkName, - '@objectname' => $removed_object->label, - ) - )); - $current_page = $form_state['islandora_bookmark_current_page']; - $current_pids = $form_state['islandora_bookmark_pids_on_page']; - - if (($current_pids - 1) != 0) { - $query = array( - 'query' => array( - 'page' => $current_page, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId, array('query' => $query)); - drupal_goto($destination['path'], $query); - } - elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); - drupal_goto($destination['path']); - } - else { - $query = array( - 'query' => array( - 'page' => $current_page - 1, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); - drupal_goto($destination['path'], $query); - } - } - // Export selected only. - elseif ($form_state['clicked_button']['#name'] === 'export_selected') { - $pids = array(); - $broke_pids = ''; - $broke_count = 0; - foreach ($form_state['values']['bookmarks']['fieldset']['table'] as $checkbox => $value) { - if ($value !== 0) { - // Make sure we can actually access the object. - if (islandora_object_load($value)) { - $pids[] = $value; - } - else { - $broke_pids .= $value . ' '; - $broke_count++; - } - } - } - - $key = $form_state['input']['islandora_bookmark_export_options_select']; - - if (count($pids) > 0 && $key !== 'default') { - if (array_key_exists('islandora_bookmark_export_styles_' . $key, $form_state['input'])) { - call_user_func($form_state['islandora_bookmark_export'][$key], $pids, $form_state['input']['islandora_bookmark_export_styles_' . $key]); - } - else { - call_user_func($form_state['islandora_bookmark_export'][$key], $pids); - } - } - elseif (count($pids) === 0 && $broke_count === 0) { - drupal_set_message(t('Please select at least one bookmark.'), 'error'); - } - elseif ($key === 'default') { - drupal_set_message(t('Please select an export type.'), 'error'); - } - else { - if ($broke_count > 0) { - $broken_pids = format_plural($broke_count, 'The following object could not be exported as it does not exist: !broke_pids.', - 'The following objects could not be exported as they do no exist: !broke_pids.', - array('!broke_pids' => trim($broke_pids))); - drupal_set_message($broken_pids, 'error'); - } - } - } - elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_selected') { - $pids = array(); - foreach ($form_state['values']['bookmarks']['fieldset']['table'] as $checkbox => $value) { - if ($value !== 0) { - $pids[] = $value; - } - } - if (count($pids) > 0) { - $bookmark = $form_state['bookmark_object']; - $bookmark->removePids($pids); - drupal_set_message(t('The selected bookmarks have been removed from @listname.', - array( - '@listname' => $bookmark->bookmarkName, - ) - )); - $current_pids = $form_state['islandora_bookmark_pids_on_page']; - $current_page = $form_state['islandora_bookmark_current_page']; - - // Still pids left on the page. - if ((count($pids) - $current_pids) != 0) { - $query = array( - 'query' => array( - 'page' => $current_page, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId, array('query' => $query)); - drupal_goto($destination['path'], $query); - } - elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); - drupal_goto($destination['path']); - } - else { - $query = array( - 'query' => array( - 'page' => $current_page - 1, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); - drupal_goto($destination['path'], $query); - } - } - else { - drupal_set_message(t('Please select at least one bookmark.'), 'error'); - } - } - elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_all') { - $bookmark = $form_state['bookmark_object']; - $remove_count = count($bookmark->pidList); - $bookmark->removeAllPids(); - - drupal_set_message(t('All PIDs have been removed from the @remove_list list.', - array( - '@remove_list' => $bookmark->bookmarkName, - ))); - } - // Export all pids. - else { - $bookmark = $form_state['bookmark_object']; - $pids = $bookmark->pidList; - $all_pids = array(); - $broke_pids = ''; - $broke_count = 0; - - foreach ($pids as $pid) { - if (islandora_object_load($pid)) { - $all_pids[] = $pid; - } - else { - $broke_pids .= $pid . ' '; - $broke_count++; - } - } + $form_state['redirect'] = array( + request_path(), + array( + 'query' => drupal_get_query_parameters(), + ), + ); - $key = $form_state['input']['islandora_bookmark_export_options_select']; - if ($key !== 'default' && count($all_pids) > 0) { - if (array_key_exists('islandora_bookmark_export_styles_' . $key, $form_state['input'])) { - call_user_func($form_state['islandora_bookmark_export'][$key], $all_pids, $form_state['input']['islandora_bookmark_export_styles_' . $key]); - } - else { - call_user_func($form_state['islandora_bookmark_export'][$key], $all_pids); - } - } - elseif ($key === 'default') { - drupal_set_message(t('Please select an export type.'), 'error'); - } - else { - if ($broke_count > 0) { - $broken_pids = format_plural($broke_count, 'The following object was unable to be exported as it does not exist: !broke_pids.', - 'The following objects were unable to be exported as they do no exist: !broke_pids.', - array('!broke_pids' => trim($broke_pids))); - drupal_set_message($broken_pids, 'error'); - } - } - } + $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); + $bookmark->formSubmit($form, $form_state); } /** @@ -1042,9 +542,9 @@ function islandora_bookmark_detailed_form_manage(array $form, array &$form_state module_load_include('inc', 'islandora_bookmark', 'includes/api'); module_load_include('inc', 'islandora', 'includes/utilities'); drupal_add_css(drupal_get_path('module', 'islandora_bookmark') . '/css/islandora_bookmark.css'); - $bookmark = islandora_bookmark_get_bookmark_by_number($list_id); $form = array('#tree' => TRUE); + $bookmark = Bookmark::getList($list_id); // Manage. if ($bookmark->bookmarkOwner === $user->uid) { @@ -1059,7 +559,7 @@ function islandora_bookmark_detailed_form_manage(array $form, array &$form_state '#type' => 'textfield', '#title' => t('List name'), '#required' => TRUE, - '#description' => t('A descriptive label for the bookmark list.'), + '#description' => t('A descriptive label for the @type list.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), '#size' => 30, '#default_value' => $bookmark->bookmarkName, ); @@ -1078,7 +578,7 @@ function islandora_bookmark_detailed_form_manage(array $form, array &$form_state $form['bookmarks']['management']['list-id'] = array( '#type' => 'value', - '#value' => $bookmark->bookmarkId, + '#value' => $bookmark->getId(), ); } $form_state['bookmark_object'] = $bookmark; @@ -1128,11 +628,11 @@ function islandora_bookmark_fedora_repository_object_form(array $form, array &$f '#suffix' => '', ); - if (count($containing_lists)) { + if (count($containing_lists) > 0) { $links = array(); foreach ($containing_lists as $key => $value) { $bookmark_object = islandora_bookmark_get_bookmark_by_number($value); - $links[] = l($bookmark_object->bookmarkName, 'islandora-bookmark/listid/' . $bookmark_object->bookmarkId); + $links[] = l($bookmark_object->bookmarkName, 'islandora-bookmark/listid/' . $bookmark_object->getId()); } $form['islandora_bookmark']['lists'] = array( @@ -1152,22 +652,22 @@ function islandora_bookmark_fedora_repository_object_form(array $form, array &$f else { $owned_lists = array(); foreach ($_SESSION['islandora_bookmark'] as $value) { - $temp_list = unserialize($value); - $owned_lists[$temp_list->bookmarkId] = $temp_list->bookmarkName; + $temp_list = $value; + $owned_lists[$temp_list->getId()] = $temp_list->bookmarkName; } } if (count($owned_lists)) { $temp_options = array_diff_key($owned_lists, $containing_lists); if (count($temp_options)) { - $options['default'] = t('- Select bookmark list -'); + $options['default'] = t('- Select @type list -', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))); foreach ($temp_options as $key => $value) { $options[$key] = $value; } if (user_access('use islandora_bookmark')) { if (!count($containing_lists)) { $form['islandora_bookmark']['title'] = array( - '#markup' => '

' . t('Bookmark') . ':

', + '#markup' => '

' . t('@type', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))) . ':

', ); } $form['islandora_bookmark']['add_bookmarks'] = array( @@ -1176,7 +676,7 @@ function islandora_bookmark_fedora_repository_object_form(array $form, array &$f ); $form['islandora_bookmark']['add_button'] = array( '#type' => 'submit', - '#value' => t('Add bookmark'), + '#value' => t('Add @type', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), '#ajax' => array( 'event' => 'click', 'callback' => 'islandora_bookmark_add_pid', @@ -1192,6 +692,20 @@ function islandora_bookmark_fedora_repository_object_form(array $form, array &$f return $form; } +/** + * Form validation callback. + */ +function islandora_bookmark_fedora_repository_object_form_validate(&$form, &$form_state) { + $key = $form_state['values']['add_bookmarks']; + if ($key == 'default') { + $pid = $form_state['islandora_bookmark_pid']; + $object = islandora_object_load($pid); + form_set_error('islandora_bookmark][add_bookmarks', t('Please select a list to bookmark @label.', array( + '@label' => $object->label, + ))); + } +} + /** * Invokes islandora_bookmark_export_formats(). * @@ -1214,6 +728,7 @@ function islandora_bookmark_get_export_format_options($option) { */ function islandora_bookmark_get_export_options() { $output = module_invoke_all('islandora_bookmark_export_handler'); + drupal_alter('islandora_bookmark_export_handler', $output); return $output; } @@ -1303,7 +818,7 @@ function islandora_bookmark_overview_form(array $form, array &$form_state) { $form['owned_bookmarks'] = array( 'fieldset' => array( '#type' => 'fieldset', - '#title' => t('My bookmarks'), + '#title' => t('@type', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')) . ' lists owned')), 'pager' => array( '#markup' => theme('pager', array('element' => 0)), '#weight' => 5, @@ -1317,7 +832,7 @@ function islandora_bookmark_overview_form(array $form, array &$form_state) { else { $form['owned_bookmarks']['fieldset']['empty'] = array( '#type' => 'item', - '#markup' => t('You have no bookmarks.'), + '#markup' => t('You have no @type lists.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), ); } } @@ -1330,7 +845,7 @@ function islandora_bookmark_overview_form(array $form, array &$form_state) { $form['shared_bookmarks'] = array( 'fieldset' => array( '#type' => 'fieldset', - '#title' => t('Bookmarks shared with me'), + '#title' => t('@type shared with me', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')) . ' lists')), 'pager' => array( '#markup' => theme('pager', array('element' => 1)), '#weight' => 5, @@ -1344,7 +859,7 @@ function islandora_bookmark_overview_form(array $form, array &$form_state) { else { $form['shared_bookmarks']['fieldset']['empty'] = array( '#type' => 'item', - '#markup' => t('No bookmarks are currently being shared with you.'), + '#markup' => t('No @type lists are currently being shared with you.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), ); } @@ -1355,12 +870,6 @@ function islandora_bookmark_overview_form(array $form, array &$form_state) { * Creates a table for the Bookmark overview page. */ function islandora_bookmark_form_overview_table(array $bookmark_objs) { - - $bookmark_ids = array(); - foreach ($bookmark_objs as $bookmark) { - $bookmark_ids[] = $bookmark->bookmarkId; - } - $headers = array( 'bookmark_name' => t('List name'), 'bookmark_owner' => t('Owner'), @@ -1369,14 +878,15 @@ function islandora_bookmark_form_overview_table(array $bookmark_objs) { foreach ($bookmark_objs as $bookmark) { $owner = user_load($bookmark->bookmarkOwner); if ($bookmark->bookmarkOwner == 0) { - $owner_output = variable_get('anonymous', t('Anonymous')); + $owner_output = t('Everyone'); } else { $owner_output = $owner->name; } - $rows[$bookmark->bookmarkId] = array( - 'bookmark_name' => l($bookmark->bookmarkName, "islandora-bookmark/listid/$bookmark->bookmarkId"), + $id = $bookmark->getId(); + $rows[$id] = array( + 'bookmark_name' => l($bookmark->bookmarkName, "islandora-bookmark/listid/$id"), 'bookmark_owner' => $owner_output, ); } @@ -1409,7 +919,7 @@ function islandora_bookmark_add_form(array $form, array &$form_state) { $form['listname'] = array( '#title' => t('List name'), '#required' => TRUE, - '#description' => t('A descriptive label for the bookmark list.'), + '#description' => t('A descriptive label for the @type list.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), '#type' => 'textfield', '#size' => 30, ); @@ -1438,23 +948,10 @@ function islandora_bookmark_add_form_submit(array $form, array &$form_state) { $bookmark_name = $form_state['values']['listname']; - // Create a new list. - $params = array( - 'retrieve' => FALSE, - 'user_id' => $user->uid, - 'bookmark_owner' => $user->uid, - 'bookmark_name' => trim($bookmark_name), - 'bookmark_users' => array($user->uid), - ); - - if ($user->uid == 0) { - $params['bookmark_session'] = TRUE; - } - - $temp_list = new Bookmark($params); + $temp_list = Bookmark::createNewList($bookmark_name); $saved_list = NULL; if ($user->uid == 0) { - $saved_list = 'Log into an account to save bookmark lists; lists created anonymously will not be saved.'; + $saved_list = 'Log into an account to save ' . variable_get('islandora_bookmark_type', 'bookmark') . ' lists; lists created anonymously will not be saved.'; } drupal_set_message(t('The new list @listname was created. @saved', array( @@ -1478,11 +975,14 @@ function islandora_bookmark_add_form_submit(array $form, array &$form_state) { * The Drupal form definition. */ function islandora_bookmark_add_pid(array $form, array &$form_state) { - module_load_include('inc', 'islandora_bookmark', 'includes/api'); - $key = $form_state['values']['add_bookmarks']; - $pid = $form_state['islandora_bookmark_pid']; - $object = islandora_object_load($pid); - if ($key !== 'default') { + $errors = form_get_errors(); + + if (count($errors) === 0) { + module_load_include('inc', 'islandora_bookmark', 'includes/api'); + $key = $form_state['values']['add_bookmarks']; + $pid = $form_state['islandora_bookmark_pid']; + $object = islandora_object_load($pid); + $bookmark_object = islandora_bookmark_get_bookmark_by_number($key); try { @@ -1497,11 +997,12 @@ function islandora_bookmark_add_pid(array $form, array &$form_state) { catch (Exception $e) { drupal_set_message($e->getMessage(), 'error'); } + + return drupal_rebuild_form('islandora_bookmark_fedora_repository_object_form', $form_state, $form); } else { - drupal_set_message(t('Please select a list to bookmark @label.', array('@label' => $object->label)), 'error'); + return $form; } - return drupal_rebuild_form('islandora_bookmark_fedora_repository_object_form', $form_state, $form); } /** @@ -1549,3 +1050,41 @@ function islandora_bookmark_islandora_solr_primary_display() { ), ); } + +/** + * Title callback for bookmark list. + * + * Changes the drupal title to be the bookmarks title. + * + * @param string $list_id + * The bookmark list id. + * + * @return string + * The the bookmark name. + */ +function islandora_bookmark_list_title($list_id) { + module_load_include('inc', 'islandora_bookmark', 'includes/api'); + + $bookmark = islandora_bookmark_get_bookmark_by_number($list_id); + check_plain($bookmark->bookmarkName); + + return check_plain($bookmark->bookmarkName); +} + +/** + * Implements hook_islandora_object_purged(). + */ +function islandora_bookmark_islandora_object_purged($pid) { + db_delete('islandora_bookmark_list_pids') + ->condition('pidid', $pid) + ->execute(); +} + +/** + * Implements hook_islandora_bookmark_database_types(). + */ +function islandora_bookmark_islandora_bookmark_database_types() { + return array( + 'bookmark' => 'BookmarkDatabase', + ); +}