View source
<?php
function fe_nodequeue_features_api() {
$info = array();
$key = 'fe_nodequeue_queue';
$info[$key] = array(
'name' => t('Nodequeues'),
'feature_source' => TRUE,
'default_hook' => 'default_' . $key,
'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
);
return $info;
}
function fe_nodequeue_queue_features_export_options() {
$table = 'fe_nodequeue_queue';
$options = array();
$schema = ctools_export_get_schema($table);
$export = $schema['export'];
$defaults = _ctools_export_get_defaults($table, $export);
foreach ($defaults as $obj) {
$options[$obj->machine_name] = t('@name [@machine_name]', array(
'@name' => $obj->title,
'@machine_name' => $obj->machine_name,
));
}
$query = "SELECT * FROM {{$table}} {$table} INNER JOIN {nodequeue_queue} v ON v.qid = {$table}.qid ORDER BY v.qid ASC";
$result = db_query($query);
while ($obj = db_fetch_object($result)) {
$options[$obj->machine_name] = t('@name [@machine_name]', array(
'@name' => $obj->title,
'@machine_name' => $obj->machine_name,
));
}
ksort($options);
return $options;
}
function fe_nodequeue_queue_features_export($data, &$export, $module_name = '') {
$pipe = array();
$export['dependencies']['fe_nodequeue'] = 'fe_nodequeue';
$map = array();
$modules = module_implements('nodequeue_info');
foreach ($modules as $module) {
$callback = $module . '_nodequeue_info';
$info = call_user_func($callback);
if (is_array($info)) {
foreach ($info as $_key => $_data) {
$map[$_key] = $module;
}
}
}
$table = 'fe_nodequeue_queue';
foreach ($data as $object_name) {
$export['features'][$table][$object_name] = $object_name;
$queue_owner = db_result(db_query("SELECT queue.owner FROM {{$table}} {$table} INNER JOIN {nodequeue_queue} queue ON queue.qid = {$table}.qid WHERE {$table}.machine_name = '%s'", $object_name));
if (!empty($queue_owner) && !empty($map[$queue_owner])) {
$export['dependencies'][$map[$queue_owner]] = $map[$queue_owner];
}
}
return $pipe;
}
function fe_nodequeue_queue_features_export_render($module_name = '', $data) {
ctools_include('export');
$component = 'fe_nodequeue_queue';
$schema = ctools_export_get_schema($component);
$objects = ctools_export_load_object($component);
$code = array();
$code[] = ' $export = array();';
$code[] = '';
foreach ($data as $machine_name) {
if ($object = $objects[$machine_name]) {
$additions = array();
if (!empty($object->qid)) {
$nodequeue_queue = nodequeue_load($object->qid);
if (!empty($nodequeue_queue->roles)) {
sort($nodequeue_queue->roles);
}
if (!empty($nodequeue_queue->types)) {
sort($nodequeue_queue->types);
}
$additions = (array) $nodequeue_queue;
unset($additions['qid']);
unset($additions['subqueues']);
}
$identifier = $schema['export']['identifier'];
$code[] = ctools_export_object($component, $object, ' ', $identifier, $additions);
$code[] = ' $export[\'' . $machine_name . '\'] = $' . $identifier . ';';
$code[] = '';
}
}
$code[] = ' return $export;';
$code = implode("\n", $code);
return array(
$schema['export']['default hook'] => $code,
);
}
function fe_nodequeue_queue_features_rebuild($module_name = NULL) {
$table = 'fe_nodequeue_queue';
$defaults = features_get_default($table, $module_name);
if (empty($defaults)) {
return;
}
foreach ($defaults as $object) {
if (empty($object->machine_name)) {
continue;
}
$qid = db_result(db_query("SELECT qid FROM {{$table}} WHERE machine_name = '%s'", $object->machine_name));
if (empty($qid) || !($nodequeue_queue = nodequeue_load($qid))) {
$result = _fe_nodequeue_save_queue((array) $object);
if (!empty($result['qid'])) {
db_query("DELETE FROM {{$table}} WHERE qid = %d OR machine_name = '%s'", $result['qid'], $object->machine_name);
db_query("INSERT INTO {{$table}} (qid, machine_name) VALUES (%d, '%s')", $result['qid'], $object->machine_name);
}
}
}
return TRUE;
}
function fe_nodequeue_queue_features_revert($module_name = NULL) {
$table = 'fe_nodequeue_queue';
$defaults = features_get_default($table, $module_name);
if (empty($defaults)) {
return;
}
foreach ($defaults as $object) {
if (empty($object->machine_name)) {
continue;
}
$qid = db_result(db_query("SELECT qid FROM {{$table}} WHERE machine_name = '%s'", $object->machine_name));
if (empty($qid) || !($nodequeue_queue = nodequeue_load($qid))) {
$result = _fe_nodequeue_save_queue((array) $object);
if (!empty($result['qid'])) {
db_query("DELETE FROM {{$table}} WHERE qid = %d OR machine_name = '%s'", $result['qid'], $object->machine_name);
db_query("INSERT INTO {{$table}} (qid, machine_name) VALUES (%d, '%s')", $result['qid'], $object->machine_name);
}
}
else {
$object->qid = $qid;
$result = _fe_nodequeue_save_queue((array) $object);
}
}
return TRUE;
}
function fe_nodequeue_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'nodequeue_edit_queue_form' && user_access('administer features')) {
$form['description']['#weight'] = -2;
$table = 'fe_nodequeue_queue';
$qid = $form['qid']['#value'];
if (!empty($qid)) {
$machine_name = db_result(db_query("SELECT machine_name FROM {{$table}} WHERE qid = %d", $qid));
}
$form['machine_name'] = array(
'#type' => 'textfield',
'#title' => t('Machine name'),
'#default_value' => empty($machine_name) ? '' : $machine_name,
'#maxlength' => 32,
'#description' => t('Give the nodequeue a machine name to make it exportable with "!features" module.', array(
'!features' => l('Features', 'http://drupal.org/project/features'),
)),
'#weight' => -1,
);
$form['#validate'][] = 'fe_nodequeue_machine_name_validate';
$form['#submit'][] = 'fe_nodequeue_machine_name_submit';
}
elseif ($form_id == 'nodequeue_admin_delete') {
$form['#submit'][] = 'fe_nodequeue_machine_name_delete';
}
}
function fe_nodequeue_machine_name_validate($form, &$form_state) {
if (empty($form_state['values']['machine_name'])) {
return;
}
$table = 'fe_nodequeue_queue';
if (!preg_match('!^[a-z0-9_]+$!', $form_state['values']['machine_name'])) {
form_set_error('machine_name', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
}
elseif (db_result(db_query("SELECT COUNT(*) FROM {{$table}} WHERE qid <> %d AND machine_name = '%s'", $form_state['values']['qid'], $form_state['values']['machine_name']))) {
form_set_error('machine_name', t('The machine-readable name has been taken. Please pick another one.'));
}
}
function fe_nodequeue_machine_name_submit($form, &$form_state) {
$table = 'fe_nodequeue_queue';
db_query("DELETE FROM {{$table}} WHERE qid = %d", $form_state['values']['qid']);
if (!empty($form_state['values']['machine_name'])) {
drupal_write_record($table, $form_state['values']);
}
}
function fe_nodequeue_machine_name_delete($form, &$form_state) {
$table = 'fe_nodequeue_queue';
db_query("DELETE FROM {{$table}} WHERE qid = %d", $form_state['values']['qid']);
}
function _fe_nodequeue_save_queue($settings = array()) {
$settings['roles'] = drupal_map_assoc($settings['roles']);
$settings['types'] = drupal_map_assoc($settings['types']);
$form_state = array();
$form_state['values'] = $settings;
module_load_include('inc', 'nodequeue', 'includes/nodequeue.admin');
nodequeue_edit_queue_form_submit(NULL, $form_state);
$settings['qid'] = $form_state['values']['qid'];
return $settings;
}