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