Skip to content
xc_account.module 38 KiB
Newer Older
<?php
/**
 * @file
 * XC Account module
 *
 * @copyright (c) 2010-2011 eXtensible Catalog Organization
 */

/**
 * Implements hook_permission().
 */
function xc_account_access($account) {
  global $user;
  return user_access('administer users') ||
         user_access('administer xc accounts') ||
         ($user->uid && ($user->uid == $account->uid));
}

function xc_account_permission() {
  return array(
    'administer xc accounts' => array(
      'title' => t('administer xc accounts'),
      'description' => t('administer xc accounts'),
    ),
    'edit own xc account' => array(
      'title' => t('edit own xc account'),
      'description' => t('edit own xc account'),
    ),
    'edit all xc accounts' => array(
      'title' => t('edit all xc accounts'),
      'description' => t('edit all xc accounts'),
    ),
  );
}

/**
 * Implements hook_node_access().
 *
 * Access XC Account
 *
 * @param $node
 *   Either a node object or the machine name of the content type on which to perform the access check.
 * @param $op
 *   The operation to be performed. Possible values: create, delete, update, view
 * @param $account
 *   The user object to perform the access check operation on.
 *   TRUE if can access XC account
function xc_account_node_access($node, $op, $account) {
  global $user;
  return user_access('administer users') ||
         user_access('administer xc accounts') ||
         ($user->uid && ($user->uid == $account->uid));
}

/**
 * Implements hook_form_alter().
 */
function xc_account_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    if (!(($user->uid && ($user->uid == $account->uid) &&
           user_access('edit own xc account')) ||
           user_access('edit all xc accounts'))) {
      unset($form['account']);
    }

    // Solves problem of Drupal forcing the validation of emails (Part 1)
    $fake_email = 'not-a-real-email@c4LSh9V5FztmvkZZZ3eb';
    if (empty($form['account']['mail']['#default_value']) || ((is_array($form_state['values']) &&
        empty($form_state['values']['mail'])))) {
      $form['account']['mail'] = array(
        '#type' => 'hidden',
        '#value' => $fake_email,
      );
    }
  }
}

/**
 * Implements hook_user_presave().
 */
function xc_account_user_presave(&$edit, $account, $category) {
  // Solves problem of Drupal forcing the validation of emails (Part 2)
  $fake_email = 'not-a-real-email@c4LSh9V5FztmvkZZZ3eb';
  if ($edit['mail'] == $fake_email) {
    $edit['mail'] = '';
  }
}

/**
 * Implements hook_user_view().
 */
function xc_account_user_view($account, $view_mode, $langcode) {
  if (xc_account_access($account)) {
    $personal_info = xc_account_get_personal_information($account);
Kiraly Peter's avatar
Kiraly Peter committed
    $xc_account_pi = variable_get('xc_account_pi', array());
    $xc_account_pi[$account->uid] = array(
      'num_loaned_items' => isset($personal_info['num_loaned_items'])?$personal_info['num_loaned_items']: '',
      'num_requested_items' => isset($personal_info['num_requested_items'])?$personal_info['num_requested_items']: '',
Kiraly Peter's avatar
Kiraly Peter committed
    );
    variable_set('xc_account_pi', $xc_account_pi);

    if (isset($personal_info['name'])
             || (isset($personal_info['address']) && is_array($personal_info['address']))
             || (isset($personal_info['email']) && is_array($personal_info['email']))) {
      $account->content['xc_account_personal_info'] = array(
        '#type' => 'user_profile_category',
        '#title' => t('Personal information'),
        '#weight' => -10,
      );

      if (isset($personal_info['name'])) {
        $account->content['xc_account_personal_info']['name'] = array(
          '#type' => 'user_profile_item',
          '#title' => t('Full name'),
          '#markup' => is_array($personal_info['name']) ? current($personal_info['name']) : $personal_info['name'],
      if (isset($personal_info['address']) && is_array($personal_info['address'])) {
        foreach ($personal_info['address'] as $address) {
          $account->content['xc_account_personal_info']['address'][] = array(
            '#type' => 'user_profile_item',
            '#title' => t($address['type']),
            '#markup' => $address['address'],
            '#weight' => -6,
          );
        }
      }

      if (is_array($personal_info['email'])) {
        foreach ($personal_info['email'] as $email) {
          $account->content['xc_account_personal_info']['email'][] = array(
            '#type' => 'user_profile_item',
            '#title' => t($email['type']),
            '#markup' => $email['email'],
            '#weight' => -8,
          );
        }
      }
    }

    if (!empty($personal_info['num_loaned_items'])
             || !empty($personal_info['num_requested_items'])
             || !empty($personal_info['account_balance'])) {
      $account->content['xc_account_status'] = array(
        '#type' => 'user_profile_category',
        '#title' => t('Library account status'),
        '#weight' => -8,
      );

      if (!empty($personal_info['num_loaned_items'])) {
        $link = l(t('(see all)'), 'user/' . $account->uid . '/loaned-items');
        $account->content['xc_account_status']['num_loaned_items'] = array(
          '#type' => 'user_profile_item',
          '#title' => t('Loaned items'),
          '#markup' => $personal_info['num_loaned_items'] . ' ' . $link,
          '#weight' => -10,
        );
      }

      if (!empty($personal_info['num_requested_items'])) {
        $link = l(t('(see all)'), 'user/' . $account->uid . '/requested-items');
        $account->content['xc_account_status']['num_requested_items'] = array(
          '#type' => 'user_profile_item',
          '#title' => t('Requested items'),
          '#markup' => $personal_info['num_requested_items'] . ' ' . $link,
          '#weight' => -8,
        );
      }

      if (!empty($personal_info['account_balance'])) {
        $account->content['xc_account_status']['account_balance'] = array(
          '#type' => 'user_profile_item',
          '#title' => t('Account balance'),
          '#markup' => $personal_info['account_balance'],
          '#weight' => -6,
        );
      }
    }
  }
  return;
}

/**
 * Implements hook_user().
 */
function xc_account_user_OLD($op, &$edit, &$account, $category = NULL) {
  // TODO Remaining code in this function needs to be moved to the appropriate new hook function.
  global $user;
}

/**
 * Implements hook_menu().
 */
function xc_account_menu() {
  // A NOTE: user/%/view weight is 10, we should add lesser weight to position the tabs before view
  $items['user/%user/loaned-items'] = array(
    'title' => 'Check-out',
    'title callback' => 'xc_account_loaned_items_title',
    'title arguments' => array(1),
    'description' => 'List of checked out items',
    'page callback' => 'xc_account_loaned_items',
    'page arguments' => array(1),
    'access callback' => 'xc_account_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => -20,
  );

  $items['user/%user/requested-items'] = array(
    'title' => 'Requested',
    'title callback' => 'xc_account_requested_items_title',
    'title arguments' => array(1),
    'description' => 'List of requested items',
    'page callback' => 'xc_account_requested_items',
    'page arguments' => array(1),
    'access callback' => 'xc_account_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => -19,
  );

  $items['user/%user/bookmarked-items'] = array(
    'title' => 'Bookmarked',
    'title callback' => 'xc_account_bookmarked_items_title',
    'title arguments' => array(1),
    'description' => 'List of bookmarked items',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('xc_account_bookmarked_items_form', 1),
    'access callback' => 'xc_account_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => -18,
  );
  /*
   $items['user/%user/bookmarked-items/%/request'] = array(
   'title' => 'Request item',
   'description' => 'Request item',
   'page callback' => 'drupal_get_form',
   'page arguments' => array('xc_account_bookmarked_item_request_form', 1, 3),
   'access callback' => 'xc_account_access',
   'access arguments' => array(1),
   'type' => MENU_LOCAL_TASK,
   'weight' => 5,
   );
   */
  $items['user/%user/bookmarked-items/%/remove'] = array(
    'title' => 'Remove item',
    'description' => 'Remove item from bookmarks',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('xc_account_bookmarked_item_remove_form', 1, 3),
    'access callback' => 'xc_account_access',
    'access arguments' => array(1),
    'type' => MENU_CALLBACK,
    'weight' => 5,
  );

  $items['xc/account/email-form'] = array(
    'title' => 'Email',
    'description' => 'Email results',
    'page callback' => 'xc_account_email_form3', //drupal_get_form',
    // 'page arguments' => array('xc_account_email_form3'),
    'access arguments' => array(TRUE),
    // 'access callback' => 'xc_account_access',
    // 'access arguments' => array(1),
    'type' => MENU_CALLBACK,
    'weight' => 5,
  );

  $items['xc/account/email-form2'] = array(
    'title' => 'Email',
    'description' => 'Email results',
    'page callback' => 'xc_account_email_form2',
    'access arguments' => array(TRUE),
    // 'access callback' => 'xc_account_access',
    // 'access arguments' => array(1),
    'type' => MENU_CALLBACK,
    'weight' => 5,
  );

  return $items;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_email_form2() {
  return drupal_get_form('xc_account_email_form');
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_email_form3() {
  $output = l(t('email'), 'xc/account/email-form2', array('attributes' => array('rel' => 'lightframe')));
  return $output;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_email_form($form) {
  global $user;
  $form['to'] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
  );
  $form['from'] = array(
    '#type' => 'textfield',
    '#title' => t('From'),
    '#default_value' => $user->email,
  );
  $form['comments'] = array(
    '#type' => 'textarea',
    '#title' => t('Comments'),
  );
  // TODO: captcha
  $form['comments'] = array(
    '#type' => 'textarea',
    '#title' => t('Comments'),
  );
  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
  );
  $form['send'] = array(
    '#type' => 'submit',
    '#value' => t('Send'),
  );

  return $form;
}

/**
 * Gets checked out items page
 *
 * @params $account (Object)
 *   User account object
 *
 * @return (String)
 *   Themed list of Checked out items
 */
function xc_account_loaned_items($account, $values = array()) {
  $loaned_items = xc_account_get_loaned_items($account, $values);
  $xc_account_pi = variable_get('xc_account_pi', array());
  $xc_account_pi[$account->uid]['num_loaned_items'] = count($loaned_items);
  variable_set('xc_account_pi', $xc_account_pi);

  $rows = array();
  $i = 0;
  $has_bibid = FALSE;
  foreach ($loaned_items as $item) {
    $schema_field = xc_ils_field_by_provider($item['ncip_provider_id']);
    $solr_field = xc_solr_schema2solr($schema_field, TRUE, 'phrase');
    $params = array('paths' => $item['renew']['path']);
    $form = drupal_get_form('xc_account_renew_form', $params);
    ++$i;
    $row = array(
      'data' => array(
        array(
          'data' => $i . '.',
          'class' => array('control count-number'),
        ),
        array(
          'data' => @date('m/d/Y', $item['date_due']),
          'class' => ($item['date_due'] < time() ? array('control overdue') : array('control')),
        ),
        array(
          'data' => $form,
          'class' => array('control renew'),
      'class' => array('xc-check-out-item'),
    );
    if (isset($item['bib_id']) && !is_null($item['bib_id'])) {
      $has_bibid = TRUE;
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'title', 'check-out'),
        'class' => array('title'),
      );
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'authors'),
        'class' => array('authors'),
      );
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'type'),
        'class' => array('xc-format'),
      );
    }
    else {
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'title', 'check-out'),
        'class' => array('title'),
        'colspan' => 3,
      );
    }
    $rows[] = $row;
  }

  if ($has_bibid) {
    $header = array(
      array(
        'data' => t('Due Date'),
        'colspan' => 3,
        'id' => 'xc-check-out-due-date',
      ),
      t('Item'),
      t('Author'),
      t('Type')
    );
    $header = array(
      array(
        'data' => t('Due Date'),
        'colspan' => 3,
        'id' => 'xc-check-out-due-date',
      ),
      array(
        'data' => t('Item'),
        'colspan' => 3,
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array('class' => array('xc-check-out-items'))
  ));
} // xc_account_loaned_items

/**
 * Renew item form definition
 *
 * @param $form_state (array)
 *   FAPI form state
 * @param $params (Array)
 *   Parameters
 *
 * @return (array)
 *   FAPI Form definition
 */
function xc_account_renew_form($form, $form_state, $params) {
  $form['paths'] = array(
    '#type' => 'hidden',
    '#value' => $params['paths'],
  );
  $form['renew'] = array(
    '#type' => 'submit',
    '#value' => t('Renew'),
    '#disabled' => TRUE,
    '#attributes' => array('class' => array('renew action-disabled')),
  );

  return $form;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_renew_form_submit($form, &$form_state) {
  $form_state['redirect'] = $form_state['values']['paths'];
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_loaned_items_title($account, $values = array()) {
  $xc_account_pi = variable_get('xc_account_pi', array());
  if (isset($xc_account_pi[$account->uid]['num_loaned_items'])) {
    $count = $xc_account_pi[$account->uid]['num_loaned_items'];
  }
  else {
    $count = count(xc_account_get_loaned_items($account, $values));
  }
  return t('Checked out (@count)', array('@count' => $count));
}

/**
 * Requested items page
 */
function xc_account_requested_items($account, $values = array()) {
  $requested_items = xc_account_get_requested_items($account, $values);
  $xc_account_pi = variable_get('xc_account_pi', array());
  $xc_account_pi[$account->uid]['num_requested_items'] = count($requested_items);
  variable_set('xc_account_pi', $xc_account_pi);
  $header = array('', t('Requested'), t('Due'), t('Item'), t('Author'), t('Type'));

  $rows = array();
  $i = 0;
  $has_bibid = FALSE;
  foreach ($requested_items as $item) {
    $schema_field = xc_ils_field_by_provider($item['ncip_provider_id']);
    $solr_field = xc_solr_schema2solr($schema_field, TRUE, 'phrase');

    $row = array(
      'data' => array(
        array(
          'data' => ++$i . '.',
          'class' => array('control count-number'),
        ),
        array(
          'data' => @date('m/d/Y', $item['date_placed']),
          'class' => array('requested'),
        ),
        array(
          'data' => @date('m/d/Y', $item['date_due']),
          'class' => ($item['date_due'] < time() ? array('control overdue') : array('control')),
      'class' => array('xc-request-item'),
    );

    if (isset($item['bib_id']) && !is_null($item['bib_id'])) {
      $has_bibid = TRUE;
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'title', 'requested'),
        'class' => array('title'),
      );
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'authors'),
        'class' => array('authors'),
      );
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'type'),
        'class' => array('xc-format'),
      );
    }
    else {
      $row['data'][] = array(
        'data' => xc_account_item_get_field($item, $solr_field, 'title', 'check-out'),
        'class' => array('title'),
        'colspan' => 3,
      );
    }

    $rows[] = $row;
  }

  if ($has_bibid) {
    $header = array('', t('Requested'), t('Due'), t('Title'), t('Author'), t('Type'));
  }
  else {
    $header = array('', t('Requested'), t('Due'), array(
        'data' => t('Title'),
        'colspan' => 3,
      ));
  }

  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array('class' => array('xc-requested-items'))
  ));
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_requested_items_title($account, $values = array()) {
  $xc_account_pi = variable_get('xc_account_pi', array());
  if (isset($xc_account_pi[$account->uid]['num_requested_items'])) {
    $count = $xc_account_pi[$account->uid]['num_requested_items'];
  }
  else {
    $count = count(xc_account_get_requested_items($account, $values));
  }
  return t('Requested (@count)', array('@count' => $count));
}

/**
 * Get a themed title information of the document represented by an NCIP item
 *
 * @param $item (Array)
 *   An item description
 * @param $solr_field (String)
 *   A Solr field to search for
 *
 * @return (String)
 *   A themed title information
 */
function xc_account_item_to_title($item, $solr_field) {

  $bib_id = $item['bib_id'];
  $result = xc_search_do_solr_search($solr_field . ':"' . $bib_id . '"');
  if (count($result['docs']) > 0) {
    $title = theme('xc_account_item_title', array('0' => $result['docs'][0]));
  }
  elseif (isset($item['bib_desc'])) {
    $bib_desc = $item['bib_desc'];
    $title = theme('xc_account_bib_desc', array('0' => $bib_desc));
  }
  else {
    $title = $bib_id;
  }
  return $title;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_item_get_field($item, $solr_field, $target = 'title', $caller = 'check-out') {
  static $cache;

  if (isset($item['bib_id']) && !is_null($item['bib_id'])) {
    $bib_id = $item['bib_id'];
    if (!isset($cache['bib'][$bib_id])) {
      $result = xc_search_do_solr_search('bibid_s:"' . $bib_id . '"');
      if (count($result['docs']) > 0) {
        $xc_record = _xc_solr_to_array($result['docs'][0]);
        $element = xc_search_get_display_template_elements($xc_record['type'], $xc_record);
        $title = l(
          xc_util_conditional_join(' &mdash; ', $element['title']),
          'node/' . $xc_record['node_id'],
          array('query' => array('caller' => $caller))
        );
        $authors = xc_util_conditional_join(' &mdash; ', $element['creator']);
        $type = xc_search_get_manifestation_formats($xc_record['format']);
      }
      elseif (isset($item['bib_desc'])) {
        $title = $item['bib_desc']['Title'];
        $authors = $item['bib_desc']['Author'];
        $type = '';
      }
      else {
        $title = $item['title'];
        $type = $authors = '';
      }

      $cache['bib'][$bib_id] = array(
        'title' => $title,
        'authors' => $authors,
        'type' => $type,
      );
    }
    return $cache['bib'][$bib_id][$target];
  }
  else {
    if ($target == 'title') {
      return $item['title'];
    }
    return NULL;
  }
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function theme_xc_account_bib_desc($variables) {
  $bib_desc = $variables['0'];
  $output = '';
  $output = $bib_desc['Title'];
  if (!preg_match('/ \/ /', $bib_desc['Title'])) {
    $output .= ' / ' . $bib_desc['Author'];
  }
  $output .= ' (' . $bib_desc['Publisher'] . ')';
  return $output;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
Kiraly Peter's avatar
Kiraly Peter committed
function xc_account_bookmarked_items_form($form, $form_state, $account) {
  $form = array(
    '#tree' => TRUE,
  );
Kiraly Peter's avatar
Kiraly Peter committed

  $bookmarked_items = xc_account_get_bookmarked_items($account);

Kiraly Peter's avatar
Kiraly Peter committed
  $i = 0;
  foreach ($bookmarked_items as $item) {
    $i++;
    $result = xc_search_do_solr_search('id:"' . $item->sid . '"', TRUE, 0, XC_SEARCH_LIMIT, array('XCNAME' => 'xc_account_bookmarked'));
    $doc = $result['docs'][0];
    $xc_record = _xc_solr_to_array($doc);
    $xc_record['full_display_url'] = xc_search_get_full_display_url($xc_record, $i);
    $element   = xc_search_get_display_template_elements($xc_record['type'], $xc_record);

    $title = l(
      xc_util_conditional_join(' &mdash; ', $element['title']),
      'node/' . $item->nid,
      array('query' => array('caller' => 'bookmark', 'hit' => $i), 'html' => TRUE)
    );

    if (!empty($element['creator'])) {
      $authors = t('by !authors', array('!authors' => xc_util_conditional_join(' &mdash; ', $element['creator'])));
    }
    else {
      $authors = '';
    }

Kiraly Peter's avatar
Kiraly Peter committed
    $formats = xc_search_get_manifestation_formats($xc_record['format']);
    $form['items'][$i] = array(
      'checkmark' => array(
        '#type' => 'checkbox',
        '#default_value' => 0,
tombui's avatar
tombui committed
        '#name' => $i
      ),
      'id' => array(
        '#type' => 'hidden',
        '#value' => $item->id,
tombui's avatar
tombui committed
        '#name' => "[items]"."[".$i."][id]"
      ),
      'uid' => array(
        '#type' => 'hidden',
        '#value' => $item->uid,
tombui's avatar
tombui committed
        '#name' => "[items]"."[".$i."][uid]"
      ),
      'sid' => array(
        '#type' => 'hidden',
        '#value' => $item->sid,
tombui's avatar
tombui committed
        '#name' => "items".$i."sid"
      ),
      'nid' => array(
        '#type' => 'hidden',
        '#value' => $item->nid,
tombui's avatar
tombui committed
        '#name' => "items".$i."nid"
      ),
      'htmlid' => array(
        '#type' => 'value',
        '#value' => 'xc-bookmark-' . $item->id,
tombui's avatar
tombui committed
        '#name' => "[items]"."[".$i."][htmlid]"
      ),
      'title' => array(
        '#type' => 'item',
        '#value' => $title,
tombui's avatar
tombui committed
        '#name' => 'title'
      ),
      'authors' => array(
        '#type' => 'item',
        '#value' => $authors,
tombui's avatar
tombui committed
        '#name' => 'authors'
      ),
      'formats' => array(
        '#type' => 'item',
        '#value' => $formats,
tombui's avatar
tombui committed
        '#name' => 'formats'
Kiraly Peter's avatar
Kiraly Peter committed
    );
Kiraly Peter's avatar
Kiraly Peter committed

  $form['submit'] = array(
    '#type' => 'submit',
    '#id' => 'dummy-remove-bookmark',
    '#value' => t('Remove'),
tombui's avatar
tombui committed
    '#name' => 'xc_account_bookmarked_items_form',
  return $form;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function theme_xc_account_bookmarked_items_form($form) {
tombui's avatar
tombui committed
  $form = xc_account_bookmarked_items_form($form=null,$form_state=null,$account=null);
Kiraly Peter's avatar
Kiraly Peter committed

  drupal_add_css(drupal_get_path('module', 'xc_account') . '/bookmarks.css');
  drupal_add_js(drupal_get_path('module', 'xc_account') . '/xc_account.js');

  $destination = drupal_get_destination();
  // TODO The second parameter to this function call should be an array.
  drupal_add_js(array('xc_search' => array(
      'page' => 'bookmark',
      'remove_bookmark_item_url' => url('xc_search/ajax/remove_bookmark_item', array('absolute' => TRUE)),
      'print_url' => url('print/',      array('absolute' => TRUE)),
      'mail_url' => url('printmail/', array('absolute' => TRUE)),
      'destination' => $destination,
    )), array('type' => 'setting', 'scope' => JS_DEFAULT));

Kiraly Peter's avatar
Kiraly Peter committed
  $output = theme('xc_search_bookmark_notification', array());

  // draw the table
  // $print_exists = module_exists('print');
  // $addthis_exists = module_exists('addthis');
Kiraly Peter's avatar
Kiraly Peter committed
  $rows = array();
tombui's avatar
tombui committed
   foreach (element_children($form['items']) as $item_id) {
Kiraly Peter's avatar
Kiraly Peter committed
    $rows[] = array(
tombui's avatar
tombui committed
      drupal_render($form['items'][$item_id]['checkmark']),
      'title'  => '<span class="title">' . $form['items'][$item_id]['title']['#value'] . '</span>',
      'authors'=> '<span class="authors">' . $form['items'][$item_id]['authors']['#value'] . '</span>',
      'format' => '<div class="format">' . $form['items'][$item_id]['formats']['#value'] .'</div>',
//drupal_render(
  if (!empty($rows)) {
    $header = array('', 
        array('data' => t('Item'), 'class' => array('item')), 
        array('data' => t('Author'), 'class' => array('author')), 
        array('data' => t('Type'), 'class' => array('type')));
    $output .= theme('table', array('header' => $header, 'rows' => $rows));
    $output .= '<div id="hidden"><div id="dialog-confirm">' // title="Remove selected bookmarks?"
      . '<p>'
      . t('Are you sure you would like to remove <span id="xc-remove-bookmark-number"></span>?')
      . '</p></div></div>';
Kiraly Peter's avatar
Kiraly Peter committed

    $output .= drupal_render_children($form);
tombui's avatar
tombui committed
    $output .= drupal_render_children($form['submit']);
  }
  else {
    $output = '<p>' . t('There is no bookmarked item yet.') . '</p>';
  }
  return $output;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_bookmarked_items_form_submit($form, &$form_state) {
  foreach ($form_state['values']['items'] as $item) {
    if ($item['checkmark'] == 1) {
      xc_account_remove_bookmark_item($item['uid'], $item['nid'], $item['sid']);
    }
  }
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
Kiraly Peter's avatar
Kiraly Peter committed
function xc_account_bookmarked_items_title($account) {
  $saved_items = xc_account_get_bookmarked_items($account);
  return t('Bookmarked (@count)', array('@count' => count($saved_items)));
}

// TODO....
/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_bookmarked_item_request_form(&$form_state, $account, $item_id) {
  return confirm_form(
    array(
    'id' => array(
      '#type' => 'hidden',
      '#default_value' => $item_id,
    ),
    'user_id' => array(
      '#type' => 'hidden',
      '#default_value' => $account->uid,
    ),
    'type' => array(
      '#type' => 'hidden',
      '#default_value' => 'bib',
    ),
  ),
    t('Are you sure, that you would like to request this item?'),
    'user/' . $account->uid . '/bookmarked-items', // path to go if user click on 'cancel'
    t('This action cannot be undone.'),
    t('Request item'),
    t('Cancel')
  );
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function xc_account_bookmarked_item_request_form_submit($form, &$form_state) {
  /*
   $values  = $form_state['values'];

   $ncip_provider                = $values['ncip_provider'];
   $user_id                      = $values['user_id'];
   $item_id                      = $values['item_id'];
   $type                         = 'bib'; //$values['type'];
   $request_type                 = 'Hold'; // $values['request_type'];
   $pickup_expiry_date           = $values['pickup_expiry_date'];
   $shipping_information_address = NULL; // $values['shipping_information_address'];
   $response = xc_ncip_provider_request_item($ncip_provider->ncip_provider_id,
   $user_id, $item_id, $type, $request_type, $pickup_expiry_date,
   $shipping_information_address, 'xml');
   */
  $form_state['redirect'] = 'user/' . $uid . '/bookmarked-items';
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
Kiraly Peter's avatar
Kiraly Peter committed
function xc_account_bookmarked_item_remove_form($form, &$form_state, $account, $item_id) {
  return confirm_form(
    array(
    'id' => array(
      '#type' => 'hidden',
      '#default_value' => $item_id,
    ),
    'uid' => array(
      '#type' => 'hidden',
      '#default_value' => $account->uid,
    ),
  ),
    t('Are you sure, that you would like to remove this item?'),
    'user/' . $account->uid . '/bookmarked-items', // path to go if user click on 'cancel'
    t('This action cannot be undone.'),
    t('Remove item'),
    t('Cancel')
  );
}

/**
 * Creates a new bookmark
 *
 * @param $uid (int)
 *   User identifier
 * @param $nid (int)
 *   Node identifier
 * @param $sid (String)
 *   The Solr identifier
 *
 * @return (int)
 *   The integer signaling the result of the operation:
 *   - -1: the bookmark does already exist
 *   - 0: the operation failed
 *   - 1: new bookmark were created
 */
Kiraly Peter's avatar
Kiraly Peter committed
function xc_account_bookmark_item($uid, $nid, $sid) {
  $exists = xc_account_bookmark_item_exists($uid, $nid, $sid);
  if (!$exists) {
    $record = new stdClass();
    $record->uid = $uid;
    $record->nid = $nid;
    $record->sid = $sid;
    $success = drupal_write_record('xc_account_bookmarked_items', $record);
    return (int) $success; // FALSE, SAVED_NEW = 1, SAVED_UPDATED = 2
  }
  else {
    return -1;
  }
}

/**
 * Removes an existing bookmark
 *
 * @param $uid (int)
 *   User identifier
 * @param $nid (int)
 *   Node identifier
 * @param $sid (String)
 *   The Solr identifier
 *
 * @return (int)
 *   The integer signaling the result of the operation:
 *   - -1: the bookmark does not exist
 *   - 0: the operation failed
 *   - 1: bookmark were deleted
tombui's avatar
tombui committed
 */
function xc_account_remove_bookmark_item($uid, $nid, $sid) {
  $exists = xc_account_bookmark_item_exists($uid, $nid, $sid);
Kiraly Peter's avatar
Kiraly Peter committed
  if ($exists) {
    $sql = "DELETE FROM {xc_account_bookmarked_items} WHERE uid = :uid AND nid = :nid";
    if (!empty($sid)) {
tombui's avatar
tombui committed
      $sql .= " AND sid = :sid";
tombui's avatar
tombui committed
    $result_code = db_query($sql,array('uid'=>$uid,'nid'=> $nid,'sid'=> $sid));
    return 1;
  }
  else {
    return -1;
  }
}

/**
 * The submit handler for the remove bookmark form
 *
 * @param $form (Array)
 *   The FAPI form definition
 * @param $form_state
 *   The FAPI form state
 */
function xc_account_bookmarked_item_remove_form_submit($form, &$form_state) {
  $id  = $form_state['values']['id'];
  $uid = $form_state['values']['uid'];
tombui's avatar
tombui committed
  $sql = 'DELETE FROM {xc_account_bookmarked_items} WHERE id = :id AND uid = :uid';
  $result_code = db_query($sql,array('id'=>$id,'uid'=> $uid));
Kiraly Peter's avatar
Kiraly Peter committed

  drupal_set_message('Bookmark removed');
  $form_state['redirect'] = 'user/' . $uid . '/bookmarked-items';
}

/**
 * Checks whether the bookmark is already existing or not.
 *