content_crud.inc 6.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
// $Id$

/**
 * @file
 * Create/Read/Update/Delete functions for CCK-defined object types.
 */

/**
 * Create a new content type.
 *
 * @param $properties
 *   An array of properties to load the content type with. Valid keys:
14
 *   - '' -
15 16 17 18 19 20 21 22 23 24 25 26
 * @return
 *   The ID of the newly-created content type.
 */
function content_type_create($properties) {
  // TODO
}

/**
 * Load a content type.
 *
 * @param $properties
 *   An array of properties to use in selecting a content type. Valid keys:
27
 *   - '' -
28 29 30 31 32 33 34 35 36 37 38 39
 * @return
 *   The content type array.
 */
function content_type_read($properties) {
  // TODO
}

/**
 * Update an existing content type.
 *
 * @param $properties
 *   An array of properties to set in the content type. Valid keys:
40
 *   - '' -
41 42 43 44 45 46 47 48 49 50 51 52
 * @return
 *   The number of content types updated.
 */
function content_type_update($properties) {
  // TODO
}

/**
 * Delete an existing content type.
 *
 * @param $properties
 *   An array of properties to use in selecting a content type. Valid keys:
53
 *   - 'type_name' - The name of the content type to be deleted.
54 55 56 57
 * @return
 *   The number of content types deleted.
 */
function content_type_delete($properties) {
58 59 60 61 62
  // Delete all nodes of this content type.
  $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $properties['type_name']);
  while ($node = db_fetch_object($result)) {
    node_delete($node->nid);
  }
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
  $result = db_query("SELECT field_name FROM {node_field_instance} WHERE type_name = '%s'", $properties['type_name']);
  $field_names = array();
  while ($type = db_fetch_array($result)) {
    $field_names[] = $type['field_name'];
  }
  foreach ($field_names as $field_name) {
    content_field_instance_delete(array('type_name' => $properties['type_name'], 'field_name' => $field_name));
  }

  $number_deleted = db_query("DELETE FROM {node_type_content} WHERE type_name = '%s'", $properties['type_name']);

  db_query("DROP TABLE {node_". $properties['type_name'] ."}");

  content_clear_type_cache();
78 79
  menu_rebuild();

80
  return $number_deleted;
81 82 83 84 85 86
}


/**
 * Create a new field.
 *
87 88 89 90
 * Any call to this function *must* be immediately followed by a call to
 * content_field_instance_create(), or the database could be left in an
 * inconsistent state.
 *
91 92
 * @param $properties
 *   An array of properties to load the field with. Valid keys:
93
 *   - '' -
94 95 96 97 98 99 100 101 102 103 104 105
 * @return
 *   The ID of the newly-created field.
 */
function content_field_create($properties) {
  // TODO
}

/**
 * Load a field.
 *
 * @param $properties
 *   An array of properties to use in selecting a field. Valid keys:
106
 *   - '' -
107 108 109 110 111 112 113 114 115 116 117 118
 * @return
 *   The field array.
 */
function content_field_read($properties) {
  // TODO
}

/**
 * Update an existing field.
 *
 * @param $properties
 *   An array of properties to set in the field. Valid keys:
119
 *   - '' -
120 121 122 123 124 125 126 127 128 129 130 131
 * @return
 *   The number of fields updated.
 */
function content_field_update($properties) {
  // TODO
}

/**
 * Delete an existing field.
 *
 * @param $properties
 *   An array of properties to use in selecting a field. Valid keys:
132
 *   - 'field_name' - The name of the field to be deleted.
133 134 135 136
 * @return
 *   The number of fields deleted.
 */
function content_field_delete($properties) {
137 138 139 140 141 142 143 144 145
  $result = db_query("SELECT type_name FROM {node_field_instance} WHERE field_name = '%s'", $properties['field_name']);
  $type_names = array();
  while ($type = db_fetch_array($result)) {
    $type_names[] = $type['type_name'];
  }
  foreach ($type_names as $type_name) {
    content_field_instance_delete(array('type_name' => $type_name, 'field_name' => $properties['field_name']));
  }
  return (count($type_names) ? 1 : 0);
146 147 148 149 150 151 152 153
}


/**
 * Create a new field instance.
 *
 * @param $properties
 *   An array of properties to load the field instance with. Valid keys:
154
 *   - '' -
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
 * @return
 *   The ID of the newly-created field instance.
 */
function content_field_instance_create($properties) {
  // TODO
}

/**
 * Load a field instance.
 *
 * @param $properties
 *   An array of properties to use in selecting a field instance. Valid keys:
 *   - 'type_name' - The name of the content type in which the instance exists.
 *   - 'field_name' - The name of the field whose instance is to be loaded.
 * @return
 *   The field instance array.
 */
function content_field_instance_read($properties) {
  // TODO
}

/**
 * Update an existing field instance.
 *
 * @param $properties
 *   An array of properties to set in the field instance. Valid keys:
181
 *   - '' -
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
 * @return
 *   The number of field instance updated.
 */
function content_field_instance_update($properties) {
  // TODO
}

/**
 * Delete an existing field instance.
 *
 * @param $properties
 *   An array of properties to use in selecting a field instance. Valid keys:
 *   - 'type_name' - The name of the content type in which the instance exists.
 *   - 'field_name' - The name of the field whose instance is to be deleted.
 * @return
 *   The number of field instances deleted.
 */
function content_field_instance_delete($properties) {
  $number_deleted = db_query("DELETE FROM {node_field_instance} WHERE type_name = '%s' AND field_name = '%s'", $properties['type_name'], $properties['field_name']);
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  $type = content_types($properties['type_name']);
  $field = $type['fields'][$properties['field_name']];
  $field_types = _content_field_types();
  $field_type = $field_types[$field['type']];
  $columns = module_invoke($field_type['module'], 'field_settings', 'database columns', $field);

  $instances = db_result(db_query("SELECT COUNT(*) FROM {node_field_instance} WHERE field_name = '%s'", $properties['field_name']));

  // If only one instance remains, we may need to change the database
  // representation for this field.
  if ($instances == 1) {
    if (!($field['multiple'])) {
      // Multiple-valued fields are always stored per-content-type.
      if (is_array($columns) && count($columns)) {
        $new_field = $field;
        $new_field['db_storage'] = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
        db_query("UPDATE {node_field} SET db_storage = %d WHERE field_name = '%s'", CONTENT_DB_STORAGE_PER_CONTENT_TYPE, $properties['field_name']);
        content_alter_db_field($field, $columns, $new_field, $columns);
      }
    }
  }

  // If no instances remain, delete the field entirely.
  else if ($instances == 0) {
    if (is_array($columns) && count($columns)) {
      content_alter_db_field($field, $columns, array(), array());
    }
    db_query("DELETE FROM {node_field} WHERE field_name = '%s'", $properties['field_name']);
  }

  content_clear_type_cache();
233

234 235
  return $number_deleted;
}
John Hwang's avatar
John Hwang committed
236