Xataface 2.0
Xataface Application Framework
Dataface/FeedTool.php
Go to the documentation of this file.
00001 <?php
00009 class Dataface_FeedTool {
00010 
00011 
00012         function buildFeedItemData(&$record){
00013                 $delegate =& $record->_table->getDelegate();
00014                 if ( isset($delegate) and method_exists($delegate, 'getFeedItem') ){
00015                         $res = $delegate->getFeedItem($record);
00016                 } else {
00017                         $res = array();
00018                 }
00019                 if ( !isset($res['title']) ) $res['title'] = $this->getItemTitle($record);
00020                 if ( !isset($res['description']) ) $res['description'] = $this->getItemDescription($record);
00021                 if ( !isset($res['link']) ) $res['link'] = $this->getItemLink($record);
00022                 if ( !isset($res['date']) ) $res['date'] = $this->getItemDate($record);
00023                 if ( !isset($res['author']) ) $res['author'] = $this->getItemAuthor($record);
00024                 if ( !isset($res['source']) ) $res['source'] = $this->getItemSource($record);
00025                 
00026                 return $res;
00027                 
00028         }
00029         
00030         function buildFeedData($query=null){
00031                 $app =& Dataface_Application::getInstance();
00032                 
00033                 $appDelegate =& $app->getDelegate();
00034                 if ( !isset($query) ){
00035 
00036                         $query = $app->getQuery();
00037                 }
00038                 $table =& Dataface_Table::loadTable($query['-table']);
00039                 $delegate =& $table->getDelegate();
00040                 
00041                 if ( isset($query['-relationship']) ){
00042                         // we are building a set of related records.
00043                         $record =& $app->getRecord();
00044                         if ( isset($delegate) and method_exists($delegate,'getRelatedFeed') ){
00045                                 $feed = $delegate->getRelatedFeed($record, $query['-relationship']);
00046                         } else if ( isset($appDelegate) and method_exists($appDelegate, 'getRelatedFeed') ){
00047                                 $feed = $appDelegate->getRelatedFeed($record, $query['-relationship']);
00048                         } else {
00049                                 $feed = array();
00050                         }
00051                         
00052                         if ( !isset($feed['title']) ) $feed['title'] =$this->getRelatedTitle($record, $query['-relationship']);
00053                         if ( !isset($feed['description']) )  $feed['description'] = $this->getRelatedDescription($record, $query['-relationship']);
00054                         if ( !isset($feed['link']) ) $feed['link'] = $this->getRelatedLink($record, $query['-relationship']);
00055                         if ( !isset($feed['syndicationURL']) ) $feed['syndicationURL'] = $this->getRelatedSyndicationURL($record, $query['-relationship']);
00056                         
00057                         return $feed;
00058                         
00059                 } else {
00060                         
00061                         if ( isset($delegate) and method_exists($delegate, 'getFeed') ){
00062                                 $feed = $delegate->getFeed($query);
00063                         } else if ( isset($appDelegate) and method_exists($appDelegate,'getFeed') ){
00064                                 $feed = $appDelegate->getFeed($query);
00065                         } else {
00066                                 $feed = array();
00067                         }
00068                         
00069                         if ( !isset($feed['title']) ) $feed['title'] = $this->getTitle($query);
00070                         if ( !isset($feed['description']) ) $feed['description'] = $this->getDescription($query);
00071                         if ( !isset($feed['link']) ) $feed['link'] = $this->getLink($query);
00072                         if ( !isset($feed['syndicationURL']) ) $feed['syndicationURL'] = $this->getSyndicationURL($query);
00073                         return $feed;
00074                         
00075                 
00076                 }
00077         
00078         }
00079         
00080         function getParsedConfigSetting($name, $context=array()){
00081                 $app =& Dataface_Application::getInstance();
00082                 $conf =& $this->getConfig();
00083                 if ( isset($conf[$name]) ) {
00084                         return $app->parseString($conf[$name], $context);
00085                 } else {
00086                         return null;
00087                 }
00088         }
00089         
00090         function getTitle($query){
00091                 $title = $this->getParsedConfigSetting('title', $query);
00092                 
00093                 if ( !$title ){
00094                         $table =& Dataface_Table::loadTable($query['-table']);
00095                         $searchparams = preg_grep('/^-/', array_keys($query), PREG_GREP_INVERT);
00096                         if ( count($searchparams) > 0 ){
00097                                 $temp = array();
00098                                 foreach ($searchparams as $key){
00099                                         $parts = explode('/', $key);
00100                                         if ( count($parts) > 1 and $table->hasRelationship($parts[0]) ){
00101                                                 $temp[] = $parts[0].'/'.$parts[1].': '.$query[$key];
00102                                                 continue;
00103                                         }
00104                                         if ( !$table->hasField($key) ) continue;
00105                                         $temp[] = $key.': '.$query[$key];
00106                                 }
00107                                 if ( count($temp) > 0 ){
00108                                         $search_params = '['.implode(', ', $temp).']';
00109                                 } else {
00110                                         $search_params = '';
00111                                 }
00112                         } else {
00113                                 $search_params = '';
00114                         }
00115                         
00116                         if ( @$query['-search'] ){
00117                                 $search_params = ' search for "'.$query['-search'].'" '.$search_params;
00118                         }
00119                         
00120                         $app =& Dataface_Application::getInstance();
00121                         
00122                         
00123                         $title= $app->getSiteTitle().' | '.$table->getLabel().' '.$search_params;
00124                 }
00125                 return $title;
00126                 
00127         }
00128         function getRelatedTitle($record, $relationshipName){
00129                 return ucwords($relationshipName)." of ".$record->getTitle();
00130         
00131         }
00132         function getDescription($query){
00133                 $description = $this->getParsedConfigSetting('description', $query);
00134                 if ( !$description ){
00135                         $description = "Feed Description";
00136                 }
00137         }
00138         function getRelatedDescription($record, $relationshipName){
00139                 return "Related records for ".$record->getTitle();
00140         }
00141         function getLink($query){
00142                 $link = $this->getParsedConfigSetting('link', $query);
00143                 if ( !$link ){
00144                         $app =& Dataface_Application::getInstance();
00145                         if ( $query['-action'] == 'feed' ) $query['-action'] = 'list';
00146                         $link = $app->url($query);
00147                 }
00148                 return $link;
00149         }
00150         function getRelatedLink($record, $relationshipName){
00151                 return $record->getURL('-relationship='.urlencode($relationshipName).'&-action=related_records_list');
00152         }
00153         function getSyndicationURL($query){
00154                 $url = $this->getParsedConfigSetting('syndicationURL', $query);
00155                 if ( !$url ) $url = $this->getParsedConfigSetting('link', $query);
00156                 return $url;
00157         }
00158         function getRelatedSyndicationURL($record, $relationshipName){
00159                 return $record->getURL('-relationship='.urlencode($relationshipName).'&-action=related_records_list');
00160         }
00161         
00162         function getItemLink(&$record){
00163                 return $record->getPublicLink();
00164         }
00165         
00166         function getItemDescription(&$record){
00167                 $delegate =& $record->_table->getDelegate();
00168                 if ( isset($delegate) and method_exists($delegate, 'getRSSDescription') ){
00169                         return $delegate->getRSSDescription($record);
00170                 } else {
00171                         $out = '<table><thead><tr><th>Field</th><th>Value</th></tr></thead>';
00172                         $out .= '<tbody>';
00173                         foreach ( $record->_table->fields() as $field){
00174                                 if ( !$record->checkPermission('view') ) continue;
00175                                 if ( @$field['visibility']['feed'] == 'hidden' ) continue;
00176                                 if ( $disp = @$record->val($field['name']) ){
00177                                         $out .= '<tr><td valign="top">'.htmlspecialchars($field['widget']['label']).'</td>';
00178                                         $out .= '<td valign="top">'.@$record->htmlValue($field['name']).'</td></tr>';
00179                                 }
00180                         }
00181                         $out .= '</tbody></table>';
00182                         return $out;
00183                 
00184                 }
00185                 
00186                 //return $record->getDescription();
00187         }
00188         
00189         function getItemTitle(&$record){
00190                 return $record->getTitle();
00191         }
00192         
00193         function getItemDate(&$record){
00194                 $mod = $record->getLastModified();
00195                 if ( !$mod ){
00196                         $mod = $record->getCreated();
00197                 }
00198                 return $mod;
00199         }
00200         
00201         function getItemAuthor(&$record){
00202                 $creator = $record->getCreator();
00203                 if ( !$creator ){
00204                         $creator = $this->getParsedConfigSetting('default_author');
00205                 }
00206                 if ( !$creator ) $creator = "Site administrator";
00207                 return $creator;
00208         }
00209         
00210         function getItemSource(&$record){
00211                 $delegate =& $record->_table->getDelegate();
00212                 if ( isset($delegate) && method_exists($delegate, 'getFeedSource') ){
00213                         return $delegate->getFeedSource($record);
00214                 }
00215                 $conf =& $this->getConfig();
00216                 if ( isset($conf['source']) ){
00217                         $app =& Dataface_Application::getInstance();
00218                         return $app->parseString($conf['source'], $record);
00219                 }
00220                 return $_SERVER['HOST_URI'].DATAFACE_SITE_HREF;
00221         }
00222         
00223         function createFeedItem(&$record){
00224                 $data = $this->buildFeedItemData($record);
00225                 $item = new FeedItem(); 
00226                 $item->title = $data['title'];
00227                 $item->link = $data['link'];
00228                 $item->description = $data['description']; 
00229                 
00230                 //optional
00231                 //item->descriptionTruncSize = 500;
00232                 //item->descriptionHtmlSyndicated = true;
00233                 
00234                 $item->date = $data['date']; 
00235 
00236                 $item->source = $data['source']; 
00237                 $item->author = $data['author'];
00238                 return $item;
00239         }
00240         
00241         
00242         function &getConfig(){
00243                 $app =& Dataface_Application::getInstance();
00244                 if ( !isset($app->_conf['_feed']) ){
00245                         $app->_conf['_feed'] = array();
00246                 }
00247                 return $app->_conf['_feed'];
00248         }
00249         
00250         function createFeed($query=null){
00251                 import('feedcreator.class.php');
00252                 $app =& Dataface_Application::getInstance();
00253                 if ( !isset($query) ){
00254                         $query = $app->getQuery();
00255                 }
00256                 $feed_data = $this->buildFeedData($query);
00257                 
00258                 $rss = new UniversalFeedCreator(); 
00259                 $rss->encoding = $app->_conf['oe'];
00260                 //$rss->useCached(); // use cached version if age<1 hour
00261                 $rss->title = $feed_data['title']; 
00262                 $rss->description = $feed_data['description'];
00263                 
00264                 //optional
00265                 //$rss->descriptionTruncSize = 500;
00266                 //$rss->descriptionHtmlSyndicated = true;
00267                 
00268                 $rss->link = htmlentities($feed_data['link']);
00269                 $rss->syndicationURL = htmlentities($feed_data['syndicationURL']);
00270                 
00271                 if ( isset($query['-relationship']) ){
00272                         // Do the related records thing
00273                         $record =& $app->getRecord();
00274                         $query['-related:start'] = 0;
00275                         $rrecords =& df_get_related_records(array_merge($query, array('-related:limit'=>1))); //$record->getRelatedRecordObjects($query['-relationship'], 0,1);
00276                         if ( count($rrecords) > 0 ){
00277                                 $testRec =& $rrecords[0]->toRecord();
00278                                 $lastUpdatedColumn = $testRec->_table->getLastUpdatedField();
00279                                 if ( $lastUpdatedColumn ){
00280                                         unset($rrecords);
00281                                         $query['-related:limit'] = 30;
00282                                         $query['-related:sort'] = $lastUpdatedColumn.' desc';
00283                                         $rrecords =& df_get_related_records($query);
00284                                         //$record->getRelatedRecordObjects($query['-relationship'], null,null, 0, $lastUpdatedColumn.' desc');
00285                                         
00286                                 } else {
00287                                         unset($rrecords);
00288                                         $query['-related:limit'] = 30;
00289                                         $rrecords =& df_get_related_records($query); //$record->getRelatedRecordObjects($query['-relationship']);
00290                                         
00291                                 }
00292                                 $records = array();
00293                                 foreach ($rrecords as $rrec){
00294                                         
00295                                         $dfRecord =& $rrec->toRecord();
00296                                         if ( $dfRecord->checkPermission('view', array('recordmask'=>array('view'=>1)  ) ) ){
00297                                                 $records[] =& $dfRecord;
00298                                         }
00299                                         unset($dfRecord);
00300                                         unset($rrec);
00301                                 }
00302                         } else {
00303                                 $records = array();
00304                         }
00305                         //trigger_error("Not implemented yet for related records", E_USER_ERROR);
00306                 } else {
00307                         $records =& df_get_records_array($query['-table'], $query);
00308                         
00309                 }
00310                 
00311                 
00312                 foreach ($records as $record){
00313                         if ( !$record->checkPermission('view') ) continue;
00314                         if ( !$record->checkPermission('view in rss') ) continue;
00315                         $item = $this->createFeedItem($record);
00316                         $del =& $record->_table->getDelegate();
00317                         if ( isset($del) and method_exists($del, 'canAddToFeed') and !$del->canAddToFeed($record, $rss) ){
00318                                 unset($del);
00319                                 continue;
00320                         }
00321                         unset($del);
00322                         $rss->addItem($item);
00323                         unset($item);
00324                 }
00325                 
00326                 return $rss;
00327 
00328         }
00329         
00330         function getFeedXML($query, $format='RSS2.0'){
00331                 $feed = $this->createFeed($query);
00332                 return $feed->createFeed($format);
00333         }       
00334         
00335         
00336 
00337 }
 All Data Structures Namespaces Files Functions Variables Enumerations