Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
data_source_rss.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/../include/asset.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/data_source/data_source/data_source.inc';
20 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
21 
34 {
35  var $reader;
36 
37 
44  function __construct($assetid=0)
45  {
46  parent::__construct($assetid);
47 
48  }//end constructor
49 
50 
58  public function _getAllowedLinks()
59  {
60  // any link is allowed
61  $allowed_link['asset']['card'] = 'M';
62  $allowed_link['asset']['exclusive'] = FALSE;
63 
64  $links[SQ_LINK_TYPE_1] = $allowed_link;
65  $links[SQ_LINK_TYPE_2] = $allowed_link;
66  $links[SQ_LINK_TYPE_3] = $allowed_link;
67  $links[SQ_LINK_NOTICE] = $allowed_link;
68 
69  return $links;
70 
71  }//end _getAllowedLinks()
72 
73 
80  function getResultSet()
81  {
82  $GLOBALS['SQ_SYSTEM']->pm->startTimer($this);
83  // use feed url as the cache key
84  $url = $this->attr('url');
85 
86  $result = Array();
87  $result = parent::getResultSet($url);
88  if (empty($result)) {
89  if (!empty($url)) {
90  if (isset($this->_tmp[$url])) {
91  $result = $this->_tmp[$url];
92  }
93  if (empty($result)) $result = $this->getItems();
94  if (!empty($result)) {
95  parent::setResultSet($result, $url);
96  $this->_tmp[$url] = $result;
97  }
98  }
99  }
100  $GLOBALS['SQ_SYSTEM']->pm->stopTimer($this);
101  return $result;
102 
103  }//end getResultSet()
104 
105 
112  function getItems()
113  {
114  $new_result = Array();
115  $result = $this->parseXML();
116  if (!$result) return Array();
117 
118  $feed_reader = $this->reader;
119  $result = $feed_reader->getItems();
120  $type = $feed_reader->getRssType();
121  foreach ($result as $index => $info) {
122  foreach ($info as $tag => $val_array) {
123  // for atom 1.30 type of rss feeds we need to add default keywords of
124  // title, description and link
125  if ($type == 'atom_1.0') {
126  if (isset($val_array['value'])) {
127  switch ($tag) {
128  case 'content':
129  $new_result[$index]['description'] = $val_array['value'];
130  break;
131  case 'heading':
132  $new_result[$index]['title'] = $val_array['value'];
133  break;
134  }
135  }
136  }
137  if ($tag != 'link') {
138  if (isset($val_array['value'])) {
139  $new_result[$index][$tag] = $val_array['value'];
140  }
141  } else {
142  if ($type == 'atom_1.0') {
143  $new_result[$index][$tag] = $val_array['_attributes']['HREF'];
144  }
145  if (isset($val_array['_attributes'])) {
146  $attributes = $val_array['_attributes'];
147  foreach ($attributes as $attr => $value) {
148  $new_result[$index][$tag.'_'.strtolower($attr)] = $value;
149  }
150  } else {
151  $new_result[$index][$tag] = $val_array['value'];
152  }
153  }
154  }//end foreach
155 
156  // timestamp data source record set attribute
157  $date_f = substr($this->attr('date_field'), strlen('ds__'));
158  if (!empty($date_f)) {
159  if (isset($info[$date_f])) {
160  $date_string = $info[$date_f]['value'];
161  if ($type == 'rss_2.0') {
162  // rss 2.0 uses GMT date format
163  $timestamp = strtotime($date_string);
164  } else {
165  // atom 1.0 uses the date construct
166  $timestamp = iso8601_ts($date_string);
167  }
168  if ($timestamp) {
169  $new_result[$index]['timestamp'] = $timestamp;
170  }
171  }
172  }
173  }//end foreach
174 
175  return $new_result;
176 
177  }//end getItems()
178 
179 
195  {
196  $keywords = parent::getAvailableKeywords();
197  $keywords['data_source_rss_channelInfo'] = translate('rss_channel_info');
198  $keywords['data_source_rss_imageInfo'] = translate('rss_image_info');
199  $keywords['data_source_rss_rss_type'] = translate('rss_type');
200  $keywords['data_source_rss_textinput'] = translate('rss_text_input');
201  $keywords['data_source_rss_items'] = translate('rss_items');
202  return $keywords;
203 
204  }//end getAvailableKeywords()
205 
206 
213  function parseXML()
214  {
215  $url = $this->attr('url');
216  require_once SQ_FUDGE_PATH.'/rss_feeds/rss_feed_reader.inc';
217  $feed_reader = new RSS_Feed_Reader();
218  $this->reader = $feed_reader;
219  if (!empty($url)) {
220  if ($feed_reader->setInputFile($url)) {
221  $result = $feed_reader->parse();
222  return $result;
223  } else {
224  return FALSE;
225  }
226  } else {
227  return FALSE;
228  }
229 
230  }//end parseXML()
231 
232 
241  function getKeywordReplacement($keyword)
242  {
243  $replacement = NULL;
244  if (strpos( $keyword, 'data_source_rss') === FALSE) {
245  $replacement = parent::getKeywordReplacement($keyword);
246  } else {
247  $keyword = substr($keyword, strlen('data_source_rss_'));
248  if (!empty($keyword)) {
249  $res = $this->parseXML();
250  $reader = $this->reader;
251  $function = 'get'.ucwords($keyword);
252  $result = $reader->{$function}();
253  }
254  if (is_array($result)) {
255  foreach ($result as $index => $info) {
256  foreach ($info as $tag => $val_array) {
257  if ($tag != 'link') {
258  if (isset($val_array['value'])) {
259  $replacement[$index][$tag] = $val_array['value'];
260  }
261  } else {
262  if (isset($val_array['_attributes'])) {
263  $attributes = $val_array['_attributes'];
264  foreach ($attributes as $attr => $value) {
265  $replacement[$index][$tag.'_'.strtolower($attr)] = $value;
266  }
267  } else {
268  $replacement[$index][$tag] = $val_array['value'];
269  }
270  }
271  }
272  }
273  ob_start();
274  ?>
275  <table>
276  <?php
277  foreach ($replacement as $replace) {
278  foreach ($replace as $key => $value) {
279  ?>
280  <tr>
281  <td><?php echo $key;?></td>
282  <td><?php echo $value;?></td>
283  </tr>
284  <?php
285  }
286  }
287  ?>
288  </table>
289  <?php
290  $replacement = ob_get_contents();
291  ob_end_clean();
292  } else {
293  $replacement = $result;
294  }
295  }//end else
296 
297  return $replacement;
298 
299  }//end getKeywordReplacement()
300 
301 
302 }//end class
303 
304 ?>