| 1 |
<?php
|
| 2 |
// $Id:
|
| 3 |
|
| 4 |
/**
|
| 5 |
* A Link to Site Module for Dog-park-USA.
|
| 6 |
*/
|
| 7 |
|
| 8 |
function link_to_us_perm() {
|
| 9 |
return array('manage link_to_us');
|
| 10 |
}
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_menu
|
| 14 |
*/
|
| 15 |
function link_to_us_menu($may_cache) {
|
| 16 |
$access = user_access('manage link_to_us');
|
| 17 |
$items = array();
|
| 18 |
|
| 19 |
if ($may_cache) {
|
| 20 |
$items[] = array(
|
| 21 |
'path' => 'link_to',
|
| 22 |
'title' => t('Link to page'),
|
| 23 |
'callback' => 'link_to_us_controller',
|
| 24 |
'access' => user_access('access content'),
|
| 25 |
'type' => MENU_CALLBACK
|
| 26 |
);
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'admin/settings/link_to_us',
|
| 29 |
'title' => t('Link To Us'),
|
| 30 |
'callback' => 'drupal_get_form',
|
| 31 |
'callback arguments' => array('link_to_us_admin_settings'),
|
| 32 |
'description' => t('Add a link_to_us link to pages.'),
|
| 33 |
'access' => $access,
|
| 34 |
);
|
| 35 |
$items[] = array(
|
| 36 |
'path' => 'admin/settings/link_to_us/images',
|
| 37 |
'title' => t('Images'),
|
| 38 |
'callback' => 'drupal_get_form',
|
| 39 |
'callback arguments' => array('link_to_us_images_form'),
|
| 40 |
'access' => $access,
|
| 41 |
'type' => MENU_LOCAL_TASK
|
| 42 |
);
|
| 43 |
$items[] = array(
|
| 44 |
'path' => 'admin/settings/link_to_us/list',
|
| 45 |
'title' => t('Edit link_to_us settings'),
|
| 46 |
'access' => $access,
|
| 47 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 48 |
'weight' => -10
|
| 49 |
);
|
| 50 |
$items[] = array(
|
| 51 |
'path' => 'admin/settings/link_to_us/delete',
|
| 52 |
'title' => t('Delete banner'),
|
| 53 |
'callback' => 'drupal_get_form',
|
| 54 |
'callback arguments' => array('link_to_us_delete_form'),
|
| 55 |
'access' => $access,
|
| 56 |
'type' => MENU_CALLBACK,
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
return $items;
|
| 61 |
}
|
| 62 |
|
| 63 |
function link_to_us_add_link() {
|
| 64 |
$path = 'link_to/'. $_GET['q'];
|
| 65 |
return theme('link_to_us_link', $path);
|
| 66 |
}
|
| 67 |
|
| 68 |
function theme_link_to_us_link($path) {
|
| 69 |
return l(t('Share this page'), $path, array('title' => 'Add a link to this page'));
|
| 70 |
}
|
| 71 |
|
| 72 |
function link_to_us_link($type, $node = NULL, $teaser = FALSE) {
|
| 73 |
$links = array();
|
| 74 |
$types = variable_get('link_to_us_nodes', NULL);
|
| 75 |
if ($type == 'node' AND !$types[$node->type] == 0) {
|
| 76 |
$links['link_to_us'] = array(
|
| 77 |
'title' => t('Share this page'),
|
| 78 |
'href' => 'link_to/node/' .$node->nid,
|
| 79 |
'attributes' => array('class' => 'link_to_us', 'title' => 'Add a link to this page', 'rel' => 'nofollow')
|
| 80 |
);
|
| 81 |
}
|
| 82 |
return $links;
|
| 83 |
}
|
| 84 |
|
| 85 |
//Much of this banner upload and admin code was borrowed from the user_badges module. Many thanks for paving the way.
|
| 86 |
|
| 87 |
function link_to_us_admin_settings() {
|
| 88 |
|
| 89 |
$form['link_to_us'] = array(
|
| 90 |
'#type' => 'fieldset',
|
| 91 |
'#title' => t('Link To Us Settings'),
|
| 92 |
'#prefix' => t('Select how you want the link_to_us module to behave. To upload or delete images, go <a href="@here">here</a>.<br />All uploaded images will be used as link banners.', array('@here' => url('admin/settings/link_to_us/images'))),
|
| 93 |
'#collapsible' => 0,
|
| 94 |
'#collapsed' => 0,
|
| 95 |
);
|
| 96 |
$form['link_to_us']['link_to_us_header'] = array(
|
| 97 |
'#type' => 'textarea',
|
| 98 |
'#title' => 'Link page header',
|
| 99 |
'#default_value' => variable_get('link_to_us_header', ''),
|
| 100 |
'#description' => 'This text will appear at the top of the Link To Site page.',
|
| 101 |
);
|
| 102 |
$form['link_to_us']['link_to_us_textlink'] = array(
|
| 103 |
'#type' => 'checkbox',
|
| 104 |
'#title' => 'Show text only link',
|
| 105 |
'#default_value' => variable_get('link_to_us_textlink', NULL),
|
| 106 |
'#description' => 'Show an optional text only link. Will use the title of the node, category, or the Site name as needed.',
|
| 107 |
);
|
| 108 |
$form['link_to_us']['link_to_us_nodes'] = array(
|
| 109 |
'#type' => 'checkboxes',
|
| 110 |
'#title' => t('Node Types'),
|
| 111 |
'#default_value' => variable_get('link_to_us_nodes', NULL),
|
| 112 |
'#options' => array_map('check_plain', node_get_types('names')),
|
| 113 |
'#description' => t('A list of node types you want to link_to links placed in the links section.'),
|
| 114 |
'#required' => TRUE,
|
| 115 |
);
|
| 116 |
|
| 117 |
return system_settings_form($form);
|
| 118 |
}
|
| 119 |
|
| 120 |
function link_to_us_image_selects() {
|
| 121 |
$selects = array();
|
| 122 |
$dir = file_create_path('link_banners');
|
| 123 |
$files = file_scan_directory($dir, '.*\.(gif|jpg|jpeg|png)', array('.', '..', 'CVS'), 0, FALSE);
|
| 124 |
foreach ($files as $file) {
|
| 125 |
$selects[$file->filename] = theme('image', $file->filename, $file->filename, $file->filename);
|
| 126 |
}
|
| 127 |
return $selects;
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Define a form to upload the banner images.
|
| 132 |
*/
|
| 133 |
function link_to_us_images_form() {
|
| 134 |
$form = array('#skip_duplicate_check' => TRUE);
|
| 135 |
if (module_exists('upload')) {
|
| 136 |
$form['new']['upload'] = array('#type' => 'file', '#title' => t('Upload image'), '#size' => 40);
|
| 137 |
$form['new']['attach'] = array('#type' => 'submit', '#value' => t('Upload'));
|
| 138 |
}
|
| 139 |
else {
|
| 140 |
drupal_set_message(t('Upload of images requires the upload module to be enabled.'), 'error');
|
| 141 |
}
|
| 142 |
|
| 143 |
$form['#attributes']['enctype'] = 'multipart/form-data';
|
| 144 |
|
| 145 |
$selects = link_to_us_image_selects();
|
| 146 |
if (count($selects)) {
|
| 147 |
$form['images'] = array('#tree' => TRUE);
|
| 148 |
foreach ($selects as $imagepath => $imageimg) {
|
| 149 |
$form['images'][$imagepath] = array(
|
| 150 |
'#type' => 'checkbox',
|
| 151 |
'#title' => $imageimg,
|
| 152 |
'#return_value' => 1,
|
| 153 |
'#default_value' => FALSE,
|
| 154 |
'#description' => check_plain($imagepath),
|
| 155 |
);
|
| 156 |
}
|
| 157 |
$form['delete_image'] = array(
|
| 158 |
'#type' => 'submit',
|
| 159 |
'#value' => t('Delete'),
|
| 160 |
);
|
| 161 |
}
|
| 162 |
return $form;
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Validate the submission.
|
| 167 |
*
|
| 168 |
* Check whether:
|
| 169 |
* Delete has been chosen AND a checkbox has been selected
|
| 170 |
* OR
|
| 171 |
* Upload has been chosen AND the file upload form is not empty.
|
| 172 |
*/
|
| 173 |
function link_to_us_images_form_validate($form_id, $form_values) {
|
| 174 |
if ($form_values['op'] == t('Upload') && file_check_upload('upload') === FALSE ) {
|
| 175 |
form_set_error('upload', t('Please enter the filename of an image to upload.'));
|
| 176 |
}
|
| 177 |
else if ($form_values['op'] == t('Delete')) {
|
| 178 |
if (count(array_filter($form_values['images'])) == 0) {
|
| 179 |
form_set_error('images', t('Please select images to delete.'));
|
| 180 |
}
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
function link_to_us_images_form_submit($form_id, $form_values) {
|
| 185 |
$op = $form_values['op'];
|
| 186 |
// Save uploaded files
|
| 187 |
if ($op == t('Upload')) {
|
| 188 |
$dir = file_create_path('link_banners');
|
| 189 |
$is_writable = file_check_directory($dir, 1);
|
| 190 |
if ($is_writable) {
|
| 191 |
if( $source = file_check_upload('upload')) {
|
| 192 |
// Security measure to prevent exploit of file.php.png
|
| 193 |
$source->filename = upload_munge_filename($source->filename);
|
| 194 |
if ($file = file_save_upload($source, $dir)) {
|
| 195 |
if (image_get_info($file->filepath)) {
|
| 196 |
drupal_set_message(t('New image saved.'));
|
| 197 |
} else {
|
| 198 |
file_delete($file->filepath);
|
| 199 |
drupal_set_message('Uploaded file does not appear to be a valid image file. Please try again.');
|
| 200 |
}
|
| 201 |
}
|
| 202 |
}
|
| 203 |
}
|
| 204 |
}
|
| 205 |
else if ($op == t('Delete')) {
|
| 206 |
foreach ($form_values['images'] as $path => $is_removed) {
|
| 207 |
if ($is_removed) {
|
| 208 |
$to_delete[] = $path;
|
| 209 |
}
|
| 210 |
}
|
| 211 |
if (is_array($to_delete)) {
|
| 212 |
link_to_us_image_delete($to_delete);
|
| 213 |
}
|
| 214 |
}
|
| 215 |
}
|
| 216 |
|
| 217 |
function link_to_us_image_delete($to_delete) {
|
| 218 |
foreach ($to_delete as $path) {
|
| 219 |
if (file_check_location($path, file_create_path('banners'))) {
|
| 220 |
file_delete($path);
|
| 221 |
}
|
| 222 |
}
|
| 223 |
}
|
| 224 |
|
| 225 |
function link_to_us_controller() {
|
| 226 |
|
| 227 |
$i = 1;
|
| 228 |
$go = 1;
|
| 229 |
$args = array();
|
| 230 |
while ($go) {
|
| 231 |
$arg = arg($i);
|
| 232 |
if (!empty($arg)) {
|
| 233 |
$args[] = $arg;
|
| 234 |
} else {
|
| 235 |
$go = 0;
|
| 236 |
}
|
| 237 |
$i++;
|
| 238 |
}
|
| 239 |
|
| 240 |
$images = link_to_us_image_selects();
|
| 241 |
if ($args[0] == 'node' AND is_numeric($args[1])) {
|
| 242 |
$url = implode("/", $args);
|
| 243 |
$path = drupal_get_path_alias($url);
|
| 244 |
$title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $args[1]));
|
| 245 |
} else if ($args[0] == 'taxonomy' AND $args[1] == 'term' and is_numeric($args[2])) {
|
| 246 |
$url = implode("/", $args);
|
| 247 |
$path = drupal_get_path_alias($url);
|
| 248 |
$term = taxonomy_get_term($args[2]);
|
| 249 |
$title = $term->name .' Dog Parks';
|
| 250 |
} else {
|
| 251 |
$url = "/";
|
| 252 |
$path = drupal_get_path_alias($url);
|
| 253 |
$title = variable_get('site_name', 'Drupal');
|
| 254 |
}
|
| 255 |
//Make and absolute path since this will be displayed on other sites.
|
| 256 |
$path = url($path, NULL, NULL, TRUE);
|
| 257 |
if (variable_get('link_to_us_textlink', NULL)) {
|
| 258 |
$links[] = array(
|
| 259 |
'link' => '<a href="'. $path .'" title="'. $title .'">'. $title .'</a>',
|
| 260 |
'banner' => '',
|
| 261 |
);
|
| 262 |
}
|
| 263 |
foreach ($images as $imagepath => $image) {
|
| 264 |
$links[] = array(
|
| 265 |
'link' => '<a href="'. $path .'" title="'. $title .'"><img src="'. url($imagepath, NULL, NULL, TRUE) .'" alt="'. $title .'" title="'. $title .'"></a>',
|
| 266 |
'banner' => '<img src="'. url($imagepath, NULL, NULL, TRUE) .'" alt="'. $title .'" title="'. $title .'">',
|
| 267 |
);
|
| 268 |
}
|
| 269 |
|
| 270 |
return theme('link_to_us_banners', $links);
|
| 271 |
}
|
| 272 |
|
| 273 |
function theme_link_to_us_banners($links) {
|
| 274 |
$output = '<div class="content"><p>'. variable_get('link_to_us_header', '') .'</p></div>';
|
| 275 |
foreach ($links as $link) {
|
| 276 |
if (empty($link['banner'])){
|
| 277 |
$output .= '<div class="banner-help">Paste HTML for a Text-Only link:</div>';
|
| 278 |
$output .= '<div class="banner-link">'. $link['link'] .'</div>';
|
| 279 |
$output .= '<input id="linkurl" type="text" class="link_input" tabindex="400" value="'. check_plain($link['link']) .'"/>';
|
| 280 |
} else {
|
| 281 |
$output .= '<div class="banner-help">Paste HTML for this Banner as a Link:</div>';
|
| 282 |
$output .= '<div class="banner-link">'. $link['banner'] .'</div>';
|
| 283 |
$output .= '<input id="linkurl" type="text" class="link_input" tabindex="400" value="'. check_plain($link['link']) .'"/>';
|
| 284 |
}
|
| 285 |
}
|
| 286 |
|
| 287 |
return $output;
|
| 288 |
}
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|