2007-08-23 16:01:01 +00:00
< ? php
/**
* @ file
2012-02-19 03:31:32 +00:00
* User page callbacks for tracker . module .
2007-08-23 16:01:01 +00:00
*/
/**
2012-02-19 03:31:32 +00:00
* Page callback : Generates a page of tracked nodes for the site .
*
* Queries the database for info , adds RDFa info if applicable , and generates
* the render array that will be used to render the page .
*
* @ return array
* A renderable array .
*
* @ see tracker_menu ()
2007-08-23 16:01:01 +00:00
*/
2007-11-06 08:51:23 +00:00
function tracker_page ( $account = NULL , $set_title = FALSE ) {
if ( $account ) {
2012-06-05 05:42:19 +00:00
$query = db_select ( 'tracker_user' , 't' )
2013-05-29 07:20:43 +00:00
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
-> addMetaData ( 'base_table' , 'tracker_user' )
-> condition ( 't.uid' , $account -> uid );
2009-08-31 06:52:50 +00:00
2007-11-06 08:51:23 +00:00
if ( $set_title ) {
// When viewed from user/%user/track, display the name of the user
// as page title -- the tab title remains Track so this needs to be done
2008-12-30 16:43:20 +00:00
// here and not in the menu definition.
2012-01-16 02:18:26 +00:00
drupal_set_title ( user_format_name ( $account ));
2007-11-06 08:51:23 +00:00
}
2007-08-23 16:01:01 +00:00
}
2009-08-31 06:52:50 +00:00
else {
2012-06-05 05:42:19 +00:00
$query = db_select ( 'tracker_node' , 't' , array ( 'target' => 'slave' ))
2013-05-29 07:20:43 +00:00
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
-> addMetaData ( 'base_table' , 'tracker_node' );
2009-08-31 06:52:50 +00:00
}
// This array acts as a placeholder for the data selected later
// while keeping the correct order.
2013-03-10 19:05:24 +00:00
$tracker_data = $query
2009-08-31 06:52:50 +00:00
-> addTag ( 'node_access' )
-> fields ( 't' , array ( 'nid' , 'changed' ))
-> condition ( 't.published' , 1 )
-> orderBy ( 't.changed' , 'DESC' )
-> limit ( 25 )
-> execute ()
-> fetchAllAssoc ( 'nid' );
2007-08-23 16:01:01 +00:00
2009-12-02 14:56:32 +00:00
$rows = array ();
2013-03-10 19:05:24 +00:00
if ( ! empty ( $tracker_data )) {
$nids = array_keys ( $tracker_data );
2013-05-26 20:18:10 +00:00
$nodes = node_load_multiple ( $nids );
2012-02-19 03:31:32 +00:00
// Now, get the data and put into the placeholder array.
2013-05-26 20:18:10 +00:00
// @todo This should be actually filtering on the desired language and just
// fall back to the default language.
$result = db_query ( 'SELECT n.nid, l.comment_count FROM {node_field_data} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.nid IN (:nids) AND n.default_langcode = 1 ORDER BY n.changed DESC' , array ( ':nids' => $nids ), array ( 'target' => 'slave' )) -> fetchAllKeyed ();
2013-03-10 19:05:24 +00:00
foreach ( $result as $nid => $comment_count ) {
$nodes [ $nid ] -> last_activity = $tracker_data [ $nid ] -> changed ;
$nodes [ $nid ] -> comment_count = $comment_count ;
2009-08-31 06:52:50 +00:00
}
2009-04-05 12:26:12 +00:00
2012-02-19 03:31:32 +00:00
// Display the data.
2009-08-31 06:52:50 +00:00
foreach ( $nodes as $node ) {
2012-02-19 03:31:32 +00:00
// Determine the number of comments.
2009-08-31 06:52:50 +00:00
$comments = 0 ;
if ( $node -> comment_count ) {
$comments = $node -> comment_count ;
2007-08-23 16:01:01 +00:00
2009-08-31 06:52:50 +00:00
if ( $new = comment_num_new ( $node -> nid )) {
$comments .= '<br />' ;
2012-02-19 03:31:32 +00:00
$comments .= l ( format_plural ( $new , '1 new' , '@count new' ), 'node/' . $node -> nid , array ( 'fragment' => 'new' ));
2009-08-31 06:52:50 +00:00
}
2007-08-23 16:01:01 +00:00
}
2013-06-09 17:03:07 +00:00
$mark_build = array (
'#theme' => 'mark' ,
2013-06-28 16:23:53 +00:00
'#status' => node_mark ( $node -> nid , $node -> changed ),
2013-06-09 17:03:07 +00:00
);
2010-01-14 06:23:40 +00:00
$row = array (
2012-09-19 09:54:05 +00:00
'type' => check_plain ( node_get_type_label ( $node )),
2012-07-22 18:07:00 +00:00
// Do not use $node->label(), because $node comes from the database.
2013-06-09 17:03:07 +00:00
'title' => array ( 'data' => l ( $node -> title , 'node/' . $node -> nid ) . ' ' . drupal_render ( $mark_build )),
2013-07-09 13:46:44 +00:00
'author' => array ( 'data' => array ( '#theme' => 'username' , '#account' => user_load ( $node -> uid ))),
2010-01-14 06:23:40 +00:00
'replies' => array ( 'class' => array ( 'replies' ), 'data' => $comments ),
'last updated' => array ( 'data' => t ( '!time ago' , array ( '!time' => format_interval ( REQUEST_TIME - $node -> last_activity )))),
2009-08-31 06:52:50 +00:00
);
2010-01-14 06:23:40 +00:00
// Adds extra RDFa markup to the $row array if the RDF module is enabled.
2013-06-24 22:21:37 +00:00
if ( module_exists ( 'rdf' )) {
$mapping = rdf_get_mapping ( 'node' , $node -> type );
2010-01-14 06:23:40 +00:00
// Adds RDFa markup to the title of the node. Because the RDFa markup is
// added to the td tag which might contain HTML code, we specify an
// empty datatype to ensure the value of the title read by the RDFa
// parsers is a plain literal.
2013-06-24 22:21:37 +00:00
$title_mapping = $mapping -> getPreparedFieldMapping ( 'title' );
$row [ 'title' ] += rdf_rdfa_attributes ( $title_mapping ) + array ( 'datatype' => '' );
2010-01-14 06:23:40 +00:00
// Annotates the td tag containing the author of the node.
2013-06-24 22:21:37 +00:00
$uid_mapping = $mapping -> getPreparedFieldMapping ( 'uid' );
$row [ 'author' ] += rdf_rdfa_attributes ( $uid_mapping );
2010-01-14 06:23:40 +00:00
// Annotates the td tag containing the number of replies. We add the
// content attribute to ensure that only the comment count is used as
// the value for 'num_replies'. Otherwise, other text such as a link
// to the number of new comments could be included in the 'num_replies'
// value.
2013-06-24 22:21:37 +00:00
$comment_count_mapping = $mapping -> getPreparedFieldMapping ( 'comment_count' );
$row [ 'replies' ] += rdf_rdfa_attributes ( $comment_count_mapping );
2010-01-14 06:23:40 +00:00
$row [ 'replies' ] += array ( 'content' => $node -> comment_count );
// If the node has no comments, we assume the node itself was modified
// and apply 'changed' in addition to 'last_activity'. If there are
// comments present, we cannot infer whether the node itself was
// modified or a comment was posted, so we use only 'last_activity'.
2013-06-24 22:21:37 +00:00
$last_activity_mapping = $mapping -> getPreparedFieldMapping ( 'last_activity' );
$last_activity_attributes = rdf_rdfa_attributes ( $last_activity_mapping , $node -> last_activity );
2010-01-14 06:23:40 +00:00
if ( $node -> comment_count == 0 ) {
2013-06-24 22:21:37 +00:00
$changed_mapping = $mapping -> getPreparedFieldMapping ( 'changed' );
$changed_attributes = rdf_rdfa_attributes ( $changed_mapping , $node -> last_activity );
$last_activity_attributes [ 'property' ] = array_merge ( $last_activity_attributes [ 'property' ], $changed_attributes [ 'property' ]);
2010-01-14 06:23:40 +00:00
}
2013-06-24 22:21:37 +00:00
$row [ 'last updated' ] += $last_activity_attributes ;
2010-01-14 06:23:40 +00:00
// We need to add the about attribute on the tr tag to specify which
2012-02-19 03:31:32 +00:00
// node the RDFa annotations above apply to. We move the content of
2010-01-14 06:23:40 +00:00
// $row to a 'data' sub array so we can specify attributes for the row.
$row = array ( 'data' => $row );
$row [ 'about' ] = url ( 'node/' . $node -> nid );
}
$rows [] = $row ;
2009-08-31 06:52:50 +00:00
}
2007-08-23 16:01:01 +00:00
}
2009-07-02 04:27:23 +00:00
$page [ 'tracker' ] = array (
'#rows' => $rows ,
2009-11-23 02:45:42 +00:00
'#header' => array ( t ( 'Type' ), t ( 'Title' ), t ( 'Author' ), t ( 'Replies' ), t ( 'Last updated' )),
2009-07-02 04:27:23 +00:00
'#theme' => 'table' ,
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No content available.' ),
2009-07-02 04:27:23 +00:00
);
$page [ 'pager' ] = array (
'#theme' => 'pager' ,
'#quantity' => 25 ,
'#weight' => 10 ,
);
$page [ '#sorted' ] = TRUE ;
2007-08-23 16:01:01 +00:00
2009-07-02 04:27:23 +00:00
return $page ;
2007-08-23 16:01:01 +00:00
}