serial.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * @file
  4. * Internal functions for the Serial module.
  5. *
  6. * Note: This module uses php in SQL to support dynamic table names.
  7. * (required because each serial field needs a dedicated dynamic table).
  8. * However, all table names are safe (passed through db_escape_table).
  9. *
  10. * It seems that this is better than using table names as arguments, e.g.
  11. * $query = 'INSERT INTO %s (nid) VALUES(%d)';
  12. * db_query($query, db_prefix_tables('{'. $table .'}'), $nid);
  13. */
  14. /**
  15. * Creates an assistant serial table for a new created field.
  16. *
  17. * @param $field
  18. * a serial field
  19. * @param $instance
  20. * a new instance of that serial field
  21. */
  22. function _serial_create_table($field, $instance) {
  23. $table = _serial_get_field_table_name($field, $instance);
  24. $schema = _serial_get_table_schema();
  25. db_create_table($table, $schema);
  26. }
  27. /**
  28. * Drops an assistant serial table for a deleted field.
  29. *
  30. * @param $field
  31. * a serial field
  32. * @param $instance
  33. * a deleted instance of that serial field
  34. */
  35. function _serial_drop_table($field, $instance) {
  36. $table = _serial_get_field_table_name($field, $instance);
  37. db_drop_table($table);
  38. }
  39. /**
  40. * Renames serial table(s) when a content type us renamed.
  41. *
  42. * @param $old_type
  43. * an old node type machine name
  44. * @param $new_type
  45. * a new node type machine name
  46. */
  47. function _serial_rename_tables($old_type, $new_type) {
  48. // Build the query to find all affected tables.
  49. $query = db_select('field_config', 'f')->fields('f', array('field_name'));
  50. $table_joined_alias = $query->join(
  51. 'field_config_instance', 'i',
  52. '(f.field_name = i.field_name) AND ' .
  53. '(f.type = :field_type) AND (i.bundle = :bundle_type)',
  54. array(':field_type' => 'serial', ':bundle_type' => $new_type)
  55. );
  56. // Add an access check and execute it.
  57. $result = $query->addTag('node_access')->execute();
  58. // Rename each affected table.
  59. foreach ($result as $record) {
  60. $old_table = _serial_get_table_name($old_type, $record->field_name);
  61. $new_table = _serial_get_table_name($new_type, $record->field_name);
  62. db_rename_table($old_table, $new_table);
  63. }
  64. }
  65. /**
  66. * Gets the name of the assistant table for a specific field.
  67. *
  68. * @param $field
  69. * a serial field
  70. * @param $instance
  71. * an instance of that serial field
  72. * @return
  73. * the name of the assistant table of the specified field instance.
  74. */
  75. function _serial_get_field_table_name($field, $instance) {
  76. return _serial_get_table_name($instance['bundle'], $field['field_name']);
  77. }
  78. /**
  79. * Gets the name of the assistant table for a specific field.
  80. *
  81. * @param $bundle
  82. * the name of the entity type that contains the field
  83. * @param $field_name
  84. * the name of the field
  85. * @return
  86. * the name of the assistant table of the specified field.
  87. */
  88. function _serial_get_table_name($bundle, $field_name) {
  89. return db_escape_table( // be on the safe side
  90. 'serial_' . $bundle . '_' . $field_name);
  91. }
  92. /**
  93. * Gets the schema of the assistant tables for generating serial values.
  94. *
  95. * @return
  96. * the assistant table schema.
  97. */
  98. function _serial_get_table_schema() {
  99. return array(
  100. 'fields' => array(
  101. 'sid' => array(
  102. 'type' => 'serial',
  103. 'unsigned' => TRUE,
  104. 'not null' => TRUE,
  105. 'description' => 'The atomic serial field.',
  106. ),
  107. 'uniqid' => array(
  108. 'description' => 'Unique temporary allocation Id.',
  109. 'type' => 'varchar',
  110. 'length' => 23,
  111. 'not null' => TRUE,
  112. 'default' => ''),
  113. ),
  114. 'primary key' => array('sid'),
  115. 'unique keys' => array(
  116. 'uniqid' => array('uniqid'),
  117. ),
  118. );
  119. }
  120. /**
  121. * Generates a unique serial value (unique per node type).
  122. *
  123. * @param $nid
  124. * id of the node for which to generate a serial value
  125. * @param $bundle
  126. * a containing bundle (e.g. content type)
  127. * @param $field_name
  128. * the field name
  129. * @param $delete
  130. * indicates if temporary records should be deleted
  131. * @return
  132. * the unique serial value number.
  133. */
  134. function _serial_generate_value($bundle, $field_name, $delete = TRUE) {
  135. // Get the name of the relevant table.
  136. $table = _serial_get_table_name($bundle, $field_name);
  137. // Insert a temporary record to get a new unique serial value.
  138. $uniqid = uniqid('', TRUE);
  139. $sid = db_insert($table)
  140. ->fields(array(
  141. 'uniqid' => $uniqid,
  142. ))
  143. ->execute();
  144. // If there's a reason why it's come back undefined, reset it.
  145. $sid = isset($sid) ? $sid : 0;
  146. // Delete the temporary record.
  147. if ($delete && ($sid % 10) == 0) {
  148. db_delete($table)
  149. ->condition('uniqid', $uniqid, '=')
  150. ->execute();
  151. }
  152. // Return the new unique serial value.
  153. return $sid;
  154. }
  155. /**
  156. * Initializes the value of a new serial field in existing nodes.
  157. *
  158. * @param $bundle
  159. * a containing bundle (e.g. content type)
  160. * @param $field_name
  161. * the field name
  162. * @return
  163. * the number of existing nodes that have been initialized.
  164. */
  165. function _serial_init_old_nodes($bundle, $field_name) {
  166. // Retrieve all the node ids of that type:
  167. $query = "SELECT nid FROM {node} WHERE type = :type ORDER BY nid";
  168. // TODO: Currently works only for nodes - should support comments and users.
  169. $result = db_query($query, array('type' => $bundle));
  170. // Allocate a serial number for every old node:
  171. $count = 0;
  172. foreach ($result as $node) {
  173. $nid = $node->nid;
  174. $node = node_load($nid);
  175. $sid = _serial_generate_value($bundle, $field_name, FALSE);
  176. $node->{$field_name} = array('und' => array(array('value' => $sid)));
  177. node_save($node);
  178. $count++;
  179. }
  180. // Return the number of existing nodes that have been initialized:
  181. return $count;
  182. }
  183. /**
  184. * Retrieves all the managed serial fields.
  185. *
  186. * @return result set containing pairs of (node type name, field name).
  187. */
  188. function _serial_get_all_fields() {
  189. $query = db_select('field_config', 'f');
  190. $query->join('field_config_instance', 'i', 'i.field_name = f.field_name');
  191. $query->fields('i', array('bundle', 'field_name'))
  192. ->condition('f.type', 'serial', '=')
  193. ->condition('i.deleted', 0, '=');
  194. $result = $query->execute();
  195. return $result->fetchAll();
  196. }