/[drupal]/contributions/modules/random_images/random_images.module
ViewVC logotype

Diff of /contributions/modules/random_images/random_images.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Wed Oct 31 20:28:27 2007 UTC revision 1.1.2.1, Wed Nov 28 04:51:27 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  /**  /**
4   * Display help and module information   * Display help and module information
5   * @param section which section of the site we're displaying help   * @param section which section of the site we're displaying help
6   * @return help text for section   * @return help text for section
7   */   */
8  function random_images_help($section='') {  function random_images_help($section='') {
9    
10    $output = '';    $output = '';
11    
12    switch ($section) {    switch ($section) {
13      case "admin/modules#description":      case "admin/modules#description":
14        $output = t("Random Image Blocks");        $output = t("Random Image Blocks");
15        break;        break;
16    }    }
17    
18    return $output;    return $output;
19  } // function random_images_help  } // function random_images_help
20    
21    
22    
23  /**  /**
24   * Valid permissions for this module   * Valid permissions for this module
25   * @return array An array of valid permissions for this module   * @return array An array of valid permissions for this module
26   */   */
27  function random_images_perm() {  function random_images_perm() {
28    return array('administer random images');    return array('administer random images');
29  } // function random_images_perm()  } // function random_images_perm()
30    
31    
32    
33  /**  /**
34   * Generate HTML for the block   * Generate HTML for the block
35   * @param op the operation from the URL   * @param op the operation from the URL
36   * @param delta offset   * @param delta offset
37   * @returns block HTML   * @returns block HTML
38   */   */
39  function random_images_block($op='list', $delta=0) {  function random_images_block($op='list', $delta=0) {
40    // listing of blocks, such as on the admin/block page    // listing of blocks, such as on the admin/block page
41    if ($op == "list") {    if ($op == "list") {
42    
43                  $result = db_query('SELECT * FROM {random_image_blocks}');                  $result = db_query('SELECT * FROM {random_image_blocks}');
44                  while ($random_block = db_fetch_object($result)) {                  while ($random_block = db_fetch_object($result)) {
45                                  $block[$random_block->bid]["info"] = 'Random Images: '.$random_block->name;                                  $block[$random_block->bid]["info"] = 'Random Images: '.$random_block->name;
46                  }                  }
47    
48      return $block;      return $block;
49    
50    }    }
51          elseif ($op == 'view') {          elseif ($op == 'view') {
52            $output = '';            $output = '';
53                  $blocks = Array();                  $blocks = Array();
54    
55                  // Grab all random block parameters                  // Grab all random block parameters
56                  $result = db_query('SELECT * FROM {random_image_blocks}');                  $result = db_query('SELECT * FROM {random_image_blocks}');
57                  while ($block = db_fetch_object($result)) {                  while ($block = db_fetch_object($result)) {
58                          $blocks[$block->bid] = $block;                          $blocks[$block->bid] = $block;
59                  }                  }
60    
61                  $image_urls = Array();                  $image_urls = Array();
62    
63                  $path = $blocks[$delta]->path;                  // Trim any user-supplied trailing slash
64                  $count = $blocks[$delta]->count;                  $path = rtrim($blocks[$delta]->path,'/\\');
65                    $count = $blocks[$delta]->count;
66                  chdir($_SERVER['DOCUMENT_ROOT']);  
67                    // Double check that $path is a real directory
68                  $images = glob($path."*.{jpg,gif,png,jpeg,jpe}", GLOB_BRACE);                  if (!is_dir($path)) return NULL;
69                  $image_count = count($images);  
70                    // Get the list of matching files
71                  // Limit number of images displayed to no more than the number in folder.                  $images = glob($path . "/*.{jpg,gif,png,jpeg,jpe}", GLOB_BRACE);
72                  if ($image_count < $count) $count = $image_count;                  $image_count = count($images);
73    
74                  // Pick at random the specified number of unique image paths                  // Limit number of images displayed to no more than the number in folder.
75                  for ($index = 0; $index < $count; $index++)                  if ($image_count < $count) $count = $image_count;
76                  {  
77                          do {                  // Pick at random the specified number of unique image paths
78                                  $item = rand(0, $image_count - 1);                  for ($index = 0; $index < $count; $index++)
79                          } while (in_array($images[$item], $image_urls));                  {
80                            do {
81                          // replace any windows slashes in the paths.                                  $item = rand(0, $image_count - 1);
82                          $clean_url = preg_replace('/\\+/','/',$images[$item]);                          } while (in_array($images[$item], $image_urls));
83    
84                          $image_urls[] = $clean_url;                          // replace any windows slashes in the paths.
85                  }                          // also remove the document root if present to form a valid URL
86                            $clean_url = preg_replace('/\\+/','/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $images[$item]));
87                  $block = theme('random_image_block', '', $image_urls);  
88                            $image_urls[] = $clean_url;
89          return $block;                  }
90    }  
91  } // end random_images_block                  $block = theme('random_image_block', '', $image_urls);
92    
93  /**          return $block;
94   * Themable function for block container and contents    }
95   * @param subject optional title of the block  } // end random_images_block
96   * @param image_urls array of image urls for this block  
97   * @returns image container with images  /**
98   */   * Themable function for block container and contents
99  function theme_random_image_block($subject, $image_urls)   * @param subject optional title of the block
100  {   * @param image_urls array of image urls for this block
101    $block['subject'] = $subject;   * @returns image container with images
102     */
103          $content = "<div class='random_image_container'>";  function theme_random_image_block($subject, $image_urls)
104          foreach($image_urls as $image_url) {  {
105      $block['subject'] = $subject;
106                  $content .= theme('random_image_image', $image_url);  
107            $content = "<div class='random_image_container'>";
108          }          foreach($image_urls as $image_url) {
109          $content .= "</div>";  
110          $block['content'] = $content;                  $content .= theme('random_image_image', $image_url);
111    
112    return $block;          }
113  }          $content .= "</div>";
114            $block['content'] = $content;
115  /**  
116   * Themable function for each image    return $block;
117   * @param image_url url of a single image  }
118   * @returns a single image's markup  
119   */  /**
120  function theme_random_image_image($image_url)   * Themable function for each image
121  {   * @param image_url url of a single image
122          $output = "";   * @returns a single image's markup
123          $base_path = base_path();   */
124    function theme_random_image_image($image_url)
125          $output .= "<img class=\"random_image_image\" src='$base_path/$image_url' />";  {
126            $output = "";
127          return $output;  
128  }          $output .= "<img class=\"random_image_image\" src='$image_url' />";
129    
130  /**          return $output;
131   * Implementation of hook_menu().  }
132   */  
133  function random_images_menu($may_cache) {  /**
134    $items = array();   * Implementation of hook_menu().
135    if (!$may_cache) {   */
136      $items[] = array('path' => 'admin/settings/random_images',  function random_images_menu($may_cache) {
137        'title' => t('Random Image Blocks'),    $items = array();
138        'description' => t('Create edit and delete blocks containing random images from your filesystem.'),    if (!$may_cache) {
139        'callback' => 'random_images_admin_list_blocks',      $items[] = array('path' => 'admin/settings/random_images',
140        'access' => user_access('administer random images'),        'title' => t('Random Image Blocks'),
141      );        'description' => t('Create edit and delete blocks containing random images from your filesystem.'),
142      $items[] = array('path' => 'admin/settings/random_images/list_blocks',        'callback' => 'random_images_admin_list_blocks',
143        'title' => t('List Blocks'),        'access' => user_access('administer random images'),
144        'callback' => 'random_images_admin_list_blocks',      );
145        'type' => MENU_DEFAULT_LOCAL_TASK,      $items[] = array('path' => 'admin/settings/random_images/list_blocks',
146            'weight' => 1,        'title' => t('List Blocks'),
147        'access' => user_access('administer random images'),        'callback' => 'random_images_admin_list_blocks',
148      );        'type' => MENU_DEFAULT_LOCAL_TASK,
149      $items[] = array('path' => 'admin/settings/random_images/add_block',            'weight' => 1,
150        'title' => t('Add Block'),        'access' => user_access('administer random images'),
151        'callback' => drupal_get_form,      );
152        'callback arguments' => array('random_images_edit_block_form'),      $items[] = array('path' => 'admin/settings/random_images/add_block',
153        'type' => MENU_LOCAL_TASK,        'title' => t('Add Block'),
154        'weight' => 2,        'callback' => drupal_get_form,
155        'access' => user_access('administer random images'),        'callback arguments' => array('random_images_edit_block_form'),
156      );        'type' => MENU_LOCAL_TASK,
157      $items[] = array('path' => 'admin/settings/random_images/edit_block/' . arg(4),        'weight' => 2,
158        'title' => t('Edit Block'),        'access' => user_access('administer random images'),
159        'callback' => drupal_get_form,      );
160        'callback arguments' => array('random_images_edit_block_form', arg(4)),      $items[] = array('path' => 'admin/settings/random_images/edit_block/' . arg(4),
161        'type' => MENU_CALLBACK,        'title' => t('Edit Block'),
162        'access' => user_access('administer random images'),        'callback' => drupal_get_form,
163      );        'callback arguments' => array('random_images_edit_block_form', arg(4)),
164      $items[] = array('path' => 'admin/settings/random_images/delete_block/' . arg(4),        'type' => MENU_CALLBACK,
165        'title' => t('Delete Block'),        'access' => user_access('administer random images'),
166        'callback' => drupal_get_form,      );
167        'callback arguments' => array('random_images_delete_block_form', arg(4)),      $items[] = array('path' => 'admin/settings/random_images/delete_block/' . arg(4),
168        'type' => MENU_CALLBACK,        'title' => t('Delete Block'),
169        'access' => user_access('administer random images'),        'callback' => drupal_get_form,
170      );        'callback arguments' => array('random_images_delete_block_form', arg(4)),
171    }        'type' => MENU_CALLBACK,
172          'access' => user_access('administer random images'),
173    return $items;      );
174  } // end random_images_menu    }
175    
176  function random_images_admin_list_blocks() {    return $items;
177          $output = '';  } // end random_images_menu
178    
179    $result = db_query('SELECT * FROM {random_image_blocks}');  function random_images_admin_list_blocks() {
180            $output = '';
181    while ($block = db_fetch_object($result)) {  
182            $rows[] = array(    $result = db_query('SELECT * FROM {random_image_blocks}');
183                  check_plain($block->name),  
184                  check_plain($block->path),    while ($block = db_fetch_object($result)) {
185                  l(t('Edit'), 'admin/settings/random_images/edit_block/'. $block->bid).' '.l(t('Delete'), 'admin/settings/random_images/delete_block/'. $block->bid),            $rows[] = array(
186            );                  check_plain($block->name),
187    }                  check_plain($block->path),
188    $header = array(t('Name'), t('Path'), t('Operations'));                  l(t('Edit'), 'admin/settings/random_images/edit_block/'. $block->bid).' '.l(t('Delete'), 'admin/settings/random_images/delete_block/'. $block->bid),
189    $output = theme('table', $header, $rows);            );
190    $output .= drupal_render($form);    }
191      $header = array(t('Name'), t('Path'), t('Operations'));
192    return $output;    $output = theme('table', $header, $rows);
193  }    $output .= drupal_render($form);
194    
195  function random_images_edit_block_form($bid = null) {    return $output;
196    if ($bid) {  }
197      $block = db_fetch_object(db_query('SELECT * FROM {random_image_blocks} WHERE bid = %d', $bid));  
198    }  function random_images_edit_block_form($bid = null) {
199          else {    if ($bid) {
200      $block = new StdClass();      $block = db_fetch_object(db_query('SELECT * FROM {random_image_blocks} WHERE bid = %d', $bid));
201    }    }
202            else {
203    $form['bid'] = array('#type' => 'hidden', '#value' => $block->bid);      $block = new StdClass();
204      }
205    $form['name'] = array(  
206    '#type' => 'textfield',    $form['bid'] = array('#type' => 'hidden', '#value' => $block->bid);
207    '#title' => t('Block Name'),  
208    '#default_value' => $block->name,    $form['name'] = array(
209    '#size' => 40,    '#type' => 'textfield',
210    '#description' => t('Name for this block;'),    '#title' => t('Block Name'),
211    );    '#default_value' => $block->name,
212    $form['path'] = array(    '#size' => 40,
213    '#type' => 'textfield',    '#description' => t('Name for this block;'),
214    '#title' => t('Images Path'),    );
215    '#default_value' => $block->path,    $form['path'] = array(
216    '#size' => 40,    '#type' => 'textfield',
217    '#description' => t('System path to random images folder for this block;'),    '#title' => t('Images Path'),
218    );    '#default_value' => $block->path,
219    $form['count'] = array(    '#size' => 40,
220    '#type' => 'textfield',    '#description' => t('System path to random images folder for this block;'),
221    '#title' => t('Image Count'),    );
222    '#default_value' => $block->count,    $form['count'] = array(
223    '#size' => 4,    '#type' => 'textfield',
224    '#description' => t('Number of images to display in block for this block.'),    '#title' => t('Image Count'),
225    );    '#default_value' => $block->count,
226      '#size' => 4,
227    $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));    '#description' => t('Number of images to display in block for this block.'),
228      );
229    return $form;  
230      $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
231  }  
232      return $form;
233  function random_images_edit_block_form_submit($form_id, $form_values) {  
234    if ($form_values['bid']) {  }
235          $path = rtrim($form_values['path'],'/\\') . '/';  
236      db_query("UPDATE {random_image_blocks} SET name = '%s', path = '%s', count=%d WHERE bid = %d", $form_values['name'], $path, $form_values['count'], $form_values['bid']);  function random_images_edit_block_form_validate($form_id, $form_values) {
237      drupal_set_message(t('Random image block updated successfully.'));          // Check that $path is a real directory
238            if (!is_dir($form_values['path'])) {
239    }                  form_set_error('path', t('The specified path does not exist.'));
240          else {          }
241      db_query("INSERT INTO {random_image_blocks} (name, path, count) VALUES ('%s', '%s', %d)", $form_values['name'], $form_values['path'], $form_values['count']);  }
242      drupal_set_message(t('Random image block created successfully.'));  
243    }  function random_images_edit_block_form_submit($form_id, $form_values) {
244      if ($form_values['bid']) {
245    drupal_goto('/admin/settings/random_images/list_blocks');      db_query("UPDATE {random_image_blocks} SET name = '%s', path = '%s', count=%d WHERE bid = %d", $form_values['name'], $form_values['path'], $form_values['count'], $form_values['bid']);
246  }      drupal_set_message(t('Random image block updated successfully.'));
247    
248  function random_images_delete_block_form($bid = null) {    }
249    $block = db_fetch_object(db_query('SELECT * FROM {random_image_blocks} WHERE bid = %d', $bid));          else {
250        db_query("INSERT INTO {random_image_blocks} (name, path, count) VALUES ('%s', '%s', %d)", $form_values['name'], $form_values['path'], $form_values['count']);
251    if ($block) {      drupal_set_message(t('Random image block created successfully.'));
252                          $form = array();    }
253        $form['bid'] = array('#type' => 'value', '#value' => $block->bid);  
254        $form['name'] = array('#type' => 'hidden', '#value' => $block->name);    drupal_goto('/admin/settings/random_images/list_blocks');
255    }
256        return confirm_form($form,  
257                                                                                                          t('Are you sure you want to delete the random image block: %block?', array('%block' => $block->name)),  function random_images_delete_block_form($bid = null) {
258                                                                                                          'admin/settings/random_images');    $block = db_fetch_object(db_query('SELECT * FROM {random_image_blocks} WHERE bid = %d', $bid));
259    }  
260    else {    if ($block) {
261      drupal_not_found();                          $form = array();
262    }        $form['bid'] = array('#type' => 'value', '#value' => $block->bid);
263  }        $form['name'] = array('#type' => 'hidden', '#value' => $block->name);
264    
265  function random_images_delete_block_form_submit($form_id, $form_values)        return confirm_form($form,
266  {                                                                                                          t('Are you sure you want to delete the random image block: %block?', array('%block' => $block->name)),
267    db_query("DELETE FROM {random_image_blocks} WHERE bid = %d", $form_values['bid']);                                                                                                          'admin/settings/random_images');
268    db_query("DELETE FROM {blocks} WHERE module = 'random_images' AND delta = %d", $form_values['bid']);    }
269      else {
270    drupal_set_message(t('Random image block deleted successfully.'));      drupal_not_found();
271      }
272    drupal_goto('/admin/settings/random_images/list_blocks');  }
273  }  
274    function random_images_delete_block_form_submit($form_id, $form_values)
275    {
276      db_query("DELETE FROM {random_image_blocks} WHERE bid = %d", $form_values['bid']);
277      db_query("DELETE FROM {blocks} WHERE module = 'random_images' AND delta = %d", $form_values['bid']);
278    
279      drupal_set_message(t('Random image block deleted successfully.'));
280    
281      drupal_goto('/admin/settings/random_images/list_blocks');
282    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2