/[drupal]/contributions/modules/multiselect/multiselect.module
ViewVC logotype

Contents of /contributions/modules/multiselect/multiselect.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Wed Sep 17 19:49:20 2008 UTC (14 months, 1 week ago) by jklsemicolon
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.6: +36 -21 lines
File MIME type: text/x-php
added css to ensure the multiselect description field  takes up whole div width

removed hook perm. For one, it was never being checked, secondly I see no security risk with this module.


added some checking for errors being thrown in the content/admin pages

now includes the description field in the node form.

uses alt tag on button images, so that when images aren't loaded, the buttons are still somewhat usable.
1 <?php
2 // $Id $
3
4 /**
5 * @file
6 * Allows users to select multiple items in an easier way than the normal node-reference widget
7 *
8 * @author Obslogic (Mike Smith aka Lionfish)
9 */
10
11
12 /**
13 * Implementation of hook_help
14 */
15
16 function multiselect_help($section='')
17 {
18 $output = '';
19 switch ($section)
20 {
21 case "admin/help#multiselect":
22 $output = '<p>'.t("Provides an easy to use control for adding multiple items.").'</p>';
23 break;
24 }
25 return $output;
26 }
27
28 /**
29 * Implementation of hook_widget_info.
30 * Specifies the label and that it is a widget for the nodereference field type
31 */
32
33 function multiselect_widget_info()
34 {
35 return array(
36 'multiselect_select' => array(
37 'label' => t('Multiselect List'),
38 'field types' => array('nodereference'),
39 ),
40 );
41 }
42
43
44 /**
45 * Implementation of hook_widget
46 * @todo Doesn't seem to populate list when in 'preview' (see http://drupal.org/node/139595)
47 */
48 function multiselect_widget($op, &$node, $field, &$items) {
49 switch ($op) {
50 case 'prepare form values':
51
52
53 $items_transposed = content_transpose_array_rows_cols($items);
54
55 //to avoid an error being thrown in admin/content/types/<type>/field
56 if(!isset($items_transposed['nid'])){
57 $items_transposed['nid']=array();
58 }
59 $items['default nids'] = $items_transposed['nid'];
60
61 //remove the null item.
62 //it was causing a meaningless entry
63 //(node with nid 0) to be in the selected list.
64 $items['default nids'] = array_diff( $items['default nids'], array(0));
65
66 break;
67
68 case 'form':
69
70 $module_base_path = drupal_get_path('module','multiselect');
71
72 //inserts javascript
73 drupal_add_js($module_base_path . '/multiselect.js');
74
75 //insert CSS (based on http://www.lullabot.com/articles/how_to_properly_add_css_files)
76 //FALSE means it doesn't aggregate: as it's not used much
77 //TODO: good decision?
78 drupal_add_css($module_base_path . '/multiselect.css','module','all',FALSE);
79 $form = array();
80
81 $options = _nodereference_potential_references($field, TRUE);
82
83 foreach ($options as $key => $value) {
84 $options[$key] = _nodereference_item($field, $value, FALSE);
85 }
86
87
88 $selected_options = array();
89 //If there already exists referenced nodes for this field, add them
90 // to the selected options array (by order of their delta)
91 if (is_array($items['default nids']))
92 {
93 foreach($items['default nids'] as $delta => $nid)
94 {
95 $selected_options[$nid] = $options[$nid];
96 }
97 }
98
99 //anything in $alloptions but not in $selectedoptions is "not selected"
100 $not_selected_options = array_diff($options,$selected_options);
101
102 //reassign $options to reflect delta.
103 //note: No new options are created in this list,
104 //but putting selected options first ensures the form is populated in
105 //the order of (thus maintaining) its deltas.
106 $options = $selected_options + $not_selected_options;
107
108 //create the containing form item
109 $form[$field['field_name']] = array(
110 '#type'=>'fieldset',
111 '#description' => t($field['widget']['description']),
112 '#title'=>t($field['widget']['label']),
113 '#tree' => TRUE,
114 '#attributes' => array('class'=>'multiselect'),
115 '#all_options' => $options,
116 '#after_build' => array('multiselect_fix_selected'),
117 );
118
119 //the list of items currently selected
120 $form[$field['field_name']]['selected'] = array(
121 '#title' => t("selected"),
122 '#attributes' => array('class' => "container-inline selected multiselect", 'id' => $field['field_name']),
123 '#type' => 'select',
124 '#weight' => 3,
125 '#options' => $selected_options,
126 '#size' => 10,
127 '#multiple' => TRUE,
128 );
129
130
131
132 //the list of items currently not selected
133 $form[$field['field_name']]['unselected'] = array(
134 '#title' => t("unselected"),
135 '#attributes' => array('class' => "container-inline unselected multiselect", 'id' => $field['field_name']),
136 '#type' => 'select',
137 '#weight' => 0,
138 '#options' => $not_selected_options,
139 '#size' => 10,
140 '#multiple' => TRUE,
141 );
142
143 /*
144 * The buttons to (de)select objects are implemented as field markup
145 * with an associated javascript function onclick
146 */
147
148 //name attribute of the associated select elements
149 $selected_name = $field['field_name'].'[selected][]';
150 $unselected_name = $field['field_name'].'[unselected][]';
151
152 $form[$field['field_name']]['select'] = array(
153 '#type' => 'markup',
154 '#submit'=> false,
155 '#value' =>
156 '<span class="select">
157 <img alt="'.t('select').'"
158 src="'.base_path().$module_base_path.'/images/add.png" '.
159 "onclick='moveSelectedOptions( \"$unselected_name\" , \"$selected_name\" )' /></span>",
160 '#weight' => 1,
161 '#attributes' => array('class' => 'select'),
162 );
163
164 $form[$field['field_name']]['deselect'] = array(
165 '#type' => 'markup',
166 '#submit'=> false,
167 '#value' =>
168 '<span class="deselect">
169 <img alt="'.t('deselect').'"
170 src="'.base_path().$module_base_path.'/images/remove.png"'.
171 "onclick='moveSelectedOptions( \"$selected_name\" , \"$unselected_name\" )' />
172 </span>",
173 '#weight' => 2,
174 );
175 return $form;
176
177 case 'process form values':
178 if ($field['multiple']) {
179
180 //adjust perspective
181 $items['nids'] = $items['selected'];
182 unset($items['selected']);
183 unset($items['unselected']);
184
185 // if nothing selected, make it 'none'
186 if (empty($items['nids'])) {
187 $items['nids'] = array(0 => '0');
188 }
189
190 $items = array_values(content_transpose_array_rows_cols(array('nid' => $items['nids'])));
191 }
192 else {
193 $items[0]['nid'] = $items['nids'];
194 }
195 // Remove the widget's data representation so it isn't saved.
196 // unset($items['nids']);
197 foreach ($items as $delta => $item) {
198 $items[$delta]['error_field'] = $field['field_name'] .'][nids';
199 }
200
201 case 'validate':
202
203 }
204
205 }
206
207
208 /**
209 * Alters the preview form to include the newly added items
210 */
211 function multiselect_fix_selected($field, $edit)
212 {
213
214 if( isset($field['#post']) && !empty($field['#post'])){
215
216 $field_id = $field['selected']['#attributes']['id'];
217
218
219 $selected = $field['selected']['#post'][$field_id]['selected'];
220
221 $field['selected']['#options'] = array();
222 $field['unselected']['#options'] = $field['#all_options'];
223
224 if ( !is_null($selected) ){
225 foreach( $selected as $selected_nid )
226 {
227 $field['selected']['#options'][$selected_nid] = $field['#all_options'][$selected_nid];
228 unset($field['unselected']['#options'][$selected_nid]);
229 }
230 }
231 }
232
233 return $field;
234 }
235
236
237
238
239
240
241 /**
242 * Implementation of hook_nodeapi.
243 * @todo The adding of javascript can be moved to the _widget function.
244 */
245
246 function multiselect_nodeapi(&$node, $op, $teaser, $page)
247 {
248 /* switch ($op)
249 {
250 case 'prepare':
251 }*/
252 }
253

  ViewVC Help
Powered by ViewVC 1.1.2