? flag_weights_comments.patch ? junk.php Index: flag_weights.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/flag_weights/flag_weights.install,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 flag_weights.install --- flag_weights.install 24 Jan 2009 05:03:43 -0000 1.1.2.1 +++ flag_weights.install 27 Jul 2010 12:26:00 -0000 @@ -24,6 +24,24 @@ db_add_field($ret, 'flag_content', 'weight', $field); } +/** + * Add a new column to the {flags} table for storing the default weight. + */ +function flag_weights_update_6000() { + $ret = array(); + + $field = array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'size' => 'tiny', + 'initial' => 0, // Sets initial value for preexisting nodes. + 'description' => t('Default weight applied to new items.'), + ); + db_add_field($ret, 'flags', 'default_weight', $field); + + return $ret; +} /** * Implementation of hook_uninstall(). Index: flag_weights.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/flag_weights/flag_weights.module,v retrieving revision 1.1.2.6 diff -u -r1.1.2.6 flag_weights.module --- flag_weights.module 26 Aug 2009 10:39:09 -0000 1.1.2.6 +++ flag_weights.module 27 Jul 2010 12:26:00 -0000 @@ -6,6 +6,9 @@ * Add flag-weights to the Flag module. */ +define('MIN_DEFAULT_WEIGHT', -128); +define('MAX_DEFAULT_WEIGHT', 127); + /** * Implementation of hook_schema_alter(). We alter $schema by reference. * @@ -13,7 +16,7 @@ * The system-wide schema collected by drupal_get_schema(). */ function flag_weights_schema_alter(&$schema) { - // Add field to existing schema. + // Add fields to existing schema. $schema['flag_content']['fields']['weight'] = array( 'type' => 'int', 'not null' => TRUE, @@ -21,6 +24,14 @@ 'size' => 'tiny', // 'description' => t('Flag weight within region.'), ); + + $schema['flags']['fields']['default_weight'] = array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'size' => 'tiny', + 'description' => t('Default weight applied to new items.'), + ); } /** @@ -120,3 +131,88 @@ ), ); } + +/** + * Implementation of hook_form_FORMID_alter(). + * + * Add the default-weight setting to the Flags edit page. + */ +function flag_weights_form_flag_form_alter(&$form, &$form_state) { + $default_weight = _flag_weights_get_default_weight($form['#flag']->fid); + if ($default_weight == MIN_DEFAULT_WEIGHT) { + $default_weight = 'MIN'; + } + elseif ($default_weight == MAX_DEFAULT_WEIGHT) { + $default_weight = 'MAX'; + } + + $form['default_weight'] = array( + '#type' => 'textfield', + '#title' => t('Default weight'), + '#default_value' => $default_weight, + '#description' => t('Enter the default weight for this Flag (or MIN/MAX to set the weight lower/higher than existing content respectively.'), + '#required' => TRUE, + '#weight' => 15, + '#length' => 3, + // '#access' => empty($flag->locked['unflag_short']), + ); + + $form['#submit'][] = 'flag_weights_flag_form_submit'; + +} + +/** + * Custom submit handler for flag_form form. + */ +function flag_weights_flag_form_submit($form, &$form_state) { + // Parse the textfield to determine the value to apply to the DB + $default_weight = $form_state['values']['default_weight']; + if (strcasecmp($default_weight, 'MIN') == 0) { + $default_weight = MIN_DEFAULT_WEIGHT; + } + elseif (strcasecmp($default_weight, 'MAX') == 0) { + $default_weight = MAX_DEFAULT_WEIGHT; + } + + // By the time this executed, the Flag will already have been inserted/updated + // so $flag->fid is set + _flag_weights_set_default_weight($form['#flag']->fid, $default_weight); +} + +function _flag_weights_get_default_weight($fid) { + return (int) db_result(db_query("SELECT default_weight FROM {flags} WHERE fid = %d", $fid)); +} + +function _flag_weights_set_default_weight($fid, $default_weight) { + db_query("UPDATE {flags} SET default_weight = %d WHERE fid = %d", $default_weight, $fid); +} + +/** + * Implementation of hook_flag(). + * + * When content has been flagged, update the flag to use the default weight. + */ +function flag_weights_flag($action, $flag, $content_id, $account) { + if ($action == 'flag') { + $default_weight = _flag_weights_get_default_weight($flag->fid); + + // If the configured default weight is MIN/MAX then set it to the right int. + if ($default_weight == MIN_DEFAULT_WEIGHT) { + $found_min = db_result(db_query("SELECT min(weight) FROM {flag_content} WHERE fid = %d AND uid = %d", $flag->fid, $account->uid)); + if ($found_min !== FALSE && $found_min > MIN_DEFAULT_WEIGHT) { + $default_weight = $found_min - 1; + } + } + elseif ($default_weight == MAX_DEFAULT_WEIGHT) { + $found_max = db_result(db_query("SELECT max(weight) FROM {flag_content} WHERE fid = %d AND uid = %d", $flag->fid, $account->uid)); + if ($found_max !== FALSE && $found_max < MAX_DEFAULT_WEIGHT) { + $default_weight = $found_max + 1; + } + } + + // Don't bother applying a weight of 0 - this is the default. + if ($default_weight != 0) { + db_query("UPDATE {flag_content} SET weight = %d WHERE fid = %d AND content_id = %d", $default_weight, $flag->fid, $content_id); + } + } +}