You are here

workspace.module in Workspace 7

Presents a user-centric view of content.

Provides a "My workspace" link in the navigation block. When clicked it brings the user to a table showing the content they have created, with handy links to edit or delete the content, or to create new content.

Optionally provides a link to a user's content on their profile page (viewable if the current user has 'view all workspace' permission.

For drupal 7

File

workspace.module
View source
<?php

/**
 * @file
 * Presents a user-centric view of content.
 *
 * Provides a "My workspace" link in the navigation block. When
 * clicked it brings the user to a table showing the content
 * they have created, with handy links to edit or delete the
 * content, or to create new content.
 *
 * Optionally provides a link to a user's content on their
 * profile page (viewable if the current user has 'view all
 * workspace' permission.
 *
 * For drupal 7
 */
define('WORKSPACE_ADMIN_PATH', 'admin/config/workflow/workspace');

/**
 * Implements hook_help().
 */
function workspace_help($path) {
  switch ($path) {
    case 'admin/help#workspace':
      $output = '<p>' . t('The workspace module provides a list of content for each user.') . '</p>';
      $output .= '<p>' . t("When enabled, each user will have a 'My workspace' link in the Navigation menu.") . '</p>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function workspace_menu() {
  $items['workspace/%user_uid_optional'] = array(
    'title' => 'My workspace',
    'page callback' => 'workspace_list_content',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'workspace_access',
    'access arguments' => array(
      1,
    ),
    'weight' => -10,
  );
  $items['workspace/%user/content'] = array(
    'title' => 'Content',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'page callback' => 'workspace_list_content',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'workspace_access',
    'access arguments' => array(
      1,
    ),
    'weight' => 10,
  );
  $items['workspace/%user/comments'] = array(
    'title' => 'Comments',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'workspace_list_comments',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'workspace_access',
    'access arguments' => array(
      1,
      module_exists('comment'),
    ),
    'weight' => 20,
  );

  /*
    $items['workspace/%user/attachments'] = array(
      'title' => 'Attachments',
      'type' => MENU_LOCAL_TASK,
      'page callback' => 'workspace_list_files',
      'page arguments' => array(1),
      'access callback' => 'workspace_access',
      'access arguments' => array(1),
      'weight' => 30,
    );
  */
  $items['workspace/%user/configure'] = array(
    'title' => 'Configure',
    'page callback' => 'workspace_configure',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'workspace_configure_access',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 50,
  );
  $items[WORKSPACE_ADMIN_PATH] = array(
    'title' => 'Workspace',
    'description' => 'Customize workspace behavior.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'workspace_settings',
    ),
    'access arguments' => array(
      'administer workspaces',
    ),
  );

  // In this entry, 'access content' permission may seem liberal
  // but since workspace_delete() simply redirects to
  // node/*/delete deletion permissions will be applied there.
  $items['workspace/delete/%'] = array(
    'title' => 'Delete',
    'page callback' => 'workspace_delete',
    'page arguments' => array(
      2,
    ),
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['workspace/%user/content/%workspace_content_type'] = array(
    'type' => MENU_CALLBACK,
    'access callback' => 'workspace_access',
    'access arguments' => array(
      1,
    ),
    'page callback' => 'workspace_list_content',
    'page arguments' => array(
      1,
      3,
    ),
  );
  $items['workspace/node/switch'] = array(
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'administer nodes',
    ),
    'page callback' => 'workspace_node_status_json_switch',
  );
  return $items;
}
function workspace_content_type_load($arg) {
  global $user;
  $found = FALSE;
  foreach (node_type_get_types() as $type => $object) {
    if ($type == $arg) {
      if (module_invoke_all('node_access', $type, 'create', $user)) {
        $found = TRUE;
      }
    }
  }
  return $found ? $arg : FALSE;
}

/**
 * Menu access callback.
 */
function workspace_access($account, $module_enabled = TRUE) {
  global $user;

  // Let admin see $account
  if ($user->uid == 1 && $account->uid > 1) {
    return user_access('access content', $account) && user_access('access workspace', $account) && $module_enabled;
  }
  return user_access('access content', $user) && user_access('access workspace', $user) && (user_access('view all workspaces', $user) || $user->uid == $account->uid && $user->uid != 0) && $module_enabled;

  // If, e.g., comment module is disabled, remove comment local task.
}

/**
 * Menu access callback.
 */
function workspace_configure_access($account) {
  global $user;
  if ($user->uid == 1 && $account->uid > 1) {
    return user_access('administer workspaces', $account) || user_access('administer own workspace', $account);
  }
  return $user->uid != 0 && user_access('administer workspaces') || $user->uid == $account->uid && user_access('administer own workspace');
}

/**
 * Implements hook_perm().
 */
function workspace_permission() {
  return array(
    'administer workspaces' => array(
      'title' => t('Administer Workspaces'),
      'description' => t('Access the Workspace administration pages.'),
    ),
    'access workspace' => array(
      'title' => t('Access Workspace'),
      'description' => t('Allow users to access their own Workspace.'),
    ),
    'administer own workspace' => array(
      'title' => t('Administer own Workspace'),
      'description' => t('Allow users to configure their own Workspace.'),
    ),
    'view all workspaces' => array(
      'title' => t('View all Workspaces'),
      'description' => t('Allow an administer to view all Workspaces'),
    ),
  );
}

/**
 * Menu callback. The configuration page.
 */
function workspace_configure($account) {
  drupal_set_title(t('Workspace: @name', array(
    '@name' => $account->name,
  )));
  $output = drupal_get_form('workspace_configure_form', $account);
  return $output;
}

/**
 * Form definition for per-user configuration form.
 */
function workspace_configure_form($form, &$form_state, $account) {
  $defaults = workspace_user_config_get_defaults();
  $workspace = variable_get('workspace_user_config_' . $account->uid, array());
  $form['maxnodes'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of items'),
    '#description' => t('Maximum number of items to display in your workspace.'),
    '#default_value' => isset($workspace['maxnodes']) && $workspace['maxnodes'] ? $workspace['maxnodes'] : $defaults['maxnodes'],
    '#size' => 4,
  );
  $form['maxfilenames'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of files'),
    '#description' => t('Maximum number of filenames to display in your workspace.'),
    '#default_value' => isset($workspace['maxfilenames']) && $workspace['maxfilenames'] ? $workspace['maxfilenames'] : $defaults['maxfilenames'],
    '#size' => 4,
  );
  if (module_exists('comment')) {
    $form['maxcomments'] = array(
      '#type' => 'textfield',
      '#title' => t('Number of comments'),
      '#description' => t('Maximum number of comments to display in your workspace.'),
      '#default_value' => isset($workspace['maxcomments']) && $workspace['maxcomments'] ? $workspace['maxcomments'] : $defaults['maxcomments'],
      '#size' => 4,
    );
  }
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $account->uid,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  return $form;
}

/**
 * Validator for per-user configuration form.
 *
 * @see workspace_configure_form()
 */
function workspace_configure_form_validate($form, $form_state) {
  if (!is_numeric($form_state['values']['maxnodes'])) {
    form_set_error('maxnodes', t('Please enter a numeric value.'));
  }
  if (!is_numeric($form_state['values']['maxfilenames']) && !form_get_errors()) {
    form_set_error('maxfilenames', t('Please enter a numeric value.'));
  }
  if (module_exists('comment')) {
    if (!is_numeric($form_state['values']['maxcomments']) && !form_get_errors()) {
      form_set_error('maxcomments', t('Please enter a numeric value.'));
    }
  }
}

/**
 * Submit handler for per-user configuration form.
 *
 * @see workspace_configure_form()
 */
function workspace_configure_form_submit($form, &$form_state) {

  // Must do a full user load to get existing serialized data.

  #$account = user_load(array('uid' => $form_state['values']['uid']));
  $uid = $form_state['values']['uid'];
  $workspace = array();
  $workspace['maxnodes'] = (int) $form_state['values']['maxnodes'];
  $workspace['maxfilenames'] = (int) $form_state['values']['maxfilenames'];
  if (module_exists('comment')) {
    $workspace['maxcomments'] = (int) $form_state['values']['maxcomments'];
  }
  variable_del('workspace_user_config_' . $uid);
  variable_set('workspace_user_config_' . $uid, $workspace);
  drupal_set_message(t('The workspace preferences have been saved.'));
  $form_state['redirect'] = 'workspace/' . $form_state['values']['uid'];
}

/**
 * Menu callback. Display list of content.
 *
 * @param $account
 *   User object representing user whose workspace is being listed.
 */
function workspace_list_content($account, $filter = '') {
  global $user;
  $js_settings = array(
    'host' => $_SERVER['HTTP_HOST'],
    'modulepath' => drupal_get_path('module', 'workspace'),
    'active' => t('Yes'),
    'inactive' => t('No'),
    'use_icons' => variable_get('workspace_use_icons', 1),
  );
  drupal_add_js(array(
    'workspace' => $js_settings,
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'workspace') . '/workspace.js');
  drupal_add_css(drupal_get_path('module', 'workspace') . '/workspace.css');
  drupal_set_title(t('Workspace: @name', array(
    '@name' => $account->name,
  )));
  $defaults = workspace_user_config_get_defaults();
  $workspace = variable_get('workspace_user_config_' . $account->uid, $defaults);
  $max = $workspace['maxnodes'];
  $comments_enabled = module_exists('comment');
  $rows = array();
  if ($comments_enabled) {

    // If the comment module is enabled, we need to get comment counts too.
    $header = array(
      array(
        'data' => t('Type'),
        'field' => 'type',
      ),
      array(
        'data' => t('Title'),
        'field' => 'title',
      ),
      array(
        'data' => t('Published'),
        'field' => 'status',
      ),
      array(
        'data' => t('Modified'),
        'field' => 'changed',
        'sort' => 'desc',
      ),
      array(
        'data' => t('Replies'),
        'field' => 'comment_count',
      ),
      array(
        'data' => t('Operations'),
        'colspan' => 3,
      ),
    );
    $countquery = db_select('node', 'n');
    $countquery
      ->addExpression('COUNT(n.nid)', 'ct');
    $countquery
      ->join('node_comment_statistics', 's', 'n.nid = s.nid');
    $countquery
      ->condition('n.uid', $account->uid);
    if ($filter) {
      $countquery
        ->condition('n.type', $filter);
    }
    $query = db_select('node', 'n')
      ->extend('PagerDefault')
      ->limit($max)
      ->extend('TableSort')
      ->orderByHeader($header);
    $query
      ->setCountQuery($countquery);
    $query
      ->fields('n', array(
      'nid',
      'uid',
      'type',
      'title',
      'status',
      'changed',
      'comment',
      'promote',
      'sticky',
    ));
    $query
      ->addField('s', 'comment_count');
    $query
      ->addExpression('0', 'cid');
    $query
      ->addExpression('1', 'node');
    $query
      ->join('node_comment_statistics', 's', 'n.nid = s.nid');
    $query
      ->condition('n.uid', $account->uid);
    if ($filter) {
      $query
        ->condition('n.type', $filter);
    }
  }
  else {

    // Otherwise we use a simpler query.
    $header = array(
      array(
        'data' => t('Type'),
        'field' => 'type',
      ),
      array(
        'data' => t('Title'),
        'field' => 'title',
      ),
      array(
        'data' => t('Published'),
        'field' => 'status',
      ),
      array(
        'data' => t('Modified'),
        'field' => 'changed',
        'sort' => 'desc',
      ),
      array(
        'data' => t('Operations'),
        'colspan' => 3,
      ),
    );
    $countquery = db_select('node', 'n');
    $countquery
      ->addExpression('COUNT(n.nid)', 'ct');
    $countquery
      ->condition('n.uid', $account->uid);
    if ($filter) {
      $countquery
        ->condition('n.type', $filter);
    }
    $query = db_select('node', 'n')
      ->extend('PagerDefault')
      ->limit($max)
      ->extend('TableSort')
      ->orderByHeader($header);
    $query
      ->setCountQuery($countquery);
    $query
      ->fields('n', array(
      'nid',
      'uid',
      'type',
      'title',
      'status',
      'changed',
      'comment',
      'promote',
      'sticky',
    ));
    $query
      ->addExpression('0', 'cid');
    $query
      ->addExpression('1', 'node');
    $query
      ->condition('n.uid', $account->uid);
    if ($filter) {
      $query
        ->condition('n.type', $filter);
    }
  }
  $records = $query
    ->execute();
  $rows = workspace_build_rows($records, $account);
  $output = '';

  // Only add the content add form if the user is viewing his/her own workspace.
  if ($user->uid == 1 || user_access('view all workspaces') || $user->uid == $account->uid) {
    $output .= render(drupal_get_form('workspace_add_form'));
  }
  $output .= render(drupal_get_form('workspace_list_form', $account->uid, $filter));
  $output .= theme('workspace_list', array(
    'header' => $header,
    'rows' => $rows,
  ));
  return $output;
}

/**
 * Menu callback. Display list of comments.
 *
 * @param $account
 *   User object representing user whose workspace is being listed.
 */
function workspace_list_comments($account) {
  drupal_set_title(t('Workspace: @name', array(
    '@name' => $account->name,
  )));
  $defaults = workspace_user_config_get_defaults();
  $workspace = variable_get('workspace_user_config_' . $account->uid, $defaults);
  $max = $workspace['maxcomments'];
  $rows = array();

  #  $sql =       'SELECT c.nid, c.uid, c.cid, c.subject, c.status, c.timestamp, c.pid, 0 FROM {comments} c WHERE c.uid = %d';

  #  $count_sql = 'SELECT COUNT(cid) FROM {comments} WHERE uid = %d';

  #  // check wether this user has any comments, if not skip pager to avoid div by zero error

  #  $result = db_query('SELECT COUNT(cid) AS ct FROM {comments} WHERE uid = %d', array($account->uid));

  #  $row = db_fetch_array($result);

  #  $has_comments = $row['ct'];

  // Build the comment listing.
  $header = array(
    array(
      'data' => t('Title'),
      'field' => 'title',
    ),
    array(
      'data' => t('Published'),
      'field' => 'status',
    ),
    array(
      'data' => t('Modified'),
      'field' => 'changed',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Replies'),
      'field' => 'comment_count',
    ),
    array(
      'data' => t('Operations'),
      'colspan' => 2,
    ),
  );
  $countquery = db_select('comment', 'c');
  $countquery
    ->addExpression('COUNT(c.cid)', 'ct');
  $countquery
    ->condition('c.uid', $account->uid);
  $query = db_select('comment', 'c')
    ->extend('PagerDefault')
    ->limit($max)
    ->extend('TableSort')
    ->orderByHeader($header);
  $query
    ->setCountQuery($countquery);
  $query
    ->fields('c', array(
    'nid',
    'uid',
    'cid',
    'subject',
    'status',
    'changed',
    'pid',
  ));
  $query
    ->condition('c.uid', $account->uid);
  $records = $query
    ->execute();
  $rows = workspace_build_rows($records, $account);
  return theme('workspace_list', array(
    'header' => $header,
    'rows' => $rows,
  ));
}

/**
 * Build the query result into a table, respecting access.
 *
 * @param $result
 *   A result object as returned from pager_query().
 * @param $account
 *   User object representing user whose workspace is being listed.
 */
function workspace_build_rows($records, $account) {
  global $user;
  $yes = t('yes');
  $no = t('no');
  $use_icons = variable_get('workspace_use_icons', 1);
  $rows = array();
  foreach ($records as $row) {

    // It's a node.
    if ($row->node == 1) {

      // Edit and delete permissions are set by the node type's access hook.
      // If no access hook is found, node-level permissions are then used.
      // This approach is part of Drupal's core design.
      $may_edit = FALSE;
      $may_delete = FALSE;

      //Check the node type's access hook.

      #      $function = $row->type . '_access';

      #      if ($cck_enabled && strstr($function, 'content_')) {

      #        $function = 'content_access';

      #      }

      #      else {
      $function = 'node_access';

      #      }
      if (function_exists($function)) {
        if (!$function('view', $row, $user)) {

          // No view permission means it is not shown in the workspace.
          continue;
        }
        $may_edit = $function('update', $row, $user) ? TRUE : FALSE;
        $may_delete = $function('delete', $row, $user) ? TRUE : FALSE;
      }
      else {

        // If we can't find an access function, deny by default.
        continue;
      }
      $editlink = $use_icons ? _workspace_get_icon('edit', "node/" . $row->nid . "/edit", array(
        'title' => t('Edit'),
      )) : l(t('Edit'), "node/" . $row->nid . "/edit");
      $deletelink = $use_icons ? _workspace_get_icon('delete', "workspace/delete/" . $row->nid, array(
        'title' => t('Delete'),
      )) : l(t('Delete'), "workspace/delete/" . $row->nid);
      $viewlink = $use_icons ? _workspace_get_icon('view', "node/{$row->nid}", array(
        'title' => t('View'),
      )) : l($row->title, "node/{$row->nid}");
      $printstatus = $row->status ? t('Yes') : t('No');
      $iconstatus = $row->status ? 'active' : 'inactive';
      if (user_access('administer nodes', $account) || user_access('administer nodes', $user)) {
        $statuslink = $use_icons ? _workspace_get_icon($iconstatus, '', array(
          'title' => $printstatus,
          'onClick' => "Drupal.workspace_json_status_switch({$row->nid}, 'workspace/node/switch')",
          'class' => 'workspace_trig',
          'id' => "workspace_switch_{$row->nid}",
        )) : "<span onClick='Drupal.workspace_json_status_switch({$row->nid}, \"workspace/node/switch\")' class='workspace_trig' id='workspace_switch_{$row->nid}'>{$printstatus}</span>";
      }
      else {
        $statuslink = $use_icons ? _workspace_get_icon($iconstatus, '', array(
          'title' => $printstatus,
        )) : $printstatus;
      }
      if (module_exists('comment')) {
        $rows[] = array(
          node_type_get_name($row->type),
          l($row->title, "node/" . $row->nid),
          $statuslink,
          format_date($row->changed, variable_get('workspace_dateformat_default', 'medium')),
          array(
            'data' => $row->comment ? $row->comment_count ? $row->comment_count : 0 : t('N/A'),
            'align' => 'right',
          ),
          $viewlink,
          $may_edit ? $editlink : '',
          $may_delete ? $deletelink : '',
        );
      }
      else {
        $rows[] = array(
          node_type_get_name($row->type),
          l($row->title, "node/{$row->nid}"),
          $statuslink,
          format_date($row->changed, variable_get('workspace_dateformat_default', 'medium')),
          $viewlink,
          $may_edit ? $editlink : '',
          $may_delete ? $deletelink : '',
        );
      }
    }
    else {

      // It's a comment.
      $num_replies = comment_num_replies($row->cid);
      $comment->cid = $row->cid;
      $comment->nid = $row->nid;

      // Delegate access permission checks and link generation to comment.module.
      $comment_links = comment_links($comment, 0);
      $link = $use_icons ? _workspace_get_icon('edit', "comment/edit/" . $comment->cid, array(
        'title' => t('Edit'),
      )) : l(t('Edit'), "comment/edit/" . $comment->cid);
      $edit_link = $comment_links['comment_edit'] ? $link : '';
      $link = $use_icons ? _workspace_get_icon('delete', "comment/delete/" . $comment->cid, array(
        'title' => t('Delete'),
      )) : l(t('Delete'), "comment/delete/" . $comment->cid);
      $delete_link = $comment_links['comment_delete'] ? $link : '';
      $printstatus = $row->status ? t('Yes') : t('No');
      $iconstatus = $row->status ? 'active' : 'inactive';
      $statuslink = $use_icons ? _workspace_get_icon($iconstatus, '', array(
        'title' => $printstatus,
      )) : $printstatus;
      $row_options = array(
        'attributes' => array(),
        'query' => NULL,
        'fragment' => "comment-{$row->cid}",
        'absolute' => FALSE,
        'html' => TRUE,
      );
      $node = node_load($row->nid);

      //Need to load node to get the content type
      if (module_exists('talk') and variable_get("comment_talk_{$node->type}", FALSE)) {
        $row_link = l($row->subject, "node/{$row->nid}/talk", $row_options);
      }
      else {
        $row_link = l($row->subject, "node/{$row->nid}", $row_options);
      }
      $rows[] = array(
        $row_link,
        $statuslink,
        format_date($row->timestamp, variable_get('workspace_dateformat_default', 'medium')),
        array(
          'data' => $num_replies,
          'align' => 'right',
        ),
        $edit_link,
        $delete_link,
      );
    }
  }
  return $rows;
}

/**
 * Menu callback. List managed files.
 *
 * @param $account
 *   User object representing user whose workspace is being listed.
 */

/*
function workspace_list_files($account) {
  global $user;
  drupal_set_title(t('Workspace: @name', array('@name' => $account->name)));
  $defaults = workspace_user_config_get_defaults();
  $workspace = variable_get('workspace_user_config_' . $account->uid, $defaults);
  $max = $workspace['maxfilenames'];
  $download = t('download');
  $rows = array();
  $header = array(
    array('data' => t('Filename'), 'field' => 'f.filename'),
    array('data' => t('Type'), 'field' => 'f.filemime'),
    array('data' => t('Modified'), 'field' => 'f.timestamp', 'sort' => 'desc'),
    array('data' => t('Size'), 'field' => 'f.filesize'),
    array('data' => t('Operations')),
  );
  $countquery = db_select('file_managed', 'f');
  $countquery->addExpression('COUNT(f.fid)', 'ct');
  $countquery->join('users', 'u', 'f.uid = u.uid');
  $countquery->condition('f.uid', $account->uid);
  $countquery->condition('f.status', 1);

  $query = db_select('file_managed', 'f')
    ->extend('PagerDefault')->limit($max)
    ->extend('TableSort')->orderByHeader($header);
  $query->setCountQuery($countquery);
  $query->fields('f', array('filename', 'uri', 'filemime', 'filesize', 'timestamp'));
  $query->join('users', 'u', 'f.uid = u.uid');
  $query->condition('f.uid', $account->uid);
  $query->condition('f.status', 1);

#  $sql = "SELECT u.nid, f.filemime, f.filename, f.filesize, f.timestamp, f.filepath FROM {files} f LEFT JOIN {upload} u ON f.fid = u.fid WHERE f.uid = %d";
#  $result = pager_query($sql . tablesort_sql($header), $max, 2, NULL, $account->uid);

  $records = $query->execute();
  foreach ($records AS $row) {
    $rows[] = array(
      l($row->filename, "node/" . $row->nid),
      $row->filemime,
      format_date($row->timestamp, variable_get('workspace_dateformat_default', 'medium')),
      format_size($row->filesize),
      l($download, file_create_url($row->uri)),
    );
  }

  return theme('workspace_list', array('header' => $header, 'rows' => $rows));
}
*/

/**
 * Menu callback. Redirect user to delete node.
 */
function workspace_delete($nid) {

  #$nid = intval(arg(2));
  if (is_numeric($nid)) {
    drupal_goto('node/' . $nid . '/delete');
  }
}

/**
 * Form definition for content addition form.
 */
function workspace_add_form($form, &$form_state) {
  $description = t('Choose what kind of content you would like to add.') . ' ' . l(t('Help?'), 'node/add');
  $options = workspace_get_types();
  if (!$options) {
    return array();
  }
  $form['add'] = array(
    '#type' => 'fieldset',
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['add']['content_type'] = array(
    '#type' => 'select',
    '#options' => $options,
  );
  $form['add']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add new item'),
    '#suffix' => $description,
  );
  return $form;
}

/**
 * Submit handler for content addition form.
 *
 * Simply redirects to appropriate node/add/* page.
 *
 * @see workspace_add_form()
 */
function workspace_add_form_submit($form, $form_state) {
  global $user;
  $node_type = $form_state['values']['content_type'];
  if ($node_type == t('Select...')) {
    drupal_goto('node/add');
  }
  foreach (node_type_get_types() as $type => $object) {
    if (node_access('create', $type, $user)) {
      $options[$type] = $object->name;
    }
    if (isset($options[$node_type])) {

      /* Start hack */
      if (stristr($node_type, "_") > -1) {
        $node_type = str_replace("_", "-", $node_type);
      }

      /* End hack */
      drupal_goto('node/add/' . $node_type);
    }
  }
}
function workspace_get_types() {
  global $user;
  $select = t('Select...');
  $options = array(
    $select => $select,
  );
  foreach (node_type_get_types() as $type => $object) {
    if (module_invoke_all('node_access', $type, 'create', $user)) {
      $options[$type] = $object->name;
    }
  }
  if (count($options) == 1) {
    return FALSE;
  }
  return $options;
}
function workspace_list_form($form, &$form_state, $account_id, $filter) {
  $options = workspace_get_types();
  if (!$options) {
    return array();
  }
  $form['content_type'] = array(
    '#title' => t('Show Type'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $filter ? $filter : '',
    '#prefix' => '<div class="container-inline">',
  );
  $form['account_id'] = array(
    '#type' => 'value',
    '#value' => $account_id,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
  );
  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
    '#submit' => array(
      'workspace_list_form_reset',
    ),
    '#suffix' => '</div>',
  );
  return $form;
}
function workspace_list_form_submit($form, &$form_state) {
  $account_id = $form_state['values']['account_id'];
  $node_type = $form_state['values']['content_type'];
  if ($node_type == t('Select...')) {
    drupal_goto('workspace/' . $account_id . '/content');
  }
  $account = user_load($account_id);
  foreach (node_type_get_types() as $type => $object) {
    if (node_access('create', $type, $account)) {
      $options[$type] = $object->name;
    }
    if (isset($options[$node_type])) {
      drupal_goto('workspace/' . $account_id . '/content/' . $node_type);
    }
  }
}
function workspace_list_form_reset($form, &$form_state) {
  $account_id = $form_state['values']['account_id'];
  drupal_goto('workspace/' . $account_id . '/content');
}

/**
 * Implements hook_settings().
 */
function workspace_settings() {
  $roles = user_roles();

  // The anonymous user does not have a profile.
  unset($roles[DRUPAL_ANONYMOUS_RID]);
  $form['info'] = array(
    '#value' => "Select each role for which you want the link to a user's workspace to show up in their user profile.",
  );
  foreach ($roles as $rid => $name) {
    $identifier = 'workspace_user_profile_' . $rid;
    $form[$identifier] = array(
      '#type' => 'checkbox',
      '#title' => $name,
      '#default_value' => variable_get($identifier, 0),
    );
  }
  $form['workspace_user_profile_link'] = array(
    '#type' => 'textfield',
    '#title' => t("Name of link to user's workspace"),
    '#description' => t("This will be used to display the link to a user's workspace on his or her profile page. The %username token will be replaced by the username of the user whose profile is being shown."),
    '#default_value' => variable_get('workspace_user_profile_link', t('View recent content created by %username')),
  );
  $form['workspace_use_icons'] = array(
    '#type' => 'checkbox',
    '#title' => t('Icons in tables'),
    '#return_value' => 1,
    '#default_value' => variable_get('workspace_use_icons', 1),
    '#description' => t('Setting this option enables icons in tables.'),
  );
  $form['workspace_dateformat_default'] = array(
    '#type' => 'select',
    '#title' => t('Set Date Format'),
    '#description' => t("Select the Date format to use in tables."),
    '#default_value' => variable_get('workspace_dateformat_default', 'medium'),
    '#options' => array(
      'short' => t('Short'),
      'medium' => t('Medium'),
      'long' => t('Long'),
    ),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_user_view().
 *
 * Place link to user's workspace on user's profile page.
 */
function workspace_user_view($account) {
  if (workspace_access($account)) {
    $variables['%username'] = $account->name;
    $link_title = variable_get('workspace_user_profile_link', t('View recent content'));
    $link = l(strtr($link_title, $variables), 'workspace/' . $account->uid);
    if (!isset($account->content['workspace'])) {
      $account->content['workspace'] = array();
    }
    $account->content['workspace'] += array(
      '#type' => 'user_profile_category',
      '#title' => variable_get('workspace_user_profile_title', t('Content')),
    );
    if (!isset($account->content['workspace']['link'])) {
      $account->content['workspace']['link'] = array();
    }
    $account->content['workspace']['link'] += array(
      '#type' => 'user_profile_item',
      '#title' => '',
      '#markup' => $link,
    );
  }
}

/**
 * Function to get an icon
 * Derived from invoice module
 *
 * @param $name
 *   Name of icon without extension.
 * @param $url
 *   URL to link the icon to.
 * @param $attributes
 *   Any optional HTML attributes.
 * @param $extension
 *   The file extension.
 * @return
 *   The icon string.
 */
function _workspace_get_icon($name, $url = NULL, $attributes = array(), $extension = 'png') {
  if (empty($attributes['alt'])) {
    $attributes['alt'] = $attributes['title'];
  }
  $file_addition = '';
  foreach ($attributes as $key => $value) {
    $file_addition .= ' ' . $key . '="' . $value . '"';
  }
  $icon = '<img src="' . base_path() . drupal_get_path('module', 'workspace') . '/images/' . $name . '.' . $extension . '"' . $file_addition . ' />';
  if (!empty($url)) {
    $icon = l($icon, $url, array(
      'html' => TRUE,
    ));
  }
  return $icon;
}
function workspace_user_config_get_defaults() {
  $workspace = array();
  $workspace['maxnodes'] = 50;
  $workspace['maxfilenames'] = 50;
  if (module_exists('comment')) {
    $workspace['maxcomments'] = 50;
  }
  return $workspace;
}

/**
 * Function to switch status of dnsman node using json.
 *
 */
function workspace_node_status_json_switch() {
  $nid = check_plain($_GET['value']);
  if ($nid) {
    $node = node_load($nid);
    if ($node->status) {

      // deactivate
      $status = 0;
    }
    else {

      // activate
      $status = 1;
    }
    $node->status = $status;
    node_save($node);
    $item['status'] = $status ? 'active' : 'inactive';
    $item['id'] = $nid;
    drupal_json_output($item);
  }
  exit;
}

/**
 * Implements hook_theme().
 */
function workspace_theme() {
  return array(
    'workspace_list' => array(
      'variables' => array(
        'header' => array(),
        'rows' => array(),
      ),
    ),
    'workspace_add_form' => array(
      'render element' => 'form',
    ),
    'workspace_list_form' => array(
      'render element' => 'form',
    ),
    'workspace_configure_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Theme the list of content. Turn the results into a table.
 */
function theme_workspace_list($variables) {
  $header = $variables['header'];
  $rows = $variables['rows'];
  $build['workspace_list'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('Your workspace is currently empty.'),
  );
  $build['workspace_pager'] = array(
    '#theme' => 'pager',
  );
  $output = render($build);
  return $output;
}
function theme_workspace_add_form($variables) {
  $form = $variables['form'];
  $output = '';
  $output .= drupal_render_children($form);
  return $output;
}
function theme_workspace_list_form($variables) {
  $form = $variables['form'];
  $output = '';
  $output .= drupal_render_children($form);
  return $output;
}
function theme_workspace_configure_form($variables) {
  $form = $variables['form'];
  $output = '';
  $output .= drupal_render_children($form);
  return $output;
}

Functions

Namesort descending Description
theme_workspace_add_form
theme_workspace_configure_form
theme_workspace_list Theme the list of content. Turn the results into a table.
theme_workspace_list_form
workspace_access Menu access callback.
workspace_add_form Form definition for content addition form.
workspace_add_form_submit Submit handler for content addition form.
workspace_build_rows Build the query result into a table, respecting access.
workspace_configure Menu callback. The configuration page.
workspace_configure_access Menu access callback.
workspace_configure_form Form definition for per-user configuration form.
workspace_configure_form_submit Submit handler for per-user configuration form.
workspace_configure_form_validate Validator for per-user configuration form.
workspace_content_type_load
workspace_delete Menu callback. Redirect user to delete node.
workspace_get_types
workspace_help Implements hook_help().
workspace_list_comments Menu callback. Display list of comments.
workspace_list_content Menu callback. Display list of content.
workspace_list_form
workspace_list_form_reset
workspace_list_form_submit
workspace_menu Implements hook_menu().
workspace_node_status_json_switch Function to switch status of dnsman node using json.
workspace_permission Implements hook_perm().
workspace_settings Implements hook_settings().
workspace_theme Implements hook_theme().
workspace_user_config_get_defaults
workspace_user_view Implements hook_user_view().
_workspace_get_icon Function to get an icon Derived from invoice module

Constants

Namesort descending Description
WORKSPACE_ADMIN_PATH @file Presents a user-centric view of content.