diff --git flag.inc flag.inc index e1fa454..43b04a0 100644 --- flag.inc +++ flag.inc @@ -198,6 +198,7 @@ class flag_flag { 'flag' => array(DRUPAL_AUTHENTICATED_RID), 'unflag' => array(DRUPAL_AUTHENTICATED_RID), ), + 'api_version' => '', ); // Merge in options from the current link type. @@ -615,7 +616,7 @@ class flag_flag { * * @private */ - function _is_flagged($content_id, $uid, $sid) { + function _is_flagged($content_id, $uid, $sid) { return db_result(db_query("SELECT fid FROM {flag_content} WHERE fid = %d AND uid = %d AND sid = %d AND content_id = %d", $this->fid, $uid, $sid, $content_id)); } diff --git flag.install flag.install index c7b7194..624d7dd 100644 --- flag.install +++ flag.install @@ -70,6 +70,7 @@ function flag_enable() { 'unflag_long' => 'Remove this post from your bookmarks', 'unflag_message' => 'This post has been removed from your bookmarks', 'types' => array('story', 'forum', 'blog'), + 'api_version' => FLAG_API_VERSION, ); $flag->form_input($configuration); $flag->save(); @@ -549,6 +550,32 @@ function flag_update_6202() { return $ret; } +/** + * Upgrade flags that are not API version 2 compatibale. + */ +function flag_update_6203() { + $ret = array(); + foreach (flag_get_flags() as $flag) { + $save = FALSE; + if (empty($flag->api_version) || $flag->api_version < 2 ) { + $flag->api_version = 2; + $save = TRUE; + } + if (!empty($flag->roles) && empty($flag->roles['flag'])) { + $flag->roles = array( + 'flag' => $flag->roles, + 'unflag' => $flag->roles, + ); + $save = TRUE; + } + // Flag was updated and needs to be saved. + if ($save) { + $flag->save(); + } + } + return $ret; +} + // This is a replacement for update_sql(). The latter doesn't support placeholders. function _flag_update_sql($sql) { $args = func_get_args(); diff --git flag.module flag.module index edebd07..5fea372 100644 --- flag.module +++ flag.module @@ -6,6 +6,8 @@ * The Flag module. */ +define('FLAG_API_VERSION', 2); + include_once dirname(__FILE__) .'/flag.inc'; /** @@ -77,6 +79,14 @@ function flag_menu() { 'file' => 'includes/flag.export.inc', 'weight' => 3, ); + $items['admin/build/flags/update/%'] = array( + 'title' => 'Update', + 'page callback' => 'flag_update_page', + 'page arguments' => array(4), + 'access arguments' => array('administer flags'), + 'type' => MENU_CALLBACK, + 'file' => 'includes/flag.export.inc', + ); $items['flag'] = array( 'title' => 'Flag', 'page callback' => 'flag_page', @@ -1291,11 +1301,19 @@ function flag_get_default_flags($include_disabled = FALSE) { foreach ($function() as $config) { $flag = flag_flag::factory_by_array($config); $flag->module = $module; + + // Disable flags that are not at the current API version. + if (!isset($flag->api_version) || $flag->api_version < FLAG_API_VERSION) { + $flag->status = FALSE; + $flag->api_version = isset($flag->api_version) ? $flag->api_version : 1; + } + // Add flags that have been enabled. if ((!isset($flag_status[$flag->name]) && (!isset($flag->status) || $flag->status)) || !empty($flag_status[$flag->name])) { $flag->status = TRUE; $default_flags[$flag->name] = $flag; } + // Add flags that have been disabled. elseif ($include_disabled) { $flag->status = FALSE; $default_flags[$flag->name] = $flag; diff --git includes/flag.admin.inc includes/flag.admin.inc index 7485a72..b814b96 100644 --- includes/flag.admin.inc +++ includes/flag.admin.inc @@ -52,11 +52,19 @@ function theme_flag_admin_page($flags, $default_flags) { $rows = array(); foreach ($default_flags as $name => $flag) { if (!isset($flags[$name])) { - $ops = theme('links', array( - 'flags_enable' => array('title' => t('enable'), 'href' => "admin/build/flags/edit/" . $flag->name), - )); - - $roles = array_flip(array_intersect(array_flip(user_roles()), $flag->roles['flag'])); + if ($flag->api_version < FLAG_API_VERSION) { + $flag_updates_needed = TRUE; + $ops = theme('links', array( + 'flags_update' => array('title' => '' . t('needs update') . '', 'href' => 'admin/build/flags/update/' . $flag->name, 'html' => TRUE), + )); + } + else { + $ops = theme('links', array( + 'flags_enable' => array('title' => t('enable'), 'href' => 'admin/build/flags/edit/' . $flag->name), + )); + } + // $flag->roles['flag'] not exist on older flags. + $roles = array_flip(array_intersect(array_flip(user_roles()), !empty($flag->roles['flag']) ? $flag->roles['flag'] : array())); $rows[] = array( $flag->name, $flag->module, @@ -66,6 +74,10 @@ function theme_flag_admin_page($flags, $default_flags) { } } + if (isset($flag_updates_needed)) { + drupal_set_message(t('Some flags provided by modules need to be updated to a new format before they can be used with this version of Flag. See the disabled flags for a list of flags that need updating.'), 'warning'); + } + if (!empty($rows)) { $header = array(t('Disabled Flags'), t('Module'), t('Flag type'), t('Operations')); $output .= theme('table', $header, $rows); diff --git includes/flag.export.inc includes/flag.export.inc index fe2f7cf..33a752e 100644 --- includes/flag.export.inc +++ includes/flag.export.inc @@ -24,6 +24,8 @@ function flag_export_flags($flags = array(), $module = '') { // implementation. $flag = flag_get_flag($flag); } + // Make sure flag is updated to current version API. + flag_update_export($flag); $new_flag = (array) $flag; if (!empty($module)) { @@ -136,11 +138,11 @@ function flag_import_form_submit($form, &$form_state) { /** * Export a flag and display it in a form. */ -function flag_export_form(&$form_state, $flag_name = NULL) { +function flag_export_form(&$form_state, $flag = NULL) { $form = array(); // Convert a flag name (if any) to the list of export flags. - if ($flag = flag_get_flag($flag_name)) { + if (is_object($flag) || ($flag = flag_get_flag($flag))) { $flags = array($flag); } @@ -193,3 +195,46 @@ function flag_export_form(&$form_state, $flag_name = NULL) { function flag_export_form_submit($form, &$form_state) { $form_state['rebuild'] = TRUE; } + +/** + * Page for displaying an upgrade message and export form for Flag 1.x flags. + */ +function flag_update_page($flag_name) { + $flags = flag_get_default_flags(TRUE); + if (!isset($flags[$flag_name])) { + drupal_not_found(); + return; + } + + if ($flags[$flag_name]->api_version == FLAG_API_VERSION) { + drupal_set_message(t('The flag %name is already up-to-date with the latest Flag API and does not need upgrading.')); + drupal_goto('admin/build/flags'); + } + + $flag = $flags[$flag_name]; + drupal_set_message(t('The flag %name is currently using the Flag API version @version, which is not compatible with the current version of Flag. You can upgrade this flag by pasting the below code into @module_flag_default_flags() function in the @module.module file.', array('%name' => $flag->name, '@version' => $flag->api_version, '@module' => $flag->module)), 'warning'); + + flag_update_export($flag); + + return drupal_get_form('flag_export_form', $flag); +} + + +/** + * Update a flag before export. + * + * @param $flag + * The flag object passed by reference. + */ +function flag_update_export(&$flag) { + // Update differences. + if (empty($flag->api_version) || $flag->api_version == 1) { + if (isset($flag->roles) && !isset($flag->roles['flag'])) { + $flag->roles = array( + 'flag' => $flag->roles, + 'unflag' => $flag->roles, + ); + } + $flag->api_version = FLAG_API_VERSION; + } +} \ No newline at end of file