toddajackson - 12 May 2009 06:06 PM
An incredibly detailed response to something I felt was possibly garbled and unintelligible. Bafadam - if you’re up for your sharing your mods to the plugin, I’d love to see them.
They’re neither fancy nor flexible. The first thing I did was write a completely separate function that swaps out custom field names for actual field names.
function customFieldSwap($arrCustomFields, $weblogId)
{
global $DB;
// Get custom fields from database
$customfieldsArray = $DB->query("SELECT exp_weblog_fields.field_id,
exp_weblog_fields.field_name,
exp_weblog_fields.field_label, exp_weblog_fields.field_fmt
FROM exp_weblogs, exp_weblog_fields
WHERE exp_weblogs.field_group = exp_weblog_fields.group_id
AND exp_weblogs.weblog_id = '".$weblogId."'");
// Map weblog custom fields to id's
$data = Array();
foreach ( $customfieldsArray->result as $row ) {
if (array_key_exists($row['field_name'], $arrCustomFields))
{
$fieldid = 'field_id_' . $row['field_id'];
$fieldname = $row['field_name'];
$data[$fieldid] = $arrCustomFields[$fieldname];
}
}
return $data;
}
It accepts an array of (customFieldName => customFieldData) and returns an array with (actualFieldName => actualFieldData). It’s not very flexible, though. Since title, pubdate, etc aren’t custom field names, they won’t get swapped, so if you’re using those fields in there, add them after you do your swapping. I suppose it could be updated to deal with system fields, but I’m honestly too lazy for that business, so… moving on.
Andrew is a better programmer than I am. So my code is infinitely less flexible than his, so bear that in mind as you continue reading on.
I added a new class variable called $parent_weblog_id. I created a new fetch param line for, you guess it, $parent_weblog_id. When you call your function, it’s now looking for the entry id of the related (I keep calling it parent) weblog entry.
Since I’m looping through the Feed level entries, the idea is that I’ve got access to this field pretty handily and can just pass it in real easy-like. Like I said, inflexible.
After this line:
$sql = $DB->insert_string('exp_weblog_data', $data);
if ( $this->runsql ) {
$DB->query($sql);
} else {
if ( $this->debug ) { print $sql . "<br/>\n"; }
}
I added in my relationship creation code:
//modification time
//Let's add our relationship code here.
//This is a two step process: 1. create the relationship and 2. update the feed item.
$sql = '';
$relData = array(
'rel_parent_id' => $entry_id,
'rel_child_id' => $this->parent_weblog_id,
'rel_type' => 'blog'
);
$sql = $DB->insert_string('exp_relationships', $relData);
$relID = '';
if ( $this->runsql ) {
$DB->query($sql);
$relID = $DB->insert_id;
} else {
if ( $this->debug ) { print $sql . "<br/>\n"; }
}
//Apparently, our relationship thing was successful, so let's update the weblog data now.
if ($relID != '') {
$relData = array(
'feed_id' => $relID
);
$relDataSwap = $this->customFieldSwap($relData, 3);
$sql = $DB->update_string('exp_weblog_data', $relDataSwap, 'entry_id = ' . $entry_id);
if ( $this->runsql ) {
$relID = $DB->query($sql);
} else {
if ( $this->debug ) { print $sql . "<br/>\n"; }
}
}
Andrew, this whole thing is your creation. If me posting any mod code to it bothers you, just let me know and I’ll yank it. You did some awesome work with this whole thing and I don’t want to piss you off. :)