]> git.mjollnir.org Git - s9y.git/commitdiff
More smarty functionality
authorgarvinhicking <garvinhicking>
Mon, 28 Nov 2005 11:05:39 +0000 (11:05 +0000)
committergarvinhicking <garvinhicking>
Mon, 28 Nov 2005 11:05:39 +0000 (11:05 +0000)
include/functions_entries.inc.php
include/functions_smarty.inc.php

index 3282d0772b74f9ce3c8fc0aa24818d43a5eaf6ba..47f672b519cef57e8f9337e57517513aca45f821 100644 (file)
@@ -805,9 +805,10 @@ function serendipity_getTotalEntries() {
  * @param   boolean     Indicates whether the assigned smarty variables should be parsed
  * @param   boolean     Indicates whether to apply footer/header event hooks
  * @param   boolean     Indicates whether the pagination footer should be displayed
+ * @param   mixed       Indicates whether the input $entries array is already grouped in preparation for the smarty $entries output array [TRUE], or if it shall be grouped by date [FALSE] or if a plugin hook shall be executed to modify data ['plugin']. This setting can also be superseded by a 'entry_display' hook.
  * @return
  */
-function serendipity_printEntries($entries, $extended = 0, $preview = false, $smarty_block = 'ENTRIES', $smarty_fetch = true, $use_hooks = true, $use_footer = true) {
+function serendipity_printEntries($entries, $extended = 0, $preview = false, $smarty_block = 'ENTRIES', $smarty_fetch = true, $use_hooks = true, $use_footer = true, $use_grouped_array = false) {
     global $serendipity;
 
     if ($use_hooks) {
@@ -826,33 +827,48 @@ function serendipity_printEntries($entries, $extended = 0, $preview = false, $sm
         $entries = array();
     }
 
-    $dategroup = array();
-    for ($x = 0, $num_entries = count($entries); $x < $num_entries; $x++) {
-        if (!empty($entries[$x]['properties']['ep_is_sticky']) && serendipity_db_bool($entries[$x]['properties']['ep_is_sticky'])) {
-            $entries[$x]['is_sticky'] = true;
-            $key = 'sticky';
-        } else {
-            $key = date('Ymd', serendipity_serverOffsetHour($entries[$x]['timestamp']));
-        }
-
-        if (!empty($entries[$x]['properties']['ep_cache_body'])) {
-            $entries[$x]['body']      = &$entries[$x]['properties']['ep_cache_body'];
-            $entries[$x]['is_cached'] = true;
-        }
+    // A plugin executed in entry_display should be able to change the way of ordering entries. Forward-Thinking. ;)
+    if (isset($entries['use_grouped_array'])) {
+        $use_grouped_array = $entries['use_grouped_array'];
+    }
 
-        if (!empty($entries[$x]['properties']['ep_cache_extended'])) {
-            $entries[$x]['extended']  = &$entries[$x]['properties']['ep_cache_extended'];
-            $entries[$x]['is_cached'] = true;
+    if ($use_grouped_array === false) {
+        // Use grouping by date (default)
+        $dategroup = array();
+        for ($x = 0, $num_entries = count($entries); $x < $num_entries; $x++) {
+            if (!empty($entries[$x]['properties']['ep_is_sticky']) && serendipity_db_bool($entries[$x]['properties']['ep_is_sticky'])) {
+                $entries[$x]['is_sticky'] = true;
+                $key = 'sticky';
+            } else {
+                $key = date('Ymd', serendipity_serverOffsetHour($entries[$x]['timestamp']));
+            }
+    
+            $dategroup[$key]['date']        = $entries[$x]['timestamp'];
+            $dategroup[$key]['is_sticky']   = (isset($entries[$x]['is_sticky']) && serendipity_db_bool($entries[$x]['is_sticky']) ? true : false);
+            $dategroup[$key]['entries'][]   = &$entries[$x];
         }
-
-        $dategroup[$key]['date']        = $entries[$x]['timestamp'];
-        $dategroup[$key]['is_sticky']   = (isset($entries[$x]['is_sticky']) && serendipity_db_bool($entries[$x]['is_sticky']) ? true : false);
-        $dategroup[$key]['entries'][]   = &$entries[$x];
+    } elseif ($use_grouped_array === 'plugin') {
+        // Let a plugin do the grouping
+        serendipity_plugin_api::hook_event('entry_groupdata', $entries);
+        $dategroup =& $entries;
+    } else {
+        $dategroup =& $entries;
     }
 
     foreach($dategroup as $properties) {
         foreach($properties['entries'] as $x => $_entry) {
             $entry = &$properties['entries'][$x]; // PHP4 Compat
+            
+            if (!empty($entry['properties']['ep_cache_body'])) {
+                $entry['body']      = &$entry['properties']['ep_cache_body'];
+                $entry['is_cached'] = true;
+            }
+    
+            if (!empty($entry['properties']['ep_cache_extended'])) {
+                $entry['extended']  = &$entry['properties']['ep_cache_extended'];
+                $entry['is_cached'] = true;
+            }
+            
             if ($preview) {
                 $entry['author']   = $entry['realname'];
                 $entry['authorid'] = $serendipity['authorid'];
index a993d9c017c4f4ef9f0d738f76ce4eb0e776b2e2..e182567db391b913d903f644b91d388f1fad2fec 100644 (file)
@@ -79,6 +79,209 @@ function serendipity_emptyPrefix($string, $prefix = ': ') {
     return (!empty($string) ? $prefix . htmlspecialchars($string) : '');
 }
 
+/**
+ * Smarty Function: Fetch and print a single or multiple entries
+ *
+ * @access public
+ * @param   array       Smarty parameter input array:
+ *                      [FETCHING]
+ *                          category:        (int)     The category ID (seperate multiple with ";") to fetch entries from
+ *                          viewAuthor:      (int)     The author ID (seperate multiple with ";") to fetch entries from
+ *                          page:            (int)     The number of the page for paginating entries
+ *                          id:              (int)     The ID of an entry. If given, only a single entry will be fetched. If left empty, multiple entries are fetched.
+ *                          range:           (mixed)   Restricts fetching entries to a specific timespan. Behaves differently depending on the type:
+ *                                           Numeric: 
+ *                                            YYYYMMDD - Shows all entries from YYYY-MM-DD. 
+ *                                            If DD is "00", it will show all entries from that month. 
+ *                                            If DD is any other number, it will show entries of that specific day.
+ *                                           2-Dimensional Array:
+ *                                            Key #0   - Specifies the start timestamp (unix seconds)
+ *                                            Key #1   - Specifies the end timestamp (unix seconds)
+ *                                           Other (null, 3-dimensional Array, ...):
+ *                                            Entries newer than $modified_since will be fetched 
+ *                          full             (boolean) Indicates if the full entry will be fetched (body+extended: TRUE), or only the body (FALSE).
+ *                          limit            (string)  Holds a "Y" or "X, Y" string that tells which entries to fetch. X is the first entry offset, Y is number of entries. If not set, the global fetchLimit will be applied (15 entries by default)
+ *                          fetchDrafts      (boolean) Indicates whether drafts should be fetched (TRUE) or not
+ *                          modified_since   (int)     Holds a unix timestamp to be used in conjunction with $range, to fetch all entries newer than this timestamp
+ *                          orderby          (string)  Holds the SQL "ORDER BY" statement.
+ *                          filter_sql       (string)  Can contain any SQL code to inject into the central SQL statement for fetching the entry
+ *                          noCache          (boolean) If set to TRUE, all entries will be fetched from scratch and any caching is ignored
+ *                          noSticky         (boolean) If set to TRUE, all sticky entries will NOT be fetched.
+ *                          
+ *                      [PRINTING]
+ *                          template:          (string)  Name of the template file to print entries with
+ *                          preview:           (boolean) Indicates if this is a preview
+ *                          block              (string   The name of the SMARTY block that this gets parsed into
+ *                          use_hooks          (boolean  Indicates whether to apply footer/header event hooks
+ *                          use_footer         (boolean  Indicates whether the pagination footer should be displayed
+ *                          groupmode          (string   Indicates whether the input $entries array is already grouped in preparation for the smarty $entries output array [TRUE], or if it shall be grouped by date [FALSE]
+ *                          skip_smarty_hooks  (boolean) If TRUE, no plugins will be executed at all
+ *                          skip_smarty_hook   (mixed)   Can be set to an array of plugin hooks to NOT execute
+ * @param   object      Smarty object
+ * @return  string      The Smarty HTML response
+ */
+function serendipity_smarty_fetchPrintEntries($params, &$smarty) {
+    global $serendipity;
+    static $entrycount = 0;
+    static $restore_var_GET_keys = array('category', 'viewAuthor', 'page');
+
+    // A counter variable to not assign template files multiple times
+    $entrycount++;
+
+    // Default values for function calls
+    if (empty($params['template'])) {
+        $params['template'] = 'entries.tpl';
+    }
+    
+    if (empty($params['range'])) {
+        $params['range'] = null;
+    }
+
+    if (empty($params['full'])) {
+        $params['full'] = true;
+    }
+    
+    if (empty($params['fetchDrafts'])) {
+        $params['fetchDrafts'] = false;
+    }
+
+    if (empty($params['modified_since'])) {
+        $params['modified_since'] = false;
+    }
+
+    if (empty($params['orderby'])) {
+        $params['orderby'] = 'timestamp DESC';
+    }
+
+    if (empty($params['noCache'])) {
+        $params['noCache'] = false;
+    }
+
+    if (empty($params['noSticky'])) {
+        $params['noSticky'] = false;
+    }
+
+    if (empty($params['preview'])) {
+        $params['preview'] = false;
+    }
+    
+    if (empty($params['block'])) {
+        $params['block'] = 'smarty_entries_' . $entrycount;
+    }
+
+    if (empty($params['use_hooks'])) {
+        $params['use_hooks'] = false;
+    }
+
+    if (empty($params['use_footer'])) {
+        $params['use_footer'] = false;
+    }
+    
+    if (empty($params['groupmode'])) {
+        $params['groupmode'] = 'date';
+    }
+    
+    if (empty($params['skip_smarty_hooks'])) {
+        $params['skip_smarty_hooks'] = true;
+    }
+
+    if (empty($params['skip_smarty_hook'])) {
+        $params['skip_smarty_hook'] = array();
+    }
+
+    // Some functions deal with the $serendipity array. To modify them, we need to store
+    // their original contents.
+    $old_var = array();
+    if (!empty($params['short_archives'])) {
+        $old_var['short_archives']     = $serendipity['short_archives'];
+        $serendipity['short_archives'] = $params['short_archives'];
+    }
+    
+    $old_var['skip_smarty_hooks']     = $serendipity['skip_smarty_hooks'];
+    $serendipity['skip_smarty_hooks'] = $params['skip_smarty_hooks'];
+
+    $old_var['skip_smarty_hook']     = $serendipity['skip_smarty_hook'];
+    $serendipity['skip_smarty_hook'] = $params['skip_smarty_hook'];
+
+    foreach($restore_var_GET_keys AS $key) {
+        if (!empty($params[$key])) {
+            $old_var['GET'][$key]     = $serendipity['GET'][$key];
+            $serendipity['GET'][$key] = $params[$key];
+        }
+    }
+
+    if (!empty($params['id'])) {
+        $entry = serendipity_fetchEntry(
+            'id', 
+            (int)$params['id'], 
+            $params['full'], 
+            $params['fetchDrafts']);
+    } else {
+        $entry = serendipity_fetchEntries(
+            $params['range'], 
+            $params['full'], 
+            $params['limit'], 
+            $params['fetchDrafts'], 
+            $params['modified_since'], 
+            $params['orderby'], 
+            $params['filter_sql'], 
+            $params['noCache'], 
+            $params['noSticky']
+        );
+        
+        // Check whether the returned entries shall be grouped specifically
+        switch ($params['groupmode']) {
+            case 'date':
+                // No regrouping required, printEntries() does it for us.
+                break;
+            
+            case 'category':
+                // Regroup by primary category
+
+                $groupdata = array();
+                foreach($entry AS $k => $_entry) {
+
+                    if (is_array($entry['categories'])) {
+                        $groupkey = $entry['categories'][0];
+                    } else {
+                        $groupkey = 'none';
+                    }
+                    $groupdata[$groupkey]['entries'] =& $_entry;
+                }
+                $entry =& $groupdata;
+                break;
+        }
+    }
+
+    serendipity_printEntries(
+        $entry,                                 // Entry data
+        (!empty($params['id']) ? true : false), // Extended data?
+        $params['preview'],                     // Entry preview?
+        false,                                  // Prevent Smarty parsing
+        $params['use_hooks'],
+        $params['use_footer'],
+        ($params['groupmode'] == 'date' ? true : false) // Grouping of $entry
+    );
+    
+    // Restore the $serendipity array after our modifications.
+    if (isset($old_var['short_archives'])) {
+        $serendipity['short_archives'] = $old_var['short_archives'];
+    }
+    
+    if (is_array($old_var['GET'])) {
+        foreach($old_var['GET'] AS $key => $val) {
+            $serendipity['GET'][$key] = $val;
+        }
+    }
+
+    $out = serendipity_smarty_fetch($params['block'], $params['template']);
+    
+    $serendipity['skip_smarty_hook']  = $old_var['skip_smarty_hook'];
+    $serendipity['skip_smarty_hooks'] = $old_var['skip_smarty_hooks'];
+
+    return $out;
+}
+
 /**
  * Smarty Function: Be able to include the output of a sidebar plugin within a smarty template
  *
@@ -358,6 +561,7 @@ function serendipity_smarty_init() {
         $serendipity['smarty']->register_function('serendipity_printComments', 'serendipity_smarty_printComments');
         $serendipity['smarty']->register_function('serendipity_printTrackbacks', 'serendipity_smarty_printTrackbacks');
         $serendipity['smarty']->register_function('serendipity_rss_getguid', 'serendipity_smarty_rss_getguid');
+        $serendipity['smarty']->register_function('serendipity_fetchPrintEntries', 'serendipity_smarty_fetchPrintEntries');
 
         $serendipity['smarty']->register_prefilter('serendipity_replaceSmartyVars');
     }