/[drupal]/contributions/modules/last_node/last_node.module
ViewVC logotype

Diff of /contributions/modules/last_node/last_node.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.6.1, Sat Jan 27 10:25:44 2007 UTC revision 1.1.6.2, Mon Jun 30 14:58:51 2008 UTC
# Line 0  Line 1 
1    <?php
2    
3    // Copyright of Fabio Mucciante. See LICENSE.txt for redistribution allowances.
4    
5    /**
6     * Implementation of hook_help().
7     *
8     */
9    function last_node_help($section) {
10      switch ($section) {
11        case 'admin/help#last_node':
12          $output = t('<p>This module displays the list of inserted lately items (sorted by time) for each node.<br />Set your own nodes that you want to see at <a href="?q=admin/settings/last_node">settings page</a> and view this page with <a href="?q=last_node/">last_node/</a> address, you can specify a title and image for each item.</p>');
13          $output .= t('<p>This module provide:<ul> <li>Block <em>Last 10</em>: Show your last 10 items of all nodes;</li> <li>Block <em>Navigate Last Node</em>: Show Block of all Type node allowed;</li> <li>Block <em>Last 10 - type</em>: any node type can show yours last 10 element at owned block;</li> <li>Block <em>Sticky Last Node</em>: this block show your ten last nodes with sticky flag enabled;</li> <li>Menu Items for each node type;</li> <li>API for display in your own page:');
14          $output .= t('<ol><li><em>lastnode_display($title,$node_type,$num_item)</em>: if you want show your last items for node specified at own page; <li><em>lastnode_imgdisplay($title,$node_type,$num_item)</em>: show also image for each node.</ol></ul></li></p>');
15          $output .= t('<h3>Example for API usage:</h3><p><ul><li> Enable the page and story module at <a href="?q=admin/modules">administer &raquo; modules</a> page. <li>Select <a href="?q=node/add/page">create content &raquo; page</a>; <li>For input format, select PHP code; <li>Check frontpage option; <li>Give the page a title. For body, put: <pre>&lt;?php print lastnode_display(\'Last 10 Node of Story\',\'story\',10); ?&gt;</pre> <li>Save the page.</ul></p>');
16          $output .= t('<h3>Tips for blocks <em>Last 10 - type</em>:</h3><p>For show these blocks only when the node page it\'s display complete then set the <em>Auto-Hide</em> option in <a href="?q=admin/settings/last_node">settings page</a></p>');
17          return $output;
18          break;
19        case 'admin/modules#description':
20          return t('This module displays the list of inserted lately items (sorted by time) for each node and provide blocks and api for a fast access to these informations.');
21          break;
22      }
23    }
24    
25    /**
26     * Implementation of hook_block().
27     *
28     */
29    function last_node_block($op = 'list',$delta = '0') {
30      if ($op == 'list') {
31        $blocks['0']['info'] = t('Last 10 Nodes');
32        $blocks['1']['info'] = t('Navigate Last Node');
33        $blocks['2']['info'] = t('Sticky Last Node');
34        _last_node_blocknode($op,2,$blocks);
35        return $blocks;
36      }
37      else if ($op == 'view') {
38        switch ($delta) {
39          case '0':
40            $block['subject'] = t('Last 10:');
41            $block['content'] = _last_node_contents('0');
42            break;
43          case '1':
44            $block['subject'] = t('Last Node:');
45            $block['content'] = _last_node_contents('1');
46            break;
47          case '2':
48            $block['subject'] = t('Sticky Last Node:');
49            $block['content'] = _last_node_contents('2');
50            break;
51          default:
52            if (_last_node_blockhide($delta)) {
53              return;
54            }
55            $block['subject'] = _last_node_blocknode($op,$delta,$block);
56            $block['content'] = _last_node_contents($delta);
57            break;
58        }
59        return $block;
60      }
61    }
62    
63    function _last_node_blockhide($type_block) {
64      if (!variable_get('last_node_blockhide', FALSE))
65        return FALSE;
66    
67      if (_last_node_validnode($type_block)) {
68        if ((arg(0) == 'node') && is_numeric(arg(1)) ) {
69          $node = node_load(arg(1));
70        }
71        return $node->type != $type_block ? TRUE : FALSE;
72      }
73    }
74    
75    function _last_node_blocknode($op,$type_block,&$blocks) {
76      if ($op == 'list') {
77        $node = array_values(variable_get('last_node_check',array()));
78        if (!empty($node)) {
79          foreach ($node as $key => $value) {
80            if ($value)
81              $blocks[$value]['info'] = 'Last 10 - '. $value;
82          }
83        }
84        return;
85      }
86      if ($op == 'view') {
87        if (_last_node_validnode($type_block)) {
88          return variable_get('last_node_labelnode_'. $type_block,t('Last 10 -') .' '. $type_block);
89        }
90      }
91    }
92    
93    /**
94     * A block content function.
95     *
96     */
97    function _last_node_contents($which_block = '0') {
98      if ($which_block == '0') {
99        $node = array_keys(array_filter(variable_get('last_node_check',array())));
100        if (!empty($node)) {
101          $placeholders = implode(',', array_fill(0, count($node), "'%s'"));
102          $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.type in ($placeholders) AND n.status = 1 ORDER BY n.created DESC"), $node, 0, 10);
103          while ($node = db_fetch_object($result)) {
104            $output .= l($node->title,'node/'. $node->nid) ."<br />-<br />";
105          }
106        }
107        $output = '<marquee id="last_node-block" direction="up" scrollamount="1" scrolldelay="1" onMouseOver="this.stop()" onMouseOut="this.start()">'. $output .'</marquee>';
108        drupal_add_css(drupal_get_path("module","last_node") ."/last_node.css");
109        return $output;
110      }
111      if ($which_block == '1') {
112        $output = '<div class="last_node-nodeblock"><div class="last_node-linknodetypeblock">';
113        $args['class'] = 'last_node-linknodetypeblock';
114        $node = array_filter(variable_get('last_node_check',array()));
115        if (!empty($node)) {
116          foreach ($node as $value => $key) {
117            $nodes["$value"] = variable_get('last_node_labelnode_'. $value, $value);
118          }
119          asort($nodes);
120          reset($nodes);
121          foreach ($nodes as $key => $value) {
122            $title = _last_node_normalize_linkblock($value, $key);
123            $output .= html_entity_decode(l($title,'last_node/'. $key, $args)) .'<br />';
124          }
125        }
126        drupal_add_css(drupal_get_path("module","last_node") ."/last_node.css");
127        return $output .'</div></div>';
128      }
129      if ($which_block == '2') {
130        $node = variable_get('last_node_check',array());
131        if (!empty($node)) {
132          $placeholders = implode(',', array_fill(0, count($node), "'%s'"));
133          $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.sticky FROM {node} n WHERE n.type in ($placeholders) AND n.status = 1 AND n.sticky = 1 ORDER BY n.created DESC"), $node, 0, 10);
134          while ($node = db_fetch_object($result)) {
135            $output .= l($node->title,'node/'. $node->nid) ."<br />-<br />";
136          }
137        }
138        $output = '<marquee id="last_node-sticky" direction="up" scrollamount="1" scrolldelay="1" onMouseOver="this.stop()" onMouseOut="this.start()">'. $output .'</marquee>';
139        drupal_add_css(drupal_get_path("module","last_node") ."/last_node.css");
140        return $output;
141      }
142      if (_last_node_validnode($which_block)) {
143        $result = db_query_range("SELECT n.title, n.nid FROM {node} n WHERE n.status = 1 AND n.type = '%s' ORDER BY n.created DESC", $which_block, 0, 10);
144        while ($node = db_fetch_object($result)) {
145          $output[] = l($node->title,'node/'. $node->nid);
146        }
147        $output = theme_item_list($output);
148        $output .= "<div class=\"more-link\">". l( t("Read More"),'last_node/'. $which_block,array('title'=>t('Read More...'))) ."</div>";
149        drupal_add_css(drupal_get_path("module","last_node") ."/last_node.css");
150        return $output;
151      }
152    
153    }
154    
155    //Deprecated
156    function lastnode_css() {
157      drupal_add_css(drupal_get_path("module","last_node") ."/last_node.css");
158    }
159    
160    function lastnode_display($title="", $node_type="", $numitem = 5, $alt_text="") {
161      $lista = array();
162      $args = array();
163    
164      drupal_add_css(drupal_get_path("module","last_node") ."/last_node.css");
165      $result = db_query_range("SELECT n.title, n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.type = '%s' ORDER BY n.created DESC", $node_type, 0, $numitem);
166    
167      while ($node = db_fetch_object($result)) {
168        $lista[] = l($node->title ." (". format_date($node->created,'small') .")",'node/'. $node->nid);
169      }
170    
171      if ($alt_text == '') {
172        $n = node_get_types();
173        $alt_text = $n[$node_type]->description;
174      }
175      $args['title'] = $alt_text;
176    
177      $output = theme('item_list',$lista);
178      $output = '<h2 class="page-title"><div class="last_node-linknodetype">'. html_entity_decode(l($title,'last_node/'. $node_type,$args)) .'</div></h2><br />'. $output;
179    
180      //---Feed RSS
181      $output .= theme('feed_icon',url('last_node/feed/'. $node_type), 'Feed: '. $node_type);
182      drupal_add_link(array('rel' => 'alternate',
183                            'type' => 'application/rss+xml',
184                            'title' => t('RSS - blogs'),
185                            'href' => url("last_node/feed/$node_type")));
186    
187      return $output;
188    }
189    
190    function lastnode_imgdisplay($title = "", $node_type = "", $numitem = 5) {
191      return lastnode_display(_last_node_normalize_title($title,$node_type),$node_type,$numitem);
192    }
193    
194    function _last_node_normalize_title($title,$node_type)  {
195      $image = variable_get('last_node_imagenode_'. $node_type,'');
196      if ($image) {
197        $title = theme('image',$image,$title,$title,array('class'=>'last_node-imagenode'),TRUE) . $title;
198      }
199      return $title;
200    }
201    
202    function _last_node_normalize_linkblock($title,$node_type)  {
203      $image = variable_get('last_node_imagenode_'. $node_type,'');
204      if ($image) {
205        $title = theme('image',$image,$title,$title,array('class'=>'last_node-imagelinkblock'),TRUE) . $title;
206      }
207      return $title;
208    }
209    
210    /**
211     * menu hook
212     *
213     */
214    function last_node_menu() {
215      $items = array();
216    
217      $items['admin/settings/last_node'] = array(
218          'title' => 'Last Node',
219          'description' => 'Enable your nodes for <em>last_node/</em> node or configure labels and images.',
220          'page callback' => 'drupal_get_form',
221          'page arguments' => array('last_node_admin_settings'),
222          'access arguments' => array('administer site configuration'),
223          'type' => MENU_NORMAL_ITEM,
224          'file' => 'last_node.admin.inc');
225    
226      $items['last_node'] = array(
227          'title' => 'Last Node',
228          'page callback' => 'last_node_page',
229          'access arguments' => array('access content'),
230          'type' => MENU_NORMAL_ITEM);
231    
232      $node = variable_get('last_node_check', array());
233      if (!empty($node)) {
234        foreach ($node as $key => $value) {
235          if ($value) {
236            $title = variable_get("last_node_labelnode_$value",$value);
237            $items["last_node/$value"] = array(
238                'title' => $title,
239                'page callback' => 'last_node_page',
240                'page arguments' => array(1,2),
241                'access arguments' => array('access content'),
242                'type' => MENU_NORMAL_ITEM);
243          }
244        }
245      }
246      return $items;
247    }
248    
249    function _last_node_validnode($type) {
250      $node = variable_get('last_node_check', array());
251    
252      if (empty($node)) {
253        return FALSE;
254      }
255      if ($type) {
256        return in_array($type, array_values($node));
257      }
258      return TRUE;
259    }
260    
261    function _last_node_feed($type) {
262      if (!_last_node_validnode($type)) {
263        drupal_not_found();
264        return;
265      }
266      $result = db_query_range("SELECT n.nid, n.title, n.created, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = '%s' AND n.status = 1 ORDER BY n.created DESC", $type, 0, 10);
267      $channel['title'] = variable_get('site_name', 'drupal') ." ". variable_get('last_node_labelnode_'. $type, $type);
268      $channel['link'] = url('last_node/'.$type);
269      $channel['description'] = variable_get('site_name', 'drupal') ." Sezione ". variable_get('last_node_labelnode_'. $type, $type);
270    
271      $items = array();
272      while ($row = db_fetch_object($result)) {
273        $items[] = $row->nid;
274      }
275      node_feed($items, $channel);
276    }
277    
278    function last_node_page($type = NULL, $id = NULL) {
279      if ($type == 'feed') {
280        if ($id) {
281          _last_node_feed($id);
282        }
283        else drupal_not_found();
284      }
285      else {
286        return _last_node_page($type);
287      }
288    }
289    
290    function _last_node_page($type = NULL) {
291      if (!_last_node_validnode($type)) {
292        drupal_not_found();
293        return;
294      }
295    
296      $output = '';
297      drupal_add_css(drupal_get_path("module","last_node") ."/last_node.css");
298      if (!$type) {
299        $node = variable_get('last_node_check', array());
300        if (!empty($node)) {
301          foreach ($node as $key => $value) {
302            if ($value) {
303              $title = variable_get('last_node_labelnode_'. $value, $value);
304              $output .= lastnode_imgdisplay($title, $value);
305            }
306          }
307        }
308        return $output;
309      }
310    
311      drupal_set_title(_last_node_normalize_title(drupal_get_title(), $type));
312    
313      $result = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = '%s' AND n.status = 1 ORDER BY n.created DESC"), variable_get('default_nodes_main', 10), 0, NULL, $type);
314    
315      while ($node = db_fetch_object($result)) {
316        $output .= node_view(node_load($node->nid), 1);
317      }
318      $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
319      return $output;
320    }
321    
322    ?>

Legend:
Removed from v.1.1.6.1  
changed lines
  Added in v.1.1.6.2

  ViewVC Help
Powered by ViewVC 1.1.2