Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
FeedTool.php
Go to the documentation of this file.
1 <?php
10 
11 
12  function buildFeedItemData(&$record){
13  $delegate =& $record->_table->getDelegate();
14  if ( isset($delegate) and method_exists($delegate, 'getFeedItem') ){
15  $res = $delegate->getFeedItem($record);
16  } else {
17  $res = array();
18  }
19  if ( !isset($res['title']) ) $res['title'] = $this->getItemTitle($record);
20  if ( !isset($res['description']) ) $res['description'] = $this->getItemDescription($record);
21  if ( !isset($res['link']) ) $res['link'] = $this->getItemLink($record);
22  if ( !isset($res['date']) ) $res['date'] = $this->getItemDate($record);
23  if ( !isset($res['author']) ) $res['author'] = $this->getItemAuthor($record);
24  if ( !isset($res['source']) ) $res['source'] = $this->getItemSource($record);
25 
26  return $res;
27 
28  }
29 
30  function buildFeedData($query=null){
32 
33  $appDelegate =& $app->getDelegate();
34  if ( !isset($query) ){
35 
36  $query = $app->getQuery();
37  }
38  $table =& Dataface_Table::loadTable($query['-table']);
39  $delegate =& $table->getDelegate();
40 
41  if ( isset($query['-relationship']) ){
42  // we are building a set of related records.
43  $record =& $app->getRecord();
44  if ( isset($delegate) and method_exists($delegate,'getRelatedFeed') ){
45  $feed = $delegate->getRelatedFeed($record, $query['-relationship']);
46  } else if ( isset($appDelegate) and method_exists($appDelegate, 'getRelatedFeed') ){
47  $feed = $appDelegate->getRelatedFeed($record, $query['-relationship']);
48  } else {
49  $feed = array();
50  }
51 
52  if ( !isset($feed['title']) ) $feed['title'] =$this->getRelatedTitle($record, $query['-relationship']);
53  if ( !isset($feed['description']) ) $feed['description'] = $this->getRelatedDescription($record, $query['-relationship']);
54  if ( !isset($feed['link']) ) $feed['link'] = $this->getRelatedLink($record, $query['-relationship']);
55  if ( !isset($feed['syndicationURL']) ) $feed['syndicationURL'] = $this->getRelatedSyndicationURL($record, $query['-relationship']);
56 
57  return $feed;
58 
59  } else {
60 
61  if ( isset($delegate) and method_exists($delegate, 'getFeed') ){
62  $feed = $delegate->getFeed($query);
63  } else if ( isset($appDelegate) and method_exists($appDelegate,'getFeed') ){
64  $feed = $appDelegate->getFeed($query);
65  } else {
66  $feed = array();
67  }
68 
69  if ( !isset($feed['title']) ) $feed['title'] = $this->getTitle($query);
70  if ( !isset($feed['description']) ) $feed['description'] = $this->getDescription($query);
71  if ( !isset($feed['link']) ) $feed['link'] = $this->getLink($query);
72  if ( !isset($feed['syndicationURL']) ) $feed['syndicationURL'] = $this->getSyndicationURL($query);
73  return $feed;
74 
75 
76  }
77 
78  }
79 
80  function getParsedConfigSetting($name, $context=array()){
82  $conf =& $this->getConfig();
83  if ( isset($conf[$name]) ) {
84  return $app->parseString($conf[$name], $context);
85  } else {
86  return null;
87  }
88  }
89 
90  function getTitle($query){
91  $title = $this->getParsedConfigSetting('title', $query);
92 
93  if ( !$title ){
94  $table =& Dataface_Table::loadTable($query['-table']);
95  $searchparams = preg_grep('/^-/', array_keys($query), PREG_GREP_INVERT);
96  if ( count($searchparams) > 0 ){
97  $temp = array();
98  foreach ($searchparams as $key){
99  $parts = explode('/', $key);
100  if ( count($parts) > 1 and $table->hasRelationship($parts[0]) ){
101  $temp[] = $parts[0].'/'.$parts[1].': '.$query[$key];
102  continue;
103  }
104  if ( !$table->hasField($key) ) continue;
105  $temp[] = $key.': '.$query[$key];
106  }
107  if ( count($temp) > 0 ){
108  $search_params = '['.implode(', ', $temp).']';
109  } else {
110  $search_params = '';
111  }
112  } else {
113  $search_params = '';
114  }
115 
116  if ( @$query['-search'] ){
117  $search_params = ' search for "'.$query['-search'].'" '.$search_params;
118  }
119 
121 
122 
123  $title= $app->getSiteTitle().' | '.$table->getLabel().' '.$search_params;
124  }
125  return $title;
126 
127  }
128  function getRelatedTitle($record, $relationshipName){
129  return ucwords($relationshipName)." of ".$record->getTitle();
130 
131  }
132  function getDescription($query){
133  $description = $this->getParsedConfigSetting('description', $query);
134  if ( !$description ){
135  $description = "Feed Description";
136  }
137  }
138  function getRelatedDescription($record, $relationshipName){
139  return "Related records for ".$record->getTitle();
140  }
141  function getLink($query){
142  $link = $this->getParsedConfigSetting('link', $query);
143  if ( !$link ){
145  if ( $query['-action'] == 'feed' ) $query['-action'] = 'list';
146  $link = $app->url($query);
147  }
148  return $link;
149  }
150  function getRelatedLink($record, $relationshipName){
151  return $record->getURL('-relationship='.urlencode($relationshipName).'&-action=related_records_list');
152  }
153  function getSyndicationURL($query){
154  $url = $this->getParsedConfigSetting('syndicationURL', $query);
155  if ( !$url ) $url = $this->getParsedConfigSetting('link', $query);
156  return $url;
157  }
158  function getRelatedSyndicationURL($record, $relationshipName){
159  return $record->getURL('-relationship='.urlencode($relationshipName).'&-action=related_records_list');
160  }
161 
162  function getItemLink(&$record){
163  return $record->getPublicLink();
164  }
165 
166  function getItemDescription(&$record){
167  $delegate =& $record->_table->getDelegate();
168  if ( isset($delegate) and method_exists($delegate, 'getRSSDescription') ){
169  return $delegate->getRSSDescription($record);
170  } else {
171  $out = '<table><thead><tr><th>Field</th><th>Value</th></tr></thead>';
172  $out .= '<tbody>';
173  foreach ( $record->_table->fields() as $field){
174  if ( !$record->checkPermission('view') ) continue;
175  if ( @$field['visibility']['feed'] == 'hidden' ) continue;
176  if ( $disp = @$record->val($field['name']) ){
177  $out .= '<tr><td valign="top">'.htmlspecialchars($field['widget']['label']).'</td>';
178  $out .= '<td valign="top">'.@$record->htmlValue($field['name']).'</td></tr>';
179  }
180  }
181  $out .= '</tbody></table>';
182  return $out;
183 
184  }
185 
186  //return $record->getDescription();
187  }
188 
189  function getItemTitle(&$record){
190  return $record->getTitle();
191  }
192 
193  function getItemDate(&$record){
194  $mod = $record->getLastModified();
195  if ( !$mod ){
196  $mod = $record->getCreated();
197  }
198  return $mod;
199  }
200 
201  function getItemAuthor(&$record){
202  $creator = $record->getCreator();
203  if ( !$creator ){
204  $creator = $this->getParsedConfigSetting('default_author');
205  }
206  if ( !$creator ) $creator = "Site administrator";
207  return $creator;
208  }
209 
210  function getItemSource(&$record){
211  $delegate =& $record->_table->getDelegate();
212  if ( isset($delegate) && method_exists($delegate, 'getFeedSource') ){
213  return $delegate->getFeedSource($record);
214  }
215  $conf =& $this->getConfig();
216  if ( isset($conf['source']) ){
218  return $app->parseString($conf['source'], $record);
219  }
220  return $_SERVER['HOST_URI'].DATAFACE_SITE_HREF;
221  }
222 
223  function createFeedItem(&$record){
224  $data = $this->buildFeedItemData($record);
225  $item = new FeedItem();
226  $item->title = $data['title'];
227  $item->link = $data['link'];
228  $item->description = $data['description'];
229 
230  //optional
231  //item->descriptionTruncSize = 500;
232  //item->descriptionHtmlSyndicated = true;
233 
234  $item->date = $data['date'];
235 
236  $item->source = $data['source'];
237  $item->author = $data['author'];
238  return $item;
239  }
240 
241 
242  function &getConfig(){
244  if ( !isset($app->_conf['_feed']) ){
245  $app->_conf['_feed'] = array();
246  }
247  return $app->_conf['_feed'];
248  }
249 
250  function createFeed($query=null){
251  import('feedcreator.class.php');
253  if ( !isset($query) ){
254  $query = $app->getQuery();
255  }
256  $feed_data = $this->buildFeedData($query);
257 
258  $rss = new UniversalFeedCreator();
259  $rss->encoding = $app->_conf['oe'];
260  //$rss->useCached(); // use cached version if age<1 hour
261  $rss->title = $feed_data['title'];
262  $rss->description = $feed_data['description'];
263 
264  //optional
265  //$rss->descriptionTruncSize = 500;
266  //$rss->descriptionHtmlSyndicated = true;
267 
268  $rss->link = htmlentities($feed_data['link']);
269  $rss->syndicationURL = htmlentities($feed_data['syndicationURL']);
270 
271  if ( isset($query['-relationship']) ){
272  // Do the related records thing
273  $record =& $app->getRecord();
274  $query['-related:start'] = 0;
275  $rrecords =& df_get_related_records(array_merge($query, array('-related:limit'=>1))); //$record->getRelatedRecordObjects($query['-relationship'], 0,1);
276  if ( count($rrecords) > 0 ){
277  $testRec =& $rrecords[0]->toRecord();
278  $lastUpdatedColumn = $testRec->_table->getLastUpdatedField();
279  if ( $lastUpdatedColumn ){
280  unset($rrecords);
281  $query['-related:limit'] = 30;
282  $query['-related:sort'] = $lastUpdatedColumn.' desc';
283  $rrecords =& df_get_related_records($query);
284  //$record->getRelatedRecordObjects($query['-relationship'], null,null, 0, $lastUpdatedColumn.' desc');
285 
286  } else {
287  unset($rrecords);
288  $query['-related:limit'] = 30;
289  $rrecords =& df_get_related_records($query); //$record->getRelatedRecordObjects($query['-relationship']);
290 
291  }
292  $records = array();
293  foreach ($rrecords as $rrec){
294 
295  $dfRecord =& $rrec->toRecord();
296  if ( $dfRecord->checkPermission('view', array('recordmask'=>array('view'=>1) ) ) ){
297  $records[] =& $dfRecord;
298  }
299  unset($dfRecord);
300  unset($rrec);
301  }
302  } else {
303  $records = array();
304  }
305  //trigger_error("Not implemented yet for related records", E_USER_ERROR);
306  } else {
307  $records =& df_get_records_array($query['-table'], $query);
308 
309  }
310 
311 
312  foreach ($records as $record){
313  if ( !$record->checkPermission('view') ) continue;
314  if ( !$record->checkPermission('view in rss') ) continue;
315  $item = $this->createFeedItem($record);
316  $del =& $record->_table->getDelegate();
317  if ( isset($del) and method_exists($del, 'canAddToFeed') and !$del->canAddToFeed($record, $rss) ){
318  unset($del);
319  continue;
320  }
321  unset($del);
322  $rss->addItem($item);
323  unset($item);
324  }
325 
326  return $rss;
327 
328  }
329 
330  function getFeedXML($query, $format='RSS2.0'){
331  $feed = $this->createFeed($query);
332  return $feed->createFeed($format);
333  }
334 
335 
336 
337 }