views_rss_core.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Preprocess functions for Views RSS: Core Elements module.
  5. */
  6. /**
  7. * Preprocess function for channel <title> element.
  8. */
  9. function views_rss_core_preprocess_channel_title(&$variables) {
  10. $view_title = $variables['view']->get_title();
  11. $variables['elements'][0]['value'] = ($view_title) ? $view_title : variable_get('site_name', t('Drupal'));
  12. }
  13. /**
  14. * Preprocess function for channel <description> element.
  15. */
  16. function views_rss_core_preprocess_channel_description(&$variables) {
  17. if (empty($variables['elements'][0]['value'])) {
  18. $variables['elements'][0]['value'] = variable_get('site_slogan', '');
  19. }
  20. }
  21. /**
  22. * Preprocess function for channel <link> element.
  23. */
  24. function views_rss_core_preprocess_channel_link(&$variables) {
  25. $variables['elements'][0]['value'] = url('<front>', array('absolute' => TRUE));
  26. }
  27. /**
  28. * Preprocess function for channel <atom:link> element.
  29. */
  30. function views_rss_core_preprocess_channel_atom_link(&$variables) {
  31. $url_options = array('absolute' => TRUE);
  32. $input = $variables['view']->get_exposed_input();
  33. if ($input) {
  34. $url_options['query'] = $input;
  35. }
  36. $url = url($variables['view']->get_url(), $url_options);
  37. $variables['elements'][0]['attributes'] = array(
  38. 'rel' => 'self',
  39. 'href' => $url,
  40. );
  41. }
  42. /**
  43. * Preprocess function for channel <language> element.
  44. */
  45. function views_rss_core_preprocess_channel_language(&$variables) {
  46. global $language;
  47. if (empty($variables['elements'][0]['value'])) {
  48. $variables['elements'][0]['value'] = $language->language;
  49. }
  50. }
  51. /**
  52. * Preprocess function for channel <category> element.
  53. */
  54. function views_rss_core_preprocess_channel_category(&$variables) {
  55. // No value = no preprocessing.
  56. if (empty($variables['elements'][0]['value'])) {
  57. return;
  58. }
  59. $elements = array();
  60. $categories = explode(',', $variables['elements'][0]['value']);
  61. foreach ($categories as $category) {
  62. $elements[] = array(
  63. 'key' => 'category',
  64. 'value' => trim($category),
  65. );
  66. }
  67. $variables['elements'] = $elements;
  68. }
  69. /**
  70. * Preprocess function for channel <image> element.
  71. */
  72. function views_rss_core_preprocess_channel_image(&$variables) {
  73. // No value = no preprocessing.
  74. if (empty($variables['elements'][0]['value'])) {
  75. return;
  76. }
  77. $path = $variables['elements'][0]['value'];
  78. // Get value of channel <title> element from its preprocess function.
  79. views_rss_core_preprocess_channel_title($variables);
  80. $title = $variables['elements'][0]['value'];
  81. // Get value of channel <title> element from its preprocess function.
  82. views_rss_core_preprocess_channel_link($variables);
  83. $link = $variables['elements'][0]['value'];
  84. // Create subelements array.
  85. $variables['elements'][0]['value'] = array(
  86. 'url' => file_create_url($path),
  87. 'title' => $title,
  88. 'link' => $link,
  89. );
  90. // Try to get image description from website's mission.
  91. $site_slogan = variable_get('site_slogan', '');
  92. if (!empty($site_slogan)) {
  93. $variables['elements'][0]['value']['description'] = $site_slogan;
  94. }
  95. // Get image width and height.
  96. $image = image_load($path);
  97. if (!empty($image)) {
  98. $variables['elements'][0]['value']['width'] = $image->info['width'];
  99. $variables['elements'][0]['value']['height'] = $image->info['height'];
  100. }
  101. }
  102. /**
  103. * Preprocess function for channel <pubDate> and <lastBuildDate> elements.
  104. *
  105. * It will return values for date element providing that original Views query
  106. * was modified appropriately by views_rss_core_views_query_alter() by adding
  107. * new fields to SELECT clause retrieving object creation (for <pubDate>)
  108. * or modification timestamp (for <lastBuildDate>).
  109. */
  110. function views_rss_core_preprocess_channel_date(&$variables) {
  111. if (count($variables['view']->result) > 0) {
  112. $max_date = 0;
  113. foreach ($variables['view']->result as $row) {
  114. $key = $variables['elements'][0]['key'];
  115. if (isset($row->$key) && $row->$key > $max_date) {
  116. $max_date = $row->$key;
  117. }
  118. }
  119. if ($max_date) {
  120. $variables['elements'][0]['value'] = date('r', $max_date);
  121. }
  122. }
  123. }
  124. /**
  125. * Preprocess function for channel <skipHours> and <skipDays> elements.
  126. */
  127. function views_rss_core_preprocess_channel_skip(&$variables) {
  128. // No value = no preprocessing.
  129. if (empty($variables['elements'][0]['value'])) {
  130. return;
  131. }
  132. $elements = array();
  133. $skips = strip_tags($variables['elements'][0]['value']);
  134. if (!empty($skips)) {
  135. foreach (explode(',', $skips) as $skip_value) {
  136. $elements[] = array(
  137. 'key' => ($variables['elements'][0]['key'] == 'skipHours') ? 'hour' : 'day',
  138. 'value' => trim($skip_value),
  139. );
  140. }
  141. }
  142. $variables['elements'][0]['value'] = $elements;
  143. }
  144. /**
  145. * Preprocess function for channel <cloud> element.
  146. */
  147. function views_rss_core_preprocess_channel_cloud(&$variables) {
  148. // No value = no preprocessing.
  149. if (empty($variables['elements'][0]['value'])) {
  150. return;
  151. }
  152. if ($url = parse_url($variables['elements'][0]['value'])) {
  153. $variables['elements'][0]['value'] = NULL;
  154. $variables['elements'][0]['attributes'] = array(
  155. 'domain' => $url['host'],
  156. 'port' => $url['port'],
  157. 'path' => $url['path'],
  158. 'registerProcedure' => $url['fragment'],
  159. 'protocol' => $url['scheme'],
  160. );
  161. }
  162. }
  163. /**
  164. * Preprocess function for item <guid> element.
  165. */
  166. function views_rss_core_preprocess_item_guid(&$variables) {
  167. // No value = no preprocessing.
  168. if (empty($variables['elements'][0]['value'])) {
  169. return;
  170. }
  171. $is_permalink = 'false';
  172. if (
  173. (!isset($variables['item']['views_rss_core']['link']) || empty($variables['item']['views_rss_core']['link']))
  174. && valid_url($variables['elements'][0]['value'], TRUE)
  175. ) {
  176. $is_permalink = 'true';
  177. }
  178. $variables['elements'][0]['attributes']['isPermaLink'] = $is_permalink;
  179. }
  180. /**
  181. * Preprocess function for item <source> element.
  182. */
  183. function views_rss_core_preprocess_item_source(&$variables) {
  184. $url_options = array('absolute' => TRUE);
  185. $input = $variables['view']->get_exposed_input();
  186. if ($input) {
  187. $url_options['query'] = $input;
  188. }
  189. $url = url($variables['view']->get_url(), $url_options);
  190. $view_title = $variables['view']->get_title();
  191. $variables['elements'][0]['value'] = (!empty($view_title)) ? $view_title : variable_get('site_name', t('Drupal'));
  192. $variables['elements'][0]['attributes'] = array('url' => $url);
  193. }