You are here

function fivestar_update_6104 in Fivestar 6

Same name and namespace in other branches
  1. 6.2 fivestar.install \fivestar_update_6104()

Update CCK target logic to rename "target" to "php_target".

File

./fivestar.install, line 166
Installation file for fivestar module.

Code

function fivestar_update_6104() {

  // Previously ran as fivestar_update_5703 on Drupal 5.
  // Also, don't run this update if CCK is not enabled.
  if (fivestar_update_version() >= 6100 && module_exists('content')) {
    $result = db_query("SELECT field_name, global_settings FROM {content_node_field} WHERE type = 'fivestar'");
    while ($field = db_fetch_object($result)) {
      $settings = unserialize($field->global_settings);
      if (!empty($settings['target'])) {
        if (is_numeric($settings['target'])) {

          // If previously a straight integer, just add a "return" to the number.
          $settings['php_target'] = 'return ' . $settings['target'];
        }
        else {

          // If already PHP code, remove the PHP brackets.
          $php = trim($settings['target']);
          $php = preg_replace('/^<\\?(php)?/', '', $php);
          $php = preg_replace('/\\?>$/', '', $php);
          $settings['php_target'] = $php;
        }
      }
      unset($settings['target']);
      unset($settings['php']);
      db_query("UPDATE {content_node_field} SET global_settings = '%s' WHERE field_name = '%s'", serialize($settings), $field->field_name);
    }
  }
  return array();
}