Coding standards
[project/simplenews_content_selection.git] / scs.theme.inc
1 <?php
2
3 /**
4 * @file
5 * Select Drupal content to create a newsletter
6 */
7
8 /**
9 * Theme the node selection form
10 */
11 function theme_scs_sortable_table($form) {
12 $form = $form['form'];
13 $headers = array(t('Node title'), t('Weight'));
14 $rows = array();
15 foreach (element_children($form['nodes']) as $nid) {
16 $row = array();
17 $row[] = db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
18 $row[] = drupal_render($form['nodes'][$nid]['weight']);
19 $rows[] = array(
20 'data' => $row,
21 'class' => array('draggable'),
22 );
23 }
24 $output = theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => 'scs-sort-nodes')));
25 $info = '';
26 if (isset($form['scs_title']) && isset($form['scs_toc'])) {
27 $info = drupal_render($form['scs_title']) . drupal_render($form['scs_toc']);
28 }
29 $output = $info . $output . drupal_render_children($form);
30 drupal_add_tabledrag('scs-sort-nodes', 'order', 'sibling', 'node-weight');
31 return $output;
32 }
33
34 /**
35 * Each selected node goes true this function to create a nice body
36 */
37 function theme_scs_node_output($node) {
38 //TODO Fix a teaser of this node
39 $output = '';
40 $output = '<div id="node_' . $node['nid'] . '">';
41 $output .= '<h1>' . $node['title'] . '</h1>';
42 $output .= '<p>' . $node['body'][LANGUAGE_NONE][0]['value'] . '</p>';
43 $output .= '<p>' . l(t('Read more'), 'node/' . $node['nid']) . '</p>';
44 $output .= '</div>';
45
46 return $output;
47 }
48
49 /**
50 * Theme the node sort form into a table
51 */
52 function theme_scs_sort_nodes($form) {
53 $form = $form['form'];
54 $headers = array(t('Node title'), t('Weight'));
55 $rows = array();
56
57 foreach ($form as $name => $field) {
58 if (preg_match('/^weight_/', $name)) {
59 $nid = explode('_', $name);
60 $nid = $nid[1];
61 $title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
62 unset($form[$name]['#title']);
63
64 $row = array();
65 $row[] = $title;
66 $row[] = drupal_render($form[$name]);
67 $rows[] = array(
68 'data' => $row,
69 'class' => array('draggable'),
70 );
71 }
72 }
73
74 return drupal_render($form['newsletter_title']) . drupal_render($form['newsletter_toc']) . drupal_render($form['newsletter_content_type']) . theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => 'scs_node_sort_table'))) . drupal_render_children($form);
75 }
76
77 /**
78 * Theme function to add the titles of the selected nodes at top of the newsletter
79 */
80 function theme_scs_node_titles($variables) {
81 if (variable_get('scs_format', 'plain') == 'plain') {
82 return implode("\n", $variables['titles']);
83 }
84 else {
85 return '<div id="toc">' . theme('item_list', array('items' => $variables['titles'])) . '</div>';
86 }
87 }
88
89 /**
90 * Theme a complete newsletter.
91 */
92 function theme_scs_newsletter_output($variables) {
93 $body = '';
94 $titles = array();
95
96 // Node information
97 foreach ($variables['nodes'] as $node) {
98 if ($variables['toc']) {
99 if (variable_get('scs_format', 'plain') == 'plain') {
100 $titles[] = $node->title;
101 }
102 else {
103 $titles[] = '<a href="#node_' . $node->nid . '">' . $node->title . '</a>';
104 }
105 }
106
107 $body .= theme('scs_node_output', (array) $node);
108 }
109
110 // ToC (if required)
111 if ($variables['toc']) {
112 $body = theme('scs_node_titles', array('titles' => $titles)) . $body;
113 }
114
115 // Complete newsletter body
116 return $body;
117 }