eads@2
|
1 <?php |
eads@2
|
2 |
eads@4
|
3 /** |
eads@4
|
4 * Implementation of hook_menu(). |
eads@4
|
5 */ |
eads@2
|
6 function dnd_test_menu() { |
eads@2
|
7 $items = array(); |
eads@2
|
8 $items['dnd-test/library'] = array( |
eads@2
|
9 'title' => 'Drag and drop test library', |
eads@2
|
10 'page callback' => 'dnd_test_library', |
eads@2
|
11 'access arguments' => array('access dnd test library'), |
eads@2
|
12 ); |
eads@2
|
13 return $items; |
eads@2
|
14 } |
eads@2
|
15 |
eads@4
|
16 /** |
eads@4
|
17 * Implementation of hook_perm(). |
eads@4
|
18 */ |
eads@2
|
19 function dnd_test_perm() { |
eads@2
|
20 return array('access dnd test library'); |
eads@2
|
21 } |
eads@2
|
22 |
eads@4
|
23 |
eads@4
|
24 /** |
eads@4
|
25 * Implementation of hook_form_alter(). |
eads@4
|
26 * |
eads@4
|
27 * This demonstrates how to attach Drag and Drop to a given textarea. |
eads@4
|
28 */ |
eads@2
|
29 function dnd_test_form_alter(&$form, &$form_state) { |
eads@2
|
30 if ($form['#id'] == 'node-form' && $form['type']['#value'] == 'page') { |
eads@2
|
31 drupal_add_css(drupal_get_path('module', 'dnd_test') .'/dnd_test.css'); |
eads@2
|
32 $form['body_field']['body']['#dnd-enabled'] = TRUE; |
eads@2
|
33 $form['body_field']['body']['#dnd-settings'] = array( |
eads@2
|
34 'drop_selector' => '#edit-body-dnd-library .drop', |
eads@2
|
35 'url' => 'dnd-test/library', |
eads@2
|
36 ); |
eads@2
|
37 $form['body_field']['body']['#rows'] = 28; |
eads@2
|
38 } |
eads@2
|
39 } |
eads@2
|
40 |
eads@4
|
41 /** |
eads@4
|
42 * Implementation of hook_theme(). |
eads@4
|
43 */ |
eads@4
|
44 function dnd_test_theme() { |
eads@4
|
45 return array( |
eads@4
|
46 'dnd_editor_item' => array( |
eads@4
|
47 'arguments' => array('i' => NULL, 'size' => NULL), |
eads@4
|
48 'template' => 'dnd-editor-item', |
eads@4
|
49 ), |
eads@4
|
50 'dnd_library_item' => array( |
eads@4
|
51 'arguments' => array('i' => NULL), |
eads@4
|
52 'template' => 'dnd-library-item', |
eads@4
|
53 ), |
eads@4
|
54 ); |
eads@4
|
55 } |
eads@4
|
56 |
eads@4
|
57 /** |
eads@4
|
58 * Page call back that returns some JSON |
eads@4
|
59 */ |
eads@2
|
60 function dnd_test_library() { |
eads@4
|
61 $page = ($_GET['page']) ? $_GET['page'] : 1; |
eads@4
|
62 $test_library = dnd_test_generate_library($page); |
eads@4
|
63 return drupal_json(array( |
eads@4
|
64 'header' => '<h3>'. t('Test library: Page @page', array('@page' => $page)) .'</h3>', |
eads@4
|
65 'library' => $test_library['library'], |
eads@4
|
66 'editor_representations' => $test_library['editor_representations'], |
eads@4
|
67 'footer' => '<div class="pager">'. l(t('1'), 'dnd-test/library') . ' '. l(t('2'), 'dnd-test/library', array('query' => array('page' => 2))) .'</div>', |
eads@4
|
68 )); |
eads@4
|
69 } |
eads@2
|
70 |
eads@4
|
71 /** |
eads@4
|
72 * Create contrived output |
eads@4
|
73 */ |
eads@4
|
74 function dnd_test_generate_library($page = 1, $limit = 5) { |
eads@4
|
75 $start = ($page * $limit) - ($limit); |
eads@4
|
76 $end = $page * $limit; |
eads@4
|
77 |
eads@4
|
78 $library = ''; |
eads@4
|
79 $editor_representations = array(); |
eads@4
|
80 for ($i=$start + 1; $i < $end + 1; $i++) { |
eads@4
|
81 $library .= theme('dnd_library_item', $i); |
eads@4
|
82 $editor_representations += dnd_editor_items($i); |
eads@4
|
83 } |
eads@4
|
84 return array( |
eads@4
|
85 'library' => $library, |
eads@4
|
86 'editor_representations' => $editor_representations, |
eads@2
|
87 ); |
eads@4
|
88 } |
eads@4
|
89 |
eads@4
|
90 /** |
eads@4
|
91 * Theme wrapper that spins out multiple library representations for a given |
eads@4
|
92 * editor representation. This is because we want to demonstrate how to allow |
eads@4
|
93 * for multiple versions (i.e. different sizes) of a single item |
eads@4
|
94 */ |
eads@4
|
95 function dnd_editor_items($i) { |
eads@4
|
96 $item = array(); |
eads@4
|
97 foreach(array(t('S'), t('M'), t('L')) as $size) { |
eads@4
|
98 $item['item-'. $i .'-'. $size] = theme('dnd_editor_item', $i, $size); |
eads@2
|
99 } |
eads@4
|
100 return $item; |
eads@2
|
101 } |
eads@4
|
102 |
eads@4
|
103 /** |
eads@4
|
104 * Completely contrived edit item theme function |
eads@4
|
105 */ |
eads@4
|
106 function template_preprocess_dnd_library_item(&$variables) { |
eads@4
|
107 $i = $variables['i']; |
eads@9
|
108 |
eads@9
|
109 if ($i % 3 == 0) { |
eads@9
|
110 $img = 3; |
eads@9
|
111 } |
eads@9
|
112 else if ($i % 2 == 0) { |
eads@9
|
113 $img = 2; |
eads@9
|
114 } |
eads@9
|
115 else { |
eads@9
|
116 $img = 1; |
eads@9
|
117 } |
eads@9
|
118 |
eads@9
|
119 $variables['image'] = theme('image', drupal_get_path('module', 'dnd_test') .'/img/item-'. $img .'-thumb.jpg'); |
eads@4
|
120 $variables['title'] = t('Lorem Ipsum @count', array('@count' => $i)); |
eads@4
|
121 $variables['date'] = 'Feb 18 2009'; |
eads@4
|
122 $variables['author'] = 'David Eads'; |
eads@4
|
123 foreach(array(t('S'), t('M'), t('L')) as $size) { |
eads@4
|
124 $sizes[] = l('<span>'. $size .'</span>', '', array( |
eads@4
|
125 'html' => TRUE, |
eads@4
|
126 'attributes' => array( |
eads@4
|
127 'class' => 'drop size-'. $size, |
eads@4
|
128 'id' => 'item-'. $i .'-'. $size, |
eads@4
|
129 ), |
eads@4
|
130 'query' => array('dnd_id' => 'item-'. $i .'-'. $size), |
eads@4
|
131 )); |
eads@4
|
132 } |
eads@4
|
133 $variables['sizes'] = '<ul><li>'. implode('</li><li>', $sizes) .'</li></ul>'; |
eads@4
|
134 } |
eads@4
|
135 |
eads@4
|
136 |
eads@4
|
137 function template_preprocess_dnd_editor_item(&$variables) { |
eads@4
|
138 list($i, $size) = array($variables['i'], $variables['size']); |
eads@9
|
139 |
eads@9
|
140 if ($i % 3 == 0) { |
eads@9
|
141 $img = 3; |
eads@9
|
142 } |
eads@9
|
143 else if ($i % 2 == 0) { |
eads@9
|
144 $img = 2; |
eads@9
|
145 } |
eads@9
|
146 else { |
eads@9
|
147 $img = 1; |
eads@9
|
148 } |
eads@9
|
149 $variables['image'] = theme('image', drupal_get_path('module', 'dnd_test') .'/img/item-'. $img .'-'. $size .'.jpg'); |
eads@4
|
150 } |