flag_lists_handler_field_ops.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class flag_lists_handler_field_ops extends views_handler_field {
  3. function option_definition() {
  4. $options = parent::option_definition();
  5. $options['flo'] = array(
  6. 'contains' => array(
  7. 'force_single' => array('default' => FALSE),
  8. 'operation' => array('default' => 'flag'),
  9. ),
  10. );
  11. return $options;
  12. }
  13. function options_form(&$form, &$form_state) {
  14. parent::options_form($form, $form_state);
  15. $form['flo'] = array(
  16. '#type' => 'fieldset',
  17. '#title' => t('Flag lists operations'),
  18. '#collapsible' => FALSE,
  19. '#collapsed' => FALSE,
  20. );
  21. $form['flo']['force_single'] = array(
  22. '#type' => 'checkbox',
  23. '#title' => t('Force single'),
  24. '#default_value' => $this->options['flo']['force_single'],
  25. '#description' => t('Check this box to restrict selection to a single value.'),
  26. );
  27. $form['flo']['operation'] = array(
  28. '#type' => 'radios',
  29. '#title' => t('Operation'),
  30. '#default_value' => $this->options['flo']['operation'],
  31. '#description' => t('The flag lists operation for this selection.'),
  32. '#options' => array(
  33. 'flag' => 'Add to list',
  34. 'unflag' => 'Remove from list',
  35. ),
  36. '#required' => TRUE,
  37. );
  38. }
  39. /**
  40. * If the view is using a table style, provide a
  41. * placeholder for a "select all" checkbox.
  42. */
  43. function label() {
  44. if ($this->view->style_plugin instanceof views_plugin_style_table && !$this->options['flo']['force_single']) {
  45. return '<!--flag-lists-ops-select-all-->';
  46. }
  47. else {
  48. return parent::label();
  49. }
  50. }
  51. function render($values) {
  52. return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  53. }
  54. function views_form(&$form, &$form_state) {
  55. // The view is empty, abort.
  56. if (empty($this->view->result)) {
  57. return;
  58. }
  59. $form[$this->options['id']] = array(
  60. '#tree' => TRUE,
  61. );
  62. // Do we add or delete?
  63. $operation = array();
  64. if ($this->options['flo']['operation'] == 'flag') {
  65. $operation[0] = 'Add';
  66. $operation[1] = 'to';
  67. }
  68. else {
  69. $operation[0] = 'Remove';
  70. $operation[1] = 'from';
  71. };
  72. // At this point, the query has already been run, so we can access the results
  73. // in order to get the base key value (for example, nid for nodes).
  74. foreach ($this->view->result as $row_index => $row) {
  75. $entity_id = $this->get_value($row);
  76. if ($this->options['flo']['force_single']) {
  77. $form[$this->options['id']][$row_index] = array(
  78. '#type' => 'radio',
  79. '#parents' => array($this->options['id']),
  80. '#return_value' => $entity_id . (isset($row->flag_lists_flags_fid) ? ('-' . $row->flag_lists_flags_fid) : ''),
  81. '#attributes' => array('title' =>
  82. array(t('@oper this item, @entity_id, @direction the @name',
  83. array('@oper' => $operation[0],
  84. '@entity_id' => $entity_id,
  85. '@direction' => $operation[1],
  86. '@name' => variable_get('flag_lists_name','list'))))),
  87. );
  88. }
  89. else {
  90. $form[$this->options['id']][$row_index] = array(
  91. '#type' => 'checkbox',
  92. '#return_value' => $entity_id . (isset($row->flag_lists_flags_fid) ? ('-' . $row->flag_lists_flags_fid) : ''),
  93. '#default_value' => FALSE,
  94. '#attributes' => array('class' => array('flo-select'),
  95. 'title' => array(t('@oper item @entity_id @direction the @name',
  96. array('@oper' => $operation[0],
  97. '@entity_id' => $entity_id,
  98. '@direction' => $operation[1],
  99. '@name' => variable_get('flag_lists_name', 'list'))))),
  100. );
  101. }
  102. }
  103. }
  104. }