comparison dnd_test/dnd_test.module @ 4:c2eb995212bf

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