View source
<?php
function heartbeat_stream($stream_name, $page = 0, $account = NULL) {
static $streams;
try {
if (!isset($streams[$stream_name])) {
$streamConfig = heartbeat_stream_config_load($stream_name);
if (_heartbeat_stream_has_access($streamConfig)) {
$heartbeatStream = HeartbeatStreamFactory::getStream($streamConfig, $page, $account);
drupal_add_js(array(
'heartbeatPollNewerMessages' => array(
$streamConfig->class => $streamConfig->poll_messages,
),
), 'setting');
drupal_add_js(array(
'heartbeatPollTypes' => array(
$streamConfig->class => $streamConfig->poll_messages_type,
),
), 'setting');
module_invoke_all('heartbeat_stream_load', $heartbeatStream);
if ($heartbeatStream instanceof HeartbeatStream && !$heartbeatStream
->hasErrors()) {
$streams[$stream_name] = $heartbeatStream;
}
else {
$streams[$stream_name] = FALSE;
}
}
else {
$streams[$stream_name] = FALSE;
}
}
} catch (HeartbeatInvalidStreamException $h) {
drupal_set_message($h
->getMessage());
drupal_not_found();
}
return $streams[$stream_name];
}
function heartbeat_stream_build(HeartbeatStream &$heartbeatStream) {
$messages = $heartbeatStream
->execute();
}
function heartbeat_stream_views_build(HeartbeatStream &$heartbeatStream, $values) {
return $heartbeatStream
->executeViews($values);
}
function heartbeat_stream_view(HeartbeatStream &$heartbeatStream, $view_mode = NULL) {
if (isset($view_mode)) {
$heartbeatStream
->setViewMode($view_mode);
}
$build = $heartbeatStream
->render();
return $build;
}
function heartbeat_stream_config_load_all($reset = FALSE) {
$streams =& drupal_static('heartbeat_streams');
if (!$reset && ($object = cache_get('heartbeat_streams'))) {
$streams = $object->data;
}
else {
ctools_include('export');
$streams = ctools_export_crud_load_all('heartbeat_streams');
cache_set('heartbeat_streams', $streams);
}
return $streams;
}
function _heartbeat_stream_config_load_all($reset) {
$streams = ctools_export_load_object('heartbeat_streams', 'all');
foreach (array_keys($streams) as $key) {
_heartbeat_stream_config_unpack($streams[$key]);
}
return $streams;
}
function heartbeat_stream_config_load($class) {
$streams =& drupal_static('heartbeat_streams');
if (!isset($streams[$class])) {
ctools_include('export');
$streams[$class] = ctools_export_crud_load('heartbeat_streams', $class);
if (isset($streams[$class])) {
_heartbeat_stream_config_unpack($streams[$class]);
}
}
return $streams[$class];
}
function _heartbeat_stream_config_load($name) {
$result = ctools_export_load_object('heartbeat_streams', 'names', array(
$name,
));
if (isset($result[$name])) {
return $result[$name];
}
}
function heartbeat_stream_config_reset($streamConfig = NULL) {
cache_clear_all('heartbeat_streams', 'cache');
drupal_static_reset('heartbeat_streams');
variable_set('menu_rebuild_needed', TRUE);
}
function _heartbeat_stream_config_unpack(HeartbeatStreamConfig &$streamConfig) {
if (!empty($streamConfig->settings)) {
foreach ($streamConfig->settings as $key => $value) {
$streamConfig->{$key} = $value;
}
}
}
function heartbeat_stream_has_access($stream) {
$streamConfig = heartbeat_stream_config_load($stream);
return _heartbeat_stream_has_access($streamConfig);
}
function _heartbeat_stream_has_access($streamConfig = NULL) {
if (isset($streamConfig) && $streamConfig instanceof HeartbeatStreamConfig) {
return user_access('view heartbeat messages') && user_access('view ' . $streamConfig->name . ' stream');
}
return FALSE;
}
class HeartbeatStreamFactory {
public static function getStream($streamConfig, $page = 0, $account = NULL) {
$className = !empty($streamConfig->real_class) ? $streamConfig->real_class : $streamConfig->class;
if (class_exists($className)) {
return new $className($streamConfig, $page, $account);
}
else {
throw new HeartbeatInvalidStreamException(t('Stream class @class does not exist in @path or it\'s not auto-included in yourmodule.info.', array(
'@class' => $streamConfig->class,
'@path' => $streamConfig->path,
)), 1000, 'stream does not exist');
}
}
}
class HeartbeatInvalidStreamException extends Exception {
public $arguments;
function __construct($message, $code = 0, $arguments = array()) {
parent::__construct($message, $code);
$this->arguments = $arguments;
}
}