Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 41 additions & 36 deletions wf_job/includes/wf_job.pages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -303,42 +303,6 @@ function wf_job_view($job) {
return reset($out['wf_job']);
}

/**
* Drush aliases export form.
*/
function wf_job_export_drush_aliases_form($form, $form_state) {
$form = array(
'introduction' => array(
'#type' => 'item',
'#description' => t('Download wf.aliases.drushrc.php file for all existing jobs'),
),
'download' => array(
'#type' => 'submit',
'#value' => t('Download File'),
),
);
return $form;
}

/**
* Submit handler for wf_job_export_drush_aliases_form().
*/
function wf_job_export_drush_aliases_form_submit($form, &$form_state) {
$aliases = wf_job_drush_aliases();
$php = "<?php\n" . theme('wf_job_drush_aliases', array('aliases' => $aliases));

$file = new stdClass();
$file->filename = 'wf.aliases.drushrc.php';
$file->filemime = 'application/x-php';
$file->filesize = strlen($php);

$headers = file_get_content_headers($file);
drupal_send_headers($headers);

echo $php;
exit;
}

/**
* Form for deleting jobs.
*/
Expand All @@ -365,3 +329,44 @@ function wf_job_delete_form_submit($form, &$form_state) {
$form_state['redirect'] = url('admin/content/jobs');
}

/**
* Renders a form page containing the Drush integrations.
*/
function wf_job_export_drush_aliases_page($user) {
$output['drush_aliases'] = array(
array(
'#theme' => 'link',
'#text' => t('Download Drush aliases'),
'#path' => 'user/jobs/get-drush-aliases',
'#options' => array(
'attributes' => array(),
'html' => FALSE,
),
'#suffix' => t("<p>Download Drush aliases for all of your Jobs's sandboxes and place this file at ~/.drush/ (~/ means your home path)</p>"),
),
);
return $output;
}

/**
* Page for exporting drush aliases file.
*/
function wf_job_output_drush_aliases_file() {
$aliases = wf_job_drush_aliases();
if (!$aliases) {
global $user;
drupal_set_message(t('No jobs available'), 'warning');
drupal_goto("user/$user->uid/jobs/drush-aliases");
}
$php = "<?php\n" . theme('wf_job_drush_aliases', array('aliases' => $aliases));
$file = new stdClass();
$file->filename = 'wf.aliases.drushrc.php';
$file->filemime = 'application/x-php';
$file->filesize = strlen($php);
$headers = file_get_content_headers($file);
// Workaround for: https://www.drupal.org/node/1773032
$headers['Content-Disposition'] = 'attachment; filename="' . $file->filename . '"';
drupal_send_headers($headers);
print $php;
drupal_exit();
}
79 changes: 56 additions & 23 deletions wf_job/wf_job.module
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,6 @@ function wf_job_menu() {
'type' => MENU_LOCAL_ACTION,
'weight' => 50,
);
$items['admin/content/jobs/drush-aliases'] = array(
'title' => 'Download drush aliases',
'description' => 'Admin page for exporting drush wf.aliases.drushrc.php file',
'page callback' => 'drupal_get_form',
'page arguments' => array('wf_job_export_drush_aliases_form'),
'access arguments' => array('export job aliases'),
'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
'file' => 'includes/wf_job.pages.inc',
);
$items['admin/structure/job-types/manage/%wf_job_type'] = array(
'title' => 'Config job type',
'access arguments' => array('administer jobs'),
Expand All @@ -273,6 +264,22 @@ function wf_job_menu() {
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/wf_job.admin.inc',
);
$items['user/%user/jobs/drush-aliases'] = array(
'title' => 'Drush aliases',
'page callback' => 'wf_job_export_drush_aliases_page',
'page arguments' => array(1),
'access callback' => 'wf_job_is_allowed_export_drush_aliases',
'access arguments' => array(1),
'file' => 'includes/wf_job.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['user/jobs/get-drush-aliases'] = array(
'page callback' => 'wf_job_output_drush_aliases_file',
'access arguments' => array('export job aliases'),
'file' => 'includes/wf_job.pages.inc',
'type' => MENU_CALLBACK,
);

return $items;
}

Expand Down Expand Up @@ -320,6 +327,10 @@ function wf_job_permission() {
'title' => t('Reallocate job'),
);

$perms['export job aliases'] = array(
'title' => t('Export Job aliases'),
);

return $perms;
}

Expand Down Expand Up @@ -1100,6 +1111,11 @@ function wf_job_sandbox_uri($job, $path = '') {
// TODO make this pluggable.
$slug = wf_job_slug($job->jid);
$dev_env = wf_environment_dev();

if (!$dev_env) {
return NULL;
}

return wf_environment_app_uri($dev_env, $job);
}

Expand All @@ -1110,21 +1126,20 @@ function wf_job_sandbox_uri($job, $path = '') {
* An array of aliases
*/
function wf_job_drush_aliases() {
$new_id = variable_get('wf_job_jsid_new');
$completed_id = variable_get('wf_job_jsid_completed');
$job_results = views_get_view_result('user_jobs', 'user_jobs');

$query = new EntityFieldQuery();
// Implement hook_entity_query_alter() if you need to change this.
$jids = $query->entityCondition('entity_type', 'wf_job')
->propertyCondition('jsid', array($new_id, $completed_id), 'NOT IN')
->propertyOrderBy('jid')
->execute();
// No results.
if (!$job_results) {
return NULL;
}

$jobs = array();
if (!empty($jids['wf_job'])) {
$jobs = entity_load('wf_job', array_keys($jids['wf_job']));
$jids = array();
foreach ($job_results as $job_result) {
$jids[] = $job_result->jid;
}

$jobs = entity_load('wf_job', $jids);

$aliases = array(
'job' => array(
'remote-host' => variable_get('wf_sandbox_remote_host', 'localhost'),
Expand All @@ -1138,13 +1153,12 @@ function wf_job_drush_aliases() {
'parent' => '@job',
'uri' => preg_replace('#^https?://#', '', $job_uri['path']),
'root' => variable_get('wf_sandbox_basepath', '/srv/www') . "/$slug/",
);
$aliases[$job->reference] = array(
'parent' => "@$slug",
'job' => $job->jid,
);
}

drupal_alter('wf_job_drush_aliases', $aliases);

return $aliases;
}

Expand Down Expand Up @@ -1730,3 +1744,22 @@ function wf_job_attach_form_log_value_to_entity(&$entity, $form_state, $log_type
wf_job_attach_log_to_entity($entity, $form_state['values']['log'], $log_type);
}
}

/**
* Checks if the user is allowed to export drush aliases.
*
* @param object $user
* The user object to be checked.
*
* @return bool
* Returns TRUE in case of success, FALSE otherwise.
*/
function wf_job_is_allowed_export_drush_aliases(stdClass $user) {
$current_user = $GLOBALS['user'];

if ($user->uid != $current_user->uid) {
return FALSE;
}

return user_access('export job aliases');
}
76 changes: 76 additions & 0 deletions wf_job/wf_job.views_default.inc
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,81 @@ function wf_job_views_default_views() {
$handler = $view->new_display('block', 'Block', 'block');
$export['job_users'] = $view;

$view = new view();
$view->name = 'user_jobs';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'wf_job';
$view->human_name = 'User\'s Jobs';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'User\'s Jobs';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
/* Field: Workflow Job: ID */
$handler->display->display_options['fields']['jid']['id'] = 'jid';
$handler->display->display_options['fields']['jid']['table'] = 'wf_job';
$handler->display->display_options['fields']['jid']['field'] = 'jid';

/* Display: User's Jobs */
$handler = $view->new_display('page', 'User\'s Jobs', 'user_jobs');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'Jobs';
$handler->display->display_options['defaults']['access'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'view own job job';
$handler->display->display_options['defaults']['relationships'] = FALSE;
/* Relationship: Workflow Job: Owner */
$handler->display->display_options['relationships']['owner']['id'] = 'owner';
$handler->display->display_options['relationships']['owner']['table'] = 'wf_job';
$handler->display->display_options['relationships']['owner']['field'] = 'owner';
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Workflow Job: ID */
$handler->display->display_options['fields']['jid']['id'] = 'jid';
$handler->display->display_options['fields']['jid']['table'] = 'wf_job';
$handler->display->display_options['fields']['jid']['field'] = 'jid';
/* Field: Workflow Job: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'wf_job';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['alter']['make_link'] = TRUE;
$handler->display->display_options['fields']['title']['alter']['path'] = 'job/[jid]';
$handler->display->display_options['defaults']['sorts'] = FALSE;
/* Sort criterion: Workflow Job: ID */
$handler->display->display_options['sorts']['jid']['id'] = 'jid';
$handler->display->display_options['sorts']['jid']['table'] = 'wf_job';
$handler->display->display_options['sorts']['jid']['field'] = 'jid';
$handler->display->display_options['sorts']['jid']['order'] = 'DESC';
$handler->display->display_options['defaults']['arguments'] = FALSE;
/* Contextual filter: User: Uid */
$handler->display->display_options['arguments']['uid']['id'] = 'uid';
$handler->display->display_options['arguments']['uid']['table'] = 'users';
$handler->display->display_options['arguments']['uid']['field'] = 'uid';
$handler->display->display_options['arguments']['uid']['relationship'] = 'owner';
$handler->display->display_options['arguments']['uid']['default_action'] = 'default';
$handler->display->display_options['arguments']['uid']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['filter_groups']['operator'] = 'OR';
$handler->display->display_options['filter_groups']['groups'] = array(
1 => 'AND',
2 => 'AND',
);
$handler->display->display_options['defaults']['filters'] = FALSE;
$handler->display->display_options['path'] = 'user/jobs';
$export['job_users'] = $view;

return $export;
}