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 * Implementation of hook_form_alter(). |
eads@4
|
25 * |
eads@4
|
26 * This demonstrates how to attach Drag and Drop to a given textarea. |
eads@4
|
27 */ |
eads@2
|
28 function dnd_test_form_alter(&$form, &$form_state) { |
eads@2
|
29 if ($form['#id'] == 'node-form' && $form['type']['#value'] == 'page') { |
eads@2
|
30 drupal_add_css(drupal_get_path('module', 'dnd_test') .'/dnd_test.css'); |
eads@2
|
31 $form['body_field']['body']['#dnd-enabled'] = TRUE; |
eads@2
|
32 $form['body_field']['body']['#dnd-settings'] = array( |
eads@2
|
33 'drop_selector' => '#edit-body-dnd-library .drop', |
eads@2
|
34 'url' => 'dnd-test/library', |
eads@2
|
35 ); |
eads@2
|
36 $form['body_field']['body']['#rows'] = 28; |
eads@2
|
37 } |
eads@2
|
38 } |
eads@2
|
39 |
eads@4
|
40 /** |
eads@4
|
41 * Implementation of hook_theme(). |
eads@4
|
42 */ |
eads@4
|
43 function dnd_test_theme() { |
eads@4
|
44 return array( |
eads@4
|
45 'dnd_editor_item' => array( |
eads@4
|
46 'arguments' => array('i' => NULL, 'size' => NULL), |
eads@4
|
47 'template' => 'dnd-editor-item', |
eads@4
|
48 ), |
eads@4
|
49 'dnd_library_item' => array( |
eads@4
|
50 'arguments' => array('i' => NULL), |
eads@4
|
51 'template' => 'dnd-library-item', |
eads@4
|
52 ), |
eads@17
|
53 'dnd_library_preview' => array( |
eads@17
|
54 'arguments' => array('i' => NULL), |
eads@17
|
55 'template' => 'dnd-library-preview', |
eads@17
|
56 ), |
eads@20
|
57 'dnd_library_header' => array( |
eads@20
|
58 'arguments' => array('page' => NULL), |
eads@20
|
59 'template' => 'dnd-library-header', |
eads@20
|
60 ), |
eads@4
|
61 ); |
eads@4
|
62 } |
eads@4
|
63 |
eads@4
|
64 /** |
eads@24
|
65 * Page callback that returns some JSON |
eads@4
|
66 */ |
eads@2
|
67 function dnd_test_library() { |
eads@4
|
68 $page = ($_GET['page']) ? $_GET['page'] : 1; |
eads@4
|
69 $test_library = dnd_test_generate_library($page); |
eads@4
|
70 return drupal_json(array( |
eads@20
|
71 'header' => theme('dnd_library_header', $page), |
eads@4
|
72 'library' => $test_library['library'], |
eads@4
|
73 'editor_representations' => $test_library['editor_representations'], |
eads@17
|
74 'library_previews' => $test_library['library_previews'], |
eads@4
|
75 'footer' => '<div class="pager">'. l(t('1'), 'dnd-test/library') . ' '. l(t('2'), 'dnd-test/library', array('query' => array('page' => 2))) .'</div>', |
eads@4
|
76 )); |
eads@4
|
77 } |
eads@2
|
78 |
eads@4
|
79 /** |
eads@4
|
80 * Create contrived output |
eads@4
|
81 */ |
eads@24
|
82 function dnd_test_generate_library($page = 1, $limit = 8) { |
eads@4
|
83 $start = ($page * $limit) - ($limit); |
eads@4
|
84 $end = $page * $limit; |
eads@4
|
85 |
eads@4
|
86 $library = ''; |
eads@4
|
87 $editor_representations = array(); |
eads@17
|
88 $library_previews = array(); |
eads@4
|
89 for ($i=$start + 1; $i < $end + 1; $i++) { |
eads@4
|
90 $library .= theme('dnd_library_item', $i); |
eads@4
|
91 $editor_representations += dnd_editor_items($i); |
eads@17
|
92 $library_previews['dnd-test-'. $i] = theme('dnd_library_preview', $i); |
eads@4
|
93 } |
eads@4
|
94 return array( |
eads@4
|
95 'library' => $library, |
eads@4
|
96 'editor_representations' => $editor_representations, |
eads@17
|
97 'library_previews' => $library_previews, |
eads@2
|
98 ); |
eads@4
|
99 } |
eads@4
|
100 |
eads@4
|
101 /** |
eads@4
|
102 * Theme wrapper that spins out multiple library representations for a given |
eads@14
|
103 * editor representation. This is because we want to demonstrate how to allow |
eads@4
|
104 * for multiple versions (i.e. different sizes) of a single item |
eads@4
|
105 */ |
eads@4
|
106 function dnd_editor_items($i) { |
eads@4
|
107 $item = array(); |
eads@4
|
108 foreach(array(t('S'), t('M'), t('L')) as $size) { |
eads@24
|
109 $item[$i .'-'. $size] = array( |
eads@24
|
110 'body' => theme('dnd_editor_item', $i, $size), |
eads@24
|
111 'title' => 'Item '. $i .'-S', |
eads@24
|
112 ); |
eads@2
|
113 } |
eads@4
|
114 return $item; |
eads@2
|
115 } |
eads@4
|
116 |
eads@4
|
117 /** |
eads@17
|
118 * Completely contrived library preview theme function |
eads@17
|
119 */ |
eads@17
|
120 function template_preprocess_dnd_library_preview(&$variables) { |
eads@17
|
121 template_preprocess_dnd_library_item($variables); |
eads@17
|
122 |
eads@17
|
123 $variables['id'] = 'dnd-preview-'. $variables['i']; |
eads@21
|
124 $variables['image'] = '<img src="http://'. $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'dnd_test') .'/img/item-'. $variables['img_num'] .'-S.jpg" class="drop" width="125" height="94" />'; |
eads@17
|
125 |
eads@21
|
126 $variables['description'] = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'; |
eads@17
|
127 } |
eads@17
|
128 |
eads@17
|
129 |
eads@17
|
130 /** |
eads@4
|
131 * Completely contrived edit item theme function |
eads@4
|
132 */ |
eads@4
|
133 function template_preprocess_dnd_library_item(&$variables) { |
eads@12
|
134 global $_SERVER; |
eads@12
|
135 |
eads@4
|
136 $i = $variables['i']; |
eads@9
|
137 |
eads@9
|
138 if ($i % 3 == 0) { |
eads@9
|
139 $img = 3; |
eads@9
|
140 } |
eads@9
|
141 else if ($i % 2 == 0) { |
eads@9
|
142 $img = 2; |
eads@9
|
143 } |
eads@9
|
144 else { |
eads@9
|
145 $img = 1; |
eads@9
|
146 } |
eads@9
|
147 |
eads@17
|
148 $variables['img_num'] = $img; |
eads@17
|
149 $variables['id'] = 'dnd-test-'. $i; |
eads@14
|
150 $variables['image'] = '<img src="http://'. $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'dnd_test') .'/img/item-'. $img .'-thumb.jpg?dnd_id='. $i .'-M" class="drop" />'; |
eads@4
|
151 $variables['title'] = t('Lorem Ipsum @count', array('@count' => $i)); |
eads@4
|
152 $variables['date'] = 'Feb 18 2009'; |
eads@4
|
153 $variables['author'] = 'David Eads'; |
eads@4
|
154 foreach(array(t('S'), t('M'), t('L')) as $size) { |
eads@14
|
155 $sizes[] = '<img src="http://'. $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'dnd_test') .'/img/icon/'. $size .'.png?dnd_id='. $i .'-'. $size .'" class="drop" />'; |
eads@4
|
156 } |
eads@4
|
157 $variables['sizes'] = '<ul><li>'. implode('</li><li>', $sizes) .'</li></ul>'; |
eads@4
|
158 } |
eads@4
|
159 |
eads@4
|
160 function template_preprocess_dnd_editor_item(&$variables) { |
eads@4
|
161 list($i, $size) = array($variables['i'], $variables['size']); |
eads@9
|
162 |
eads@9
|
163 if ($i % 3 == 0) { |
eads@9
|
164 $img = 3; |
eads@9
|
165 } |
eads@9
|
166 else if ($i % 2 == 0) { |
eads@9
|
167 $img = 2; |
eads@9
|
168 } |
eads@9
|
169 else { |
eads@9
|
170 $img = 1; |
eads@9
|
171 } |
eads@23
|
172 $variables['image'] = theme('image', drupal_get_path('module', 'dnd_test') .'/img/item-'. $img .'-'. $size .'.jpg', '', '', array('class' => 'dnd-dropped', 'id' => 'dnd-id-'. $i)); |
eads@4
|
173 } |
eads@20
|
174 |
eads@20
|
175 function template_preprocess_dnd_library_header(&$variables) {} |