From bdbd6343d3c9f8c815ff7cfc751715e85147ae98 Mon Sep 17 00:00:00 2001
From: baverhey
Date: Sat, 21 Feb 2015 23:30:23 +0100
Subject: [PATCH] everything manualey added
---
serial.inc | 3 ++
serial.info | 2 +-
serial.module | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 100 insertions(+), 2 deletions(-)
diff --git a/serial.inc b/serial.inc
index cb3dec2..b2c267f 100644
--- a/serial.inc
+++ b/serial.inc
@@ -165,6 +165,9 @@ function _serial_generate_value($bundle, $field_name, $delete = TRUE) {
->execute();
}
+ // Return the new unique serial value divided with the correction factor.
+ $configured_factor = variable_get('serial_increment', 1);
+ $sid = (int) ($sid / $configured_factor);
// Return the new unique serial value.
return $sid;
}
diff --git a/serial.info b/serial.info
index 27df301..dfa5f2a 100644
--- a/serial.info
+++ b/serial.info
@@ -4,4 +4,4 @@ package = Fields
core = 7.x
dependencies[] = field
files[] = serial.module
-
+configure = admin/settings/serial
diff --git a/serial.module b/serial.module
index 688bb10..ad1b22a 100644
--- a/serial.module
+++ b/serial.module
@@ -5,6 +5,102 @@
* The Serial module main file.
*/
+// ============== //
+// Viewable pages //
+// ============== //
+
+/**
+ * Implements hook_help().
+ */
+function serial_help($path, $arg) {
+ switch ($path) {
+ // Main module help for the serial module.
+ case 'admin/help#serial':
+ return '' .
+ t('The serial field relies on the MYSQL database auto
+ increment value, to provide unique serial fields
+ (asyncronus writing safe).
+ On shared hosting or shared databases the
+ auto_increment_increment is not editable, but
+ locally on the settings page it is.
+ The MySQL-related help
+ page.
+
+
Warning:
+
+ - Use only when you know what you are doing!
+ - First number will be 0 when using a correction
+ factor different than 1 on the settings page.
+ - The used formula is:
+ sid = sid_fromdb / correction_factor
+ eg: when auto increment is 7 you put 7 in the field
+
+
+ Special attention when changing the correction factor
+ on an existing project with existing serial field data:
+
+ If there is already data in a serial field and you
+ want to change the correction factor.
+ You need to do the following to insure the
+ uniqueness of serial fields.
+ No new content with serial field should be added
+ during this process (maintenance mode).
+ Multiply the next auto increment value in the db
+ with your configuration number (you have to do this
+ once for every serial field table with data in
+ the serial field).
+
',
+ array('@mysql_help' => url('https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html'))) . '
';
+ }
+}
+
+/**
+ * Implements hook_menu().
+ */
+function serial_menu() {
+ $items = array();
+ $items['admin/settings/serial'] = array(
+ 'title' => 'Administer serial field',
+ 'description' => t('Settings page for serial field'),
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('serial_settings_form'),
+ 'access arguments' => array('administer serial settings'),
+ );
+
+ return $items;
+}
+
+/**
+ * Form builder.
+ */
+function serial_settings_form($form, &$form_state) {
+ $form['serial_increment'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Increment neutralize factor to use'),
+ '#default_value' => variable_get('serial_increment', 1),
+ '#description' => t('Set the value you want to use
+ to neutralize the MySQL auto_increment.
+ The used formula is:
+ sid = sid_fromdb / correction_factor
+ Read the help
+ page before changing
+ eg: when auto increment is 7
+ you put 7 in the field'),
+ '#required' => TRUE,
+ );
+ return system_settings_form($form);
+}
+
+/**
+ * Form validate handler.
+ */
+function serial_settings_form_validate($form, &$form_state) {
+ $increment = $form_state['values']['serial_increment'];
+ if (!is_numeric($increment) || $increment == 0) {
+ form_set_error('serial_increment', t('You must enter a non-zero integer.'));
+ }
+}
+
//==================//
// Field Definition //
//==================//
@@ -231,4 +327,3 @@ function serial_field_widget(&$form, &$form_state, $field, $instance, $items, $d
)
);
}
-
--
1.9.1