function mysite_icon_help($section) {
switch ($section) {
case 'admin/help#mysite_icon':
$output = t("<p>MySite offers the option to view content selections as a table of image icons. In order to use this feature, you must have a directory writable by your Drupal installation. For most users, this directory will be <em>files/mysite</em>. MySite Icons may not work properly until you configure a storage directory.</p><p>To use the MySite Icons module's features, you must also configure the MySite option <em>Content Browser</em> to the value <em>Display rows of icons</em>.");
return $output;
break;
case 'admin/settings/mysite/icons':
$output = t('<p>MySite offers the option to view content selections as a table of image icons. In order to use this feature, you must have a directory writable by your Drupal installation. For most users, this directory will be <em>files/mysite</em>. If you see an error message below, MySite Icons may not work properly until you configure a storage directory.</p>');
return $output;
break;
}
}
function mysite_icon_menu($may_cache) {
$items = array();
$admin = user_access('administer mysite');
if ($may_cache) {
$items[] = array('path' => 'admin/settings/mysite/icons',
'title' => t('Content icons'),
'description' => t('Manage icons for use with the MySite content browser.'),
'weight' => 6,
'callback' => 'mysite_icon_configure',
'type' => MENU_LOCAL_TASK,
'access' => $admin);
$items[] = array('path' => 'admin/settings/mysite_icon',
'title' => t('MySite Icons'),
'description' => t('Manage icons for use with the MySite content browser.'),
'weight' => 0,
'callback' => 'mysite_icon_admin',
'access' => $admin);
$items[] = array('path' => 'admin/settings/mysite/icons/browse',
'title' => t('Current icons'),
'description' => t('Add icons for use with the MySite content browser.'),
'weight' => -6,
'callback' => 'mysite_icon_current',
'type' => MENU_LOCAL_TASK,
'access' => $admin);
$items[] = array('path' => 'admin/settings/mysite/icons/add',
'title' => t('Add content icon'),
'description' => t('Add icons for use with the MySite content browser.'),
'weight' => 2,
'callback' => 'mysite_icon_add',
'type' => MENU_LOCAL_TASK,
'access' => $admin);
$items[] = array('path' => 'admin/settings/mysite/icons/configure',
'title' => t('Icon settings'),
'description' => t('Default icon settings for the MySite content browser.'),
'weight' => -10,
'callback' => 'mysite_icon_configure',
'type' => MENU_DEFAULT_LOCAL_TASK,
'access' => $admin);
}
else {
$type = MENU_CALLBACK;
if (is_numeric(arg(5))) {
$type = MENU_LOCAL_TASK;
}
$items[] = array('path' => 'admin/settings/mysite/icons/change/'. arg(5),
'title' => t('Change'),
'description' => t('Change content icon.'),
'weight' => 4,
'callback' => 'mysite_icon_change',
'type' => $type,
'access' => $admin);
$items[] = array('path' => 'admin/settings/mysite/icons/delete/'. arg(5),
'title' => t('Delete'),
'description' => t('Delete content icon.'),
'weight' => 6,
'callback' => 'mysite_icon_delete',
'type' => $type,
'access' => $admin);
}
if (arg(0) == 'admin' && arg(2) == 'mysite' && arg(3) == 'icons') {
$types = variable_get('mysite_content', array());
foreach ($types as $type) {
mysite_load_includes('types', $type);
}
}
return $items;
}
function mysite_icon_admin() {
drupal_goto('admin/settings/mysite/icons');
return;
}
function mysite_icon_configure() {
$folder = variable_get('mysite_icon_path', 'mysite');
$path = file_create_path(file_directory_path() .'/'. $folder);
$dir = mysite_icon_folder_check($path);
$output .= drupal_get_form('mysite_icon_settings_form', $path, $folder);
return $output;
}
function mysite_icon_folder_check($path) {
$dir = file_check_directory($path, 1);
if (!$dir) {
drupal_set_message(t('The MySite Icons directory needs to reside in a folder that is writable by Drupal. The recommended path is <b>mysite</b>. ') . l(t('Please check your file upload settings'), 'admin/settings/file-system'), 'error');
}
return $dir;
}
function mysite_icon_settings_form($path, $folder) {
$form = array();
$form['mysite_icon_path'] = array(
'#type' => 'textfield',
'#title' => t('MySite Icons Directory'),
'#size' => 30,
'#maxlength' => 80,
'#default_value' => $folder,
'#required' => TRUE,
'#description' => t('This folder will reside in %path, and is a file system path where the files will be stored. Do not use a leading or trailing slash. This directory has to exist and be writable by Drupal. Changing this location after the site has been in use will cause problems so only change this setting on an existing site if you know what you are doing.', array('%path' => file_directory_path()))
);
$form['mysite_icon_dimensions'] = array(
'#type' => 'textfield',
'#title' => t('Icon maximum dimensions'),
'#default_value' => variable_get('mysite_icon_dimensions', '120x60'),
'#size' => 15, '#maxlength' => 10,
'#description' => t('Maximum dimensions for icons, Width x Height, in pixels.')
);
$form['mysite_icon_file_size'] = array(
'#type' => 'textfield',
'#title' => t('Icon maximum file size'),
'#default_value' => variable_get('mysite_icon_file_size', '32'),
'#size' => 15, '#maxlength' => 10,
'#description' => t('Maximum file size for icons, in kB.')
);
$form['mysite_icon_download'] = array(
'#type' => 'radios',
'#title' => t('Download icons'),
'#options' => array(0 => t('Do not download icons.'), 1 => t('Attempt to download icons.')),
'#default_value' => variable_get('mysite_icon_download', 1),
'#description' => t('Should MySite attempt to download icons from external websites?')
);
return system_settings_form($form);
}
function mysite_icon_current() {
global $base_url;
$folder = variable_get('mysite_icon_path', 'mysite');
$path = file_create_path(file_directory_path() .'/'. $folder);
$dir = mysite_icon_folder_check($path);
if ($dir) {
$header = array(array('data' => t('Id'), 'field' => 'iid', 'sort' => 'asc'), t('Name'), array('data' => t('Type'), 'field' => 'type', 'sort' => 'asc'), array('data' => t('Type Id'), 'field' => 'type_id', 'sort' => 'asc'), array('data' => t('Icon'), 'field' => 'icon', 'sort' => 'asc'), t('Actions'));
$rows = array();
$result = pager_query("SELECT iid, type, type_id, icon FROM {mysite_icon}". tablesort_sql($header), 50);
while ($icon = db_fetch_object($result)) {
$name = module_invoke('mysite_type', $icon->type .'_title', $icon->type_id);
$image = file_create_url($path .'/'. $icon->icon);
$img = theme_image($image, $icon->icon, $icon->icon, $attributes = array('height' => '20'), $getsize = FALSE);
$rows[] = array($icon->iid, check_plain($name), $icon->type, $icon->type_id, l($img, $image, array(), NULL, NULL, FALSE, TRUE), l('change', 'admin/settings/mysite/icons/change/'. $icon->iid) .' | '. l('delete', 'admin/settings/mysite/icons/delete/'. $icon->iid));
}
$output = '<h3>'. t('Current Icons') .'</h3>';
if (count($rows) > 0) {
$output .= theme_table($header, $rows);
$output .= theme_pager(array(), 50);
}
else {
$output = t('No icons found.');
}
}
else {
$output = t('There is an error in the configuration of the MySite Icons upload folder.');
}
return $output;
}
function mysite_icon_add() {
$folder = variable_get('mysite_icon_path', 'mysite');
$path = file_create_path(file_directory_path() .'/'. $folder);
$dir = mysite_icon_folder_check($path);
if ($dir) {
$data = array();
$type = arg(5);
$type_id = arg(6);
$types = variable_get('mysite_content', array());
if (in_array($type, $types) && is_numeric($type_id)) {
$icon = mysite_icon_lookup($type, $type_id);
if ($icon->iid) {
drupal_goto('admin/settings/mysite/icons/change/'. $icon->iid);
}
$name = module_invoke('mysite_type', $type .'_title', $type_id);
$output = '<h3>'. t('Upload icon: ') . check_plain($name) .'</h3>';
$output .= drupal_get_form('mysite_icon_new_form', $type, $type_id);
return $output;
}
else {
foreach ($types as $type) {
$data[$type] = module_invoke('mysite_type', $type);
}
$options = array();
foreach ($data as $key => $value) {
if (!empty($value['options'])) {
for ($i = 0; $i < count($value['options']['type']); $i++) {
if (empty($value['options']['icon'][$i])) {
$k = $value['options']['type'][$i] .'-'. $value['options']['type_id'][$i];
$options[$value['include']][$k] = $value['options']['name'][$i];
}
}
}
}
if (!empty($options)) {
return drupal_get_form('mysite_icon_add_form', $options);
}
else {
return t('No options are currently available.');
}
}
}
else {
return t('There is an error in the configuration of the MySite Icons upload folder.');
}
}
function mysite_icon_new_form($type, $type_id) {
$form = mysite_icon_form($type, $type_id, $icon = NULL, $name = 'mysite_icon');
return $form;
}
function mysite_icon_new_form_submit($form_id, &$form_values) {
if ($file = file_check_upload('icon')) {
mysite_icon_validate_picture($file, $form_values);
$iid = db_next_id('{mysite_icon}_iid');
db_query("INSERT INTO {mysite_icon} (iid, type, type_id, icon) VALUES ('%d', '%s', %d, '%s')", $iid, $form_values['type'], $form_values['type_id'], $form_values['icon']);
drupal_set_message(t('Icon successfully changed.'));
}
else {
form_set_error('icon', t('The image upload has failed.'));
}
}
function mysite_icon_add_form($options) {
$form = array();
$types = variable_get('mysite_content', array());
$form['intro'] = array('#value' => t('<h3>Add new MySite icon</h3><p>Select from the options below. Only content elements that do not have existing icons are shown.</p>'));
foreach ($options as $key => $value) {
$opts = array($key => $options[$key]);
array_unshift($opts, t('Select @key', array('@key' => $key)));
$form['icon_id_'. $key] = array(
'#type' => 'select',
'#title' => t('Add new %key icon', array('%key' => $key)),
'#default_value' => 0,
'#options' => $opts,
);
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Select Item and Continue'));
return $form;
}
function mysite_icon_add_form_submit($form_id, &$form_values) {
foreach ($form_values as $key => $value) {
if ($str = strstr($key, 'icon_id') && !empty($value)) {
$arg = explode('-', $value);
}
}
if (empty($arg)) {
form_set_error('icon_id', t('You must select an option.'));
}
drupal_goto('admin/settings/mysite/icons/add/'. $arg[0] .'/'. $arg[1]);
}
function mysite_icon_change() {
$folder = variable_get('mysite_icon_path', 'mysite');
$path = file_create_path(file_directory_path() .'/'. $folder);
$dir = mysite_icon_folder_check($path);
if ($dir) {
$iid = arg(5);
$icon = mysite_icon_get($iid);
if (!empty($icon->iid)) {
$name = module_invoke('mysite_type', $icon->type .'_title', $icon->type_id);
$output = '<h3>'. t('Change icon: ') . check_plain($name) .'</h3>';
$output .= drupal_get_form('mysite_icon_change_form', $icon);
}
else {
$output = t('Invalid selection.');
}
}
else {
$output = t('There is an error in the configuration of the MySite Icons upload folder.');
}
return $output;
}
function mysite_icon_change_form($icon) {
$form = mysite_icon_form($icon->type, $icon->type_id, $icon->icon, 'mysite_icon_change');
$form['iid'] = array('#type' => 'value', '#value' => $icon->iid);
return $form;
}
function mysite_icon_change_form_submit($form_id, &$form_values) {
if ($file = file_check_upload('icon')) {
mysite_icon_validate_picture($file, $form_values);
db_query("UPDATE {mysite_icon} SET icon = '%s' WHERE iid = %d", $form_values['icon'], $form_values['iid']);
drupal_set_message(t('Icon successfully changed.'));
}
else {
form_set_error('icon', t('The image upload has failed.'));
}
}
function mysite_icon_delete() {
$iid = arg(5);
$icon = mysite_icon_get($iid);
if (!empty($icon->iid)) {
$name = module_invoke('mysite_type', $icon->type .'_title', $icon->type_id);
$output = '<h3>'. t('Delete icon: ') . check_plain($name) .'</h3>';
$output .= drupal_get_form('mysite_icon_delete_form', $icon);
}
else {
$output = t('Invalid selection.');
}
return $output;
}
function mysite_icon_delete_form($icon) {
$form = array();
$form['iid'] = array('#type' => 'value', '#value' => $icon->iid);
$image = file_directory_path() .'/'. variable_get('mysite_icon_path', 'mysite') .'/'. $icon->icon;
$img = file_create_url($image);
$image_div = '<div style="float: right; margin: 12px;">'. theme_image($img, '', '', array(), FALSE) .'</div>';
$form['image'] = array('#value' => $image_div);
$form['note'] = array('#value' => t('<p>The default icon will be used if you delete this icon.</p>'));
$form['cancel'] = array('#value' => '<ul><li>'. l(t('Do not delete this icon.'), 'admin/settings/mysite/icons/browse') .'</li></ul>');
$form['submit'] = array('#type' => 'submit', '#value' => t('Delete Icon'));
return $form;
}
function mysite_icon_delete_form_submit($form_id, $form_values) {
$icon = mysite_icon_get($form_values['iid']);
$path = file_directory_path() .'/'. variable_get('mysite_icon_path', 'mysite') .'/'. $icon->icon;
if ($success = file_delete($path)) {
db_query("DELETE FROM {mysite_icon} WHERE iid = %d", $icon->iid);
drupal_set_message(t('Icon successfully deleted.'));
drupal_goto('admin/settings/mysite/icons/browse');
}
else {
form_set_error('submit', t('Failed to delete icon. Check the server permissions in your MySite icon directory.'));
}
}
function mysite_icon_get($iid) {
$icon = db_fetch_object(db_query("SELECT iid, type, type_id, icon FROM {mysite_icon} WHERE iid = %d", $iid));
return $icon;
}
function mysite_icon_lookup($type, $type_id) {
$icon = db_fetch_object(db_query("SELECT iid, type, type_id, icon FROM {mysite_icon} WHERE type = '%s' AND type_id = %d", $type, $type_id));
return $icon;
}
function mysite_icon_form($type = NULL, $type_id = NULL, $icon = NULL, $name = 'mysite_icon') {
$form = array();
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#redirect'] = referer_uri();
if ($type == 'blog' || $type == 'user') {
$form['blog'] = array('#value' => t('<p>Blog and user icons are set to be user pictures by default. If the user has no picture, or you wish to override the user picture, upload a new icon here. Changing the MySite icon will not change the user picture.</p>'));
}
$form['type'] =array('#type' => 'value', '#value' => $type);
$form['type_id'] =array('#type' => 'value', '#value' => $type_id);
list($maxwidth, $maxheight) = explode('x', variable_get('mysite_icon_dimensions', '120x60'));
if (empty($icon)) {
$default = 'icon.png';
if (!empty($type)) {
$default = 'icon-'. $type .'.png';
}
$path = drupal_get_path('module', 'mysite') .'/plugins/icons';
$image = $path .'/'. $default;
if (!file_exists(file_check_location($image, $path))) {
$image = $path .'/icon.png';
}
$image_div = theme_mysite_icon_default($image, $edit['title'], $maxwidth, $maxheight);
}
else {
$image = file_directory_path() .'/'. variable_get('mysite_icon_path', 'mysite') .'/'. $icon;
$image_div = theme_mysite_icon_custom($image, $edit['title'], $maxwidth, $maxheight);
}
$form[$name]['image'] = array(
'#value' => $image_div
);
$form[$name]['icon'] = array(
'#type' => 'file',
'#title' => t('Icon'),
'#size' => 40,
'#description' => t('Must be a .GIF, .JPG or .PNG and should be %width x %height pixels. Maximum size is %size kB.', array('%width' => $maxwidth, '%height' => $maxheight, '%size' => variable_get('mysite_icon_file_size', 32)))
);
$form[$name]['submit'] = array('#type' => 'submit', '#value' => t('Save Icon'));
return $form;
}
function mysite_icon_validate_picture($file, &$form_values, $form_element = 'icon') {
list($maxwidth, $maxheight) = explode('x', variable_get('mysite_icon_dimensions', '120x60'));
$size = variable_get('mysite_icon_file_size', 32);
$info = image_get_info($file->filepath);
if (!$info || !$info['extension']) {
form_set_error('icon', t('The uploaded file was not an image.'));
}
else if (image_get_toolkit()) {
image_scale($file->filepath, $file->filepath, $maxwidth, $maxheight);
}
else if (filesize($file->filepath) > ($size * 1000)) {
form_set_error('icon', t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => $size)));
}
else if ($info['width'] > $maxwidth || $info['height'] > $maxheight) {
form_set_error($form_element, t('The uploaded image is too large; the maximum dimensions are %width x %height pixels.', array('%width' => $maxwidth, '%height' => $maxheight)));
}
$directory = file_directory_path() .'/'. variable_get('mysite_icon_path', 'mysite');
$dir = file_check_directory($directory, $mode = 1, $form_item = 'icon');
if (!form_get_errors()) {
if ($file = file_save_upload('icon', $directory .'/'. $form_values['type'] .'-'. $form_values['type_id'] .'.'. $info['extension'], 1)) {
$form_values[$form_element] = $form_values['type'] .'-'. $form_values['type_id'] .'.'. $info['extension'];
}
else {
form_set_error($form_element, t("Failed to upload the picture image; the %directory directory doesn't exist.", array('%directory' => file_directory_path() .'/'. variable_get('mysite_icon_path', 'mysite'))));
}
}
}
function mysite_icon_file_download($file) {
$mysite = variable_get('mysite_icon_path', 'mysite');
$data = explode('/', $file);
$icon = array_pop($data);
$folder = implode('/', $data);
if ($mysite == $folder) {
$check = db_result(db_query("SELECT COUNT(iid) FROM {mysite_icon} WHERE icon = '%s'", $icon));
if ($check) {
$info = image_get_info(file_create_path($file));
return array('Content-type: '. $info['mime_type']);
}
else {
return -1;
}
}
}
function theme_mysite_icon($type, $type_id, $title, $icon) {
$output = NULL;
list($maxwidth, $maxheight) = explode('x', variable_get('mysite_icon_dimensions', '120x60'));
$path = file_directory_path() .'/'. variable_get('mysite_icon_path', 'mysite');
$title = check_plain($title);
if (!empty($icon)) {
$image = $path .'/'. $icon;
if (is_array($icon)) {
$path = $icon['path'];
$image = $icon['file'];
}
if ($file = file_check_location($image, $path)) {
$output = theme_mysite_icon_custom($image, $title, $maxwidth, $maxheight);
}
}
if (empty($output)) {
$path = drupal_get_path('module', 'mysite') .'/plugins/icons';
$image = $path .'/icon-'. $type .'.png';
if (!file_exists(file_check_location($image, $path))) {
$image = $path .'/icon.png';
}
$output = theme_mysite_icon_default($image, $title, $maxwidth, $maxheight);
}
return $output;
}
function theme_mysite_icon_add_link($type, $type_id) {
$icon = mysite_icon_lookup($type, $type_id);
if (empty($icon)) {
return l(' * ', 'admin/settings/mysite/icons/add/'. $type .'/'. $type_id, array('title' => t('Upload new icon.')));
}
else {
return l(' * ', 'admin/settings/mysite/icons/change/'. $icon->iid, array('title' => t('Change current icon.')));
}
}
function theme_mysite_icon_default($image, $title, $maxwidth = 120, $maxheight = 60) {
global $base_url;
$image = $base_url .'/'. $image;
if (strlen($title) > 32) {
$title = substr($title, 0, 32) .'...';
}
$output = '<div class="mysite-icon" style="width: '. $maxwidth .'px; height: '. $maxheight .'px; background: no-repeat url('. $image .');"><div class="mysite-icon-text">'. $title .'</div></div>';
return $output;
}
function theme_mysite_icon_custom($image, $title, $maxwidth = 120, $maxheight = 60) {
$size = @getimagesize($image);
$width = $maxwidth;
$height = $maxheight;
if ($size[0] <= $maxwidth) {
$width = $size[0];
}
if ($size[1] <= $maxheight) {
$height = $size[1];
}
$image = file_create_url($image);
$output = '<div class="mysite-icon" style="width: '. $maxwidth .'px; height: '. $maxheight .'px;">'. theme('image', $image, $title, $title, array('height' => $height, 'width' => $width), FALSE) .'</div>';
return $output;
}