uc_order_handler_field_order_id.inc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @file
  4. * Contains the basic 'order' field handler.
  5. */
  6. /**
  7. * Field handler: simple renderer that links to the order administration page.
  8. */
  9. class uc_order_handler_field_order_id extends views_handler_field {
  10. /**
  11. * Override init function to provide generic option to link to user.
  12. */
  13. function init(&$view, &$data) {
  14. parent::init($view, $data);
  15. if (!empty($this->options['link_to_order'])) {
  16. $this->additional_fields['order_id'] = array('table' => 'uc_orders', 'field' => 'order_id');
  17. $this->additional_fields['uid'] = array('table' => 'uc_orders', 'field' => 'uid');
  18. }
  19. }
  20. /**
  21. * Overrides views_handler::option_definition().
  22. */
  23. function option_definition() {
  24. $options = parent::option_definition();
  25. $options['link_to_order'] = array('default' => FALSE);
  26. return $options;
  27. }
  28. /**
  29. * Overrides views_handler::options_form().
  30. *
  31. * Provides link to order administration page.
  32. */
  33. function options_form(&$form, &$form_state) {
  34. parent::options_form($form, $form_state);
  35. $form['link_to_order'] = array(
  36. '#title' => t('Link this field to the order view page'),
  37. '#description' => t('This will override any other link you have set.'),
  38. '#type' => 'checkbox',
  39. '#default_value' => !empty($this->options['link_to_order']),
  40. );
  41. }
  42. /**
  43. * Renders whatever the data is as a link to the order.
  44. *
  45. * Data should be made XSS safe prior to calling this function.
  46. */
  47. function render_link($data, $values) {
  48. if (!empty($this->options['link_to_order'])) {
  49. $this->options['alter']['make_link'] = FALSE;
  50. if (user_access('view all orders')) {
  51. $path = 'admin/store/orders/' . $this->get_value($values, 'order_id');
  52. }
  53. elseif (user_access('view own orders') && $this->get_value($values, 'uid') == $GLOBALS['user']->uid) {
  54. $path = 'user/' . $GLOBALS['user']->uid . '/orders/' . $this->get_value($values, 'order_id');
  55. }
  56. else {
  57. $path = FALSE;
  58. }
  59. if ($path && $data !== NULL && $data !== '') {
  60. $this->options['alter']['make_link'] = TRUE;
  61. $this->options['alter']['path'] = $path;
  62. }
  63. }
  64. return $data;
  65. }
  66. /**
  67. * Overrides views_handler_field::render().
  68. */
  69. function render($values) {
  70. return $this->render_link(check_plain($values->{$this->field_alias}), $values);
  71. }
  72. }