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@28
|
10 'page callback' => 'dnd_test_dnd_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 $form['body_field']['body']['#dnd-enabled'] = TRUE; |
eads@2
|
31 $form['body_field']['body']['#dnd-settings'] = array( |
eads@2
|
32 'drop_selector' => '#edit-body-dnd-library .drop', |
eads@28
|
33 'url' => 'dnd-test/library/?json', |
eads@2
|
34 ); |
eads@2
|
35 $form['body_field']['body']['#rows'] = 28; |
eads@2
|
36 } |
eads@2
|
37 } |
eads@2
|
38 |
eads@4
|
39 /** |
eads@4
|
40 * Implementation of hook_theme(). |
eads@4
|
41 */ |
eads@4
|
42 function dnd_test_theme() { |
eads@4
|
43 return array( |
eads@4
|
44 'dnd_editor_item' => array( |
eads@4
|
45 'arguments' => array('i' => NULL, 'size' => NULL), |
eads@4
|
46 'template' => 'dnd-editor-item', |
eads@4
|
47 ), |
eads@4
|
48 'dnd_library_item' => array( |
eads@4
|
49 'arguments' => array('i' => NULL), |
eads@4
|
50 'template' => 'dnd-library-item', |
eads@4
|
51 ), |
eads@17
|
52 'dnd_library_preview' => array( |
eads@17
|
53 'arguments' => array('i' => NULL), |
eads@17
|
54 'template' => 'dnd-library-preview', |
eads@17
|
55 ), |
eads@4
|
56 ); |
eads@4
|
57 } |
eads@4
|
58 |
eads@4
|
59 /** |
eads@28
|
60 * Overloaded page callback. |
eads@4
|
61 */ |
eads@28
|
62 function dnd_test_dnd_library($element = NULL) { |
eads@28
|
63 $page = (array_key_exists('page', $_GET)) ? $_GET['page'] : 1; |
eads@28
|
64 drupal_add_css(drupal_get_path('module', 'dnd_test') .'/dnd_test.css'); |
eads@28
|
65 drupal_json(dnd_test_generate_library($page)); |
eads@4
|
66 } |
eads@2
|
67 |
eads@4
|
68 /** |
eads@4
|
69 * Create contrived output |
eads@4
|
70 */ |
eads@24
|
71 function dnd_test_generate_library($page = 1, $limit = 8) { |
eads@4
|
72 $start = ($page * $limit) - ($limit); |
eads@4
|
73 $end = $page * $limit; |
eads@4
|
74 |
eads@28
|
75 $library = theme('dnd_library_header', $page); |
eads@4
|
76 $editor_representations = array(); |
eads@17
|
77 $library_previews = array(); |
eads@4
|
78 for ($i=$start + 1; $i < $end + 1; $i++) { |
eads@4
|
79 $library .= theme('dnd_library_item', $i); |
eads@4
|
80 $editor_representations += dnd_editor_items($i); |
eads@17
|
81 $library_previews['dnd-test-'. $i] = theme('dnd_library_preview', $i); |
eads@4
|
82 } |
eads@4
|
83 return array( |
eads@4
|
84 'library' => $library, |
eads@4
|
85 'editor_representations' => $editor_representations, |
eads@17
|
86 'library_previews' => $library_previews, |
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@14
|
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@24
|
98 $item[$i .'-'. $size] = array( |
eads@24
|
99 'body' => theme('dnd_editor_item', $i, $size), |
eads@24
|
100 'title' => 'Item '. $i .'-S', |
eads@24
|
101 ); |
eads@2
|
102 } |
eads@4
|
103 return $item; |
eads@2
|
104 } |
eads@4
|
105 |
eads@4
|
106 /** |
eads@17
|
107 * Completely contrived library preview theme function |
eads@17
|
108 */ |
eads@17
|
109 function template_preprocess_dnd_library_preview(&$variables) { |
eads@17
|
110 template_preprocess_dnd_library_item($variables); |
eads@17
|
111 |
eads@17
|
112 $variables['id'] = 'dnd-preview-'. $variables['i']; |
eads@21
|
113 $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
|
114 |
eads@21
|
115 $variables['description'] = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'; |
eads@17
|
116 } |
eads@17
|
117 |
eads@17
|
118 |
eads@17
|
119 /** |
eads@4
|
120 * Completely contrived edit item theme function |
eads@4
|
121 */ |
eads@4
|
122 function template_preprocess_dnd_library_item(&$variables) { |
eads@12
|
123 global $_SERVER; |
eads@12
|
124 |
eads@4
|
125 $i = $variables['i']; |
eads@9
|
126 |
eads@9
|
127 if ($i % 3 == 0) { |
eads@9
|
128 $img = 3; |
eads@9
|
129 } |
eads@9
|
130 else if ($i % 2 == 0) { |
eads@9
|
131 $img = 2; |
eads@9
|
132 } |
eads@9
|
133 else { |
eads@9
|
134 $img = 1; |
eads@9
|
135 } |
eads@9
|
136 |
eads@17
|
137 $variables['img_num'] = $img; |
eads@17
|
138 $variables['id'] = 'dnd-test-'. $i; |
eads@14
|
139 $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
|
140 $variables['title'] = t('Lorem Ipsum @count', array('@count' => $i)); |
eads@4
|
141 $variables['date'] = 'Feb 18 2009'; |
eads@4
|
142 $variables['author'] = 'David Eads'; |
eads@4
|
143 foreach(array(t('S'), t('M'), t('L')) as $size) { |
eads@14
|
144 $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
|
145 } |
eads@4
|
146 $variables['sizes'] = '<ul><li>'. implode('</li><li>', $sizes) .'</li></ul>'; |
eads@4
|
147 } |
eads@4
|
148 |
eads@4
|
149 function template_preprocess_dnd_editor_item(&$variables) { |
eads@4
|
150 list($i, $size) = array($variables['i'], $variables['size']); |
eads@9
|
151 |
eads@9
|
152 if ($i % 3 == 0) { |
eads@9
|
153 $img = 3; |
eads@9
|
154 } |
eads@9
|
155 else if ($i % 2 == 0) { |
eads@9
|
156 $img = 2; |
eads@9
|
157 } |
eads@9
|
158 else { |
eads@9
|
159 $img = 1; |
eads@9
|
160 } |
eads@23
|
161 $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
|
162 } |
eads@20
|
163 |
eads@20
|
164 function template_preprocess_dnd_library_header(&$variables) {} |