From: garvinhicking Date: Fri, 8 Jun 2007 08:52:02 +0000 (+0000) Subject: Support random ordering, patch by chrisbra X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ba3ecd5a8039b79820c165e5bc7ecccef4165ac7;p=s9y.git Support random ordering, patch by chrisbra --- diff --git a/plugins/serendipity_plugin_recententries/lang_en.inc.php b/plugins/serendipity_plugin_recententries/lang_en.inc.php index 6ae4424..3e972b1 100644 --- a/plugins/serendipity_plugin_recententries/lang_en.inc.php +++ b/plugins/serendipity_plugin_recententries/lang_en.inc.php @@ -14,5 +14,6 @@ @define('PLUGIN_RECENTENTRIES_NUMBER_FROM_DESC', 'Only recent entries that are not on the front page will be shown. (Default: latest ' . $serendipity['fetchLimit'] . ' will be skipped)'); @define('PLUGIN_RECENTENTRIES_NUMBER_FROM_RADIO_ALL', 'Show all'); @define('PLUGIN_RECENTENTRIES_NUMBER_FROM_RADIO_RECENT', 'Skip front page items'); +@define('PLUGIN_RECENTENTRIES_RANDOMIZE', 'Show Random Articles'); +@define('PLUGIN_RECENTENTRIES_RANDOMIZE_DESC', 'If set, random articles will be selected.'); -?> diff --git a/plugins/serendipity_plugin_recententries/serendipity_plugin_recententries.php b/plugins/serendipity_plugin_recententries/serendipity_plugin_recententries.php index 3aaca78..87ef287 100644 --- a/plugins/serendipity_plugin_recententries/serendipity_plugin_recententries.php +++ b/plugins/serendipity_plugin_recententries/serendipity_plugin_recententries.php @@ -1,6 +1,7 @@ +// Randomizing contributed by Christian Brabandt // Probe for a language include with constants. Still include defines later on, if some constants were missing $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php'; @@ -19,14 +20,14 @@ class serendipity_plugin_recententries extends serendipity_plugin { $propbag->add('name', PLUGIN_RECENTENTRIES_TITLE); $propbag->add('description', PLUGIN_RECENTENTRIES_BLAHBLAH); $propbag->add('stackable', true); - $propbag->add('author', 'Christian Machmeier'); - $propbag->add('version', '1.5'); + $propbag->add('author', 'Christian Machmeier, Christian Brabandt'); + $propbag->add('version', '1.7'); $propbag->add('requirements', array( 'serendipity' => '0.8', 'smarty' => '2.6.7', 'php' => '4.1.0' )); - $propbag->add('configuration', array('title', 'number', 'number_from', 'dateformat', 'category')); + $propbag->add('configuration', array('title', 'number', 'number_from', 'dateformat', 'category', 'randomize')); $propbag->add('groups', array('FRONTEND_VIEWS')); } @@ -46,16 +47,27 @@ class serendipity_plugin_recententries extends serendipity_plugin { $propbag->add('default', 10); break; - case 'number_from': - $propbag->add('type', 'radio'); - $propbag->add('name', PLUGIN_RECENTENTRIES_NUMBER_FROM); - $propbag->add('description', PLUGIN_RECENTENTRIES_NUMBER_FROM_DESC); - $propbag->add('radio', array( - 'value' => array('all', 'skip'), - 'desc' => array(PLUGIN_RECENTENTRIES_NUMBER_FROM_RADIO_ALL, PLUGIN_RECENTENTRIES_NUMBER_FROM_RADIO_RECENT) - )); - $propbag->add('default', 'all'); - break; + case 'number_from': + $propbag->add('type', 'radio'); + $propbag->add('name', PLUGIN_RECENTENTRIES_NUMBER_FROM); + $propbag->add('description', PLUGIN_RECENTENTRIES_NUMBER_FROM_DESC); + $propbag->add('radio', array( + 'value' => array('all', 'skip'), + 'desc' => array(PLUGIN_RECENTENTRIES_NUMBER_FROM_RADIO_ALL, PLUGIN_RECENTENTRIES_NUMBER_FROM_RADIO_RECENT) + )); + $propbag->add('default', 'all'); + break; + + case 'randomize': + $propbag->add('type', 'radio'); + $propbag->add('name', PLUGIN_RECENTENTRIES_RANDOMIZE); + $propbag->add('description', PLUGIN_RECENTENTRIES_RANDOMIZE_DESC); + $propbag->add('radio', array( + 'value' => array('yes', 'no'), + 'desc' => array(YES, NO) + )); + $propbag->add('default', 'no'); + break; case 'dateformat': $propbag->add('type', 'string'); @@ -108,6 +120,7 @@ class serendipity_plugin_recententries extends serendipity_plugin { $category = $this->get_config('category', 'none'); $title = $this->get_config('title', $this->title); $number_from_sw = $this->get_config('number_from'); + $randomize = ($this->get_config('randomize') == "yes") ? true : false ; $sql_join = ''; $sql_where = ''; @@ -135,6 +148,7 @@ class serendipity_plugin_recententries extends serendipity_plugin { } $sql_number = serendipity_db_limit_sql($number); + $db = $serendipity['dbType']; switch($number_from_sw) { case 'skip': @@ -145,16 +159,26 @@ class serendipity_plugin_recententries extends serendipity_plugin { if (!$dateformat || strlen($dateformat) < 1) { $dateformat = '%A, %B %e %Y'; } + if ($randomize) + if ($db == 'mysql' || $db == 'mysqli')) { + $sql_order = "ORDER BY RAND()"; + } else { + // SQLite and PostgreSQL support this, hooray. + $sql_order = "ORDER BY RANDOM()"; + } + } else { + $sql_order = "ORDER BY timestamp DESC "; + } - $entries_query = "SELECT id, - title, - timestamp - FROM {$serendipity['dbPrefix']}entries - $sql_join - WHERE isdraft = 'false' AND timestamp <= " . time() . " - $sql_where - ORDER BY timestamp DESC - $sql_number"; + $entries_query = "SELECT DISTINCT id, + title, + timestamp + FROM {$serendipity['dbPrefix']}entries + $sql_join + WHERE isdraft = 'false' AND timestamp <= " . time() . " + $sql_where + $sql_order + $sql_number"; $entries = serendipity_db_query($entries_query); if (isset($entries) && is_array($entries)) { @@ -180,5 +204,4 @@ class serendipity_plugin_recententries extends serendipity_plugin { } } -/* vim: set sts=4 ts=4 expandtab : */ -?> +/* vim: set sts=4 ts=4 smartindent autoindent : */