add('name', PLUGIN_EVENT_COMMUNITYRATING_TITLE); $propbag->add('description', PLUGIN_EVENT_COMMUNITYRATING_DESC); $propbag->add('stackable', false); $propbag->add('author', 'Garvin Hicking, Lewe Zipfel'); $propbag->add('version', '1.11'); $propbag->add('requirements', array( 'serendipity' => '0.9', 'smarty' => '2.6.7', 'php' => '4.1.0' )); $propbag->add('event_hooks', array( 'backend_publish' => true, 'backend_save' => true, 'backend_display' => true, 'entry_display' => true, 'external_plugin' => true, 'frontend_rss' => true, 'frontend_fetchentries' => true )); $propbag->add('groups', array('BACKEND_EDITOR')); $propbag->add('configuration', array('types')); $this->dependencies = array('serendipity_event_entryproperties' => 'keep'); } function introspect_config_item($name, &$propbag) { switch($name) { case 'types': $propbag->add('type', 'string'); $propbag->add('name', PLUGIN_EVENT_COMMUNITYRATING_TYPES); $propbag->add('description', ''); $propbag->add('default', PLUGIN_EVENT_COMMUNITYRATING_TYPES_DEFAULT); break; } return true; } function smarty_init() { global $serendipity; if (!isset($this->smarty_init)) { include_once dirname(__FILE__) . '/smarty.inc.php'; if (isset($serendipity['smarty'])) { $serendipity['smarty']->register_function('communityrating_show', 'communityrating_serendipity_show'); $this->smarty_init = true; } } } function generate_content(&$title) { $title = $this->title; } function &getSupportedProperties() { static $supported_properties = null; if ($supported_properties === null) { $_supported_properties = explode(',', $this->get_config('types')); $supported_properties = array(); foreach($_supported_properties AS $idx => $prop) { $prop = trim($prop); $supported_properties[$prop] = $prop; $supported_properties[$prop . '_id'] = ''; $supported_properties[$prop . '_rating'] = ''; } } return $supported_properties; } function addProperties(&$properties, &$eventData) { global $serendipity; // Get existing data $property = serendipity_fetchEntryProperties($eventData['id']); $supported_properties =& $this->getSupportedProperties(); foreach($supported_properties AS $prop_key => $_pkey) { if (!preg_match('@_(id|rating)$@', $prop_key)) { continue; } $prop_val = (isset($properties[$prop_key]) ? $properties[$prop_key] : null); $prop_key = 'cr_' . $prop_key; if (is_array($prop_val)) { $prop_val = ";" . implode(';', $prop_val) . ";"; } $q = "DELETE FROM {$serendipity['dbPrefix']}entryproperties WHERE entryid = " . (int)$eventData['id'] . " AND property = '" . serendipity_db_escape_string($prop_key) . "'"; serendipity_db_query($q); if (!empty($prop_val)) { $q = "INSERT INTO {$serendipity['dbPrefix']}entryproperties (entryid, property, value) VALUES (" . (int)$eventData['id'] . ", '" . serendipity_db_escape_string($prop_key) . "', '" . serendipity_db_escape_string($prop_val) . "')"; serendipity_db_query($q); } } } function event_hook($event, &$bag, &$eventData, $addData = null) { global $serendipity; $hooks = &$bag->get('event_hooks'); if (isset($hooks[$event])) { switch($event) { case 'backend_display': $props =& $this->getSupportedProperties(); ?>
$prop_val) { if (preg_match('@_(id|rating)$@', $prop)) { continue; } $vals = array('id' => '', 'rating' => ''); foreach($vals AS $vidx => $val) { if (isset($eventData['properties']['cr_' . $prop . '_' . $vidx])) { $vals[$vidx] = $eventData['properties']['cr_' . $prop . '_' . $vidx]; } elseif (isset($serendipity['POST']['properties'][$prop . '_' . $vidx])) { $vals[$vidx] = $serendipity['POST']['properties'][$prop . '_' . $vidx]; } else { $vals[$vidx] = ''; } } ?> :
: :
addProperties($serendipity['POST']['properties'], $eventData); return true; break; case 'entry_display': $this->smarty_init(); // PH: This is done after Garvins suggestion to patchup $eventData in case an entry // is in the process of being created. This must be done for the extended properties // to be applied in the preview. if (is_array($serendipity['POST']['properties']) && count($serendipity['POST']['properties']) > 0){ $parr = array(); $supported_properties =& $this->getSupportedProperties(); foreach($supported_properties AS $prop_key => $prop_val) { if (isset($serendipity['POST']['properties'][$prop_key])) $eventData[0]['properties']['cr_' . $prop_key] = $serendipity['POST']['properties'][$prop_key]; } } break; case 'frontend_fetchentries': case 'frontend_rss': $this->smarty_init(); break; case 'external_plugin': $params = explode('_', $eventData); if ($params[0] == 'communityrating') { $type = serendipity_db_escape_string($params[1]); $id = serendipity_db_escape_string($params[2]); if ($id == 'all') { $sql = "SELECT e.title, e.timestamp, p1.property, p1.entryid, p2.value, p1.value AS backupvalue FROM {$serendipity['dbPrefix']}entryproperties AS p1 LEFT OUTER JOIN {$serendipity['dbPrefix']}entryproperties AS p2 ON (p2.entryid = p1.entryid AND p2.property = 'cr_{$type}_rating') LEFT OUTER JOIN {$serendipity['dbPrefix']}entries AS e ON p2.entryid = e.id WHERE (p1.property = 'cr_{$type}_id') OR (p1.property LIKE 'cr_{$type}_rating:%') ORDER BY p2.value DESC"; } else { $sql = "SELECT e.title, e.timestamp, p1.property, p1.entryid, p2.value, p1.value AS backupvalue FROM {$serendipity['dbPrefix']}entryproperties AS p1 LEFT OUTER JOIN {$serendipity['dbPrefix']}entryproperties AS p2 ON (p2.entryid = p1.entryid AND p2.property = 'cr_{$type}_rating') LEFT OUTER JOIN {$serendipity['dbPrefix']}entries AS e ON p2.entryid = e.id WHERE (p1.property = 'cr_{$type}_id' AND p1.value = '{$id}') OR (p1.property = 'cr_{$type}_rating:{$id}') ORDER BY p2.value DESC LIMIT 1"; } $rows = serendipity_db_query($sql, false, 'assoc'); header('HTTP/1.0 200'); header('Status: 200 OK'); header('Content-Type: text/xml'); echo '' . "\n"; echo '' . "\n"; if (is_array($rows)) { foreach($rows AS $row) { if ($id == 'all') { if (preg_match('@:(.*)$@', $row['property'], $match)) { $cid = $match[1]; } else { $cid = $row['backupvalue']; } } else { $cid = $id; } echo '' . "\n"; echo '' . serendipity_utf8_encode($cid) . '' . "\n"; if (empty($row['value'])) { $row['value'] = $row['backupvalue']; } echo '' . serendipity_utf8_encode($row['value']) . '' . "\n"; if ($row['entryid'] > 0) { echo '' . serendipity_utf8_encode(serendipity_archiveURL($row['entryid'], $row['title'], 'baseURL', true, array('timestamp' => $row['timestamp']))) . '' . "\n"; } echo '' . "\n"; } } echo '' . "\n"; } default: return false; break; } } else { return false; } } } /* vim: set sts=4 ts=4 expandtab : */