View source
<?php
function views_atom_rules_action_info() {
$actions['views_atom_rules_action_add_to_feed'] = array(
'label' => t('Add an entity to a feed'),
'arguments' => array(
'node' => array(
'type' => 'node',
'label' => t('Node'),
),
),
'module' => 'Views Atom',
);
$actions['views_atom_rules_action_add_nids_to_feed'] = array(
'label' => t('Add multiple nids to a feed'),
'arguments' => array(
'nids' => array(
'type' => 'icalinstance_nids',
'label' => t('Nids'),
),
),
'module' => 'Views Atom',
);
return $actions;
}
function views_atom_rules_action_add_to_feed_form($settings, &$form) {
foreach (views_atom_get_feed_displays() as $entry) {
$options[$entry['title']][$entry['name'] . '-' . $entry['display']] = $entry['display_title'];
}
$form['generating_display'] = array(
'#type' => 'select',
'#title' => t('Generating view'),
'#options' => $options,
'#default_value' => $settings['view'] . '-' . $settings['view_display'],
'#description' => t('Select the view and display that is responsible for generating the feed for this node. Only Feed displays are available.'),
);
}
function views_atom_rules_action_add_to_feed_submit(&$settings, $form, $form_state) {
list($settings['view'], $settings['view_display']) = explode('-', $form_state['values']['generating_display']);
}
function views_atom_rules_action_add_to_feed($node, $settings) {
$nid = $node->nid;
$view = views_get_view($settings['view']);
$view
->set_display($settings['view_display']);
if ($view
->access($settings['view_display'])) {
node_load($param = array(), $revision = NULL, $reset = TRUE);
$result = $view
->preview($settings['view_display'], array(
$nid,
));
if (!empty($view->result)) {
$topic_url = url($view->display[$view->current_display]->display_options['path'], array(
'absolute' => TRUE,
));
push_hub_notify($topic_url, $result);
}
}
}
function views_atom_rules_action_add_nids_to_feed_form($settings, &$form) {
foreach (views_atom_get_feed_displays() as $entry) {
$options[$entry['title']][$entry['name'] . '-' . $entry['display']] = $entry['display_title'];
}
$form['generating_display'] = array(
'#type' => 'select',
'#title' => t('Generating view'),
'#options' => $options,
'#default_value' => $settings['view'] . '-' . $settings['view_display'],
'#description' => t('Select the view and display that is responsible for generating the feed for these nodes. Only Feed displays are available.'),
);
$form['settings']['max_push_size'] = array(
'#type' => 'textfield',
'#title' => t('Maximum push size'),
'#default_value' => $settings['max_push_size'],
'#description' => t('This action may have a very large number of nodes passed to it. It may be beneficial to break this list of nodes into smaller chunks before passing the chunks on to Push Hub. Enter a positive integer as the maximum number of nodes to be sent to Push Hub together. If you enter 40 in this field and this action is called on a list of 120 nodes there will be three chunks of 40 nodes sent to Push Hub. Leave this field blank or enter 0 to always send the list of nodes to Push Hub as one chunk, no matter how large.'),
'#element_validate' => array(
'views_atom_element_validate_integer_positive_or_zero',
),
);
}
function views_atom_element_validate_integer_positive_or_zero($element, &$form_state) {
$value = $element['#value'];
if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value < 0)) {
form_error($element, t('%name must be a positive integer or 0.', array(
'%name' => $element['#title'],
)));
}
}
function views_atom_rules_action_add_nids_to_feed_submit(&$settings, $form, $form_state) {
list($settings['view'], $settings['view_display']) = explode('-', $form_state['values']['generating_display']);
}
function views_atom_rules_action_add_nids_to_feed($nids, $settings) {
if ($nids) {
if (is_numeric($settings['max_push_size']) && $settings['max_push_size'] >= 1) {
$chunk_size = $settings['max_push_size'];
}
else {
$chunk_size = count($nids);
}
$chunked_array = array_chunk($nids, $chunk_size);
foreach ($chunked_array as $nids_chunk) {
$view = views_get_view($settings['view']);
$view
->set_display($settings['view_display']);
if ($view
->access($settings['view_display'])) {
$nid_string = implode(',', $nids_chunk);
node_load($param = array(), $revision = NULL, $reset = TRUE);
$result = $view
->preview($settings['view_display'], array(
$nid_string,
));
if (!empty($view->result)) {
$topic_url = url($view->display[$view->current_display]->display_options['path'], array(
'absolute' => TRUE,
));
push_hub_notify($topic_url, $result);
}
}
}
}
}