function content_content_save
Search API
7.x content.module | content_content_save(&$content) |
6.x content.module | content_content_save(&$content) |
Implements hook_content_save. We will save the content object to the database.
1 call to content_content_save()
- announcements_enable in modules/
announcements/ announcements.install
File
- modules/
content/ content.module, line 1813
Code
function content_content_save(&$content) {
global $user;
$types = content_get_types();
$type = $content->type;
if (!isset($content->title) || !$content->title || $content->title === NULL || $content->title === FALSE) {
$content->title = t("Content") . " " . time(); // give a default title, since title cannot be null.
}
$log = @trim($content->log);
// If this is a NEW content, then insert once into content table to get cid. Then, insert into content_versions to get vid. Then update content table with vid.
if ($content->cid == 'new') {
db_query('INSERT INTO content (user_id, `type`, title, posted, updated, published, delete_flag, `log`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)', array($user->id, $type, $content->title, time(), time(), $content->published, 0, $log));
$content->cid = db_insert_id();
// insert into content_versions to get the vid.
db_query("INSERT INTO content_versions (cid, `user_id`) VALUES (?,?)", array($content->cid, $user->id));
$content->vid = db_insert_id();
}
else {
// If this is an EXISTING content, then insert once into content_versions to get new vid. Update title and vid in content table.
// insert into content_versions to get the vid.
db_query("INSERT INTO content_versions (`cid`, `user_id`) VALUES (?,?)", array($content->cid, $user->id));
$content->vid = db_insert_id();
}
if (!$content->delete_flag) {
$content->delete_flag = 0; // force content->delete_flag to equal a value.
}
// Now we should have a fully created $content object. Let's update the title and other values in the content table.
db_query("UPDATE content SET vid = ?, title = ?, updated = ?, published = ?, delete_flag = ?, `log` = ? WHERE
cid = ?", array($content->vid, $content->title, time(), $content->published, $content->delete_flag, $log, $content->cid));
// TODO: If deleting, should we delete associated files too? Maybe we do this on a cron after X number of months?
// Now we are going to insert all of the field values. We assume there is a table ALREADY CREATED with our field names
// as field__the_name.
// Insert new row into content__$type table, including all the extra fields and such. It should
// look like cid | vid | field__name | field__name | and so on.
//
// Add in our new field values.
$params = array();
$all_fields = array();
$param_names = array();
$all_fields [] = 'cid';
$all_fields [] = 'vid';
$param_names [] = ':cid';
$param_names [] = ':vid';
$params [':cid'] = $content->cid;
$params [':vid'] = $content->vid;
foreach ($types [$type]['fields'] as $fieldname => $details) {
if ($details ['type'] != 'cfieldset' && $details ['type'] != '' && !strstr($details ['type'], 'markup')) { // skip markup and fieldsets
$db_field_name = "field__$fieldname";
$all_fields [] = $db_field_name;
$param_names [] = ":$db_field_name";
$val = NULL;
if (isset($content->{$db_field_name}) && isset($content->{$db_field_name}['value'])) {
$val = $content->{$db_field_name}['value'];
}
if ($details ['type'] == 'checkboxes' && is_array($val)) {
// Since we came from "checkboxes", and val is an array, we will save the following way:
// ~~key~~key~~key~~. This lets us query like so: LIKE '%~~key~~%' to find elements with that key. Not the most
// elegant way to do it, but it should work for now.
$new_val = "~~"; // always begin with ~~
foreach ($val as $k => $v) {
$new_val .= $k . "~~"; // always end with ~~.
}
$val = $new_val;
}
else if (is_array($val)) {
// Not sure what this is, but we will serialize it.
$val = serialize($val);
}
$params [":field__$fieldname"] = $val;
}
else if ($details ['type'] == 'cfieldset') {
// This is a fieldset, so now we need to descend into its elements to find the fields we need to save.
foreach ($details ['elements'] as $c => $v) {
foreach ($details ['elements'][$c] as $fieldname => $edetails) {
if (strstr($edetails ['type'], 'markup') || $edetails ['type'] == '') {
continue; // skip markup
}
$db_field_name = "field__$fieldname";
$all_fields [] = $db_field_name;
$param_names [] = ":$db_field_name";
$val = NULL;
if (isset($content->{$db_field_name}) && isset($content->{$db_field_name}['value'])) {
$val = $content->{$db_field_name}['value'];
}
if ($edetails ['type'] == 'checkboxes' && is_array($val)) {
// Since we came from "checkboxes", and val is an array, we will save the following way:
// ~~key~~key~~key~~. This lets us query like so: LIKE '%~~key~~%' to find elements with that key. Not the most
// elegant way to do it, but it should work for now.
$new_val = "~~"; // always begin with ~~
foreach ($val as $k => $v) {
$new_val .= $k . "~~"; // always end with ~~.
}
$val = $new_val;
}
else if (is_array($val)) {
// Not sure what this is, but we will serialize it.
$val = serialize($val);
}
$params [":field__$fieldname"] = $val;
}
}
} //else/if
} // foreach
$sql = "INSERT INTO content__$type
(" . join(", ", $all_fields) . ")
VALUES (" . join(", ", $param_names) . ")";
db_query($sql, $params);
// Re-calc our alert counts.
fp_recalculate_alert_count_by_type();
watchdog('content', "content_save cid:$content->cid");
}