You are here

function field_permissions_update_6001 in Field Permissions 6

Update global field settings for field permissions.

File

./field_permissions.install, line 76
Installation hooks for Field Permissions module.

Code

function field_permissions_update_6001() {
  $ret = array();
  drupal_load('module', 'content');
  $result = db_query('SELECT field_name, global_settings FROM {' . content_field_tablename() . '}');
  while ($field = db_fetch_object($result)) {
    $global_settings = unserialize($field->global_settings);
    if (isset($global_settings['field_permissions']) && !is_array($global_settings['field_permissions'])) {
      if ($global_settings['field_permissions'] == 'any') {
        $global_settings['field_permissions'] = array(
          'view' => 'view',
          'edit' => 'edit',
        );
      }
      elseif ($global_settings['field_permissions'] == 'own') {
        $global_settings['field_permissions'] = array(
          'view' => 'view',
          'view own' => 'view own',
          'edit' => 'edit',
          'edit own' => 'edit own',
        );
      }
      else {
        unset($global_settings['field_permissions']);
      }
      $global_settings = serialize($global_settings);

      // We cannot use update_sql() here because of curly braces in serialized array.
      $sql = "UPDATE {" . content_field_tablename() . "} SET global_settings = '%s' WHERE field_name = '%s'";
      db_query($sql, $global_settings, $field->field_name);
      $ret[] = array(
        'success' => TRUE,
        'query' => strtr('Field permission settings have been updated for field %field-name.', array(
          '%field-name' => $field->field_name,
        )),
      );
    }
  }

  // Rebuild content caches only if necessary.
  if (!empty($ret)) {
    content_clear_type_cache();
  }
  return $ret;
}