From 7484850b7cec8459f7c8415c799f641392895ad7 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Tue, 7 May 2013 12:48:24 -0300 Subject: [PATCH 01/57] Allow for configurable display variable for different implementations. --- includes/admin.form.inc | 7 ++++++ includes/solr_results.inc | 4 ++-- islandora_bookmark.module | 46 ++++++++++++++++++++------------------- 3 files changed, 33 insertions(+), 24 deletions(-) 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/solr_results.inc b/includes/solr_results.inc index 3bd77b0..94039d3 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -164,7 +164,7 @@ function islandora_bookmark_results_form(array $form, array &$form_state, $objec 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; @@ -173,7 +173,7 @@ function islandora_bookmark_results_form(array $form, array &$form_state, $objec $form['islandora_bookmark_save_fieldset'] = array( 'fieldset' => array( '#type' => 'fieldset', - '#title' => t('Bookmark'), + '#title' => t('@type', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), '#weight' => 0, ), ); diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 760bab6..876167a 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -254,7 +254,7 @@ 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')); } /** @@ -305,7 +305,7 @@ 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 +351,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.')); } /** @@ -576,13 +576,13 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list '#header' => $header, '#type' => 'tableselect', '#options' => $rows, - '#empty' => t('Bookmark list is empty.'), + '#empty' => t('@type list is empty.', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), '#weight' => -2, ); } else { $form['bookmarks']['fieldset']['table'] = array( - '#markup' => t('Bookmark list is empty.'), + '#markup' => t('@type list is empty.', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), ); } @@ -631,13 +631,13 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list ); $form['bookmarks']['fieldset']['format_opts']['export_selected_submit'] = array( '#type' => 'submit', - '#value' => t('Export selected bookmarks'), + '#value' => t('Export selected @type', array('@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's')), '#name' => 'export_selected', '#weight' => 5, ); $form['bookmarks']['fieldset']['format_opts']['export_all_submit'] = array( '#type' => 'submit', - '#value' => t('Export all bookmarks'), + '#value' => t('Export all @type', array('@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's')), '#name' => 'export_all', '#weight' => 6, ); @@ -710,7 +710,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list if (count($bookmark->userList) == 1) { $fku['empty'] = array( '#type' => 'item', - '#markup' => t('Bookmark list is not shared with other users.'), + '#markup' => t('@type list is not shared with other users.', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), ); } else { @@ -852,8 +852,9 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state $bookmark->removePids(array($match[1])); $removed_object = islandora_object_load($match[1]); - drupal_set_message(t('The bookmark @objectname has been removed from @listname.', + drupal_set_message(t('The @type @objectname has been removed from @listname.', array( + '@type' => variable_get('islandora_bookmark_type', 'bookmark'), '@listname' => $bookmark->bookmarkName, '@objectname' => $removed_object->label, ) @@ -913,7 +914,7 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } } elseif (count($pids) === 0 && $broke_count === 0) { - drupal_set_message(t('Please select at least one bookmark.'), 'error'); + drupal_set_message(t('Please select at least one @type.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), 'error'); } elseif ($key === 'default') { drupal_set_message(t('Please select an export type.'), 'error'); @@ -937,9 +938,10 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state if (count($pids) > 0) { $bookmark = $form_state['bookmark_object']; $bookmark->removePids($pids); - drupal_set_message(t('The selected bookmarks have been removed from @listname.', + drupal_set_message(t('The selected @type have been removed from @listname.', array( '@listname' => $bookmark->bookmarkName, + '@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's', ) )); $current_pids = $form_state['islandora_bookmark_pids_on_page']; @@ -970,7 +972,7 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } } else { - drupal_set_message(t('Please select at least one bookmark.'), 'error'); + drupal_set_message(t('Please select at least one @type.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), 'error'); } } elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_all') { @@ -1059,7 +1061,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, ); @@ -1160,14 +1162,14 @@ function islandora_bookmark_fedora_repository_object_form(array $form, array &$f 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 +1178,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', @@ -1303,7 +1305,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 +1319,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 +1332,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 +1346,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'))), ); } @@ -1409,7 +1411,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, ); @@ -1454,7 +1456,7 @@ function islandora_bookmark_add_form_submit(array $form, array &$form_state) { $temp_list = new Bookmark($params); $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( From 26118e62045dbee0deae6e8e8d066e089bfafb3c Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Tue, 7 May 2013 13:00:16 -0300 Subject: [PATCH 02/57] Code style changes for Travis love. --- css/islandora_bookmark.css | 40 ++++++++++++++++++-------------------- includes/bookmark.inc | 12 ++++++------ islandora_bookmark.module | 10 +++++++--- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/css/islandora_bookmark.css b/css/islandora_bookmark.css index 74748d6..1c86020 100644 --- a/css/islandora_bookmark.css +++ b/css/islandora_bookmark.css @@ -3,46 +3,44 @@ * 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 +} diff --git a/includes/bookmark.inc b/includes/bookmark.inc index ed9e87a..a951048 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -107,7 +107,7 @@ class Bookmark { } } else { - throw new Exception(t("You do not have access to remove bookmarks from the list $this->bookmarkName")); + throw new Exception(t("You do not have access to remove bookmarks from the list @name", array('@name' => $this->bookmarkName))); } } else { @@ -134,7 +134,7 @@ class Bookmark { $this->pidList = array(); } else { - throw new Exception(t("You do not have access to remove bookmarks from the list $this->bookmarkName")); + throw new Exception(t("You do not have access to remove bookmarks from the list @name", array('@name' => $this->bookmarkName))); } } else { @@ -169,7 +169,7 @@ class Bookmark { $this->removeUserDB($this->userId); } else { - throw new Exception(t("You do not have access to remove users from the list $this->bookmarkName")); + throw new Exception(t("You do not have access to remove users from the list @name", array('@name' => $this->bookmarkName))); } } else { @@ -207,7 +207,7 @@ class Bookmark { $this->addPidDB($value); } else { - throw new Exception(t("You do not have access to add bookmarks to the list $this->bookmarkName")); + throw new Exception(t("You do not have access to add bookmarks to the list @name", array('@name' => $this->bookmarkName))); } } else { @@ -240,7 +240,7 @@ class Bookmark { $this->addUserDB($value); } else { - throw new Exception(t("You do not have access to add users to the list $this->bookmarkName")); + throw new Exception(t("You do not have access to add users to the list @name", array('@name' => $this->bookmarkName))); } } else { @@ -318,7 +318,7 @@ class Bookmark { } } else { - throw new Exception(t("You do not have access to remove the list $this->bookmarkName")); + throw new Exception(t("You do not have access to remove the list @name", array('@name' => $this->bookmarkName))); } } else { diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 876167a..1511c63 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -254,7 +254,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 @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')); + 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')); } /** @@ -305,7 +307,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 @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.'), + 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')); } @@ -596,7 +600,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list foreach ($export as $module_name => $export_array) { foreach ($export_array as $name => $export_func) { - $export_options[t($module_name)][$name] = $name; + $export_options[$module_name][$name] = $name; $form_state['islandora_bookmark_export'][$name] = $export_func; $export_styles[$name] = islandora_bookmark_get_export_format_options($name); } From f6259377a1b213a125c8f598946bde4789853031 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Tue, 7 May 2013 13:19:36 -0300 Subject: [PATCH 03/57] Make export permissionable. --- includes/solr_results.inc | 106 +++++++++++++++++++------------------- islandora_bookmark.module | 6 ++- 2 files changed, 59 insertions(+), 53 deletions(-) diff --git a/includes/solr_results.inc b/includes/solr_results.inc index 94039d3..a8209cc 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -101,65 +101,67 @@ function islandora_bookmark_results_form(array $form, array &$form_state, $objec ); } 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); + 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( - '#type' => 'fieldset', - '#title' => t('Export to file'), - '#weight' => 0, - ), - ); + $form['islandora_bookmark_export_fieldset'] = array( + 'fieldset' => 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), + $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), + ), ), - ), - ); + ); + } } - } - $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', - ); + $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', + ); - $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, - ); + $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(); if (count($user_bookmarks) > 0) { diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 1511c63..f8aa79b 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -193,6 +193,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.'), + ), ); } @@ -590,7 +594,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list ); } - if (count($pids) != 0) { + if (count($pids) != 0 && user_access('export islandora bookmarks')) { $form['bookmarks']['fieldset']['format_opts'] = array( '#weight' => 4, ); From 85b64dfc203c3232c052e91bd5ebe090a491694c Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Wed, 8 May 2013 13:59:23 -0300 Subject: [PATCH 04/57] Fix object url. --- islandora_bookmark.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index f8aa79b..0667391 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -381,7 +381,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, ); From 63ad97ec76f631e20a7fd73be0b9e3137d77ebea Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Mon, 13 May 2013 08:37:18 -0300 Subject: [PATCH 05/57] Grab users only who are not blocked. --- islandora_bookmark.module | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 0667391..baba33c 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -773,9 +773,14 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list } $selectoptions = array("" => t("- Select user -")); - + if ($user->uid != 0) { + $options['0'] = array( + 'uid' => 0, + 'name' => variable_get('anonymous', 'anonymous'), + ); + } // Get all users for use in select for forms. - $result = db_query("SELECT uid, name FROM {users} ORDER BY uid"); + $result = db_query("SELECT uid, name FROM {users} WHERE STATUS != 0 ORDER BY uid"); foreach ($result as $record) { if ($record->uid != $user->uid) { From b2c8207f17d5a0e0d131e2bb37ce890e9eb990aa Mon Sep 17 00:00:00 2001 From: Islandora Date: Tue, 4 Jun 2013 13:27:05 -0300 Subject: [PATCH 06/57] Only showing the bookmark block on the islandora/object view route. --- islandora_bookmark.module | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index baba33c..d54a7fd 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -160,7 +160,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); } From 5c3cf5b4426f98b31b7f7b607ff1094e076c1639 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Wed, 17 Jul 2013 12:01:26 -0300 Subject: [PATCH 07/57] Add an alter. --- islandora_bookmark.module | 1 + 1 file changed, 1 insertion(+) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index baba33c..6781aad 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -1229,6 +1229,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; } From 805a346703d99ed0ab6900a1a73d077f63d54068 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Wed, 17 Jul 2013 12:03:44 -0300 Subject: [PATCH 08/57] Update the api.php. --- islandora_bookmark.api.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/islandora_bookmark.api.php b/islandora_bookmark.api.php index 5058748..685f5a2 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. * From 2544f6b9dabde72e6c6e493d7ce0d6f9b6128e17 Mon Sep 17 00:00:00 2001 From: phil Date: Thu, 25 Jul 2013 11:57:57 -0300 Subject: [PATCH 09/57] Bookmark rss --- includes/bookmark_rss.inc | 130 +++++++++++++++++++++++++++++++++++++ islandora_bookmark.api.php | 34 ++++++++++ islandora_bookmark.module | 10 +++ 3 files changed, 174 insertions(+) create mode 100644 includes/bookmark_rss.inc diff --git a/includes/bookmark_rss.inc b/includes/bookmark_rss.inc new file mode 100644 index 0000000..6fc5af2 --- /dev/null +++ b/includes/bookmark_rss.inc @@ -0,0 +1,130 @@ +pidList; + + // Loop thourgh all the pids and build the formatted RSS items. + $items = NULL; + foreach ($all_pids as $pid) { + $items .= islandora_bookmark_create_rss_item($pid); + } + + // 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 string $pid + * The PID of the object to create RSS item. + * + * @return string + * The formated RSS item. + */ +function islandora_bookmark_create_rss_item($pid) { + // islandora_invoke_hook_list + module_load_include('inc', 'islandora', 'includes/utilities'); + // Load fedora object from pid. + $fedora_object = islandora_object_load($pid); + + // Create rss item array. + $rss_item = array(); + + // Invokes the islandora_bookmark_rss_item hook. + foreach (islandora_build_hook_list('islandora_bookmark_rss_item', $fedora_object->models) as $hook) { + $temp = module_invoke_all($hook, $fedora_object); + + // If something returns then merge it with the $rss_item + 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($fedora_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 defualt 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'] = '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/islandora_bookmark.api.php b/islandora_bookmark.api.php index 5058748..2dd6549 100644 --- a/islandora_bookmark.api.php +++ b/islandora_bookmark.api.php @@ -43,3 +43,37 @@ 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. + * + * Somtimes 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; +} diff --git a/islandora_bookmark.module b/islandora_bookmark.module index d54a7fd..bd79723 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -32,6 +32,15 @@ function islandora_bookmark_menu() { 'access callback' => 'islandora_bookmark_access', 'access arguments' => array(2), ); + $items['islandora-bookmark/listid/%/rss'] = array( + 'title' => 'RSS Feed', + 'type' => MENU_LOCAL_ACTION, + '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, @@ -423,6 +432,7 @@ function islandora_bookmark_generate_markup($pid, $object_url_info = NULL) { return $output; } + /** * Themes a Fedora object. * From b6e816a704f1d477ba4fbcc11d9aac9cee23f99c Mon Sep 17 00:00:00 2001 From: phil Date: Thu, 25 Jul 2013 12:50:14 -0300 Subject: [PATCH 10/57] fixed syntax error --- includes/bookmark_rss.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/bookmark_rss.inc b/includes/bookmark_rss.inc index 6fc5af2..cb6dc4f 100644 --- a/includes/bookmark_rss.inc +++ b/includes/bookmark_rss.inc @@ -35,7 +35,7 @@ function islandora_bookmark_rss($list_id) { // 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; + $link = $base_url; // The description of the RSS channel. $description = t('Aggregated results of: @bookmarkName', array('@bookmarkName' => $bookmark->bookmarkName)); From 9be79a3d0be28c5b4478e087551f21dcf1d01703 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Thu, 25 Jul 2013 15:43:05 -0300 Subject: [PATCH 11/57] Make the get bookmark by number return FALSE if it does not exist in the db. --- includes/api.inc | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index 18cb01b..78614e0 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -154,33 +154,36 @@ function islandora_bookmark_get_bookmark_by_number($listid) { if (!preg_match('/session_/', $listid)) { $listresult = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $listid))->fetchObject(); + if ($listresult) { + $userresult = db_query('SELECT uid from {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $listid)); - $userresult = db_query('SELECT uid from {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $listid)); + $userlist = array(); - $userlist = array(); + foreach ($userresult as $record) { + array_push($userlist, $record->uid); + } - 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(); - $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); + } - 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); + } + else { + return FALSE; } - - $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); - } else { $templist = unserialize($_SESSION['islandora_bookmark'][$listid]); From eab61f1aebcf00110e8fefb98e0bae75914483b4 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Thu, 25 Jul 2013 15:44:36 -0300 Subject: [PATCH 12/57] Comment better. --- includes/api.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index 78614e0..b90d068 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -146,8 +146,8 @@ 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; From 8a93915a513ecbb921432ba532326163060fe7fc Mon Sep 17 00:00:00 2001 From: phil Date: Fri, 26 Jul 2013 10:52:07 -0300 Subject: [PATCH 13/57] code review changes --- css/islandora_bookmark.css | 10 +++++++++ images/rss.png | Bin 0 -> 896 bytes includes/bookmark_rss.inc | 30 +++++++++++++------------- islandora_bookmark.module | 42 ++++++++++++++++++++++++++++++++++--- 4 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 images/rss.png diff --git a/css/islandora_bookmark.css b/css/islandora_bookmark.css index 1c86020..39832d9 100644 --- a/css/islandora_bookmark.css +++ b/css/islandora_bookmark.css @@ -44,3 +44,13 @@ div.islandora-bookmark-error-message { #islandora-bookmark-detailed-form table .operations { width: 100px; } + +#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 0000000000000000000000000000000000000000..6326a898decd7c02b594a3f710b2b86caf535bfc GIT binary patch literal 896 zcmV-`1AqL9P)Dl1*q`MHq&kZ|0nPbIspv z(j+ITTB>PM^3&EuY@txm3hw;bnCe1;-MG-5Py`XNcA*Ow;z}2S*dG^)h(#?(wcw(q zrX(aSwZv9Jn#44e*fi-qXJ$SZC-*ib;KE^sZ)Pzs-}`=#09+Vp-O=3Mb+S}f8ZeQy zA_4*;tksQg=bT@?b!U9>#_UT&U*5bT7e-olbUoTTq04uzI){w}6AQ))Cd!Xg?`u^I zh!Lt`iGnQ4#y*fd}7> zOu$4!oM&ENTC6f?4~k)3SV)xuGB^tG51D{0Aesn zKk*ZXDZY3dQ+|kW{$t$OF#t@`PvP-bYa+oZs$dXmBd+}THrd$^33Fq43w?Mdf}V$%b=jEO9&XaH3}^Ds3id7PW8qB0000pidList; - // Loop thourgh all the pids and build the formatted RSS items. + // Loop through all the pids and build the formatted RSS items. $items = NULL; foreach ($all_pids as $pid) { - $items .= islandora_bookmark_create_rss_item($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. @@ -53,26 +57,24 @@ function islandora_bookmark_rss($list_id) { /** * Creates an bookmark RSS item. * - * @param string $pid - * The PID of the object to create RSS item. + * @param AbstractObject $object + * The object to create RSS item. * * @return string * The formated RSS item. */ -function islandora_bookmark_create_rss_item($pid) { - // islandora_invoke_hook_list +function islandora_bookmark_create_rss_item(AbstractObject $object) { + module_load_include('inc', 'islandora', 'includes/utilities'); - // Load fedora object from pid. - $fedora_object = islandora_object_load($pid); // Create rss item array. $rss_item = array(); // Invokes the islandora_bookmark_rss_item hook. - foreach (islandora_build_hook_list('islandora_bookmark_rss_item', $fedora_object->models) as $hook) { - $temp = module_invoke_all($hook, $fedora_object); + 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_item + // If something returns then merge it with the $rss_ite. if (!empty($temp)) { $rss_item = array_merge_recursive($rss_item, $temp); } @@ -80,7 +82,7 @@ function islandora_bookmark_create_rss_item($pid) { // If $rss_item is empty then call the default RSS item. if (empty($rss_item)) { - $rss_item = islandora_bookmark_rss_item($fedora_object); + $rss_item = islandora_bookmark_rss_item($object); } // Return the formatted RSS item. @@ -108,12 +110,12 @@ function islandora_bookmark_rss_item(AbstractObject $object) { // Create an associative array for the required elements // for a valid bookmark RSS item. $rss_item = array(); - // The defualt title of the item will be the objects label. + // 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'] = '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"); diff --git a/islandora_bookmark.module b/islandora_bookmark.module index ffe1c73..ca745b1 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -26,7 +26,8 @@ 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', @@ -34,7 +35,7 @@ function islandora_bookmark_menu() { ); $items['islandora-bookmark/listid/%/rss'] = array( 'title' => 'RSS Feed', - 'type' => MENU_LOCAL_ACTION, + 'type' => MENU_CALLBACK, 'page callback' => 'islandora_bookmark_rss', 'page arguments' => array(2), 'access callback' => 'islandora_bookmark_access', @@ -99,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) { @@ -516,10 +518,24 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list '#suffix' => '', ); + // This is the RSS icon link. + if (!empty($bookmark->pidList) && !$bookmark->bookmarkSession) { + $form['bookmark_rss'] = array( + '#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/$bookmark->bookmarkId/rss", array('html' => TRUE)), + '#prefix' => '
', + '#suffix' => '
', + ); + } $form['bookmarks'] = array( 'fieldset' => array( '#type' => 'fieldset', - '#title' => check_plain($bookmark->bookmarkName), + '#title' => 'Detailed List', + '#prefix' => '
', + '#suffix' => '
', ), ); @@ -1583,3 +1599,23 @@ 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); +} From 778f9522ffab981651cf813f8d9c52f3f8810b77 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Fri, 26 Jul 2013 12:56:30 -0300 Subject: [PATCH 14/57] Delete pids from lists if the object becomes purged. --- islandora_bookmark.module | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index ca745b1..15f2dc4 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -1619,3 +1619,12 @@ function islandora_bookmark_list_title($list_id) { 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(); +} From 8de2e9a51acceefb164d3d28d781c76622f6d7b0 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 31 Jul 2013 09:36:12 -0300 Subject: [PATCH 15/57] merged with cselig 7.x branch --- .travis.yml | 25 +++++++++++++++++++++++++ includes/bookmark.inc | 12 ++++++------ islandora_bookmark.module | 14 ++++++-------- 3 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..bc0632a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,25 @@ +language: php +php: + - 5.3.3 + - 5.4 +branches: + only: + - 7.x +env: + - FEDORA_VERSION="3.5" +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/includes/bookmark.inc b/includes/bookmark.inc index a951048..c85e33c 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -107,7 +107,7 @@ class Bookmark { } } else { - throw new Exception(t("You do not have access to remove bookmarks from the list @name", array('@name' => $this->bookmarkName))); + throw new Exception(t("You do not have access to remove bookmarks from the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); } } else { @@ -134,7 +134,7 @@ class Bookmark { $this->pidList = array(); } else { - throw new Exception(t("You do not have access to remove bookmarks from the list @name", array('@name' => $this->bookmarkName))); + throw new Exception(t("You do not have access to remove bookmarks from the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); } } else { @@ -169,7 +169,7 @@ class Bookmark { $this->removeUserDB($this->userId); } else { - throw new Exception(t("You do not have access to remove users from the list @name", array('@name' => $this->bookmarkName))); + throw new Exception(t("You do not have access to remove users from the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); } } else { @@ -207,7 +207,7 @@ class Bookmark { $this->addPidDB($value); } else { - throw new Exception(t("You do not have access to add bookmarks to the list @name", array('@name' => $this->bookmarkName))); + throw new Exception(t("You do not have access to add bookmarks from the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); } } else { @@ -240,7 +240,7 @@ class Bookmark { $this->addUserDB($value); } else { - throw new Exception(t("You do not have access to add users to the list @name", array('@name' => $this->bookmarkName))); + throw new Exception(t("You do not have access to add users from the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); } } else { @@ -318,7 +318,7 @@ class Bookmark { } } else { - throw new Exception(t("You do not have access to remove the list @name", array('@name' => $this->bookmarkName))); + throw new Exception(t("You do not have access to remove the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); } } else { diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 15f2dc4..8cd01a2 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -426,7 +426,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)) . '
'; } @@ -439,12 +439,10 @@ function islandora_bookmark_generate_markup($pid, $object_url_info = NULL) { * 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 @@ -638,7 +636,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list foreach ($export as $module_name => $export_array) { foreach ($export_array as $name => $export_func) { - $export_options[$module_name][$name] = $name; + $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); } From 3cf1923a36f6d9695158fe1297f70789624d69b0 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 31 Jul 2013 09:51:02 -0300 Subject: [PATCH 16/57] travis bugs --- islandora_bookmark.module | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 8cd01a2..b7ad799 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -92,9 +92,9 @@ function islandora_bookmark_menu() { */ function islandora_bookmark_access($list_id) { global $user; - + // @codingStandardsIgnoreStart preg_match('/session_/', $list_id, $match); - + // @codingStandardsIgnoreEnd if ($match) { $bookmark_access = TRUE; } @@ -132,9 +132,9 @@ function islandora_bookmark_access($list_id) { */ function islandora_bookmark_delete_bookmark_access($list_id) { global $user; - + // @codingStandardsIgnoreStart preg_match('/session_/', $list_id, $match); - + // @codingStandardsIgnoreEnd if ($match) { $bookmark_access = TRUE; } @@ -886,7 +886,9 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state $form_state['redirect'] = array(request_path(), array('query' => $queries)); foreach ($_POST as $key => $value) { + // @codingStandardsIgnoreStart preg_match('/remove_pid_(.*)/', $key, $match); + // @codingStandardsIgnoreEnd if ($match) { break; } From 76f481542cf73fe35833702d0885e882e90eb039 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 31 Jul 2013 10:33:54 -0300 Subject: [PATCH 17/57] travis bugs --- includes/api.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/api.inc b/includes/api.inc index b90d068..943c6ca 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -151,8 +151,9 @@ function islandora_bookmark_get_user_shared_bookmarks() { */ function islandora_bookmark_get_bookmark_by_number($listid) { global $user; - + // @codingStandardsIgnoreStart if (!preg_match('/session_/', $listid)) { + // @codingStandardsIgnoreEnd $listresult = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $listid))->fetchObject(); if ($listresult) { $userresult = db_query('SELECT uid from {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $listid)); From a55d5dd1554e4f0c1ff0989d99eb879aeb6330b1 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 31 Jul 2013 11:48:56 -0300 Subject: [PATCH 18/57] fixed duplicates for travis --- includes/api.inc | 65 +++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index 943c6ca..5ec001c 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -28,20 +28,10 @@ function islandora_bookmark_get_user_owned_bookmarks() { } 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)); + $listpids = islandora_bookmark_list_pids_query($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; - } + $listusers = islandora_bookmark_list_users_query($value); $list_record = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $value))->fetchObject(); $listid = $value; @@ -105,20 +95,9 @@ function islandora_bookmark_get_user_shared_bookmarks() { 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; - } + $listpids = islandora_bookmark_list_pids_query($value); - $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; - } + $listusers = islandora_bookmark_list_users_query($value); $list_record = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $value))->fetchObject(); $listid = $value; @@ -223,3 +202,39 @@ function islandora_bookmark_get_bookmarks_visible_to_user($pid, $uid) { } 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) { + $pid_query = db_query('SELECT pidid FROM {islandora_bookmark_list_pids} WHERE listid = :listid', array(':listid' => $value)); + $listpids = array(); + foreach ($pid_query as $record) { + $listpids[] = $record->pidid; + } + return $listpids; +} + +/** + * Retrieves all the uids from the bookmark listid. + * + * @param string $value + * The listid. + * + * @return array + * An array containing user ids. + */ +function islandora_bookmark_list_users_query($value) { + $user_query = db_query('SELECT uid FROM {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $value)); + $listusers = array(); + foreach ($user_query as $record) { + $listusers[] = $record->uid; + } + return $listusers; +} From 9b0c5128092bd0ffa019a1434abe09eebc023cbf Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 31 Jul 2013 12:20:05 -0300 Subject: [PATCH 19/57] added fedora 3.6.2 to travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index bc0632a..b20020e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ branches: - 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 From 971b7438b837e1d263b3d1df2203b78c7815f779 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 31 Jul 2013 14:14:03 -0300 Subject: [PATCH 20/57] updated the README file --- README.txt => README.md | 8 ++++++++ 1 file changed, 8 insertions(+) rename README.txt => README.md (88%) 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 ================== From 76165ee88c600518017decc1b760393b80c8b212 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 31 Jul 2013 14:28:51 -0300 Subject: [PATCH 21/57] comment some of the coding standards ignore --- includes/api.inc | 4 ++++ islandora_bookmark.module | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/includes/api.inc b/includes/api.inc index 5ec001c..cf4f240 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -130,6 +130,10 @@ function islandora_bookmark_get_user_shared_bookmarks() { */ function islandora_bookmark_get_bookmark_by_number($listid) { global $user; + + // The error that codesniffer was giving was Using the e flag in preg_match. + // This is a bug. so we will ignore it until it is fixed. + // https://drupal.org/node/1779020 // @codingStandardsIgnoreStart if (!preg_match('/session_/', $listid)) { // @codingStandardsIgnoreEnd diff --git a/islandora_bookmark.module b/islandora_bookmark.module index b7ad799..6abbaf3 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -92,6 +92,10 @@ function islandora_bookmark_menu() { */ function islandora_bookmark_access($list_id) { global $user; + + // The error that codesniffer was giving was Using the e flag in preg_match. + // This is a bug. so we will ignore it until it is fixed. + // https://drupal.org/node/1779020 // @codingStandardsIgnoreStart preg_match('/session_/', $list_id, $match); // @codingStandardsIgnoreEnd @@ -132,6 +136,10 @@ function islandora_bookmark_access($list_id) { */ function islandora_bookmark_delete_bookmark_access($list_id) { global $user; + + // The error that codesniffer was giving was Using the e flag in preg_match. + // This is a bug. so we will ignore it until it is fixed. + // https://drupal.org/node/1779020 // @codingStandardsIgnoreStart preg_match('/session_/', $list_id, $match); // @codingStandardsIgnoreEnd @@ -886,6 +894,9 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state $form_state['redirect'] = array(request_path(), array('query' => $queries)); foreach ($_POST as $key => $value) { + // The error that codesniffer was giving was Using the e flag in preg_match. + // This is a bug. so we will ignore it until it is fixed. + // https://drupal.org/node/1779020 // @codingStandardsIgnoreStart preg_match('/remove_pid_(.*)/', $key, $match); // @codingStandardsIgnoreEnd From c712c9af680813452b948d4aa83aac6a1efcbdd0 Mon Sep 17 00:00:00 2001 From: phil Date: Mon, 12 Aug 2013 15:40:09 -0300 Subject: [PATCH 22/57] bookmarks share with anonymous can be viewed by everyone --- includes/api.inc | 2 +- islandora_bookmark.module | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index cf4f240..1b24b93 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -86,7 +86,7 @@ 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; diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 6abbaf3..63e6d07 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -727,7 +727,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list $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')); + $output_user = variable_get('anonymous', t('Everyone')); } else { $output_user = user_load($remove_user)->name; @@ -773,7 +773,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list foreach ($bookmark_users as $itr => $uid) { $tempuser = user_load($uid); if ($tempuser->uid == 0) { - $user_output = variable_get('anonymous', t('Anonymous')); + $user_output = variable_get('anonymous', t('Everyone')); } else { $user_output = $tempuser->name; @@ -816,7 +816,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list if ($user->uid != 0) { $options['0'] = array( 'uid' => 0, - 'name' => variable_get('anonymous', 'anonymous'), + 'name' => variable_get('anonymous', 'Everyone'), ); } // Get all users for use in select for forms. From e2c0ed3e6252f41ac1726a14137282dbf943fa83 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 13 Aug 2013 20:11:04 +0000 Subject: [PATCH 23/57] Carry through changes from Islandora Scholar Citation Exporter. --- includes/solr_results.inc | 2 +- islandora_bookmark.module | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/solr_results.inc b/includes/solr_results.inc index a8209cc..aed6124 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -70,7 +70,7 @@ function islandora_bookmark_results_form(array $form, array &$form_state, $objec '#tag' => 'meta', '#attributes' => array( 'http-equiv' => 'refresh', - 'content' => '1.0;' . url(MENU_CITATION_EXPORTER_EXPORT, array('absolute' => TRUE)), + 'content' => '1.0;' . url(CITATION_EXPORTER_EXPORT_MENU, array('absolute' => TRUE)), ), ); drupal_add_html_head($citation_export_head, 'citation_exporter_head'); diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 6abbaf3..3a53be6 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -511,7 +511,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list '#tag' => 'meta', '#attributes' => array( 'http-equiv' => 'refresh', - 'content' => '1.0;' . url(MENU_CITATION_EXPORTER_EXPORT, array('absolute' => TRUE)), + 'content' => '1.0;' . url(CITATION_EXPORTER_EXPORT_MENU, array('absolute' => TRUE)), ), ); drupal_add_html_head($citation_export_head, 'citation_exporter_head'); From 569306c968c23f6af1a7c90322e0e1d9d5fedc17 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 14 Aug 2013 09:37:12 -0300 Subject: [PATCH 24/57] removed the variable_get --- islandora_bookmark.module | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 63e6d07..8ff9ea3 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -727,7 +727,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list $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('Everyone')); + $output_user = t('Everyone'); } else { $output_user = user_load($remove_user)->name; @@ -773,7 +773,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list foreach ($bookmark_users as $itr => $uid) { $tempuser = user_load($uid); if ($tempuser->uid == 0) { - $user_output = variable_get('anonymous', t('Everyone')); + $user_output = t('Everyone'); } else { $user_output = $tempuser->name; @@ -816,7 +816,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list if ($user->uid != 0) { $options['0'] = array( 'uid' => 0, - 'name' => variable_get('anonymous', 'Everyone'), + 'name' => t('Everyone'), ); } // Get all users for use in select for forms. From e8cd8a1b53d6ff8a44e82b3b3a55b3201cfd0a52 Mon Sep 17 00:00:00 2001 From: phil Date: Thu, 15 Aug 2013 09:48:46 -0300 Subject: [PATCH 25/57] fixed user access to bookmarks --- islandora_bookmark.module | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 8ff9ea3..325ab8b 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -96,9 +96,8 @@ function islandora_bookmark_access($list_id) { // The error that codesniffer was giving was Using the e flag in preg_match. // This is a bug. so we will ignore it until it is fixed. // https://drupal.org/node/1779020 - // @codingStandardsIgnoreStart preg_match('/session_/', $list_id, $match); - // @codingStandardsIgnoreEnd + if ($match) { $bookmark_access = TRUE; } @@ -117,6 +116,10 @@ function islandora_bookmark_access($list_id) { $bookmark_access = TRUE; break; } + elseif ($record->uid == 0) { + $bookmark_access = TRUE; + break; + } } } } From 2f977c3634c1beef2155b073a83ec1aa0e3c3a99 Mon Sep 17 00:00:00 2001 From: phil Date: Mon, 19 Aug 2013 12:39:44 -0300 Subject: [PATCH 26/57] removed coding standards ignore --- includes/api.inc | 2 -- islandora_bookmark.module | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index 1b24b93..5f22776 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -134,9 +134,7 @@ function islandora_bookmark_get_bookmark_by_number($listid) { // The error that codesniffer was giving was Using the e flag in preg_match. // This is a bug. so we will ignore it until it is fixed. // https://drupal.org/node/1779020 - // @codingStandardsIgnoreStart if (!preg_match('/session_/', $listid)) { - // @codingStandardsIgnoreEnd $listresult = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $listid))->fetchObject(); if ($listresult) { $userresult = db_query('SELECT uid from {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $listid)); diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 325ab8b..d2497d0 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -143,9 +143,8 @@ function islandora_bookmark_delete_bookmark_access($list_id) { // The error that codesniffer was giving was Using the e flag in preg_match. // This is a bug. so we will ignore it until it is fixed. // https://drupal.org/node/1779020 - // @codingStandardsIgnoreStart preg_match('/session_/', $list_id, $match); - // @codingStandardsIgnoreEnd + if ($match) { $bookmark_access = TRUE; } From ce2f85a350f8ddd73963ce8653f91519c09d9927 Mon Sep 17 00:00:00 2001 From: phil Date: Mon, 19 Aug 2013 12:54:39 -0300 Subject: [PATCH 27/57] removing comments --- includes/api.inc | 3 --- islandora_bookmark.module | 8 -------- 2 files changed, 11 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index 5f22776..a533483 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -131,9 +131,6 @@ function islandora_bookmark_get_user_shared_bookmarks() { function islandora_bookmark_get_bookmark_by_number($listid) { global $user; - // The error that codesniffer was giving was Using the e flag in preg_match. - // This is a bug. so we will ignore it until it is fixed. - // https://drupal.org/node/1779020 if (!preg_match('/session_/', $listid)) { $listresult = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $listid))->fetchObject(); if ($listresult) { diff --git a/islandora_bookmark.module b/islandora_bookmark.module index d2497d0..af2f2d1 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -140,9 +140,6 @@ function islandora_bookmark_access($list_id) { function islandora_bookmark_delete_bookmark_access($list_id) { global $user; - // The error that codesniffer was giving was Using the e flag in preg_match. - // This is a bug. so we will ignore it until it is fixed. - // https://drupal.org/node/1779020 preg_match('/session_/', $list_id, $match); if ($match) { @@ -896,12 +893,7 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state $form_state['redirect'] = array(request_path(), array('query' => $queries)); foreach ($_POST as $key => $value) { - // The error that codesniffer was giving was Using the e flag in preg_match. - // This is a bug. so we will ignore it until it is fixed. - // https://drupal.org/node/1779020 - // @codingStandardsIgnoreStart preg_match('/remove_pid_(.*)/', $key, $match); - // @codingStandardsIgnoreEnd if ($match) { break; } From 1ee369c0b4fc8f4c4a475a5231b5cb9cccbf9759 Mon Sep 17 00:00:00 2001 From: phil Date: Mon, 19 Aug 2013 14:03:59 -0300 Subject: [PATCH 28/57] removed more comments --- islandora_bookmark.module | 3 --- 1 file changed, 3 deletions(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index af2f2d1..b22258e 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -93,9 +93,6 @@ function islandora_bookmark_menu() { function islandora_bookmark_access($list_id) { global $user; - // The error that codesniffer was giving was Using the e flag in preg_match. - // This is a bug. so we will ignore it until it is fixed. - // https://drupal.org/node/1779020 preg_match('/session_/', $list_id, $match); if ($match) { From 9d5a3a5c7318c26952e03184c76304aa289b7103 Mon Sep 17 00:00:00 2001 From: phil Date: Wed, 21 Aug 2013 09:11:42 -0300 Subject: [PATCH 29/57] removed anonymous references --- islandora_bookmark.module | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index b22258e..a10d448 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -697,7 +697,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list $add_user = $form_state['input']['bookmarks']['users']['listusers']; if (drupal_strlen($add_user)) { if ($add_user == 0) { - $output_user = variable_get('anonymous', t('Anonymous')); + $output_user = t('Everyone'); } else { $output_user = user_load($add_user)->name; @@ -822,7 +822,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list if ($record->uid != $user->uid) { $resultname = $record->name; if ($record->uid == 0) { - $resultname = variable_get('anonymous', t('Anonymous')); + $resultname = t('Everyone'); } $options[$record->uid] = array( 'uid' => $record->uid, @@ -1421,7 +1421,7 @@ 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; From e666005b2080efb7ef7c2b0fc3808cd7b6e6290a Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 5 Sep 2013 14:24:09 +0000 Subject: [PATCH 30/57] Refactored form building/submitting logic. --- includes/solr_results.inc | 818 ++++++++++++++++++++------------------ 1 file changed, 437 insertions(+), 381 deletions(-) diff --git a/includes/solr_results.inc b/includes/solr_results.inc index aed6124..61b71d3 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -34,454 +34,510 @@ 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(CITATION_EXPORTER_EXPORT_MENU, 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 buildForm($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 = array( - 'markup' => t('Label'), - ); - $rows = array(); - $form_state['islandora_solr_query_processor'] = $query_processor; - foreach ($object_results as $object_result) { - $doc = $object_result['solr_doc']; - $markup = ''; - if (isset($doc['object_label'])) { - $markup = $doc['object_label']; - } - else { - $markup = $doc['PID']; + $header = $this->getTableHeader(); + $rows = array(); + foreach ($object_results as $object_result) { + $doc = $object_result['solr_doc']; + $rows[$doc['PID']] = $this->getTableRow($object_result); } - $object_url_info = array( - 'path' => $object_result['object_url'], - 'params' => $object_result['object_url_params'], - 'markup' => $markup, - ); - $rows[$doc['PID']] = array( - 'markup' => islandora_bookmark_generate_markup($doc['PID'], $object_url_info), - ); - } - if (count($rows) > 0) { - 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); + if (count($rows) > 0) { + 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( - '#type' => 'fieldset', - '#title' => t('Export to file'), - '#weight' => 0, - ), - ); + $form['islandora_bookmark_export_fieldset'] = array( + 'fieldset' => 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), + $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), + ), ), - ), - ); + ); + } } + + $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', + ); + + $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(); - $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', - ); + if (count($user_bookmarks) > 0) { + $save_options = array(); + $save_options['default'] = t('- Select @type list -', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))); - $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, - ); + foreach ($user_bookmarks as $bookmark) { + $save_options[$bookmark->bookmarkId] = $bookmark->bookmarkName; + } + + $form['islandora_bookmark_save_fieldset'] = array( + 'fieldset' => 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( + '#type' => 'select', + '#options' => $save_options, + '#weight' => 3, + ); + $form['islandora_bookmark_save_fieldset']['fieldset']['save_selected_submit'] = array( + '#type' => 'submit', + '#value' => t('Bookmark selected objects'), + '#name' => 'save_selected', + '#weight' => 4, + ); + $form['islandora_bookmark_save_fieldset']['fieldset']['save_all_submit'] = array( + '#type' => 'submit', + '#value' => t('Bookmark all results'), + '#name' => 'save_all', + '#weight' => 5, + ); + $form['islandora_bookmark_save_fieldset']['fieldset']['go_to_bookmark'] = array( + '#type' => 'submit', + '#value' => t('View list'), + '#name' => 'goto_bookmark', + '#weight' => 6, + ); + } } - $user_bookmarks = islandora_bookmark_get_user_owned_bookmarks(); - if (count($user_bookmarks) > 0) { - $save_options = array(); - $save_options['default'] = t('- Select @type list -', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))); + $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; + } + + /** + * 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']; - foreach ($user_bookmarks as $bookmark) { - $save_options[$bookmark->bookmarkId] = $bookmark->bookmarkName; + $markup = ''; + if (isset($doc['object_label'])) { + $markup = $doc['object_label']; + } + else { + $markup = $doc['PID']; } - $form['islandora_bookmark_save_fieldset'] = array( - 'fieldset' => 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( - '#type' => 'select', - '#options' => $save_options, - '#weight' => 3, + $object_url_info = array( + 'path' => $object_result['object_url'], + 'params' => $object_result['object_url_params'], + 'markup' => $markup, ); - $form['islandora_bookmark_save_fieldset']['fieldset']['save_selected_submit'] = array( - '#type' => 'submit', - '#value' => t('Bookmark selected objects'), - '#name' => 'save_selected', - '#weight' => 4, - ); - $form['islandora_bookmark_save_fieldset']['fieldset']['save_all_submit'] = array( - '#type' => 'submit', - '#value' => t('Bookmark all results'), - '#name' => 'save_all', - '#weight' => 5, - ); - $form['islandora_bookmark_save_fieldset']['fieldset']['go_to_bookmark'] = array( - '#type' => 'submit', - '#value' => t('View list'), - '#name' => 'goto_bookmark', - '#weight' => 6, + + return array( + 'markup' => islandora_bookmark_generate_markup($doc['PID'], $object_url_info), ); - } } - $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; -} -/** - * 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; - - foreach ($form_state['values']['islandora_bookmark_table'] as $checkbox => $value) { - if ($value !== 0) { - if (islandora_object_load($value)) { - $pids[] = $value; + /** + * Submit handler for the islandora_bookmark_results form. + * + * @param array $form + * The Drupal form definition. + * @param array $form_state + * The Drupal form state. + */ + public function submitForm(&$form, &$form_state) { + $queries = drupal_get_query_parameters(); + + $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 = $this->islandoraSolrQueryProcessor; + $query_proc->solrLimit = 100000; + + if ($form_state['clicked_button']['#name'] === 'export_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++; + } + } + } + $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 { - $broke_pids .= $value . ' '; - $broke_count++; + call_user_func($form_state['islandora_bookmark_export'][$key], $pids); } } - } - $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]); + elseif (count($pids) === 0 && $broke_count === 0) { + drupal_set_message(t('Please select at least one object.'), 'error'); + } + elseif ($key === 'default') { + drupal_set_message(t('Please select an export type.'), 'error'); } else { - call_user_func($form_state['islandora_bookmark_export'][$key], $pids); + 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'); + } } } - elseif (count($pids) === 0 && $broke_count === 0) { - drupal_set_message(t('Please select at least one object.'), '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 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'); + 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++; + } } - } - } - 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; + $key = $form_state['input']['islandora_bookmark_export_options_select']; - 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++; + 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); + } } - } - $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]); + elseif ($key === 'default') { + drupal_set_message(t('Please select an option to export as!'), 'error'); } else { - call_user_func($form_state['islandora_bookmark_export'][$key], $all_pids); + 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'); + } } } - elseif ($key === 'default') { - drupal_set_message(t('Please select an option to export as!'), 'error'); - } - else { + 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++; + } + } + } 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.', + $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'); } - } - } - 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++; - } - } - } - 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++; + $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))); + 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'); } - 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'); } } - 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'); - } - } - 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; - $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']; + elseif (count($pids) === 0) { + drupal_set_message(t('Please select at least one object.'), 'error'); } else { - $broke_pids .= $doc['PID'] . ' '; - $broke_count++; + drupal_set_message(t('Please select a bookmark list.'), 'error'); } } - - 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; + elseif ($form_state['clicked_button']['#name'] === 'save_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 { - $skipped_pids .= $pid . ' '; - $skipped_count++; + $broke_pids .= $doc['PID'] . ' '; + $broke_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))); + 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++; } - 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'); + 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 { - $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'); + drupal_set_message(t('Please select a bookmark list.'), 'error'); } } else { - drupal_set_message(t('Please select a bookmark list.'), 'error'); - } - } - else { - $key = $form_state['values']['save_options']; + $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'); + if ($key !== 'default') { + drupal_goto('islandora-bookmark/listid/' . $key); + } + else { + drupal_set_message(t('Please select a bookmark list.'), 'error'); + } } } } + +/** + * 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->buildForm($form, $form_state, $object_results); +} + +/** + * 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]->submitForm($form, $form_state); +} From 4ab3983c09485fef6e2c1a86a889c494be61ebd5 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 5 Sep 2013 18:53:13 +0000 Subject: [PATCH 31/57] And... fully working again. --- includes/solr_results.inc | 768 ++++++++++++++++++++------------------ 1 file changed, 408 insertions(+), 360 deletions(-) diff --git a/includes/solr_results.inc b/includes/solr_results.inc index 61b71d3..bfa8387 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -78,109 +78,10 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { $doc = $object_result['solr_doc']; $rows[$doc['PID']] = $this->getTableRow($object_result); } - if (count($rows) > 0) { - 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( - '#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), - ), - ), - ); - } - } - - $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', - ); - - $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(); - if (count($user_bookmarks) > 0) { - $save_options = array(); - $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; - } + if (count($rows) > 0) { + $form = array_merge($form, $this->getActions($form_state)); - $form['islandora_bookmark_save_fieldset'] = array( - 'fieldset' => 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( - '#type' => 'select', - '#options' => $save_options, - '#weight' => 3, - ); - $form['islandora_bookmark_save_fieldset']['fieldset']['save_selected_submit'] = array( - '#type' => 'submit', - '#value' => t('Bookmark selected objects'), - '#name' => 'save_selected', - '#weight' => 4, - ); - $form['islandora_bookmark_save_fieldset']['fieldset']['save_all_submit'] = array( - '#type' => 'submit', - '#value' => t('Bookmark all results'), - '#name' => 'save_all', - '#weight' => 5, - ); - $form['islandora_bookmark_save_fieldset']['fieldset']['go_to_bookmark'] = array( - '#type' => 'submit', - '#value' => t('View list'), - '#name' => 'goto_bookmark', - '#weight' => 6, - ); - } } $form['islandora_bookmark_table'] = array( @@ -216,31 +117,368 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * $this->getTableHeader(). */ protected function getTableRow($object_result) { - $doc = $object_result['solr_doc']; + $doc = $object_result['solr_doc']; - $markup = ''; - if (isset($doc['object_label'])) { - $markup = $doc['object_label']; + $markup = ''; + if (isset($doc['object_label'])) { + $markup = $doc['object_label']; + } + else { + $markup = $doc['PID']; + } + + $object_url_info = array( + 'path' => $object_result['object_url'], + 'params' => $object_result['object_url_params'], + 'markup' => $markup, + ); + + return array( + 'markup' => islandora_bookmark_generate_markup($doc['PID'], $object_url_info), + ); + } + + /** + * 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); + } } - else { - $markup = $doc['PID']; + + $group = array( + '#type' => 'fieldset', + '#title' => t('Export to file'), + '#weight' => 0, + ); + + $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), + ), + ), + ); + } } - $object_url_info = array( - 'path' => $object_result['object_url'], - 'params' => $object_result['object_url_params'], - 'markup' => $markup, + $group['export_options'] = array( + '#type' => 'select', + '#options' => $export_options, + '#weight' => 3, + '#title' => t('Format'), + ); + + $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', + ), ); - return array( - 'markup' => islandora_bookmark_generate_markup($doc['PID'], $object_url_info), + $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 @type list -', array( + '@type' => variable_get('islandora_bookmark_type', 'bookmark'), + )); + + foreach ($user_bookmarks as $bookmark) { + $save_options[$bookmark->bookmarkId] = $bookmark->bookmarkName; + } + + $group = array( + '#type' => 'fieldset', + '#title' => t('@type', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), + '#weight' => 0, + ); + $group['save_options'] = array( + '#type' => 'select', + '#options' => $save_options, + '#weight' => 3, + ); + $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', + ), + ); + $group['save_all_submit'] = array( + '#type' => 'submit', + '#value' => t('Bookmark all results'), + '#name' => 'save_all', + '#weight' => 5, + '#action_submit' => 'saveAll', + '#action_validate' => array( + 'hasListSelected', + ), + ); + $group['go_to_bookmark'] = array( + '#type' => 'submit', + '#value' => t('View list'), + '#name' => 'goto_bookmark', + '#weight' => 6, + '#action_submit' => 'goToBookmark', + '#action_validate' => array( + 'hasListSelected', + ), + ); + + $groups['islandora_bookmark_save_fieldset'] = array( + 'fieldset' => $group, + ); + } + + return $groups; } + /** + * 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.')); + } + } + + /** + * 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.')); + } + } + + /** + * 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.')); + } + } + + /** + * 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']); + + 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); + } + } + + /** + * 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->pidList); + $skipped_pids = array_intersect($pids, $bookmark_object->pidList); + + 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, + ) + )); + } + catch (Exception $e) { + drupal_set_message($e->getMessage(), '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, + ) + )); + } + } + + /** + * Go over any '#action_validate' functions on the clicked button. + */ + public function validateForm($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); + } + } + } /** * 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 @@ -250,263 +488,66 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { $queries = drupal_get_query_parameters(); $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 = $this->islandoraSolrQueryProcessor; - $query_proc->solrLimit = 100000; - - if ($form_state['clicked_button']['#name'] === 'export_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++; - } - } - } - $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'); - } - 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 not exist: !broke_pids.', - array('!broke_pids' => trim($broke_pids))); - drupal_set_message($broken_pids, 'error'); - } - } - } - 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++; - } - } - $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 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'); - } + $clicked_button = $form_state['clicked_button']; + if (isset($clicked_button['#action_submit'])) { + foreach ((array) $clicked_button['#action_submit'] as $submit_function) { + call_user_func(array($this, $submit_function), $form_state); } } - 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++; - } - } - } - 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'); - } - } - 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); + + for ($i = $qp->solrLimit; $i < $result_count; $i += $qp->solrLimit) { + $qp->executeQuery(); + $solr_results = $qp->islandoraSolrResult; $object_results = $solr_results['response']['objects']; - $all_pids = array(); - $broke_pids = ''; - $broke_count = 0; + $pids = array_merge( + $pids, + array_map($get_pid, $object_results) + ); + } - 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++; - } - } + return $pids; + } - 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'); - } + /** + * 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); } - 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'); - } - } + return $functional; } } @@ -530,6 +571,13 @@ function islandora_bookmark_results_form($form, &$form_state, $results_class, $o return $results_class->buildForm($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]->validateForm($form, $form_state); +} + /** * Submit handler for the islandora_bookmark_results form. * From 671a4ab321c78d233d806a3396e2a212da7fee35 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Thu, 5 Sep 2013 18:58:55 +0000 Subject: [PATCH 32/57] Add a type column for use in distinguishing lists. --- islandora_bookmark.install | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/islandora_bookmark.install b/islandora_bookmark.install index d91dd09..33831c5 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', + )); +} \ No newline at end of file From 61373667398cac23c1a4e37878fa0c8b36171311 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Thu, 5 Sep 2013 19:00:20 +0000 Subject: [PATCH 33/57] Newline. --- islandora_bookmark.install | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/islandora_bookmark.install b/islandora_bookmark.install index 33831c5..f08d401 100644 --- a/islandora_bookmark.install +++ b/islandora_bookmark.install @@ -88,4 +88,5 @@ function islandora_bookmark_update_7100(&$sandbox) { 'length' => 255, 'default' => 'bookmark', )); -} \ No newline at end of file +} + From 8fb613854d4953db2f1abc948e4bc7815ad349ae Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Thu, 5 Sep 2013 19:12:44 +0000 Subject: [PATCH 34/57] Fix for Travis. --- islandora_bookmark.install | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/islandora_bookmark.install b/islandora_bookmark.install index f08d401..0238045 100644 --- a/islandora_bookmark.install +++ b/islandora_bookmark.install @@ -70,7 +70,7 @@ function islandora_bookmark_schema() { 'type' => 'varchar', 'length' => 255, 'default' => 'bookmark', - ) + ), ), 'primary key' => array('listid'), ); @@ -89,4 +89,3 @@ function islandora_bookmark_update_7100(&$sandbox) { 'default' => 'bookmark', )); } - From 7a4426f53b73aba5c09c258b2a18e22f4fa932c1 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 11 Sep 2013 18:47:46 +0000 Subject: [PATCH 35/57] Major refactor, allowing branching based on our "column" value. --- includes/api.inc | 134 ++--------- includes/bookmark.inc | 435 ++++++++++++---------------------- includes/bookmark_db.inc | 258 ++++++++++++++++++++ includes/bookmark_rss.inc | 2 +- includes/bookmark_session.inc | 135 +++++++++++ includes/solr_results.inc | 6 +- islandora_bookmark.info | 2 + islandora_bookmark.module | 130 +++++----- 8 files changed, 628 insertions(+), 474 deletions(-) create mode 100644 includes/bookmark_db.inc create mode 100644 includes/bookmark_session.inc diff --git a/includes/api.inc b/includes/api.inc index a533483..00f5062 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -16,54 +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 = islandora_bookmark_list_pids_query($value); - - $listusers = islandora_bookmark_list_users_query($value); - - $list_record = db_query('SELECT listname, listowner from {islandora_bookmark_list_names} WHERE listid = :listid', array(':listid' => $value))->fetchObject(); - $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); - $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; } @@ -93,25 +62,8 @@ function islandora_bookmark_get_user_shared_bookmarks() { } $shared_ids = array_diff($shared_ids, $owner_ids); - foreach ($shared_ids as $key => $value) { - - $listpids = islandora_bookmark_list_pids_query($value); - - $listusers = islandora_bookmark_list_users_query($value); - - $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; @@ -129,45 +81,11 @@ function islandora_bookmark_get_user_shared_bookmarks() { * 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(); - if ($listresult) { - $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); - } - else { - return FALSE; - } + $list = Bookmark::getList($listid); + if ($list === NULL) { + return FALSE; } - else { - $templist = unserialize($_SESSION['islandora_bookmark'][$listid]); - } - return $templist; + return $list; } /** @@ -186,9 +104,9 @@ 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(); } } } @@ -212,12 +130,8 @@ function islandora_bookmark_get_bookmarks_visible_to_user($pid, $uid) { * An array containing listpids. */ function islandora_bookmark_list_pids_query($value) { - $pid_query = db_query('SELECT pidid FROM {islandora_bookmark_list_pids} WHERE listid = :listid', array(':listid' => $value)); - $listpids = array(); - foreach ($pid_query as $record) { - $listpids[] = $record->pidid; - } - return $listpids; + $list = Bookmark::getList($value); + return $list->getPids(); } /** @@ -230,10 +144,6 @@ function islandora_bookmark_list_pids_query($value) { * An array containing user ids. */ function islandora_bookmark_list_users_query($value) { - $user_query = db_query('SELECT uid FROM {islandora_bookmark_list_users} WHERE listid = :listid', array(':listid' => $value)); - $listusers = array(); - foreach ($user_query as $record) { - $listusers[] = $record->uid; - } - return $listusers; + $list = Bookmark::getList($value); + return $list->getUsers(); } diff --git a/includes/bookmark.inc b/includes/bookmark.inc index c85e33c..66ace79 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -6,374 +6,247 @@ */ /** - * 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); /** - * The unique ID of the object. This is generated through the database or - * explicity assigned when adding objects to the session. + * Shares a list with the given user. * - * @var integer + * @param int $user + * A user ID. */ - public $bookmarkId; + public function addUser($user); /** - * The owner of the object. + * Get a list of all the users associated with a list. * - * @var integer + * @return array + * An array of integers, representing user IDs. */ - public $bookmarkOwner; + public function getUsers(); + + /** + * Removes the given user from the object. + * + * @param int $user + * A user ID. + */ + 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 user id of the current user accessing the object. + * The owner of the object. * * @var integer */ - public $userId; + public $bookmarkOwner; /** - * Whether the object is created or retrieved through the session. + * The ID of the given list. * - * @var boolean + * @var mixed */ - public $bookmarkSession = FALSE; + protected $bookmarkId; /** * 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']; - } + /** + * Accessor for the list ID. + * + * @return mixed + * The value of $this->bookmarkId. + */ + public function getId() { + return $this->bookmarkId; + } - if (!empty($params['bookmark_pids'])) { - $this->pidList = $params['bookmark_pids']; - } + /** + * 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; - if (!empty($params['bookmark_name'])) { - $this->bookmarkName = $params['bookmark_name']; + $bookmark = NULL; + if ($user->uid === 0 || $force_session_attempt) { + $bookmark = BookmarkSession::getList($id, TRUE); } - if (!empty($params['bookmark_id'])) { - $this->bookmarkId = $params['bookmark_id']; + if ($bookmark === NULL) { + // Pass off to BookmarkDatabase. + $bookmark = BookmarkDatabase::getList($id, FALSE); } - if (!$params['retrieve']) { - $this->createList(); - } - else { - $this->bookmarkOwner = $params['bookmark_owner']; - } + return $bookmark; } /** - * Removes the given pids from the object and from the database or session. + * 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()); + } + + /** + * 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); - - 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 @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); - } - } - 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); - } - } + foreach ($pids as $pid) { + $this->removePid($pid); } } /** - * Remove all pids from the object and either the database or session. + * Remove all PIDs from the list. + * + * Individual backends could probably do this more efficiently, so feel free + * to override. */ 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 @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); - } - } - else { - $superserial = unserialize($_SESSION['islandora_bookmark'][$this->bookmarkId]); - $superserial->pidList = array(); - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($superserial); - } + $this->removePids($this->getPids()); } /** - * Removes the given users from the object and either the database or session. + * Remove multiple users from the list. * - * @global type $user + * Individual backends could probably do this more efficiently, so feel free + * to override. * * @param array $users - * An array of users. + * An array of integers, each representing a user ID. */ public function removeUsers($users) { - 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 @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); - } - } - 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); - } - } + foreach ($users as $user) { + $this->removeUser($user); } } /** * Adds the specified pids to the object and either the database or session. * - * @param array $pids - * An array of pids. + * Individual backends could probably do this more efficiently, so feel free + * to override. * - * @throws Exception + * @param array $pids + * An array of strings, representing object PIDs. */ 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 from the list @bookmarkname", array("@bookmarkname" => $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))); - } + $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 users. + * An array of integers, representings user IDs. */ public function addUsers($users) { - foreach ($users as $key => $value) { - if (!in_array($value, $this->userList)) { - array_push($this->userList, $value); - - if (!$this->bookmarkSession) { - if ($this->managementAccess()) { - $this->addUserDB($value); - } - else { - throw new Exception(t("You do not have access to add users from the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); - } - } - else { - $superserial = unserialize($_SESSION['islandora_bookmark'][$this->bookmarkId]); - $superserial->userList[] = $value; - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($superserial); - } - } - } - } - - /** - * Inserts a record into the database or adds the object to the session. - */ - public function createList() { - $this->bookmarkOwner = $this->userId; - - if (!$this->bookmarkSession) { - $fields = array( - 'listname' => $this->bookmarkName, - 'listowner' => $this->userId, - ); - $list_id = db_insert('islandora_bookmark_list_names')->fields($fields)->execute(); - - $this->bookmarkId = $list_id; - - 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, - ); - db_insert('islandora_bookmark_list_pids')->fields($fields)->execute(); - } - } - else { - // Add to session. - if (isset($_SESSION['islandora_bookmark'])) { - $index = count($_SESSION['islandora_bookmark']); - } - else { - $index = 0; - } - $this->bookmarkId = 'session_' . $index; - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($this); + foreach ($users as $user) { + $this->addUser($user); } } /** - * Removes the record from the database or the object from the session. + * Create a new list for the current user. + * + * @param string $name + * A name for the newly created list. */ - 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(); - } + public static function createNewList($name) { + global $user; - foreach ($this->pidList as $key => $value) { - db_delete('islandora_bookmark_list_pids') - ->condition('listid', $this->bookmarkId) - ->execute(); - } - } - else { - throw new Exception(t("You do not have access to remove the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); - } + if ($user->uid === 0) { + return BookmarkSession::createNewList($name); } else { - unset($_SESSION['islandora_bookmark'][$this->bookmarkId]); + return BookmarkDatabase::createNewList($name); } } /** - * Removes the specified user from the object's entry in the database. - * - * @param int $user_delete - * The user to be removed. - */ - 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(); - } - - /** - * Adds the specified user to the object's entry in the database. + * Removes the list from the backend. * - * @param int $user_add - * The user to be added. + * @deprecated Just here to conform with the old API. */ - protected function addUserDB($user_add) { - $fields = array( - 'uid' => $user_add, - 'listid' => $this->bookmarkId, - ); - db_insert('islandora_bookmark_list_users')->fields($fields)->execute(); - } - - /** - * Removes the specified pid from the object's entry in the database. - * - * @param string $pid - * The pid to be removed. - */ - protected function removePidDB($pid) { - $and = db_and()->condition('pidid', $pid)->condition('listid', $this->bookmarkId); - db_delete('islandora_bookmark_list_pids')->condition($and)->execute(); - } - - /** - * Add the specified pid to the object's entry in the database. - * - * @param string $pid - * The user to be added. - */ - protected function addPidDB($pid) { - $fields = array( - 'listid' => $this->bookmarkId, - 'pidid' => $pid, - ); - db_insert('islandora_bookmark_list_pids')->fields($fields)->execute(); + public function removeList() { + $this->delete(); } /** @@ -384,17 +257,6 @@ class Bookmark { */ 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(); - } - else { - $_SESSION['islandora_bookmark'][$this->bookmarkId] = serialize($this); - } } /** @@ -404,7 +266,8 @@ class Bookmark { * TRUE if they can execute management functions, FALSE otherwise. */ public function managementAccess() { - if ($this->userId === $this->bookmarkOwner) { + global $user; + if ($user->uid === $this->bookmarkOwner) { return TRUE; } else { diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc new file mode 100644 index 0000000..b9b1788 --- /dev/null +++ b/includes/bookmark_db.inc @@ -0,0 +1,258 @@ +getMapped(). + * + * @var + */ + protected $magic_map = array( + 'bookmarkName' => 'listname', + 'bookmarkOwner' => 'listowner', + ); + + /** + * Constructor for the Bookmark object. + */ + public function __construct($list_id) { + parent::__construct($list_id, NULL, NULL); + + foreach (array_keys($this->magic_map) 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->magic_map[$property])) { + return $this->magic_map[$property]; + } + else { + throw new Exception(format_string('Unknown property "@property".', array( + '@property' => $property, + ))); + } + } + + /** + * Magic property... Map to our column name, and set it. + */ + public function __set($property, $value) { + db_update('islandora_bookmark_list_names') + ->fields(array( + $this->getMapped($property) => $value, + )) + ->condition('listid', $this->bookmarkId) + ->execute(); + return $value; + } + + /** + * Magic property... Map to our column name, and get it. + */ + public function __get($property) { + return db_select('islandora_bookmark_list_names', 'n') + ->fields('n', array($this->getMapped($property))) + ->condition('listid', $this->bookmarkId) + ->execute() + ->fetchField(); + } + + /** + * Branch to different classes based on type. + * + * Falls back to the current class if a mapping for the type does not exist. + * + * @param mixed $id + * A bookmark list ID. + * + * @return Bookmark + * A subclass of Bookmark, to which we can add and remove items. + */ + public static function getList($id) { + $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 new static($id); + } + } + + /** + * 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(); + } + + /** + * {@inheritdoc} + */ + public function getPids($offset = 0, $limit = -1) { + $s = $this->getPidQuery(); + + if ($limit > 0) { + $s->range($offset, $limit); + } + + return $s->execute()->fetchAll(PDO::FETCH_COLUMN); + } + + /** + * {@inheritdoc} + */ + 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. + * @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); + } + + /** + * {@inheritdoc} + */ + public function delete() { + 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(); + } + + foreach ($this->pidList as $key => $value) { + db_delete('islandora_bookmark_list_pids') + ->condition('listid', $this->bookmarkId) + ->execute(); + } + } + else { + throw new Exception(t("You do not have access to remove the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); + } + } + + /** + * {@inheritdoc} + */ + public function removeUser($user_delete) { + db_delete('islandora_bookmark_list_users') + ->condition('uid', $user_delete) + ->condition('listid', $this->bookmarkId) + ->execute(); + } + + /** + * {@inheritdoc} + */ + public function addUser($user_add) { + $fields = array( + 'uid' => $user_add, + 'listid' => $this->bookmarkId, + ); + db_insert('islandora_bookmark_list_users')->fields($fields)->execute(); + } + + /** + * {@inheritdoc} + */ + public function removePid($pid) { + db_delete('islandora_bookmark_list_pids') + ->condition('pidid', $pid) + ->condition('listid', $this->bookmarkId) + ->execute(); + } + + /** + * {@inheritdoc} + */ + public function addPid($pid) { + $fields = array( + 'listid' => $this->bookmarkId, + 'pidid' => $pid, + ); + db_insert('islandora_bookmark_list_pids')->fields($fields)->execute(); + } + + /** + * {@inheritdoc} + */ + public function getUsers() { + return db_select('islandora_bookmark_list_users', 'u') + ->fields('u', array('uid')) + ->condition('listid', $this->bookmarkId) + ->execute() + ->fetchAll(PDO::FETCH_COLUMN); + } +} diff --git a/includes/bookmark_rss.inc b/includes/bookmark_rss.inc index d6c6317..555c7de 100644 --- a/includes/bookmark_rss.inc +++ b/includes/bookmark_rss.inc @@ -21,7 +21,7 @@ function islandora_bookmark_rss($list_id) { // Get bookmark based on boookmark list id. $bookmark = islandora_bookmark_get_bookmark_by_number($list_id); // Get all the pid belonging to that bookmark. - $all_pids = $bookmark->pidList; + $all_pids = $bookmark->getPids(); // Loop through all the pids and build the formatted RSS items. $items = NULL; diff --git a/includes/bookmark_session.inc b/includes/bookmark_session.inc new file mode 100644 index 0000000..c1c01b5 --- /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(); + } + + /** + * {@inheritdoc} + */ + public static function getList($id) { + if(isset($_SESSION['islandora_bookmark'][$id])) { + return $_SESSION['islandora_bookmark'][$id]; + } + return NULL; + } + + /** + * {@inheritdoc} + */ + 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); + + unset($_SESSION['islandora_bookmark']); + $_SESSION['islandora_bookmark'][$list_id] = new static($name); + + return static::getList($list_id); + } + + /** + * {@inheritdoc} + */ + public function delete() { + unset($_SESSION['islandora_bookmark'][$this->bookmarkId]); + } + + /** + * {@inheritdoc} + */ + public function getUsers() { + return array( + $this->bookmarkOwner, + ); + } + + /** + * {@inheritdoc} + */ + public function removeUser($user_delete) { + // No-op. + } + + /** + * {@inheritdoc} + */ + public function addUser($user_add) { + // No-op. + } + + /** + * {@inheritdoc} + */ + public function removePid($pid) { + $this->pidList = array_diff($this->pidList, array($pid)); + } + + /** + * {@inheritdoc} + */ + public function addPid($pid) { + $this->pidList[] = $pid; + } +} diff --git a/includes/solr_results.inc b/includes/solr_results.inc index bfa8387..c274e88 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -235,7 +235,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { )); foreach ($user_bookmarks as $bookmark) { - $save_options[$bookmark->bookmarkId] = $bookmark->bookmarkName; + $save_options[$bookmark->getId()] = $bookmark->bookmarkName; } $group = array( @@ -429,8 +429,8 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { 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->pidList); - $skipped_pids = array_intersect($pids, $bookmark_object->pidList); + $pids_to_add = array_diff($pids, $bookmark_object->getPids()); + $skipped_pids = array_intersect($pids, $bookmark_object->getPids()); if (count($pids_to_add) > 0) { try { 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.module b/islandora_bookmark.module index e15bcd6..f220fb6 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -242,16 +242,14 @@ 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); + $bookmark_session = $value; + $anon_index = array_search(0, $bookmark_session->getUsers(), 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->userId = $account->uid; + $bookmark_session->userList[$anon_index] = $account->uid; $bookmark_session->bookmarkSession = FALSE; $bookmark_session->createList(); unset($_SESSION['islandora_bookmark'][$key]); @@ -263,7 +261,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()); } /** @@ -315,7 +313,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['bookmark_object']->getId()); } /** @@ -521,13 +519,14 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list ); // This is the RSS icon link. - if (!empty($bookmark->pidList) && !$bookmark->bookmarkSession) { + if (!$bookmark instanceof BookmarkSession && $bookmark->getPidCount() > 0) { + $id = $bookmark->getId(); $form['bookmark_rss'] = array( '#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/$bookmark->bookmarkId/rss", array('html' => TRUE)), + )), "islandora-bookmark/listid/$id/rss", array('html' => TRUE)), '#prefix' => '
', '#suffix' => '
', ); @@ -545,8 +544,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list // 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); + $num_pids = $bookmark->getPidCount(); // Incase we get a page request > than the pages, Drupal will // show the last page automatically, need to reflect the pids populated @@ -566,7 +564,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list $offset = $page * $num_eles; } - $pids = array_slice($all_pids, $offset, $num_eles); + $pids = $bookmark->getPids($offset, $num_eles); // Loop the pids and determine how to display the markup. foreach ($pids as $pid) { @@ -574,7 +572,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list 'markup' => islandora_bookmark_generate_markup($pid), ); } - pager_default_initialize(count($all_pids), $num_eles); + pager_default_initialize($num_pids, $num_eles); $form['bookmarks']['fieldset']['pager'] = array( '#markup' => theme('pager'), '#weight' => -1, @@ -751,10 +749,13 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list '#suffix' => '', ); - if (count($bookmark->userList) == 1) { + $users = $bookmark->getUsers(); + if (count($users) < 2) { $fku['empty'] = array( '#type' => 'item', - '#markup' => t('@type list is not shared with other users.', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), + '#markup' => t('@type list is not shared with other users.', array( + '@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')), + )), ); } else { @@ -788,7 +789,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list ), 'list-id' => array( '#type' => 'value', - '#value' => $bookmark->bookmarkId, + '#value' => $bookmark->getId(), ), 'remove' => array( '#type' => 'button', @@ -830,10 +831,9 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list ); } } - foreach ($options as $index => $item) { - if (!in_array($index, $bookmark->userList)) { - $selectoptions[$index] = $item['uid'] . ': ' . $item['name']; - } + $available = array_diff_key($options, drupal_map_assoc($bookmark->getUsers())); + foreach ($available as $index => $item) { + $selectoptions[$index] = $item['uid'] . ': ' . $item['name']; } $fku['listusers'] = array( '#type' => 'select', @@ -917,11 +917,11 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state 'page' => $current_page, ), ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId, array('query' => $query)); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId(), array('query' => $query)); drupal_goto($destination['path'], $query); } elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); drupal_goto($destination['path']); } else { @@ -930,7 +930,7 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state 'page' => $current_page - 1, ), ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); drupal_goto($destination['path'], $query); } } @@ -1003,11 +1003,11 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state 'page' => $current_page, ), ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId, array('query' => $query)); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId(), array('query' => $query)); drupal_goto($destination['path'], $query); } elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); drupal_goto($destination['path']); } else { @@ -1016,7 +1016,7 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state 'page' => $current_page - 1, ), ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->bookmarkId); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); drupal_goto($destination['path'], $query); } } @@ -1026,7 +1026,7 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_all') { $bookmark = $form_state['bookmark_object']; - $remove_count = count($bookmark->pidList); + $remove_count = $bookmark->getPidCount(); $bookmark->removeAllPids(); drupal_set_message(t('All PIDs have been removed from the @remove_list list.', @@ -1037,20 +1037,10 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state // 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++; - } - } + $pids = $bookmark->getPids(); + $all_pids = array_filter($pids, 'islandora_object_load'); + $broke_pids = array_diff($pids, $all_pids); + $broke_count = count($broke_pids); $key = $form_state['input']['islandora_bookmark_export_options_select']; if ($key !== 'default' && count($all_pids) > 0) { @@ -1066,9 +1056,14 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } 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))); + $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' => implode(', ', $broke_pids), + ) + ); drupal_set_message($broken_pids, 'error'); } } @@ -1129,7 +1124,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; @@ -1183,7 +1178,7 @@ function islandora_bookmark_fedora_repository_object_form(array $form, array &$f $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( @@ -1203,8 +1198,8 @@ 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; } } @@ -1407,12 +1402,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'), @@ -1427,8 +1416,9 @@ function islandora_bookmark_form_overview_table(array $bookmark_objs) { $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, ); } @@ -1490,20 +1480,7 @@ 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 ' . variable_get('islandora_bookmark_type', 'bookmark') . ' lists; lists created anonymously will not be saved.'; @@ -1630,3 +1607,12 @@ function islandora_bookmark_islandora_object_purged($pid) { ->condition('pidid', $pid) ->execute(); } + +/** + * Implements hook_islandora_bookmark_database_types(). + */ +function islandora_bookmark_islandora_bookmark_database_types() { + return array( + 'bookmark' => 'BookmarkDatabase', + ); +} From 1008b047970d3458036c6751616e7f6a11e0bd47 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 11 Sep 2013 18:55:58 +0000 Subject: [PATCH 36/57] Update documentation. --- islandora_bookmark.api.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/islandora_bookmark.api.php b/islandora_bookmark.api.php index 6604e23..92bd06f 100644 --- a/islandora_bookmark.api.php +++ b/islandora_bookmark.api.php @@ -57,11 +57,11 @@ function hook_islandora_bookmark_export_styles($option) { /** * Hook to change or add values to RSS fields. * - * Somtimes you might want to alter fields for an rss item. - * + * 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. */ @@ -87,3 +87,21 @@ function hook_islandora_bookmark_rss_item(AbstractObject $object) { // 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_list_names() { + return array( + 'my_type' => 'my_awesome_bookmark_class', + ); +} From fc6b99fb61ed6cde002806973d62c248229816c0 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 11 Sep 2013 19:10:47 +0000 Subject: [PATCH 37/57] Fix error left in while testing... --- includes/bookmark_session.inc | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/bookmark_session.inc b/includes/bookmark_session.inc index c1c01b5..29f2c98 100644 --- a/includes/bookmark_session.inc +++ b/includes/bookmark_session.inc @@ -83,7 +83,6 @@ class BookmarkSession extends Bookmark { $list_id = static::getKey($name); - unset($_SESSION['islandora_bookmark']); $_SESSION['islandora_bookmark'][$list_id] = new static($name); return static::getList($list_id); From ef1f5923db6816ec176abca1d7299f27d7f4775d Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 11 Sep 2013 19:24:34 +0000 Subject: [PATCH 38/57] Fix more logicks... --- includes/bookmark_db.inc | 4 ++-- islandora_bookmark.module | 19 +++++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index b9b1788..7b84772 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -186,13 +186,13 @@ class BookmarkDatabase extends Bookmark { ->condition('listid', $this->bookmarkId) ->execute(); - foreach ($this->userList as $key => $value) { + foreach ($this->getUsers() as $key => $value) { db_delete('islandora_bookmark_list_users') ->condition('listid', $this->bookmarkId) ->execute(); } - foreach ($this->pidList as $key => $value) { + foreach ($this->getPids() as $key => $value) { db_delete('islandora_bookmark_list_pids') ->condition('listid', $this->bookmarkId) ->execute(); diff --git a/islandora_bookmark.module b/islandora_bookmark.module index f220fb6..53bb592 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -244,16 +244,11 @@ function islandora_bookmark_theme() { */ function islandora_bookmark_user_login(&$edit, $account) { if (!empty($_SESSION['islandora_bookmark'])) { - foreach ($_SESSION['islandora_bookmark'] as $key => $value) { - $bookmark_session = $value; - $anon_index = array_search(0, $bookmark_session->getUsers(), TRUE); - // Replace the anon who's logged in with their associated uid. - $bookmark_session->userId = $account->uid; - $bookmark_session->userList[$anon_index] = $account->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']); } } @@ -763,8 +758,8 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list '#markup' => '

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

', ); // Remove the user from the populated shared users. - $key = array_search($user->uid, $bookmark->userList); - $bookmark_users = $bookmark->userList; + $bookmark_users = $bookmark->getUsers(); + $key = array_search($user->uid, $bookmark_users); unset($bookmark_users[$key]); foreach ($bookmark_users as $itr => $uid) { @@ -897,7 +892,7 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } if ($match) { - $bookmark = $form_state['bookmark_object']; + $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); $bookmark->removePids(array($match[1])); $removed_object = islandora_object_load($match[1]); From dfc53bdd93f5eec44c065a74215d4fe30b6aaad1 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 11 Sep 2013 19:35:04 +0000 Subject: [PATCH 39/57] Add flag indicating persistence of a list. --- includes/bookmark.inc | 17 +++++++++++++++++ includes/bookmark_db.inc | 1 + includes/bookmark_session.inc | 1 + islandora_bookmark.module | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/includes/bookmark.inc b/includes/bookmark.inc index 66ace79..c873224 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -92,6 +92,13 @@ abstract class Bookmark implements BookmarkInterface { */ protected $bookmarkId; + /** + * Flag whether or not the given backend is persistent. + * + * @var bool + */ + protected $isPersistent; + /** * Constructor for the Bookmark object. */ @@ -111,6 +118,16 @@ abstract class Bookmark implements BookmarkInterface { return $this->bookmarkId; } + /** + * Accessor for the persistence. + * + * @return mixed + * The value of $this->isPersistent. + */ + public function getIsPersistent() { + return $this->isPersistent; + } + /** * Get the given (existing) list. * diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index 7b84772..2986a7e 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -25,6 +25,7 @@ class BookmarkDatabase extends Bookmark { */ public function __construct($list_id) { parent::__construct($list_id, NULL, NULL); + $this->isPersistent = TRUE; foreach (array_keys($this->magic_map) as $for_magic) { unset($this->$for_magic); diff --git a/includes/bookmark_session.inc b/includes/bookmark_session.inc index 29f2c98..317144b 100644 --- a/includes/bookmark_session.inc +++ b/includes/bookmark_session.inc @@ -39,6 +39,7 @@ class BookmarkSession extends Bookmark { global $user; parent::__construct(static::getKey($name), $name, $user->uid); $this->pidList = array(); + $this->isPersistent = FALSE; } /** diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 53bb592..c8d55ee 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -514,7 +514,7 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list ); // This is the RSS icon link. - if (!$bookmark instanceof BookmarkSession && $bookmark->getPidCount() > 0) { + if ($bookmark->getIsPersistent() && $bookmark->getPidCount() > 0) { $id = $bookmark->getId(); $form['bookmark_rss'] = array( '#markup' => l(theme_image(array( From 3984d30db4de01082236a3c0d0d22b431ba5fc8d Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 11 Sep 2013 19:37:49 +0000 Subject: [PATCH 40/57] Fix an error in the docs... --- islandora_bookmark.api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/islandora_bookmark.api.php b/islandora_bookmark.api.php index 92bd06f..464311e 100644 --- a/islandora_bookmark.api.php +++ b/islandora_bookmark.api.php @@ -100,7 +100,7 @@ function hook_islandora_bookmark_rss_item(AbstractObject $object) { * "islandora_bookmark_list_names" to a class to use to interact with the * given list. */ -function hook_islandora_bookmark_list_names() { +function hook_islandora_bookmark_database_types() { return array( 'my_type' => 'my_awesome_bookmark_class', ); From 1200180d0d95b5d31af653e2e4574f2585dd53ce Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 11 Sep 2013 19:47:13 +0000 Subject: [PATCH 41/57] Coding standards. --- includes/bookmark_db.inc | 26 +++++++++++++------------- includes/bookmark_session.inc | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index 2986a7e..2437140 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -13,9 +13,9 @@ class BookmarkDatabase extends Bookmark { /** * Mapping for use in $this->getMapped(). * - * @var + * @var array */ - protected $magic_map = array( + protected $magicMap = array( 'bookmarkName' => 'listname', 'bookmarkOwner' => 'listowner', ); @@ -27,7 +27,7 @@ class BookmarkDatabase extends Bookmark { parent::__construct($list_id, NULL, NULL); $this->isPersistent = TRUE; - foreach (array_keys($this->magic_map) as $for_magic) { + foreach (array_keys($this->magicMap) as $for_magic) { unset($this->$for_magic); } } @@ -49,8 +49,8 @@ class BookmarkDatabase extends Bookmark { * If we do not have a mapping for the given exception, throw an exception. */ protected function getMapped($property) { - if (isset($this->magic_map[$property])) { - return $this->magic_map[$property]; + if (isset($this->magicMap[$property])) { + return $this->magicMap[$property]; } else { throw new Exception(format_string('Unknown property "@property".', array( @@ -125,7 +125,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function getPids($offset = 0, $limit = -1) { $s = $this->getPidQuery(); @@ -138,7 +138,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function getPidCount() { $s = $this->getPidQuery()->countQuery(); @@ -179,7 +179,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function delete() { if ($this->managementAccess()) { @@ -205,7 +205,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function removeUser($user_delete) { db_delete('islandora_bookmark_list_users') @@ -215,7 +215,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function addUser($user_add) { $fields = array( @@ -226,7 +226,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function removePid($pid) { db_delete('islandora_bookmark_list_pids') @@ -236,7 +236,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function addPid($pid) { $fields = array( @@ -247,7 +247,7 @@ class BookmarkDatabase extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function getUsers() { return db_select('islandora_bookmark_list_users', 'u') diff --git a/includes/bookmark_session.inc b/includes/bookmark_session.inc index 317144b..6363385 100644 --- a/includes/bookmark_session.inc +++ b/includes/bookmark_session.inc @@ -13,7 +13,7 @@ class BookmarkSession extends Bookmark { /** * Place to store the list of keys in the session. * - * @var + * @var array */ protected $pidList; @@ -43,17 +43,17 @@ class BookmarkSession extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public static function getList($id) { - if(isset($_SESSION['islandora_bookmark'][$id])) { + if (isset($_SESSION['islandora_bookmark'][$id])) { return $_SESSION['islandora_bookmark'][$id]; } return NULL; } /** - * {@inheritdoc} + * Inherits. */ public function getPids($offset = 0, $limit = -1) { if ($limit > 0) { @@ -90,14 +90,14 @@ class BookmarkSession extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function delete() { unset($_SESSION['islandora_bookmark'][$this->bookmarkId]); } /** - * {@inheritdoc} + * Inherits. */ public function getUsers() { return array( @@ -106,28 +106,28 @@ class BookmarkSession extends Bookmark { } /** - * {@inheritdoc} + * Inherits. */ public function removeUser($user_delete) { // No-op. } /** - * {@inheritdoc} + * Inherits. */ public function addUser($user_add) { // No-op. } /** - * {@inheritdoc} + * Inherits. */ public function removePid($pid) { $this->pidList = array_diff($this->pidList, array($pid)); } /** - * {@inheritdoc} + * Inherits. */ public function addPid($pid) { $this->pidList[] = $pid; From fef62c89628fdf6c1f9cd5ee00d216d6bb96bfe7 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 12 Sep 2013 13:16:20 +0000 Subject: [PATCH 42/57] Make type available. --- includes/bookmark_db.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index 2437140..5d0a6d1 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -18,6 +18,7 @@ class BookmarkDatabase extends Bookmark { protected $magicMap = array( 'bookmarkName' => 'listname', 'bookmarkOwner' => 'listowner', + 'type' => 'type', ); /** From 7d85e57ae69dfeb759ff637071d710796f86b5c5 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 12 Sep 2013 14:10:16 +0000 Subject: [PATCH 43/57] Allow for graceful deprecation of old "userList" and "pidList" properties. --- includes/bookmark.inc | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/includes/bookmark.inc b/includes/bookmark.inc index c873224..9a9eb99 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -108,6 +108,46 @@ abstract class Bookmark implements BookmarkInterface { $this->bookmarkOwner = $owner; } + /** + * 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(); + } + 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(); + } + } + + /** + * 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; + } + 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. * From 7a94afb154676823b77e062f5626ac3e2a5a45bd Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 12 Sep 2013 14:37:16 +0000 Subject: [PATCH 44/57] Use our other methods when deleting lists. --- includes/bookmark_db.inc | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index 5d0a6d1..d1fb951 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -153,7 +153,7 @@ class BookmarkDatabase extends Bookmark { * @param string $name * The name for the list. * @param string $type - * A string indicating the type of list. + * 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. @@ -184,21 +184,11 @@ class BookmarkDatabase extends Bookmark { */ 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(); - - foreach ($this->getUsers() as $key => $value) { - db_delete('islandora_bookmark_list_users') - ->condition('listid', $this->bookmarkId) - ->execute(); - } - - foreach ($this->getPids() as $key => $value) { - db_delete('islandora_bookmark_list_pids') - ->condition('listid', $this->bookmarkId) - ->execute(); - } } else { throw new Exception(t("You do not have access to remove the list @bookmarkname", array("@bookmarkname" => $this->bookmarkName))); From f00db3c56ad50ea1440ad975c0b1f3ddcd4d3759 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 12 Sep 2013 16:03:33 +0000 Subject: [PATCH 45/57] Make some logic more concise. --- islandora_bookmark.module | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index c8d55ee..1347591 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -546,18 +546,12 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list // 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; + $page = min($_GET['page'], $calc_pages); } else { - if ($page > $calc_pages) { - $page = $calc_pages; - } - $offset = $page * $num_eles; + $page = 0; } + $offset = $page * $num_eles; $pids = $bookmark->getPids($offset, $num_eles); From 480012ec10e3f37fd54528d8dddf4b64fb0f5f17 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 12 Sep 2013 19:47:29 +0000 Subject: [PATCH 46/57] Move around some form code so it should be easier to reuse. --- includes/bookmark_db.inc | 33 ++-- islandora_bookmark.module | 330 ++++++++++++++++++++------------------ 2 files changed, 194 insertions(+), 169 deletions(-) diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index d1fb951..af5a261 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -54,7 +54,8 @@ class BookmarkDatabase extends Bookmark { return $this->magicMap[$property]; } else { - throw new Exception(format_string('Unknown property "@property".', array( + return FALSE; + trigger_error(format_string('Unknown property "@property".', array( '@property' => $property, ))); } @@ -64,24 +65,30 @@ class BookmarkDatabase extends Bookmark { * Magic property... Map to our column name, and set it. */ public function __set($property, $value) { - db_update('islandora_bookmark_list_names') - ->fields(array( - $this->getMapped($property) => $value, - )) - ->condition('listid', $this->bookmarkId) - ->execute(); - return $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) { - return db_select('islandora_bookmark_list_names', 'n') - ->fields('n', array($this->getMapped($property))) - ->condition('listid', $this->bookmarkId) - ->execute() - ->fetchField(); + $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(); + } } /** diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 1347591..37139c8 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -482,16 +482,16 @@ 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); + + $bookmark = Bookmark::getList($list_id); if (module_exists('citation_exporter')) { if (CitationExporter::ReadyToExport()) { @@ -509,15 +509,30 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list $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 ($bookmark->getIsPersistent() && $bookmark->getPidCount() > 0) { $id = $bookmark->getId(); $form['bookmark_rss'] = array( - '#markup' => l(theme_image(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(), @@ -526,31 +541,15 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list '#suffix' => '', ); } - $form['bookmarks'] = array( - 'fieldset' => array( - '#type' => 'fieldset', - '#title' => 'Detailed List', - '#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 = $bookmark->getPidCount(); + $pager_element = 0; - // 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 = min($_GET['page'], $calc_pages); - } - else { - $page = 0; - } + $page = pager_default_initialize($num_pids, $num_eles, $pager_element); $offset = $page * $num_eles; $pids = $bookmark->getPids($offset, $num_eles); @@ -561,20 +560,15 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list 'markup' => islandora_bookmark_generate_markup($pid), ); } - pager_default_initialize($num_pids, $num_eles); - $form['bookmarks']['fieldset']['pager'] = array( - '#markup' => theme('pager'), - '#weight' => -1, - ); + $row_count = count($rows); $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); + $form_state['islandora_bookmark_pids_on_page'] = $row_count; if ($bookmark->bookmarkOwner === $user->uid) { $header['remove'] = array('data' => t('Operations')); - foreach ($pids as $key => $pid) { + foreach ($rows as $pid => &$row) { $remove_button = array( '#type' => 'submit', '#name' => 'remove_pid_' . $pid, @@ -584,94 +578,116 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list '#value' => t('Remove'), ); - $rows[$pid]['remove'] = array( + $row['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', - ); - } + // XXX: Silly looping with references... + unset($row); } - if (count($rows)) { - $form['bookmarks']['fieldset']['table'] = array( + + $form['bookmarks']['fieldset'] += array( + 'table' => array( '#header' => $header, '#type' => 'tableselect', '#options' => $rows, '#empty' => t('@type list is empty.', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), '#weight' => -2, - ); - } - else { - $form['bookmarks']['fieldset']['table'] = array( - '#markup' => t('@type list is empty.', array('@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')))), - ); - } + ), + 'pager' => array( + '#markup' => theme('pager', array( + 'element' => $pager_element, + )), + '#weight' => -1, + ), + ); - if (count($pids) != 0 && user_access('export islandora bookmarks')) { - $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", 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); - } + if ($row_count > 0) { + if ($bookmark->bookmarkOwner === $user->uid) { + $form['bookmarks']['fieldset'] += array( + 'remove_select' => array( + '#type' => 'submit', + '#value' => t('Remove selected'), + '#name' => 'islandora_bookmark_remove_selected', + ), + 'remove_all' => array( + '#type' => 'submit', + '#value' => t('Remove all'), + '#name' => 'islandora_bookmark_remove_all', + ), + ); } - foreach ($export_styles as $key => $value) { - if (count($export_styles[$key])) { - $form['bookmarks']['fieldset']['format_opts']['export_styles'][$key] = array( + + 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); + } + } + + $style_elements = array(); + foreach ($export_styles as $key => $value) { + if (!empty($value)) { + $style_elements[$key] = array( + '#type' => 'select', + '#title' => t('Style'), + '#options' => $value, + '#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'] += array( + 'export_title' => array( + '#markup' => '

' . t('Export') . '

', + '#weight' => 3, + ), + 'export_options' => array( '#type' => 'select', - '#title' => t('Style'), - '#options' => $export_styles[$key], + '#title' => t('Format'), + '#options' => $export_options, '#weight' => 4, - '#name' => 'islandora_bookmark_export_styles_' . $key, - '#states' => array( - 'visible' => array( - ':input[name="islandora_bookmark_export_options_select"]' => array('value' => $key), - ), + '#name' => 'islandora_bookmark_export_options_select', + ), + 'format_opts' => array( + '#weight' => 4, + 'export_styles' => $style_elements, + 'export_selected_submit' => 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', + )), + '#name' => 'export_selected', + '#weight' => 5, ), - ); - } + 'export_all_submit' => 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' + )), + '#name' => 'export_all', + '#weight' => 6, + ), + ), + ); } - - $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 @type', array('@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's')), - '#name' => 'export_selected', - '#weight' => 5, - ); - $form['bookmarks']['fieldset']['format_opts']['export_all_submit'] = array( - '#type' => 'submit', - '#value' => t('Export all @type', array('@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's')), - '#name' => 'export_all', - '#weight' => 6, - ); } // Share. @@ -689,21 +705,12 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list 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, - ) - )); + 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') { @@ -726,20 +733,10 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list } $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 ($bookmark->getIsPersistent()) { $users = $bookmark->getUsers(); - if (count($users) < 2) { + if (count($users) === 0) { $fku['empty'] = array( '#type' => 'item', '#markup' => t('@type list is not shared with other users.', array( @@ -754,7 +751,9 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list // Remove the user from the populated shared users. $bookmark_users = $bookmark->getUsers(); $key = array_search($user->uid, $bookmark_users); - unset($bookmark_users[$key]); + if ($key !== FALSE) { + unset($bookmark_users[$key]); + } foreach ($bookmark_users as $itr => $uid) { $tempuser = user_load($uid); @@ -798,7 +797,9 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list } } - $selectoptions = array("" => t("- Select user -")); + $selectoptions = array( + '' => t('- Select user -'), + ); if ($user->uid != 0) { $options['0'] = array( 'uid' => 0, @@ -806,20 +807,18 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list ); } // Get all users for use in select for forms. - $result = db_query("SELECT uid, name FROM {users} WHERE STATUS != 0 ORDER BY uid"); - - foreach ($result as $record) { - if ($record->uid != $user->uid) { - $resultname = $record->name; - if ($record->uid == 0) { - $resultname = t('Everyone'); - } - $options[$record->uid] = array( - 'uid' => $record->uid, - 'name' => $resultname, - ); - } + $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($bookmark->getUsers())); foreach ($available as $index => $item) { $selectoptions[$index] = $item['uid'] . ': ' . $item['name']; @@ -843,7 +842,15 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list 'method' => 'replace', ), ); - $form['bookmarks']['users'] += $fku; + + $form['bookmarks']['users'] = array( + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => FALSE, + '#title' => t('Share'), + '#prefix' => '
', + '#suffix' => '
', + ) + $fku; } } } @@ -861,6 +868,21 @@ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list return $form; } +function islandora_bookmark_detailed_form_validate(&$form, &$form_state) { + $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); + if (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' => $bookmark->bookmarkName, + ))); + } + } + } +} + /** * Submit handler for the detailed view of a Bookmark. * @@ -885,18 +907,17 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } } + $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); + if ($match) { - $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); $bookmark->removePids(array($match[1])); $removed_object = islandora_object_load($match[1]); - drupal_set_message(t('The @type @objectname has been removed from @listname.', - array( - '@type' => variable_get('islandora_bookmark_type', 'bookmark'), - '@listname' => $bookmark->bookmarkName, - '@objectname' => $removed_object->label, - ) - )); + drupal_set_message(t('The @type @objectname has been removed from @listname.', array( + '@type' => variable_get('islandora_bookmark_type', 'bookmark'), + '@listname' => $bookmark->bookmarkName, + '@objectname' => $removed_object->label, + ))); $current_page = $form_state['islandora_bookmark_current_page']; $current_pids = $form_state['islandora_bookmark_pids_on_page']; @@ -974,7 +995,6 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } } if (count($pids) > 0) { - $bookmark = $form_state['bookmark_object']; $bookmark->removePids($pids); drupal_set_message(t('The selected @type have been removed from @listname.', array( @@ -1014,7 +1034,6 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } } elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_all') { - $bookmark = $form_state['bookmark_object']; $remove_count = $bookmark->getPidCount(); $bookmark->removeAllPids(); @@ -1025,7 +1044,6 @@ function islandora_bookmark_detailed_form_submit(array $form, array &$form_state } // Export all pids. else { - $bookmark = $form_state['bookmark_object']; $pids = $bookmark->getPids(); $all_pids = array_filter($pids, 'islandora_object_load'); $broke_pids = array_diff($pids, $all_pids); @@ -1077,9 +1095,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) { From a1c82fd936c71eac456398f03a69e17c850eea09 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 12 Sep 2013 20:28:25 +0000 Subject: [PATCH 47/57] Move form building/handling into bookmark class. ... Now we can override! --- includes/bookmark.inc | 578 +++++++++++++++++++++++++++++++++++++- includes/bookmark_db.inc | 3 +- islandora_bookmark.module | 577 +------------------------------------ 3 files changed, 590 insertions(+), 568 deletions(-) diff --git a/includes/bookmark.inc b/includes/bookmark.inc index 9a9eb99..e2c3dbd 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -42,7 +42,7 @@ interface BookmarkInterface { public function addUser($user); /** - * Get a list of all the users associated with a list. + * Get a list of all the users who a list is shared with. * * @return array * An array of integers, representing user IDs. @@ -81,7 +81,7 @@ abstract class Bookmark implements BookmarkInterface { /** * The owner of the object. * - * @var integer + * @var int */ public $bookmarkOwner; @@ -331,4 +331,578 @@ abstract class Bookmark implements BookmarkInterface { return FALSE; } } + + /** + * 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)), + ), + ); + drupal_add_html_head($citation_export_head, 'citation_exporter_head'); + } + } + + $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(); + $pager_element = 0; + + $page = pager_default_initialize($num_pids, $num_eles, $pager_element); + $offset = $page * $num_eles; + + $pids = $this->getPids($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), + ); + } + $row_count = count($rows); + + $header = array('markup' => array('data' => t('Label'))); + + $form_state['islandora_bookmark_current_page'] = $page; + $form_state['islandora_bookmark_pids_on_page'] = $row_count; + if ($this->managementAccess()) { + $header['remove'] = array('data' => t('Operations')); + foreach ($rows as $pid => &$row) { + $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', + ); + } + // XXX: Silly looping with references... + unset($row); + } + + $form['bookmarks']['fieldset'] += array( + 'table' => array( + '#header' => $header, + '#type' => 'tableselect', + '#options' => $rows, + '#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, + ), + ); + if ($row_count > 0) { + if ($this->managementAccess()) { + $form['bookmarks']['fieldset'] += array( + 'remove_select' => array( + '#type' => 'submit', + '#value' => t('Remove selected'), + '#name' => 'islandora_bookmark_remove_selected', + ), + 'remove_all' => array( + '#type' => 'submit', + '#value' => t('Remove all'), + '#name' => 'islandora_bookmark_remove_all', + ), + ); + } + + 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); + } + } + + $style_elements = array(); + foreach ($export_styles as $key => $value) { + if (!empty($value)) { + $style_elements[$key] = array( + '#type' => 'select', + '#title' => t('Style'), + '#options' => $value, + '#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'] += array( + 'export_title' => array( + '#markup' => '

' . t('Export') . '

', + '#weight' => 3, + ), + 'export_options' => array( + '#type' => 'select', + '#title' => t('Format'), + '#options' => $export_options, + '#weight' => 4, + '#name' => 'islandora_bookmark_export_options_select', + ), + 'format_opts' => array( + '#weight' => 4, + 'export_styles' => $style_elements, + 'export_selected_submit' => 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', + )), + '#name' => 'export_selected', + '#weight' => 5, + ), + 'export_all_submit' => 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', + )), + '#name' => 'export_all', + '#weight' => 6, + ), + ), + ); + } + } + + // 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)); + } + } + + $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 { + if ($user->uid != 0) { + $form['bookmarks']['remove_self'] = array( + '#type' => 'submit', + '#value' => t('Remove yourself from this list'), + '#submit' => array('islandora_bookmark_remove_self'), + ); + } + } + + return $form; + } + + /** + * Form validation handler. + */ + public function formValidate(&$form, &$form_state) { + if (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, + ))); + } + } + } + } + + /** + * Form submission handler. + */ + public function formSubmit(&$form, &$form_state) { + foreach ($_POST as $key => $value) { + preg_match('/remove_pid_(.*)/', $key, $match); + if ($match) { + break; + } + } + if ($match) { + $this->removePids(array($match[1])); + $removed_object = islandora_object_load($match[1]); + + 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, + ))); + $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/' . $this->getId(), array('query' => $query)); + drupal_goto($destination['path'], $query); + } + elseif ($current_page == 0) { + $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); + drupal_goto($destination['path']); + } + else { + $query = array( + 'query' => array( + 'page' => $current_page - 1, + ), + ); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); + 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 @type.', array('@type' => variable_get('islandora_bookmark_type', '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) { + $this->removePids($pids); + drupal_set_message(t('The selected @type have been removed from @listname.', + array( + '@listname' => $this->bookmarkName, + '@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's', + ) + )); + $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/' . $this->getId(), array('query' => $query)); + drupal_goto($destination['path'], $query); + } + elseif ($current_page == 0) { + $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); + drupal_goto($destination['path']); + } + else { + $query = array( + 'query' => array( + 'page' => $current_page - 1, + ), + ); + $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); + drupal_goto($destination['path'], $query); + } + } + else { + drupal_set_message(t('Please select at least one @type.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), 'error'); + } + } + elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_all') { + $remove_count = $this->getPidCount(); + $this->removeAllPids(); + + drupal_set_message(t('All PIDs have been removed from the @remove_list list.', + array( + '@remove_list' => $this->bookmarkName, + ))); + } + // Export all pids. + else { + $pids = $this->getPids(); + $all_pids = array_filter($pids, 'islandora_object_load'); + $broke_pids = array_diff($pids, $all_pids); + $broke_count = count($broke_pids); + + $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' => implode(', ', $broke_pids), + ) + ); + drupal_set_message($broken_pids, 'error'); + } + } + } + } } diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index af5a261..6ca0ae3 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -54,10 +54,10 @@ class BookmarkDatabase extends Bookmark { return $this->magicMap[$property]; } else { - return FALSE; trigger_error(format_string('Unknown property "@property".', array( '@property' => $property, ))); + return FALSE; } } @@ -251,6 +251,7 @@ class BookmarkDatabase extends Bookmark { 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/islandora_bookmark.module b/islandora_bookmark.module index 37139c8..36129e7 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -489,398 +489,16 @@ function islandora_bookmark_detailed_forms($list_id) { * The Drupal form definition. */ function islandora_bookmark_detailed_form(array $form, array &$form_state, $list_id) { - global $user; - $bookmark = Bookmark::getList($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(CITATION_EXPORTER_EXPORT_MENU, array('absolute' => TRUE)), - ), - ); - drupal_add_html_head($citation_export_head, 'citation_exporter_head'); - } - } - - $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 ($bookmark->getIsPersistent() && $bookmark->getPidCount() > 0) { - $id = $bookmark->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 = $bookmark->getPidCount(); - $pager_element = 0; - - $page = pager_default_initialize($num_pids, $num_eles, $pager_element); - $offset = $page * $num_eles; - - $pids = $bookmark->getPids($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), - ); - } - $row_count = count($rows); - - $header = array('markup' => array('data' => t('Label'))); - - $form_state['islandora_bookmark_current_page'] = $page; - $form_state['islandora_bookmark_pids_on_page'] = $row_count; - if ($bookmark->bookmarkOwner === $user->uid) { - $header['remove'] = array('data' => t('Operations')); - foreach ($rows as $pid => &$row) { - $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', - ); - } - // XXX: Silly looping with references... - unset($row); - } - - $form['bookmarks']['fieldset'] += array( - 'table' => array( - '#header' => $header, - '#type' => 'tableselect', - '#options' => $rows, - '#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, - ), - ); - - if ($row_count > 0) { - if ($bookmark->bookmarkOwner === $user->uid) { - $form['bookmarks']['fieldset'] += array( - 'remove_select' => array( - '#type' => 'submit', - '#value' => t('Remove selected'), - '#name' => 'islandora_bookmark_remove_selected', - ), - 'remove_all' => array( - '#type' => 'submit', - '#value' => t('Remove all'), - '#name' => 'islandora_bookmark_remove_all', - ), - ); - } - - 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); - } - } - - $style_elements = array(); - foreach ($export_styles as $key => $value) { - if (!empty($value)) { - $style_elements[$key] = array( - '#type' => 'select', - '#title' => t('Style'), - '#options' => $value, - '#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'] += array( - 'export_title' => array( - '#markup' => '

' . t('Export') . '

', - '#weight' => 3, - ), - 'export_options' => array( - '#type' => 'select', - '#title' => t('Format'), - '#options' => $export_options, - '#weight' => 4, - '#name' => 'islandora_bookmark_export_options_select', - ), - 'format_opts' => array( - '#weight' => 4, - 'export_styles' => $style_elements, - 'export_selected_submit' => 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', - )), - '#name' => 'export_selected', - '#weight' => 5, - ), - 'export_all_submit' => 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' - )), - '#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 = t('Everyone'); - } - 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)); - } - } - // 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' => $bookmark->bookmarkName, - '@username' => $output_user, - ) - )); - $bookmark->removeUsers(array($remove_user)); - } - } - - $fku = array(); - if ($bookmark->getIsPersistent()) { - - $users = $bookmark->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 = $bookmark->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' => $bookmark->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($bookmark->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 { - 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; - - return $form; + return $bookmark->formGet($form, $form_state); } +/** + * Form validation handler. + */ function islandora_bookmark_detailed_form_validate(&$form, &$form_state) { $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); - if (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' => $bookmark->bookmarkName, - ))); - } - } - } + $bookmark->formValidate($form, $form_state); } /** @@ -895,186 +513,15 @@ function islandora_bookmark_detailed_form_validate(&$form, &$form_state) { * 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; - } - } + $form_state['redirect'] = array( + request_path(), + array( + 'query' => drupal_get_query_parameters(), + ), + ); $bookmark = Bookmark::getList($form_state['build_info']['args'][0]); - - if ($match) { - $bookmark->removePids(array($match[1])); - $removed_object = islandora_object_load($match[1]); - - drupal_set_message(t('The @type @objectname has been removed from @listname.', array( - '@type' => variable_get('islandora_bookmark_type', 'bookmark'), - '@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->getId(), array('query' => $query)); - drupal_goto($destination['path'], $query); - } - elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); - drupal_goto($destination['path']); - } - else { - $query = array( - 'query' => array( - 'page' => $current_page - 1, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); - 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 @type.', array('@type' => variable_get('islandora_bookmark_type', '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->removePids($pids); - drupal_set_message(t('The selected @type have been removed from @listname.', - array( - '@listname' => $bookmark->bookmarkName, - '@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's', - ) - )); - $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->getId(), array('query' => $query)); - drupal_goto($destination['path'], $query); - } - elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); - drupal_goto($destination['path']); - } - else { - $query = array( - 'query' => array( - 'page' => $current_page - 1, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $bookmark->getId()); - drupal_goto($destination['path'], $query); - } - } - else { - drupal_set_message(t('Please select at least one @type.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), 'error'); - } - } - elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_all') { - $remove_count = $bookmark->getPidCount(); - $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 { - $pids = $bookmark->getPids(); - $all_pids = array_filter($pids, 'islandora_object_load'); - $broke_pids = array_diff($pids, $all_pids); - $broke_count = count($broke_pids); - - $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' => implode(', ', $broke_pids), - ) - ); - drupal_set_message($broken_pids, 'error'); - } - } - } + $bookmark->formSubmit($form, $form_state); } /** From 03037e1ea7a73948954f77429cfc8bc5b6ca64e8 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 16 Sep 2013 12:00:33 +0000 Subject: [PATCH 48/57] Break out actions. --- includes/bookmark.inc | 583 +++++++++++++++++++------------------- includes/solr_results.inc | 26 +- 2 files changed, 307 insertions(+), 302 deletions(-) diff --git a/includes/bookmark.inc b/includes/bookmark.inc index e2c3dbd..e5ddfa6 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -332,6 +332,48 @@ abstract class Bookmark implements BookmarkInterface { } } + protected function getTable($pids) { + return array( + '#type' => 'tableselect', + '#header' => $this->getTableHeader(), + '#options' => array_map(array($this, 'getTableRow'), drupal_map_assoc($pids)), + ); + } + + protected function getTableHeader() { + $header = array('markup' => array('data' => t('Label')));; + + if ($this->managementAccess()) { + $header['remove'] = array('data' => t('Operations')); + } + + return $header; + } + + protected function getTableRow($pid) { + $row = array( + 'markup' => islandora_bookmark_generate_markup($pid), + ); + + 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', + ); + } + + return $row; + } + /** * Form building method. */ @@ -391,51 +433,16 @@ abstract class Bookmark implements BookmarkInterface { // 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(); - $pager_element = 0; + $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); - // Loop the pids and determine how to display the markup. - foreach ($pids as $pid) { - $rows[$pid] = array( - 'markup' => islandora_bookmark_generate_markup($pid), - ); - } - $row_count = count($rows); - - $header = array('markup' => array('data' => t('Label'))); - - $form_state['islandora_bookmark_current_page'] = $page; - $form_state['islandora_bookmark_pids_on_page'] = $row_count; - if ($this->managementAccess()) { - $header['remove'] = array('data' => t('Operations')); - foreach ($rows as $pid => &$row) { - $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', - ); - } - // XXX: Silly looping with references... - unset($row); - } - + $table = $this->getTable($pids); $form['bookmarks']['fieldset'] += array( - 'table' => array( - '#header' => $header, - '#type' => 'tableselect', - '#options' => $rows, + 'table' => $table += array( '#empty' => t('@type list is empty.', array( '@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')), )), @@ -448,90 +455,10 @@ abstract class Bookmark implements BookmarkInterface { '#weight' => -1, ), ); - if ($row_count > 0) { - if ($this->managementAccess()) { - $form['bookmarks']['fieldset'] += array( - 'remove_select' => array( - '#type' => 'submit', - '#value' => t('Remove selected'), - '#name' => 'islandora_bookmark_remove_selected', - ), - 'remove_all' => array( - '#type' => 'submit', - '#value' => t('Remove all'), - '#name' => 'islandora_bookmark_remove_all', - ), - ); - } - - if (user_access('export islandora bookmarks')) { - $export_styles = array(); - $export = islandora_bookmark_get_export_options(); - $export_options['default'] = t('- Export type -'); + $row_count = count($form['bookmarks']['fieldset']['table']['#options']); - 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); - } - } - - $style_elements = array(); - foreach ($export_styles as $key => $value) { - if (!empty($value)) { - $style_elements[$key] = array( - '#type' => 'select', - '#title' => t('Style'), - '#options' => $value, - '#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'] += array( - 'export_title' => array( - '#markup' => '

' . t('Export') . '

', - '#weight' => 3, - ), - 'export_options' => array( - '#type' => 'select', - '#title' => t('Format'), - '#options' => $export_options, - '#weight' => 4, - '#name' => 'islandora_bookmark_export_options_select', - ), - 'format_opts' => array( - '#weight' => 4, - 'export_styles' => $style_elements, - 'export_selected_submit' => 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', - )), - '#name' => 'export_selected', - '#weight' => 5, - ), - 'export_all_submit' => 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', - )), - '#name' => 'export_all', - '#weight' => 6, - ), - ), - ); - } + if ($row_count > 0) { + $this->formAddActions($form['bookmarks']['fieldset'], $form_state); } // Share. @@ -698,24 +625,197 @@ abstract class Bookmark implements BookmarkInterface { } } } - else { - if ($user->uid != 0) { - $form['bookmarks']['remove_self'] = array( + elseif ($user->uid != 0 && $user->uid != $this->bookmarkOwner) { + $form['bookmarks']['remove_self'] = array( + '#type' => 'submit', + '#value' => t('Remove yourself from this list'), + '#submit' => array('islandora_bookmark_remove_self'), + ); + } + + return $form; + } + + public function formAddActions(&$container, &$form_state) { + $groups = array(); + + if ($this->managementAccess()) { + $groups['remove'] = array( + 'remove_selected' => array( '#type' => 'submit', - '#value' => t('Remove yourself from this list'), - '#submit' => array('islandora_bookmark_remove_self'), - ); + '#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); + } } + + $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), + ), + ), + ); + } + } + + $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', + ), + ), + ), + ); } - return $form; + if (!empty($groups)) { + $container += $groups; + } + } + + 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.')); + } + } + + /** + * One of our '#action_validate' callbacks. + * + * Checks that the export action has an export type selected. + */ + 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.')); + } + } + + protected function formExportSelected($form, $form_state) { + $pids = $this->formGetSelected($form_state); + $this->formExport($pids, $form_state); + } + + protected function formExportAll($form, $form_state) { + $pids = $this->formGetAll($form_state); + $this->formExport($pids, $form_state); + } + + 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, + ); + + 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); + } + + protected function formRemoveSelectedFromList($form_state) { + $pids = $this->formGetAll($form_state); + $this->formRemoveFromList($pids, $form_state); + } + + protected function formRemoveAllFromList($form_state) { + $pids = $this->formGetAll($form_state); + $this->formRemoveFromList($pids, $form_state); + } + + protected function formRemoveFromList($pids, $form_state) { + $this->removePids($pids); } /** * Form validation handler. */ public function formValidate(&$form, &$form_state) { - if (isset($form_state['triggering_element']['#button_id'])) { + if (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); @@ -732,177 +832,82 @@ abstract class Bookmark implements BookmarkInterface { * Form submission handler. */ public function formSubmit(&$form, &$form_state) { - foreach ($_POST as $key => $value) { - preg_match('/remove_pid_(.*)/', $key, $match); - if ($match) { - break; - } - } - if ($match) { - $this->removePids(array($match[1])); - $removed_object = islandora_object_load($match[1]); - - 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, - ))); - $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/' . $this->getId(), array('query' => $query)); - drupal_goto($destination['path'], $query); - } - elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); - drupal_goto($destination['path']); - } - else { - $query = array( - 'query' => array( - 'page' => $current_page - 1, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); - drupal_goto($destination['path'], $query); + if (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); } } - // 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++; - } + else { + foreach ($_POST as $key => $value) { + preg_match('/remove_pid_(.*)/', $key, $match); + if ($match) { + break; } } - $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 @type.', array('@type' => variable_get('islandora_bookmark_type', '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'); - } + if ($match) { + $this->removePids(array($match[1])); + $removed_object = islandora_object_load($match[1]); + + 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, + ))); } } - 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) { - $this->removePids($pids); - drupal_set_message(t('The selected @type have been removed from @listname.', - array( - '@listname' => $this->bookmarkName, - '@type' => variable_get('islandora_bookmark_type', 'bookmark') . 's', - ) - )); - $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/' . $this->getId(), array('query' => $query)); - drupal_goto($destination['path'], $query); - } - elseif ($current_page == 0) { - $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); - drupal_goto($destination['path']); - } - else { - $query = array( - 'query' => array( - 'page' => $current_page - 1, - ), - ); - $destination = drupal_parse_url('islandora-bookmark/listid/' . $this->getId()); - drupal_goto($destination['path'], $query); - } - } - else { - drupal_set_message(t('Please select at least one @type.', array('@type' => variable_get('islandora_bookmark_type', 'bookmark'))), 'error'); + + $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, + ); } } - elseif ($form_state['clicked_button']['#name'] === 'islandora_bookmark_remove_all') { - $remove_count = $this->getPidCount(); - $this->removeAllPids(); + } - drupal_set_message(t('All PIDs have been removed from the @remove_list list.', - array( - '@remove_list' => $this->bookmarkName, - ))); + /** + * 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); } - // Export all pids. - else { - $pids = $this->getPids(); - $all_pids = array_filter($pids, 'islandora_object_load'); - $broke_pids = array_diff($pids, $all_pids); - $broke_count = count($broke_pids); - - $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' => implode(', ', $broke_pids), - ) - ); - drupal_set_message($broken_pids, 'error'); - } - } + + 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/solr_results.inc b/includes/solr_results.inc index c274e88..8f3a070 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -52,7 +52,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @return array * An array containing form structure. */ - public function buildForm($form, &$form_state, $object_results) { + public function formGet($form, &$form_state, $object_results) { $form_state['islandora_solr_query_processor'] = $this->islandoraSolrQueryProcessor; module_load_include('inc', 'islandora_bookmark', 'includes/api'); @@ -81,7 +81,6 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { if (count($rows) > 0) { $form = array_merge($form, $this->getActions($form_state)); - } $form['islandora_bookmark_table'] = array( @@ -251,7 +250,6 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { $group['save_selected_submit'] = array( '#type' => 'submit', '#value' => t('Bookmark selected objects'), - '#name' => 'save_selected', '#weight' => 4, '#action_submit' => 'saveSelected', '#action_validate' => array( @@ -262,7 +260,6 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { $group['save_all_submit'] = array( '#type' => 'submit', '#value' => t('Bookmark all results'), - '#name' => 'save_all', '#weight' => 5, '#action_submit' => 'saveAll', '#action_validate' => array( @@ -364,12 +361,15 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { $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'])) { - 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); + $params[] = $form_state['input']['islandora_bookmark_export_styles_' . $key]; } + + call_user_func_array($form_state['islandora_bookmark_export'][$key], $params); } /** @@ -465,7 +465,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { /** * Go over any '#action_validate' functions on the clicked button. */ - public function validateForm($form, $form_state) { + 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) { @@ -484,7 +484,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The Drupal form state. */ - public function submitForm(&$form, &$form_state) { + public function formSubmit(&$form, &$form_state) { $queries = drupal_get_query_parameters(); $form_state['redirect'] = array(request_path(), array('query' => $queries)); @@ -568,14 +568,14 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * An array containing form structure. */ function islandora_bookmark_results_form($form, &$form_state, $results_class, $object_results) { - return $results_class->buildForm($form, $form_state, $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]->validateForm($form, $form_state); + $form_state['build_info']['args'][0]->formValidate($form, $form_state); } /** @@ -587,5 +587,5 @@ function islandora_bookmark_results_form_validate(array &$form, array &$form_sta * The Drupal form state. */ function islandora_bookmark_results_form_submit(array &$form, array &$form_state) { - $form_state['build_info']['args'][0]->submitForm($form, $form_state); + $form_state['build_info']['args'][0]->formSubmit($form, $form_state); } From 602718fadbbfb04c04e0b9040863328d75efc161 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 16 Sep 2013 13:47:12 +0000 Subject: [PATCH 49/57] A bit of cleanup and coding standards. --- includes/api.inc | 20 +++++--- includes/bookmark.inc | 98 +++++++++++++++++++++++++++++++-------- islandora_bookmark.module | 34 ++++++++++---- 3 files changed, 119 insertions(+), 33 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index 00f5062..011b6f0 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -112,10 +112,16 @@ function islandora_bookmark_get_bookmarks_visible_to_user($pid, $uid) { } 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; } @@ -135,7 +141,7 @@ function islandora_bookmark_list_pids_query($value) { } /** - * Retrieves all the uids from the bookmark listid. + * Retrieves all the uids from the bookmark listid, including owner. * * @param string $value * The listid. @@ -145,5 +151,7 @@ function islandora_bookmark_list_pids_query($value) { */ function islandora_bookmark_list_users_query($value) { $list = Bookmark::getList($value); - return $list->getUsers(); + $users = $list->getUsers(); + $users[] = $list->bookmarkOwner; + return $users; } diff --git a/includes/bookmark.inc b/includes/bookmark.inc index e5ddfa6..b2bab05 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -332,6 +332,15 @@ abstract class Bookmark implements BookmarkInterface { } } + /** + * Get the definition for the table. + * + * @param array $pids + * An array of PIDs, for which to build a tableselect. + * + * @return array + * An associative array defining a tableselect element. + */ protected function getTable($pids) { return array( '#type' => 'tableselect', @@ -340,6 +349,13 @@ abstract class Bookmark implements BookmarkInterface { ); } + /** + * Build the header for the table. + * + * @return array + * An array representing the header for a tableselect, as used by + * theme_tableselect(). + */ protected function getTableHeader() { $header = array('markup' => array('data' => t('Label')));; @@ -350,6 +366,15 @@ abstract class Bookmark implements BookmarkInterface { return $header; } + /** + * 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. + */ protected function getTableRow($pid) { $row = array( 'markup' => islandora_bookmark_generate_markup($pid), @@ -442,7 +467,7 @@ abstract class Bookmark implements BookmarkInterface { $table = $this->getTable($pids); $form['bookmarks']['fieldset'] += array( - 'table' => $table += array( + 'table' => $table += array( '#empty' => t('@type list is empty.', array( '@type' => ucwords(variable_get('islandora_bookmark_type', 'bookmark')), )), @@ -636,6 +661,12 @@ abstract class Bookmark implements BookmarkInterface { 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. + */ public function formAddActions(&$container, &$form_state) { $groups = array(); @@ -746,6 +777,9 @@ abstract class Bookmark implements BookmarkInterface { } } + /** + * Form validation callback; ensure at least one object is selected. + */ protected function formHasObjectsSelected($form, $form_state) { $pids = $this->formGetSelected($form_state); if (empty($pids)) { @@ -766,16 +800,25 @@ abstract class Bookmark implements BookmarkInterface { } } + /** + * Submission callback; export the selected PIDs. + */ protected function formExportSelected($form, $form_state) { $pids = $this->formGetSelected($form_state); $this->formExport($pids, $form_state); } + /** + * Submission callback; export all PIDs in the list. + */ 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']); @@ -791,16 +834,25 @@ abstract class Bookmark implements BookmarkInterface { call_user_func_array($form_state['islandora_bookmark_export'][$key], $params); } + /** + * Submission callback; remove the selected PIDs from the list. + */ 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); } @@ -809,7 +861,11 @@ abstract class Bookmark implements BookmarkInterface { * Form validation handler. */ public function formValidate(&$form, &$form_state) { - if (isset($form_state['clicked_button']['#action_validate'])) { + $pids_to_remove = $this->formGetPidToRemove($form_state); + if ($pids_to_remove) { + // No-op. + } + 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); @@ -828,28 +884,26 @@ abstract class Bookmark implements BookmarkInterface { } } + /** + * Get the PIDs of any single remove button(s). + * + * @return array + * An array of PIDs--may be empty if there are none present. + */ + 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) { - if (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); - } - } - else { - foreach ($_POST as $key => $value) { - preg_match('/remove_pid_(.*)/', $key, $match); - if ($match) { - break; - } - } - - if ($match) { - $this->removePids(array($match[1])); - $removed_object = islandora_object_load($match[1]); + $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, @@ -857,6 +911,12 @@ abstract class Bookmark implements BookmarkInterface { ))); } } + 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) { diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 36129e7..8481159 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -628,7 +628,7 @@ 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); @@ -692,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(). * @@ -961,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 { @@ -980,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); } /** From 6b8365232c140d91dc3f026ef7f6c74c4b110c90 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 26 Sep 2013 18:36:22 +0000 Subject: [PATCH 50/57] Fix passing of the list ID. --- islandora_bookmark.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/islandora_bookmark.module b/islandora_bookmark.module index 8481159..8c385a2 100644 --- a/islandora_bookmark.module +++ b/islandora_bookmark.module @@ -308,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']->getId()); + drupal_goto('islandora-bookmark/remove-self/' . $form_state['build_info']['args'][0]); } /** From f1a8d74ba8270d4ddc8928970a30de25070a0de9 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 26 Sep 2013 18:38:29 +0000 Subject: [PATCH 51/57] Stop "remove self" button from rendering when it's shared with uid 0. --- includes/bookmark.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/bookmark.inc b/includes/bookmark.inc index b2bab05..ab9b8cb 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -650,7 +650,7 @@ abstract class Bookmark implements BookmarkInterface { } } } - elseif ($user->uid != 0 && $user->uid != $this->bookmarkOwner) { + 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'), From ec0fea3bf03553884b3e33641635c85e7baee075 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 26 Sep 2013 18:45:22 +0000 Subject: [PATCH 52/57] Remove owner from our users table, just in case. --- includes/bookmark_db.inc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index 6ca0ae3..b78a48b 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -196,6 +196,10 @@ class BookmarkDatabase extends Bookmark { 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))); From 8072174640238d80e46d41601b352eebc30f0664 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 26 Sep 2013 18:48:32 +0000 Subject: [PATCH 53/57] Additional bit of safety. ... Effectively, don't let people try to load lists which don't exist. --- includes/api.inc | 2 +- includes/bookmark_db.inc | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/includes/api.inc b/includes/api.inc index 011b6f0..de81896 100644 --- a/includes/api.inc +++ b/includes/api.inc @@ -66,7 +66,7 @@ function islandora_bookmark_get_user_shared_bookmarks() { $templist = Bookmark::getList($id); $bookmark_list[] = $templist; } - return $bookmark_list; + return array_filter($bookmark_list); } /** diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index b78a48b..d49f865 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -99,8 +99,9 @@ class BookmarkDatabase extends Bookmark { * @param mixed $id * A bookmark list ID. * - * @return Bookmark - * A subclass of Bookmark, to which we can add and remove items. + * @return Bookmark|NULL + * A subclass of Bookmark, to which we can add and remove items, or NULL if + * there is an error. */ public static function getList($id) { $types = module_invoke_all('islandora_bookmark_database_types'); @@ -115,7 +116,7 @@ class BookmarkDatabase extends Bookmark { return new $class($id); } else { - return new static($id); + return NULL; } } From 24e1440a2fcc77e80c5876918a0177edec16388c Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 2 Oct 2013 18:09:15 +0000 Subject: [PATCH 54/57] Break out "secondary" actions. ... They can now be overriden by other llist types! Hurray! --- includes/bookmark.inc | 244 ++++++++++++++++++++++-------------------- 1 file changed, 126 insertions(+), 118 deletions(-) diff --git a/includes/bookmark.inc b/includes/bookmark.inc index ab9b8cb..3af053f 100644 --- a/includes/bookmark.inc +++ b/includes/bookmark.inc @@ -486,6 +486,132 @@ abstract class Bookmark implements BookmarkInterface { $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); + } + } + + $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), + ), + ), + ); + } + } + + $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; + } + } + + /** + * Add secondary actions to the form. + */ + protected function formAddSecondaryActions(&$form, &$form_state) { + global $user; // Share. if ($this->managementAccess()) { if (user_access('share islandora bookmarks')) { @@ -657,124 +783,6 @@ abstract class Bookmark implements BookmarkInterface { '#submit' => array('islandora_bookmark_remove_self'), ); } - - 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. - */ - public 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); - } - } - - $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), - ), - ), - ); - } - } - - $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; - } } /** From 43f7e82490ca70fda66328fd107fb1d73c4b3522 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 4 Oct 2013 18:02:32 +0000 Subject: [PATCH 55/57] Pass $form_state as a reference, so we can change it... --- includes/solr_results.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/solr_results.inc b/includes/solr_results.inc index 8f3a070..971613c 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -491,8 +491,9 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { $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($this, $submit_function), $form_state); + call_user_func_array(array($this, $submit_function), $params); } } } From 954dbf4ed959f21298b4493b9d8d4a2115a395ef Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 8 Oct 2013 14:37:05 +0000 Subject: [PATCH 56/57] Make method signatures of getList() the same. --- includes/bookmark_db.inc | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/includes/bookmark_db.inc b/includes/bookmark_db.inc index d49f865..c159d89 100644 --- a/includes/bookmark_db.inc +++ b/includes/bookmark_db.inc @@ -92,18 +92,17 @@ class BookmarkDatabase extends Bookmark { } /** - * Branch to different classes based on type. + * Refer to parent docs. * - * Falls back to the current class if a mapping for the type does not exist. - * - * @param mixed $id - * A bookmark list ID. - * - * @return Bookmark|NULL - * A subclass of Bookmark, to which we can add and remove items, or NULL if - * there is an error. + * Branch to different classes based on type. Return NULL if a mapping for + * the given type is not available. */ - public static function getList($id) { + 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')) From b833ddfe2c8bfc95c78449300dd278acab58445d Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 8 Oct 2013 14:51:52 +0000 Subject: [PATCH 57/57] Always pass form state as reference... just in case. --- includes/solr_results.inc | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/includes/solr_results.inc b/includes/solr_results.inc index 971613c..a8d30f6 100644 --- a/includes/solr_results.inc +++ b/includes/solr_results.inc @@ -290,7 +290,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * * Checks that the export action has an export type selected. */ - protected function hasExportTypeSelected($form, $form_state) { + 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') { @@ -303,7 +303,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * * Checks that the bookmark save/view action has a list selected. */ - protected function hasListSelected($form, $form_state) { + 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') { @@ -316,7 +316,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * * Checks that some objects are selected. */ - protected function hasObjectsSelected($form, $form_state) { + 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.')); @@ -331,7 +331,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The array of form state. */ - protected function exportAll($form_state) { + protected function exportAll(&$form_state) { $pids = $this->getAll($form_state); $this->export($pids, $form_state); } @@ -344,7 +344,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The array of form state. */ - protected function exportSelected($form_state) { + protected function exportSelected(&$form_state) { $pids = $this->getSelected($form_state); $this->export($pids, $form_state); } @@ -357,7 +357,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The array of form state. */ - protected function export($pids, $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']); @@ -380,7 +380,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The array of form state. */ - protected function goToBookmark($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); @@ -394,7 +394,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The array of form state. */ - protected function saveAll($form_state) { + protected function saveAll(&$form_state) { $pids = $this->getAll($form_state); $this->save($pids, $form_state); } @@ -407,7 +407,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The array of form state. */ - protected function saveSelected($form_state) { + protected function saveSelected(&$form_state) { $pids = $this->getSelected($form_state); $this->save($pids, $form_state); } @@ -420,7 +420,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { * @param array $form_state * The array of form state. */ - protected function save($pids, $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']); @@ -501,7 +501,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { /** * Submission helper; gets the PIDs all results. */ - protected function getAll($form_state) { + protected function getAll(&$form_state) { $pids = array(); // Helper anonymous function... Just get the PID. @@ -538,7 +538,7 @@ class IslandoraSolrResultsBookmark extends IslandoraSolrResults { /** * Submission helper; gets the PIDs of the selected results. */ - protected function getSelected($form_state) { + 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);