From b1e71251b757630720d43bef8f2038c4179dfe07 Thu, 31 Oct 2013 04:10:40 +0100
From: hass <hass@85918.no-reply.drupal.org>
Date: Thu, 31 Oct 2013 04:10:26 +0100
Subject: [PATCH] Full module review

diff --git a/field_conditional_state.admin.inc b/field_conditional_state.admin.inc
index 11313a2..5294d1a 100644
--- a/field_conditional_state.admin.inc
+++ b/field_conditional_state.admin.inc
@@ -11,32 +11,34 @@
  *   the field type
  */
 function field_conditional_state_get_field_states($type) {
-  $ret = array();
+  $states_available = array();
+
   switch ($type) {
     case 'list_boolean':
-      $ret = array(
-        'enabled',
-        'disabled',
-        'required',
-        'optional',
-        'visible',
-        'invisible',
-        'checked',
-        'unchecked',
+      $states_available = array(
+        'enabled' => t('Enabled'),
+        'disabled' => t('Disabled'),
+        'required' => t('Required'),
+        'optional' => t('Optional'),
+        'visible' => t('Visible'),
+        'invisible' => t('Invisible'),
+        'checked' => t('Checked'),
+        'unchecked' => t('Unchecked'),
       );
       break;
 
     default:
-      $ret = array('enabled',
-        'disabled',
-        'required',
-        'optional',
-        'visible',
-        'invisible',
+      $states_available = array(
+        'enabled' => t('Enabled'),
+        'disabled' => t('Disabled'),
+        'required' => t('Required'),
+        'optional' => t('Optional'),
+        'visible' => t('Visible'),
+        'invisible' => t('Invisible'),
       );
   }
 
-  return $ret;
+  return $states_available;
 }
 
 /**
@@ -46,28 +48,29 @@
  *   the field type
  */
 function field_conditional_state_get_trigger_states($type) {
-  $ret = array();
+  $states_triggerable = array();
+
   switch ($type) {
     case 'list_boolean':
-      $ret = array('checked', 'unchecked');
+      $states_triggerable = array('checked', 'unchecked');
       break;
 
     case 'list_text':
     case 'list_float':
     case 'list_integer':
-      $ret = array('value');
+      $states_triggerable = array('value');
       break;
 
     case 'file':
     case 'image':
-      $ret = array('empty', 'filled');
+      $states_triggerable = array('empty', 'filled');
       break;
 
     default:
-      $ret = array('empty', 'filled', 'value');
+      $states_triggerable = array('empty', 'filled', 'value');
   }
 
-  return $ret;
+  return $states_triggerable;
 }
 
 /**
@@ -119,17 +122,17 @@
   );
 
   $subforms = array();
-  foreach ($fcs_states['possible_states'] as $state) {
+  foreach (array_keys($fcs_states['possible_states']) as $state) {
     $form['content']['tab-' . $state] = array(
       '#type' => 'fieldset',
-      '#title' => check_plain($state),
+      '#title' => $fcs_states['possible_states'][$state],
       'content' => array(),
     );
 
     $subforms[$state] = &$form['content']['tab-' . $state]['content'];
 
     $subforms[$state]['info'] = array(
-      '#markup' => "<p>" . t('The state is activated if and only if all listed groups are true.') . "</p>",
+      '#markup' => '<p>' . t('The state is activated if and only if all listed groups are true.') . '</p>',
     );
 
     $subforms[$state]['new_group'] = array(
@@ -153,8 +156,7 @@
     $new_row = &$subforms[$group['state']][$group['group_id']];
     $new_row = array(
       'states' => array(
-        '#prefix' => '<table id="' . $table_id . '">
-          <tr><th>' . t('Control field') . '</th><th>' . t('Trigger state') . '</th><th>' . t('Trigger values') . '</th><th></th></tr>',
+        '#prefix' => '<table ' . drupal_attributes(array('id' => $table_id)) . '><tr><th>' . t('Control field') . '</th><th>' . t('Trigger state') . '</th><th>' . t('Trigger value') . '</th><th>' . t('Operation') . '</th></tr>',
         '#suffix' => '</table>',
       ),
     );
@@ -286,41 +288,41 @@
       $fields['trigger_value'] = $values['trigger_value-' . $id];
     }
 
-    $query = db_update('field_conditional_states');
-    $query->fields($fields);
-    $query->condition('id', $id);
-    $query->execute();
+    db_update('field_conditional_state')
+      ->fields($fields)
+      ->condition('id', $id)
+      ->execute();
   }
 
   foreach ($fcs_states['existing_groups'] as $id) {
     if (!empty($values['control_field-new-' . $id]) && !empty($values['trigger_state-new-' . $id])) {
-      $query = db_insert('field_conditional_states');
-      $trigger_value = ($values['trigger_state-new-' . $id] == 'value' ? $values['trigger_value-new-' . $id] : 1);
-      $query->fields(array(
-        'group_id' => $id,
-        'control_field' => $values['control_field-new-' . $id],
-        'trigger_state' => $values['trigger_state-new-' . $id],
-        'trigger_value' => $trigger_value,
-      ));
-      $query->execute();
+      $trigger_value = ($values['trigger_state-new-' . $id] == 'value') ? $values['trigger_value-new-' . $id] : 1;
+      db_insert('field_conditional_state')
+        ->fields(array(
+          'group_id' => $id,
+          'control_field' => $values['control_field-new-' . $id],
+          'trigger_state' => $values['trigger_state-new-' . $id],
+          'trigger_value' => $trigger_value,
+        ))
+        ->execute();
     }
-    $q = db_update('field_conditional_states_groups');
-    $q->fields(array('type' => $values['group_type-' . $id]));
-    $q->condition('group_id', $id);
-    $q->execute();
+    db_update('field_conditional_states_group')
+      ->fields(array('type' => $values['group_type-' . $id]))
+      ->condition('group_id', $id)
+      ->execute();
   }
 
-  foreach ($fcs_states['possible_states'] as $state) {
+  foreach (array_keys($fcs_states['possible_states']) as $state) {
     if (!empty($values['new-group-' . $state])) {
-      $q = db_insert('field_conditional_states_groups');
-      $q->fields(array(
-        'state' => $state,
-        'entity_type' => $instance['entity_type'],
-        'bundle' => $instance['bundle'],
-        'field_name' => $instance['field_name'],
-        'type' => $values['new-group-' . $state],
-      ));
-      $q->execute();
+      db_insert('field_conditional_states_group')
+        ->fields(array(
+          'state' => $fcs_states['possible_states'][$state],
+          'entity_type' => $instance['entity_type'],
+          'bundle' => $instance['bundle'],
+          'field_name' => $instance['field_name'],
+          'type' => $values['new-group-' . $state],
+        ))
+        ->execute();
     }
   }
 }
@@ -329,10 +331,11 @@
  * Returns a basic / empty row of a single condition.
  */
 function field_conditional_state_get_state_default_row($id, $classes = array()) {
-  $class_string = implode(" ", $classes);
+  //$class_string = implode(" ", $classes);
   $state_id = drupal_html_id('field_conditional_state-state-' . $id);
   $new_state = array(
-    '#prefix' => '<tr class="' . $class_string . '" id="' . $state_id . '">',
+    //'#prefix' => '<tr ' . drupal_attributes(array('id' => $state_id, 'class' => $class_string)) . '>',
+    '#prefix' => '<tr ' . drupal_attributes(array('id' => $state_id, 'class' => $classes)) . '>',
     '#suffix' => '</tr>',
   );
 
@@ -349,7 +352,7 @@
   );
 
   $new_state['trigger_state-' . $id] = array(
-    '#prefix' => '<td id="trigger_state-' . $id . '">',
+    '#prefix' => '<td ' . drupal_attributes(array('id' => drupal_html_id('trigger_state-' . $id))) . '>',
     '#suffix' => '</td>',
     '#type' => 'select',
     '#options' => array(),
@@ -361,7 +364,7 @@
     '#type' => 'textfield',
     '#states' => array(
       'visible' => array(
-        ':input[name="trigger_state-' . $id . '"]' => array('value' => 'value'),
+        ':input[name="trigger_state-' . check_plain($id) . '"]' => array('value' => 'value'),
       ),
     ),
   );
@@ -396,7 +399,7 @@
  * Ajax callback for removing elements.
  */
 function field_conditional_state_ajax_remove($form, $form_state) {
-  return "";
+  return '';
 }
 
 /**
@@ -406,9 +409,9 @@
   $elem = &$form_state['triggering_element'];
   $state_id = $elem['#field_conditional_state_state_id'];
   if (!empty($state_id)) {
-    $query = db_delete('field_conditional_states');
-    $query->condition('id', $state_id);
-    $query->execute();
+    db_delete('field_conditional_state')
+      ->condition('id', $state_id)
+      ->execute();
   }
 }
 
@@ -420,12 +423,12 @@
   $elem = &$form_state['triggering_element'];
   $group_id = $elem['#field_conditional_state_group_id'];
   if (!empty($group_id)) {
-    $query = db_delete('field_conditional_states');
-    $query->condition('group_id', $group_id);
-    $query->execute();
+    db_delete('field_conditional_state')
+      ->condition('group_id', $group_id)
+      ->execute();
 
-    $query = db_delete('field_conditional_states_groups');
-    $query->condition('group_id', $group_id);
-    $query->execute();
+    db_delete('field_conditional_states_group')
+      ->condition('group_id', $group_id)
+      ->execute();
   }
 }
diff --git a/field_conditional_state.install b/field_conditional_state.install
index 071c5b6..25146e9 100644
--- a/field_conditional_state.install
+++ b/field_conditional_state.install
@@ -10,17 +10,17 @@
  */
 function field_conditional_state_install() {
   // Set the module weight to 100.
-  $q = db_update('system');
-  $q->fields(array('weight' => 100));
-  $q->condition('name', 'field_conditional_state');
-  $q->execute();
+  db_update('system')
+    ->fields(array('weight' => 100))
+    ->condition('name', 'field_conditional_state')
+    ->execute();
 }
 
 /**
  * Implements hook_schema().
  */
 function field_conditional_state_schema() {
-  $schema['field_conditional_states'] = array(
+  $schema['field_conditional_state'] = array(
     'fields' => array(
       'id' => array(
         'type' => 'serial',
@@ -52,14 +52,14 @@
     ),
     'primary key' => array('id'),
     'foreign keys' => array(
-      'field_conditional_states_groups' => array(
-        'table' => 'field_conditional_states_groups',
+      'field_conditional_states_group' => array(
+        'table' => 'field_conditional_states_group',
         'columns' => array('group_id' => 'group_id'),
       ),
     ),
   );
 
-  $schema['field_conditional_states_groups'] = array(
+  $schema['field_conditional_states_group'] = array(
     'fields' => array(
       'group_id' => array(
         'type' => 'serial',
@@ -102,3 +102,138 @@
 
   return $schema;
 }
+
+/**
+ * Change the field_conditional_state.trigger_values field from varchar to text
+ */
+function field_conditional_state_update_7100() {
+  $spec = array(
+    'type' => 'text',
+    'not null' => TRUE,
+    'description' => 'The trigger values.',
+  );
+
+  db_change_field('field_conditional_state', 'trigger_values', 'trigger_values', $spec);
+
+  return t('Changed "trigger_values" field from type "varchar" to "text".');
+}
+
+/**
+ * Upgrade module from 7.x-1.x to 7.x-2.x.
+ */
+function field_conditional_state_update_7200() {
+
+  // Add new group_id field.
+  $spec = array(
+    'type' => 'int',
+    'not null' => TRUE,
+    'description' => 'The field conditional states group id.',
+  );
+  db_add_field('field_conditional_state', 'group_id', $spec);
+
+  $spec = array(
+    'type' => 'text',
+    'not null' => TRUE,
+    'description' => 'The trigger value.',
+  );
+  db_add_field('field_conditional_state', 'trigger_value', $spec);
+
+  // Change control_field field properties.
+  $spec = array(
+    'type' => 'varchar',
+    'length' => '255',
+    'not null' => TRUE,
+    'description' => 'The control field name.',
+  );
+  db_change_field('field_conditional_state', 'control_field', 'control_field', $spec);
+
+  // Add new states group table.
+  $schema['field_conditional_states_group'] = array(
+    'fields' => array(
+      'group_id' => array(
+        'type' => 'serial',
+        'not null' => TRUE,
+        'description' => 'The internal identifier.',
+      ),
+      'state' => array(
+        'type' => 'varchar',
+        'length' => '15',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'entity_type' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'bundle' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'field_name' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'type' => array(
+        'type' => 'varchar',
+        'length' => '3',
+        'not null' => TRUE,
+        'description' => 'The logical connection.',
+      ),
+    ),
+    'primary key' => array('group_id'),
+  );
+
+  db_create_table('field_conditional_states_group', $schema['field_conditional_states_group']);
+
+  // Upgrade settings data from 7.x-1.x to 7.x-2.x.
+  $fields = db_query("SELECT field_name FROM {field_conditional_state} GROUP BY field_name");
+  foreach ($fields as $field) {
+    $group_id = db_insert('field_conditional_states_group')
+      ->fields(array(
+        'state' => $field->state
+        'entity_type' => 'node', // Was something else supported in past?
+        'bundle' => $field->bundle,
+        'field_name' => $field->field_name,
+        'type' => 'or',
+      ))
+      ->execute();
+
+    db_update('field_conditional_state')
+      ->fields(array('group_id' => $group_id))
+      ->condition('field_name', $field->field_name)
+      ->execute();
+  }
+
+  $fields = db_query("SELECT * FROM {field_conditional_state}");
+  foreach ($fields as $field) {
+    foreach(array_values($field->trigger_values) as $trigger_value) {
+      db_insert('field_conditional_state')
+        ->fields(array(
+          'group_id' => $field->group_id,
+          'control_field' => $field->control_field,
+          'trigger_state' => 'value', // How can we find out?
+          'trigger_value' => $trigger_value,
+        ))
+        ->execute();
+    }
+
+    db_delete('field_conditional_state')
+      ->condition('id', $field->id)
+      ->execute()
+  }
+
+  // Remove no longer needed fields.
+  db_drop_field('field_conditional_state', 'field_name');
+  db_drop_field('field_conditional_state', 'state');
+  db_drop_field('field_conditional_state', 'bundle');
+  db_drop_field('field_conditional_state', 'trigger_values');
+  db_drop_field('field_conditional_state', 'condition_type');
+
+  return t('Upgraded module settings from 7.x-1.x to 7.x-2.x.');
+}
diff --git a/field_conditional_state.module b/field_conditional_state.module
index e96f663..51e435d 100644
--- a/field_conditional_state.module
+++ b/field_conditional_state.module
@@ -28,12 +28,12 @@
     return $records[$entity_type][$bundle][$field_name];
   }
   else {
-    $query = db_select('field_conditional_states_groups', 'g');
-    $query->fields('g');
-    $query->condition('entity_type', $entity_type);
-    $query->condition('bundle', $bundle);
-    $query->condition('field_name', $field_name);
-    $groups = $query->execute();
+    $groups = db_select('field_conditional_states_group', 'g')
+      ->fields('g')
+      ->condition('entity_type', $entity_type)
+      ->condition('bundle', $bundle)
+      ->condition('field_name', $field_name)
+      ->execute();
 
     $data = array();
     while ($group = $groups->fetchAssoc()) {
@@ -41,10 +41,12 @@
       $tmp = $group;
       $tmp['states'] = array();
 
-      $query = db_select('field_conditional_states', 's');
-      $query->fields('s');
-      $query->condition('group_id', $group['group_id']);
-      $states = $query->execute();
+      $states = db_select('field_conditional_state', 's')
+        ->fields('s')
+        ->condition('group_id', $group['group_id'])
+        ->orderBy('control_field', 'ASC')
+        ->execute();
+
       while ($state = $states->fetchAssoc()) {
         $tmp['states'][] = $state;
       }
@@ -119,8 +121,8 @@
       }
     }
 
-    $element['#prefix'] = '<div id="' . $element['#field_name'] . '_field_conditional_state_control_' . $element['#field_conditional_state_id'] . '">';
-    $element['#suffix'] = "</div>";
+    $element['#prefix'] = '<div ' . drupal_attributes(array('id' => drupal_html_id($element['#field_name'] . '_field_conditional_state_control_' . $element['#field_conditional_state_id']))) . '>';
+    $element['#suffix'] = '</div>';
     $form_state['#field_conditional_state_parents'][$element['#field_name'] . $element['#field_conditional_state_id']] = $element['#parents'];
 
   }
@@ -228,7 +230,7 @@
   foreach (element_children($table) as $key) {
     if (array_key_exists($key, $instances)) {
       $admin_field_path = $admin_path . '/fields/' . $instances[$key]['field_name'];
-      $table[$key]['field_conditional_states'] = array(
+      $table[$key]['field_conditional_state'] = array(
         '#type' => 'link',
         '#title' => t('States'),
         '#href' => $admin_field_path . '/field_conditional_state',
@@ -236,7 +238,7 @@
       );
     }
     else {
-      $table[$key]['field_conditional_states'] = array('#markup' => '');
+      $table[$key]['field_conditional_state'] = array('#markup' => '');
     }
   }
 }
@@ -262,7 +264,7 @@
  * and where the ID would otherwise not be rendered
  */
 function field_conditional_state_add_id_container($elements) {
-  $elements['#prefix'] = '<div id="' . $elements['#id'] . '">';
+  $elements['#prefix'] = '<div '. drupal_attributes(array('id' => drupal_html_id($elements['#id']))) . '>';
   $elements['#suffix'] = '</div>';
   return $elements;
 }
@@ -330,29 +332,25 @@
  * Implements hook_field_delete_instance().
  */
 function field_conditional_state_field_delete_instance($instance) {
-  $args = array(
-    ':type' => $instance['entity_type'],
-    ':bundle' => $instance['bundle'],
-  );
-  $q = db_query("SELECT group_id, field_name FROM {field_conditional_states_groups} WHERE entity_type = :type AND bundle = :bundle", $args);
+  $query = db_query("SELECT group_id, field_name FROM {field_conditional_states_groups} WHERE entity_type = :type AND bundle = :bundle", array(':type' => $instance['entity_type'], ':bundle' => $instance['bundle']));
 
-  while ($record = $q->fetchAssoc()) {
-    $del = db_delete('field_conditional_states');
-    $del->condition('group_id', $record['group_id']);
+  while ($record = $query->fetchAssoc()) {
+    $delete = db_delete('field_conditional_state');
+    $delete->condition('group_id', $record['group_id']);
 
     // If the current groups field name is equal to the deleted field name
     // delete the group and ALL associated states
     // otherwise delete only the conditions
     // where the deleted field is the control field.
     if ($record['field_name'] == $instance['field_name']) {
-      $del_group = db_delete('field_conditional_states_groups');
-      $del_group->condition('group_id', $record['group_id']);
-      $del_group->execute();
+      db_delete('field_conditional_states_group')
+        ->condition('group_id', $record['group_id'])
+        ->execute();
     }
     else {
-      $del->condition('control_field', $record['field_name']);
+      $delete->condition('control_field', $record['field_name']);
     }
 
-    $del->execute();
+    $delete->execute();
   }
 }
