Commit 31c61ab7 authored by lyricnz's avatar lyricnz
Browse files

Issue 1011664: Fix issue with Global flags, and size of flag-weights in database.

parent a1cf66be
Loading
Loading
Loading
Loading
+37 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ function flag_weights_install() {
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'unsigned' => FALSE,
    'initial' => 0, // Sets initial value for preexisting nodes.
    // 'description' => t('Flag weight within region.'),
  );
@@ -28,7 +28,7 @@ function flag_weights_install() {
     'type' => 'int',
     'not null' => TRUE,
     'default' => 0,
     'size' => 'tiny',
     'unsigned' => FALSE,
     'initial' => 0, // Sets initial value for preexisting nodes.
     'description' => 'Default weight applied to new items.',
   );
@@ -56,6 +56,41 @@ function flag_weights_update_6000() {
  return $ret;
}

/**
 * Update the size of flag weights and defaults.
 */
function flag_weights_update_6001() {
  $ret = array();

  // Change the size of the weight field in flag_content table (Flag module)
  $field = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'unsigned' => FALSE,
    'initial' => 0, // Sets initial value for preexisting nodes.
    // 'description' => t('Flag weight within region.'),
  );

  db_change_field($ret, 'flag_content', 'weight', 'weight', $field);


  // Change the size of the weight field in flags table (Flag module)
  $field = array(
     'type' => 'int',
     'not null' => TRUE,
     'default' => 0,
     'unsigned' => FALSE,
     'initial' => 0, // Sets initial value for preexisting nodes.
     'description' => t('Default weight applied to new items.'),
   );

  db_change_field($ret, 'flags', 'default_weight', 'default_weight', $field);

  return $ret;
}


/**
 * Implementation of hook_uninstall().
 */
+18 −7
Original line number Diff line number Diff line
@@ -5,8 +5,8 @@
 * Add flag-weights to the Flag module.
 */

define('MIN_DEFAULT_WEIGHT', -128);
define('MAX_DEFAULT_WEIGHT', 127);
define('MIN_DEFAULT_WEIGHT', -2147483648);
define('MAX_DEFAULT_WEIGHT', 2147483647);

/**
 * Implements hook_schema_alter(). We alter $schema by reference().
@@ -20,7 +20,7 @@ function flag_weights_schema_alter(&$schema) {
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'unsigned' => FALSE,
    // 'description' => t('Flag weight within region.'),
  );

@@ -28,7 +28,7 @@ function flag_weights_schema_alter(&$schema) {
     'type' => 'int',
     'not null' => TRUE,
     'default' => 0,
     'size' => 'tiny',
     'unsigned' => FALSE,
     'description' => t('Default weight applied to new items.'),
   );
}
@@ -187,7 +187,7 @@ function flag_weights_flag_form_submit($form, &$form_state) {
}

function _flag_weights_get_default_weight($fid) {
  return (int) db_query("SELECT default_weight FROM {flags} WHERE fid = :fid", array(':fid' => $fid))->fetchField();
  return db_query("SELECT default_weight FROM {flags} WHERE fid = :fid", array(':fid' => $fid))->fetchField();
}

function _flag_weights_set_default_weight($fid, $default_weight) {
@@ -210,13 +210,24 @@ function flag_weights_flag($action, $flag, $content_id, $account) {

    // If the configured default weight is MIN/MAX then set it to the right int.
    if ($default_weight == MIN_DEFAULT_WEIGHT) {
      if ($flag->global) {
        $found_min = db_query("SELECT min(weight) FROM {flag_content} WHERE fid = :fid", array(':fid' => $flag->fid))->fetchField();
      }
      else {
        $found_min = db_query("SELECT min(weight) FROM {flag_content} WHERE fid = :fid AND uid = :uid", array(':fid' => $flag->fid, ':uid' => $account->uid))->fetchField();
      }

      if ($found_min !== FALSE && $found_min > MIN_DEFAULT_WEIGHT) {
        $default_weight = $found_min - 1;
      }
    }
    elseif ($default_weight == MAX_DEFAULT_WEIGHT) {
      if ($flag->global) {
        $found_max = db_query("SELECT max(weight) FROM {flag_content} WHERE fid = :fid", array(':fid' => $flag->fid))->fetchField();
      }
      else {
        $found_max = db_query("SELECT max(weight) FROM {flag_content} WHERE fid = :fid AND uid = :uid", array(':fid' => $flag->fid, ':uid' => $account->uid))->fetchField();
      }
      if ($found_max !== FALSE && $found_max < MAX_DEFAULT_WEIGHT) {
        $default_weight = $found_max + 1;
      }